chacha.c 5.3 KB

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