sshccp.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063
  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. };
  803. static ssh2_mac *poly_ssh2_new(
  804. const ssh2_macalg *alg, ssh_cipher *cipher)
  805. {
  806. struct ccp_context *ctx = container_of(cipher, struct ccp_context, ciph);
  807. ctx->mac_if.vt = alg;
  808. BinarySink_DELEGATE_INIT(&ctx->mac_if, ctx);
  809. return &ctx->mac_if;
  810. }
  811. static void poly_ssh2_free(ssh2_mac *mac)
  812. {
  813. /* Not allocated, just forwarded, no need to free */
  814. }
  815. static void poly_setkey(ssh2_mac *mac, ptrlen key)
  816. {
  817. /* Uses the same context as ChaCha20, so ignore */
  818. }
  819. static void poly_start(ssh2_mac *mac)
  820. {
  821. struct ccp_context *ctx = container_of(mac, struct ccp_context, mac_if);
  822. ctx->mac_initialised = 0;
  823. memset(ctx->mac_iv, 0, 8);
  824. poly1305_init(&ctx->mac);
  825. }
  826. static void poly_BinarySink_write(BinarySink *bs, const void *blkv, size_t len)
  827. {
  828. struct ccp_context *ctx = BinarySink_DOWNCAST(bs, struct ccp_context);
  829. const unsigned char *blk = (const unsigned char *)blkv;
  830. /* First 4 bytes are the IV */
  831. while (ctx->mac_initialised < 4 && len) {
  832. ctx->mac_iv[7 - ctx->mac_initialised] = *blk++;
  833. ++ctx->mac_initialised;
  834. --len;
  835. }
  836. /* Initialise the IV if needed */
  837. if (ctx->mac_initialised == 4) {
  838. chacha20_iv(&ctx->b_cipher, ctx->mac_iv);
  839. ++ctx->mac_initialised; /* Don't do it again */
  840. /* Do first rotation */
  841. chacha20_round(&ctx->b_cipher);
  842. /* Set the poly key */
  843. poly1305_key(&ctx->mac, make_ptrlen(ctx->b_cipher.current, 32));
  844. /* Set the first round as used */
  845. ctx->b_cipher.currentIndex = 64;
  846. }
  847. /* Update the MAC with anything left */
  848. if (len) {
  849. poly1305_feed(&ctx->mac, blk, len);
  850. }
  851. }
  852. static void poly_genresult(ssh2_mac *mac, unsigned char *blk)
  853. {
  854. struct ccp_context *ctx = container_of(mac, struct ccp_context, mac_if);
  855. poly1305_finalise(&ctx->mac, blk);
  856. }
  857. static const char *poly_text_name(ssh2_mac *mac)
  858. {
  859. return "Poly1305";
  860. }
  861. const ssh2_macalg ssh2_poly1305 = {
  862. .new = poly_ssh2_new,
  863. .free = poly_ssh2_free,
  864. .setkey = poly_setkey,
  865. .start = poly_start,
  866. .genresult = poly_genresult,
  867. .text_name = poly_text_name,
  868. .name = "",
  869. .etm_name = "", /* Not selectable individually, just part of
  870. * ChaCha20-Poly1305 */
  871. .len = 16,
  872. .keylen = 0,
  873. };
  874. static ssh_cipher *ccp_new(const ssh_cipheralg *alg)
  875. {
  876. struct ccp_context *ctx = snew(struct ccp_context);
  877. BinarySink_INIT(ctx, poly_BinarySink_write);
  878. poly1305_init(&ctx->mac);
  879. ctx->ciph.vt = alg;
  880. return &ctx->ciph;
  881. }
  882. static void ccp_free(ssh_cipher *cipher)
  883. {
  884. struct ccp_context *ctx = container_of(cipher, struct ccp_context, ciph);
  885. smemclr(&ctx->a_cipher, sizeof(ctx->a_cipher));
  886. smemclr(&ctx->b_cipher, sizeof(ctx->b_cipher));
  887. smemclr(&ctx->mac, sizeof(ctx->mac));
  888. sfree(ctx);
  889. }
  890. static void ccp_iv(ssh_cipher *cipher, const void *iv)
  891. {
  892. /* struct ccp_context *ctx =
  893. container_of(cipher, struct ccp_context, ciph); */
  894. /* IV is set based on the sequence number */
  895. }
  896. static void ccp_key(ssh_cipher *cipher, const void *vkey)
  897. {
  898. const unsigned char *key = (const unsigned char *)vkey;
  899. struct ccp_context *ctx = container_of(cipher, struct ccp_context, ciph);
  900. /* Initialise the a_cipher (for decrypting lengths) with the first 256 bits */
  901. chacha20_key(&ctx->a_cipher, key + 32);
  902. /* Initialise the b_cipher (for content and MAC) with the second 256 bits */
  903. chacha20_key(&ctx->b_cipher, key);
  904. }
  905. static void ccp_encrypt(ssh_cipher *cipher, void *blk, int len)
  906. {
  907. struct ccp_context *ctx = container_of(cipher, struct ccp_context, ciph);
  908. chacha20_encrypt(&ctx->b_cipher, blk, len);
  909. }
  910. static void ccp_decrypt(ssh_cipher *cipher, void *blk, int len)
  911. {
  912. struct ccp_context *ctx = container_of(cipher, struct ccp_context, ciph);
  913. chacha20_decrypt(&ctx->b_cipher, blk, len);
  914. }
  915. static void ccp_length_op(struct ccp_context *ctx, void *blk, int len,
  916. unsigned long seq)
  917. {
  918. unsigned char iv[8];
  919. /*
  920. * According to RFC 4253 (section 6.4), the packet sequence number wraps
  921. * at 2^32, so its 32 high-order bits will always be zero.
  922. */
  923. PUT_32BIT_LSB_FIRST(iv, 0);
  924. PUT_32BIT_LSB_FIRST(iv + 4, seq);
  925. chacha20_iv(&ctx->a_cipher, iv);
  926. chacha20_iv(&ctx->b_cipher, iv);
  927. /* Reset content block count to 1, as the first is the key for Poly1305 */
  928. ++ctx->b_cipher.state[12];
  929. smemclr(iv, sizeof(iv));
  930. }
  931. static void ccp_encrypt_length(ssh_cipher *cipher, void *blk, int len,
  932. unsigned long seq)
  933. {
  934. struct ccp_context *ctx = container_of(cipher, struct ccp_context, ciph);
  935. ccp_length_op(ctx, blk, len, seq);
  936. chacha20_encrypt(&ctx->a_cipher, blk, len);
  937. }
  938. static void ccp_decrypt_length(ssh_cipher *cipher, void *blk, int len,
  939. unsigned long seq)
  940. {
  941. struct ccp_context *ctx = container_of(cipher, struct ccp_context, ciph);
  942. ccp_length_op(ctx, blk, len, seq);
  943. chacha20_decrypt(&ctx->a_cipher, blk, len);
  944. }
  945. const ssh_cipheralg ssh2_chacha20_poly1305 = {
  946. .new = ccp_new,
  947. .free = ccp_free,
  948. .setiv = ccp_iv,
  949. .setkey = ccp_key,
  950. .encrypt = ccp_encrypt,
  951. .decrypt = ccp_decrypt,
  952. .encrypt_length = ccp_encrypt_length,
  953. .decrypt_length = ccp_decrypt_length,
  954. .ssh2_id = "chacha20-poly1305@openssh.com",
  955. .blksize = 1,
  956. .real_keybits = 512,
  957. .padded_keybytes = 64,
  958. .flags = SSH_CIPHER_SEPARATE_LENGTH,
  959. .text_name = "ChaCha20",
  960. .required_mac = &ssh2_poly1305,
  961. };
  962. static const ssh_cipheralg *const ccp_list[] = {
  963. &ssh2_chacha20_poly1305
  964. };
  965. const ssh2_ciphers ssh2_ccp = { lenof(ccp_list), ccp_list };