chacha_private.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. chacha-merged.c version 20080118
  3. D. J. Bernstein
  4. Public domain.
  5. */
  6. /* $OpenBSD: chacha_private.h,v 1.2 2013/10/04 07:02:27 djm Exp $ */
  7. typedef unsigned char u8;
  8. typedef unsigned int u32;
  9. typedef struct
  10. {
  11. u32 input[16]; /* could be compressed */
  12. } chacha_ctx;
  13. #define U8C(v) (v##U)
  14. #define U32C(v) (v##U)
  15. #define U8V(v) ((u8)(v) & U8C(0xFF))
  16. #define U32V(v) ((u32)(v) & U32C(0xFFFFFFFF))
  17. #define ROTL32(v, n) \
  18. (U32V((v) << (n)) | ((v) >> (32 - (n))))
  19. #define U8TO32_LITTLE(p) \
  20. (((u32)((p)[0]) ) | \
  21. ((u32)((p)[1]) << 8) | \
  22. ((u32)((p)[2]) << 16) | \
  23. ((u32)((p)[3]) << 24))
  24. #define U32TO8_LITTLE(p, v) \
  25. do { \
  26. (p)[0] = U8V((v) ); \
  27. (p)[1] = U8V((v) >> 8); \
  28. (p)[2] = U8V((v) >> 16); \
  29. (p)[3] = U8V((v) >> 24); \
  30. } while (0)
  31. #define ROTATE(v,c) (ROTL32(v,c))
  32. #define XOR(v,w) ((v) ^ (w))
  33. #define PLUS(v,w) (U32V((v) + (w)))
  34. #define PLUSONE(v) (PLUS((v),1))
  35. #define QUARTERROUND(a,b,c,d) \
  36. a = PLUS(a,b); d = ROTATE(XOR(d,a),16); \
  37. c = PLUS(c,d); b = ROTATE(XOR(b,c),12); \
  38. a = PLUS(a,b); d = ROTATE(XOR(d,a), 8); \
  39. c = PLUS(c,d); b = ROTATE(XOR(b,c), 7);
  40. static const char sigma[16] = "expand 32-byte k";
  41. static const char tau[16] = "expand 16-byte k";
  42. static void
  43. chacha_keysetup(chacha_ctx *x,const u8 *k,u32 kbits,u32 ivbits)
  44. {
  45. const char *constants;
  46. x->input[4] = U8TO32_LITTLE(k + 0);
  47. x->input[5] = U8TO32_LITTLE(k + 4);
  48. x->input[6] = U8TO32_LITTLE(k + 8);
  49. x->input[7] = U8TO32_LITTLE(k + 12);
  50. if (kbits == 256) { /* recommended */
  51. k += 16;
  52. constants = sigma;
  53. } else { /* kbits == 128 */
  54. constants = tau;
  55. }
  56. x->input[8] = U8TO32_LITTLE(k + 0);
  57. x->input[9] = U8TO32_LITTLE(k + 4);
  58. x->input[10] = U8TO32_LITTLE(k + 8);
  59. x->input[11] = U8TO32_LITTLE(k + 12);
  60. x->input[0] = U8TO32_LITTLE(constants + 0);
  61. x->input[1] = U8TO32_LITTLE(constants + 4);
  62. x->input[2] = U8TO32_LITTLE(constants + 8);
  63. x->input[3] = U8TO32_LITTLE(constants + 12);
  64. }
  65. static void
  66. chacha_ivsetup(chacha_ctx *x,const u8 *iv)
  67. {
  68. x->input[12] = 0;
  69. x->input[13] = 0;
  70. x->input[14] = U8TO32_LITTLE(iv + 0);
  71. x->input[15] = U8TO32_LITTLE(iv + 4);
  72. }
  73. static void
  74. chacha_encrypt_bytes(chacha_ctx *x,const u8 *m,u8 *c,u32 bytes)
  75. {
  76. u32 x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15;
  77. u32 j0, j1, j2, j3, j4, j5, j6, j7, j8, j9, j10, j11, j12, j13, j14, j15;
  78. u8 *ctarget = NULL;
  79. u8 tmp[64];
  80. u_int i;
  81. if (!bytes) return;
  82. j0 = x->input[0];
  83. j1 = x->input[1];
  84. j2 = x->input[2];
  85. j3 = x->input[3];
  86. j4 = x->input[4];
  87. j5 = x->input[5];
  88. j6 = x->input[6];
  89. j7 = x->input[7];
  90. j8 = x->input[8];
  91. j9 = x->input[9];
  92. j10 = x->input[10];
  93. j11 = x->input[11];
  94. j12 = x->input[12];
  95. j13 = x->input[13];
  96. j14 = x->input[14];
  97. j15 = x->input[15];
  98. for (;;) {
  99. if (bytes < 64) {
  100. for (i = 0;i < bytes;++i) tmp[i] = m[i];
  101. m = tmp;
  102. ctarget = c;
  103. c = tmp;
  104. }
  105. x0 = j0;
  106. x1 = j1;
  107. x2 = j2;
  108. x3 = j3;
  109. x4 = j4;
  110. x5 = j5;
  111. x6 = j6;
  112. x7 = j7;
  113. x8 = j8;
  114. x9 = j9;
  115. x10 = j10;
  116. x11 = j11;
  117. x12 = j12;
  118. x13 = j13;
  119. x14 = j14;
  120. x15 = j15;
  121. for (i = 20;i > 0;i -= 2) {
  122. QUARTERROUND( x0, x4, x8,x12)
  123. QUARTERROUND( x1, x5, x9,x13)
  124. QUARTERROUND( x2, x6,x10,x14)
  125. QUARTERROUND( x3, x7,x11,x15)
  126. QUARTERROUND( x0, x5,x10,x15)
  127. QUARTERROUND( x1, x6,x11,x12)
  128. QUARTERROUND( x2, x7, x8,x13)
  129. QUARTERROUND( x3, x4, x9,x14)
  130. }
  131. x0 = PLUS(x0,j0);
  132. x1 = PLUS(x1,j1);
  133. x2 = PLUS(x2,j2);
  134. x3 = PLUS(x3,j3);
  135. x4 = PLUS(x4,j4);
  136. x5 = PLUS(x5,j5);
  137. x6 = PLUS(x6,j6);
  138. x7 = PLUS(x7,j7);
  139. x8 = PLUS(x8,j8);
  140. x9 = PLUS(x9,j9);
  141. x10 = PLUS(x10,j10);
  142. x11 = PLUS(x11,j11);
  143. x12 = PLUS(x12,j12);
  144. x13 = PLUS(x13,j13);
  145. x14 = PLUS(x14,j14);
  146. x15 = PLUS(x15,j15);
  147. #ifndef KEYSTREAM_ONLY
  148. x0 = XOR(x0,U8TO32_LITTLE(m + 0));
  149. x1 = XOR(x1,U8TO32_LITTLE(m + 4));
  150. x2 = XOR(x2,U8TO32_LITTLE(m + 8));
  151. x3 = XOR(x3,U8TO32_LITTLE(m + 12));
  152. x4 = XOR(x4,U8TO32_LITTLE(m + 16));
  153. x5 = XOR(x5,U8TO32_LITTLE(m + 20));
  154. x6 = XOR(x6,U8TO32_LITTLE(m + 24));
  155. x7 = XOR(x7,U8TO32_LITTLE(m + 28));
  156. x8 = XOR(x8,U8TO32_LITTLE(m + 32));
  157. x9 = XOR(x9,U8TO32_LITTLE(m + 36));
  158. x10 = XOR(x10,U8TO32_LITTLE(m + 40));
  159. x11 = XOR(x11,U8TO32_LITTLE(m + 44));
  160. x12 = XOR(x12,U8TO32_LITTLE(m + 48));
  161. x13 = XOR(x13,U8TO32_LITTLE(m + 52));
  162. x14 = XOR(x14,U8TO32_LITTLE(m + 56));
  163. x15 = XOR(x15,U8TO32_LITTLE(m + 60));
  164. #endif
  165. j12 = PLUSONE(j12);
  166. if (!j12) {
  167. j13 = PLUSONE(j13);
  168. /* stopping at 2^70 bytes per nonce is user's responsibility */
  169. }
  170. U32TO8_LITTLE(c + 0,x0);
  171. U32TO8_LITTLE(c + 4,x1);
  172. U32TO8_LITTLE(c + 8,x2);
  173. U32TO8_LITTLE(c + 12,x3);
  174. U32TO8_LITTLE(c + 16,x4);
  175. U32TO8_LITTLE(c + 20,x5);
  176. U32TO8_LITTLE(c + 24,x6);
  177. U32TO8_LITTLE(c + 28,x7);
  178. U32TO8_LITTLE(c + 32,x8);
  179. U32TO8_LITTLE(c + 36,x9);
  180. U32TO8_LITTLE(c + 40,x10);
  181. U32TO8_LITTLE(c + 44,x11);
  182. U32TO8_LITTLE(c + 48,x12);
  183. U32TO8_LITTLE(c + 52,x13);
  184. U32TO8_LITTLE(c + 56,x14);
  185. U32TO8_LITTLE(c + 60,x15);
  186. if (bytes <= 64) {
  187. if (bytes < 64) {
  188. for (i = 0;i < bytes;++i) ctarget[i] = c[i];
  189. }
  190. x->input[12] = j12;
  191. x->input[13] = j13;
  192. return;
  193. }
  194. bytes -= 64;
  195. c += 64;
  196. #ifndef KEYSTREAM_ONLY
  197. m += 64;
  198. #endif
  199. }
  200. }