3 This file is part of the AVR-Crypto-Lib.
4 Copyright (C) 2008 Daniel Otte (daniel.otte@rub.de)
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
30 #define STRING(x) STRING2(x)
31 #define STR_LINE STRING(__LINE__)
37 #include "bigint_io.h"
40 #define MAX(a,b) (((a)>(b))?(a):(b))
44 #define MIN(a,b) (((a)<(b))?(a):(b))
47 #define SET_FBS(a, v) do{(a)->info &=0xF8; (a)->info |= (v);}while(0)
48 #define GET_FBS(a) ((a)->info&BIGINT_FBS_MASK)
49 #define SET_NEG(a) (a)->info |= BIGINT_NEG_MASK
50 #define SET_POS(a) (a)->info &= ~BIGINT_NEG_MASK
51 #define XCHG(a,b) do{(a)^=(b); (b)^=(a); (a)^=(b);}while(0)
52 #define XCHG_PTR(a,b) do{ a = (void*)(((uint16_t)(a)) ^ ((uint16_t)(b))); \
53 b = (void*)(((uint16_t)(a)) ^ ((uint16_t)(b))); \
54 a = (void*)(((uint16_t)(a)) ^ ((uint16_t)(b)));}while(0)
56 #define GET_SIGN(a) ((a)->info&BIGINT_NEG_MASK)
58 /******************************************************************************/
59 void bigint_adjust(bigint_t* a){
60 while(a->length_B!=0 && a->wordv[a->length_B-1]==0){
69 t = a->wordv[a->length_B-1];
70 while((t&0x80)==0 && i){
77 /******************************************************************************/
79 void bigint_copy(bigint_t* dest, const bigint_t* src){
80 memcpy(dest->wordv, src->wordv, src->length_B);
81 dest->length_B = src->length_B;
82 dest->info = src->info;
85 /******************************************************************************/
87 /* this should be implemented in assembly */
89 void bigint_add_u(bigint_t* dest, const bigint_t* a, const bigint_t* b){
91 if(a->length_B < b->length_B){
94 for(i=0; i<b->length_B; ++i){
95 t = a->wordv[i] + b->wordv[i] + t;
96 dest->wordv[i] = (uint8_t)t;
99 for(; i<a->length_B; ++i){
101 dest->wordv[i] = (uint8_t)t;
104 dest->wordv[i++] = t;
109 /******************************************************************************/
111 /* this should be implemented in assembly */
112 void bigint_add_scale_u(bigint_t* dest, const bigint_t* a, uint16_t scale){
115 if(scale>dest->length_B)
116 memset(dest->wordv+dest->length_B, 0, scale-dest->length_B);
117 for(i=scale; i<a->length_B+scale; ++i,++j){
119 if(dest->length_B>i){
122 dest->wordv[i] = (uint8_t)t;
126 if(dest->length_B>i){
127 t = dest->wordv[i] + t;
129 dest->wordv[i] = (uint8_t)t;
133 if(dest->length_B < i){
139 /******************************************************************************/
141 /* this should be implemented in assembly */
142 void bigint_sub_u(bigint_t* dest, const bigint_t* a, const bigint_t* b){
146 uint16_t i, min, max;
147 min = MIN(a->length_B, b->length_B);
148 max = MAX(a->length_B, b->length_B);
149 r = bigint_cmp_u(a,b);
157 dest->length_B = a->length_B;
158 memcpy(dest->wordv, a->wordv, a->length_B);
159 dest->info = a->info;
164 dest->length_B = b->length_B;
165 memcpy(dest->wordv, b->wordv, b->length_B);
166 dest->info = b->info;
171 bigint_sub_u(dest, b, a);
174 for(i=0; i<min; ++i){
175 t = a->wordv[i] - b->wordv[i] - borrow;
178 dest->wordv[i]=(uint8_t)t;
181 dest->wordv[i]=(uint8_t)t;
185 t = a->wordv[i] - borrow;
188 dest->wordv[i]=(uint8_t)t;
191 dest->wordv[i]=(uint8_t)t;
201 /******************************************************************************/
203 int8_t bigint_cmp_u(const bigint_t* a, const bigint_t* b){
204 if(a->length_B > b->length_B){
207 if(a->length_B < b->length_B){
216 if(a->wordv[i]!=b->wordv[i]){
217 if(a->wordv[i]>b->wordv[i]){
227 /******************************************************************************/
229 void bigint_add_s(bigint_t* dest, const bigint_t* a, const bigint_t* b){
232 s |= GET_SIGN(b)?1:0;
234 case 0: /* both positive */
235 bigint_add_u(dest, a,b);
238 case 1: /* a positive, b negative */
239 bigint_sub_u(dest, a, b);
241 case 2: /* a negative, b positive */
242 bigint_sub_u(dest, b, a);
244 case 3: /* both negative */
245 bigint_add_u(dest, a, b);
248 default: /* how can this happen?*/
253 /******************************************************************************/
255 void bigint_sub_s(bigint_t* dest, const bigint_t* a, const bigint_t* b){
258 s |= GET_SIGN(b)?1:0;
260 case 0: /* both positive */
261 bigint_sub_u(dest, a,b);
263 case 1: /* a positive, b negative */
264 bigint_add_u(dest, a, b);
267 case 2: /* a negative, b positive */
268 bigint_add_u(dest, a, b);
271 case 3: /* both negative */
272 bigint_sub_u(dest, b, a);
274 default: /* how can this happen?*/
280 /******************************************************************************/
282 int8_t bigint_cmp_s(const bigint_t* a, const bigint_t* b){
284 if(a->length_B==0 && b->length_B==0){
288 s |= GET_SIGN(b)?1:0;
290 case 0: /* both positive */
291 return bigint_cmp_u(a, b);
293 case 1: /* a positive, b negative */
296 case 2: /* a negative, b positive */
299 case 3: /* both negative */
300 return bigint_cmp_u(b, a);
302 default: /* how can this happen?*/
305 return 0; /* just to satisfy the compiler */
308 /******************************************************************************/
310 void bigint_shiftleft(bigint_t* a, uint16_t shift){
315 byteshift = (shift+3)/8;
317 memmove(a->wordv+byteshift, a->wordv, a->length_B);
318 memset(a->wordv, 0, byteshift);
320 if(bitshift<=4){ /* shift to the left */
321 for(i=byteshift; i<a->length_B+byteshift; ++i){
322 t |= (a->wordv[i])<<bitshift;
323 a->wordv[i] = (uint8_t)t;
326 a->wordv[i] = (uint8_t)t;
328 }else{ /* shift to the right */
329 for(i=a->length_B+byteshift-1; i>byteshift-1; --i){
330 t |= (a->wordv[i])<<(bitshift);
331 a->wordv[i] = (uint8_t)(t>>8);
334 t |= (a->wordv[i])<<(bitshift);
335 a->wordv[i] = (uint8_t)(t>>8);
338 a->length_B += byteshift;
342 /******************************************************************************/
344 void bigint_shiftright(bigint_t* a, uint16_t shift){
351 if(byteshift >= a->length_B){ /* we would shift out more than we have */
355 if(byteshift == a->length_B-1 && bitshift>GET_FBS(a)){
360 memmove(a->wordv, a->wordv+byteshift, a->length_B-byteshift);
361 memset(a->wordv+a->length_B-byteshift, 0, byteshift);
364 /* shift to the right */
365 for(i=a->length_B-byteshift-1; i>0; --i){
366 t |= (a->wordv[i])<<(8-bitshift);
367 a->wordv[i] = (uint8_t)(t>>8);
370 t |= (a->wordv[0])<<(8-bitshift);
371 a->wordv[0] = (uint8_t)(t>>8);
373 a->length_B -= byteshift;
377 /******************************************************************************/
379 void bigint_xor(bigint_t* dest, const bigint_t* a){
381 for(i=0; i<a->length_B; ++i){
382 dest->wordv[i] ^= a->wordv[i];
387 /******************************************************************************/
389 void bigint_set_zero(bigint_t* a){
393 /******************************************************************************/
395 /* using the Karatsuba-Algorithm */
396 /* x*y = (xh*yh)*b**2n + ((xh+xl)*(yh+yl) - xh*yh - xl*yl)*b**n + yh*yl */
397 void bigint_mul_u(bigint_t* dest, const bigint_t* a, const bigint_t* b){
398 if(a->length_B==0 || b->length_B==0){
399 bigint_set_zero(dest);
402 if(dest==a || dest==b){
404 uint8_t d_b[a->length_B+b->length_B];
406 bigint_mul_u(&d, a, b);
407 bigint_copy(dest, &d);
410 if(a->length_B==1 || b->length_B==1){
415 uint8_t x = a->wordv[0];
416 for(i=0; i<b->length_B; ++i){
418 dest->wordv[i] = (uint8_t)t;
421 dest->wordv[i] = (uint8_t)t;
426 if(a->length_B<=4 && b->length_B<=4){
429 memcpy(&p, a->wordv, a->length_B);
430 memcpy(&q, b->wordv, b->length_B);
431 r = (uint64_t)p*(uint64_t)q;
432 memcpy(dest->wordv, &r, a->length_B+b->length_B);
433 dest->length_B = a->length_B+b->length_B;
437 bigint_set_zero(dest);
438 /* split a in xh & xl; split b in yh & yl */
440 n=(MAX(a->length_B, b->length_B)+1)/2;
441 bigint_t xl, xh, yl, yh;
447 xl.length_B = a->length_B;
453 xh.wordv = a->wordv+n;
454 xh.length_B = a->length_B-n;
460 yl.length_B = b->length_B;
466 yh.wordv = b->wordv+n;
467 yh.length_B = b->length_B-n;
470 /* now we have split up a and b */
471 uint8_t tmp_b[2*n+2], m_b[2*(n+1)];
472 bigint_t tmp, tmp2, m;
474 tmp2.wordv = tmp_b+n+1;
477 bigint_mul_u(dest, &xl, &yl); /* dest <= xl*yl */
478 bigint_add_u(&tmp2, &xh, &xl); /* tmp2 <= xh+xl */
479 bigint_add_u(&tmp, &yh, &yl); /* tmp <= yh+yl */
480 bigint_mul_u(&m, &tmp2, &tmp); /* m <= tmp2*tmp */
481 bigint_mul_u(&tmp, &xh, &yh); /* h <= xh*yh */
482 bigint_sub_u(&m, &m, dest); /* m <= m-dest */
483 bigint_sub_u(&m, &m, &tmp); /* m <= m-h */
484 bigint_add_scale_u(dest, &m, n);
485 bigint_add_scale_u(dest, &tmp, 2*n);
488 /******************************************************************************/
490 void bigint_mul_s(bigint_t* dest, const bigint_t* a, const bigint_t* b){
493 s |= GET_SIGN(b)?1:0;
495 case 0: /* both positive */
496 bigint_mul_u(dest, a,b);
499 case 1: /* a positive, b negative */
500 bigint_mul_u(dest, a,b);
503 case 2: /* a negative, b positive */
504 bigint_mul_u(dest, a,b);
507 case 3: /* both negative */
508 bigint_mul_u(dest, a,b);
511 default: /* how can this happen?*/
516 /******************************************************************************/
519 /* (xh*b^n+xl)^2 = xh^2*b^2n + 2*xh*xl*b^n + xl^2 */
520 void bigint_square(bigint_t* dest, const bigint_t* a){
523 memcpy(&r, a->wordv, a->length_B);
525 memcpy(dest->wordv, &r, 2*a->length_B);
527 dest->length_B=2*a->length_B;
533 uint8_t d_b[a->length_B*2];
535 bigint_square(&d, a);
536 bigint_copy(dest, &d);
541 bigint_t xh, xl, tmp; /* x-high, x-low, temp */
542 uint8_t buffer[2*n+1];
545 xh.wordv = a->wordv+n;
546 xh.length_B = a->length_B-n;
548 bigint_square(dest, &xl);
549 bigint_square(&tmp, &xh);
550 bigint_add_scale_u(dest, &tmp, 2*n);
551 bigint_mul_u(&tmp, &xl, &xh);
552 bigint_shiftleft(&tmp, 1);
553 bigint_add_scale_u(dest, &tmp, n);
556 /******************************************************************************/
558 void bigint_sub_u_bitscale(bigint_t* a, const bigint_t* b, uint16_t bitscale){
560 uint8_t tmp_b[b->length_B+1];
561 uint16_t i,j,byteshift=bitscale/8;
565 if(a->length_B < b->length_B+byteshift){
571 bigint_copy(&tmp, b);
572 bigint_shiftleft(&tmp, bitscale&7);
574 for(j=0,i=byteshift; i<tmp.length_B+byteshift; ++i, ++j){
575 t = a->wordv[i] - tmp.wordv[j] - borrow;
576 a->wordv[i] = (uint8_t)t;
584 if(i+1 > a->length_B){
588 a->wordv[i] -= borrow;
589 if(a->wordv[i]!=0xff){
597 /******************************************************************************/
599 void bigint_reduce(bigint_t* a, const bigint_t* r){
601 uint8_t rfbs = GET_FBS(r);
603 if(r->length_B==0 || a->length_B==0){
606 while(a->length_B > r->length_B){
607 bigint_sub_u_bitscale(a, r, (a->length_B-r->length_B)*8+GET_FBS(a)-rfbs-1);
609 while((GET_FBS(a) > rfbs+1) && (a->length_B == r->length_B)){
610 bigint_sub_u_bitscale(a, r, GET_FBS(a)-rfbs-1);
612 while(bigint_cmp_u(a,r)>=0){
618 /******************************************************************************/
620 /* calculate dest = a**exp % r */
621 /* using square&multiply */
622 void bigint_expmod_u(bigint_t* dest, const bigint_t* a, const bigint_t* exp, const bigint_t* r){
623 if(a->length_B==0 || r->length_B==0){
628 uint8_t base_b[MAX(a->length_B,r->length_B*2)], res_b[r->length_B*2];
633 bigint_copy(&base, a);
634 bigint_reduce(&base, r);
639 for(i=0; i+1<exp->length_B; ++i){
643 bigint_mul_u(&res, &res, &base);
644 bigint_reduce(&res, r);
646 bigint_square(&base, &base);
647 bigint_reduce(&base, r);
654 bigint_mul_u(&res, &res, &base);
655 bigint_reduce(&res, r);
657 bigint_square(&base, &base);
658 bigint_reduce(&base, r);
662 bigint_copy(dest, &res);
665 /******************************************************************************/
666 /* gcd <-- gcd(x,y) a*x+b*y=gcd */
667 void bigint_gcdext(bigint_t* gcd, bigint_t* a, bigint_t* b, const bigint_t* x, const bigint_t* y){
668 bigint_t g, x_, y_, u, v, a_, b_, c_, d_;
669 volatile uint16_t i=0;
670 if(x->length_B==0 || y->length_B==0){
673 while(x->wordv[i]==0 && y->wordv[i]==0){
676 uint8_t g_b[i+2], x_b[x->length_B-i], y_b[y->length_B-i];
677 uint8_t u_b[x->length_B-i], v_b[y->length_B-i];
678 uint8_t a_b[y->length_B+2], c_b[y->length_B+2];
679 uint8_t b_b[x->length_B+2], d_b[x->length_B+2];
688 x_.info = y_.info = 0;
689 x_.length_B = x->length_B-i;
690 y_.length_B = y->length_B-i;
691 memcpy(x_.wordv, x->wordv+i, x_.length_B);
692 memcpy(y_.wordv, y->wordv+i, y_.length_B);
693 for(i=0; (x_.wordv[0]&(1<<i))==0 && (y_.wordv[0]&(1<<i))==0; ++i){
700 bigint_shiftleft(&g, i);
701 bigint_shiftright(&x_, i);
702 bigint_shiftright(&y_, i);
711 bigint_copy(&u, &x_);
712 bigint_copy(&v, &y_);
719 bigint_set_zero(&b_);
720 bigint_set_zero(&c_);
722 while((u.wordv[0]&1)==0){
723 bigint_shiftright(&u, 1);
724 if((a_.wordv[0]&1) || (b_.wordv[0]&1)){
725 bigint_add_s(&a_, &a_, &y_);
726 bigint_sub_s(&b_, &b_, &x_);
728 bigint_shiftright(&a_, 1);
729 bigint_shiftright(&b_, 1);
731 while((v.wordv[0]&1)==0){
732 bigint_shiftright(&v, 1);
733 if((c_.wordv[0]&1) || (d_.wordv[0]&1)){
734 bigint_add_s(&c_, &c_, &y_);
735 bigint_sub_s(&d_, &d_, &x_);
737 bigint_shiftright(&c_, 1);
738 bigint_shiftright(&d_, 1);
741 if(bigint_cmp_u(&u, &v)>=0){
742 bigint_sub_u(&u, &u, &v);
743 bigint_sub_s(&a_, &a_, &c_);
744 bigint_sub_s(&b_, &b_, &d_);
746 bigint_sub_u(&v, &v, &u);
747 bigint_sub_s(&c_, &c_, &a_);
748 bigint_sub_s(&d_, &d_, &b_);
752 bigint_mul_s(gcd, &v, &g);
762 /******************************************************************************/
764 void bigint_inverse(bigint_t* dest, const bigint_t* a, const bigint_t* m){
765 bigint_gcdext(NULL, dest, NULL, a, m);
766 while(dest->info&BIGINT_NEG_MASK){
767 bigint_add_s(dest, dest, m);
771 /******************************************************************************/
773 void bigint_changeendianess(bigint_t* a){
785 /******************************************************************************/