2 * ChaCha20-Poly1305 Implementation for SSH-2
5 * http://cvsweb.openbsd.org/cgi-bin/cvsweb/src/usr.bin/ssh/PROTOCOL.chacha20poly1305?rev=1.2&content-type=text/x-cvsweb-markup
8 * http://cr.yp.to/chacha/chacha-20080128.pdf
11 * http://cr.yp.to/snuffle/spec.pdf
14 * http://cr.yp.to/mac/poly1305-20050329.pdf
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!
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 ssh2_cipher structure ensures that the MAC is
24 * set (and others ignored) if this cipher is chosen.
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.
39 /* ChaCha20 implementation, only supporting 256-bit keys */
41 /* State for each ChaCha20 instance */
43 /* Current context, usually with the count incremented
44 * 0-3 are the static constant
46 * 12-13 are the counter
49 /* The output of the state above ready to xor */
50 unsigned char current[64];
51 /* The index of the above currently used to allow a true streaming cipher */
55 static INLINE void chacha20_round(struct chacha20 *ctx)
61 memcpy(copy, ctx->state, sizeof(copy));
63 /* A circular rotation for a 32bit number */
64 #define rotl(x, shift) x = ((x << shift) | (x >> (32 - shift)))
66 /* What to do for each quarter round operation */
67 #define qrop(a, b, c, d) \
73 #define quarter(a, b, c, d) \
79 /* Do 20 rounds, in pairs because every other is different */
80 for (i = 0; i < 20; i += 2) {
84 quarter(2, 6, 10, 14);
85 quarter(3, 7, 11, 15);
86 /* Another slightly different round */
87 quarter(0, 5, 10, 15);
88 quarter(1, 6, 11, 12);
93 /* Dump the macros, don't need them littering */
98 /* Add the initial state */
99 for (i = 0; i < 16; ++i) {
100 copy[i] += ctx->state[i];
103 /* Update the content of the xor buffer */
104 for (i = 0; i < 16; ++i) {
105 ctx->current[i * 4 + 0] = copy[i] >> 0;
106 ctx->current[i * 4 + 1] = copy[i] >> 8;
107 ctx->current[i * 4 + 2] = copy[i] >> 16;
108 ctx->current[i * 4 + 3] = copy[i] >> 24;
110 /* State full, reset pointer to beginning */
111 ctx->currentIndex = 0;
112 smemclr(copy, sizeof(copy));
114 /* Increment round counter */
116 /* Check for overflow, not done in one line so the 32 bits are chopped by the type */
117 if (!(uint32)(ctx->state[12])) {
122 /* Initialise context with 256bit key */
123 static void chacha20_key(struct chacha20 *ctx, const unsigned char *key)
125 static const char constant[16] = "expand 32-byte k";
127 /* Add the fixed string to the start of the state */
128 ctx->state[0] = GET_32BIT_LSB_FIRST(constant + 0);
129 ctx->state[1] = GET_32BIT_LSB_FIRST(constant + 4);
130 ctx->state[2] = GET_32BIT_LSB_FIRST(constant + 8);
131 ctx->state[3] = GET_32BIT_LSB_FIRST(constant + 12);
134 ctx->state[4] = GET_32BIT_LSB_FIRST(key + 0);
135 ctx->state[5] = GET_32BIT_LSB_FIRST(key + 4);
136 ctx->state[6] = GET_32BIT_LSB_FIRST(key + 8);
137 ctx->state[7] = GET_32BIT_LSB_FIRST(key + 12);
138 ctx->state[8] = GET_32BIT_LSB_FIRST(key + 16);
139 ctx->state[9] = GET_32BIT_LSB_FIRST(key + 20);
140 ctx->state[10] = GET_32BIT_LSB_FIRST(key + 24);
141 ctx->state[11] = GET_32BIT_LSB_FIRST(key + 28);
143 /* New key, dump context */
144 ctx->currentIndex = 64;
147 static void chacha20_iv(struct chacha20 *ctx, const unsigned char *iv)
151 ctx->state[14] = GET_32BIT_MSB_FIRST(iv);
152 ctx->state[15] = GET_32BIT_MSB_FIRST(iv + 4);
154 /* New IV, dump context */
155 ctx->currentIndex = 64;
158 static void chacha20_encrypt(struct chacha20 *ctx, unsigned char *blk, int len)
161 /* If we don't have any state left, then cycle to the next */
162 if (ctx->currentIndex >= 64) {
166 /* Do the xor while there's some state left and some plaintext left */
167 while (ctx->currentIndex < 64 && len) {
168 *blk++ ^= ctx->current[ctx->currentIndex++];
174 /* Decrypt is encrypt... It's xor against a PRNG... */
175 static INLINE void chacha20_decrypt(struct chacha20 *ctx,
176 unsigned char *blk, int len)
178 chacha20_encrypt(ctx, blk, len);
181 /* Poly1305 implementation (no AES, nonce is not encrypted) */
183 #define NWORDS ((130 + BIGNUM_INT_BITS-1) / BIGNUM_INT_BITS)
184 typedef struct bigval {
188 static void bigval_clear(bigval *r)
191 for (i = 0; i < NWORDS; i++)
195 static void bigval_import_le(bigval *r, const void *vdata, int len)
197 const unsigned char *data = (const unsigned char *)vdata;
200 for (i = 0; i < len; i++)
201 r->w[i / BIGNUM_INT_BYTES] |= data[i] << (8 * (i % BIGNUM_INT_BYTES));
204 static void bigval_export_le(const bigval *r, void *vdata, int len)
206 unsigned char *data = (unsigned char *)vdata;
208 for (i = 0; i < len; i++)
209 data[i] = r->w[i / BIGNUM_INT_BYTES] >> (8 * (i % BIGNUM_INT_BYTES));
213 * Addition of bigvals, not mod p.
215 static void bigval_add(bigval *r, const bigval *a, const bigval *b)
217 #if BIGNUM_INT_BITS == 32
218 /* ./contrib/make1305.py add 32 */
241 #elif BIGNUM_INT_BITS == 16
242 /* ./contrib/make1305.py add 16 */
282 #error Run contrib/make1305.py again with a different bit count
287 * Multiplication of bigvals mod p. Uses r as temporary storage, so
288 * don't pass r aliasing a or b.
290 static void bigval_mul_mod_p(bigval *r, const bigval *a, const bigval *b)
292 #if BIGNUM_INT_BITS == 32
293 /* ./contrib/make1305.py mul 32 */
300 tmp = (BignumDblInt)(a->w[0]) * (b->w[0]);
301 acclo += tmp & BIGNUM_INT_MASK;
304 acclo = acchi + (acclo >> 32);
306 tmp = (BignumDblInt)(a->w[0]) * (b->w[1]);
307 acclo += tmp & BIGNUM_INT_MASK;
309 tmp = (BignumDblInt)(a->w[1]) * (b->w[0]);
310 acclo += tmp & BIGNUM_INT_MASK;
313 acclo = acchi + (acclo >> 32);
315 tmp = (BignumDblInt)(a->w[0]) * (b->w[2]);
316 acclo += tmp & BIGNUM_INT_MASK;
318 tmp = (BignumDblInt)(a->w[1]) * (b->w[1]);
319 acclo += tmp & BIGNUM_INT_MASK;
321 tmp = (BignumDblInt)(a->w[2]) * (b->w[0]);
322 acclo += tmp & BIGNUM_INT_MASK;
325 acclo = acchi + (acclo >> 32);
327 tmp = (BignumDblInt)(a->w[0]) * (b->w[3]);
328 acclo += tmp & BIGNUM_INT_MASK;
330 tmp = (BignumDblInt)(a->w[1]) * (b->w[2]);
331 acclo += tmp & BIGNUM_INT_MASK;
333 tmp = (BignumDblInt)(a->w[2]) * (b->w[1]);
334 acclo += tmp & BIGNUM_INT_MASK;
336 tmp = (BignumDblInt)(a->w[3]) * (b->w[0]);
337 acclo += tmp & BIGNUM_INT_MASK;
340 acclo = acchi + (acclo >> 32);
342 tmp = (BignumDblInt)(a->w[0]) * (b->w[4]);
343 acclo += tmp & BIGNUM_INT_MASK;
345 tmp = (BignumDblInt)(a->w[1]) * (b->w[3]);
346 acclo += tmp & BIGNUM_INT_MASK;
348 tmp = (BignumDblInt)(a->w[2]) * (b->w[2]);
349 acclo += tmp & BIGNUM_INT_MASK;
351 tmp = (BignumDblInt)(a->w[3]) * (b->w[1]);
352 acclo += tmp & BIGNUM_INT_MASK;
354 tmp = (BignumDblInt)(a->w[4]) * (b->w[0]);
355 acclo += tmp & BIGNUM_INT_MASK;
357 r->w[4] = acclo & (((BignumInt)1 << 2)-1);
359 acc2lo += ((acclo >> 2) & (((BignumInt)1 << 30)-1)) * ((BignumDblInt)5 << 0);
360 acclo = acchi + (acclo >> 32);
362 tmp = (BignumDblInt)(a->w[1]) * (b->w[4]);
363 acclo += tmp & BIGNUM_INT_MASK;
365 tmp = (BignumDblInt)(a->w[2]) * (b->w[3]);
366 acclo += tmp & BIGNUM_INT_MASK;
368 tmp = (BignumDblInt)(a->w[3]) * (b->w[2]);
369 acclo += tmp & BIGNUM_INT_MASK;
371 tmp = (BignumDblInt)(a->w[4]) * (b->w[1]);
372 acclo += tmp & BIGNUM_INT_MASK;
374 acc2lo += (acclo & (((BignumInt)1 << 2)-1)) * ((BignumDblInt)5 << 30);
378 acc2lo += ((acclo >> 2) & (((BignumInt)1 << 30)-1)) * ((BignumDblInt)5 << 0);
379 acclo = acchi + (acclo >> 32);
381 tmp = (BignumDblInt)(a->w[2]) * (b->w[4]);
382 acclo += tmp & BIGNUM_INT_MASK;
384 tmp = (BignumDblInt)(a->w[3]) * (b->w[3]);
385 acclo += tmp & BIGNUM_INT_MASK;
387 tmp = (BignumDblInt)(a->w[4]) * (b->w[2]);
388 acclo += tmp & BIGNUM_INT_MASK;
390 acc2lo += (acclo & (((BignumInt)1 << 2)-1)) * ((BignumDblInt)5 << 30);
394 acc2lo += ((acclo >> 2) & (((BignumInt)1 << 30)-1)) * ((BignumDblInt)5 << 0);
395 acclo = acchi + (acclo >> 32);
397 tmp = (BignumDblInt)(a->w[3]) * (b->w[4]);
398 acclo += tmp & BIGNUM_INT_MASK;
400 tmp = (BignumDblInt)(a->w[4]) * (b->w[3]);
401 acclo += tmp & BIGNUM_INT_MASK;
403 acc2lo += (acclo & (((BignumInt)1 << 2)-1)) * ((BignumDblInt)5 << 30);
407 acc2lo += ((acclo >> 2) & (((BignumInt)1 << 30)-1)) * ((BignumDblInt)5 << 0);
408 acclo = acchi + (acclo >> 32);
410 tmp = (BignumDblInt)(a->w[4]) * (b->w[4]);
411 acclo += tmp & BIGNUM_INT_MASK;
413 acc2lo += (acclo & (((BignumInt)1 << 2)-1)) * ((BignumDblInt)5 << 30);
417 acc2lo += ((acclo >> 2) & (((BignumInt)1 << 2)-1)) * ((BignumDblInt)5 << 0);
421 acc2lo += ((acclo >> 4) & (((BignumInt)1 << 28)-1)) * ((BignumDblInt)25 << 0);
422 acclo = acchi + (acclo >> 32);
424 acc2lo += (acclo & (((BignumInt)1 << 4)-1)) * ((BignumDblInt)25 << 28);
428 acc2lo += ((acclo >> 4) & (((BignumInt)1 << 28)-1)) * ((BignumDblInt)25 << 0);
429 acclo = acchi + (acclo >> 32);
443 #elif BIGNUM_INT_BITS == 16
444 /* ./contrib/make1305.py mul 16 */
451 tmp = (BignumDblInt)(a->w[0]) * (b->w[0]);
452 acclo += tmp & BIGNUM_INT_MASK;
455 acclo = acchi + (acclo >> 16);
457 tmp = (BignumDblInt)(a->w[0]) * (b->w[1]);
458 acclo += tmp & BIGNUM_INT_MASK;
460 tmp = (BignumDblInt)(a->w[1]) * (b->w[0]);
461 acclo += tmp & BIGNUM_INT_MASK;
464 acclo = acchi + (acclo >> 16);
466 tmp = (BignumDblInt)(a->w[0]) * (b->w[2]);
467 acclo += tmp & BIGNUM_INT_MASK;
469 tmp = (BignumDblInt)(a->w[1]) * (b->w[1]);
470 acclo += tmp & BIGNUM_INT_MASK;
472 tmp = (BignumDblInt)(a->w[2]) * (b->w[0]);
473 acclo += tmp & BIGNUM_INT_MASK;
476 acclo = acchi + (acclo >> 16);
478 tmp = (BignumDblInt)(a->w[0]) * (b->w[3]);
479 acclo += tmp & BIGNUM_INT_MASK;
481 tmp = (BignumDblInt)(a->w[1]) * (b->w[2]);
482 acclo += tmp & BIGNUM_INT_MASK;
484 tmp = (BignumDblInt)(a->w[2]) * (b->w[1]);
485 acclo += tmp & BIGNUM_INT_MASK;
487 tmp = (BignumDblInt)(a->w[3]) * (b->w[0]);
488 acclo += tmp & BIGNUM_INT_MASK;
491 acclo = acchi + (acclo >> 16);
493 tmp = (BignumDblInt)(a->w[0]) * (b->w[4]);
494 acclo += tmp & BIGNUM_INT_MASK;
496 tmp = (BignumDblInt)(a->w[1]) * (b->w[3]);
497 acclo += tmp & BIGNUM_INT_MASK;
499 tmp = (BignumDblInt)(a->w[2]) * (b->w[2]);
500 acclo += tmp & BIGNUM_INT_MASK;
502 tmp = (BignumDblInt)(a->w[3]) * (b->w[1]);
503 acclo += tmp & BIGNUM_INT_MASK;
505 tmp = (BignumDblInt)(a->w[4]) * (b->w[0]);
506 acclo += tmp & BIGNUM_INT_MASK;
509 acclo = acchi + (acclo >> 16);
511 tmp = (BignumDblInt)(a->w[0]) * (b->w[5]);
512 acclo += tmp & BIGNUM_INT_MASK;
514 tmp = (BignumDblInt)(a->w[1]) * (b->w[4]);
515 acclo += tmp & BIGNUM_INT_MASK;
517 tmp = (BignumDblInt)(a->w[2]) * (b->w[3]);
518 acclo += tmp & BIGNUM_INT_MASK;
520 tmp = (BignumDblInt)(a->w[3]) * (b->w[2]);
521 acclo += tmp & BIGNUM_INT_MASK;
523 tmp = (BignumDblInt)(a->w[4]) * (b->w[1]);
524 acclo += tmp & BIGNUM_INT_MASK;
526 tmp = (BignumDblInt)(a->w[5]) * (b->w[0]);
527 acclo += tmp & BIGNUM_INT_MASK;
530 acclo = acchi + (acclo >> 16);
532 tmp = (BignumDblInt)(a->w[0]) * (b->w[6]);
533 acclo += tmp & BIGNUM_INT_MASK;
535 tmp = (BignumDblInt)(a->w[1]) * (b->w[5]);
536 acclo += tmp & BIGNUM_INT_MASK;
538 tmp = (BignumDblInt)(a->w[2]) * (b->w[4]);
539 acclo += tmp & BIGNUM_INT_MASK;
541 tmp = (BignumDblInt)(a->w[3]) * (b->w[3]);
542 acclo += tmp & BIGNUM_INT_MASK;
544 tmp = (BignumDblInt)(a->w[4]) * (b->w[2]);
545 acclo += tmp & BIGNUM_INT_MASK;
547 tmp = (BignumDblInt)(a->w[5]) * (b->w[1]);
548 acclo += tmp & BIGNUM_INT_MASK;
550 tmp = (BignumDblInt)(a->w[6]) * (b->w[0]);
551 acclo += tmp & BIGNUM_INT_MASK;
554 acclo = acchi + (acclo >> 16);
556 tmp = (BignumDblInt)(a->w[0]) * (b->w[7]);
557 acclo += tmp & BIGNUM_INT_MASK;
559 tmp = (BignumDblInt)(a->w[1]) * (b->w[6]);
560 acclo += tmp & BIGNUM_INT_MASK;
562 tmp = (BignumDblInt)(a->w[2]) * (b->w[5]);
563 acclo += tmp & BIGNUM_INT_MASK;
565 tmp = (BignumDblInt)(a->w[3]) * (b->w[4]);
566 acclo += tmp & BIGNUM_INT_MASK;
568 tmp = (BignumDblInt)(a->w[4]) * (b->w[3]);
569 acclo += tmp & BIGNUM_INT_MASK;
571 tmp = (BignumDblInt)(a->w[5]) * (b->w[2]);
572 acclo += tmp & BIGNUM_INT_MASK;
574 tmp = (BignumDblInt)(a->w[6]) * (b->w[1]);
575 acclo += tmp & BIGNUM_INT_MASK;
577 tmp = (BignumDblInt)(a->w[7]) * (b->w[0]);
578 acclo += tmp & BIGNUM_INT_MASK;
581 acclo = acchi + (acclo >> 16);
583 tmp = (BignumDblInt)(a->w[0]) * (b->w[8]);
584 acclo += tmp & BIGNUM_INT_MASK;
586 tmp = (BignumDblInt)(a->w[1]) * (b->w[7]);
587 acclo += tmp & BIGNUM_INT_MASK;
589 tmp = (BignumDblInt)(a->w[2]) * (b->w[6]);
590 acclo += tmp & BIGNUM_INT_MASK;
592 tmp = (BignumDblInt)(a->w[3]) * (b->w[5]);
593 acclo += tmp & BIGNUM_INT_MASK;
595 tmp = (BignumDblInt)(a->w[4]) * (b->w[4]);
596 acclo += tmp & BIGNUM_INT_MASK;
598 tmp = (BignumDblInt)(a->w[5]) * (b->w[3]);
599 acclo += tmp & BIGNUM_INT_MASK;
601 tmp = (BignumDblInt)(a->w[6]) * (b->w[2]);
602 acclo += tmp & BIGNUM_INT_MASK;
604 tmp = (BignumDblInt)(a->w[7]) * (b->w[1]);
605 acclo += tmp & BIGNUM_INT_MASK;
607 tmp = (BignumDblInt)(a->w[8]) * (b->w[0]);
608 acclo += tmp & BIGNUM_INT_MASK;
610 r->w[8] = acclo & (((BignumInt)1 << 2)-1);
612 acc2lo += ((acclo >> 2) & (((BignumInt)1 << 14)-1)) * ((BignumDblInt)5 << 0);
613 acclo = acchi + (acclo >> 16);
615 tmp = (BignumDblInt)(a->w[1]) * (b->w[8]);
616 acclo += tmp & BIGNUM_INT_MASK;
618 tmp = (BignumDblInt)(a->w[2]) * (b->w[7]);
619 acclo += tmp & BIGNUM_INT_MASK;
621 tmp = (BignumDblInt)(a->w[3]) * (b->w[6]);
622 acclo += tmp & BIGNUM_INT_MASK;
624 tmp = (BignumDblInt)(a->w[4]) * (b->w[5]);
625 acclo += tmp & BIGNUM_INT_MASK;
627 tmp = (BignumDblInt)(a->w[5]) * (b->w[4]);
628 acclo += tmp & BIGNUM_INT_MASK;
630 tmp = (BignumDblInt)(a->w[6]) * (b->w[3]);
631 acclo += tmp & BIGNUM_INT_MASK;
633 tmp = (BignumDblInt)(a->w[7]) * (b->w[2]);
634 acclo += tmp & BIGNUM_INT_MASK;
636 tmp = (BignumDblInt)(a->w[8]) * (b->w[1]);
637 acclo += tmp & BIGNUM_INT_MASK;
639 acc2lo += (acclo & (((BignumInt)1 << 2)-1)) * ((BignumDblInt)5 << 14);
643 acc2lo += ((acclo >> 2) & (((BignumInt)1 << 14)-1)) * ((BignumDblInt)5 << 0);
644 acclo = acchi + (acclo >> 16);
646 tmp = (BignumDblInt)(a->w[2]) * (b->w[8]);
647 acclo += tmp & BIGNUM_INT_MASK;
649 tmp = (BignumDblInt)(a->w[3]) * (b->w[7]);
650 acclo += tmp & BIGNUM_INT_MASK;
652 tmp = (BignumDblInt)(a->w[4]) * (b->w[6]);
653 acclo += tmp & BIGNUM_INT_MASK;
655 tmp = (BignumDblInt)(a->w[5]) * (b->w[5]);
656 acclo += tmp & BIGNUM_INT_MASK;
658 tmp = (BignumDblInt)(a->w[6]) * (b->w[4]);
659 acclo += tmp & BIGNUM_INT_MASK;
661 tmp = (BignumDblInt)(a->w[7]) * (b->w[3]);
662 acclo += tmp & BIGNUM_INT_MASK;
664 tmp = (BignumDblInt)(a->w[8]) * (b->w[2]);
665 acclo += tmp & BIGNUM_INT_MASK;
667 acc2lo += (acclo & (((BignumInt)1 << 2)-1)) * ((BignumDblInt)5 << 14);
671 acc2lo += ((acclo >> 2) & (((BignumInt)1 << 14)-1)) * ((BignumDblInt)5 << 0);
672 acclo = acchi + (acclo >> 16);
674 tmp = (BignumDblInt)(a->w[3]) * (b->w[8]);
675 acclo += tmp & BIGNUM_INT_MASK;
677 tmp = (BignumDblInt)(a->w[4]) * (b->w[7]);
678 acclo += tmp & BIGNUM_INT_MASK;
680 tmp = (BignumDblInt)(a->w[5]) * (b->w[6]);
681 acclo += tmp & BIGNUM_INT_MASK;
683 tmp = (BignumDblInt)(a->w[6]) * (b->w[5]);
684 acclo += tmp & BIGNUM_INT_MASK;
686 tmp = (BignumDblInt)(a->w[7]) * (b->w[4]);
687 acclo += tmp & BIGNUM_INT_MASK;
689 tmp = (BignumDblInt)(a->w[8]) * (b->w[3]);
690 acclo += tmp & BIGNUM_INT_MASK;
692 acc2lo += (acclo & (((BignumInt)1 << 2)-1)) * ((BignumDblInt)5 << 14);
696 acc2lo += ((acclo >> 2) & (((BignumInt)1 << 14)-1)) * ((BignumDblInt)5 << 0);
697 acclo = acchi + (acclo >> 16);
699 tmp = (BignumDblInt)(a->w[4]) * (b->w[8]);
700 acclo += tmp & BIGNUM_INT_MASK;
702 tmp = (BignumDblInt)(a->w[5]) * (b->w[7]);
703 acclo += tmp & BIGNUM_INT_MASK;
705 tmp = (BignumDblInt)(a->w[6]) * (b->w[6]);
706 acclo += tmp & BIGNUM_INT_MASK;
708 tmp = (BignumDblInt)(a->w[7]) * (b->w[5]);
709 acclo += tmp & BIGNUM_INT_MASK;
711 tmp = (BignumDblInt)(a->w[8]) * (b->w[4]);
712 acclo += tmp & BIGNUM_INT_MASK;
714 acc2lo += (acclo & (((BignumInt)1 << 2)-1)) * ((BignumDblInt)5 << 14);
718 acc2lo += ((acclo >> 2) & (((BignumInt)1 << 14)-1)) * ((BignumDblInt)5 << 0);
719 acclo = acchi + (acclo >> 16);
721 tmp = (BignumDblInt)(a->w[5]) * (b->w[8]);
722 acclo += tmp & BIGNUM_INT_MASK;
724 tmp = (BignumDblInt)(a->w[6]) * (b->w[7]);
725 acclo += tmp & BIGNUM_INT_MASK;
727 tmp = (BignumDblInt)(a->w[7]) * (b->w[6]);
728 acclo += tmp & BIGNUM_INT_MASK;
730 tmp = (BignumDblInt)(a->w[8]) * (b->w[5]);
731 acclo += tmp & BIGNUM_INT_MASK;
733 acc2lo += (acclo & (((BignumInt)1 << 2)-1)) * ((BignumDblInt)5 << 14);
737 acc2lo += ((acclo >> 2) & (((BignumInt)1 << 14)-1)) * ((BignumDblInt)5 << 0);
738 acclo = acchi + (acclo >> 16);
740 tmp = (BignumDblInt)(a->w[6]) * (b->w[8]);
741 acclo += tmp & BIGNUM_INT_MASK;
743 tmp = (BignumDblInt)(a->w[7]) * (b->w[7]);
744 acclo += tmp & BIGNUM_INT_MASK;
746 tmp = (BignumDblInt)(a->w[8]) * (b->w[6]);
747 acclo += tmp & BIGNUM_INT_MASK;
749 acc2lo += (acclo & (((BignumInt)1 << 2)-1)) * ((BignumDblInt)5 << 14);
753 acc2lo += ((acclo >> 2) & (((BignumInt)1 << 14)-1)) * ((BignumDblInt)5 << 0);
754 acclo = acchi + (acclo >> 16);
756 tmp = (BignumDblInt)(a->w[7]) * (b->w[8]);
757 acclo += tmp & BIGNUM_INT_MASK;
759 tmp = (BignumDblInt)(a->w[8]) * (b->w[7]);
760 acclo += tmp & BIGNUM_INT_MASK;
762 acc2lo += (acclo & (((BignumInt)1 << 2)-1)) * ((BignumDblInt)5 << 14);
766 acc2lo += ((acclo >> 2) & (((BignumInt)1 << 14)-1)) * ((BignumDblInt)5 << 0);
767 acclo = acchi + (acclo >> 16);
769 tmp = (BignumDblInt)(a->w[8]) * (b->w[8]);
770 acclo += tmp & BIGNUM_INT_MASK;
772 acc2lo += (acclo & (((BignumInt)1 << 2)-1)) * ((BignumDblInt)5 << 14);
776 acc2lo += ((acclo >> 2) & (((BignumInt)1 << 2)-1)) * ((BignumDblInt)5 << 0);
780 acc2lo += ((acclo >> 4) & (((BignumInt)1 << 12)-1)) * ((BignumDblInt)25 << 0);
781 acclo = acchi + (acclo >> 16);
783 acc2lo += (acclo & (((BignumInt)1 << 4)-1)) * ((BignumDblInt)25 << 12);
787 acc2lo += ((acclo >> 4) & (((BignumInt)1 << 12)-1)) * ((BignumDblInt)25 << 0);
788 acclo = acchi + (acclo >> 16);
815 #error Run contrib/make1305.py again with a different bit count
819 static void bigval_final_reduce(bigval *n)
821 #if BIGNUM_INT_BITS == 32
822 /* ./contrib/make1305.py final_reduce 32 */
825 acclo += 5 * ((n->w[4] >> 2) + 1);
835 acclo = 5 * (acclo >> 2);
851 n->w[4] &= (1 << 2) - 1;
852 #elif BIGNUM_INT_BITS == 16
853 /* ./contrib/make1305.py final_reduce 16 */
856 acclo += 5 * ((n->w[8] >> 2) + 1);
874 acclo = 5 * (acclo >> 2);
902 n->w[8] &= (1 << 2) - 1;
904 #error Run contrib/make1305.py again with a different bit count
909 unsigned char nonce[16];
913 /* Buffer in case we get less that a multiple of 16 bytes */
914 unsigned char buffer[16];
918 static void poly1305_init(struct poly1305 *ctx)
920 memset(ctx->nonce, 0, 16);
921 ctx->bufferIndex = 0;
922 bigval_clear(&ctx->h);
925 /* Takes a 256 bit key */
926 static void poly1305_key(struct poly1305 *ctx, const unsigned char *key)
928 unsigned char key_copy[16];
929 memcpy(key_copy, key, 16);
931 /* Key the MAC itself
932 * bytes 4, 8, 12 and 16 are required to have their top four bits clear */
935 key_copy[11] &= 0x0f;
936 key_copy[15] &= 0x0f;
937 /* bytes 5, 9 and 13 are required to have their bottom two bits clear */
940 key_copy[12] &= 0xfc;
941 bigval_import_le(&ctx->r, key_copy, 16);
942 smemclr(key_copy, sizeof(key_copy));
944 /* Use second 128 bits are the nonce */
945 memcpy(ctx->nonce, key+16, 16);
948 /* Feed up to 16 bytes (should only be less for the last chunk) */
949 static void poly1305_feed_chunk(struct poly1305 *ctx,
950 const unsigned char *chunk, int len)
953 bigval_import_le(&c, chunk, len);
954 c.w[len / BIGNUM_INT_BYTES] |= 1 << (8 * (len % BIGNUM_INT_BYTES));
955 bigval_add(&c, &c, &ctx->h);
956 bigval_mul_mod_p(&ctx->h, &c, &ctx->r);
959 static void poly1305_feed(struct poly1305 *ctx,
960 const unsigned char *buf, int len)
962 /* Check for stuff left in the buffer from last time */
963 if (ctx->bufferIndex) {
964 /* Try to fill up to 16 */
965 while (ctx->bufferIndex < 16 && len) {
966 ctx->buffer[ctx->bufferIndex++] = *buf++;
969 if (ctx->bufferIndex == 16) {
970 poly1305_feed_chunk(ctx, ctx->buffer, 16);
971 ctx->bufferIndex = 0;
975 /* Process 16 byte whole chunks */
977 poly1305_feed_chunk(ctx, buf, 16);
982 /* Cache stuff that's left over */
984 memcpy(ctx->buffer, buf, len);
985 ctx->bufferIndex = len;
989 /* Finalise and populate buffer with 16 byte with MAC */
990 static void poly1305_finalise(struct poly1305 *ctx, unsigned char *mac)
994 if (ctx->bufferIndex) {
995 poly1305_feed_chunk(ctx, ctx->buffer, ctx->bufferIndex);
998 bigval_import_le(&tmp, ctx->nonce, 16);
999 bigval_final_reduce(&ctx->h);
1000 bigval_add(&tmp, &tmp, &ctx->h);
1001 bigval_export_le(&tmp, mac, 16);
1006 struct ccp_context {
1007 struct chacha20 a_cipher; /* Used for length */
1008 struct chacha20 b_cipher; /* Used for content */
1010 /* Cache of the first 4 bytes because they are the sequence number */
1011 /* Kept in 8 bytes with the top as zero to allow easy passing to setiv */
1012 int mac_initialised; /* Where we have got to in filling mac_iv */
1013 unsigned char mac_iv[8];
1015 struct poly1305 mac;
1018 static void *poly_make_context(void *ctx)
1023 static void poly_free_context(void *ctx)
1025 /* Not allocated, just forwarded, no need to free */
1028 static void poly_setkey(void *ctx, unsigned char *key)
1030 /* Uses the same context as ChaCha20, so ignore */
1033 static void poly_start(void *handle)
1035 struct ccp_context *ctx = (struct ccp_context *)handle;
1037 ctx->mac_initialised = 0;
1038 memset(ctx->mac_iv, 0, 8);
1039 poly1305_init(&ctx->mac);
1042 static void poly_bytes(void *handle, unsigned char const *blk, int len)
1044 struct ccp_context *ctx = (struct ccp_context *)handle;
1046 /* First 4 bytes are the IV */
1047 while (ctx->mac_initialised < 4 && len) {
1048 ctx->mac_iv[7 - ctx->mac_initialised] = *blk++;
1049 ++ctx->mac_initialised;
1053 /* Initialise the IV if needed */
1054 if (ctx->mac_initialised == 4) {
1055 chacha20_iv(&ctx->b_cipher, ctx->mac_iv);
1056 ++ctx->mac_initialised; /* Don't do it again */
1058 /* Do first rotation */
1059 chacha20_round(&ctx->b_cipher);
1061 /* Set the poly key */
1062 poly1305_key(&ctx->mac, ctx->b_cipher.current);
1064 /* Set the first round as used */
1065 ctx->b_cipher.currentIndex = 64;
1068 /* Update the MAC with anything left */
1070 poly1305_feed(&ctx->mac, blk, len);
1074 static void poly_genresult(void *handle, unsigned char *blk)
1076 struct ccp_context *ctx = (struct ccp_context *)handle;
1077 poly1305_finalise(&ctx->mac, blk);
1080 static int poly_verresult(void *handle, unsigned char const *blk)
1082 struct ccp_context *ctx = (struct ccp_context *)handle;
1084 unsigned char mac[16];
1085 poly1305_finalise(&ctx->mac, mac);
1086 res = smemeq(blk, mac, 16);
1090 /* The generic poly operation used before generate and verify */
1091 static void poly_op(void *handle, unsigned char *blk, int len, unsigned long seq)
1093 unsigned char iv[4];
1095 PUT_32BIT_MSB_FIRST(iv, seq);
1096 /* poly_bytes expects the first 4 bytes to be the IV */
1097 poly_bytes(handle, iv, 4);
1098 smemclr(iv, sizeof(iv));
1099 poly_bytes(handle, blk, len);
1102 static void poly_generate(void *handle, unsigned char *blk, int len, unsigned long seq)
1104 poly_op(handle, blk, len, seq);
1105 poly_genresult(handle, blk+len);
1108 static int poly_verify(void *handle, unsigned char *blk, int len, unsigned long seq)
1110 poly_op(handle, blk, len, seq);
1111 return poly_verresult(handle, blk+len);
1114 static const struct ssh_mac ssh2_poly1305 = {
1115 poly_make_context, poly_free_context,
1118 /* whole-packet operations */
1119 poly_generate, poly_verify,
1121 /* partial-packet operations */
1122 poly_start, poly_bytes, poly_genresult, poly_verresult,
1124 "", "", /* Not selectable individually, just part of ChaCha20-Poly1305 */
1128 static void *ccp_make_context(void)
1130 struct ccp_context *ctx = snew(struct ccp_context);
1132 poly1305_init(&ctx->mac);
1137 static void ccp_free_context(void *vctx)
1139 struct ccp_context *ctx = (struct ccp_context *)vctx;
1140 smemclr(&ctx->a_cipher, sizeof(ctx->a_cipher));
1141 smemclr(&ctx->b_cipher, sizeof(ctx->b_cipher));
1142 smemclr(&ctx->mac, sizeof(ctx->mac));
1146 static void ccp_iv(void *vctx, unsigned char *iv)
1148 /* struct ccp_context *ctx = (struct ccp_context *)vctx; */
1149 /* IV is set based on the sequence number */
1152 static void ccp_key(void *vctx, unsigned char *key)
1154 struct ccp_context *ctx = (struct ccp_context *)vctx;
1155 /* Initialise the a_cipher (for decrypting lengths) with the first 256 bits */
1156 chacha20_key(&ctx->a_cipher, key + 32);
1157 /* Initialise the b_cipher (for content and MAC) with the second 256 bits */
1158 chacha20_key(&ctx->b_cipher, key);
1161 static void ccp_encrypt(void *vctx, unsigned char *blk, int len)
1163 struct ccp_context *ctx = (struct ccp_context *)vctx;
1164 chacha20_encrypt(&ctx->b_cipher, blk, len);
1167 static void ccp_decrypt(void *vctx, unsigned char *blk, int len)
1169 struct ccp_context *ctx = (struct ccp_context *)vctx;
1170 chacha20_decrypt(&ctx->b_cipher, blk, len);
1173 static void ccp_length_op(struct ccp_context *ctx, unsigned char *blk, int len,
1176 unsigned char iv[8];
1177 PUT_32BIT_LSB_FIRST(iv, seq >> 32);
1178 PUT_32BIT_LSB_FIRST(iv + 4, seq);
1179 chacha20_iv(&ctx->a_cipher, iv);
1180 chacha20_iv(&ctx->b_cipher, iv);
1181 /* Reset content block count to 1, as the first is the key for Poly1305 */
1182 ++ctx->b_cipher.state[12];
1183 smemclr(iv, sizeof(iv));
1186 static void ccp_encrypt_length(void *vctx, unsigned char *blk, int len,
1189 struct ccp_context *ctx = (struct ccp_context *)vctx;
1190 ccp_length_op(ctx, blk, len, seq);
1191 chacha20_encrypt(&ctx->a_cipher, blk, len);
1194 static void ccp_decrypt_length(void *vctx, unsigned char *blk, int len,
1197 struct ccp_context *ctx = (struct ccp_context *)vctx;
1198 ccp_length_op(ctx, blk, len, seq);
1199 chacha20_decrypt(&ctx->a_cipher, blk, len);
1202 static const struct ssh2_cipher ssh2_chacha20_poly1305 = {
1213 "chacha20-poly1305@openssh.com",
1214 1, 512, SSH_CIPHER_SEPARATE_LENGTH, "ChaCha20",
1219 static const struct ssh2_cipher *const ccp_list[] = {
1220 &ssh2_chacha20_poly1305
1223 const struct ssh2_ciphers ssh2_ccp = {
1224 sizeof(ccp_list) / sizeof(*ccp_list),