ecrypt-sync.h 7.7 KB

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