diff --git a/bigint/bigint-stub.c b/bigint/bigint-stub.c index 64e8c91..a5dc765 100644 --- a/bigint/bigint-stub.c +++ b/bigint/bigint-stub.c @@ -59,8 +59,8 @@ /******************************************************************************/ /* void bigint_copy(bigint_t* dest, const bigint_t* src){ - memcpy(dest->wordv, src->wordv, src->length_B); - dest->length_B = src->length_B; + memcpy(dest->wordv, src->wordv, src->length_W); + dest->length_W = src->length_W; dest->info = src->info; } */ @@ -70,21 +70,21 @@ void bigint_copy(bigint_t* dest, const bigint_t* src){ /* void bigint_add_u(bigint_t* dest, const bigint_t* a, const bigint_t* b){ uint16_t t=0, i; - if(a->length_B < b->length_B){ + if(a->length_W < b->length_W){ XCHG_PTR(a,b); } - for(i=0; ilength_B; ++i){ + for(i=0; ilength_W; ++i){ t = a->wordv[i] + b->wordv[i] + t; dest->wordv[i] = (uint8_t)t; t>>=8; } - for(; ilength_B; ++i){ + for(; ilength_W; ++i){ t = a->wordv[i] + t; dest->wordv[i] = (uint8_t)t; t>>=8; } dest->wordv[i++] = t; - dest->length_B = i; + dest->length_W = i; bigint_adjust(dest); } */ @@ -95,26 +95,26 @@ void bigint_add_u(bigint_t* dest, const bigint_t* a, const bigint_t* b){ void bigint_add_scale_u(bigint_t* dest, const bigint_t* a, uint16_t scale){ uint16_t i,j=0; uint16_t t=0; - if(scale>dest->length_B) - memset(dest->wordv+dest->length_B, 0, scale-dest->length_B); - for(i=scale; ilength_B+scale; ++i,++j){ + if(scale>dest->length_W) + memset(dest->wordv+dest->length_W, 0, scale-dest->length_W); + for(i=scale; ilength_W+scale; ++i,++j){ t = a->wordv[j] + t; - if(dest->length_B>i){ + if(dest->length_W>i){ t += dest->wordv[i]; } dest->wordv[i] = (uint8_t)t; t>>=8; } while(t){ - if(dest->length_B>i){ + if(dest->length_W>i){ t = dest->wordv[i] + t; } dest->wordv[i] = (uint8_t)t; t>>=8; ++i; } - if(dest->length_B < i){ - dest->length_B = i; + if(dest->length_W < i){ + dest->length_W = i; } bigint_adjust(dest); } @@ -127,25 +127,25 @@ void bigint_sub_u(bigint_t* dest, const bigint_t* a, const bigint_t* b){ int8_t r; int16_t t; uint16_t i, min, max; - min = MIN(a->length_B, b->length_B); - max = MAX(a->length_B, b->length_B); + min = MIN(a->length_W, b->length_W); + max = MAX(a->length_W, b->length_W); r = bigint_cmp_u(a,b); if(r==0){ - dest->length_B = 0; + dest->length_W = 0; dest->wordv[0] = 0; bigint_adjust(dest); return; } - if(b->length_B==0){ - dest->length_B = a->length_B; - memcpy(dest->wordv, a->wordv, a->length_B); + if(b->length_W==0){ + dest->length_W = a->length_W; + memcpy(dest->wordv, a->wordv, a->length_W); dest->info = a->info; SET_POS(dest); return; } - if(a->length_B==0){ - dest->length_B = b->length_B; - memcpy(dest->wordv, b->wordv, b->length_B); + if(a->length_W==0){ + dest->length_W = b->length_W; + memcpy(dest->wordv, b->wordv, b->length_W); dest->info = b->info; SET_NEG(dest); return; @@ -176,7 +176,7 @@ void bigint_sub_u(bigint_t* dest, const bigint_t* a, const bigint_t* b){ } SET_POS(dest); - dest->length_B = i; + dest->length_W = i; bigint_adjust(dest); } } @@ -184,17 +184,17 @@ void bigint_sub_u(bigint_t* dest, const bigint_t* a, const bigint_t* b){ /******************************************************************************/ int8_t bigint_cmp_u(const bigint_t* a, const bigint_t* b){ - if(a->length_B > b->length_B){ + if(a->length_W > b->length_W){ return 1; } - if(a->length_B < b->length_B){ + if(a->length_W < b->length_W){ return -1; } - if(a->length_B==0){ + if(a->length_W==0){ return 0; } uint16_t i; - i = a->length_B-1; + i = a->length_W-1; do{ if(a->wordv[i]!=b->wordv[i]){ if(a->wordv[i]>b->wordv[i]){ @@ -264,7 +264,7 @@ void bigint_sub_s(bigint_t* dest, const bigint_t* a, const bigint_t* b){ int8_t bigint_cmp_s(const bigint_t* a, const bigint_t* b){ uint8_t s; - if(a->length_B==0 && b->length_B==0){ + if(a->length_W==0 && b->length_W==0){ return 0; } s = GET_SIGN(a)?2:0; @@ -297,11 +297,11 @@ void bigint_shiftleft(bigint_t* a, uint16_t shift){ uint16_t t=0; byteshift = (shift+3)/8; bitshift = shift&7; - memmove(a->wordv+byteshift, a->wordv, a->length_B); + memmove(a->wordv+byteshift, a->wordv, a->length_W); memset(a->wordv, 0, byteshift); if(bitshift!=0){ if(bitshift<=4){ /* shift to the left */ - for(i=byteshift; ilength_B+byteshift; ++i){ + for(i=byteshift; ilength_W+byteshift; ++i){ t |= (a->wordv[i])<wordv[i] = (uint8_t)t; t >>= 8; @@ -309,7 +309,7 @@ void bigint_shiftleft(bigint_t* a, uint16_t shift){ a->wordv[i] = (uint8_t)t; byteshift++; }else{ /* shift to the right */ - for(i=a->length_B+byteshift-1; i>byteshift-1; --i){ + for(i=a->length_W+byteshift-1; i>byteshift-1; --i){ t |= (a->wordv[i])<<(bitshift); a->wordv[i] = (uint8_t)(t>>8); t <<= 8; @@ -318,7 +318,7 @@ void bigint_shiftleft(bigint_t* a, uint16_t shift){ a->wordv[i] = (uint8_t)(t>>8); } } - a->length_B += byteshift; + a->length_W += byteshift; bigint_adjust(a); } @@ -331,21 +331,21 @@ void bigint_shiftright(bigint_t* a, uint16_t shift){ uint16_t t=0; byteshift = shift/8; bitshift = shift&7; - if(byteshift >= a->length_B){ /* we would shift out more than we have */ + if(byteshift >= a->length_W){ /* we would shift out more than we have */ bigint_set_zero(a); return; } - if(byteshift == a->length_B-1 && bitshift>GET_FBS(a)){ + if(byteshift == a->length_W-1 && bitshift>GET_FBS(a)){ bigint_set_zero(a); return; } if(byteshift){ - memmove(a->wordv, a->wordv+byteshift, a->length_B-byteshift); - memset(a->wordv+a->length_B-byteshift, 0, byteshift); + memmove(a->wordv, a->wordv+byteshift, a->length_W-byteshift); + memset(a->wordv+a->length_W-byteshift, 0, byteshift); } if(bitshift!=0){ /* shift to the right */ - for(i=a->length_B-byteshift-1; i>0; --i){ + for(i=a->length_W-byteshift-1; i>0; --i){ t |= (a->wordv[i])<<(8-bitshift); a->wordv[i] = (uint8_t)(t>>8); t <<= 8; @@ -353,7 +353,7 @@ void bigint_shiftright(bigint_t* a, uint16_t shift){ t |= (a->wordv[0])<<(8-bitshift); a->wordv[0] = (uint8_t)(t>>8); } - a->length_B -= byteshift; + a->length_W -= byteshift; bigint_adjust(a); } @@ -361,7 +361,7 @@ void bigint_shiftright(bigint_t* a, uint16_t shift){ void bigint_xor(bigint_t* dest, const bigint_t* a){ uint16_t i; - for(i=0; ilength_B; ++i){ + for(i=0; ilength_W; ++i){ dest->wordv[i] ^= a->wordv[i]; } bigint_adjust(dest); @@ -370,7 +370,7 @@ void bigint_xor(bigint_t* dest, const bigint_t* a){ /******************************************************************************/ void bigint_set_zero(bigint_t* a){ - a->length_B=0; + a->length_W=0; } /******************************************************************************/ @@ -378,76 +378,76 @@ void bigint_set_zero(bigint_t* a){ /* using the Karatsuba-Algorithm */ /* x*y = (xh*yh)*b**2n + ((xh+xl)*(yh+yl) - xh*yh - xl*yl)*b**n + yh*yl */ void bigint_mul_u(bigint_t* dest, const bigint_t* a, const bigint_t* b){ - if(a->length_B==0 || b->length_B==0){ + if(a->length_W==0 || b->length_W==0){ bigint_set_zero(dest); return; } if(dest==a || dest==b){ bigint_t d; - uint8_t d_b[a->length_B+b->length_B]; + uint8_t d_b[a->length_W+b->length_W]; d.wordv = d_b; bigint_mul_u(&d, a, b); bigint_copy(dest, &d); return; } - if(a->length_B==1 || b->length_B==1){ - if(a->length_B!=1){ + if(a->length_W==1 || b->length_W==1){ + if(a->length_W!=1){ XCHG_PTR(a,b); } uint16_t i, t=0; uint8_t x = a->wordv[0]; - for(i=0; ilength_B; ++i){ + for(i=0; ilength_W; ++i){ t += b->wordv[i]*x; dest->wordv[i] = (uint8_t)t; t>>=8; } dest->wordv[i] = (uint8_t)t; - dest->length_B=i+1; + dest->length_W=i+1; bigint_adjust(dest); return; } - if(a->length_B<=4 && b->length_B<=4){ + if(a->length_W<=4 && b->length_W<=4){ uint32_t p=0, q=0; uint64_t r; - memcpy(&p, a->wordv, a->length_B); - memcpy(&q, b->wordv, b->length_B); + memcpy(&p, a->wordv, a->length_W); + memcpy(&q, b->wordv, b->length_W); r = (uint64_t)p*(uint64_t)q; - memcpy(dest->wordv, &r, a->length_B+b->length_B); - dest->length_B = a->length_B+b->length_B; + memcpy(dest->wordv, &r, a->length_W+b->length_W); + dest->length_W = a->length_W+b->length_W; bigint_adjust(dest); return; } bigint_set_zero(dest); /* split a in xh & xl; split b in yh & yl */ uint16_t n; - n=(MAX(a->length_B, b->length_B)+1)/2; + n=(MAX(a->length_W, b->length_W)+1)/2; bigint_t xl, xh, yl, yh; xl.wordv = a->wordv; yl.wordv = b->wordv; - if(a->length_B<=n){ + if(a->length_W<=n){ xh.info=0; - xh.length_B = 0; - xl.length_B = a->length_B; + xh.length_W = 0; + xl.length_W = a->length_W; xl.info = 0; }else{ - xl.length_B=n; + xl.length_W=n; xl.info = 0; bigint_adjust(&xl); xh.wordv = a->wordv+n; - xh.length_B = a->length_B-n; + xh.length_W = a->length_W-n; xh.info = 0; } - if(b->length_B<=n){ + if(b->length_W<=n){ yh.info=0; - yh.length_B = 0; - yl.length_B = b->length_B; + yh.length_W = 0; + yl.length_W = b->length_W; yl.info = b->info; }else{ - yl.length_B=n; + yl.length_W=n; yl.info = 0; bigint_adjust(&yl); yh.wordv = b->wordv+n; - yh.length_B = b->length_B-n; + yh.length_W = b->length_W-n; yh.info = 0; } /* now we have split up a and b */ @@ -501,32 +501,32 @@ void bigint_mul_s(bigint_t* dest, const bigint_t* a, const bigint_t* b){ /* square */ /* (xh*b^n+xl)^2 = xh^2*b^2n + 2*xh*xl*b^n + xl^2 */ void bigint_square(bigint_t* dest, const bigint_t* a){ - if(a->length_B<=4){ + if(a->length_W<=4){ uint64_t r=0; - memcpy(&r, a->wordv, a->length_B); + memcpy(&r, a->wordv, a->length_W); r = r*r; - memcpy(dest->wordv, &r, 2*a->length_B); + memcpy(dest->wordv, &r, 2*a->length_W); SET_POS(dest); - dest->length_B=2*a->length_B; + dest->length_W=2*a->length_W; bigint_adjust(dest); return; } if(dest==a){ bigint_t d; - uint8_t d_b[a->length_B*2]; + uint8_t d_b[a->length_W*2]; d.wordv = d_b; bigint_square(&d, a); bigint_copy(dest, &d); return; } uint16_t n; - n=(a->length_B+1)/2; + n=(a->length_W+1)/2; bigint_t xh, xl, tmp; /* x-high, x-low, temp */ uint8_t buffer[2*n+1]; xl.wordv = a->wordv; - xl.length_B = n; + xl.length_W = n; xh.wordv = a->wordv+n; - xh.length_B = a->length_B-n; + xh.length_W = a->length_W-n; tmp.wordv = buffer; bigint_square(dest, &xl); bigint_square(&tmp, &xh); @@ -540,12 +540,12 @@ void bigint_square(bigint_t* dest, const bigint_t* a){ void bigint_sub_u_bitscale(bigint_t* a, const bigint_t* b, uint16_t bitscale){ bigint_t tmp; - uint8_t tmp_b[b->length_B+1]; + uint8_t tmp_b[b->length_W+1]; uint16_t i,j,byteshift=bitscale/8; uint8_t borrow=0; int16_t t; - if(a->length_B < b->length_B+byteshift){ + if(a->length_W < b->length_W+byteshift){ bigint_set_zero(a); return; } @@ -554,7 +554,7 @@ void bigint_sub_u_bitscale(bigint_t* a, const bigint_t* b, uint16_t bitscale){ bigint_copy(&tmp, b); bigint_shiftleft(&tmp, bitscale&7); - for(j=0,i=byteshift; iwordv[i] - tmp.wordv[j] - borrow; a->wordv[i] = (uint8_t)t; if(t<0){ @@ -564,7 +564,7 @@ void bigint_sub_u_bitscale(bigint_t* a, const bigint_t* b, uint16_t bitscale){ } } while(borrow){ - if(i+1 > a->length_B){ + if(i+1 > a->length_W){ bigint_set_zero(a); return; } @@ -583,13 +583,13 @@ void bigint_reduce(bigint_t* a, const bigint_t* r){ // bigint_adjust(r); uint8_t rfbs = GET_FBS(r); - if(r->length_B==0 || a->length_B==0){ + if(r->length_W==0 || a->length_W==0){ return; } - while(a->length_B > r->length_B){ - bigint_sub_u_bitscale(a, r, (a->length_B-r->length_B)*8+GET_FBS(a)-rfbs-1); + while(a->length_W > r->length_W){ + bigint_sub_u_bitscale(a, r, (a->length_W-r->length_W)*8+GET_FBS(a)-rfbs-1); } - while((GET_FBS(a) > rfbs+1) && (a->length_B == r->length_B)){ + while((GET_FBS(a) > rfbs+1) && (a->length_W == r->length_W)){ bigint_sub_u_bitscale(a, r, GET_FBS(a)-rfbs-1); } while(bigint_cmp_u(a,r)>=0){ @@ -603,12 +603,12 @@ void bigint_reduce(bigint_t* a, const bigint_t* r){ /* calculate dest = a**exp % r */ /* using square&multiply */ void bigint_expmod_u(bigint_t* dest, const bigint_t* a, const bigint_t* exp, const bigint_t* r){ - if(a->length_B==0 || r->length_B==0){ + if(a->length_W==0 || r->length_W==0){ return; } bigint_t res, base; - uint8_t base_b[MAX(a->length_B,r->length_B*2)], res_b[r->length_B*2]; + uint8_t base_b[MAX(a->length_W,r->length_W*2)], res_b[r->length_W*2]; uint16_t i; uint8_t j, t; res.wordv = res_b; @@ -616,10 +616,10 @@ void bigint_expmod_u(bigint_t* dest, const bigint_t* a, const bigint_t* exp, con bigint_copy(&base, a); bigint_reduce(&base, r); res.wordv[0]=1; - res.length_B=1; + res.length_W=1; res.info = 0; bigint_adjust(&res); - for(i=0; i+1length_B; ++i){ + for(i=0; i+1length_W; ++i){ t=exp->wordv[i]; for(j=0; j<8; ++j){ if(t&1){ @@ -650,29 +650,29 @@ void bigint_expmod_u(bigint_t* dest, const bigint_t* a, const bigint_t* exp, con void bigint_gcdext(bigint_t* gcd, bigint_t* a, bigint_t* b, const bigint_t* x, const bigint_t* y){ bigint_t g, x_, y_, u, v, a_, b_, c_, d_; volatile uint16_t i=0; - if(x->length_B==0 || y->length_B==0){ + if(x->length_W==0 || y->length_W==0){ return; } while(x->wordv[i]==0 && y->wordv[i]==0){ ++i; } - uint8_t g_b[i+2], x_b[x->length_B-i], y_b[y->length_B-i]; - uint8_t u_b[x->length_B-i], v_b[y->length_B-i]; - uint8_t a_b[y->length_B+2], c_b[y->length_B+2]; - uint8_t b_b[x->length_B+2], d_b[x->length_B+2]; + uint8_t g_b[i+2], x_b[x->length_W-i], y_b[y->length_W-i]; + uint8_t u_b[x->length_W-i], v_b[y->length_W-i]; + uint8_t a_b[y->length_W+2], c_b[y->length_W+2]; + uint8_t b_b[x->length_W+2], d_b[x->length_W+2]; g.wordv = g_b; x_.wordv = x_b; y_.wordv = y_b; memset(g_b, 0, i); g_b[i]=1; - g.length_B = i+1; + g.length_W = i+1; g.info=0; x_.info = y_.info = 0; - x_.length_B = x->length_B-i; - y_.length_B = y->length_B-i; - memcpy(x_.wordv, x->wordv+i, x_.length_B); - memcpy(y_.wordv, y->wordv+i, y_.length_B); + x_.length_W = x->length_W-i; + y_.length_W = y->length_W-i; + memcpy(x_.wordv, x->wordv+i, x_.length_W); + memcpy(y_.wordv, y->wordv+i, y_.length_W); for(i=0; (x_.wordv[0]&(1<wordv; - q = p+a->length_B-1; + q = p+a->length_W-1; while(plength_B!=0 && a->wordv[a->length_B-1]==0){ - a->length_B--; + while(a->length_W!=0 && a->wordv[a->length_W-1]==0){ + a->length_W--; } - if(a->length_B==0){ + if(a->length_W==0){ a->info=0; return; } bigint_word_t t; uint8_t i = BIGINT_WORD_SIZE-1; - t = a->wordv[a->length_B-1]; + t = a->wordv[a->length_W-1]; while((t&(1L<<(BIGINT_WORD_SIZE-1)))==0 && i){ t<<=1; i--; @@ -81,25 +81,25 @@ void bigint_adjust(bigint_t* a){ /******************************************************************************/ uint16_t bigint_length_b(const bigint_t* a){ - if(!a->length_B || a->length_B==0){ + if(!a->length_W || a->length_W==0){ return 0; } - return (a->length_B-1) * BIGINT_WORD_SIZE + GET_FBS(a); + return (a->length_W-1) * BIGINT_WORD_SIZE + GET_FBS(a); } /******************************************************************************/ uint16_t bigint_length_B(const bigint_t* a){ - return a->length_B * sizeof(bigint_word_t); + return a->length_W * sizeof(bigint_word_t); } /******************************************************************************/ uint32_t bigint_get_first_set_bit(const bigint_t* a){ - if(a->length_B==0){ + if(a->length_W==0){ return (uint32_t)(-1); } - return (a->length_B-1)*sizeof(bigint_word_t)*8+GET_FBS(a); + return (a->length_W-1)*sizeof(bigint_word_t)*8+GET_FBS(a); } @@ -109,10 +109,10 @@ uint32_t bigint_get_last_set_bit(const bigint_t* a){ uint32_t r=0; uint8_t b=0; bigint_word_t x=1; - if(a->length_B==0){ + if(a->length_W==0){ return (uint32_t)(-1); } - while(a->wordv[r]==0 && rlength_B){ + while(a->wordv[r]==0 && rlength_W){ ++r; } if(a->wordv[r] == 0){ @@ -128,8 +128,8 @@ uint32_t bigint_get_last_set_bit(const bigint_t* a){ /******************************************************************************/ void bigint_copy(bigint_t* dest, const bigint_t* src){ - memcpy(dest->wordv, src->wordv, src->length_B*sizeof(bigint_word_t)); - dest->length_B = src->length_B; + memcpy(dest->wordv, src->wordv, src->length_W*sizeof(bigint_word_t)); + dest->length_W = src->length_W; dest->info = src->info; } @@ -139,16 +139,16 @@ void bigint_copy(bigint_t* dest, const bigint_t* src){ void bigint_add_u(bigint_t* dest, const bigint_t* a, const bigint_t* b){ uint16_t i; bigint_wordplus_t t = 0LL; - if(a->length_B < b->length_B){ + if(a->length_W < b->length_W){ XCHG_PTR(a,b); } - for(i = 0; i < b->length_B; ++i){ + for(i = 0; i < b->length_W; ++i){ t += a->wordv[i]; t += b->wordv[i]; dest->wordv[i] = (bigint_word_t)t; t >>= BIGINT_WORD_SIZE; } - for(; i < a->length_B; ++i){ + for(; i < a->length_W; ++i){ t += a->wordv[i]; dest->wordv[i] = (bigint_word_t)t; t >>= BIGINT_WORD_SIZE; @@ -156,7 +156,7 @@ void bigint_add_u(bigint_t* dest, const bigint_t* a, const bigint_t* b){ if(t){ dest->wordv[i++] = (bigint_word_t)t; } - dest->length_B = i; + dest->length_W = i; bigint_adjust(dest); } @@ -164,7 +164,7 @@ void bigint_add_u(bigint_t* dest, const bigint_t* a, const bigint_t* b){ /* this should be implemented in assembly */ void bigint_add_scale_u(bigint_t* dest, const bigint_t* a, uint16_t scale){ - if(a->length_B == 0){ + if(a->length_W == 0){ return; } if(scale == 0){ @@ -181,47 +181,47 @@ void bigint_add_scale_u(bigint_t* dest, const bigint_t* a, uint16_t scale){ cli_putc(')'); #endif #if BIGINT_WORD_SIZE == 8 - if(scale >= dest->length_B){ + if(scale >= dest->length_W){ #if DEBUG_ADD_SCALE cli_putstr_P(PSTR("\r\n\tpath one")); #endif - memset(dest->wordv + dest->length_B, 0, scale - dest->length_B); - memcpy(dest->wordv + scale, a->wordv, a->length_B); + memset(dest->wordv + dest->length_W, 0, scale - dest->length_W); + memcpy(dest->wordv + scale, a->wordv, a->length_W); dest->info = a->info; - dest->length_B = a->length_B + scale; + dest->length_W = a->length_W + scale; return; } bigint_t x; #if DEBUG_ADD_SCALE cli_putstr_P(PSTR("\r\n\tpath two")); #endif - x.length_B = dest->length_B - scale; + x.length_W = dest->length_W - scale; x.info = dest->info; x.wordv = dest->wordv + scale; bigint_add_u(&x, &x, a); - dest->length_B = x.length_B + scale; + dest->length_W = x.length_W + scale; dest->info = 0; bigint_adjust(dest); #else bigint_t s; uint16_t word_shift = scale / sizeof(bigint_word_t), byte_shift = scale % sizeof(bigint_word_t); - bigint_word_t bv[a->length_B + 1]; + bigint_word_t bv[a->length_W + 1]; s.wordv = bv; - bv[0] = bv[a->length_B] = 0; - memcpy((uint8_t*)bv + byte_shift, a->wordv, a->length_B * sizeof(bigint_word_t)); - s.length_B = a->length_B + 1; + bv[0] = bv[a->length_W] = 0; + memcpy((uint8_t*)bv + byte_shift, a->wordv, a->length_W * sizeof(bigint_word_t)); + s.length_W = a->length_W + 1; bigint_adjust(&s); - memset(dest->wordv + dest->length_B, 0, (MAX(dest->length_B, s.length_B + word_shift) - dest->length_B) * sizeof(bigint_word_t)); + memset(dest->wordv + dest->length_W, 0, (MAX(dest->length_W, s.length_W + word_shift) - dest->length_W) * sizeof(bigint_word_t)); x.wordv = dest->wordv + word_shift; - x.length_B = dest->length_B - word_shift; - if((int16_t)x.length_B < 0){ - x.length_B = 0; + x.length_W = dest->length_W - word_shift; + if((int16_t)x.length_W < 0){ + x.length_W = 0; x.info = 0; }else{ x.info = dest->info; } bigint_add_u(&x, &x, &s); - dest->length_B = x.length_B + word_shift; + dest->length_W = x.length_W + word_shift; dest->info = 0; bigint_adjust(dest); #endif @@ -232,29 +232,29 @@ void bigint_add_scale_u(bigint_t* dest, const bigint_t* a, uint16_t scale){ bigint_word_t *dst; bigint_wordplus_t t=0; scale_w = (scale+sizeof(bigint_word_t)-1)/sizeof(bigint_word_t); - if(scale>dest->length_B*sizeof(bigint_word_t)){ - memset(((uint8_t*)dest->wordv)+dest->length_B*sizeof(bigint_word_t), 0, scale-dest->length_B*sizeof(bigint_word_t)); + if(scale>dest->length_W*sizeof(bigint_word_t)){ + memset(((uint8_t*)dest->wordv)+dest->length_W*sizeof(bigint_word_t), 0, scale-dest->length_W*sizeof(bigint_word_t)); } // a->wordv = (const uint32_t*)(((uint8_t*)a->wordv)+(scale&3)); dst = dest->wordv + (scale&(sizeof(bigint_word_t)-1)); - for(i=scale/sizeof(bigint_word_t); ilength_B+scale_w; ++i,++j){ + for(i=scale/sizeof(bigint_word_t); ilength_W+scale_w; ++i,++j){ t += a->wordv[j]; - if(dest->length_B>i){ + if(dest->length_W>i){ t += dst[i]; } dst[i] = (bigint_word_t)t; t>>=BIGINT_WORD_SIZE; } while(t){ - if(dest->length_B>i){ + if(dest->length_W>i){ t += dst[i]; } dst[i] = (bigint_word_t)t; t>>=BIGINT_WORD_SIZE; ++i; } - if(dest->length_B < i){ - dest->length_B = i; + if(dest->length_W < i){ + dest->length_W = i; } bigint_adjust(dest); */ @@ -273,12 +273,12 @@ void bigint_sub_u(bigint_t* dest, const bigint_t* a, const bigint_t* b){ bigint_set_zero(dest); return; } - if(b->length_B==0){ + if(b->length_W==0){ bigint_copy(dest, a); SET_POS(dest); return; } - if(a->length_B==0){ + if(a->length_W==0){ bigint_copy(dest, b); SET_NEG(dest); return; @@ -288,9 +288,9 @@ void bigint_sub_u(bigint_t* dest, const bigint_t* a, const bigint_t* b){ SET_NEG(dest); return; } - for(i=0; i < a->length_B; ++i){ + for(i=0; i < a->length_W; ++i){ t = a->wordv[i]; - if(i < b->length_B){ + if(i < b->length_W){ t -= b->wordv[i]; } t -= borrow; @@ -302,24 +302,24 @@ void bigint_sub_u(bigint_t* dest, const bigint_t* a, const bigint_t* b){ } } SET_POS(dest); - dest->length_B = i; + dest->length_W = i; bigint_adjust(dest); } /******************************************************************************/ int8_t bigint_cmp_u(const bigint_t* a, const bigint_t* b){ - if(a->length_B > b->length_B){ + if(a->length_W > b->length_W){ return 1; } - if(a->length_B < b->length_B){ + if(a->length_W < b->length_W){ return -1; } - if(a->length_B==0){ + if(a->length_W==0){ return 0; } uint16_t i; - i = a->length_B-1; + i = a->length_W-1; do{ if(a->wordv[i] != b->wordv[i]){ if(a->wordv[i] > b->wordv[i]){ @@ -389,7 +389,7 @@ void bigint_sub_s(bigint_t* dest, const bigint_t* a, const bigint_t* b){ int8_t bigint_cmp_s(const bigint_t* a, const bigint_t* b){ uint8_t s; - if(a->length_B==0 && b->length_B==0){ + if(a->length_W==0 && b->length_W==0){ return 0; } s = GET_SIGN(a)?2:0; @@ -426,14 +426,14 @@ void bigint_shiftleft(bigint_t* a, uint16_t shift){ } byteshift = shift / 8; bitshift = shift & 7; - memset(&a->wordv[a->length_B], 0x00, byteshift); + memset(&a->wordv[a->length_W], 0x00, byteshift); if(byteshift){ - memmove(((uint8_t*)a->wordv)+byteshift, a->wordv, a->length_B * sizeof(bigint_word_t)); + memmove(((uint8_t*)a->wordv)+byteshift, a->wordv, a->length_W * sizeof(bigint_word_t)); memset(a->wordv, 0, byteshift); } if(bitshift != 0){ p = (bigint_word_t*)((uint8_t*)a->wordv + byteshift / sizeof(bigint_word_t)); - words_to_shift = a->length_B + ((byteshift % sizeof(bigint_word_t))?1:0); + words_to_shift = a->length_W + ((byteshift % sizeof(bigint_word_t))?1:0); /* XXX */ for(i = 0; i < words_to_shift; ++i){ t |= ((bigint_wordplus_t)p[i]) << bitshift; @@ -442,7 +442,7 @@ void bigint_shiftleft(bigint_t* a, uint16_t shift){ } p[i] = (bigint_word_t)t; } - a->length_B += (shift + BIGINT_WORD_SIZE - 1) / BIGINT_WORD_SIZE; + a->length_W += (shift + BIGINT_WORD_SIZE - 1) / BIGINT_WORD_SIZE; bigint_adjust(a); } @@ -455,23 +455,23 @@ void bigint_shiftright(bigint_t* a, uint16_t shift){ bigint_wordplus_t t=0; byteshift = shift/8; bitshift = shift&7; - if(byteshift >= a->length_B * sizeof(bigint_word_t)){ /* we would shift out more than we have */ + if(byteshift >= a->length_W * sizeof(bigint_word_t)){ /* we would shift out more than we have */ bigint_set_zero(a); return; } - if(byteshift == a->length_B * sizeof(bigint_word_t) - 1 && bitshift > GET_FBS(a)){ + if(byteshift == a->length_W * sizeof(bigint_word_t) - 1 && bitshift > GET_FBS(a)){ bigint_set_zero(a); return; } if(byteshift){ - memmove(a->wordv, (uint8_t*)a->wordv + byteshift, a->length_B * sizeof(bigint_word_t) - byteshift); - memset((uint8_t*)a->wordv + a->length_B * sizeof(bigint_word_t) - byteshift, 0, byteshift); + memmove(a->wordv, (uint8_t*)a->wordv + byteshift, a->length_W * sizeof(bigint_word_t) - byteshift); + memset((uint8_t*)a->wordv + a->length_W * sizeof(bigint_word_t) - byteshift, 0, byteshift); } byteshift /= sizeof(bigint_word_t); - a->length_B -= (byteshift + sizeof(bigint_word_t) - 1) / sizeof(bigint_word_t); - if(bitshift != 0 && a->length_B){ + a->length_W -= (byteshift + sizeof(bigint_word_t) - 1) / sizeof(bigint_word_t); + if(bitshift != 0 && a->length_W){ /* shift to the right */ - i = a->length_B - 1; + i = a->length_W - 1; do{ t |= ((bigint_wordplus_t)(a->wordv[i])) << (BIGINT_WORD_SIZE - bitshift); a->wordv[i] = (bigint_word_t)(t >> BIGINT_WORD_SIZE); @@ -485,7 +485,7 @@ void bigint_shiftright(bigint_t* a, uint16_t shift){ void bigint_xor(bigint_t* dest, const bigint_t* a){ uint16_t i; - for(i=0; ilength_B; ++i){ + for(i=0; ilength_W; ++i){ dest->wordv[i] ^= a->wordv[i]; } bigint_adjust(dest); @@ -494,7 +494,7 @@ void bigint_xor(bigint_t* dest, const bigint_t* a){ /******************************************************************************/ void bigint_set_zero(bigint_t* a){ - a->length_B=0; + a->length_W=0; } /******************************************************************************/ @@ -502,55 +502,55 @@ void bigint_set_zero(bigint_t* a){ /* using the Karatsuba-Algorithm */ /* x*y = (xh*yh)*b**2n + ((xh+xl)*(yh+yl) - xh*yh - xl*yl)*b**n + yh*yl */ void bigint_mul_u(bigint_t* dest, const bigint_t* a, const bigint_t* b){ - if(a->length_B==0 || b->length_B==0){ + if(a->length_W==0 || b->length_W==0){ bigint_set_zero(dest); return; } if(dest==a || dest==b){ bigint_t d; - bigint_word_t d_b[a->length_B + b->length_B]; + bigint_word_t d_b[a->length_W + b->length_W]; d.wordv = d_b; bigint_mul_u(&d, a, b); bigint_copy(dest, &d); return; } - if(a->length_B==1 || b->length_B==1){ - if(a->length_B != 1){ + if(a->length_W==1 || b->length_W==1){ + if(a->length_W != 1){ XCHG_PTR(a,b); } bigint_wordplus_t t=0; uint16_t i; bigint_word_t x = a->wordv[0]; - for(i=0; i < b->length_B; ++i){ + for(i=0; i < b->length_W; ++i){ t += ((bigint_wordplus_t)b->wordv[i]) * ((bigint_wordplus_t)x); dest->wordv[i] = (bigint_word_t)t; t>>=BIGINT_WORD_SIZE; } dest->wordv[i] = (bigint_word_t)t; - dest->length_B = i+1; + dest->length_W = i+1; dest->info = 0; bigint_adjust(dest); return; } - if(a->length_B * sizeof(bigint_word_t) <= 4 && b->length_B * sizeof(bigint_word_t) <= 4){ + if(a->length_W * sizeof(bigint_word_t) <= 4 && b->length_W * sizeof(bigint_word_t) <= 4){ uint32_t p=0, q=0; uint64_t r; - memcpy(&p, a->wordv, a->length_B*sizeof(bigint_word_t)); - memcpy(&q, b->wordv, b->length_B*sizeof(bigint_word_t)); + memcpy(&p, a->wordv, a->length_W*sizeof(bigint_word_t)); + memcpy(&q, b->wordv, b->length_W*sizeof(bigint_word_t)); r = (uint64_t)p * (uint64_t)q; - memcpy(dest->wordv, &r, (dest->length_B = a->length_B + b->length_B)*sizeof(bigint_word_t)); + memcpy(dest->wordv, &r, (dest->length_W = a->length_W + b->length_W)*sizeof(bigint_word_t)); bigint_adjust(dest); return; } #if BIGINT_WORD_SIZE == 8 - if(a->length_B <= 4 || b->length_B <= 4){ - if(a->length_B > 4){ + if(a->length_W <= 4 || b->length_W <= 4){ + if(a->length_W > 4){ XCHG_PTR(a,b); } uint32_t x = 0, y = 0; - uint16_t j = b->length_B / 4, idx = 0; + uint16_t j = b->length_W / 4, idx = 0; uint64_t r = 0; - memcpy(&x, a->wordv, a->length_B); + memcpy(&x, a->wordv, a->length_W); while(j){ r += (uint64_t)((uint32_t*)b->wordv)[idx] * (uint64_t)x; ((uint32_t*)dest->wordv)[idx] = (uint32_t)r; @@ -559,44 +559,44 @@ void bigint_mul_u(bigint_t* dest, const bigint_t* a, const bigint_t* b){ --j; } idx *= 4; - memcpy(&y, b->wordv + idx, b->length_B - idx); + memcpy(&y, b->wordv + idx, b->length_W - idx); r += (uint64_t)y * (uint64_t)x; while(r){ dest->wordv[idx++] = (uint8_t)r; r >>= 8; } - dest->length_B = idx; + dest->length_W = idx; bigint_adjust(dest); return; } #endif /* split a in xh & xl; split b in yh & yl */ - const uint16_t n = (MAX(a->length_B, b->length_B)+1)/2; + const uint16_t n = (MAX(a->length_W, b->length_W)+1)/2; bigint_t xl, xh, yl, yh; xl.wordv = a->wordv; yl.wordv = b->wordv; - if(a->length_B <= n){ + if(a->length_W <= n){ bigint_set_zero(&xh); - xl.length_B = a->length_B; + xl.length_W = a->length_W; xl.info = a->info; }else{ - xl.length_B = n; + xl.length_W = n; xl.info = 0; bigint_adjust(&xl); xh.wordv = &(a->wordv[n]); - xh.length_B = a->length_B-n; + xh.length_W = a->length_W-n; xh.info = a->info; } - if(b->length_B <= n){ + if(b->length_W <= n){ bigint_set_zero(&yh); - yl.length_B = b->length_B; + yl.length_W = b->length_W; yl.info = b->info; }else{ - yl.length_B = n; + yl.length_W = n; yl.info = 0; bigint_adjust(&yl); yh.wordv = &(b->wordv[n]); - yh.length_B = b->length_B-n; + yh.length_W = b->length_W-n; yh.info = b->info; } /* now we have split up a and b */ @@ -669,33 +669,33 @@ void bigint_mul_s(bigint_t* dest, const bigint_t* a, const bigint_t* b){ /* square */ /* (xh*b^n+xl)^2 = xh^2*b^2n + 2*xh*xl*b^n + xl^2 */ void bigint_square(bigint_t* dest, const bigint_t* a){ - if(a->length_B * sizeof(bigint_word_t) <= 4){ + if(a->length_W * sizeof(bigint_word_t) <= 4){ uint64_t r = 0; - memcpy(&r, a->wordv, a->length_B * sizeof(bigint_word_t)); + memcpy(&r, a->wordv, a->length_W * sizeof(bigint_word_t)); r = r * r; - memcpy(dest->wordv, &r, 2 * a->length_B*sizeof(bigint_word_t)); + memcpy(dest->wordv, &r, 2 * a->length_W*sizeof(bigint_word_t)); SET_POS(dest); - dest->length_B = 2 * a->length_B; + dest->length_W = 2 * a->length_W; bigint_adjust(dest); return; } if(dest==a){ bigint_t d; - bigint_word_t d_b[a->length_B * 2]; + bigint_word_t d_b[a->length_W * 2]; d.wordv = d_b; bigint_square(&d, a); bigint_copy(dest, &d); return; } uint16_t n; - n = (a->length_B + 1) / 2; + n = (a->length_W + 1) / 2; bigint_t xh, xl, tmp; /* x-high, x-low, temp */ bigint_word_t buffer[2 * n + 1]; xl.wordv = a->wordv; - xl.length_B = n; + xl.length_W = n; xl.info = 0; xh.wordv = &(a->wordv[n]); - xh.length_B = a->length_B - n; + xh.length_W = a->length_W - n; xh.info = a->info; bigint_adjust(&xl); tmp.wordv = buffer; @@ -721,10 +721,10 @@ void bigint_square(bigint_t* dest, const bigint_t* a){ /******************************************************************************/ void bigint_sub_u_bitscale(bigint_t* a, const bigint_t* b, uint16_t bitscale){ bigint_t tmp, x; - bigint_word_t tmp_b[b->length_B + 1]; + bigint_word_t tmp_b[b->length_W + 1]; const uint16_t word_shift = bitscale / BIGINT_WORD_SIZE; - if(a->length_B < b->length_B + word_shift){ + if(a->length_W < b->length_W + word_shift){ #if DEBUG cli_putstr("\r\nDBG: *bang*\r\n"); #endif @@ -739,10 +739,10 @@ void bigint_sub_u_bitscale(bigint_t* a, const bigint_t* b, uint16_t bitscale){ x.info = a->info; x.wordv = &(a->wordv[word_shift]); - x.length_B = a->length_B - word_shift; + x.length_W = a->length_W - word_shift; bigint_sub_u(&x, &x, &tmp); - a->length_B = x.length_B + word_shift; + a->length_W = x.length_W + word_shift; bigint_adjust(a); return; } @@ -755,40 +755,40 @@ void bigint_reduce(bigint_t* a, const bigint_t* r){ #if DEBUG cli_putstr("\r\nDBG: (a) = "); bigint_print_hex(a); #endif - if(r->length_B==0 || a->length_B==0){ + if(r->length_W==0 || a->length_W==0){ return; } - if((r->length_B*sizeof(bigint_word_t)<=4) && (a->length_B*sizeof(bigint_word_t)<=4)){ + if((r->length_W*sizeof(bigint_word_t)<=4) && (a->length_W*sizeof(bigint_word_t)<=4)){ uint32_t p=0, q=0; - memcpy(&p, a->wordv, a->length_B*sizeof(bigint_word_t)); - memcpy(&q, r->wordv, r->length_B*sizeof(bigint_word_t)); + memcpy(&p, a->wordv, a->length_W*sizeof(bigint_word_t)); + memcpy(&q, r->wordv, r->length_W*sizeof(bigint_word_t)); p %= q; - memcpy(a->wordv, &p, a->length_B*sizeof(bigint_word_t)); + memcpy(a->wordv, &p, a->length_W*sizeof(bigint_word_t)); bigint_adjust(a); // cli_putstr("\r\nDBG: (0) = "); bigint_print_hex(a); return; } uint16_t shift; - while(a->length_B > r->length_B){ - shift = (a->length_B - r->length_B) * 8 * sizeof(bigint_word_t) + GET_FBS(a) - rfbs - 1; + while(a->length_W > r->length_W){ + shift = (a->length_W - r->length_W) * 8 * sizeof(bigint_word_t) + GET_FBS(a) - rfbs - 1; /* - if((a->wordv[a->length_B-1] & ((1LL< r->wordv[r->length_B-1]){ + if((a->wordv[a->length_W-1] & ((1LL< r->wordv[r->length_W-1]){ // cli_putc('~'); cli_putstr("\r\n ~ [a] = "); - cli_hexdump_rev(&a->wordv[a->length_B-1], 4); + cli_hexdump_rev(&a->wordv[a->length_W-1], 4); cli_putstr(" [r] = "); - cli_hexdump_rev(&r->wordv[r->length_B-1], 4); + cli_hexdump_rev(&r->wordv[r->length_W-1], 4); shift += 1; } */ // cli_putstr("\r\nDBG: (p) shift = "); cli_hexdump_rev(&shift, 2); -// cli_putstr(" a_len = "); cli_hexdump_rev(&a->length_B, 2); -// cli_putstr(" r_len = "); cli_hexdump_rev(&r->length_B, 2); +// cli_putstr(" a_len = "); cli_hexdump_rev(&a->length_W, 2); +// cli_putstr(" r_len = "); cli_hexdump_rev(&r->length_W, 2); // uart_flush(0); bigint_sub_u_bitscale(a, r, shift); // cli_putstr("\r\nDBG: (1) = "); bigint_print_hex(a); } - while((GET_FBS(a) > rfbs) && (a->length_B == r->length_B)){ + while((GET_FBS(a) > rfbs) && (a->length_W == r->length_W)){ shift = GET_FBS(a)-rfbs-1; // cli_putstr("\r\nDBG: (2a) = "); bigint_print_hex(a); // cli_putstr("\r\nDBG: (q) shift = "); cli_hexdump_rev(&shift, 2); @@ -810,12 +810,12 @@ void bigint_reduce(bigint_t* a, const bigint_t* r){ /* calculate dest = a**exp % r */ /* using square&multiply */ void bigint_expmod_u(bigint_t* dest, const bigint_t* a, const bigint_t* exp, const bigint_t* r){ - if(a->length_B==0 || r->length_B==0){ + if(a->length_W==0 || r->length_W==0){ return; } bigint_t res, base; - bigint_word_t t, base_b[MAX(a->length_B,r->length_B)], res_b[r->length_B*2]; + bigint_word_t t, base_b[MAX(a->length_W,r->length_W)], res_b[r->length_W*2]; uint16_t i; uint8_t j; // uint16_t *xaddr = &i; @@ -828,16 +828,16 @@ void bigint_expmod_u(bigint_t* dest, const bigint_t* a, const bigint_t* exp, con // cli_putstr("\r\npost-copy"); bigint_reduce(&base, r); res.wordv[0]=1; - res.length_B=1; + res.length_W=1; res.info = 0; bigint_adjust(&res); - if(exp->length_B == 0){ + if(exp->length_W == 0){ bigint_copy(dest, &res); return; } uint8_t flag = 0; - t=exp->wordv[exp->length_B - 1]; - for(i=exp->length_B; i > 0; --i){ + t=exp->wordv[exp->length_W - 1]; + for(i=exp->length_W; i > 0; --i){ t = exp->wordv[i - 1]; for(j=BIGINT_WORD_SIZE; j > 0; --j){ if(!flag){ @@ -873,14 +873,14 @@ void bigint_expmod_u(bigint_t* dest, const bigint_t* a, const bigint_t* exp, con void bigint_gcdext(bigint_t* gcd, bigint_t* a, bigint_t* b, const bigint_t* x, const bigint_t* y){ bigint_t g, x_, y_, u, v, a_, b_, c_, d_; uint16_t i=0; - if(x->length_B==0 || y->length_B==0){ + if(x->length_W==0 || y->length_W==0){ return; } - if(x->length_B==1 && x->wordv[0]==1){ - gcd->length_B = 1; + if(x->length_W==1 && x->wordv[0]==1){ + gcd->length_W = 1; gcd->wordv[0] = 1; if(a){ - a->length_B = 1; + a->length_W = 1; a->wordv[0] = 1; SET_POS(a); bigint_adjust(a); @@ -890,11 +890,11 @@ void bigint_gcdext(bigint_t* gcd, bigint_t* a, bigint_t* b, const bigint_t* x, c } return; } - if(y->length_B==1 && y->wordv[0]==1){ - gcd->length_B = 1; + if(y->length_W==1 && y->wordv[0]==1){ + gcd->length_W = 1; gcd->wordv[0] = 1; if(b){ - b->length_B = 1; + b->length_W = 1; b->wordv[0] = 1; SET_POS(b); bigint_adjust(b); @@ -908,23 +908,23 @@ void bigint_gcdext(bigint_t* gcd, bigint_t* a, bigint_t* b, const bigint_t* x, c while(x->wordv[i]==0 && y->wordv[i]==0){ ++i; } - bigint_word_t g_b[i+2], x_b[x->length_B-i], y_b[y->length_B-i]; - bigint_word_t u_b[x->length_B-i], v_b[y->length_B-i]; - bigint_word_t a_b[y->length_B+2], c_b[y->length_B+2]; - bigint_word_t b_b[x->length_B+2], d_b[x->length_B+2]; + bigint_word_t g_b[i+2], x_b[x->length_W-i], y_b[y->length_W-i]; + bigint_word_t u_b[x->length_W-i], v_b[y->length_W-i]; + bigint_word_t a_b[y->length_W+2], c_b[y->length_W+2]; + bigint_word_t b_b[x->length_W+2], d_b[x->length_W+2]; g.wordv = g_b; x_.wordv = x_b; y_.wordv = y_b; memset(g_b, 0, i*sizeof(bigint_word_t)); g_b[i]=1; - g.length_B = i+1; + g.length_W = i+1; g.info=0; x_.info = y_.info = 0; - x_.length_B = x->length_B-i; - y_.length_B = y->length_B-i; - memcpy(x_.wordv, x->wordv+i, x_.length_B*sizeof(bigint_word_t)); - memcpy(y_.wordv, y->wordv+i, y_.length_B*sizeof(bigint_word_t)); + x_.length_W = x->length_W-i; + y_.length_W = y->length_W-i; + memcpy(x_.wordv, x->wordv+i, x_.length_W*sizeof(bigint_word_t)); + memcpy(y_.wordv, y->wordv+i, y_.length_W*sizeof(bigint_word_t)); for(i=0; (x_.wordv[0]&(1<wordv); - q = p + a->length_B * sizeof(bigint_word_t) - 1; + q = p + a->length_W * sizeof(bigint_word_t) - 1; while(p void bigint_print_hex(const bigint_t* a){ - if(a->length_B==0){ + if(a->length_W==0){ cli_putc('0'); return; } @@ -32,18 +32,18 @@ void bigint_print_hex(const bigint_t* a){ cli_putc('-'); } // cli_putc((a->info&BIGINT_NEG_MASK)?'-':'+'); /* print sign */ -/* if(a->wordv[a->length_B-1]<0x10){ - cli_putc(hexdigit_tab_uc[a->wordv[a->length_B-1]]); - cli_hexdump_rev(a->wordv, a->length_B-1); +/* if(a->wordv[a->length_W-1]<0x10){ + cli_putc(hexdigit_tab_uc[a->wordv[a->length_W-1]]); + cli_hexdump_rev(a->wordv, a->length_W-1); } else { */ - // cli_hexdump_rev(a->wordv, a->length_B*sizeof(bigint_word_t)); + // cli_hexdump_rev(a->wordv, a->length_W*sizeof(bigint_word_t)); // } uint32_t idx; uint8_t print_zero=0; uint8_t *p,x,y; - p = (uint8_t*)&(a->wordv[a->length_B-1])+sizeof(bigint_word_t)-1; - for(idx = a->length_B * sizeof(bigint_word_t); idx > 0; --idx){ + p = (uint8_t*)&(a->wordv[a->length_W-1])+sizeof(bigint_word_t)-1; + for(idx = a->length_W * sizeof(bigint_word_t); idx > 0; --idx){ x = *p >> 4; y = *p & 0xf; if(x!=0 || print_zero!=0){ @@ -98,7 +98,7 @@ uint8_t bigint_read_hex_echo(bigint_t* a){ uint16_t allocated=0; uint8_t shift4=0; uint16_t t, idx = 0; - a->length_B = 0; + a->length_W = 0; a->wordv = NULL; a->info = 0; for(;;){ @@ -140,9 +140,9 @@ uint8_t bigint_read_hex_echo(bigint_t* a){ /* we have to reverse the byte array */ uint8_t tmp; uint8_t *p, *q; - a->length_B = (idx + sizeof(bigint_word_t)-1)/sizeof(bigint_word_t); + a->length_W = (idx + sizeof(bigint_word_t)-1)/sizeof(bigint_word_t); p = (uint8_t*)(a->wordv); - q = (uint8_t*)a->wordv + a->length_B * sizeof(bigint_word_t) - 1; + q = (uint8_t*)a->wordv + a->length_W * sizeof(bigint_word_t) - 1; while(q>p){ tmp = *p; *p = *q; diff --git a/dsa/dsa_key_blob.c b/dsa/dsa_key_blob.c index 4a7955f..f6e761c 100644 --- a/dsa/dsa_key_blob.c +++ b/dsa/dsa_key_blob.c @@ -280,15 +280,15 @@ void load_dsa_key_blob(dsa_ctx_t* ctx){ return; } memcpy_P(ctx->priv.wordv, dsa_key_blob, ALL_LEN_B); - ctx->priv.length_B=PRIV_LEN_B; + ctx->priv.length_W=PRIV_LEN_B; ctx->pub.wordv = ctx->priv.wordv+PRIV_LEN_B; - ctx->pub.length_B = PUB_LEN_B; + ctx->pub.length_W = PUB_LEN_B; ctx->domain.p.wordv = ctx->priv.wordv+PRIV_LEN_B+PUB_LEN_B; - ctx->domain.p.length_B = P_LEN_B; + ctx->domain.p.length_W = P_LEN_B; ctx->domain.q.wordv = ctx->priv.wordv+PRIV_LEN_B+PUB_LEN_B+P_LEN_B; - ctx->domain.q.length_B = Q_LEN_B; + ctx->domain.q.length_W = Q_LEN_B; ctx->domain.g.wordv = ctx->priv.wordv+PRIV_LEN_B+PUB_LEN_B+P_LEN_B+Q_LEN_B; - ctx->domain.g.length_B = G_LEN_B; + ctx->domain.g.length_W = G_LEN_B; bigint_changeendianess(&(ctx->priv)); bigint_changeendianess(&(ctx->pub)); diff --git a/dsa/dsa_sign.c b/dsa/dsa_sign.c index fab2197..7bd57e6 100644 --- a/dsa/dsa_sign.c +++ b/dsa/dsa_sign.c @@ -27,7 +27,7 @@ uint8_t dsa_sign_bigint(dsa_signature_t* s, const bigint_t* m, const dsa_ctx_t* ctx, const bigint_t* k){ bigint_t tmp, tmp2; - uint8_t tmp_b[ctx->domain.p.length_B+5], tmp2_b[ctx->domain.q.length_B+5]; + uint8_t tmp_b[ctx->domain.p.length_W+5], tmp2_b[ctx->domain.q.length_W+5]; tmp.wordv= tmp_b; tmp2.wordv = tmp2_b; bigint_expmod_u(&tmp, &(ctx->domain.g), k, &(ctx->domain.p)); @@ -40,7 +40,7 @@ uint8_t dsa_sign_bigint(dsa_signature_t* s, const bigint_t* m, bigint_reduce(&tmp, &(ctx->domain.q)); bigint_copy(&(s->s), &tmp); - if(s->s.length_B==0 || s->r.length_B==0){ + if(s->s.length_W==0 || s->r.length_W==0){ return 1; } @@ -51,20 +51,20 @@ uint8_t dsa_sign_message(dsa_signature_t* s, const void* m, uint16_t m_len_b, const hfdesc_t* hash_desc, const dsa_ctx_t* ctx, const rand_func_ptr_t rand_in){ bigint_t z, k; - uint8_t i, n_B = ctx->domain.q.length_B; + uint8_t i, n_B = ctx->domain.q.length_W; uint8_t hash_value[(n_B>(hfal_hash_getHashsize(hash_desc)+7)/8)?n_B:(hfal_hash_getHashsize(hash_desc)+7)/8]; uint8_t k_b[n_B]; hfal_hash_mem(hash_desc, hash_value, m, m_len_b); z.wordv = hash_value; - z.length_B = n_B; + z.length_W = n_B; bigint_changeendianess(&z); k.wordv = k_b; - k.length_B = n_B; + k.length_W = n_B; do{ for(i=0; ir.length_B==0 || s->s.length_B==0){ + if(s->r.length_W==0 || s->s.length_W==0){ return DSA_SIGNATURE_FAIL; } if(bigint_cmp_u(&(s->r), &(ctx->domain.q))>=0 || bigint_cmp_u(&(s->s), &(ctx->domain.q))>=0){ return DSA_SIGNATURE_FAIL; } bigint_t w, u1, u2, v1, v2; - uint8_t w_b[ctx->domain.q.length_B], u1_b[ctx->domain.q.length_B*2], u2_b[ctx->domain.q.length_B*2]; - uint8_t v1_b[ctx->domain.p.length_B*2], v2_b[ctx->domain.p.length_B]; + uint8_t w_b[ctx->domain.q.length_W], u1_b[ctx->domain.q.length_W*2], u2_b[ctx->domain.q.length_W*2]; + uint8_t v1_b[ctx->domain.p.length_W*2], v2_b[ctx->domain.p.length_W]; w.wordv = w_b; u1.wordv = u1_b; u2.wordv = u2_b; @@ -57,11 +57,11 @@ uint8_t dsa_verify_bigint(const dsa_signature_t* s, const bigint_t* m, uint8_t dsa_verify_message(const dsa_signature_t* s, const void* m, uint16_t m_len_b, const hfdesc_t* hash_desc, const dsa_ctx_t* ctx){ bigint_t z; - uint8_t n_B = ctx->domain.q.length_B; + uint8_t n_B = ctx->domain.q.length_W; uint8_t hash_value[(hfal_hash_getHashsize(hash_desc)+7)/8]; hfal_hash_mem(hash_desc, hash_value, m, m_len_b); z.wordv=hash_value; - z.length_B=n_B; + z.length_W=n_B; bigint_changeendianess(&z); bigint_adjust(&z); return dsa_verify_bigint(s, &z, ctx); diff --git a/rsa/rsa_basic.c b/rsa/rsa_basic.c index 49e0919..fbdc80b 100644 --- a/rsa/rsa_basic.c +++ b/rsa/rsa_basic.c @@ -52,8 +52,8 @@ m = m2 + q * h uint8_t rsa_dec_crt_mono(bigint_t* data, const rsa_privatekey_t* key){ bigint_t m1, m2; - m1.wordv = malloc((key->components[0].length_B /* + 1 */) * sizeof(bigint_word_t)); - m2.wordv = malloc((key->components[1].length_B /* + 1 */) * sizeof(bigint_word_t)); + m1.wordv = malloc((key->components[0].length_W /* + 1 */) * sizeof(bigint_word_t)); + m2.wordv = malloc((key->components[1].length_W /* + 1 */) * sizeof(bigint_word_t)); if(!m1.wordv || !m2.wordv){ #if DEBUG cli_putstr_P(PSTR("\r\nERROR: OOM!")); @@ -183,7 +183,7 @@ void rsa_os2ip(bigint_t* dest, const void* data, uint32_t length_B){ if(data){ memcpy(dest->wordv, data, length_B); } - dest->length_B = length_B; + dest->length_W = length_B; #else uint8_t off; off = (sizeof(bigint_word_t) - length_B % sizeof(bigint_word_t)) % sizeof(bigint_word_t); @@ -203,10 +203,10 @@ void rsa_os2ip(bigint_t* dest, const void* data, uint32_t length_B){ memset(dest->wordv, 0, off); } } - dest->length_B = (length_B + off) / sizeof(bigint_word_t); + dest->length_W = (length_B + off) / sizeof(bigint_word_t); #if DEBUG - cli_putstr_P(PSTR("\r\nDBG: dest->length_B = 0x")); - cli_hexdump_rev(&(dest->length_B), 2); + cli_putstr_P(PSTR("\r\nDBG: dest->length_W = 0x")); + cli_hexdump_rev(&(dest->length_W), 2); #endif #endif dest->info = 0; @@ -217,9 +217,9 @@ void rsa_os2ip(bigint_t* dest, const void* data, uint32_t length_B){ void rsa_i2osp(void* dest, bigint_t* src, uint16_t* out_length_B){ #if BIGINT_WORD_SIZE == 8 if(dest){ - uint8_t *e = src->wordv + src->length_B; + uint8_t *e = src->wordv + src->length_W; uint16_t i; - for(i=src->length_B; i>0; --i){ + for(i=src->length_W; i>0; --i){ *((uint8_t*)dest) = *--e; dest = (uint8_t*)dest + 1; } @@ -227,7 +227,7 @@ void rsa_i2osp(void* dest, bigint_t* src, uint16_t* out_length_B){ bigint_changeendianess(src); } - *out_length_B = src->length_B; + *out_length_B = src->length_W; #else *out_length_B = bigint_get_first_set_bit(src) / 8 + 1; if(dest){ diff --git a/rsa/rsaes_oaep.c b/rsa/rsaes_oaep.c index 3057d9a..c1c70e7 100644 --- a/rsa/rsaes_oaep.c +++ b/rsa/rsaes_oaep.c @@ -72,7 +72,7 @@ uint8_t rsa_encrypt_oaep(void* dest, uint16_t* out_length, cli_putstr("\r\n buffer_len = "); cli_hexdump_rev(&buffer_len, 2); cli_putstr("\r\n modulus_len = "); - cli_hexdump_rev(&key->modulus.length_B, 2); + cli_hexdump_rev(&key->modulus.length_W, 2); #endif uint8_t* buffer = (uint8_t*)dest; uint8_t off; @@ -121,7 +121,7 @@ uint8_t rsa_encrypt_oaep(void* dest, uint16_t* out_length, cli_hexdump_block(dest, bigint_length_B(&key->modulus), 4, 16); #endif x.info = 0; - x.length_B = key->modulus.length_B; + x.length_W = key->modulus.length_W; x.wordv = dest; bigint_adjust(&x); rsa_os2ip(&x, NULL, bigint_length_B(&key->modulus)); @@ -170,7 +170,7 @@ uint8_t rsa_decrypt_oaep(void* dest, uint16_t* out_length, // cli_putc('a'); uart_flush(0); x.wordv = dest; - x.length_B = key->modulus.length_B; + x.length_W = key->modulus.length_W; x.info = 0; bigint_adjust(&x); diff --git a/rsa/rsaes_pkcs1v15.c b/rsa/rsaes_pkcs1v15.c index ac7e87f..2bc35ac 100644 --- a/rsa/rsaes_pkcs1v15.c +++ b/rsa/rsaes_pkcs1v15.c @@ -73,10 +73,10 @@ uint8_t rsa_encrypt_pkcs1v15(void* dest, uint16_t* out_length, const void* src, ((uint8_t*)dest)[2+pad_length] = 0x00; memcpy((uint8_t*)dest+3+pad_length, src, length_B); x.wordv = dest; - x.length_B = (length_B+pad_length+3+sizeof(bigint_word_t)-1)/sizeof(bigint_word_t); + x.length_W = (length_B+pad_length+3+sizeof(bigint_word_t)-1)/sizeof(bigint_word_t); #if DEBUG cli_putstr_P(PSTR("\r\nx-data: ")); - cli_hexdump_block(x.wordv, x.length_B * sizeof(bigint_word_t), 4, 16); + cli_hexdump_block(x.wordv, x.length_W * sizeof(bigint_word_t), 4, 16); #endif bigint_adjust(&x); rsa_os2ip(&x, NULL, length_B+pad_length+3); diff --git a/test_src/main-bigint-test.c b/test_src/main-bigint-test.c index 00baffc..b757f28 100644 --- a/test_src/main-bigint-test.c +++ b/test_src/main-bigint-test.c @@ -72,7 +72,7 @@ void test_add_bigint(void){ bigint_print_hex(&b); cli_putstr_P(PSTR(" = ")); uint8_t *c_b; - c_b = malloc(((a.length_B>b.length_B)?a.length_B:b.length_B)+2); + c_b = malloc(((a.length_W>b.length_W)?a.length_W:b.length_W)+2); if(c_b==NULL){ cli_putstr_P(PSTR("\n\rERROR: Out of memory!")); free(a.wordv); @@ -118,7 +118,7 @@ void test_add_scale_bigint(void){ } */ uint8_t *c_b; - c_b = malloc(((a.length_B>(b.length_B+scale))?a.length_B:(b.length_B+scale))+2); + c_b = malloc(((a.length_W>(b.length_W+scale))?a.length_W:(b.length_W+scale))+2); if(c_b==NULL){ cli_putstr_P(PSTR("\n\rERROR: Out of memory!")); free(a.wordv); @@ -164,7 +164,7 @@ void test_mul_bigint(void){ bigint_print_hex(&b); cli_putstr_P(PSTR(" = ")); uint8_t *c_b; - c_b = malloc((((a.length_B>b.length_B)?a.length_B:b.length_B)+1)*2); + c_b = malloc((((a.length_W>b.length_W)?a.length_W:b.length_W)+1)*2); if(c_b==NULL){ cli_putstr_P(PSTR("\n\rERROR: Out of memory!")); free(a.wordv); @@ -194,7 +194,7 @@ void test_square_bigint(void){ bigint_print_hex(&a); cli_putstr_P(PSTR("**2 = ")); uint8_t *c_b; - c_b = malloc(a.length_B*2); + c_b = malloc(a.length_W*2); if(c_b==NULL){ cli_putstr_P(PSTR("\n\rERROR: Out of memory!")); free(a.wordv); @@ -260,7 +260,7 @@ void test_expmod_bigint(void){ cli_putstr_P(PSTR("\r\n end expmod test")); return; } - d_b = malloc(c.length_B); + d_b = malloc(c.length_W); if(d_b==NULL){ cli_putstr_P(PSTR("\n\rERROR: Out of memory!")); free(a.wordv); @@ -302,9 +302,9 @@ void test_gcdext_bigint(void){ cli_putstr_P(PSTR("\r\n end gcdext test")); return; } - c.wordv = malloc((a.length_Bb.length_B)?a.length_B:b.length_B); - e.wordv = malloc(1+(a.length_B>b.length_B)?a.length_B:b.length_B); + c.wordv = malloc((a.length_Wb.length_W)?a.length_W:b.length_W); + e.wordv = malloc(1+(a.length_W>b.length_W)?a.length_W:b.length_W); cli_putstr_P(PSTR("\r\n gcdext( ")); bigint_print_hex(&a); @@ -334,8 +334,8 @@ void test_simple(void){ a.wordv=a_b; b.wordv=b_b; c.wordv=c_b; - a.length_B = 1; - b.length_B = 1; + a.length_W = 1; + b.length_W = 1; a_b[0] = 1; b_b[0] = 2; bigint_add_u(&c, &a, &b); @@ -351,8 +351,8 @@ void test_mul_simple(void){ a.wordv=a_b; b.wordv=b_b; c.wordv=c_b; - a.length_B = 5; - b.length_B = 5; + a.length_W = 5; + b.length_W = 5; bigint_adjust(&a); bigint_adjust(&b); bigint_mul_s(&c, &a, &b); @@ -376,8 +376,8 @@ void test_mul_simple(void){ a.wordv=a_b; b.wordv=b_b; c.wordv=c_b; - a.length_B = 8; - b.length_B = 8; + a.length_W = 8; + b.length_W = 8; a.info=0x80; bigint_adjust(&a); bigint_adjust(&b); @@ -418,7 +418,7 @@ void test_square_simple(void){ uint8_t c_b[22]; a.wordv=a_b; c.wordv=c_b; - a.length_B = 11; + a.length_W = 11; a.info=0x00; bigint_adjust(&a); bigint_square(&c, &a); @@ -436,11 +436,11 @@ void test_reduce_simple(void){ uint8_t b_b[2] = {0x52, 0x27}; uint8_t c_b[2]; a.wordv=a_b; - a.length_B = 2; + a.length_W = 2; a.info=0x00; bigint_adjust(&a); b.wordv=b_b; - b.length_B = 2; + b.length_W = 2; b.info=0x00; bigint_adjust(&b); c.wordv = c_b; @@ -465,11 +465,11 @@ void test_gcdext_simple(void){ uint8_t b_b[5] = {0x72, 0x7D, 0x57, 0xAC, 0X6F}; uint8_t c_b[6], d_b[6], e_b[6]; a.wordv=a_b; - a.length_B = 5; + a.length_W = 5; a.info=0x00; bigint_adjust(&a); b.wordv=b_b; - b.length_B = 5; + b.length_W = 5; b.info=0x00; bigint_adjust(&b); c.wordv = c_b; diff --git a/test_src/main-dsa-test.c b/test_src/main-dsa-test.c index 7516341..f3710c0 100644 --- a/test_src/main-dsa-test.c +++ b/test_src/main-dsa-test.c @@ -53,8 +53,8 @@ void dsa_print_item(bigint_t* a, PGM_P pstr){ cli_putstr_P(pstr); cli_putstr_P(PSTR(": ")); uint16_t i; - p = a->wordv + a->length_B -1; - for(i=0; ilength_B-1; ++i){ + p = a->wordv + a->length_W -1; + for(i=0; ilength_W-1; ++i){ if(i%16==0){ cli_putstr_P(PSTR("\r\n ")); } @@ -70,8 +70,8 @@ void dsa_print_item(bigint_t* a, PGM_P pstr){ void dsa_print_signature_b64(dsa_signature_t* s){ uint16_t size_r, size_s, size_o, i,j; - size_r = s->r.length_B +2; - size_s = s->s.length_B +2; + size_r = s->r.length_W +2; + size_s = s->s.length_W +2; size_o = size_r + size_s +2; uint8_t bin_b[size_o]; bin_b[0] = 0x30; @@ -79,12 +79,12 @@ void dsa_print_signature_b64(dsa_signature_t* s){ bin_b[2] = 0x02; bin_b[3] = size_r-2; j=4; - for(i=s->r.length_B; i>0; --i){ + for(i=s->r.length_W; i>0; --i){ bin_b[j++] = s->r.wordv[i-1]; } bin_b[j++] = 0x02; bin_b[j++] = size_s -2; - for(i=s->s.length_B; i>0; --i){ + for(i=s->s.length_W; i>0; --i){ bin_b[j++] = s->s.wordv[i-1]; } char b64_b[size_o*4/3+5]; @@ -111,8 +111,8 @@ void quick_test(void){ dsa_signature_t dsa_sig; uint8_t i, t=0, message[] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef}; load_fix_dsa(); - uint8_t dsa_sig_s_b[dsa_ctx.domain.q.length_B], - dsa_sig_r_b[dsa_ctx.domain.q.length_B]; + uint8_t dsa_sig_s_b[dsa_ctx.domain.q.length_W], + dsa_sig_r_b[dsa_ctx.domain.q.length_W]; dsa_print_ctx(&dsa_ctx); dsa_sig.r.wordv = dsa_sig_r_b; dsa_sig.s.wordv = dsa_sig_s_b; diff --git a/test_src/main-rsaes_oaep-test.c b/test_src/main-rsaes_oaep-test.c index 53a6c22..5f86ec2 100644 --- a/test_src/main-rsaes_oaep-test.c +++ b/test_src/main-rsaes_oaep-test.c @@ -422,7 +422,7 @@ uint8_t read_bigint(bigint_t* a, char* prompt){ return 1; } a->wordv = (bigint_word_t*)buffer; - a->length_B = (read_length + sizeof(bigint_word_t) - 1) / sizeof(bigint_word_t); + a->length_W = (read_length + sizeof(bigint_word_t) - 1) / sizeof(bigint_word_t); a->info = 0; bigint_changeendianess(a); bigint_adjust(a); @@ -491,7 +491,7 @@ uint8_t read_key_conv(void){ } uint8_t load_bigint_from_os(bigint_t* a, PGM_VOID_P os, uint16_t length_B){ - a->length_B = BIGINT_CEIL(length_B) / sizeof(bigint_word_t); + a->length_W = BIGINT_CEIL(length_B) / sizeof(bigint_word_t); a->wordv = malloc(BIGINT_CEIL(length_B)); if(!a->wordv){ cli_putstr_P(PSTR("\r\nOOM!\r\n")); @@ -534,8 +534,8 @@ void quick_test(void){ if(!keys_allocated){ load_fix_rsa(); } - ciphertext = malloc(clen = pub_key.modulus.length_B * sizeof(bigint_word_t)); - plaintext = malloc(pub_key.modulus.length_B * sizeof(bigint_word_t)); + ciphertext = malloc(clen = pub_key.modulus.length_W * sizeof(bigint_word_t)); + plaintext = malloc(pub_key.modulus.length_W * sizeof(bigint_word_t)); memcpy_P(plaintext, MSG, sizeof(MSG)); memcpy_P(seed, SEED, sizeof(SEED)); cli_putstr_P(PSTR("\r\nplaintext:")); diff --git a/test_src/main-rsaes_pkcs1v15-test.c b/test_src/main-rsaes_pkcs1v15-test.c index 00f12ed..268c120 100644 --- a/test_src/main-rsaes_pkcs1v15-test.c +++ b/test_src/main-rsaes_pkcs1v15-test.c @@ -240,7 +240,7 @@ uint8_t read_bigint(bigint_t* a, char* prompt){ return 1; } a->wordv = (bigint_word_t*)buffer; - a->length_B = (read_length + sizeof(bigint_word_t) - 1) / sizeof(bigint_word_t); + a->length_W = (read_length + sizeof(bigint_word_t) - 1) / sizeof(bigint_word_t); a->info = 0; bigint_changeendianess(a); bigint_adjust(a); @@ -309,7 +309,7 @@ uint8_t read_key_conv(void){ } uint8_t load_bigint_from_os(bigint_t* a, PGM_VOID_P os, uint16_t length_B){ - a->length_B = BIGINT_CEIL(length_B) / sizeof(bigint_word_t); + a->length_W = BIGINT_CEIL(length_B) / sizeof(bigint_word_t); a->wordv = malloc(BIGINT_CEIL(length_B)); if(!a->wordv){ cli_putstr_P(PSTR("\r\nOOM!\r\n")); @@ -474,8 +474,8 @@ void load_priv_conventional(void){ cli_putstr_P(PSTR("\r\nERROR: OOM!")); return; } - epriv->length_B = (sizeof(PRIV_EXPONENT) + sizeof(bigint_word_t) - 1) / sizeof(bigint_word_t); - epriv->wordv = malloc(epriv->length_B * sizeof(bigint_word_t)); + epriv->length_W = (sizeof(PRIV_EXPONENT) + sizeof(bigint_word_t) - 1) / sizeof(bigint_word_t); + epriv->wordv = malloc(epriv->length_W * sizeof(bigint_word_t)); if(!epriv->wordv){ cli_putstr_P(PSTR("\r\nERROR: OOM!")); return; @@ -508,8 +508,8 @@ void load_priv_crt_mono(void){ for(i=0; i<5; ++i){ v[i] = malloc(sizeof(bigint_t)); v[i]->info = 0; - v[i]->length_B = (sv[i] + sizeof(bigint_word_t) - 1) / sizeof(bigint_word_t); - v[i]->wordv = calloc(v[i]->length_B , sizeof(bigint_word_t)); + v[i]->length_W = (sv[i] + sizeof(bigint_word_t) - 1) / sizeof(bigint_word_t); + v[i]->wordv = calloc(v[i]->length_W , sizeof(bigint_word_t)); if(!v[i]->wordv){ cli_putstr_P(PSTR("\r\nERROR: OOM!")); return; @@ -522,7 +522,7 @@ void load_priv_crt_mono(void){ } uint8_t load_bigint_from_os(bigint_t* a, PGM_VOID_P os, uint16_t length_B){ - a->length_B = BIGINT_CEIL(length_B) / sizeof(bigint_word_t); + a->length_W = BIGINT_CEIL(length_B) / sizeof(bigint_word_t); a->wordv = malloc(BIGINT_CEIL(length_B)); if(!a->wordv){ cli_putstr_P(PSTR("\r\nOOM!\r\n"));