chacha.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*
  2. chacha-merged.c version 20080118
  3. D. J. Bernstein
  4. Public domain.
  5. */
  6. #include <libwebsockets.h>
  7. #include "lws-ssh.h"
  8. #include <string.h>
  9. #include <stdlib.h>
  10. struct chacha_ctx {
  11. u_int input[16];
  12. };
  13. #define CHACHA_MINKEYLEN 16
  14. #define CHACHA_NONCELEN 8
  15. #define CHACHA_CTRLEN 8
  16. #define CHACHA_STATELEN (CHACHA_NONCELEN+CHACHA_CTRLEN)
  17. #define CHACHA_BLOCKLEN 64
  18. typedef unsigned char u8;
  19. typedef unsigned int u32;
  20. typedef struct chacha_ctx chacha_ctx;
  21. #define U8C(v) (v##U)
  22. #define U32C(v) (v##U)
  23. #define U8V(v) ((u8)(v) & U8C(0xFF))
  24. #define U32V(v) ((u32)(v) & U32C(0xFFFFFFFF))
  25. #define ROTL32(v, n) \
  26. (U32V((v) << (n)) | ((v) >> (32 - (n))))
  27. #define U8TO32_LITTLE(p) \
  28. (((u32)((p)[0]) ) | \
  29. ((u32)((p)[1]) << 8) | \
  30. ((u32)((p)[2]) << 16) | \
  31. ((u32)((p)[3]) << 24))
  32. #define U32TO8_LITTLE(p, v) \
  33. do { \
  34. (p)[0] = U8V((v) ); \
  35. (p)[1] = U8V((v) >> 8); \
  36. (p)[2] = U8V((v) >> 16); \
  37. (p)[3] = U8V((v) >> 24); \
  38. } while (0)
  39. #define ROTATE(v,c) (ROTL32(v,c))
  40. #define XOR(v,w) ((v) ^ (w))
  41. #define PLUS(v,w) (U32V((v) + (w)))
  42. #define PLUSONE(v) (PLUS((v),1))
  43. #define QUARTERROUND(a,b,c,d) \
  44. a = PLUS(a,b); d = ROTATE(XOR(d,a),16); \
  45. c = PLUS(c,d); b = ROTATE(XOR(b,c),12); \
  46. a = PLUS(a,b); d = ROTATE(XOR(d,a), 8); \
  47. c = PLUS(c,d); b = ROTATE(XOR(b,c), 7);
  48. static const char sigma[16] = "expand 32-byte k";
  49. static const char tau[16] = "expand 16-byte k";
  50. void
  51. chacha_keysetup(chacha_ctx *x,const u8 *k,u32 kbits)
  52. {
  53. const char *constants;
  54. x->input[4] = U8TO32_LITTLE(k + 0);
  55. x->input[5] = U8TO32_LITTLE(k + 4);
  56. x->input[6] = U8TO32_LITTLE(k + 8);
  57. x->input[7] = U8TO32_LITTLE(k + 12);
  58. if (kbits == 256) { /* recommended */
  59. k += 16;
  60. constants = sigma;
  61. } else { /* kbits == 128 */
  62. constants = tau;
  63. }
  64. x->input[8] = U8TO32_LITTLE(k + 0);
  65. x->input[9] = U8TO32_LITTLE(k + 4);
  66. x->input[10] = U8TO32_LITTLE(k + 8);
  67. x->input[11] = U8TO32_LITTLE(k + 12);
  68. x->input[0] = U8TO32_LITTLE(constants + 0);
  69. x->input[1] = U8TO32_LITTLE(constants + 4);
  70. x->input[2] = U8TO32_LITTLE(constants + 8);
  71. x->input[3] = U8TO32_LITTLE(constants + 12);
  72. }
  73. void
  74. chacha_ivsetup(chacha_ctx *x, const u8 *iv, const u8 *counter)
  75. {
  76. x->input[12] = counter == NULL ? 0 : U8TO32_LITTLE(counter + 0);
  77. x->input[13] = counter == NULL ? 0 : U8TO32_LITTLE(counter + 4);
  78. x->input[14] = U8TO32_LITTLE(iv + 0);
  79. x->input[15] = U8TO32_LITTLE(iv + 4);
  80. }
  81. void
  82. chacha_encrypt_bytes(chacha_ctx *x,const u8 *m,u8 *c,u32 bytes)
  83. {
  84. u32 x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15;
  85. u32 j0, j1, j2, j3, j4, j5, j6, j7, j8, j9, j10, j11, j12, j13, j14, j15;
  86. u8 *ctarget = NULL;
  87. u8 tmp[64];
  88. u_int i;
  89. if (!bytes) return;
  90. j0 = x->input[0];
  91. j1 = x->input[1];
  92. j2 = x->input[2];
  93. j3 = x->input[3];
  94. j4 = x->input[4];
  95. j5 = x->input[5];
  96. j6 = x->input[6];
  97. j7 = x->input[7];
  98. j8 = x->input[8];
  99. j9 = x->input[9];
  100. j10 = x->input[10];
  101. j11 = x->input[11];
  102. j12 = x->input[12];
  103. j13 = x->input[13];
  104. j14 = x->input[14];
  105. j15 = x->input[15];
  106. for (;;) {
  107. if (bytes < 64) {
  108. for (i = 0;i < bytes;++i) tmp[i] = m[i];
  109. m = tmp;
  110. ctarget = c;
  111. c = tmp;
  112. }
  113. x0 = j0;
  114. x1 = j1;
  115. x2 = j2;
  116. x3 = j3;
  117. x4 = j4;
  118. x5 = j5;
  119. x6 = j6;
  120. x7 = j7;
  121. x8 = j8;
  122. x9 = j9;
  123. x10 = j10;
  124. x11 = j11;
  125. x12 = j12;
  126. x13 = j13;
  127. x14 = j14;
  128. x15 = j15;
  129. for (i = 20;i > 0;i -= 2) {
  130. QUARTERROUND( x0, x4, x8,x12)
  131. QUARTERROUND( x1, x5, x9,x13)
  132. QUARTERROUND( x2, x6,x10,x14)
  133. QUARTERROUND( x3, x7,x11,x15)
  134. QUARTERROUND( x0, x5,x10,x15)
  135. QUARTERROUND( x1, x6,x11,x12)
  136. QUARTERROUND( x2, x7, x8,x13)
  137. QUARTERROUND( x3, x4, x9,x14)
  138. }
  139. x0 = PLUS(x0,j0);
  140. x1 = PLUS(x1,j1);
  141. x2 = PLUS(x2,j2);
  142. x3 = PLUS(x3,j3);
  143. x4 = PLUS(x4,j4);
  144. x5 = PLUS(x5,j5);
  145. x6 = PLUS(x6,j6);
  146. x7 = PLUS(x7,j7);
  147. x8 = PLUS(x8,j8);
  148. x9 = PLUS(x9,j9);
  149. x10 = PLUS(x10,j10);
  150. x11 = PLUS(x11,j11);
  151. x12 = PLUS(x12,j12);
  152. x13 = PLUS(x13,j13);
  153. x14 = PLUS(x14,j14);
  154. x15 = PLUS(x15,j15);
  155. x0 = XOR(x0,U8TO32_LITTLE(m + 0));
  156. x1 = XOR(x1,U8TO32_LITTLE(m + 4));
  157. x2 = XOR(x2,U8TO32_LITTLE(m + 8));
  158. x3 = XOR(x3,U8TO32_LITTLE(m + 12));
  159. x4 = XOR(x4,U8TO32_LITTLE(m + 16));
  160. x5 = XOR(x5,U8TO32_LITTLE(m + 20));
  161. x6 = XOR(x6,U8TO32_LITTLE(m + 24));
  162. x7 = XOR(x7,U8TO32_LITTLE(m + 28));
  163. x8 = XOR(x8,U8TO32_LITTLE(m + 32));
  164. x9 = XOR(x9,U8TO32_LITTLE(m + 36));
  165. x10 = XOR(x10,U8TO32_LITTLE(m + 40));
  166. x11 = XOR(x11,U8TO32_LITTLE(m + 44));
  167. x12 = XOR(x12,U8TO32_LITTLE(m + 48));
  168. x13 = XOR(x13,U8TO32_LITTLE(m + 52));
  169. x14 = XOR(x14,U8TO32_LITTLE(m + 56));
  170. x15 = XOR(x15,U8TO32_LITTLE(m + 60));
  171. j12 = PLUSONE(j12);
  172. if (!j12)
  173. j13 = PLUSONE(j13);
  174. /* stopping at 2^70 bytes per nonce is user's responsibility */
  175. U32TO8_LITTLE(c + 0,x0);
  176. U32TO8_LITTLE(c + 4,x1);
  177. U32TO8_LITTLE(c + 8,x2);
  178. U32TO8_LITTLE(c + 12,x3);
  179. U32TO8_LITTLE(c + 16,x4);
  180. U32TO8_LITTLE(c + 20,x5);
  181. U32TO8_LITTLE(c + 24,x6);
  182. U32TO8_LITTLE(c + 28,x7);
  183. U32TO8_LITTLE(c + 32,x8);
  184. U32TO8_LITTLE(c + 36,x9);
  185. U32TO8_LITTLE(c + 40,x10);
  186. U32TO8_LITTLE(c + 44,x11);
  187. U32TO8_LITTLE(c + 48,x12);
  188. U32TO8_LITTLE(c + 52,x13);
  189. U32TO8_LITTLE(c + 56,x14);
  190. U32TO8_LITTLE(c + 60,x15);
  191. if (bytes <= 64) {
  192. if (bytes < 64) {
  193. for (i = 0;i < bytes;++i) ctarget[i] = c[i];
  194. }
  195. x->input[12] = j12;
  196. x->input[13] = j13;
  197. return;
  198. }
  199. bytes -= 64;
  200. c += 64;
  201. m += 64;
  202. }
  203. }
  204. struct lws_cipher_chacha {
  205. struct chacha_ctx ccctx[2];
  206. };
  207. #define K_1(_keys) &((struct lws_cipher_chacha *)_keys->cipher)->ccctx[0]
  208. #define K_2(_keys) &((struct lws_cipher_chacha *)_keys->cipher)->ccctx[1]
  209. int
  210. lws_chacha_activate(struct lws_ssh_keys *keys)
  211. {
  212. if (keys->cipher) {
  213. free(keys->cipher);
  214. keys->cipher = NULL;
  215. }
  216. keys->cipher = malloc(sizeof(struct lws_cipher_chacha));
  217. if (!keys->cipher)
  218. return 1;
  219. memset(keys->cipher, 0, sizeof(struct lws_cipher_chacha));
  220. /* uses 2 x 256-bit keys, so 512 bits (64 bytes) needed */
  221. chacha_keysetup(K_2(keys), keys->key[SSH_KEYIDX_ENC], 256);
  222. chacha_keysetup(K_1(keys), &keys->key[SSH_KEYIDX_ENC][32], 256);
  223. keys->valid = 1;
  224. keys->full_length = 1;
  225. keys->padding_alignment = 8; // CHACHA_BLOCKLEN;
  226. keys->MAC_length = POLY1305_TAGLEN;
  227. return 0;
  228. }
  229. void
  230. lws_chacha_destroy(struct lws_ssh_keys *keys)
  231. {
  232. if (keys->cipher) {
  233. free(keys->cipher);
  234. keys->cipher = NULL;
  235. }
  236. }
  237. uint32_t
  238. lws_chachapoly_get_length(struct lws_ssh_keys *keys, uint32_t seq,
  239. const uint8_t *in4)
  240. {
  241. uint8_t buf[4], seqbuf[8];
  242. /*
  243. * When receiving a packet, the length must be decrypted first. When 4
  244. * bytes of ciphertext length have been received, they may be decrypted
  245. * using the K_1 key, a nonce consisting of the packet sequence number
  246. * encoded as a uint64 under the usual SSH wire encoding and a zero
  247. * block counter to obtain the plaintext length.
  248. */
  249. POKE_U64(seqbuf, seq);
  250. chacha_ivsetup(K_1(keys), seqbuf, NULL);
  251. chacha_encrypt_bytes(K_1(keys), in4, buf, 4);
  252. return PEEK_U32(buf);
  253. }
  254. /*
  255. * chachapoly_crypt() operates as following:
  256. * En/decrypt with header key 'aadlen' bytes from 'src', storing result
  257. * to 'dest'. The ciphertext here is treated as additional authenticated
  258. * data for MAC calculation.
  259. * En/decrypt 'len' bytes at offset 'aadlen' from 'src' to 'dest'. Use
  260. * POLY1305_TAGLEN bytes at offset 'len'+'aadlen' as the authentication
  261. * tag. This tag is written on encryption and verified on decryption.
  262. */
  263. int
  264. chachapoly_crypt(struct lws_ssh_keys *keys, u_int seqnr, u_char *dest,
  265. const u_char *src, u_int len, u_int aadlen, u_int authlen, int do_encrypt)
  266. {
  267. u_char seqbuf[8];
  268. const u_char one[8] = { 1, 0, 0, 0, 0, 0, 0, 0 }; /* NB little-endian */
  269. u_char expected_tag[POLY1305_TAGLEN], poly_key[POLY1305_KEYLEN];
  270. int r = 1;
  271. /*
  272. * Run ChaCha20 once to generate the Poly1305 key. The IV is the
  273. * packet sequence number.
  274. */
  275. memset(poly_key, 0, sizeof(poly_key));
  276. POKE_U64(seqbuf, seqnr);
  277. chacha_ivsetup(K_2(keys), seqbuf, NULL);
  278. chacha_encrypt_bytes(K_2(keys),
  279. poly_key, poly_key, sizeof(poly_key));
  280. /* If decrypting, check tag before anything else */
  281. if (!do_encrypt) {
  282. const u_char *tag = src + aadlen + len;
  283. poly1305_auth(expected_tag, src, aadlen + len, poly_key);
  284. if (lws_timingsafe_bcmp(expected_tag, tag, POLY1305_TAGLEN)) {
  285. r = 2;
  286. goto out;
  287. }
  288. }
  289. /* Crypt additional data */
  290. if (aadlen) {
  291. chacha_ivsetup(K_1(keys), seqbuf, NULL);
  292. chacha_encrypt_bytes(K_1(keys), src, dest, aadlen);
  293. }
  294. /* Set Chacha's block counter to 1 */
  295. chacha_ivsetup(K_2(keys), seqbuf, one);
  296. chacha_encrypt_bytes(K_2(keys), src + aadlen, dest + aadlen, len);
  297. /* If encrypting, calculate and append tag */
  298. if (do_encrypt) {
  299. poly1305_auth(dest + aadlen + len, dest, aadlen + len,
  300. poly_key);
  301. }
  302. r = 0;
  303. out:
  304. lws_explicit_bzero(expected_tag, sizeof(expected_tag));
  305. lws_explicit_bzero(seqbuf, sizeof(seqbuf));
  306. lws_explicit_bzero(poly_key, sizeof(poly_key));
  307. return r;
  308. }
  309. int
  310. lws_chacha_decrypt(struct lws_ssh_keys *keys, uint32_t seq,
  311. const uint8_t *ct, uint32_t len, uint8_t *pt)
  312. {
  313. return chachapoly_crypt(keys, seq, pt, ct, len - POLY1305_TAGLEN - 4, 4,
  314. POLY1305_TAGLEN, 0);
  315. }
  316. int
  317. lws_chacha_encrypt(struct lws_ssh_keys *keys, uint32_t seq,
  318. const uint8_t *ct, uint32_t len, uint8_t *pt)
  319. {
  320. return chachapoly_crypt(keys, seq, pt, ct, len - 4, 4, 0, 1);
  321. }