chacha20.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. chacha-merged.c version 20080118
  3. D. J. Bernstein
  4. Public domain.
  5. */
  6. #include <stdint.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include "utils.h"
  10. #include "chacha20.h"
  11. struct chacha_ctx {
  12. uint32_t input[16];
  13. };
  14. #define LOAD32_LE(SRC) get_unaligned_le32(SRC)
  15. #define STORE32_LE(DST, W) store32_le((DST), (W))
  16. static inline void
  17. store32_le(uint8_t dst[4], uint32_t w)
  18. {
  19. dst[0] = (uint8_t) w; w >>= 8;
  20. dst[1] = (uint8_t) w; w >>= 8;
  21. dst[2] = (uint8_t) w; w >>= 8;
  22. dst[3] = (uint8_t) w;
  23. }
  24. #define ROTL32(X, B) rotl32((X), (B))
  25. static inline uint32_t
  26. rotl32(const uint32_t x, const int b)
  27. {
  28. return (x << b) | (x >> (32 - b));
  29. }
  30. typedef struct chacha_ctx chacha_ctx;
  31. #define U32C(v) (v##U)
  32. #define U32V(v) ((uint32_t)(v) &U32C(0xFFFFFFFF))
  33. #define ROTATE(v, c) (ROTL32(v, c))
  34. #define XOR(v, w) ((v) ^ (w))
  35. #define PLUS(v, w) (U32V((v) + (w)))
  36. #define PLUSONE(v) (PLUS((v), 1))
  37. #define QUARTERROUND(a, b, c, d) \
  38. a = PLUS(a, b); \
  39. d = ROTATE(XOR(d, a), 16); \
  40. c = PLUS(c, d); \
  41. b = ROTATE(XOR(b, c), 12); \
  42. a = PLUS(a, b); \
  43. d = ROTATE(XOR(d, a), 8); \
  44. c = PLUS(c, d); \
  45. b = ROTATE(XOR(b, c), 7);
  46. static void
  47. chacha_keysetup(chacha_ctx *ctx, const uint8_t *k)
  48. {
  49. ctx->input[0] = U32C(0x61707865);
  50. ctx->input[1] = U32C(0x3320646e);
  51. ctx->input[2] = U32C(0x79622d32);
  52. ctx->input[3] = U32C(0x6b206574);
  53. ctx->input[4] = LOAD32_LE(k + 0);
  54. ctx->input[5] = LOAD32_LE(k + 4);
  55. ctx->input[6] = LOAD32_LE(k + 8);
  56. ctx->input[7] = LOAD32_LE(k + 12);
  57. ctx->input[8] = LOAD32_LE(k + 16);
  58. ctx->input[9] = LOAD32_LE(k + 20);
  59. ctx->input[10] = LOAD32_LE(k + 24);
  60. ctx->input[11] = LOAD32_LE(k + 28);
  61. }
  62. static void
  63. chacha_ivsetup(chacha_ctx *ctx, const uint8_t *iv, const uint8_t *counter)
  64. {
  65. ctx->input[12] = counter == NULL ? 0 : LOAD32_LE(counter + 0);
  66. ctx->input[13] = counter == NULL ? 0 : LOAD32_LE(counter + 4);
  67. ctx->input[14] = LOAD32_LE(iv + 0);
  68. ctx->input[15] = LOAD32_LE(iv + 4);
  69. }
  70. static void
  71. chacha20_encrypt_bytes(chacha_ctx *ctx, const uint8_t *m, uint8_t *c,
  72. unsigned long long bytes)
  73. {
  74. uint32_t x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14,
  75. x15;
  76. uint32_t j0, j1, j2, j3, j4, j5, j6, j7, j8, j9, j10, j11, j12, j13, j14,
  77. j15;
  78. uint8_t *ctarget = NULL;
  79. uint8_t tmp[64];
  80. unsigned int i;
  81. if (!bytes) {
  82. return; /* LCOV_EXCL_LINE */
  83. }
  84. j0 = ctx->input[0];
  85. j1 = ctx->input[1];
  86. j2 = ctx->input[2];
  87. j3 = ctx->input[3];
  88. j4 = ctx->input[4];
  89. j5 = ctx->input[5];
  90. j6 = ctx->input[6];
  91. j7 = ctx->input[7];
  92. j8 = ctx->input[8];
  93. j9 = ctx->input[9];
  94. j10 = ctx->input[10];
  95. j11 = ctx->input[11];
  96. j12 = ctx->input[12];
  97. j13 = ctx->input[13];
  98. j14 = ctx->input[14];
  99. j15 = ctx->input[15];
  100. for (;;) {
  101. if (bytes < 64) {
  102. memset(tmp, 0, 64);
  103. for (i = 0; i < bytes; ++i) {
  104. tmp[i] = m[i];
  105. }
  106. m = tmp;
  107. ctarget = c;
  108. c = tmp;
  109. }
  110. x0 = j0;
  111. x1 = j1;
  112. x2 = j2;
  113. x3 = j3;
  114. x4 = j4;
  115. x5 = j5;
  116. x6 = j6;
  117. x7 = j7;
  118. x8 = j8;
  119. x9 = j9;
  120. x10 = j10;
  121. x11 = j11;
  122. x12 = j12;
  123. x13 = j13;
  124. x14 = j14;
  125. x15 = j15;
  126. for (i = 20; i > 0; i -= 2) {
  127. QUARTERROUND(x0, x4, x8, x12)
  128. QUARTERROUND(x1, x5, x9, x13)
  129. QUARTERROUND(x2, x6, x10, x14)
  130. QUARTERROUND(x3, x7, x11, x15)
  131. QUARTERROUND(x0, x5, x10, x15)
  132. QUARTERROUND(x1, x6, x11, x12)
  133. QUARTERROUND(x2, x7, x8, x13)
  134. QUARTERROUND(x3, x4, x9, x14)
  135. }
  136. x0 = PLUS(x0, j0);
  137. x1 = PLUS(x1, j1);
  138. x2 = PLUS(x2, j2);
  139. x3 = PLUS(x3, j3);
  140. x4 = PLUS(x4, j4);
  141. x5 = PLUS(x5, j5);
  142. x6 = PLUS(x6, j6);
  143. x7 = PLUS(x7, j7);
  144. x8 = PLUS(x8, j8);
  145. x9 = PLUS(x9, j9);
  146. x10 = PLUS(x10, j10);
  147. x11 = PLUS(x11, j11);
  148. x12 = PLUS(x12, j12);
  149. x13 = PLUS(x13, j13);
  150. x14 = PLUS(x14, j14);
  151. x15 = PLUS(x15, j15);
  152. x0 = XOR(x0, LOAD32_LE(m + 0));
  153. x1 = XOR(x1, LOAD32_LE(m + 4));
  154. x2 = XOR(x2, LOAD32_LE(m + 8));
  155. x3 = XOR(x3, LOAD32_LE(m + 12));
  156. x4 = XOR(x4, LOAD32_LE(m + 16));
  157. x5 = XOR(x5, LOAD32_LE(m + 20));
  158. x6 = XOR(x6, LOAD32_LE(m + 24));
  159. x7 = XOR(x7, LOAD32_LE(m + 28));
  160. x8 = XOR(x8, LOAD32_LE(m + 32));
  161. x9 = XOR(x9, LOAD32_LE(m + 36));
  162. x10 = XOR(x10, LOAD32_LE(m + 40));
  163. x11 = XOR(x11, LOAD32_LE(m + 44));
  164. x12 = XOR(x12, LOAD32_LE(m + 48));
  165. x13 = XOR(x13, LOAD32_LE(m + 52));
  166. x14 = XOR(x14, LOAD32_LE(m + 56));
  167. x15 = XOR(x15, LOAD32_LE(m + 60));
  168. j12 = PLUSONE(j12);
  169. /* LCOV_EXCL_START */
  170. if (!j12) {
  171. j13 = PLUSONE(j13);
  172. }
  173. /* LCOV_EXCL_STOP */
  174. STORE32_LE(c + 0, x0);
  175. STORE32_LE(c + 4, x1);
  176. STORE32_LE(c + 8, x2);
  177. STORE32_LE(c + 12, x3);
  178. STORE32_LE(c + 16, x4);
  179. STORE32_LE(c + 20, x5);
  180. STORE32_LE(c + 24, x6);
  181. STORE32_LE(c + 28, x7);
  182. STORE32_LE(c + 32, x8);
  183. STORE32_LE(c + 36, x9);
  184. STORE32_LE(c + 40, x10);
  185. STORE32_LE(c + 44, x11);
  186. STORE32_LE(c + 48, x12);
  187. STORE32_LE(c + 52, x13);
  188. STORE32_LE(c + 56, x14);
  189. STORE32_LE(c + 60, x15);
  190. if (bytes <= 64) {
  191. if (bytes < 64) {
  192. for (i = 0; i < (unsigned int) bytes; ++i) {
  193. ctarget[i] = c[i]; /* ctarget cannot be NULL */
  194. }
  195. }
  196. ctx->input[12] = j12;
  197. ctx->input[13] = j13;
  198. return;
  199. }
  200. bytes -= 64;
  201. c += 64;
  202. m += 64;
  203. }
  204. }
  205. void chacha20_encrypt_msg(void *msg, size_t len, const void *nonce, const void *key)
  206. {
  207. struct chacha_ctx ctx;
  208. chacha_keysetup(&ctx, key);
  209. chacha_ivsetup(&ctx, nonce, NULL);
  210. chacha20_encrypt_bytes(&ctx, msg, msg, len);
  211. }