ecrypt_sync.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. #define ECRYPT_VARIANT 1
  2. #define ECRYPT_API
  3. /* ecrypt_sync.h */
  4. /*
  5. * Header file for synchronous stream ciphers without authentication
  6. * mechanism.
  7. *
  8. * *** Please only edit parts marked with "[edit]". ***
  9. */
  10. #ifndef ECRYPT_SYNC
  11. #define ECRYPT_SYNC
  12. #include "ecrypt_types.h"
  13. /* ------------------------------------------------------------------------- */
  14. /* Cipher parameters */
  15. /*
  16. * The name of your cipher.
  17. */
  18. #define ECRYPT_NAME "ChaCha20"
  19. #define ECRYPT_PROFILE "_____"
  20. /*
  21. * Specify which key and IV sizes are supported by your cipher. A user
  22. * should be able to enumerate the supported sizes by running the
  23. * following code:
  24. *
  25. * for (i = 0; ECRYPT_KEYSIZE(i) <= ECRYPT_MAXKEYSIZE; ++i)
  26. * {
  27. * keysize = ECRYPT_KEYSIZE(i);
  28. *
  29. * ...
  30. * }
  31. *
  32. * All sizes are in bits.
  33. */
  34. #define ECRYPT_MAXKEYSIZE 256 /* [edit] */
  35. #define ECRYPT_KEYSIZE(i) (128 + (i)*128) /* [edit] */
  36. #define ECRYPT_MAXIVSIZE 64 /* [edit] */
  37. #define ECRYPT_IVSIZE(i) (64 + (i)*64) /* [edit] */
  38. /* ------------------------------------------------------------------------- */
  39. /* Data structures */
  40. /*
  41. * ECRYPT_ctx is the structure containing the representation of the
  42. * internal state of your cipher.
  43. */
  44. typedef struct {
  45. u32 input[16]; /* could be compressed */
  46. /*
  47. * [edit]
  48. *
  49. * Put here all state variable needed during the encryption process.
  50. */
  51. } ECRYPT_ctx;
  52. /* ------------------------------------------------------------------------- */
  53. /* Mandatory functions */
  54. /*
  55. * Key and message independent initialization. This function will be
  56. * called once when the program starts (e.g., to build expanded S-box
  57. * tables).
  58. */
  59. void ECRYPT_init(void);
  60. /*
  61. * Key setup. It is the user's responsibility to select the values of
  62. * keysize and ivsize from the set of supported values specified
  63. * above.
  64. */
  65. void ECRYPT_keysetup(
  66. ECRYPT_ctx* ctx,
  67. const u8* key,
  68. u32 keysize, /* Key size in bits. */
  69. u32 ivsize); /* IV size in bits. */
  70. /*
  71. * IV setup. After having called ECRYPT_keysetup(), the user is
  72. * allowed to call ECRYPT_ivsetup() different times in order to
  73. * encrypt/decrypt different messages with the same key but different
  74. * IV's. ECRYPT_ivsetup() also sets block counter to zero.
  75. */
  76. void ECRYPT_ivsetup(ECRYPT_ctx* ctx, const u8* iv);
  77. /*
  78. * Block counter setup. It is used only for special purposes,
  79. * since block counter is usually initialized with ECRYPT_ivsetup.
  80. * ECRYPT_ctrsetup has to be called after ECRYPT_ivsetup.
  81. */
  82. void ECRYPT_ctrsetup(ECRYPT_ctx* ctx, const u8* ctr);
  83. /*
  84. * Encryption/decryption of arbitrary length messages.
  85. *
  86. * For efficiency reasons, the API provides two types of
  87. * encrypt/decrypt functions. The ECRYPT_encrypt_bytes() function
  88. * (declared here) encrypts byte strings of arbitrary length, while
  89. * the ECRYPT_encrypt_blocks() function (defined later) only accepts
  90. * lengths which are multiples of ECRYPT_BLOCKLENGTH.
  91. *
  92. * The user is allowed to make multiple calls to
  93. * ECRYPT_encrypt_blocks() to incrementally encrypt a long message,
  94. * but he is NOT allowed to make additional encryption calls once he
  95. * has called ECRYPT_encrypt_bytes() (unless he starts a new message
  96. * of course). For example, this sequence of calls is acceptable:
  97. *
  98. * ECRYPT_keysetup();
  99. *
  100. * ECRYPT_ivsetup();
  101. * ECRYPT_encrypt_blocks();
  102. * ECRYPT_encrypt_blocks();
  103. * ECRYPT_encrypt_bytes();
  104. *
  105. * ECRYPT_ivsetup();
  106. * ECRYPT_encrypt_blocks();
  107. * ECRYPT_encrypt_blocks();
  108. *
  109. * ECRYPT_ivsetup();
  110. * ECRYPT_encrypt_bytes();
  111. *
  112. * The following sequence is not:
  113. *
  114. * ECRYPT_keysetup();
  115. * ECRYPT_ivsetup();
  116. * ECRYPT_encrypt_blocks();
  117. * ECRYPT_encrypt_bytes();
  118. * ECRYPT_encrypt_blocks();
  119. */
  120. void ECRYPT_encrypt_bytes(
  121. ECRYPT_ctx* ctx,
  122. const u8* plaintext,
  123. u8* ciphertext,
  124. u32 msglen); /* Message length in bytes. */
  125. void ECRYPT_decrypt_bytes(
  126. ECRYPT_ctx* ctx,
  127. const u8* ciphertext,
  128. u8* plaintext,
  129. u32 msglen); /* Message length in bytes. */
  130. /* ------------------------------------------------------------------------- */
  131. /* Optional features */
  132. /*
  133. * For testing purposes it can sometimes be useful to have a function
  134. * which immediately generates keystream without having to provide it
  135. * with a zero plaintext. If your cipher cannot provide this function
  136. * (e.g., because it is not strictly a synchronous cipher), please
  137. * reset the ECRYPT_GENERATES_KEYSTREAM flag.
  138. */
  139. #define ECRYPT_GENERATES_KEYSTREAM
  140. #ifdef ECRYPT_GENERATES_KEYSTREAM
  141. void ECRYPT_keystream_bytes(
  142. ECRYPT_ctx* ctx,
  143. u8* keystream,
  144. u32 length); /* Length of keystream in bytes. */
  145. #endif
  146. /* ------------------------------------------------------------------------- */
  147. /* Optional optimizations */
  148. /*
  149. * By default, the functions in this section are implemented using
  150. * calls to functions declared above. However, you might want to
  151. * implement them differently for performance reasons.
  152. */
  153. /*
  154. * All-in-one encryption/decryption of (short) packets.
  155. *
  156. * The default definitions of these functions can be found in
  157. * "ecrypt-sync.c". If you want to implement them differently, please
  158. * undef the ECRYPT_USES_DEFAULT_ALL_IN_ONE flag.
  159. */
  160. #define ECRYPT_USES_DEFAULT_ALL_IN_ONE /* [edit] */
  161. void ECRYPT_encrypt_packet(
  162. ECRYPT_ctx* ctx,
  163. const u8* iv,
  164. const u8* plaintext,
  165. u8* ciphertext,
  166. u32 msglen);
  167. void ECRYPT_decrypt_packet(
  168. ECRYPT_ctx* ctx,
  169. const u8* iv,
  170. const u8* ciphertext,
  171. u8* plaintext,
  172. u32 msglen);
  173. /*
  174. * Encryption/decryption of blocks.
  175. *
  176. * By default, these functions are defined as macros. If you want to
  177. * provide a different implementation, please undef the
  178. * ECRYPT_USES_DEFAULT_BLOCK_MACROS flag and implement the functions
  179. * declared below.
  180. */
  181. #define ECRYPT_BLOCKLENGTH 64 /* [edit] */
  182. #define ECRYPT_USES_DEFAULT_BLOCK_MACROS /* [edit] */
  183. #ifdef ECRYPT_USES_DEFAULT_BLOCK_MACROS
  184. #define ECRYPT_encrypt_blocks(ctx, plaintext, ciphertext, blocks) \
  185. ECRYPT_encrypt_bytes(ctx, plaintext, ciphertext, (blocks)*ECRYPT_BLOCKLENGTH)
  186. #define ECRYPT_decrypt_blocks(ctx, ciphertext, plaintext, blocks) \
  187. ECRYPT_decrypt_bytes(ctx, ciphertext, plaintext, (blocks)*ECRYPT_BLOCKLENGTH)
  188. #ifdef ECRYPT_GENERATES_KEYSTREAM
  189. #define ECRYPT_keystream_blocks(ctx, keystream, blocks) \
  190. ECRYPT_keystream_bytes(ctx, keystream, (blocks)*ECRYPT_BLOCKLENGTH)
  191. #endif
  192. #else
  193. void ECRYPT_encrypt_blocks(
  194. ECRYPT_ctx* ctx,
  195. const u8* plaintext,
  196. u8* ciphertext,
  197. u32 blocks); /* Message length in blocks. */
  198. void ECRYPT_decrypt_blocks(
  199. ECRYPT_ctx* ctx,
  200. const u8* ciphertext,
  201. u8* plaintext,
  202. u32 blocks); /* Message length in blocks. */
  203. #ifdef ECRYPT_GENERATES_KEYSTREAM
  204. void ECRYPT_keystream_blocks(
  205. ECRYPT_ctx* ctx,
  206. const u8* keystream,
  207. u32 blocks); /* Keystream length in blocks. */
  208. #endif
  209. #endif
  210. /*
  211. * If your cipher can be implemented in different ways, you can use
  212. * the ECRYPT_VARIANT parameter to allow the user to choose between
  213. * them at compile time (e.g., gcc -DECRYPT_VARIANT=3 ...). Please
  214. * only use this possibility if you really think it could make a
  215. * significant difference and keep the number of variants
  216. * (ECRYPT_MAXVARIANT) as small as possible (definitely not more than
  217. * 10). Note also that all variants should have exactly the same
  218. * external interface (i.e., the same ECRYPT_BLOCKLENGTH, etc.).
  219. */
  220. #define ECRYPT_MAXVARIANT 1 /* [edit] */
  221. #ifndef ECRYPT_VARIANT
  222. #define ECRYPT_VARIANT 1
  223. #endif
  224. #if(ECRYPT_VARIANT > ECRYPT_MAXVARIANT)
  225. #error this variant does not exist
  226. #endif
  227. /* ------------------------------------------------------------------------- */
  228. #endif