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 */
88 void bigint_add_u(bigint_t* dest, const bigint_t* a, const bigint_t* b){
90 if(a->length_B < b->length_B){
93 for(i=0; i<b->length_B; ++i){
94 t = a->wordv[i] + b->wordv[i] + t;
95 dest->wordv[i] = (uint8_t)t;
98 for(; i<a->length_B; ++i){
100 dest->wordv[i] = (uint8_t)t;
103 dest->wordv[i++] = t;
108 /******************************************************************************/
110 /* this should be implemented in assembly */
111 void bigint_add_scale_u(bigint_t* dest, const bigint_t* a, uint16_t scale){
114 if(scale>dest->length_B)
115 memset(dest->wordv+dest->length_B, 0, scale-dest->length_B);
116 for(i=scale; i<a->length_B+scale; ++i,++j){
118 if(dest->length_B>i){
121 dest->wordv[i] = (uint8_t)t;
125 if(dest->length_B>i){
126 t = dest->wordv[i] + t;
128 dest->wordv[i] = (uint8_t)t;
132 if(dest->length_B < i){
138 /******************************************************************************/
140 /* this should be implemented in assembly */
141 void bigint_sub_u(bigint_t* dest, const bigint_t* a, const bigint_t* b){
145 uint16_t i, min, max;
146 min = MIN(a->length_B, b->length_B);
147 max = MAX(a->length_B, b->length_B);
148 r = bigint_cmp_u(a,b);
156 dest->length_B = a->length_B;
157 memcpy(dest->wordv, a->wordv, a->length_B);
158 dest->info = a->info;
163 dest->length_B = b->length_B;
164 memcpy(dest->wordv, b->wordv, b->length_B);
165 dest->info = b->info;
170 bigint_sub_u(dest, b, a);
173 for(i=0; i<min; ++i){
174 t = a->wordv[i] - b->wordv[i] - borrow;
177 dest->wordv[i]=(uint8_t)t;
180 dest->wordv[i]=(uint8_t)t;
184 t = a->wordv[i] - borrow;
187 dest->wordv[i]=(uint8_t)t;
190 dest->wordv[i]=(uint8_t)t;
200 /******************************************************************************/
202 int8_t bigint_cmp_u(const bigint_t* a, const bigint_t* b){
203 if(a->length_B > b->length_B){
206 if(a->length_B < b->length_B){
215 if(a->wordv[i]!=b->wordv[i]){
216 if(a->wordv[i]>b->wordv[i]){
226 /******************************************************************************/
228 void bigint_add_s(bigint_t* dest, const bigint_t* a, const bigint_t* b){
231 s |= GET_SIGN(b)?1:0;
233 case 0: /* both positive */
234 bigint_add_u(dest, a,b);
237 case 1: /* a positive, b negative */
238 bigint_sub_u(dest, a, b);
240 case 2: /* a negative, b positive */
241 bigint_sub_u(dest, b, a);
243 case 3: /* both negative */
244 bigint_add_u(dest, a, b);
247 default: /* how can this happen?*/
252 /******************************************************************************/
254 void bigint_sub_s(bigint_t* dest, const bigint_t* a, const bigint_t* b){
257 s |= GET_SIGN(b)?1:0;
259 case 0: /* both positive */
260 bigint_sub_u(dest, a,b);
262 case 1: /* a positive, b negative */
263 bigint_add_u(dest, a, b);
266 case 2: /* a negative, b positive */
267 bigint_add_u(dest, a, b);
270 case 3: /* both negative */
271 bigint_sub_u(dest, b, a);
273 default: /* how can this happen?*/
279 /******************************************************************************/
281 int8_t bigint_cmp_s(const bigint_t* a, const bigint_t* b){
283 if(a->length_B==0 && b->length_B==0){
287 s |= GET_SIGN(b)?1:0;
289 case 0: /* both positive */
290 return bigint_cmp_u(a, b);
292 case 1: /* a positive, b negative */
295 case 2: /* a negative, b positive */
298 case 3: /* both negative */
299 return bigint_cmp_u(b, a);
301 default: /* how can this happen?*/
304 return 0; /* just to satisfy the compiler */
307 /******************************************************************************/
309 void bigint_shiftleft(bigint_t* a, uint16_t shift){
314 byteshift = (shift+3)/8;
316 memmove(a->wordv+byteshift, a->wordv, a->length_B);
317 memset(a->wordv, 0, byteshift);
319 if(bitshift<=4){ /* shift to the left */
320 for(i=byteshift; i<a->length_B+byteshift; ++i){
321 t |= (a->wordv[i])<<bitshift;
322 a->wordv[i] = (uint8_t)t;
325 a->wordv[i] = (uint8_t)t;
327 }else{ /* shift to the right */
328 for(i=a->length_B+byteshift-1; i>byteshift-1; --i){
329 t |= (a->wordv[i])<<(bitshift);
330 a->wordv[i] = (uint8_t)(t>>8);
333 t |= (a->wordv[i])<<(bitshift);
334 a->wordv[i] = (uint8_t)(t>>8);
337 a->length_B += byteshift;
341 /******************************************************************************/
343 void bigint_shiftright(bigint_t* a, uint16_t shift){
350 if(byteshift >= a->length_B){ /* we would shift out more than we have */
354 if(byteshift == a->length_B-1 && bitshift>GET_FBS(a)){
359 memmove(a->wordv, a->wordv+byteshift, a->length_B-byteshift);
360 memset(a->wordv+a->length_B-byteshift, 0, byteshift);
363 /* shift to the right */
364 for(i=a->length_B-byteshift-1; i>0; --i){
365 t |= (a->wordv[i])<<(8-bitshift);
366 a->wordv[i] = (uint8_t)(t>>8);
369 t |= (a->wordv[0])<<(8-bitshift);
370 a->wordv[0] = (uint8_t)(t>>8);
372 a->length_B -= byteshift;
376 /******************************************************************************/
378 void bigint_xor(bigint_t* dest, const bigint_t* a){
380 for(i=0; i<a->length_B; ++i){
381 dest->wordv[i] ^= a->wordv[i];
386 /******************************************************************************/
388 void bigint_set_zero(bigint_t* a){
392 /******************************************************************************/
394 /* using the Karatsuba-Algorithm */
395 /* x*y = (xh*yh)*b**2n + ((xh+xl)*(yh+yl) - xh*yh - xl*yl)*b**n + yh*yl */
396 void bigint_mul_u(bigint_t* dest, const bigint_t* a, const bigint_t* b){
397 if(a->length_B==0 || b->length_B==0){
398 bigint_set_zero(dest);
401 if(dest==a || dest==b){
403 uint8_t d_b[a->length_B+b->length_B];
405 bigint_mul_u(&d, a, b);
406 bigint_copy(dest, &d);
409 if(a->length_B==1 || b->length_B==1){
414 uint8_t x = a->wordv[0];
415 for(i=0; i<b->length_B; ++i){
417 dest->wordv[i] = (uint8_t)t;
420 dest->wordv[i] = (uint8_t)t;
425 if(a->length_B<=4 && b->length_B<=4){
428 memcpy(&p, a->wordv, a->length_B);
429 memcpy(&q, b->wordv, b->length_B);
430 r = (uint64_t)p*(uint64_t)q;
431 memcpy(dest->wordv, &r, a->length_B+b->length_B);
432 dest->length_B = a->length_B+b->length_B;
436 bigint_set_zero(dest);
437 /* split a in xh & xl; split b in yh & yl */
439 n=(MAX(a->length_B, b->length_B)+1)/2;
440 bigint_t xl, xh, yl, yh;
446 xl.length_B = a->length_B;
452 xh.wordv = a->wordv+n;
453 xh.length_B = a->length_B-n;
459 yl.length_B = b->length_B;
465 yh.wordv = b->wordv+n;
466 yh.length_B = b->length_B-n;
469 /* now we have split up a and b */
470 uint8_t tmp_b[2*n+2], m_b[2*(n+1)];
471 bigint_t tmp, tmp2, m;
473 tmp2.wordv = tmp_b+n+1;
476 bigint_mul_u(dest, &xl, &yl); /* dest <= xl*yl */
477 bigint_add_u(&tmp2, &xh, &xl); /* tmp2 <= xh+xl */
478 bigint_add_u(&tmp, &yh, &yl); /* tmp <= yh+yl */
479 bigint_mul_u(&m, &tmp2, &tmp); /* m <= tmp2*tmp */
480 bigint_mul_u(&tmp, &xh, &yh); /* h <= xh*yh */
481 bigint_sub_u(&m, &m, dest); /* m <= m-dest */
482 bigint_sub_u(&m, &m, &tmp); /* m <= m-h */
483 bigint_add_scale_u(dest, &m, n);
484 bigint_add_scale_u(dest, &tmp, 2*n);
487 /******************************************************************************/
489 void bigint_mul_s(bigint_t* dest, const bigint_t* a, const bigint_t* b){
492 s |= GET_SIGN(b)?1:0;
494 case 0: /* both positive */
495 bigint_mul_u(dest, a,b);
498 case 1: /* a positive, b negative */
499 bigint_mul_u(dest, a,b);
502 case 2: /* a negative, b positive */
503 bigint_mul_u(dest, a,b);
506 case 3: /* both negative */
507 bigint_mul_u(dest, a,b);
510 default: /* how can this happen?*/
515 /******************************************************************************/
518 /* (xh*b^n+xl)^2 = xh^2*b^2n + 2*xh*xl*b^n + xl^2 */
519 void bigint_square(bigint_t* dest, const bigint_t* a){
522 memcpy(&r, a->wordv, a->length_B);
524 memcpy(dest->wordv, &r, 2*a->length_B);
526 dest->length_B=2*a->length_B;
532 uint8_t d_b[a->length_B*2];
534 bigint_square(&d, a);
535 bigint_copy(dest, &d);
540 bigint_t xh, xl, tmp; /* x-high, x-low, temp */
541 uint8_t buffer[2*n+1];
544 xh.wordv = a->wordv+n;
545 xh.length_B = a->length_B-n;
547 bigint_square(dest, &xl);
548 bigint_square(&tmp, &xh);
549 bigint_add_scale_u(dest, &tmp, 2*n);
550 bigint_mul_u(&tmp, &xl, &xh);
551 bigint_shiftleft(&tmp, 1);
552 bigint_add_scale_u(dest, &tmp, n);
555 /******************************************************************************/
557 void bigint_sub_u_bitscale(bigint_t* a, const bigint_t* b, uint16_t bitscale){
559 uint8_t tmp_b[b->length_B+1];
560 uint16_t i,j,byteshift=bitscale/8;
564 if(a->length_B < b->length_B+byteshift){
570 bigint_copy(&tmp, b);
571 bigint_shiftleft(&tmp, bitscale&7);
573 for(j=0,i=byteshift; i<tmp.length_B+byteshift; ++i, ++j){
574 t = a->wordv[i] - tmp.wordv[j] - borrow;
575 a->wordv[i] = (uint8_t)t;
583 if(i+1 > a->length_B){
587 a->wordv[i] -= borrow;
588 if(a->wordv[i]!=0xff){
596 /******************************************************************************/
598 void bigint_reduce(bigint_t* a, const bigint_t* r){
600 uint8_t rfbs = GET_FBS(r);
602 if(r->length_B==0 || a->length_B==0){
605 while(a->length_B > r->length_B){
606 bigint_sub_u_bitscale(a, r, (a->length_B-r->length_B)*8+GET_FBS(a)-rfbs-1);
608 while((GET_FBS(a) > rfbs+1) && (a->length_B == r->length_B)){
609 bigint_sub_u_bitscale(a, r, GET_FBS(a)-rfbs-1);
611 while(bigint_cmp_u(a,r)>=0){
617 /******************************************************************************/
619 /* calculate dest = a**exp % r */
620 /* using square&multiply */
621 void bigint_expmod_u(bigint_t* dest, const bigint_t* a, const bigint_t* exp, const bigint_t* r){
622 if(a->length_B==0 || r->length_B==0){
627 uint8_t base_b[MAX(a->length_B,r->length_B*2)], res_b[r->length_B*2];
632 bigint_copy(&base, a);
633 bigint_reduce(&base, r);
638 for(i=0; i+1<exp->length_B; ++i){
642 bigint_mul_u(&res, &res, &base);
643 bigint_reduce(&res, r);
645 bigint_square(&base, &base);
646 bigint_reduce(&base, r);
653 bigint_mul_u(&res, &res, &base);
654 bigint_reduce(&res, r);
656 bigint_square(&base, &base);
657 bigint_reduce(&base, r);
661 bigint_copy(dest, &res);
664 /******************************************************************************/
665 /* gcd <-- gcd(x,y) a*x+b*y=gcd */
666 void bigint_gcdext(bigint_t* gcd, bigint_t* a, bigint_t* b, const bigint_t* x, const bigint_t* y){
667 bigint_t g, x_, y_, u, v, a_, b_, c_, d_;
668 volatile uint16_t i=0;
669 if(x->length_B==0 || y->length_B==0){
672 while(x->wordv[i]==0 && y->wordv[i]==0){
675 uint8_t g_b[i+2], x_b[x->length_B-i], y_b[y->length_B-i];
676 uint8_t u_b[x->length_B-i], v_b[y->length_B-i];
677 uint8_t a_b[y->length_B+2], c_b[y->length_B+2];
678 uint8_t b_b[x->length_B+2], d_b[x->length_B+2];
687 x_.info = y_.info = 0;
688 x_.length_B = x->length_B-i;
689 y_.length_B = y->length_B-i;
690 memcpy(x_.wordv, x->wordv+i, x_.length_B);
691 memcpy(y_.wordv, y->wordv+i, y_.length_B);
692 for(i=0; (x_.wordv[0]&(1<<i))==0 && (y_.wordv[0]&(1<<i))==0; ++i){
699 bigint_shiftleft(&g, i);
700 bigint_shiftright(&x_, i);
701 bigint_shiftright(&y_, i);
710 bigint_copy(&u, &x_);
711 bigint_copy(&v, &y_);
718 bigint_set_zero(&b_);
719 bigint_set_zero(&c_);
721 while((u.wordv[0]&1)==0){
722 bigint_shiftright(&u, 1);
723 if((a_.wordv[0]&1) || (b_.wordv[0]&1)){
724 bigint_add_s(&a_, &a_, &y_);
725 bigint_sub_s(&b_, &b_, &x_);
727 bigint_shiftright(&a_, 1);
728 bigint_shiftright(&b_, 1);
730 while((v.wordv[0]&1)==0){
731 bigint_shiftright(&v, 1);
732 if((c_.wordv[0]&1) || (d_.wordv[0]&1)){
733 bigint_add_s(&c_, &c_, &y_);
734 bigint_sub_s(&d_, &d_, &x_);
736 bigint_shiftright(&c_, 1);
737 bigint_shiftright(&d_, 1);
740 if(bigint_cmp_u(&u, &v)>=0){
741 bigint_sub_u(&u, &u, &v);
742 bigint_sub_s(&a_, &a_, &c_);
743 bigint_sub_s(&b_, &b_, &d_);
745 bigint_sub_u(&v, &v, &u);
746 bigint_sub_s(&c_, &c_, &a_);
747 bigint_sub_s(&d_, &d_, &b_);
751 bigint_mul_s(gcd, &v, &g);
761 /******************************************************************************/
763 void bigint_inverse(bigint_t* dest, const bigint_t* a, const bigint_t* m){
764 bigint_gcdext(NULL, dest, NULL, a, m);
765 while(dest->info&BIGINT_NEG_MASK){
766 bigint_add_s(dest, dest, m);
770 /******************************************************************************/
772 void bigint_changeendianess(bigint_t* a){
784 /******************************************************************************/