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__)
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)
59 /******************************************************************************/
61 void bigint_copy(bigint_t* dest, const bigint_t* src){
62 memcpy(dest->wordv, src->wordv, src->length_B);
63 dest->length_B = src->length_B;
64 dest->info = src->info;
67 /******************************************************************************/
69 /* this should be implemented in assembly */
71 void bigint_add_u(bigint_t* dest, const bigint_t* a, const bigint_t* b){
73 if(a->length_B < b->length_B){
76 for(i=0; i<b->length_B; ++i){
77 t = a->wordv[i] + b->wordv[i] + t;
78 dest->wordv[i] = (uint8_t)t;
81 for(; i<a->length_B; ++i){
83 dest->wordv[i] = (uint8_t)t;
91 /******************************************************************************/
93 /* this should be implemented in assembly */
94 void bigint_add_scale_u(bigint_t* dest, const bigint_t* a, uint16_t scale){
97 if(scale>dest->length_B)
98 memset(dest->wordv+dest->length_B, 0, scale-dest->length_B);
99 for(i=scale; i<a->length_B+scale; ++i,++j){
101 if(dest->length_B>i){
104 dest->wordv[i] = (uint8_t)t;
108 if(dest->length_B>i){
109 t = dest->wordv[i] + t;
111 dest->wordv[i] = (uint8_t)t;
115 if(dest->length_B < i){
121 /******************************************************************************/
123 /* this should be implemented in assembly */
124 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_B, b->length_B);
130 max = MAX(a->length_B, b->length_B);
131 r = bigint_cmp_u(a,b);
139 dest->length_B = a->length_B;
140 memcpy(dest->wordv, a->wordv, a->length_B);
141 dest->info = a->info;
146 dest->length_B = b->length_B;
147 memcpy(dest->wordv, b->wordv, b->length_B);
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;
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){
186 if(a->length_B > b->length_B){
189 if(a->length_B < b->length_B){
198 if(a->wordv[i]!=b->wordv[i]){
199 if(a->wordv[i]>b->wordv[i]){
209 /******************************************************************************/
211 void bigint_add_s(bigint_t* dest, const bigint_t* a, const bigint_t* b){
214 s |= GET_SIGN(b)?1:0;
216 case 0: /* both positive */
217 bigint_add_u(dest, a,b);
220 case 1: /* a positive, b negative */
221 bigint_sub_u(dest, a, b);
223 case 2: /* a negative, b positive */
224 bigint_sub_u(dest, b, a);
226 case 3: /* both negative */
227 bigint_add_u(dest, a, b);
230 default: /* how can this happen?*/
235 /******************************************************************************/
237 void bigint_sub_s(bigint_t* dest, const bigint_t* a, const bigint_t* b){
240 s |= GET_SIGN(b)?1:0;
242 case 0: /* both positive */
243 bigint_sub_u(dest, a,b);
245 case 1: /* a positive, b negative */
246 bigint_add_u(dest, a, b);
249 case 2: /* a negative, b positive */
250 bigint_add_u(dest, a, b);
253 case 3: /* both negative */
254 bigint_sub_u(dest, b, a);
256 default: /* how can this happen?*/
262 /******************************************************************************/
264 int8_t bigint_cmp_s(const bigint_t* a, const bigint_t* b){
266 if(a->length_B==0 && b->length_B==0){
270 s |= GET_SIGN(b)?1:0;
272 case 0: /* both positive */
273 return bigint_cmp_u(a, b);
275 case 1: /* a positive, b negative */
278 case 2: /* a negative, b positive */
281 case 3: /* both negative */
282 return bigint_cmp_u(b, a);
284 default: /* how can this happen?*/
287 return 0; /* just to satisfy the compiler */
290 /******************************************************************************/
292 void bigint_shiftleft(bigint_t* a, uint16_t shift){
297 byteshift = (shift+3)/8;
299 memmove(a->wordv+byteshift, a->wordv, a->length_B);
300 memset(a->wordv, 0, byteshift);
302 if(bitshift<=4){ /* shift to the left */
303 for(i=byteshift; i<a->length_B+byteshift; ++i){
304 t |= (a->wordv[i])<<bitshift;
305 a->wordv[i] = (uint8_t)t;
308 a->wordv[i] = (uint8_t)t;
310 }else{ /* shift to the right */
311 for(i=a->length_B+byteshift-1; i>byteshift-1; --i){
312 t |= (a->wordv[i])<<(bitshift);
313 a->wordv[i] = (uint8_t)(t>>8);
316 t |= (a->wordv[i])<<(bitshift);
317 a->wordv[i] = (uint8_t)(t>>8);
320 a->length_B += byteshift;
324 /******************************************************************************/
326 void bigint_shiftright(bigint_t* a, uint16_t shift){
333 if(byteshift >= a->length_B){ /* we would shift out more than we have */
337 if(byteshift == a->length_B-1 && bitshift>GET_FBS(a)){
342 memmove(a->wordv, a->wordv+byteshift, a->length_B-byteshift);
343 memset(a->wordv+a->length_B-byteshift, 0, byteshift);
346 /* shift to the right */
347 for(i=a->length_B-byteshift-1; i>0; --i){
348 t |= (a->wordv[i])<<(8-bitshift);
349 a->wordv[i] = (uint8_t)(t>>8);
352 t |= (a->wordv[0])<<(8-bitshift);
353 a->wordv[0] = (uint8_t)(t>>8);
355 a->length_B -= byteshift;
359 /******************************************************************************/
361 void bigint_xor(bigint_t* dest, const bigint_t* a){
363 for(i=0; i<a->length_B; ++i){
364 dest->wordv[i] ^= a->wordv[i];
369 /******************************************************************************/
371 void bigint_set_zero(bigint_t* a){
375 /******************************************************************************/
377 /* using the Karatsuba-Algorithm */
378 /* x*y = (xh*yh)*b**2n + ((xh+xl)*(yh+yl) - xh*yh - xl*yl)*b**n + yh*yl */
379 void bigint_mul_u(bigint_t* dest, const bigint_t* a, const bigint_t* b){
380 if(a->length_B==0 || b->length_B==0){
381 bigint_set_zero(dest);
384 if(dest==a || dest==b){
386 uint8_t d_b[a->length_B+b->length_B];
388 bigint_mul_u(&d, a, b);
389 bigint_copy(dest, &d);
392 if(a->length_B==1 || b->length_B==1){
397 uint8_t x = a->wordv[0];
398 for(i=0; i<b->length_B; ++i){
400 dest->wordv[i] = (uint8_t)t;
403 dest->wordv[i] = (uint8_t)t;
408 if(a->length_B<=4 && b->length_B<=4){
411 memcpy(&p, a->wordv, a->length_B);
412 memcpy(&q, b->wordv, b->length_B);
413 r = (uint64_t)p*(uint64_t)q;
414 memcpy(dest->wordv, &r, a->length_B+b->length_B);
415 dest->length_B = a->length_B+b->length_B;
419 bigint_set_zero(dest);
420 /* split a in xh & xl; split b in yh & yl */
422 n=(MAX(a->length_B, b->length_B)+1)/2;
423 bigint_t xl, xh, yl, yh;
429 xl.length_B = a->length_B;
435 xh.wordv = a->wordv+n;
436 xh.length_B = a->length_B-n;
442 yl.length_B = b->length_B;
448 yh.wordv = b->wordv+n;
449 yh.length_B = b->length_B-n;
452 /* now we have split up a and b */
453 uint8_t tmp_b[2*n+2], m_b[2*(n+1)];
454 bigint_t tmp, tmp2, m;
456 tmp2.wordv = tmp_b+n+1;
459 bigint_mul_u(dest, &xl, &yl); /* dest <= xl*yl */
460 bigint_add_u(&tmp2, &xh, &xl); /* tmp2 <= xh+xl */
461 bigint_add_u(&tmp, &yh, &yl); /* tmp <= yh+yl */
462 bigint_mul_u(&m, &tmp2, &tmp); /* m <= tmp2*tmp */
463 bigint_mul_u(&tmp, &xh, &yh); /* h <= xh*yh */
464 bigint_sub_u(&m, &m, dest); /* m <= m-dest */
465 bigint_sub_u(&m, &m, &tmp); /* m <= m-h */
466 bigint_add_scale_u(dest, &m, n);
467 bigint_add_scale_u(dest, &tmp, 2*n);
470 /******************************************************************************/
472 void bigint_mul_s(bigint_t* dest, const bigint_t* a, const bigint_t* b){
475 s |= GET_SIGN(b)?1:0;
477 case 0: /* both positive */
478 bigint_mul_u(dest, a,b);
481 case 1: /* a positive, b negative */
482 bigint_mul_u(dest, a,b);
485 case 2: /* a negative, b positive */
486 bigint_mul_u(dest, a,b);
489 case 3: /* both negative */
490 bigint_mul_u(dest, a,b);
493 default: /* how can this happen?*/
498 /******************************************************************************/
501 /* (xh*b^n+xl)^2 = xh^2*b^2n + 2*xh*xl*b^n + xl^2 */
502 void bigint_square(bigint_t* dest, const bigint_t* a){
505 memcpy(&r, a->wordv, a->length_B);
507 memcpy(dest->wordv, &r, 2*a->length_B);
509 dest->length_B=2*a->length_B;
515 uint8_t d_b[a->length_B*2];
517 bigint_square(&d, a);
518 bigint_copy(dest, &d);
523 bigint_t xh, xl, tmp; /* x-high, x-low, temp */
524 uint8_t buffer[2*n+1];
527 xh.wordv = a->wordv+n;
528 xh.length_B = a->length_B-n;
530 bigint_square(dest, &xl);
531 bigint_square(&tmp, &xh);
532 bigint_add_scale_u(dest, &tmp, 2*n);
533 bigint_mul_u(&tmp, &xl, &xh);
534 bigint_shiftleft(&tmp, 1);
535 bigint_add_scale_u(dest, &tmp, n);
538 /******************************************************************************/
540 void bigint_sub_u_bitscale(bigint_t* a, const bigint_t* b, uint16_t bitscale){
542 uint8_t tmp_b[b->length_B+1];
543 uint16_t i,j,byteshift=bitscale/8;
547 if(a->length_B < b->length_B+byteshift){
553 bigint_copy(&tmp, b);
554 bigint_shiftleft(&tmp, bitscale&7);
556 for(j=0,i=byteshift; i<tmp.length_B+byteshift; ++i, ++j){
557 t = a->wordv[i] - tmp.wordv[j] - borrow;
558 a->wordv[i] = (uint8_t)t;
566 if(i+1 > a->length_B){
570 a->wordv[i] -= borrow;
571 if(a->wordv[i]!=0xff){
579 /******************************************************************************/
581 void bigint_reduce(bigint_t* a, const bigint_t* r){
583 uint8_t rfbs = GET_FBS(r);
585 if(r->length_B==0 || a->length_B==0){
588 while(a->length_B > r->length_B){
589 bigint_sub_u_bitscale(a, r, (a->length_B-r->length_B)*8+GET_FBS(a)-rfbs-1);
591 while((GET_FBS(a) > rfbs+1) && (a->length_B == r->length_B)){
592 bigint_sub_u_bitscale(a, r, GET_FBS(a)-rfbs-1);
594 while(bigint_cmp_u(a,r)>=0){
600 /******************************************************************************/
602 /* calculate dest = a**exp % r */
603 /* using square&multiply */
604 void bigint_expmod_u(bigint_t* dest, const bigint_t* a, const bigint_t* exp, const bigint_t* r){
605 if(a->length_B==0 || r->length_B==0){
610 uint8_t base_b[MAX(a->length_B,r->length_B*2)], res_b[r->length_B*2];
615 bigint_copy(&base, a);
616 bigint_reduce(&base, r);
621 for(i=0; i+1<exp->length_B; ++i){
625 bigint_mul_u(&res, &res, &base);
626 bigint_reduce(&res, r);
628 bigint_square(&base, &base);
629 bigint_reduce(&base, r);
636 bigint_mul_u(&res, &res, &base);
637 bigint_reduce(&res, r);
639 bigint_square(&base, &base);
640 bigint_reduce(&base, r);
644 bigint_copy(dest, &res);
647 /******************************************************************************/
648 /* gcd <-- gcd(x,y) a*x+b*y=gcd */
649 void bigint_gcdext(bigint_t* gcd, bigint_t* a, bigint_t* b, const bigint_t* x, const bigint_t* y){
650 bigint_t g, x_, y_, u, v, a_, b_, c_, d_;
651 volatile uint16_t i=0;
652 if(x->length_B==0 || y->length_B==0){
655 while(x->wordv[i]==0 && y->wordv[i]==0){
658 uint8_t g_b[i+2], x_b[x->length_B-i], y_b[y->length_B-i];
659 uint8_t u_b[x->length_B-i], v_b[y->length_B-i];
660 uint8_t a_b[y->length_B+2], c_b[y->length_B+2];
661 uint8_t b_b[x->length_B+2], d_b[x->length_B+2];
670 x_.info = y_.info = 0;
671 x_.length_B = x->length_B-i;
672 y_.length_B = y->length_B-i;
673 memcpy(x_.wordv, x->wordv+i, x_.length_B);
674 memcpy(y_.wordv, y->wordv+i, y_.length_B);
675 for(i=0; (x_.wordv[0]&(1<<i))==0 && (y_.wordv[0]&(1<<i))==0; ++i){
682 bigint_shiftleft(&g, i);
683 bigint_shiftright(&x_, i);
684 bigint_shiftright(&y_, i);
693 bigint_copy(&u, &x_);
694 bigint_copy(&v, &y_);
701 bigint_set_zero(&b_);
702 bigint_set_zero(&c_);
704 while((u.wordv[0]&1)==0){
705 bigint_shiftright(&u, 1);
706 if((a_.wordv[0]&1) || (b_.wordv[0]&1)){
707 bigint_add_s(&a_, &a_, &y_);
708 bigint_sub_s(&b_, &b_, &x_);
710 bigint_shiftright(&a_, 1);
711 bigint_shiftright(&b_, 1);
713 while((v.wordv[0]&1)==0){
714 bigint_shiftright(&v, 1);
715 if((c_.wordv[0]&1) || (d_.wordv[0]&1)){
716 bigint_add_s(&c_, &c_, &y_);
717 bigint_sub_s(&d_, &d_, &x_);
719 bigint_shiftright(&c_, 1);
720 bigint_shiftright(&d_, 1);
723 if(bigint_cmp_u(&u, &v)>=0){
724 bigint_sub_u(&u, &u, &v);
725 bigint_sub_s(&a_, &a_, &c_);
726 bigint_sub_s(&b_, &b_, &d_);
728 bigint_sub_u(&v, &v, &u);
729 bigint_sub_s(&c_, &c_, &a_);
730 bigint_sub_s(&d_, &d_, &b_);
734 bigint_mul_s(gcd, &v, &g);
744 /******************************************************************************/
746 void bigint_inverse(bigint_t* dest, const bigint_t* a, const bigint_t* m){
747 bigint_gcdext(NULL, dest, NULL, a, m);
748 while(dest->info&BIGINT_NEG_MASK){
749 bigint_add_s(dest, dest, m);
753 /******************************************************************************/
755 void bigint_changeendianess(bigint_t* a){
767 /******************************************************************************/