chacha20-poly1305.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080
  1. /*
  2. * ChaCha20-Poly1305 Implementation for SSH-2
  3. *
  4. * Protocol spec:
  5. * http://cvsweb.openbsd.org/cgi-bin/cvsweb/src/usr.bin/ssh/PROTOCOL.chacha20poly1305?rev=1.2&content-type=text/x-cvsweb-markup
  6. *
  7. * ChaCha20 spec:
  8. * http://cr.yp.to/chacha/chacha-20080128.pdf
  9. *
  10. * Salsa20 spec:
  11. * http://cr.yp.to/snuffle/spec.pdf
  12. *
  13. * Poly1305-AES spec:
  14. * http://cr.yp.to/mac/poly1305-20050329.pdf
  15. *
  16. * The nonce for the Poly1305 is the second part of the key output
  17. * from the first round of ChaCha20. This removes the AES requirement.
  18. * This is undocumented!
  19. *
  20. * This has an intricate link between the cipher and the MAC. The
  21. * keying of both is done in by the cipher and setting of the IV is
  22. * done by the MAC. One cannot operate without the other. The
  23. * configuration of the ssh_cipheralg structure ensures that the MAC is
  24. * set (and others ignored) if this cipher is chosen.
  25. *
  26. * This cipher also encrypts the length using a different
  27. * instantiation of the cipher using a different key and IV made from
  28. * the sequence number which is passed in addition when calling
  29. * encrypt/decrypt on it.
  30. */
  31. #include "ssh.h"
  32. #include "mpint_i.h"
  33. #ifndef INLINE
  34. #define INLINE
  35. #endif
  36. /* ChaCha20 implementation, only supporting 256-bit keys */
  37. /* State for each ChaCha20 instance */
  38. struct chacha20 {
  39. /* Current context, usually with the count incremented
  40. * 0-3 are the static constant
  41. * 4-11 are the key
  42. * 12-13 are the counter
  43. * 14-15 are the IV */
  44. uint32_t state[16];
  45. /* The output of the state above ready to xor */
  46. unsigned char current[64];
  47. /* The index of the above currently used to allow a true streaming cipher */
  48. int currentIndex;
  49. };
  50. static INLINE void chacha20_round(struct chacha20 *ctx)
  51. {
  52. int i;
  53. uint32_t copy[16];
  54. /* Take a copy */
  55. memcpy(copy, ctx->state, sizeof(copy));
  56. /* A circular rotation for a 32bit number */
  57. #define rotl(x, shift) x = ((x << shift) | (x >> (32 - shift)))
  58. /* What to do for each quarter round operation */
  59. #define qrop(a, b, c, d) \
  60. copy[a] += copy[b]; \
  61. copy[c] ^= copy[a]; \
  62. rotl(copy[c], d)
  63. /* A quarter round */
  64. #define quarter(a, b, c, d) \
  65. qrop(a, b, d, 16); \
  66. qrop(c, d, b, 12); \
  67. qrop(a, b, d, 8); \
  68. qrop(c, d, b, 7)
  69. /* Do 20 rounds, in pairs because every other is different */
  70. for (i = 0; i < 20; i += 2) {
  71. /* A round */
  72. quarter(0, 4, 8, 12);
  73. quarter(1, 5, 9, 13);
  74. quarter(2, 6, 10, 14);
  75. quarter(3, 7, 11, 15);
  76. /* Another slightly different round */
  77. quarter(0, 5, 10, 15);
  78. quarter(1, 6, 11, 12);
  79. quarter(2, 7, 8, 13);
  80. quarter(3, 4, 9, 14);
  81. }
  82. /* Dump the macros, don't need them littering */
  83. #undef rotl
  84. #undef qrop
  85. #undef quarter
  86. /* Add the initial state */
  87. for (i = 0; i < 16; ++i) {
  88. copy[i] += ctx->state[i];
  89. }
  90. /* Update the content of the xor buffer */
  91. for (i = 0; i < 16; ++i) {
  92. ctx->current[i * 4 + 0] = copy[i] >> 0;
  93. ctx->current[i * 4 + 1] = copy[i] >> 8;
  94. ctx->current[i * 4 + 2] = copy[i] >> 16;
  95. ctx->current[i * 4 + 3] = copy[i] >> 24;
  96. }
  97. /* State full, reset pointer to beginning */
  98. ctx->currentIndex = 0;
  99. smemclr(copy, sizeof(copy));
  100. /* Increment round counter */
  101. ++ctx->state[12];
  102. /* Check for overflow, not done in one line so the 32 bits are chopped by the type */
  103. if (!(uint32_t)(ctx->state[12])) {
  104. ++ctx->state[13];
  105. }
  106. }
  107. /* Initialise context with 256bit key */
  108. static void chacha20_key(struct chacha20 *ctx, const unsigned char *key)
  109. {
  110. static const char constant[16] = "expand 32-byte k";
  111. /* Add the fixed string to the start of the state */
  112. ctx->state[0] = GET_32BIT_LSB_FIRST(constant + 0);
  113. ctx->state[1] = GET_32BIT_LSB_FIRST(constant + 4);
  114. ctx->state[2] = GET_32BIT_LSB_FIRST(constant + 8);
  115. ctx->state[3] = GET_32BIT_LSB_FIRST(constant + 12);
  116. /* Add the key */
  117. ctx->state[4] = GET_32BIT_LSB_FIRST(key + 0);
  118. ctx->state[5] = GET_32BIT_LSB_FIRST(key + 4);
  119. ctx->state[6] = GET_32BIT_LSB_FIRST(key + 8);
  120. ctx->state[7] = GET_32BIT_LSB_FIRST(key + 12);
  121. ctx->state[8] = GET_32BIT_LSB_FIRST(key + 16);
  122. ctx->state[9] = GET_32BIT_LSB_FIRST(key + 20);
  123. ctx->state[10] = GET_32BIT_LSB_FIRST(key + 24);
  124. ctx->state[11] = GET_32BIT_LSB_FIRST(key + 28);
  125. /* New key, dump context */
  126. ctx->currentIndex = 64;
  127. }
  128. static void chacha20_iv(struct chacha20 *ctx, const unsigned char *iv)
  129. {
  130. ctx->state[12] = 0;
  131. ctx->state[13] = 0;
  132. ctx->state[14] = GET_32BIT_MSB_FIRST(iv);
  133. ctx->state[15] = GET_32BIT_MSB_FIRST(iv + 4);
  134. /* New IV, dump context */
  135. ctx->currentIndex = 64;
  136. }
  137. static void chacha20_encrypt(struct chacha20 *ctx, unsigned char *blk, int len)
  138. {
  139. while (len) {
  140. /* If we don't have any state left, then cycle to the next */
  141. if (ctx->currentIndex >= 64) {
  142. chacha20_round(ctx);
  143. }
  144. /* Do the xor while there's some state left and some plaintext left */
  145. while (ctx->currentIndex < 64 && len) {
  146. *blk++ ^= ctx->current[ctx->currentIndex++];
  147. --len;
  148. }
  149. }
  150. }
  151. /* Decrypt is encrypt... It's xor against a PRNG... */
  152. static INLINE void chacha20_decrypt(struct chacha20 *ctx,
  153. unsigned char *blk, int len)
  154. {
  155. chacha20_encrypt(ctx, blk, len);
  156. }
  157. /* Poly1305 implementation (no AES, nonce is not encrypted) */
  158. #define NWORDS ((130 + BIGNUM_INT_BITS-1) / BIGNUM_INT_BITS)
  159. typedef struct bigval {
  160. BignumInt w[NWORDS];
  161. } bigval;
  162. static void bigval_clear(bigval *r)
  163. {
  164. int i;
  165. for (i = 0; i < NWORDS; i++)
  166. r->w[i] = 0;
  167. }
  168. static void bigval_import_le(bigval *r, const void *vdata, int len)
  169. {
  170. const unsigned char *data = (const unsigned char *)vdata;
  171. int i;
  172. bigval_clear(r);
  173. for (i = 0; i < len; i++)
  174. r->w[i / BIGNUM_INT_BYTES] |=
  175. (BignumInt)data[i] << (8 * (i % BIGNUM_INT_BYTES));
  176. }
  177. static void bigval_export_le(const bigval *r, void *vdata, int len)
  178. {
  179. unsigned char *data = (unsigned char *)vdata;
  180. int i;
  181. for (i = 0; i < len; i++)
  182. data[i] = r->w[i / BIGNUM_INT_BYTES] >> (8 * (i % BIGNUM_INT_BYTES));
  183. }
  184. /*
  185. * Core functions to do arithmetic mod p = 2^130-5. The whole
  186. * collection of these, up to and including the surrounding #if, are
  187. * generated automatically for various sizes of BignumInt by
  188. * contrib/make1305.py.
  189. */
  190. #if BIGNUM_INT_BITS == 16
  191. static void bigval_add(bigval *r, const bigval *a, const bigval *b)
  192. {
  193. BignumInt v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14;
  194. BignumInt v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26;
  195. BignumCarry carry;
  196. v0 = a->w[0];
  197. v1 = a->w[1];
  198. v2 = a->w[2];
  199. v3 = a->w[3];
  200. v4 = a->w[4];
  201. v5 = a->w[5];
  202. v6 = a->w[6];
  203. v7 = a->w[7];
  204. v8 = a->w[8];
  205. v9 = b->w[0];
  206. v10 = b->w[1];
  207. v11 = b->w[2];
  208. v12 = b->w[3];
  209. v13 = b->w[4];
  210. v14 = b->w[5];
  211. v15 = b->w[6];
  212. v16 = b->w[7];
  213. v17 = b->w[8];
  214. BignumADC(v18, carry, v0, v9, 0);
  215. BignumADC(v19, carry, v1, v10, carry);
  216. BignumADC(v20, carry, v2, v11, carry);
  217. BignumADC(v21, carry, v3, v12, carry);
  218. BignumADC(v22, carry, v4, v13, carry);
  219. BignumADC(v23, carry, v5, v14, carry);
  220. BignumADC(v24, carry, v6, v15, carry);
  221. BignumADC(v25, carry, v7, v16, carry);
  222. v26 = v8 + v17 + carry;
  223. r->w[0] = v18;
  224. r->w[1] = v19;
  225. r->w[2] = v20;
  226. r->w[3] = v21;
  227. r->w[4] = v22;
  228. r->w[5] = v23;
  229. r->w[6] = v24;
  230. r->w[7] = v25;
  231. r->w[8] = v26;
  232. }
  233. static void bigval_mul_mod_p(bigval *r, const bigval *a, const bigval *b)
  234. {
  235. BignumInt v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14;
  236. BignumInt v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27;
  237. BignumInt v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40;
  238. BignumInt v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53;
  239. BignumInt v54, v55, v56, v57, v58, v59, v60, v61, v62, v63, v64, v65, v66;
  240. BignumInt v67, v68, v69, v70, v71, v72, v73, v74, v75, v76, v77, v78, v79;
  241. BignumInt v80, v81, v82, v83, v84, v85, v86, v87, v88, v89, v90, v91, v92;
  242. BignumInt v93, v94, v95, v96, v97, v98, v99, v100, v101, v102, v103, v104;
  243. BignumInt v105, v106, v107, v108, v109, v110, v111, v112, v113, v114;
  244. BignumInt v115, v116, v117, v118, v119, v120, v121, v122, v123, v124;
  245. BignumInt v125, v126, v127, v128, v129, v130, v131, v132, v133, v134;
  246. BignumInt v135, v136, v137, v138, v139, v140, v141, v142, v143, v144;
  247. BignumInt v145, v146, v147, v148, v149, v150, v151, v152, v153, v154;
  248. BignumInt v155, v156, v157, v158, v159, v160, v161, v162, v163, v164;
  249. BignumInt v165, v166, v167, v168, v169, v170, v171, v172, v173, v174;
  250. BignumInt v175, v176, v177, v178, v180, v181, v182, v183, v184, v185;
  251. BignumInt v186, v187, v188, v189, v190, v191, v192, v193, v194, v195;
  252. BignumInt v196, v197, v198, v199, v200, v201, v202, v203, v204, v205;
  253. BignumInt v206, v207, v208, v210, v212, v213, v214, v215, v216, v217;
  254. BignumInt v218, v219, v220, v221, v222, v223, v224, v225, v226, v227;
  255. BignumInt v228, v229;
  256. BignumCarry carry;
  257. v0 = a->w[0];
  258. v1 = a->w[1];
  259. v2 = a->w[2];
  260. v3 = a->w[3];
  261. v4 = a->w[4];
  262. v5 = a->w[5];
  263. v6 = a->w[6];
  264. v7 = a->w[7];
  265. v8 = a->w[8];
  266. v9 = b->w[0];
  267. v10 = b->w[1];
  268. v11 = b->w[2];
  269. v12 = b->w[3];
  270. v13 = b->w[4];
  271. v14 = b->w[5];
  272. v15 = b->w[6];
  273. v16 = b->w[7];
  274. v17 = b->w[8];
  275. BignumMUL(v19, v18, v0, v9);
  276. BignumMULADD(v21, v20, v0, v10, v19);
  277. BignumMULADD(v23, v22, v0, v11, v21);
  278. BignumMULADD(v25, v24, v0, v12, v23);
  279. BignumMULADD(v27, v26, v0, v13, v25);
  280. BignumMULADD(v29, v28, v0, v14, v27);
  281. BignumMULADD(v31, v30, v0, v15, v29);
  282. BignumMULADD(v33, v32, v0, v16, v31);
  283. BignumMULADD(v35, v34, v0, v17, v33);
  284. BignumMULADD(v37, v36, v1, v9, v20);
  285. BignumMULADD2(v39, v38, v1, v10, v22, v37);
  286. BignumMULADD2(v41, v40, v1, v11, v24, v39);
  287. BignumMULADD2(v43, v42, v1, v12, v26, v41);
  288. BignumMULADD2(v45, v44, v1, v13, v28, v43);
  289. BignumMULADD2(v47, v46, v1, v14, v30, v45);
  290. BignumMULADD2(v49, v48, v1, v15, v32, v47);
  291. BignumMULADD2(v51, v50, v1, v16, v34, v49);
  292. BignumMULADD2(v53, v52, v1, v17, v35, v51);
  293. BignumMULADD(v55, v54, v2, v9, v38);
  294. BignumMULADD2(v57, v56, v2, v10, v40, v55);
  295. BignumMULADD2(v59, v58, v2, v11, v42, v57);
  296. BignumMULADD2(v61, v60, v2, v12, v44, v59);
  297. BignumMULADD2(v63, v62, v2, v13, v46, v61);
  298. BignumMULADD2(v65, v64, v2, v14, v48, v63);
  299. BignumMULADD2(v67, v66, v2, v15, v50, v65);
  300. BignumMULADD2(v69, v68, v2, v16, v52, v67);
  301. BignumMULADD2(v71, v70, v2, v17, v53, v69);
  302. BignumMULADD(v73, v72, v3, v9, v56);
  303. BignumMULADD2(v75, v74, v3, v10, v58, v73);
  304. BignumMULADD2(v77, v76, v3, v11, v60, v75);
  305. BignumMULADD2(v79, v78, v3, v12, v62, v77);
  306. BignumMULADD2(v81, v80, v3, v13, v64, v79);
  307. BignumMULADD2(v83, v82, v3, v14, v66, v81);
  308. BignumMULADD2(v85, v84, v3, v15, v68, v83);
  309. BignumMULADD2(v87, v86, v3, v16, v70, v85);
  310. BignumMULADD2(v89, v88, v3, v17, v71, v87);
  311. BignumMULADD(v91, v90, v4, v9, v74);
  312. BignumMULADD2(v93, v92, v4, v10, v76, v91);
  313. BignumMULADD2(v95, v94, v4, v11, v78, v93);
  314. BignumMULADD2(v97, v96, v4, v12, v80, v95);
  315. BignumMULADD2(v99, v98, v4, v13, v82, v97);
  316. BignumMULADD2(v101, v100, v4, v14, v84, v99);
  317. BignumMULADD2(v103, v102, v4, v15, v86, v101);
  318. BignumMULADD2(v105, v104, v4, v16, v88, v103);
  319. BignumMULADD2(v107, v106, v4, v17, v89, v105);
  320. BignumMULADD(v109, v108, v5, v9, v92);
  321. BignumMULADD2(v111, v110, v5, v10, v94, v109);
  322. BignumMULADD2(v113, v112, v5, v11, v96, v111);
  323. BignumMULADD2(v115, v114, v5, v12, v98, v113);
  324. BignumMULADD2(v117, v116, v5, v13, v100, v115);
  325. BignumMULADD2(v119, v118, v5, v14, v102, v117);
  326. BignumMULADD2(v121, v120, v5, v15, v104, v119);
  327. BignumMULADD2(v123, v122, v5, v16, v106, v121);
  328. BignumMULADD2(v125, v124, v5, v17, v107, v123);
  329. BignumMULADD(v127, v126, v6, v9, v110);
  330. BignumMULADD2(v129, v128, v6, v10, v112, v127);
  331. BignumMULADD2(v131, v130, v6, v11, v114, v129);
  332. BignumMULADD2(v133, v132, v6, v12, v116, v131);
  333. BignumMULADD2(v135, v134, v6, v13, v118, v133);
  334. BignumMULADD2(v137, v136, v6, v14, v120, v135);
  335. BignumMULADD2(v139, v138, v6, v15, v122, v137);
  336. BignumMULADD2(v141, v140, v6, v16, v124, v139);
  337. BignumMULADD2(v143, v142, v6, v17, v125, v141);
  338. BignumMULADD(v145, v144, v7, v9, v128);
  339. BignumMULADD2(v147, v146, v7, v10, v130, v145);
  340. BignumMULADD2(v149, v148, v7, v11, v132, v147);
  341. BignumMULADD2(v151, v150, v7, v12, v134, v149);
  342. BignumMULADD2(v153, v152, v7, v13, v136, v151);
  343. BignumMULADD2(v155, v154, v7, v14, v138, v153);
  344. BignumMULADD2(v157, v156, v7, v15, v140, v155);
  345. BignumMULADD2(v159, v158, v7, v16, v142, v157);
  346. BignumMULADD2(v161, v160, v7, v17, v143, v159);
  347. BignumMULADD(v163, v162, v8, v9, v146);
  348. BignumMULADD2(v165, v164, v8, v10, v148, v163);
  349. BignumMULADD2(v167, v166, v8, v11, v150, v165);
  350. BignumMULADD2(v169, v168, v8, v12, v152, v167);
  351. BignumMULADD2(v171, v170, v8, v13, v154, v169);
  352. BignumMULADD2(v173, v172, v8, v14, v156, v171);
  353. BignumMULADD2(v175, v174, v8, v15, v158, v173);
  354. BignumMULADD2(v177, v176, v8, v16, v160, v175);
  355. v178 = v8 * v17 + v161 + v177;
  356. v180 = (v162) & ((((BignumInt)1) << 2)-1);
  357. v181 = ((v162) >> 2) | ((v164) << 14);
  358. v182 = ((v164) >> 2) | ((v166) << 14);
  359. v183 = ((v166) >> 2) | ((v168) << 14);
  360. v184 = ((v168) >> 2) | ((v170) << 14);
  361. v185 = ((v170) >> 2) | ((v172) << 14);
  362. v186 = ((v172) >> 2) | ((v174) << 14);
  363. v187 = ((v174) >> 2) | ((v176) << 14);
  364. v188 = ((v176) >> 2) | ((v178) << 14);
  365. v189 = (v178) >> 2;
  366. v190 = (v189) & ((((BignumInt)1) << 2)-1);
  367. v191 = (v178) >> 4;
  368. BignumMUL(v193, v192, 5, v181);
  369. BignumMULADD(v195, v194, 5, v182, v193);
  370. BignumMULADD(v197, v196, 5, v183, v195);
  371. BignumMULADD(v199, v198, 5, v184, v197);
  372. BignumMULADD(v201, v200, 5, v185, v199);
  373. BignumMULADD(v203, v202, 5, v186, v201);
  374. BignumMULADD(v205, v204, 5, v187, v203);
  375. BignumMULADD(v207, v206, 5, v188, v205);
  376. v208 = 5 * v190 + v207;
  377. v210 = 25 * v191;
  378. BignumADC(v212, carry, v18, v192, 0);
  379. BignumADC(v213, carry, v36, v194, carry);
  380. BignumADC(v214, carry, v54, v196, carry);
  381. BignumADC(v215, carry, v72, v198, carry);
  382. BignumADC(v216, carry, v90, v200, carry);
  383. BignumADC(v217, carry, v108, v202, carry);
  384. BignumADC(v218, carry, v126, v204, carry);
  385. BignumADC(v219, carry, v144, v206, carry);
  386. v220 = v180 + v208 + carry;
  387. BignumADC(v221, carry, v212, v210, 0);
  388. BignumADC(v222, carry, v213, 0, carry);
  389. BignumADC(v223, carry, v214, 0, carry);
  390. BignumADC(v224, carry, v215, 0, carry);
  391. BignumADC(v225, carry, v216, 0, carry);
  392. BignumADC(v226, carry, v217, 0, carry);
  393. BignumADC(v227, carry, v218, 0, carry);
  394. BignumADC(v228, carry, v219, 0, carry);
  395. v229 = v220 + 0 + carry;
  396. r->w[0] = v221;
  397. r->w[1] = v222;
  398. r->w[2] = v223;
  399. r->w[3] = v224;
  400. r->w[4] = v225;
  401. r->w[5] = v226;
  402. r->w[6] = v227;
  403. r->w[7] = v228;
  404. r->w[8] = v229;
  405. }
  406. static void bigval_final_reduce(bigval *n)
  407. {
  408. BignumInt v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v13, v14, v15;
  409. BignumInt v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28;
  410. BignumInt v29, v30, v31, v32, v34, v35, v36, v37, v38, v39, v40, v41, v42;
  411. BignumInt v43;
  412. BignumCarry carry;
  413. v0 = n->w[0];
  414. v1 = n->w[1];
  415. v2 = n->w[2];
  416. v3 = n->w[3];
  417. v4 = n->w[4];
  418. v5 = n->w[5];
  419. v6 = n->w[6];
  420. v7 = n->w[7];
  421. v8 = n->w[8];
  422. v9 = (v8) >> 2;
  423. v10 = (v8) & ((((BignumInt)1) << 2)-1);
  424. v11 = 5 * v9;
  425. BignumADC(v13, carry, v0, v11, 0);
  426. BignumADC(v14, carry, v1, 0, carry);
  427. BignumADC(v15, carry, v2, 0, carry);
  428. BignumADC(v16, carry, v3, 0, carry);
  429. BignumADC(v17, carry, v4, 0, carry);
  430. BignumADC(v18, carry, v5, 0, carry);
  431. BignumADC(v19, carry, v6, 0, carry);
  432. BignumADC(v20, carry, v7, 0, carry);
  433. v21 = v10 + 0 + carry;
  434. BignumADC(v22, carry, v13, 5, 0);
  435. (void)v22;
  436. BignumADC(v23, carry, v14, 0, carry);
  437. (void)v23;
  438. BignumADC(v24, carry, v15, 0, carry);
  439. (void)v24;
  440. BignumADC(v25, carry, v16, 0, carry);
  441. (void)v25;
  442. BignumADC(v26, carry, v17, 0, carry);
  443. (void)v26;
  444. BignumADC(v27, carry, v18, 0, carry);
  445. (void)v27;
  446. BignumADC(v28, carry, v19, 0, carry);
  447. (void)v28;
  448. BignumADC(v29, carry, v20, 0, carry);
  449. (void)v29;
  450. v30 = v21 + 0 + carry;
  451. v31 = (v30) >> 2;
  452. v32 = 5 * v31;
  453. BignumADC(v34, carry, v13, v32, 0);
  454. BignumADC(v35, carry, v14, 0, carry);
  455. BignumADC(v36, carry, v15, 0, carry);
  456. BignumADC(v37, carry, v16, 0, carry);
  457. BignumADC(v38, carry, v17, 0, carry);
  458. BignumADC(v39, carry, v18, 0, carry);
  459. BignumADC(v40, carry, v19, 0, carry);
  460. BignumADC(v41, carry, v20, 0, carry);
  461. v42 = v21 + 0 + carry;
  462. v43 = (v42) & ((((BignumInt)1) << 2)-1);
  463. n->w[0] = v34;
  464. n->w[1] = v35;
  465. n->w[2] = v36;
  466. n->w[3] = v37;
  467. n->w[4] = v38;
  468. n->w[5] = v39;
  469. n->w[6] = v40;
  470. n->w[7] = v41;
  471. n->w[8] = v43;
  472. }
  473. #elif BIGNUM_INT_BITS == 32
  474. static void bigval_add(bigval *r, const bigval *a, const bigval *b)
  475. {
  476. BignumInt v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14;
  477. BignumCarry carry;
  478. v0 = a->w[0];
  479. v1 = a->w[1];
  480. v2 = a->w[2];
  481. v3 = a->w[3];
  482. v4 = a->w[4];
  483. v5 = b->w[0];
  484. v6 = b->w[1];
  485. v7 = b->w[2];
  486. v8 = b->w[3];
  487. v9 = b->w[4];
  488. BignumADC(v10, carry, v0, v5, 0);
  489. BignumADC(v11, carry, v1, v6, carry);
  490. BignumADC(v12, carry, v2, v7, carry);
  491. BignumADC(v13, carry, v3, v8, carry);
  492. v14 = v4 + v9 + carry;
  493. r->w[0] = v10;
  494. r->w[1] = v11;
  495. r->w[2] = v12;
  496. r->w[3] = v13;
  497. r->w[4] = v14;
  498. }
  499. static void bigval_mul_mod_p(bigval *r, const bigval *a, const bigval *b)
  500. {
  501. BignumInt v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14;
  502. BignumInt v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27;
  503. BignumInt v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40;
  504. BignumInt v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53;
  505. BignumInt v54, v55, v56, v57, v58, v60, v61, v62, v63, v64, v65, v66, v67;
  506. BignumInt v68, v69, v70, v71, v72, v73, v74, v75, v76, v78, v80, v81, v82;
  507. BignumInt v83, v84, v85, v86, v87, v88, v89;
  508. BignumCarry carry;
  509. v0 = a->w[0];
  510. v1 = a->w[1];
  511. v2 = a->w[2];
  512. v3 = a->w[3];
  513. v4 = a->w[4];
  514. v5 = b->w[0];
  515. v6 = b->w[1];
  516. v7 = b->w[2];
  517. v8 = b->w[3];
  518. v9 = b->w[4];
  519. BignumMUL(v11, v10, v0, v5);
  520. BignumMULADD(v13, v12, v0, v6, v11);
  521. BignumMULADD(v15, v14, v0, v7, v13);
  522. BignumMULADD(v17, v16, v0, v8, v15);
  523. BignumMULADD(v19, v18, v0, v9, v17);
  524. BignumMULADD(v21, v20, v1, v5, v12);
  525. BignumMULADD2(v23, v22, v1, v6, v14, v21);
  526. BignumMULADD2(v25, v24, v1, v7, v16, v23);
  527. BignumMULADD2(v27, v26, v1, v8, v18, v25);
  528. BignumMULADD2(v29, v28, v1, v9, v19, v27);
  529. BignumMULADD(v31, v30, v2, v5, v22);
  530. BignumMULADD2(v33, v32, v2, v6, v24, v31);
  531. BignumMULADD2(v35, v34, v2, v7, v26, v33);
  532. BignumMULADD2(v37, v36, v2, v8, v28, v35);
  533. BignumMULADD2(v39, v38, v2, v9, v29, v37);
  534. BignumMULADD(v41, v40, v3, v5, v32);
  535. BignumMULADD2(v43, v42, v3, v6, v34, v41);
  536. BignumMULADD2(v45, v44, v3, v7, v36, v43);
  537. BignumMULADD2(v47, v46, v3, v8, v38, v45);
  538. BignumMULADD2(v49, v48, v3, v9, v39, v47);
  539. BignumMULADD(v51, v50, v4, v5, v42);
  540. BignumMULADD2(v53, v52, v4, v6, v44, v51);
  541. BignumMULADD2(v55, v54, v4, v7, v46, v53);
  542. BignumMULADD2(v57, v56, v4, v8, v48, v55);
  543. v58 = v4 * v9 + v49 + v57;
  544. v60 = (v50) & ((((BignumInt)1) << 2)-1);
  545. v61 = ((v50) >> 2) | ((v52) << 30);
  546. v62 = ((v52) >> 2) | ((v54) << 30);
  547. v63 = ((v54) >> 2) | ((v56) << 30);
  548. v64 = ((v56) >> 2) | ((v58) << 30);
  549. v65 = (v58) >> 2;
  550. v66 = (v65) & ((((BignumInt)1) << 2)-1);
  551. v67 = (v58) >> 4;
  552. BignumMUL(v69, v68, 5, v61);
  553. BignumMULADD(v71, v70, 5, v62, v69);
  554. BignumMULADD(v73, v72, 5, v63, v71);
  555. BignumMULADD(v75, v74, 5, v64, v73);
  556. v76 = 5 * v66 + v75;
  557. v78 = 25 * v67;
  558. BignumADC(v80, carry, v10, v68, 0);
  559. BignumADC(v81, carry, v20, v70, carry);
  560. BignumADC(v82, carry, v30, v72, carry);
  561. BignumADC(v83, carry, v40, v74, carry);
  562. v84 = v60 + v76 + carry;
  563. BignumADC(v85, carry, v80, v78, 0);
  564. BignumADC(v86, carry, v81, 0, carry);
  565. BignumADC(v87, carry, v82, 0, carry);
  566. BignumADC(v88, carry, v83, 0, carry);
  567. v89 = v84 + 0 + carry;
  568. r->w[0] = v85;
  569. r->w[1] = v86;
  570. r->w[2] = v87;
  571. r->w[3] = v88;
  572. r->w[4] = v89;
  573. }
  574. static void bigval_final_reduce(bigval *n)
  575. {
  576. BignumInt v0, v1, v2, v3, v4, v5, v6, v7, v9, v10, v11, v12, v13, v14;
  577. BignumInt v15, v16, v17, v18, v19, v20, v22, v23, v24, v25, v26, v27;
  578. BignumCarry carry;
  579. v0 = n->w[0];
  580. v1 = n->w[1];
  581. v2 = n->w[2];
  582. v3 = n->w[3];
  583. v4 = n->w[4];
  584. v5 = (v4) >> 2;
  585. v6 = (v4) & ((((BignumInt)1) << 2)-1);
  586. v7 = 5 * v5;
  587. BignumADC(v9, carry, v0, v7, 0);
  588. BignumADC(v10, carry, v1, 0, carry);
  589. BignumADC(v11, carry, v2, 0, carry);
  590. BignumADC(v12, carry, v3, 0, carry);
  591. v13 = v6 + 0 + carry;
  592. BignumADC(v14, carry, v9, 5, 0);
  593. (void)v14;
  594. BignumADC(v15, carry, v10, 0, carry);
  595. (void)v15;
  596. BignumADC(v16, carry, v11, 0, carry);
  597. (void)v16;
  598. BignumADC(v17, carry, v12, 0, carry);
  599. (void)v17;
  600. v18 = v13 + 0 + carry;
  601. v19 = (v18) >> 2;
  602. v20 = 5 * v19;
  603. BignumADC(v22, carry, v9, v20, 0);
  604. BignumADC(v23, carry, v10, 0, carry);
  605. BignumADC(v24, carry, v11, 0, carry);
  606. BignumADC(v25, carry, v12, 0, carry);
  607. v26 = v13 + 0 + carry;
  608. v27 = (v26) & ((((BignumInt)1) << 2)-1);
  609. n->w[0] = v22;
  610. n->w[1] = v23;
  611. n->w[2] = v24;
  612. n->w[3] = v25;
  613. n->w[4] = v27;
  614. }
  615. #elif BIGNUM_INT_BITS == 64
  616. static void bigval_add(bigval *r, const bigval *a, const bigval *b)
  617. {
  618. BignumInt v0, v1, v2, v3, v4, v5, v6, v7, v8;
  619. BignumCarry carry;
  620. v0 = a->w[0];
  621. v1 = a->w[1];
  622. v2 = a->w[2];
  623. v3 = b->w[0];
  624. v4 = b->w[1];
  625. v5 = b->w[2];
  626. BignumADC(v6, carry, v0, v3, 0);
  627. BignumADC(v7, carry, v1, v4, carry);
  628. v8 = v2 + v5 + carry;
  629. r->w[0] = v6;
  630. r->w[1] = v7;
  631. r->w[2] = v8;
  632. }
  633. static void bigval_mul_mod_p(bigval *r, const bigval *a, const bigval *b)
  634. {
  635. BignumInt v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14;
  636. BignumInt v15, v16, v17, v18, v19, v20, v21, v22, v24, v25, v26, v27, v28;
  637. BignumInt v29, v30, v31, v32, v33, v34, v36, v38, v39, v40, v41, v42, v43;
  638. BignumCarry carry;
  639. v0 = a->w[0];
  640. v1 = a->w[1];
  641. v2 = a->w[2];
  642. v3 = b->w[0];
  643. v4 = b->w[1];
  644. v5 = b->w[2];
  645. BignumMUL(v7, v6, v0, v3);
  646. BignumMULADD(v9, v8, v0, v4, v7);
  647. BignumMULADD(v11, v10, v0, v5, v9);
  648. BignumMULADD(v13, v12, v1, v3, v8);
  649. BignumMULADD2(v15, v14, v1, v4, v10, v13);
  650. BignumMULADD2(v17, v16, v1, v5, v11, v15);
  651. BignumMULADD(v19, v18, v2, v3, v14);
  652. BignumMULADD2(v21, v20, v2, v4, v16, v19);
  653. v22 = v2 * v5 + v17 + v21;
  654. v24 = (v18) & ((((BignumInt)1) << 2)-1);
  655. v25 = ((v18) >> 2) | ((v20) << 62);
  656. v26 = ((v20) >> 2) | ((v22) << 62);
  657. v27 = (v22) >> 2;
  658. v28 = (v27) & ((((BignumInt)1) << 2)-1);
  659. v29 = (v22) >> 4;
  660. BignumMUL(v31, v30, 5, v25);
  661. BignumMULADD(v33, v32, 5, v26, v31);
  662. v34 = 5 * v28 + v33;
  663. v36 = 25 * v29;
  664. BignumADC(v38, carry, v6, v30, 0);
  665. BignumADC(v39, carry, v12, v32, carry);
  666. v40 = v24 + v34 + carry;
  667. BignumADC(v41, carry, v38, v36, 0);
  668. BignumADC(v42, carry, v39, 0, carry);
  669. v43 = v40 + 0 + carry;
  670. r->w[0] = v41;
  671. r->w[1] = v42;
  672. r->w[2] = v43;
  673. }
  674. static void bigval_final_reduce(bigval *n)
  675. {
  676. BignumInt v0, v1, v2, v3, v4, v5, v7, v8, v9, v10, v11, v12, v13, v14;
  677. BignumInt v16, v17, v18, v19;
  678. BignumCarry carry;
  679. v0 = n->w[0];
  680. v1 = n->w[1];
  681. v2 = n->w[2];
  682. v3 = (v2) >> 2;
  683. v4 = (v2) & ((((BignumInt)1) << 2)-1);
  684. v5 = 5 * v3;
  685. BignumADC(v7, carry, v0, v5, 0);
  686. BignumADC(v8, carry, v1, 0, carry);
  687. v9 = v4 + 0 + carry;
  688. BignumADC(v10, carry, v7, 5, 0);
  689. (void)v10;
  690. BignumADC(v11, carry, v8, 0, carry);
  691. (void)v11;
  692. v12 = v9 + 0 + carry;
  693. v13 = (v12) >> 2;
  694. v14 = 5 * v13;
  695. BignumADC(v16, carry, v7, v14, 0);
  696. BignumADC(v17, carry, v8, 0, carry);
  697. v18 = v9 + 0 + carry;
  698. v19 = (v18) & ((((BignumInt)1) << 2)-1);
  699. n->w[0] = v16;
  700. n->w[1] = v17;
  701. n->w[2] = v19;
  702. }
  703. #else
  704. #error Add another bit count to contrib/make1305.py and rerun it
  705. #endif
  706. struct poly1305 {
  707. unsigned char nonce[16];
  708. bigval r;
  709. bigval h;
  710. /* Buffer in case we get less that a multiple of 16 bytes */
  711. unsigned char buffer[16];
  712. int bufferIndex;
  713. };
  714. static void poly1305_init(struct poly1305 *ctx)
  715. {
  716. memset(ctx->nonce, 0, 16);
  717. ctx->bufferIndex = 0;
  718. bigval_clear(&ctx->h);
  719. }
  720. static void poly1305_key(struct poly1305 *ctx, ptrlen key)
  721. {
  722. assert(key.len == 32); /* Takes a 256 bit key */
  723. unsigned char key_copy[16];
  724. memcpy(key_copy, key.ptr, 16);
  725. /* Key the MAC itself
  726. * bytes 4, 8, 12 and 16 are required to have their top four bits clear */
  727. key_copy[3] &= 0x0f;
  728. key_copy[7] &= 0x0f;
  729. key_copy[11] &= 0x0f;
  730. key_copy[15] &= 0x0f;
  731. /* bytes 5, 9 and 13 are required to have their bottom two bits clear */
  732. key_copy[4] &= 0xfc;
  733. key_copy[8] &= 0xfc;
  734. key_copy[12] &= 0xfc;
  735. bigval_import_le(&ctx->r, key_copy, 16);
  736. smemclr(key_copy, sizeof(key_copy));
  737. /* Use second 128 bits as the nonce */
  738. memcpy(ctx->nonce, (const char *)key.ptr + 16, 16);
  739. }
  740. /* Feed up to 16 bytes (should only be less for the last chunk) */
  741. static void poly1305_feed_chunk(struct poly1305 *ctx,
  742. const unsigned char *chunk, int len)
  743. {
  744. bigval c;
  745. bigval_import_le(&c, chunk, len);
  746. c.w[len / BIGNUM_INT_BYTES] |=
  747. (BignumInt)1 << (8 * (len % BIGNUM_INT_BYTES));
  748. bigval_add(&c, &c, &ctx->h);
  749. bigval_mul_mod_p(&ctx->h, &c, &ctx->r);
  750. }
  751. static void poly1305_feed(struct poly1305 *ctx,
  752. const unsigned char *buf, int len)
  753. {
  754. /* Check for stuff left in the buffer from last time */
  755. if (ctx->bufferIndex) {
  756. /* Try to fill up to 16 */
  757. while (ctx->bufferIndex < 16 && len) {
  758. ctx->buffer[ctx->bufferIndex++] = *buf++;
  759. --len;
  760. }
  761. if (ctx->bufferIndex == 16) {
  762. poly1305_feed_chunk(ctx, ctx->buffer, 16);
  763. ctx->bufferIndex = 0;
  764. }
  765. }
  766. /* Process 16 byte whole chunks */
  767. while (len >= 16) {
  768. poly1305_feed_chunk(ctx, buf, 16);
  769. len -= 16;
  770. buf += 16;
  771. }
  772. /* Cache stuff that's left over */
  773. if (len) {
  774. memcpy(ctx->buffer, buf, len);
  775. ctx->bufferIndex = len;
  776. }
  777. }
  778. /* Finalise and populate buffer with 16 byte with MAC */
  779. static void poly1305_finalise(struct poly1305 *ctx, unsigned char *mac)
  780. {
  781. bigval tmp;
  782. if (ctx->bufferIndex) {
  783. poly1305_feed_chunk(ctx, ctx->buffer, ctx->bufferIndex);
  784. }
  785. bigval_import_le(&tmp, ctx->nonce, 16);
  786. bigval_final_reduce(&ctx->h);
  787. bigval_add(&tmp, &tmp, &ctx->h);
  788. bigval_export_le(&tmp, mac, 16);
  789. }
  790. /* SSH-2 wrapper */
  791. struct ccp_context {
  792. struct chacha20 a_cipher; /* Used for length */
  793. struct chacha20 b_cipher; /* Used for content */
  794. /* Cache of the first 4 bytes because they are the sequence number */
  795. /* Kept in 8 bytes with the top as zero to allow easy passing to setiv */
  796. int mac_initialised; /* Where we have got to in filling mac_iv */
  797. unsigned char mac_iv[8];
  798. struct poly1305 mac;
  799. BinarySink_IMPLEMENTATION;
  800. ssh_cipher ciph;
  801. ssh2_mac mac_if;
  802. bool ciph_allocated, mac_allocated;
  803. };
  804. static ssh2_mac *poly_ssh2_new(
  805. const ssh2_macalg *alg, ssh_cipher *cipher)
  806. {
  807. struct ccp_context *ctx = container_of(cipher, struct ccp_context, ciph);
  808. ctx->mac_if.vt = alg;
  809. ctx->mac_allocated = true;
  810. BinarySink_DELEGATE_INIT(&ctx->mac_if, ctx);
  811. return &ctx->mac_if;
  812. }
  813. static void ccp_common_free(struct ccp_context *ctx)
  814. {
  815. if (ctx->ciph_allocated || ctx->mac_allocated)
  816. return;
  817. smemclr(&ctx->a_cipher, sizeof(ctx->a_cipher));
  818. smemclr(&ctx->b_cipher, sizeof(ctx->b_cipher));
  819. smemclr(&ctx->mac, sizeof(ctx->mac));
  820. sfree(ctx);
  821. }
  822. static void poly_ssh2_free(ssh2_mac *mac)
  823. {
  824. struct ccp_context *ctx = container_of(mac, struct ccp_context, mac_if);
  825. ctx->mac_allocated = false;
  826. ccp_common_free(ctx);
  827. }
  828. static void poly_setkey(ssh2_mac *mac, ptrlen key)
  829. {
  830. /* Uses the same context as ChaCha20, so ignore */
  831. }
  832. static void poly_start(ssh2_mac *mac)
  833. {
  834. struct ccp_context *ctx = container_of(mac, struct ccp_context, mac_if);
  835. ctx->mac_initialised = 0;
  836. memset(ctx->mac_iv, 0, 8);
  837. poly1305_init(&ctx->mac);
  838. }
  839. static void poly_BinarySink_write(BinarySink *bs, const void *blkv, size_t len)
  840. {
  841. struct ccp_context *ctx = BinarySink_DOWNCAST(bs, struct ccp_context);
  842. const unsigned char *blk = (const unsigned char *)blkv;
  843. /* First 4 bytes are the IV */
  844. while (ctx->mac_initialised < 4 && len) {
  845. ctx->mac_iv[7 - ctx->mac_initialised] = *blk++;
  846. ++ctx->mac_initialised;
  847. --len;
  848. }
  849. /* Initialise the IV if needed */
  850. if (ctx->mac_initialised == 4) {
  851. chacha20_iv(&ctx->b_cipher, ctx->mac_iv);
  852. ++ctx->mac_initialised; /* Don't do it again */
  853. /* Do first rotation */
  854. chacha20_round(&ctx->b_cipher);
  855. /* Set the poly key */
  856. poly1305_key(&ctx->mac, make_ptrlen(ctx->b_cipher.current, 32));
  857. /* Set the first round as used */
  858. ctx->b_cipher.currentIndex = 64;
  859. }
  860. /* Update the MAC with anything left */
  861. if (len) {
  862. poly1305_feed(&ctx->mac, blk, len);
  863. }
  864. }
  865. static void poly_genresult(ssh2_mac *mac, unsigned char *blk)
  866. {
  867. struct ccp_context *ctx = container_of(mac, struct ccp_context, mac_if);
  868. poly1305_finalise(&ctx->mac, blk);
  869. }
  870. static const char *poly_text_name(ssh2_mac *mac)
  871. {
  872. return "Poly1305";
  873. }
  874. const ssh2_macalg ssh2_poly1305 = {
  875. .new = poly_ssh2_new,
  876. .free = poly_ssh2_free,
  877. .setkey = poly_setkey,
  878. .start = poly_start,
  879. .genresult = poly_genresult,
  880. .next_message = nullmac_next_message,
  881. .text_name = poly_text_name,
  882. .name = "",
  883. .etm_name = "", /* Not selectable individually, just part of
  884. * ChaCha20-Poly1305 */
  885. .len = 16,
  886. .keylen = 0,
  887. };
  888. static ssh_cipher *ccp_new(const ssh_cipheralg *alg)
  889. {
  890. struct ccp_context *ctx = snew(struct ccp_context);
  891. BinarySink_INIT(ctx, poly_BinarySink_write);
  892. poly1305_init(&ctx->mac);
  893. ctx->ciph.vt = alg;
  894. ctx->ciph_allocated = true;
  895. ctx->mac_allocated = false;
  896. return &ctx->ciph;
  897. }
  898. static void ccp_free(ssh_cipher *cipher)
  899. {
  900. struct ccp_context *ctx = container_of(cipher, struct ccp_context, ciph);
  901. ctx->ciph_allocated = false;
  902. ccp_common_free(ctx);
  903. }
  904. static void ccp_iv(ssh_cipher *cipher, const void *iv)
  905. {
  906. /* struct ccp_context *ctx =
  907. container_of(cipher, struct ccp_context, ciph); */
  908. /* IV is set based on the sequence number */
  909. }
  910. static void ccp_key(ssh_cipher *cipher, const void *vkey)
  911. {
  912. const unsigned char *key = (const unsigned char *)vkey;
  913. struct ccp_context *ctx = container_of(cipher, struct ccp_context, ciph);
  914. /* Initialise the a_cipher (for decrypting lengths) with the first 256 bits */
  915. chacha20_key(&ctx->a_cipher, key + 32);
  916. /* Initialise the b_cipher (for content and MAC) with the second 256 bits */
  917. chacha20_key(&ctx->b_cipher, key);
  918. }
  919. static void ccp_encrypt(ssh_cipher *cipher, void *blk, int len)
  920. {
  921. struct ccp_context *ctx = container_of(cipher, struct ccp_context, ciph);
  922. chacha20_encrypt(&ctx->b_cipher, blk, len);
  923. }
  924. static void ccp_decrypt(ssh_cipher *cipher, void *blk, int len)
  925. {
  926. struct ccp_context *ctx = container_of(cipher, struct ccp_context, ciph);
  927. chacha20_decrypt(&ctx->b_cipher, blk, len);
  928. }
  929. static void ccp_length_op(struct ccp_context *ctx, void *blk, int len,
  930. unsigned long seq)
  931. {
  932. unsigned char iv[8];
  933. /*
  934. * According to RFC 4253 (section 6.4), the packet sequence number wraps
  935. * at 2^32, so its 32 high-order bits will always be zero.
  936. */
  937. PUT_32BIT_LSB_FIRST(iv, 0);
  938. PUT_32BIT_LSB_FIRST(iv + 4, seq);
  939. chacha20_iv(&ctx->a_cipher, iv);
  940. chacha20_iv(&ctx->b_cipher, iv);
  941. /* Reset content block count to 1, as the first is the key for Poly1305 */
  942. ++ctx->b_cipher.state[12];
  943. smemclr(iv, sizeof(iv));
  944. }
  945. static void ccp_encrypt_length(ssh_cipher *cipher, void *blk, int len,
  946. unsigned long seq)
  947. {
  948. struct ccp_context *ctx = container_of(cipher, struct ccp_context, ciph);
  949. ccp_length_op(ctx, blk, len, seq);
  950. chacha20_encrypt(&ctx->a_cipher, blk, len);
  951. }
  952. static void ccp_decrypt_length(ssh_cipher *cipher, void *blk, int len,
  953. unsigned long seq)
  954. {
  955. struct ccp_context *ctx = container_of(cipher, struct ccp_context, ciph);
  956. ccp_length_op(ctx, blk, len, seq);
  957. chacha20_decrypt(&ctx->a_cipher, blk, len);
  958. }
  959. const ssh_cipheralg ssh2_chacha20_poly1305 = {
  960. .new = ccp_new,
  961. .free = ccp_free,
  962. .setiv = ccp_iv,
  963. .setkey = ccp_key,
  964. .encrypt = ccp_encrypt,
  965. .decrypt = ccp_decrypt,
  966. .encrypt_length = ccp_encrypt_length,
  967. .decrypt_length = ccp_decrypt_length,
  968. .next_message = nullcipher_next_message,
  969. .ssh2_id = "chacha20-poly1305@openssh.com",
  970. .blksize = 1,
  971. .real_keybits = 512,
  972. .padded_keybytes = 64,
  973. .flags = SSH_CIPHER_SEPARATE_LENGTH,
  974. .text_name = "ChaCha20",
  975. .required_mac = &ssh2_poly1305,
  976. };
  977. static const ssh_cipheralg *const ccp_list[] = {
  978. &ssh2_chacha20_poly1305
  979. };
  980. const ssh2_ciphers ssh2_ccp = { lenof(ccp_list), ccp_list };