3 This file is part of the AVR-Crypto-Lib.
4 Copyright (C) 2006-2015 Daniel Otte (bg@nerilex.org)
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 * \license GPLv3 or later
29 #define STRING(x) STRING2(x)
30 #define STR_LINE STRING(__LINE__)
36 #include "bigint_io.h"
39 #define MAX(a,b) (((a)>(b))?(a):(b))
43 #define MIN(a,b) (((a)<(b))?(a):(b))
46 #define SET_FBS(a, v) do{(a)->info &=0xF8; (a)->info |= (v);}while(0)
47 #define GET_FBS(a) ((a)->info&BIGINT_FBS_MASK)
48 #define SET_NEG(a) (a)->info |= BIGINT_NEG_MASK
49 #define SET_POS(a) (a)->info &= ~BIGINT_NEG_MASK
50 #define XCHG(a,b) do{(a)^=(b); (b)^=(a); (a)^=(b);}while(0)
51 #define XCHG_PTR(a,b) do{ a = (void*)(((uint16_t)(a)) ^ ((uint16_t)(b))); \
52 b = (void*)(((uint16_t)(a)) ^ ((uint16_t)(b))); \
53 a = (void*)(((uint16_t)(a)) ^ ((uint16_t)(b)));}while(0)
55 #define GET_SIGN(a) ((a)->info&BIGINT_NEG_MASK)
57 /******************************************************************************/
59 void bigint_copy(bigint_t *dest, const bigint_t *src){
60 memcpy(dest->wordv, src->wordv, src->length_W);
61 dest->length_W = src->length_W;
62 dest->info = src->info;
65 /******************************************************************************/
67 /* this should be implemented in assembly */
69 void bigint_add_u(bigint_t *dest, const bigint_t *a, const bigint_t *b){
71 if(a->length_W < b->length_W){
74 for(i=0; i<b->length_W; ++i){
75 t = a->wordv[i] + b->wordv[i] + t;
76 dest->wordv[i] = (uint8_t)t;
79 for(; i<a->length_W; ++i){
81 dest->wordv[i] = (uint8_t)t;
89 /******************************************************************************/
91 /* this should be implemented in assembly */
93 void bigint_add_scale_u(bigint_t *dest, const bigint_t *a, uint16_t scale){
96 if(scale>dest->length_W)
97 memset(dest->wordv+dest->length_W, 0, scale-dest->length_W);
98 for(i=scale; i<a->length_W+scale; ++i,++j){
100 if(dest->length_W>i){
103 dest->wordv[i] = (uint8_t)t;
107 if(dest->length_W>i){
108 t = dest->wordv[i] + t;
110 dest->wordv[i] = (uint8_t)t;
114 if(dest->length_W < i){
120 /******************************************************************************/
122 /* this should be implemented in assembly */
123 void bigint_sub_u(bigint_t *dest, const bigint_t *a, const bigint_t *b)
128 uint16_t i, min, max;
129 min = MIN(a->length_W, b->length_W);
130 max = MAX(a->length_W, b->length_W);
131 r = bigint_cmp_u(a, b);
138 if (b->length_W == 0) {
139 dest->length_W = a->length_W;
140 memcpy(dest->wordv, a->wordv, a->length_W);
141 dest->info = a->info;
145 if (a->length_W == 0) {
146 dest->length_W = b->length_W;
147 memcpy(dest->wordv, b->wordv, b->length_W);
148 dest->info = b->info;
153 bigint_sub_u(dest, b, a);
156 for (i = 0; i < min; ++i) {
157 t = a->wordv[i] - b->wordv[i] - borrow;
160 dest->wordv[i] = (uint8_t) t;
163 dest->wordv[i] = (uint8_t) t;
166 for (; i < max; ++i) {
167 t = a->wordv[i] - borrow;
170 dest->wordv[i] = (uint8_t) t;
173 dest->wordv[i] = (uint8_t) t;
183 /******************************************************************************/
185 int8_t bigint_cmp_u(const bigint_t *a, const bigint_t *b)
187 if (a->length_W > b->length_W) {
190 if (a->length_W < b->length_W) {
193 if (a->length_W == 0) {
199 if (a->wordv[i] != b->wordv[i]) {
200 if (a->wordv[i] > b->wordv[i]) {
210 /******************************************************************************/
212 void bigint_add_s(bigint_t *dest, const bigint_t *a, const bigint_t *b)
215 s = GET_SIGN(a) ? 2 : 0;
216 s |= GET_SIGN(b) ? 1 : 0;
218 case 0: /* both positive */
219 bigint_add_u(dest, a, b);
222 case 1: /* a positive, b negative */
223 bigint_sub_u(dest, a, b);
225 case 2: /* a negative, b positive */
226 bigint_sub_u(dest, b, a);
228 case 3: /* both negative */
229 bigint_add_u(dest, a, b);
232 default: /* how can this happen?*/
237 /******************************************************************************/
239 void bigint_sub_s(bigint_t *dest, const bigint_t *a, const bigint_t *b)
242 s = GET_SIGN(a) ? 2 : 0;
243 s |= GET_SIGN(b) ? 1 : 0;
245 case 0: /* both positive */
246 bigint_sub_u(dest, a, b);
248 case 1: /* a positive, b negative */
249 bigint_add_u(dest, a, b);
252 case 2: /* a negative, b positive */
253 bigint_add_u(dest, a, b);
256 case 3: /* both negative */
257 bigint_sub_u(dest, b, a);
259 default: /* how can this happen?*/
265 /******************************************************************************/
267 int8_t bigint_cmp_s(const bigint_t *a, const bigint_t *b)
270 if (a->length_W == 0 && b->length_W == 0) {
273 s = GET_SIGN(a) ? 2 : 0;
274 s |= GET_SIGN(b) ? 1 : 0;
276 case 0: /* both positive */
277 return bigint_cmp_u(a, b);
279 case 1: /* a positive, b negative */
282 case 2: /* a negative, b positive */
285 case 3: /* both negative */
286 return bigint_cmp_u(b, a);
288 default: /* how can this happen?*/
291 return 0; /* just to satisfy the compiler */
294 /******************************************************************************/
296 void bigint_shiftleft(bigint_t *a, uint16_t shift)
302 byteshift = (shift + 3) / 8;
303 bitshift = shift & 7;
304 memmove(a->wordv + byteshift, a->wordv, a->length_W);
305 memset(a->wordv, 0, byteshift);
307 if (bitshift <= 4) { /* shift to the left */
308 for (i = byteshift; i < a->length_W + byteshift; ++i) {
309 t |= (a->wordv[i]) << bitshift;
310 a->wordv[i] = (uint8_t) t;
313 a->wordv[i] = (uint8_t) t;
315 } else { /* shift to the right */
316 for (i = a->length_W + byteshift - 1; i > byteshift - 1; --i) {
317 t |= (a->wordv[i]) << (bitshift);
318 a->wordv[i] = (uint8_t) (t >> 8);
321 t |= (a->wordv[i]) << (bitshift);
322 a->wordv[i] = (uint8_t) (t >> 8);
325 a->length_W += byteshift;
329 /******************************************************************************/
331 void bigint_shiftright(bigint_t *a, uint16_t shift)
337 byteshift = shift / 8;
338 bitshift = shift & 7;
339 if (byteshift >= a->length_W) { /* we would shift out more than we have */
343 if (byteshift == a->length_W - 1 && bitshift > GET_FBS(a)) {
348 memmove(a->wordv, a->wordv + byteshift, a->length_W - byteshift);
349 memset(a->wordv + a->length_W - byteshift, 0, byteshift);
352 /* shift to the right */
353 for (i = a->length_W - byteshift - 1; i > 0; --i) {
354 t |= (a->wordv[i]) << (8 - bitshift);
355 a->wordv[i] = (uint8_t) (t >> 8);
358 t |= (a->wordv[0]) << (8 - bitshift);
359 a->wordv[0] = (uint8_t) (t >> 8);
361 a->length_W -= byteshift;
365 /******************************************************************************/
367 void bigint_xor(bigint_t *dest, const bigint_t *a)
370 for (i = 0; i < a->length_W; ++i) {
371 dest->wordv[i] ^= a->wordv[i];
376 /******************************************************************************/
378 void bigint_set_zero(bigint_t *a)
383 /******************************************************************************/
385 /* using the Karatsuba-Algorithm */
386 /* x*y = (xh*yh)*b**2n + ((xh+xl)*(yh+yl) - xh*yh - xl*yl)*b**n + yh*yl */
387 void bigint_mul_u(bigint_t *dest, const bigint_t *a, const bigint_t *b)
389 if (a->length_W == 0 || b->length_W == 0) {
390 bigint_set_zero(dest);
393 if (dest == a || dest == b) {
395 uint8_t d_b[a->length_W + b->length_W];
397 bigint_mul_u(&d, a, b);
398 bigint_copy(dest, &d);
401 if (a->length_W == 1 || b->length_W == 1) {
402 if (a->length_W != 1) {
406 uint8_t x = a->wordv[0];
407 for (i = 0; i < b->length_W; ++i) {
408 t += b->wordv[i] * x;
409 dest->wordv[i] = (uint8_t) t;
412 dest->wordv[i] = (uint8_t) t;
413 dest->length_W = i + 1;
417 if (a->length_W <= 4 && b->length_W <= 4) {
418 uint32_t p = 0, q = 0;
420 memcpy(&p, a->wordv, a->length_W);
421 memcpy(&q, b->wordv, b->length_W);
422 r = (uint64_t) p * (uint64_t) q;
423 memcpy(dest->wordv, &r, a->length_W + b->length_W);
424 dest->length_W = a->length_W + b->length_W;
428 bigint_set_zero(dest);
429 /* split a in xh & xl; split b in yh & yl */
431 n = (MAX(a->length_W, b->length_W) + 1) / 2;
432 bigint_t xl, xh, yl, yh;
435 if (a->length_W <= n) {
438 xl.length_W = a->length_W;
444 xh.wordv = a->wordv + n;
445 xh.length_W = a->length_W - n;
448 if (b->length_W <= n) {
451 yl.length_W = b->length_W;
457 yh.wordv = b->wordv + n;
458 yh.length_W = b->length_W - n;
461 /* now we have split up a and b */
462 uint8_t tmp_b[2 * n + 2], m_b[2 * (n + 1)];
463 bigint_t tmp, tmp2, m;
465 tmp2.wordv = tmp_b + n + 1;
468 bigint_mul_u(dest, &xl, &yl); /* dest <= xl*yl */
469 bigint_add_u(&tmp2, &xh, &xl); /* tmp2 <= xh+xl */
470 bigint_add_u(&tmp, &yh, &yl); /* tmp <= yh+yl */
471 bigint_mul_u(&m, &tmp2, &tmp); /* m <= tmp2*tmp */
472 bigint_mul_u(&tmp, &xh, &yh); /* h <= xh*yh */
473 bigint_sub_u(&m, &m, dest); /* m <= m-dest */
474 bigint_sub_u(&m, &m, &tmp); /* m <= m-h */
475 bigint_add_scale_u(dest, &m, n);
476 bigint_add_scale_u(dest, &tmp, 2 * n);
479 /******************************************************************************/
481 void bigint_mul_s(bigint_t *dest, const bigint_t *a, const bigint_t *b)
484 s = GET_SIGN(a) ? 2 : 0;
485 s |= GET_SIGN(b) ? 1 : 0;
487 case 0: /* both positive */
488 bigint_mul_u(dest, a, b);
491 case 1: /* a positive, b negative */
492 bigint_mul_u(dest, a, b);
495 case 2: /* a negative, b positive */
496 bigint_mul_u(dest, a, b);
499 case 3: /* both negative */
500 bigint_mul_u(dest, a, b);
503 default: /* how can this happen?*/
508 /******************************************************************************/
511 /* (xh*b^n+xl)^2 = xh^2*b^2n + 2*xh*xl*b^n + xl^2 */
512 void bigint_square(bigint_t *dest, const bigint_t *a)
514 if (a->length_W <= 4) {
516 memcpy(&r, a->wordv, a->length_W);
518 memcpy(dest->wordv, &r, 2 * a->length_W);
520 dest->length_W = 2 * a->length_W;
526 uint8_t d_b[a->length_W * 2];
528 bigint_square(&d, a);
529 bigint_copy(dest, &d);
533 n = (a->length_W + 1) / 2;
534 bigint_t xh, xl, tmp; /* x-high, x-low, temp */
535 uint8_t buffer[2 * n + 1];
538 xh.wordv = a->wordv + n;
539 xh.length_W = a->length_W - n;
541 bigint_square(dest, &xl);
542 bigint_square(&tmp, &xh);
543 bigint_add_scale_u(dest, &tmp, 2 * n);
544 bigint_mul_u(&tmp, &xl, &xh);
545 bigint_shiftleft(&tmp, 1);
546 bigint_add_scale_u(dest, &tmp, n);
549 /******************************************************************************/
551 void bigint_sub_u_bitscale(bigint_t *a, const bigint_t *b, uint16_t bitscale)
554 uint8_t tmp_b[b->length_W + 1];
555 uint16_t i, j, byteshift = bitscale / 8;
559 if (a->length_W < b->length_W + byteshift) {
565 bigint_copy(&tmp, b);
566 bigint_shiftleft(&tmp, bitscale & 7);
568 for (j = 0, i = byteshift; i < tmp.length_W + byteshift; ++i, ++j) {
569 t = a->wordv[i] - tmp.wordv[j] - borrow;
570 a->wordv[i] = (uint8_t) t;
578 if (i + 1 > a->length_W) {
582 a->wordv[i] -= borrow;
583 if (a->wordv[i] != 0xff) {
591 /******************************************************************************/
593 void bigint_reduce(bigint_t *a, const bigint_t *r)
596 uint8_t rfbs = GET_FBS(r);
598 if (r->length_W == 0 || a->length_W == 0) {
601 while (a->length_W > r->length_W) {
602 bigint_sub_u_bitscale(a, r, (a->length_W - r->length_W) * 8 + GET_FBS(a)
605 while ((GET_FBS(a) > rfbs + 1) && (a->length_W == r->length_W)) {
606 bigint_sub_u_bitscale(a, r, GET_FBS(a) - rfbs - 1);
608 while (bigint_cmp_u(a, r) >= 0) {
609 bigint_sub_u(a, a, r);
614 /******************************************************************************/
616 /* calculate dest = a**exp % r */
617 /* using square&multiply */
618 void bigint_expmod_u(bigint_t *dest, const bigint_t *a, const bigint_t *exp,
621 if (a->length_W == 0 || r->length_W == 0) {
626 uint8_t base_b[MAX(a->length_W, r->length_W * 2)], res_b[r->length_W * 2];
631 bigint_copy(&base, a);
632 bigint_reduce(&base, r);
637 for (i = 0; i + 1 < exp->length_W; ++i) {
639 for (j = 0; j < 8; ++j) {
641 bigint_mul_u(&res, &res, &base);
642 bigint_reduce(&res, r);
644 bigint_square(&base, &base);
645 bigint_reduce(&base, r);
652 bigint_mul_u(&res, &res, &base);
653 bigint_reduce(&res, r);
655 bigint_square(&base, &base);
656 bigint_reduce(&base, r);
660 bigint_copy(dest, &res);
663 /******************************************************************************/
664 /* gcd <-- gcd(x,y) a*x+b*y=gcd */
665 void bigint_gcdext(bigint_t *gcd, bigint_t *a, bigint_t *b, const bigint_t *x,
668 bigint_t g, x_, y_, u, v, a_, b_, c_, d_;
669 volatile uint16_t i = 0;
670 if (x->length_W == 0 || y->length_W == 0) {
673 while (x->wordv[i] == 0 && y->wordv[i] == 0) {
676 uint8_t g_b[i + 2], x_b[x->length_W - i], y_b[y->length_W - i];
677 uint8_t u_b[x->length_W - i], v_b[y->length_W - i];
678 uint8_t a_b[y->length_W + 2], c_b[y->length_W + 2];
679 uint8_t b_b[x->length_W + 2], d_b[x->length_W + 2];
688 x_.info = y_.info = 0;
689 x_.length_W = x->length_W - i;
690 y_.length_W = y->length_W - i;
691 memcpy(x_.wordv, x->wordv + i, x_.length_W);
692 memcpy(y_.wordv, y->wordv + i, y_.length_W);
693 for (i = 0; (x_.wordv[0] & (1 << i)) == 0 && (y_.wordv[0] & (1 << i)) == 0;
701 bigint_shiftleft(&g, i);
702 bigint_shiftright(&x_, i);
703 bigint_shiftright(&y_, i);
712 bigint_copy(&u, &x_);
713 bigint_copy(&v, &y_);
720 bigint_set_zero(&b_);
721 bigint_set_zero(&c_);
723 while ((u.wordv[0] & 1) == 0) {
724 bigint_shiftright(&u, 1);
725 if ((a_.wordv[0] & 1) || (b_.wordv[0] & 1)) {
726 bigint_add_s(&a_, &a_, &y_);
727 bigint_sub_s(&b_, &b_, &x_);
729 bigint_shiftright(&a_, 1);
730 bigint_shiftright(&b_, 1);
732 while ((v.wordv[0] & 1) == 0) {
733 bigint_shiftright(&v, 1);
734 if ((c_.wordv[0] & 1) || (d_.wordv[0] & 1)) {
735 bigint_add_s(&c_, &c_, &y_);
736 bigint_sub_s(&d_, &d_, &x_);
738 bigint_shiftright(&c_, 1);
739 bigint_shiftright(&d_, 1);
742 if (bigint_cmp_u(&u, &v) >= 0) {
743 bigint_sub_u(&u, &u, &v);
744 bigint_sub_s(&a_, &a_, &c_);
745 bigint_sub_s(&b_, &b_, &d_);
747 bigint_sub_u(&v, &v, &u);
748 bigint_sub_s(&c_, &c_, &a_);
749 bigint_sub_s(&d_, &d_, &b_);
751 } while (u.length_W);
753 bigint_mul_s(gcd, &v, &g);
763 /******************************************************************************/
765 void bigint_inverse(bigint_t *dest, const bigint_t *a, const bigint_t *m)
767 bigint_gcdext(NULL, dest, NULL, a, m);
768 while (dest->info & BIGINT_NEG_MASK) {
769 bigint_add_s(dest, dest, m);
773 /******************************************************************************/
775 void bigint_changeendianess(bigint_t *a)
779 q = p + a->length_W - 1;
789 /******************************************************************************/