3 This file is part of the ARM-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__)
40 #include "uart_lowlevel.h"
41 #include "bigint_io.h"
45 #define MAX(a,b) (((a)>(b))?(a):(b))
49 #define MIN(a,b) (((a)<(b))?(a):(b))
52 #define SET_FBS(a, v) do{(a)->info &=~BIGINT_FBS_MASK; (a)->info |= (v);}while(0)
53 #define GET_FBS(a) ((a)->info&BIGINT_FBS_MASK)
54 #define SET_NEG(a) (a)->info |= BIGINT_NEG_MASK
55 #define SET_POS(a) (a)->info &= ~BIGINT_NEG_MASK
56 #define XCHG(a,b) do{(a)^=(b); (b)^=(a); (a)^=(b);}while(0)
57 #define XCHG_PTR(a,b) do{ a = (void*)(((uint32_t)(a)) ^ ((uint32_t)(b))); \
58 b = (void*)(((uint32_t)(a)) ^ ((uint32_t)(b))); \
59 a = (void*)(((uint32_t)(a)) ^ ((uint32_t)(b)));}while(0)
61 #define GET_SIGN(a) ((a)->info&BIGINT_NEG_MASK)
63 /******************************************************************************/
64 void bigint_adjust(bigint_t* a){
65 while(a->length_B!=0 && a->wordv[a->length_B-1]==0){
73 uint8_t i = BIGINT_WORD_SIZE-1;
74 t = a->wordv[a->length_B-1];
75 while((t&(1<<(BIGINT_WORD_SIZE-1)))==0 && i){
82 /******************************************************************************/
84 void bigint_copy(bigint_t* dest, const bigint_t* src){
85 memcpy(dest->wordv, src->wordv, src->length_B*sizeof(bigint_word_t));
86 dest->length_B = src->length_B;
87 dest->info = src->info;
90 /******************************************************************************/
92 /* this should be implemented in assembly */
93 void bigint_add_u(bigint_t* dest, const bigint_t* a, const bigint_t* b){
95 bigint_wordplus_t t=0LL;
96 if(a->length_B < b->length_B){
99 for(i=0; i<b->length_B; ++i){
100 // t = (bigint_wordplus_t)(a->wordv[i]) + (bigint_wordplus_t)(b->wordv[i]) + t;
103 dest->wordv[i] = (bigint_word_t)t;
104 t>>=BIGINT_WORD_SIZE;
106 for(; i<a->length_B; ++i){
108 dest->wordv[i] = (bigint_word_t)t;
109 t>>=BIGINT_WORD_SIZE;
111 dest->wordv[i++] = (bigint_word_t)t;
116 /******************************************************************************/
118 /* this should be implemented in assembly */
119 void bigint_add_scale_u(bigint_t* dest, const bigint_t* a, uint16_t scale){
123 bigint_wordplus_t t=0;
124 scale_w = (scale+sizeof(bigint_word_t)-1)/sizeof(bigint_word_t);
125 if(scale>dest->length_B*sizeof(bigint_word_t)){
126 memset(((uint8_t*)dest->wordv)+dest->length_B*sizeof(bigint_word_t), 0, scale-dest->length_B*sizeof(bigint_word_t));
128 // a->wordv = (const uint32_t*)(((uint8_t*)a->wordv)+(scale&3));
129 dst = dest->wordv + (scale&(sizeof(bigint_word_t)-1));
130 for(i=scale/sizeof(bigint_word_t); i<a->length_B+scale_w; ++i,++j){
132 if(dest->length_B>i){
135 dst[i] = (bigint_word_t)t;
136 t>>=BIGINT_WORD_SIZE;
139 if(dest->length_B>i){
142 dst[i] = (bigint_word_t)t;
143 t>>=BIGINT_WORD_SIZE;
146 if(dest->length_B < i){
152 /******************************************************************************/
154 /* this should be implemented in assembly */
155 void bigint_sub_u(bigint_t* dest, const bigint_t* a, const bigint_t* b){
158 bigint_wordplus_signed_t t=0LL;
159 uint16_t i, min, max;
160 min = MIN(a->length_B, b->length_B);
161 max = MAX(a->length_B, b->length_B);
162 r = bigint_cmp_u(a,b);
164 bigint_set_zero(dest);
168 bigint_copy(dest, a);
173 bigint_copy(dest, b);
178 bigint_sub_u(dest, b, a);
181 for(i=0; i<min; ++i){
187 dest->wordv[i]=(bigint_word_t)t;
190 dest->wordv[i]=(bigint_word_t)t;
194 t = a->wordv[i] - borrow;
197 dest->wordv[i]=(bigint_word_t)t;
200 dest->wordv[i]=(bigint_word_t)t;
210 /******************************************************************************/
212 int8_t bigint_cmp_u(const bigint_t* a, const bigint_t* b){
213 if(a->length_B > b->length_B){
216 if(a->length_B < b->length_B){
225 if(a->wordv[i]!=b->wordv[i]){
226 if(a->wordv[i]>b->wordv[i]){
236 /******************************************************************************/
238 void bigint_add_s(bigint_t* dest, const bigint_t* a, const bigint_t* b){
241 s |= GET_SIGN(b)?1:0;
243 case 0: /* both positive */
244 bigint_add_u(dest, a,b);
247 case 1: /* a positive, b negative */
248 bigint_sub_u(dest, a, b);
250 case 2: /* a negative, b positive */
251 bigint_sub_u(dest, b, a);
253 case 3: /* both negative */
254 bigint_add_u(dest, a, b);
257 default: /* how can this happen?*/
262 /******************************************************************************/
264 void bigint_sub_s(bigint_t* dest, const bigint_t* a, const bigint_t* b){
267 s |= GET_SIGN(b)?1:0;
269 case 0: /* both positive */
270 bigint_sub_u(dest, a,b);
272 case 1: /* a positive, b negative */
273 bigint_add_u(dest, a, b);
276 case 2: /* a negative, b positive */
277 bigint_add_u(dest, a, b);
280 case 3: /* both negative */
281 bigint_sub_u(dest, b, a);
283 default: /* how can this happen?*/
289 /******************************************************************************/
291 int8_t bigint_cmp_s(const bigint_t* a, const bigint_t* b){
293 if(a->length_B==0 && b->length_B==0){
297 s |= GET_SIGN(b)?1:0;
299 case 0: /* both positive */
300 return bigint_cmp_u(a, b);
302 case 1: /* a positive, b negative */
305 case 2: /* a negative, b positive */
308 case 3: /* both negative */
309 return bigint_cmp_u(b, a);
311 default: /* how can this happen?*/
314 return 0; /* just to satisfy the compiler */
317 /******************************************************************************/
319 void bigint_shiftleft(bigint_t* a, uint16_t shift){
320 uint16_t byteshift, word_alloc;
324 bigint_wordplus_t t=0;
330 for(i=0;i<=byteshift/sizeof(bigint_word_t); ++i){
331 a->wordv[a->length_B+i] = 0;
334 memmove(((uint8_t*)a->wordv)+byteshift, a->wordv, a->length_B*sizeof(bigint_word_t));
335 memset(a->wordv, 0, byteshift);
337 p = (bigint_word_t*)(((uint8_t*)a->wordv)+byteshift);
338 word_alloc = a->length_B+(byteshift+sizeof(bigint_word_t)-1)/sizeof(bigint_word_t)+1;
339 a->wordv[word_alloc-1]=0;
341 for(i=0; i<a->length_B; ++i){
342 t |= ((bigint_wordplus_t)p[i])<<bitshift;
343 p[i] = (bigint_word_t)t;
344 t >>= BIGINT_WORD_SIZE;
346 p[i] = (bigint_word_t)t;
348 a->length_B = word_alloc;
352 /******************************************************************************/
354 void bigint_shiftright(bigint_t* a, uint16_t shift){
358 bigint_wordplus_t t=0;
361 if(byteshift >= a->length_B*sizeof(bigint_word_t)){ /* we would shift out more than we have */
365 if(byteshift == a->length_B*sizeof(bigint_word_t)-1 && bitshift>GET_FBS(a)){
370 memmove(a->wordv, (uint8_t*)a->wordv+byteshift, a->length_B-byteshift);
371 memset((uint8_t*)a->wordv+a->length_B-byteshift, 0, byteshift);
373 byteshift /= sizeof(bigint_word_t);
375 /* shift to the right */
376 for(i=a->length_B-byteshift-1; i>0; --i){
377 t |= ((bigint_wordplus_t)(a->wordv[i]))<<(BIGINT_WORD_SIZE-bitshift);
378 a->wordv[i] = (bigint_word_t)(t>>BIGINT_WORD_SIZE);
379 t <<= BIGINT_WORD_SIZE;
381 t |= ((bigint_wordplus_t)(a->wordv[0]))<<(BIGINT_WORD_SIZE-bitshift);
382 a->wordv[0] = (bigint_word_t)(t>>BIGINT_WORD_SIZE);
384 a->length_B -= ((shift/8)+sizeof(bigint_word_t)-1)/sizeof(bigint_word_t);
388 /******************************************************************************/
390 void bigint_xor(bigint_t* dest, const bigint_t* a){
392 for(i=0; i<a->length_B; ++i){
393 dest->wordv[i] ^= a->wordv[i];
398 /******************************************************************************/
400 void bigint_set_zero(bigint_t* a){
404 /******************************************************************************/
406 /* using the Karatsuba-Algorithm */
407 /* x*y = (xh*yh)*b**2n + ((xh+xl)*(yh+yl) - xh*yh - xl*yl)*b**n + yh*yl */
408 void bigint_mul_u(bigint_t* dest, const bigint_t* a, const bigint_t* b){
409 if(a->length_B==0 || b->length_B==0){
410 bigint_set_zero(dest);
413 if(dest==a || dest==b){
415 bigint_word_t d_b[a->length_B+b->length_B];
417 bigint_mul_u(&d, a, b);
418 bigint_copy(dest, &d);
421 if(a->length_B==1 || b->length_B==1){
425 bigint_wordplus_t i, t=0;
426 bigint_word_t x = a->wordv[0];
427 for(i=0; i<b->length_B; ++i){
428 t += ((bigint_wordplus_t)b->wordv[i])*((bigint_wordplus_t)x);
429 dest->wordv[i] = (bigint_word_t)t;
430 t>>=BIGINT_WORD_SIZE;
432 dest->wordv[i] = (bigint_word_t)t;
437 if(a->length_B<=4/sizeof(bigint_word_t) && b->length_B<=4/sizeof(bigint_word_t)){
440 memcpy(&p, a->wordv, a->length_B*sizeof(bigint_word_t));
441 memcpy(&q, b->wordv, b->length_B*sizeof(bigint_word_t));
442 r = (uint64_t)p*(uint64_t)q;
443 memcpy(dest->wordv, &r, (a->length_B+b->length_B)*sizeof(bigint_word_t));
444 dest->length_B = a->length_B+b->length_B;
448 bigint_set_zero(dest);
449 /* split a in xh & xl; split b in yh & yl */
451 n=(MAX(a->length_B, b->length_B)+1)/2;
452 bigint_t xl, xh, yl, yh;
458 xl.length_B = a->length_B;
464 xh.wordv = a->wordv+n;
465 xh.length_B = a->length_B-n;
471 yl.length_B = b->length_B;
477 yh.wordv = b->wordv+n;
478 yh.length_B = b->length_B-n;
481 /* now we have split up a and b */
482 bigint_word_t tmp_b[2*n+2], m_b[2*(n+1)];
483 bigint_t tmp, tmp2, m;
485 tmp2.wordv = tmp_b+n+1;
488 bigint_mul_u(dest, &xl, &yl); /* dest <= xl*yl */
489 bigint_add_u(&tmp2, &xh, &xl); /* tmp2 <= xh+xl */
490 bigint_add_u(&tmp, &yh, &yl); /* tmp <= yh+yl */
491 bigint_mul_u(&m, &tmp2, &tmp); /* m <= tmp2*tmp */
492 bigint_mul_u(&tmp, &xh, &yh); /* h <= xh*yh */
493 bigint_sub_u(&m, &m, dest); /* m <= m-dest */
494 bigint_sub_u(&m, &m, &tmp); /* m <= m-h */
495 bigint_add_scale_u(dest, &m, n*sizeof(bigint_word_t));
496 bigint_add_scale_u(dest, &tmp, 2*n*sizeof(bigint_word_t));
499 /******************************************************************************/
501 void bigint_mul_s(bigint_t* dest, const bigint_t* a, const bigint_t* b){
504 s |= GET_SIGN(b)?1:0;
506 case 0: /* both positive */
507 bigint_mul_u(dest, a,b);
510 case 1: /* a positive, b negative */
511 bigint_mul_u(dest, a,b);
514 case 2: /* a negative, b positive */
515 bigint_mul_u(dest, a,b);
518 case 3: /* both negative */
519 bigint_mul_u(dest, a,b);
522 default: /* how can this happen?*/
527 /******************************************************************************/
530 /* (xh*b^n+xl)^2 = xh^2*b^2n + 2*xh*xl*b^n + xl^2 */
531 void bigint_square(bigint_t* dest, const bigint_t* a){
532 if(a->length_B*sizeof(bigint_word_t)<=4){
534 memcpy(&r, a->wordv, a->length_B*sizeof(bigint_word_t));
536 memcpy(dest->wordv, &r, 2*a->length_B*sizeof(bigint_word_t));
538 dest->length_B=2*a->length_B;
544 bigint_word_t d_b[a->length_B*2];
546 bigint_square(&d, a);
547 bigint_copy(dest, &d);
552 bigint_t xh, xl, tmp; /* x-high, x-low, temp */
553 bigint_word_t buffer[2*n+1];
556 xh.wordv = &(a->wordv[n]);
557 xh.length_B = a->length_B-n;
559 // cli_putstr("\r\nDBG (a): xl: "); bigint_print_hex(&xl);
560 // cli_putstr("\r\nDBG (b): xh: "); bigint_print_hex(&xh);
561 bigint_square(dest, &xl);
562 // cli_putstr("\r\nDBG (1): xl**2: "); bigint_print_hex(dest);
563 bigint_square(&tmp, &xh);
564 // cli_putstr("\r\nDBG (2): xh**2: "); bigint_print_hex(&tmp);
565 bigint_add_scale_u(dest, &tmp, 2*n*sizeof(bigint_word_t));
566 // cli_putstr("\r\nDBG (3): xl**2 + xh**2*n**2: "); bigint_print_hex(dest);
567 bigint_mul_u(&tmp, &xl, &xh);
568 // cli_putstr("\r\nDBG (4): xl*xh: "); bigint_print_hex(&tmp);
569 bigint_shiftleft(&tmp, 1);
570 // cli_putstr("\r\nDBG (5): xl*xh*2: "); bigint_print_hex(&tmp);
571 bigint_add_scale_u(dest, &tmp, n*sizeof(bigint_word_t));
572 // cli_putstr("\r\nDBG (6): x**2: "); bigint_print_hex(dest);
573 // cli_putstr("\r\n");
576 /******************************************************************************/
577 void bigint_sub_u_bitscale(bigint_t* a, const bigint_t* b, uint16_t bitscale){
579 bigint_word_t tmp_b[b->length_B+4];
580 uint16_t i,j,word_shift=bitscale/(8*sizeof(bigint_word_t));
582 bigint_wordplus_signed_t t;
584 if(a->length_B < b->length_B+word_shift){
585 cli_putstr("\r\nDBG: *bang*\r\n");
590 bigint_copy(&tmp, b);
591 bigint_shiftleft(&tmp, bitscale&(BIGINT_WORD_SIZE-1));
592 // cli_putstr("\r\nDBG(sub_ub.0) tmp_shift = "); bigint_print_hex(&tmp);
593 for(j=0,i=word_shift; i<tmp.length_B+word_shift; ++i, ++j){
597 a->wordv[i] = (bigint_word_t)t;
605 if(i+1 > a->length_B){
606 cli_putstr("\r\nDBG: *boom*\r\n");
610 a->wordv[i] -= borrow;
611 if(a->wordv[i]!=0xff){
619 /******************************************************************************/
621 void bigint_reduce(bigint_t* a, const bigint_t* r){
623 uint8_t rfbs = GET_FBS(r);
625 // cli_putstr("\r\nDBG: (a) = "); bigint_print_hex(a);
626 if(r->length_B==0 || a->length_B==0){
629 if((r->length_B*sizeof(bigint_word_t)<=4) && (a->length_B*sizeof(bigint_word_t)<=4)){
631 memcpy(&p, a->wordv, a->length_B*sizeof(bigint_word_t));
632 memcpy(&q, r->wordv, r->length_B*sizeof(bigint_word_t));
634 memcpy(a->wordv, &p, a->length_B*sizeof(bigint_word_t));
636 // cli_putstr("\r\nDBG: (0) = "); bigint_print_hex(a);
640 while(a->length_B > r->length_B){
641 shift = (a->length_B-r->length_B)*8*sizeof(bigint_word_t)+GET_FBS(a)-rfbs-1;
642 // cli_putstr("\r\nDBG: (p) shift = "); cli_hexdump_rev(&shift, 2);
644 bigint_sub_u_bitscale(a, r, shift);
645 // cli_putstr("\r\nDBG: (1) = "); bigint_print_hex(a);
647 while((GET_FBS(a) > rfbs+1) && (a->length_B == r->length_B)){
648 shift = GET_FBS(a)-rfbs-1;
649 // cli_putstr("\r\nDBG: (q) shift = "); cli_hexdump_rev(&shift, 2);
650 bigint_sub_u_bitscale(a, r, GET_FBS(a)-rfbs-1);
651 // cli_putstr("\r\nDBG: (2) = "); bigint_print_hex(a);
653 while(bigint_cmp_u(a,r)>=0){
655 // cli_putstr("\r\nDBG: (3) = "); bigint_print_hex(a);
658 // cli_putstr("\r\nDBG: (a) = "); bigint_print_hex(a);
659 // cli_putstr("\r\n");
662 /******************************************************************************/
664 /* calculate dest = a**exp % r */
665 /* using square&multiply */
666 void bigint_expmod_u(bigint_t* dest, const bigint_t* a, const bigint_t* exp, const bigint_t* r){
667 if(a->length_B==0 || r->length_B==0){
672 bigint_word_t t, base_b[MAX(a->length_B,r->length_B*2)], res_b[r->length_B*2];
677 bigint_copy(&base, a);
678 bigint_reduce(&base, r);
683 for(i=0; i+1<exp->length_B; ++i){
685 for(j=0; j<BIGINT_WORD_SIZE; ++j){
687 bigint_mul_u(&res, &res, &base);
688 bigint_reduce(&res, r);
690 bigint_square(&base, &base);
691 bigint_reduce(&base, r);
698 bigint_mul_u(&res, &res, &base);
699 bigint_reduce(&res, r);
701 bigint_square(&base, &base);
702 bigint_reduce(&base, r);
706 bigint_copy(dest, &res);
709 /******************************************************************************/
711 #define cli_putstr(a)
712 #define bigint_print_hex(a)
713 #define cli_hexdump_rev(a,b)
714 #define uart_flush(a)
716 /* gcd <-- gcd(x,y) a*x+b*y=gcd */
717 void bigint_gcdext(bigint_t* gcd, bigint_t* a, bigint_t* b, const bigint_t* x, const bigint_t* y){
718 bigint_t g, x_, y_, u, v, a_, b_, c_, d_;
720 if(x->length_B==0 || y->length_B==0){
723 if(x->length_B==1 && x->wordv[0]==1){
737 if(y->length_B==1 && y->wordv[0]==1){
752 while(x->wordv[i]==0 && y->wordv[i]==0){
755 bigint_word_t g_b[i+2], x_b[x->length_B-i], y_b[y->length_B-i];
756 bigint_word_t u_b[x->length_B-i], v_b[y->length_B-i];
757 bigint_word_t a_b[y->length_B+2], c_b[y->length_B+2];
758 bigint_word_t b_b[x->length_B+2], d_b[x->length_B+2];
763 memset(g_b, 0, i*sizeof(bigint_word_t));
767 x_.info = y_.info = 0;
768 x_.length_B = x->length_B-i;
769 y_.length_B = y->length_B-i;
770 memcpy(x_.wordv, x->wordv+i, x_.length_B*sizeof(bigint_word_t));
771 memcpy(y_.wordv, y->wordv+i, y_.length_B*sizeof(bigint_word_t));
772 for(i=0; (x_.wordv[0]&(1<<i))==0 && (y_.wordv[0]&(1<<i))==0; ++i){
779 bigint_shiftleft(&g, i);
780 bigint_shiftright(&x_, i);
781 bigint_shiftright(&y_, i);
791 bigint_copy(&u, &x_);
792 bigint_copy(&v, &y_);
799 bigint_set_zero(&b_);
800 bigint_set_zero(&c_);
802 cli_putstr("\r\nDBG (gcdext) 0");
803 while((u.wordv[0]&1)==0){
804 cli_putstr("\r\nDBG (gcdext) 0.1");
805 bigint_shiftright(&u, 1);
806 if((a_.wordv[0]&1) || (b_.wordv[0]&1)){
807 bigint_add_s(&a_, &a_, &y_);
808 bigint_sub_s(&b_, &b_, &x_);
810 bigint_shiftright(&a_, 1);
811 bigint_shiftright(&b_, 1);
813 while((v.wordv[0]&1)==0){
814 cli_putstr("\r\nDBG (gcdext) 0.2");
815 bigint_shiftright(&v, 1);
816 if((c_.wordv[0]&1) || (d_.wordv[0]&1)){
817 bigint_add_s(&c_, &c_, &y_);
818 bigint_sub_s(&d_, &d_, &x_);
820 bigint_shiftright(&c_, 1);
821 bigint_shiftright(&d_, 1);
824 if(bigint_cmp_u(&u, &v)>=0){
825 bigint_sub_u(&u, &u, &v);
826 bigint_sub_s(&a_, &a_, &c_);
827 bigint_sub_s(&b_, &b_, &d_);
829 bigint_sub_u(&v, &v, &u);
830 bigint_sub_s(&c_, &c_, &a_);
831 bigint_sub_s(&d_, &d_, &b_);
835 bigint_mul_s(gcd, &v, &g);
845 /******************************************************************************/
847 void bigint_inverse(bigint_t* dest, const bigint_t* a, const bigint_t* m){
848 bigint_gcdext(NULL, dest, NULL, a, m);
849 while(dest->info&BIGINT_NEG_MASK){
850 bigint_add_s(dest, dest, m);
854 /******************************************************************************/
856 void bigint_changeendianess(bigint_t* a){
858 p = (uint8_t*)(a->wordv);
859 q = ((uint8_t*)p)+a->length_B*sizeof(bigint_word_t)-1;
868 /******************************************************************************/