changing length_B to length_W to reflect earlier changes

This commit is contained in:
bg 2012-09-07 23:34:12 +02:00
parent cd6cc49401
commit fdbda6486d
14 changed files with 314 additions and 314 deletions

View File

@ -59,8 +59,8 @@
/******************************************************************************/ /******************************************************************************/
/* /*
void bigint_copy(bigint_t* dest, const bigint_t* src){ void bigint_copy(bigint_t* dest, const bigint_t* src){
memcpy(dest->wordv, src->wordv, src->length_B); memcpy(dest->wordv, src->wordv, src->length_W);
dest->length_B = src->length_B; dest->length_W = src->length_W;
dest->info = src->info; 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){ void bigint_add_u(bigint_t* dest, const bigint_t* a, const bigint_t* b){
uint16_t t=0, i; uint16_t t=0, i;
if(a->length_B < b->length_B){ if(a->length_W < b->length_W){
XCHG_PTR(a,b); XCHG_PTR(a,b);
} }
for(i=0; i<b->length_B; ++i){ for(i=0; i<b->length_W; ++i){
t = a->wordv[i] + b->wordv[i] + t; t = a->wordv[i] + b->wordv[i] + t;
dest->wordv[i] = (uint8_t)t; dest->wordv[i] = (uint8_t)t;
t>>=8; t>>=8;
} }
for(; i<a->length_B; ++i){ for(; i<a->length_W; ++i){
t = a->wordv[i] + t; t = a->wordv[i] + t;
dest->wordv[i] = (uint8_t)t; dest->wordv[i] = (uint8_t)t;
t>>=8; t>>=8;
} }
dest->wordv[i++] = t; dest->wordv[i++] = t;
dest->length_B = i; dest->length_W = i;
bigint_adjust(dest); 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){ void bigint_add_scale_u(bigint_t* dest, const bigint_t* a, uint16_t scale){
uint16_t i,j=0; uint16_t i,j=0;
uint16_t t=0; uint16_t t=0;
if(scale>dest->length_B) if(scale>dest->length_W)
memset(dest->wordv+dest->length_B, 0, scale-dest->length_B); memset(dest->wordv+dest->length_W, 0, scale-dest->length_W);
for(i=scale; i<a->length_B+scale; ++i,++j){ for(i=scale; i<a->length_W+scale; ++i,++j){
t = a->wordv[j] + t; t = a->wordv[j] + t;
if(dest->length_B>i){ if(dest->length_W>i){
t += dest->wordv[i]; t += dest->wordv[i];
} }
dest->wordv[i] = (uint8_t)t; dest->wordv[i] = (uint8_t)t;
t>>=8; t>>=8;
} }
while(t){ while(t){
if(dest->length_B>i){ if(dest->length_W>i){
t = dest->wordv[i] + t; t = dest->wordv[i] + t;
} }
dest->wordv[i] = (uint8_t)t; dest->wordv[i] = (uint8_t)t;
t>>=8; t>>=8;
++i; ++i;
} }
if(dest->length_B < i){ if(dest->length_W < i){
dest->length_B = i; dest->length_W = i;
} }
bigint_adjust(dest); 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; int8_t r;
int16_t t; int16_t t;
uint16_t i, min, max; uint16_t i, min, max;
min = MIN(a->length_B, b->length_B); min = MIN(a->length_W, b->length_W);
max = MAX(a->length_B, b->length_B); max = MAX(a->length_W, b->length_W);
r = bigint_cmp_u(a,b); r = bigint_cmp_u(a,b);
if(r==0){ if(r==0){
dest->length_B = 0; dest->length_W = 0;
dest->wordv[0] = 0; dest->wordv[0] = 0;
bigint_adjust(dest); bigint_adjust(dest);
return; return;
} }
if(b->length_B==0){ if(b->length_W==0){
dest->length_B = a->length_B; dest->length_W = a->length_W;
memcpy(dest->wordv, a->wordv, a->length_B); memcpy(dest->wordv, a->wordv, a->length_W);
dest->info = a->info; dest->info = a->info;
SET_POS(dest); SET_POS(dest);
return; return;
} }
if(a->length_B==0){ if(a->length_W==0){
dest->length_B = b->length_B; dest->length_W = b->length_W;
memcpy(dest->wordv, b->wordv, b->length_B); memcpy(dest->wordv, b->wordv, b->length_W);
dest->info = b->info; dest->info = b->info;
SET_NEG(dest); SET_NEG(dest);
return; return;
@ -176,7 +176,7 @@ void bigint_sub_u(bigint_t* dest, const bigint_t* a, const bigint_t* b){
} }
SET_POS(dest); SET_POS(dest);
dest->length_B = i; dest->length_W = i;
bigint_adjust(dest); 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){ 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; return 1;
} }
if(a->length_B < b->length_B){ if(a->length_W < b->length_W){
return -1; return -1;
} }
if(a->length_B==0){ if(a->length_W==0){
return 0; return 0;
} }
uint16_t i; uint16_t i;
i = a->length_B-1; i = a->length_W-1;
do{ do{
if(a->wordv[i]!=b->wordv[i]){ if(a->wordv[i]!=b->wordv[i]){
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){ int8_t bigint_cmp_s(const bigint_t* a, const bigint_t* b){
uint8_t s; uint8_t s;
if(a->length_B==0 && b->length_B==0){ if(a->length_W==0 && b->length_W==0){
return 0; return 0;
} }
s = GET_SIGN(a)?2:0; s = GET_SIGN(a)?2:0;
@ -297,11 +297,11 @@ void bigint_shiftleft(bigint_t* a, uint16_t shift){
uint16_t t=0; uint16_t t=0;
byteshift = (shift+3)/8; byteshift = (shift+3)/8;
bitshift = shift&7; 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); memset(a->wordv, 0, byteshift);
if(bitshift!=0){ if(bitshift!=0){
if(bitshift<=4){ /* shift to the left */ if(bitshift<=4){ /* shift to the left */
for(i=byteshift; i<a->length_B+byteshift; ++i){ for(i=byteshift; i<a->length_W+byteshift; ++i){
t |= (a->wordv[i])<<bitshift; t |= (a->wordv[i])<<bitshift;
a->wordv[i] = (uint8_t)t; a->wordv[i] = (uint8_t)t;
t >>= 8; t >>= 8;
@ -309,7 +309,7 @@ void bigint_shiftleft(bigint_t* a, uint16_t shift){
a->wordv[i] = (uint8_t)t; a->wordv[i] = (uint8_t)t;
byteshift++; byteshift++;
}else{ /* shift to the right */ }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); t |= (a->wordv[i])<<(bitshift);
a->wordv[i] = (uint8_t)(t>>8); a->wordv[i] = (uint8_t)(t>>8);
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->wordv[i] = (uint8_t)(t>>8);
} }
} }
a->length_B += byteshift; a->length_W += byteshift;
bigint_adjust(a); bigint_adjust(a);
} }
@ -331,21 +331,21 @@ void bigint_shiftright(bigint_t* a, uint16_t shift){
uint16_t t=0; uint16_t t=0;
byteshift = shift/8; byteshift = shift/8;
bitshift = shift&7; 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); bigint_set_zero(a);
return; 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); bigint_set_zero(a);
return; return;
} }
if(byteshift){ if(byteshift){
memmove(a->wordv, a->wordv+byteshift, a->length_B-byteshift); memmove(a->wordv, a->wordv+byteshift, a->length_W-byteshift);
memset(a->wordv+a->length_B-byteshift, 0, byteshift); memset(a->wordv+a->length_W-byteshift, 0, byteshift);
} }
if(bitshift!=0){ if(bitshift!=0){
/* shift to the right */ /* 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); t |= (a->wordv[i])<<(8-bitshift);
a->wordv[i] = (uint8_t)(t>>8); a->wordv[i] = (uint8_t)(t>>8);
t <<= 8; t <<= 8;
@ -353,7 +353,7 @@ void bigint_shiftright(bigint_t* a, uint16_t shift){
t |= (a->wordv[0])<<(8-bitshift); t |= (a->wordv[0])<<(8-bitshift);
a->wordv[0] = (uint8_t)(t>>8); a->wordv[0] = (uint8_t)(t>>8);
} }
a->length_B -= byteshift; a->length_W -= byteshift;
bigint_adjust(a); 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){ void bigint_xor(bigint_t* dest, const bigint_t* a){
uint16_t i; uint16_t i;
for(i=0; i<a->length_B; ++i){ for(i=0; i<a->length_W; ++i){
dest->wordv[i] ^= a->wordv[i]; dest->wordv[i] ^= a->wordv[i];
} }
bigint_adjust(dest); bigint_adjust(dest);
@ -370,7 +370,7 @@ void bigint_xor(bigint_t* dest, const bigint_t* a){
/******************************************************************************/ /******************************************************************************/
void bigint_set_zero(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 */ /* using the Karatsuba-Algorithm */
/* x*y = (xh*yh)*b**2n + ((xh+xl)*(yh+yl) - xh*yh - xl*yl)*b**n + yh*yl */ /* 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){ 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); bigint_set_zero(dest);
return; return;
} }
if(dest==a || dest==b){ if(dest==a || dest==b){
bigint_t d; 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; d.wordv = d_b;
bigint_mul_u(&d, a, b); bigint_mul_u(&d, a, b);
bigint_copy(dest, &d); bigint_copy(dest, &d);
return; return;
} }
if(a->length_B==1 || b->length_B==1){ if(a->length_W==1 || b->length_W==1){
if(a->length_B!=1){ if(a->length_W!=1){
XCHG_PTR(a,b); XCHG_PTR(a,b);
} }
uint16_t i, t=0; uint16_t i, t=0;
uint8_t x = a->wordv[0]; uint8_t x = a->wordv[0];
for(i=0; i<b->length_B; ++i){ for(i=0; i<b->length_W; ++i){
t += b->wordv[i]*x; t += b->wordv[i]*x;
dest->wordv[i] = (uint8_t)t; dest->wordv[i] = (uint8_t)t;
t>>=8; t>>=8;
} }
dest->wordv[i] = (uint8_t)t; dest->wordv[i] = (uint8_t)t;
dest->length_B=i+1; dest->length_W=i+1;
bigint_adjust(dest); bigint_adjust(dest);
return; 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; uint32_t p=0, q=0;
uint64_t r; uint64_t r;
memcpy(&p, a->wordv, a->length_B); memcpy(&p, a->wordv, a->length_W);
memcpy(&q, b->wordv, b->length_B); memcpy(&q, b->wordv, b->length_W);
r = (uint64_t)p*(uint64_t)q; r = (uint64_t)p*(uint64_t)q;
memcpy(dest->wordv, &r, a->length_B+b->length_B); memcpy(dest->wordv, &r, a->length_W+b->length_W);
dest->length_B = a->length_B+b->length_B; dest->length_W = a->length_W+b->length_W;
bigint_adjust(dest); bigint_adjust(dest);
return; return;
} }
bigint_set_zero(dest); bigint_set_zero(dest);
/* split a in xh & xl; split b in yh & yl */ /* split a in xh & xl; split b in yh & yl */
uint16_t n; 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; bigint_t xl, xh, yl, yh;
xl.wordv = a->wordv; xl.wordv = a->wordv;
yl.wordv = b->wordv; yl.wordv = b->wordv;
if(a->length_B<=n){ if(a->length_W<=n){
xh.info=0; xh.info=0;
xh.length_B = 0; xh.length_W = 0;
xl.length_B = a->length_B; xl.length_W = a->length_W;
xl.info = 0; xl.info = 0;
}else{ }else{
xl.length_B=n; xl.length_W=n;
xl.info = 0; xl.info = 0;
bigint_adjust(&xl); bigint_adjust(&xl);
xh.wordv = a->wordv+n; xh.wordv = a->wordv+n;
xh.length_B = a->length_B-n; xh.length_W = a->length_W-n;
xh.info = 0; xh.info = 0;
} }
if(b->length_B<=n){ if(b->length_W<=n){
yh.info=0; yh.info=0;
yh.length_B = 0; yh.length_W = 0;
yl.length_B = b->length_B; yl.length_W = b->length_W;
yl.info = b->info; yl.info = b->info;
}else{ }else{
yl.length_B=n; yl.length_W=n;
yl.info = 0; yl.info = 0;
bigint_adjust(&yl); bigint_adjust(&yl);
yh.wordv = b->wordv+n; yh.wordv = b->wordv+n;
yh.length_B = b->length_B-n; yh.length_W = b->length_W-n;
yh.info = 0; yh.info = 0;
} }
/* now we have split up a and b */ /* 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 */ /* square */
/* (xh*b^n+xl)^2 = xh^2*b^2n + 2*xh*xl*b^n + xl^2 */ /* (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){ void bigint_square(bigint_t* dest, const bigint_t* a){
if(a->length_B<=4){ if(a->length_W<=4){
uint64_t r=0; uint64_t r=0;
memcpy(&r, a->wordv, a->length_B); memcpy(&r, a->wordv, a->length_W);
r = r*r; r = r*r;
memcpy(dest->wordv, &r, 2*a->length_B); memcpy(dest->wordv, &r, 2*a->length_W);
SET_POS(dest); SET_POS(dest);
dest->length_B=2*a->length_B; dest->length_W=2*a->length_W;
bigint_adjust(dest); bigint_adjust(dest);
return; return;
} }
if(dest==a){ if(dest==a){
bigint_t d; bigint_t d;
uint8_t d_b[a->length_B*2]; uint8_t d_b[a->length_W*2];
d.wordv = d_b; d.wordv = d_b;
bigint_square(&d, a); bigint_square(&d, a);
bigint_copy(dest, &d); bigint_copy(dest, &d);
return; return;
} }
uint16_t n; 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_t xh, xl, tmp; /* x-high, x-low, temp */
uint8_t buffer[2*n+1]; uint8_t buffer[2*n+1];
xl.wordv = a->wordv; xl.wordv = a->wordv;
xl.length_B = n; xl.length_W = n;
xh.wordv = a->wordv+n; xh.wordv = a->wordv+n;
xh.length_B = a->length_B-n; xh.length_W = a->length_W-n;
tmp.wordv = buffer; tmp.wordv = buffer;
bigint_square(dest, &xl); bigint_square(dest, &xl);
bigint_square(&tmp, &xh); 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){ void bigint_sub_u_bitscale(bigint_t* a, const bigint_t* b, uint16_t bitscale){
bigint_t tmp; 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; uint16_t i,j,byteshift=bitscale/8;
uint8_t borrow=0; uint8_t borrow=0;
int16_t t; int16_t t;
if(a->length_B < b->length_B+byteshift){ if(a->length_W < b->length_W+byteshift){
bigint_set_zero(a); bigint_set_zero(a);
return; 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_copy(&tmp, b);
bigint_shiftleft(&tmp, bitscale&7); bigint_shiftleft(&tmp, bitscale&7);
for(j=0,i=byteshift; i<tmp.length_B+byteshift; ++i, ++j){ for(j=0,i=byteshift; i<tmp.length_W+byteshift; ++i, ++j){
t = a->wordv[i] - tmp.wordv[j] - borrow; t = a->wordv[i] - tmp.wordv[j] - borrow;
a->wordv[i] = (uint8_t)t; a->wordv[i] = (uint8_t)t;
if(t<0){ if(t<0){
@ -564,7 +564,7 @@ void bigint_sub_u_bitscale(bigint_t* a, const bigint_t* b, uint16_t bitscale){
} }
} }
while(borrow){ while(borrow){
if(i+1 > a->length_B){ if(i+1 > a->length_W){
bigint_set_zero(a); bigint_set_zero(a);
return; return;
} }
@ -583,13 +583,13 @@ void bigint_reduce(bigint_t* a, const bigint_t* r){
// bigint_adjust(r); // bigint_adjust(r);
uint8_t rfbs = GET_FBS(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; return;
} }
while(a->length_B > r->length_B){ while(a->length_W > r->length_W){
bigint_sub_u_bitscale(a, r, (a->length_B-r->length_B)*8+GET_FBS(a)-rfbs-1); 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); bigint_sub_u_bitscale(a, r, GET_FBS(a)-rfbs-1);
} }
while(bigint_cmp_u(a,r)>=0){ 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 */ /* calculate dest = a**exp % r */
/* using square&multiply */ /* using square&multiply */
void bigint_expmod_u(bigint_t* dest, const bigint_t* a, const bigint_t* exp, const bigint_t* r){ 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; return;
} }
bigint_t res, base; 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; uint16_t i;
uint8_t j, t; uint8_t j, t;
res.wordv = res_b; 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_copy(&base, a);
bigint_reduce(&base, r); bigint_reduce(&base, r);
res.wordv[0]=1; res.wordv[0]=1;
res.length_B=1; res.length_W=1;
res.info = 0; res.info = 0;
bigint_adjust(&res); bigint_adjust(&res);
for(i=0; i+1<exp->length_B; ++i){ for(i=0; i+1<exp->length_W; ++i){
t=exp->wordv[i]; t=exp->wordv[i];
for(j=0; j<8; ++j){ for(j=0; j<8; ++j){
if(t&1){ 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){ 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_; bigint_t g, x_, y_, u, v, a_, b_, c_, d_;
volatile uint16_t i=0; volatile uint16_t i=0;
if(x->length_B==0 || y->length_B==0){ if(x->length_W==0 || y->length_W==0){
return; return;
} }
while(x->wordv[i]==0 && y->wordv[i]==0){ while(x->wordv[i]==0 && y->wordv[i]==0){
++i; ++i;
} }
uint8_t g_b[i+2], x_b[x->length_B-i], y_b[y->length_B-i]; uint8_t g_b[i+2], x_b[x->length_W-i], y_b[y->length_W-i];
uint8_t u_b[x->length_B-i], v_b[y->length_B-i]; uint8_t u_b[x->length_W-i], v_b[y->length_W-i];
uint8_t a_b[y->length_B+2], c_b[y->length_B+2]; uint8_t a_b[y->length_W+2], c_b[y->length_W+2];
uint8_t b_b[x->length_B+2], d_b[x->length_B+2]; uint8_t b_b[x->length_W+2], d_b[x->length_W+2];
g.wordv = g_b; g.wordv = g_b;
x_.wordv = x_b; x_.wordv = x_b;
y_.wordv = y_b; y_.wordv = y_b;
memset(g_b, 0, i); memset(g_b, 0, i);
g_b[i]=1; g_b[i]=1;
g.length_B = i+1; g.length_W = i+1;
g.info=0; g.info=0;
x_.info = y_.info = 0; x_.info = y_.info = 0;
x_.length_B = x->length_B-i; x_.length_W = x->length_W-i;
y_.length_B = y->length_B-i; y_.length_W = y->length_W-i;
memcpy(x_.wordv, x->wordv+i, x_.length_B); memcpy(x_.wordv, x->wordv+i, x_.length_W);
memcpy(y_.wordv, y->wordv+i, y_.length_B); memcpy(y_.wordv, y->wordv+i, y_.length_W);
for(i=0; (x_.wordv[0]&(1<<i))==0 && (y_.wordv[0]&(1<<i))==0; ++i){ for(i=0; (x_.wordv[0]&(1<<i))==0 && (y_.wordv[0]&(1<<i))==0; ++i){
} }
@ -694,10 +694,10 @@ void bigint_gcdext(bigint_t* gcd, bigint_t* a, bigint_t* b, const bigint_t* x, c
bigint_copy(&u, &x_); bigint_copy(&u, &x_);
bigint_copy(&v, &y_); bigint_copy(&v, &y_);
a_.wordv[0] = 1; a_.wordv[0] = 1;
a_.length_B = 1; a_.length_W = 1;
a_.info = 0; a_.info = 0;
d_.wordv[0] = 1; d_.wordv[0] = 1;
d_.length_B = 1; d_.length_W = 1;
d_.info = 0; d_.info = 0;
bigint_set_zero(&b_); bigint_set_zero(&b_);
bigint_set_zero(&c_); bigint_set_zero(&c_);
@ -730,7 +730,7 @@ void bigint_gcdext(bigint_t* gcd, bigint_t* a, bigint_t* b, const bigint_t* x, c
bigint_sub_s(&c_, &c_, &a_); bigint_sub_s(&c_, &c_, &a_);
bigint_sub_s(&d_, &d_, &b_); bigint_sub_s(&d_, &d_, &b_);
} }
}while(u.length_B); }while(u.length_W);
if(gcd){ if(gcd){
bigint_mul_s(gcd, &v, &g); bigint_mul_s(gcd, &v, &g);
} }
@ -756,7 +756,7 @@ void bigint_inverse(bigint_t* dest, const bigint_t* a, const bigint_t* m){
void bigint_changeendianess(bigint_t* a){ void bigint_changeendianess(bigint_t* a){
uint8_t t, *p, *q; uint8_t t, *p, *q;
p = a->wordv; p = a->wordv;
q = p+a->length_B-1; q = p+a->length_W-1;
while(p<q){ while(p<q){
t = *p; t = *p;
*p = *q; *p = *q;

View File

@ -61,16 +61,16 @@
/******************************************************************************/ /******************************************************************************/
void bigint_adjust(bigint_t* a){ void bigint_adjust(bigint_t* a){
while(a->length_B!=0 && a->wordv[a->length_B-1]==0){ while(a->length_W!=0 && a->wordv[a->length_W-1]==0){
a->length_B--; a->length_W--;
} }
if(a->length_B==0){ if(a->length_W==0){
a->info=0; a->info=0;
return; return;
} }
bigint_word_t t; bigint_word_t t;
uint8_t i = BIGINT_WORD_SIZE-1; 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){ while((t&(1L<<(BIGINT_WORD_SIZE-1)))==0 && i){
t<<=1; t<<=1;
i--; i--;
@ -81,25 +81,25 @@ void bigint_adjust(bigint_t* a){
/******************************************************************************/ /******************************************************************************/
uint16_t bigint_length_b(const 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 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){ 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){ 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 (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; uint32_t r=0;
uint8_t b=0; uint8_t b=0;
bigint_word_t x=1; bigint_word_t x=1;
if(a->length_B==0){ if(a->length_W==0){
return (uint32_t)(-1); return (uint32_t)(-1);
} }
while(a->wordv[r]==0 && r<a->length_B){ while(a->wordv[r]==0 && r<a->length_W){
++r; ++r;
} }
if(a->wordv[r] == 0){ 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){ void bigint_copy(bigint_t* dest, const bigint_t* src){
memcpy(dest->wordv, src->wordv, src->length_B*sizeof(bigint_word_t)); memcpy(dest->wordv, src->wordv, src->length_W*sizeof(bigint_word_t));
dest->length_B = src->length_B; dest->length_W = src->length_W;
dest->info = src->info; 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){ void bigint_add_u(bigint_t* dest, const bigint_t* a, const bigint_t* b){
uint16_t i; uint16_t i;
bigint_wordplus_t t = 0LL; bigint_wordplus_t t = 0LL;
if(a->length_B < b->length_B){ if(a->length_W < b->length_W){
XCHG_PTR(a,b); 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 += a->wordv[i];
t += b->wordv[i]; t += b->wordv[i];
dest->wordv[i] = (bigint_word_t)t; dest->wordv[i] = (bigint_word_t)t;
t >>= BIGINT_WORD_SIZE; t >>= BIGINT_WORD_SIZE;
} }
for(; i < a->length_B; ++i){ for(; i < a->length_W; ++i){
t += a->wordv[i]; t += a->wordv[i];
dest->wordv[i] = (bigint_word_t)t; dest->wordv[i] = (bigint_word_t)t;
t >>= BIGINT_WORD_SIZE; 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){ if(t){
dest->wordv[i++] = (bigint_word_t)t; dest->wordv[i++] = (bigint_word_t)t;
} }
dest->length_B = i; dest->length_W = i;
bigint_adjust(dest); 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 */ /* this should be implemented in assembly */
void bigint_add_scale_u(bigint_t* dest, const bigint_t* a, uint16_t scale){ 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; return;
} }
if(scale == 0){ if(scale == 0){
@ -181,47 +181,47 @@ void bigint_add_scale_u(bigint_t* dest, const bigint_t* a, uint16_t scale){
cli_putc(')'); cli_putc(')');
#endif #endif
#if BIGINT_WORD_SIZE == 8 #if BIGINT_WORD_SIZE == 8
if(scale >= dest->length_B){ if(scale >= dest->length_W){
#if DEBUG_ADD_SCALE #if DEBUG_ADD_SCALE
cli_putstr_P(PSTR("\r\n\tpath one")); cli_putstr_P(PSTR("\r\n\tpath one"));
#endif #endif
memset(dest->wordv + dest->length_B, 0, scale - dest->length_B); memset(dest->wordv + dest->length_W, 0, scale - dest->length_W);
memcpy(dest->wordv + scale, a->wordv, a->length_B); memcpy(dest->wordv + scale, a->wordv, a->length_W);
dest->info = a->info; dest->info = a->info;
dest->length_B = a->length_B + scale; dest->length_W = a->length_W + scale;
return; return;
} }
bigint_t x; bigint_t x;
#if DEBUG_ADD_SCALE #if DEBUG_ADD_SCALE
cli_putstr_P(PSTR("\r\n\tpath two")); cli_putstr_P(PSTR("\r\n\tpath two"));
#endif #endif
x.length_B = dest->length_B - scale; x.length_W = dest->length_W - scale;
x.info = dest->info; x.info = dest->info;
x.wordv = dest->wordv + scale; x.wordv = dest->wordv + scale;
bigint_add_u(&x, &x, a); bigint_add_u(&x, &x, a);
dest->length_B = x.length_B + scale; dest->length_W = x.length_W + scale;
dest->info = 0; dest->info = 0;
bigint_adjust(dest); bigint_adjust(dest);
#else #else
bigint_t s; bigint_t s;
uint16_t word_shift = scale / sizeof(bigint_word_t), byte_shift = scale % sizeof(bigint_word_t); 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; s.wordv = bv;
bv[0] = bv[a->length_B] = 0; bv[0] = bv[a->length_W] = 0;
memcpy((uint8_t*)bv + byte_shift, a->wordv, a->length_B * sizeof(bigint_word_t)); memcpy((uint8_t*)bv + byte_shift, a->wordv, a->length_W * sizeof(bigint_word_t));
s.length_B = a->length_B + 1; s.length_W = a->length_W + 1;
bigint_adjust(&s); 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.wordv = dest->wordv + word_shift;
x.length_B = dest->length_B - word_shift; x.length_W = dest->length_W - word_shift;
if((int16_t)x.length_B < 0){ if((int16_t)x.length_W < 0){
x.length_B = 0; x.length_W = 0;
x.info = 0; x.info = 0;
}else{ }else{
x.info = dest->info; x.info = dest->info;
} }
bigint_add_u(&x, &x, &s); 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; dest->info = 0;
bigint_adjust(dest); bigint_adjust(dest);
#endif #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_word_t *dst;
bigint_wordplus_t t=0; bigint_wordplus_t t=0;
scale_w = (scale+sizeof(bigint_word_t)-1)/sizeof(bigint_word_t); scale_w = (scale+sizeof(bigint_word_t)-1)/sizeof(bigint_word_t);
if(scale>dest->length_B*sizeof(bigint_word_t)){ if(scale>dest->length_W*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)); 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)); // a->wordv = (const uint32_t*)(((uint8_t*)a->wordv)+(scale&3));
dst = dest->wordv + (scale&(sizeof(bigint_word_t)-1)); dst = dest->wordv + (scale&(sizeof(bigint_word_t)-1));
for(i=scale/sizeof(bigint_word_t); i<a->length_B+scale_w; ++i,++j){ for(i=scale/sizeof(bigint_word_t); i<a->length_W+scale_w; ++i,++j){
t += a->wordv[j]; t += a->wordv[j];
if(dest->length_B>i){ if(dest->length_W>i){
t += dst[i]; t += dst[i];
} }
dst[i] = (bigint_word_t)t; dst[i] = (bigint_word_t)t;
t>>=BIGINT_WORD_SIZE; t>>=BIGINT_WORD_SIZE;
} }
while(t){ while(t){
if(dest->length_B>i){ if(dest->length_W>i){
t += dst[i]; t += dst[i];
} }
dst[i] = (bigint_word_t)t; dst[i] = (bigint_word_t)t;
t>>=BIGINT_WORD_SIZE; t>>=BIGINT_WORD_SIZE;
++i; ++i;
} }
if(dest->length_B < i){ if(dest->length_W < i){
dest->length_B = i; dest->length_W = i;
} }
bigint_adjust(dest); 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); bigint_set_zero(dest);
return; return;
} }
if(b->length_B==0){ if(b->length_W==0){
bigint_copy(dest, a); bigint_copy(dest, a);
SET_POS(dest); SET_POS(dest);
return; return;
} }
if(a->length_B==0){ if(a->length_W==0){
bigint_copy(dest, b); bigint_copy(dest, b);
SET_NEG(dest); SET_NEG(dest);
return; return;
@ -288,9 +288,9 @@ void bigint_sub_u(bigint_t* dest, const bigint_t* a, const bigint_t* b){
SET_NEG(dest); SET_NEG(dest);
return; return;
} }
for(i=0; i < a->length_B; ++i){ for(i=0; i < a->length_W; ++i){
t = a->wordv[i]; t = a->wordv[i];
if(i < b->length_B){ if(i < b->length_W){
t -= b->wordv[i]; t -= b->wordv[i];
} }
t -= borrow; t -= borrow;
@ -302,24 +302,24 @@ void bigint_sub_u(bigint_t* dest, const bigint_t* a, const bigint_t* b){
} }
} }
SET_POS(dest); SET_POS(dest);
dest->length_B = i; dest->length_W = i;
bigint_adjust(dest); bigint_adjust(dest);
} }
/******************************************************************************/ /******************************************************************************/
int8_t bigint_cmp_u(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; return 1;
} }
if(a->length_B < b->length_B){ if(a->length_W < b->length_W){
return -1; return -1;
} }
if(a->length_B==0){ if(a->length_W==0){
return 0; return 0;
} }
uint16_t i; uint16_t i;
i = a->length_B-1; i = a->length_W-1;
do{ do{
if(a->wordv[i] != b->wordv[i]){ if(a->wordv[i] != b->wordv[i]){
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){ int8_t bigint_cmp_s(const bigint_t* a, const bigint_t* b){
uint8_t s; uint8_t s;
if(a->length_B==0 && b->length_B==0){ if(a->length_W==0 && b->length_W==0){
return 0; return 0;
} }
s = GET_SIGN(a)?2:0; s = GET_SIGN(a)?2:0;
@ -426,14 +426,14 @@ void bigint_shiftleft(bigint_t* a, uint16_t shift){
} }
byteshift = shift / 8; byteshift = shift / 8;
bitshift = shift & 7; bitshift = shift & 7;
memset(&a->wordv[a->length_B], 0x00, byteshift); memset(&a->wordv[a->length_W], 0x00, byteshift);
if(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); memset(a->wordv, 0, byteshift);
} }
if(bitshift != 0){ if(bitshift != 0){
p = (bigint_word_t*)((uint8_t*)a->wordv + byteshift / sizeof(bigint_word_t)); 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 */ /* XXX */
for(i = 0; i < words_to_shift; ++i){ for(i = 0; i < words_to_shift; ++i){
t |= ((bigint_wordplus_t)p[i]) << bitshift; 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; 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); bigint_adjust(a);
} }
@ -455,23 +455,23 @@ void bigint_shiftright(bigint_t* a, uint16_t shift){
bigint_wordplus_t t=0; bigint_wordplus_t t=0;
byteshift = shift/8; byteshift = shift/8;
bitshift = shift&7; 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); bigint_set_zero(a);
return; 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); bigint_set_zero(a);
return; return;
} }
if(byteshift){ if(byteshift){
memmove(a->wordv, (uint8_t*)a->wordv + byteshift, a->length_B * sizeof(bigint_word_t) - byteshift); memmove(a->wordv, (uint8_t*)a->wordv + byteshift, a->length_W * sizeof(bigint_word_t) - byteshift);
memset((uint8_t*)a->wordv + a->length_B * sizeof(bigint_word_t) - byteshift, 0, byteshift); memset((uint8_t*)a->wordv + a->length_W * sizeof(bigint_word_t) - byteshift, 0, byteshift);
} }
byteshift /= sizeof(bigint_word_t); byteshift /= sizeof(bigint_word_t);
a->length_B -= (byteshift + sizeof(bigint_word_t) - 1) / sizeof(bigint_word_t); a->length_W -= (byteshift + sizeof(bigint_word_t) - 1) / sizeof(bigint_word_t);
if(bitshift != 0 && a->length_B){ if(bitshift != 0 && a->length_W){
/* shift to the right */ /* shift to the right */
i = a->length_B - 1; i = a->length_W - 1;
do{ do{
t |= ((bigint_wordplus_t)(a->wordv[i])) << (BIGINT_WORD_SIZE - bitshift); t |= ((bigint_wordplus_t)(a->wordv[i])) << (BIGINT_WORD_SIZE - bitshift);
a->wordv[i] = (bigint_word_t)(t >> BIGINT_WORD_SIZE); 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){ void bigint_xor(bigint_t* dest, const bigint_t* a){
uint16_t i; uint16_t i;
for(i=0; i<a->length_B; ++i){ for(i=0; i<a->length_W; ++i){
dest->wordv[i] ^= a->wordv[i]; dest->wordv[i] ^= a->wordv[i];
} }
bigint_adjust(dest); bigint_adjust(dest);
@ -494,7 +494,7 @@ void bigint_xor(bigint_t* dest, const bigint_t* a){
/******************************************************************************/ /******************************************************************************/
void bigint_set_zero(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 */ /* using the Karatsuba-Algorithm */
/* x*y = (xh*yh)*b**2n + ((xh+xl)*(yh+yl) - xh*yh - xl*yl)*b**n + yh*yl */ /* 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){ 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); bigint_set_zero(dest);
return; return;
} }
if(dest==a || dest==b){ if(dest==a || dest==b){
bigint_t d; 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; d.wordv = d_b;
bigint_mul_u(&d, a, b); bigint_mul_u(&d, a, b);
bigint_copy(dest, &d); bigint_copy(dest, &d);
return; return;
} }
if(a->length_B==1 || b->length_B==1){ if(a->length_W==1 || b->length_W==1){
if(a->length_B != 1){ if(a->length_W != 1){
XCHG_PTR(a,b); XCHG_PTR(a,b);
} }
bigint_wordplus_t t=0; bigint_wordplus_t t=0;
uint16_t i; uint16_t i;
bigint_word_t x = a->wordv[0]; 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); t += ((bigint_wordplus_t)b->wordv[i]) * ((bigint_wordplus_t)x);
dest->wordv[i] = (bigint_word_t)t; dest->wordv[i] = (bigint_word_t)t;
t>>=BIGINT_WORD_SIZE; t>>=BIGINT_WORD_SIZE;
} }
dest->wordv[i] = (bigint_word_t)t; dest->wordv[i] = (bigint_word_t)t;
dest->length_B = i+1; dest->length_W = i+1;
dest->info = 0; dest->info = 0;
bigint_adjust(dest); bigint_adjust(dest);
return; 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; uint32_t p=0, q=0;
uint64_t r; uint64_t r;
memcpy(&p, a->wordv, a->length_B*sizeof(bigint_word_t)); memcpy(&p, a->wordv, a->length_W*sizeof(bigint_word_t));
memcpy(&q, b->wordv, b->length_B*sizeof(bigint_word_t)); memcpy(&q, b->wordv, b->length_W*sizeof(bigint_word_t));
r = (uint64_t)p * (uint64_t)q; 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); bigint_adjust(dest);
return; return;
} }
#if BIGINT_WORD_SIZE == 8 #if BIGINT_WORD_SIZE == 8
if(a->length_B <= 4 || b->length_B <= 4){ if(a->length_W <= 4 || b->length_W <= 4){
if(a->length_B > 4){ if(a->length_W > 4){
XCHG_PTR(a,b); XCHG_PTR(a,b);
} }
uint32_t x = 0, y = 0; 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; uint64_t r = 0;
memcpy(&x, a->wordv, a->length_B); memcpy(&x, a->wordv, a->length_W);
while(j){ while(j){
r += (uint64_t)((uint32_t*)b->wordv)[idx] * (uint64_t)x; r += (uint64_t)((uint32_t*)b->wordv)[idx] * (uint64_t)x;
((uint32_t*)dest->wordv)[idx] = (uint32_t)r; ((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; --j;
} }
idx *= 4; 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; r += (uint64_t)y * (uint64_t)x;
while(r){ while(r){
dest->wordv[idx++] = (uint8_t)r; dest->wordv[idx++] = (uint8_t)r;
r >>= 8; r >>= 8;
} }
dest->length_B = idx; dest->length_W = idx;
bigint_adjust(dest); bigint_adjust(dest);
return; return;
} }
#endif #endif
/* split a in xh & xl; split b in yh & yl */ /* 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; bigint_t xl, xh, yl, yh;
xl.wordv = a->wordv; xl.wordv = a->wordv;
yl.wordv = b->wordv; yl.wordv = b->wordv;
if(a->length_B <= n){ if(a->length_W <= n){
bigint_set_zero(&xh); bigint_set_zero(&xh);
xl.length_B = a->length_B; xl.length_W = a->length_W;
xl.info = a->info; xl.info = a->info;
}else{ }else{
xl.length_B = n; xl.length_W = n;
xl.info = 0; xl.info = 0;
bigint_adjust(&xl); bigint_adjust(&xl);
xh.wordv = &(a->wordv[n]); xh.wordv = &(a->wordv[n]);
xh.length_B = a->length_B-n; xh.length_W = a->length_W-n;
xh.info = a->info; xh.info = a->info;
} }
if(b->length_B <= n){ if(b->length_W <= n){
bigint_set_zero(&yh); bigint_set_zero(&yh);
yl.length_B = b->length_B; yl.length_W = b->length_W;
yl.info = b->info; yl.info = b->info;
}else{ }else{
yl.length_B = n; yl.length_W = n;
yl.info = 0; yl.info = 0;
bigint_adjust(&yl); bigint_adjust(&yl);
yh.wordv = &(b->wordv[n]); yh.wordv = &(b->wordv[n]);
yh.length_B = b->length_B-n; yh.length_W = b->length_W-n;
yh.info = b->info; yh.info = b->info;
} }
/* now we have split up a and b */ /* 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 */ /* square */
/* (xh*b^n+xl)^2 = xh^2*b^2n + 2*xh*xl*b^n + xl^2 */ /* (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){ 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; 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; 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); SET_POS(dest);
dest->length_B = 2 * a->length_B; dest->length_W = 2 * a->length_W;
bigint_adjust(dest); bigint_adjust(dest);
return; return;
} }
if(dest==a){ if(dest==a){
bigint_t d; 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; d.wordv = d_b;
bigint_square(&d, a); bigint_square(&d, a);
bigint_copy(dest, &d); bigint_copy(dest, &d);
return; return;
} }
uint16_t n; 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_t xh, xl, tmp; /* x-high, x-low, temp */
bigint_word_t buffer[2 * n + 1]; bigint_word_t buffer[2 * n + 1];
xl.wordv = a->wordv; xl.wordv = a->wordv;
xl.length_B = n; xl.length_W = n;
xl.info = 0; xl.info = 0;
xh.wordv = &(a->wordv[n]); xh.wordv = &(a->wordv[n]);
xh.length_B = a->length_B - n; xh.length_W = a->length_W - n;
xh.info = a->info; xh.info = a->info;
bigint_adjust(&xl); bigint_adjust(&xl);
tmp.wordv = buffer; 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){ void bigint_sub_u_bitscale(bigint_t* a, const bigint_t* b, uint16_t bitscale){
bigint_t tmp, x; 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; 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 #if DEBUG
cli_putstr("\r\nDBG: *bang*\r\n"); cli_putstr("\r\nDBG: *bang*\r\n");
#endif #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.info = a->info;
x.wordv = &(a->wordv[word_shift]); 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); 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); bigint_adjust(a);
return; return;
} }
@ -755,40 +755,40 @@ void bigint_reduce(bigint_t* a, const bigint_t* r){
#if DEBUG #if DEBUG
cli_putstr("\r\nDBG: (a) = "); bigint_print_hex(a); cli_putstr("\r\nDBG: (a) = "); bigint_print_hex(a);
#endif #endif
if(r->length_B==0 || a->length_B==0){ if(r->length_W==0 || a->length_W==0){
return; 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; uint32_t p=0, q=0;
memcpy(&p, a->wordv, a->length_B*sizeof(bigint_word_t)); memcpy(&p, a->wordv, a->length_W*sizeof(bigint_word_t));
memcpy(&q, r->wordv, r->length_B*sizeof(bigint_word_t)); memcpy(&q, r->wordv, r->length_W*sizeof(bigint_word_t));
p %= q; 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); bigint_adjust(a);
// cli_putstr("\r\nDBG: (0) = "); bigint_print_hex(a); // cli_putstr("\r\nDBG: (0) = "); bigint_print_hex(a);
return; return;
} }
uint16_t shift; uint16_t shift;
while(a->length_B > r->length_B){ while(a->length_W > r->length_W){
shift = (a->length_B - r->length_B) * 8 * sizeof(bigint_word_t) + GET_FBS(a) - rfbs - 1; 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<<GET_FBS(a)) - 1)) > r->wordv[r->length_B-1]){ if((a->wordv[a->length_W-1] & ((1LL<<GET_FBS(a)) - 1)) > r->wordv[r->length_W-1]){
// cli_putc('~'); // cli_putc('~');
cli_putstr("\r\n ~ [a] = "); 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_putstr(" [r] = ");
cli_hexdump_rev(&r->wordv[r->length_B-1], 4); cli_hexdump_rev(&r->wordv[r->length_W-1], 4);
shift += 1; shift += 1;
} }
*/ */
// cli_putstr("\r\nDBG: (p) shift = "); cli_hexdump_rev(&shift, 2); // cli_putstr("\r\nDBG: (p) shift = "); cli_hexdump_rev(&shift, 2);
// cli_putstr(" a_len = "); cli_hexdump_rev(&a->length_B, 2); // cli_putstr(" a_len = "); cli_hexdump_rev(&a->length_W, 2);
// cli_putstr(" r_len = "); cli_hexdump_rev(&r->length_B, 2); // cli_putstr(" r_len = "); cli_hexdump_rev(&r->length_W, 2);
// uart_flush(0); // uart_flush(0);
bigint_sub_u_bitscale(a, r, shift); bigint_sub_u_bitscale(a, r, shift);
// cli_putstr("\r\nDBG: (1) = "); bigint_print_hex(a); // 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; shift = GET_FBS(a)-rfbs-1;
// cli_putstr("\r\nDBG: (2a) = "); bigint_print_hex(a); // cli_putstr("\r\nDBG: (2a) = "); bigint_print_hex(a);
// cli_putstr("\r\nDBG: (q) shift = "); cli_hexdump_rev(&shift, 2); // 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 */ /* calculate dest = a**exp % r */
/* using square&multiply */ /* using square&multiply */
void bigint_expmod_u(bigint_t* dest, const bigint_t* a, const bigint_t* exp, const bigint_t* r){ 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; return;
} }
bigint_t res, base; 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; uint16_t i;
uint8_t j; uint8_t j;
// uint16_t *xaddr = &i; // 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"); // cli_putstr("\r\npost-copy");
bigint_reduce(&base, r); bigint_reduce(&base, r);
res.wordv[0]=1; res.wordv[0]=1;
res.length_B=1; res.length_W=1;
res.info = 0; res.info = 0;
bigint_adjust(&res); bigint_adjust(&res);
if(exp->length_B == 0){ if(exp->length_W == 0){
bigint_copy(dest, &res); bigint_copy(dest, &res);
return; return;
} }
uint8_t flag = 0; uint8_t flag = 0;
t=exp->wordv[exp->length_B - 1]; t=exp->wordv[exp->length_W - 1];
for(i=exp->length_B; i > 0; --i){ for(i=exp->length_W; i > 0; --i){
t = exp->wordv[i - 1]; t = exp->wordv[i - 1];
for(j=BIGINT_WORD_SIZE; j > 0; --j){ for(j=BIGINT_WORD_SIZE; j > 0; --j){
if(!flag){ 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){ 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_; bigint_t g, x_, y_, u, v, a_, b_, c_, d_;
uint16_t i=0; uint16_t i=0;
if(x->length_B==0 || y->length_B==0){ if(x->length_W==0 || y->length_W==0){
return; return;
} }
if(x->length_B==1 && x->wordv[0]==1){ if(x->length_W==1 && x->wordv[0]==1){
gcd->length_B = 1; gcd->length_W = 1;
gcd->wordv[0] = 1; gcd->wordv[0] = 1;
if(a){ if(a){
a->length_B = 1; a->length_W = 1;
a->wordv[0] = 1; a->wordv[0] = 1;
SET_POS(a); SET_POS(a);
bigint_adjust(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; return;
} }
if(y->length_B==1 && y->wordv[0]==1){ if(y->length_W==1 && y->wordv[0]==1){
gcd->length_B = 1; gcd->length_W = 1;
gcd->wordv[0] = 1; gcd->wordv[0] = 1;
if(b){ if(b){
b->length_B = 1; b->length_W = 1;
b->wordv[0] = 1; b->wordv[0] = 1;
SET_POS(b); SET_POS(b);
bigint_adjust(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){ while(x->wordv[i]==0 && y->wordv[i]==0){
++i; ++i;
} }
bigint_word_t g_b[i+2], x_b[x->length_B-i], y_b[y->length_B-i]; 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_B-i], v_b[y->length_B-i]; bigint_word_t u_b[x->length_W-i], v_b[y->length_W-i];
bigint_word_t a_b[y->length_B+2], c_b[y->length_B+2]; bigint_word_t a_b[y->length_W+2], c_b[y->length_W+2];
bigint_word_t b_b[x->length_B+2], d_b[x->length_B+2]; bigint_word_t b_b[x->length_W+2], d_b[x->length_W+2];
g.wordv = g_b; g.wordv = g_b;
x_.wordv = x_b; x_.wordv = x_b;
y_.wordv = y_b; y_.wordv = y_b;
memset(g_b, 0, i*sizeof(bigint_word_t)); memset(g_b, 0, i*sizeof(bigint_word_t));
g_b[i]=1; g_b[i]=1;
g.length_B = i+1; g.length_W = i+1;
g.info=0; g.info=0;
x_.info = y_.info = 0; x_.info = y_.info = 0;
x_.length_B = x->length_B-i; x_.length_W = x->length_W-i;
y_.length_B = y->length_B-i; y_.length_W = y->length_W-i;
memcpy(x_.wordv, x->wordv+i, x_.length_B*sizeof(bigint_word_t)); memcpy(x_.wordv, x->wordv+i, x_.length_W*sizeof(bigint_word_t));
memcpy(y_.wordv, y->wordv+i, y_.length_B*sizeof(bigint_word_t)); memcpy(y_.wordv, y->wordv+i, y_.length_W*sizeof(bigint_word_t));
for(i=0; (x_.wordv[0]&(1<<i))==0 && (y_.wordv[0]&(1<<i))==0; ++i){ for(i=0; (x_.wordv[0]&(1<<i))==0 && (y_.wordv[0]&(1<<i))==0; ++i){
} }
@ -947,10 +947,10 @@ void bigint_gcdext(bigint_t* gcd, bigint_t* a, bigint_t* b, const bigint_t* x, c
bigint_copy(&u, &x_); bigint_copy(&u, &x_);
bigint_copy(&v, &y_); bigint_copy(&v, &y_);
a_.wordv[0] = 1; a_.wordv[0] = 1;
a_.length_B = 1; a_.length_W = 1;
a_.info = 0; a_.info = 0;
d_.wordv[0] = 1; d_.wordv[0] = 1;
d_.length_B = 1; d_.length_W = 1;
d_.info = 0; d_.info = 0;
bigint_set_zero(&b_); bigint_set_zero(&b_);
bigint_set_zero(&c_); bigint_set_zero(&c_);
@ -986,7 +986,7 @@ void bigint_gcdext(bigint_t* gcd, bigint_t* a, bigint_t* b, const bigint_t* x, c
bigint_sub_s(&c_, &c_, &a_); bigint_sub_s(&c_, &c_, &a_);
bigint_sub_s(&d_, &d_, &b_); bigint_sub_s(&d_, &d_, &b_);
} }
}while(u.length_B); }while(u.length_W);
if(gcd){ if(gcd){
bigint_mul_s(gcd, &v, &g); bigint_mul_s(gcd, &v, &g);
} }
@ -1012,7 +1012,7 @@ void bigint_inverse(bigint_t* dest, const bigint_t* a, const bigint_t* m){
void bigint_changeendianess(bigint_t* a){ void bigint_changeendianess(bigint_t* a){
uint8_t t, *p, *q; uint8_t t, *p, *q;
p = (uint8_t*)(a->wordv); p = (uint8_t*)(a->wordv);
q = p + a->length_B * sizeof(bigint_word_t) - 1; q = p + a->length_W * sizeof(bigint_word_t) - 1;
while(p<q){ while(p<q){
t = *p; t = *p;
*p = *q; *p = *q;

View File

@ -38,7 +38,7 @@ typedef int16_t bigint_wordplus_signed_t;
#define BIGINT_FBS_MASK (BIGINT_WORD_SIZE-1) /* the last five bits indicate which is the first bit set */ #define BIGINT_FBS_MASK (BIGINT_WORD_SIZE-1) /* the last five bits indicate which is the first bit set */
#define BIGINT_NEG_MASK 0x80 /* this bit indicates a negative value */ #define BIGINT_NEG_MASK 0x80 /* this bit indicates a negative value */
typedef struct{ typedef struct{
uint16_t length_B; uint16_t length_W;
uint8_t info; uint8_t info;
bigint_word_t *wordv; /* word vector, pointing to the LSB */ bigint_word_t *wordv; /* word vector, pointing to the LSB */
}bigint_t; }bigint_t;

View File

@ -24,7 +24,7 @@
#include <string.h> #include <string.h>
void bigint_print_hex(const bigint_t* a){ void bigint_print_hex(const bigint_t* a){
if(a->length_B==0){ if(a->length_W==0){
cli_putc('0'); cli_putc('0');
return; return;
} }
@ -32,18 +32,18 @@ void bigint_print_hex(const bigint_t* a){
cli_putc('-'); cli_putc('-');
} }
// cli_putc((a->info&BIGINT_NEG_MASK)?'-':'+'); /* print sign */ // cli_putc((a->info&BIGINT_NEG_MASK)?'-':'+'); /* print sign */
/* if(a->wordv[a->length_B-1]<0x10){ /* if(a->wordv[a->length_W-1]<0x10){
cli_putc(hexdigit_tab_uc[a->wordv[a->length_B-1]]); cli_putc(hexdigit_tab_uc[a->wordv[a->length_W-1]]);
cli_hexdump_rev(a->wordv, a->length_B-1); cli_hexdump_rev(a->wordv, a->length_W-1);
} else { } 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; uint32_t idx;
uint8_t print_zero=0; uint8_t print_zero=0;
uint8_t *p,x,y; uint8_t *p,x,y;
p = (uint8_t*)&(a->wordv[a->length_B-1])+sizeof(bigint_word_t)-1; p = (uint8_t*)&(a->wordv[a->length_W-1])+sizeof(bigint_word_t)-1;
for(idx = a->length_B * sizeof(bigint_word_t); idx > 0; --idx){ for(idx = a->length_W * sizeof(bigint_word_t); idx > 0; --idx){
x = *p >> 4; x = *p >> 4;
y = *p & 0xf; y = *p & 0xf;
if(x!=0 || print_zero!=0){ if(x!=0 || print_zero!=0){
@ -98,7 +98,7 @@ uint8_t bigint_read_hex_echo(bigint_t* a){
uint16_t allocated=0; uint16_t allocated=0;
uint8_t shift4=0; uint8_t shift4=0;
uint16_t t, idx = 0; uint16_t t, idx = 0;
a->length_B = 0; a->length_W = 0;
a->wordv = NULL; a->wordv = NULL;
a->info = 0; a->info = 0;
for(;;){ for(;;){
@ -140,9 +140,9 @@ uint8_t bigint_read_hex_echo(bigint_t* a){
/* we have to reverse the byte array */ /* we have to reverse the byte array */
uint8_t tmp; uint8_t tmp;
uint8_t *p, *q; 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); 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){ while(q>p){
tmp = *p; tmp = *p;
*p = *q; *p = *q;

View File

@ -280,15 +280,15 @@ void load_dsa_key_blob(dsa_ctx_t* ctx){
return; return;
} }
memcpy_P(ctx->priv.wordv, dsa_key_blob, ALL_LEN_B); 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.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.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.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.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->priv));
bigint_changeendianess(&(ctx->pub)); bigint_changeendianess(&(ctx->pub));

View File

@ -27,7 +27,7 @@
uint8_t dsa_sign_bigint(dsa_signature_t* s, const bigint_t* m, uint8_t dsa_sign_bigint(dsa_signature_t* s, const bigint_t* m,
const dsa_ctx_t* ctx, const bigint_t* k){ const dsa_ctx_t* ctx, const bigint_t* k){
bigint_t tmp, tmp2; 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; tmp.wordv= tmp_b;
tmp2.wordv = tmp2_b; tmp2.wordv = tmp2_b;
bigint_expmod_u(&tmp, &(ctx->domain.g), k, &(ctx->domain.p)); 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_reduce(&tmp, &(ctx->domain.q));
bigint_copy(&(s->s), &tmp); 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; 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 hfdesc_t* hash_desc, const dsa_ctx_t* ctx,
const rand_func_ptr_t rand_in){ const rand_func_ptr_t rand_in){
bigint_t z, k; 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 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]; uint8_t k_b[n_B];
hfal_hash_mem(hash_desc, hash_value, m, m_len_b); hfal_hash_mem(hash_desc, hash_value, m, m_len_b);
z.wordv = hash_value; z.wordv = hash_value;
z.length_B = n_B; z.length_W = n_B;
bigint_changeendianess(&z); bigint_changeendianess(&z);
k.wordv = k_b; k.wordv = k_b;
k.length_B = n_B; k.length_W = n_B;
do{ do{
for(i=0; i<n_B; ++i){ for(i=0; i<n_B; ++i){
k_b[i] = rand_in(); k_b[i] = rand_in();
} }
k.length_B = n_B; k.length_W = n_B;
bigint_adjust(&k); bigint_adjust(&k);
}while(dsa_sign_bigint(s, &z, ctx, &k)); }while(dsa_sign_bigint(s, &z, ctx, &k));
cli_putstr_P(PSTR("\r\nsignature computed")); cli_putstr_P(PSTR("\r\nsignature computed"));

View File

@ -24,15 +24,15 @@
uint8_t dsa_verify_bigint(const dsa_signature_t* s, const bigint_t* m, uint8_t dsa_verify_bigint(const dsa_signature_t* s, const bigint_t* m,
const dsa_ctx_t* ctx){ const dsa_ctx_t* ctx){
if(s->r.length_B==0 || s->s.length_B==0){ if(s->r.length_W==0 || s->s.length_W==0){
return DSA_SIGNATURE_FAIL; return DSA_SIGNATURE_FAIL;
} }
if(bigint_cmp_u(&(s->r), &(ctx->domain.q))>=0 || bigint_cmp_u(&(s->s), &(ctx->domain.q))>=0){ if(bigint_cmp_u(&(s->r), &(ctx->domain.q))>=0 || bigint_cmp_u(&(s->s), &(ctx->domain.q))>=0){
return DSA_SIGNATURE_FAIL; return DSA_SIGNATURE_FAIL;
} }
bigint_t w, u1, u2, v1, v2; 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 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_B*2], v2_b[ctx->domain.p.length_B]; uint8_t v1_b[ctx->domain.p.length_W*2], v2_b[ctx->domain.p.length_W];
w.wordv = w_b; w.wordv = w_b;
u1.wordv = u1_b; u1.wordv = u1_b;
u2.wordv = u2_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, 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){ const hfdesc_t* hash_desc, const dsa_ctx_t* ctx){
bigint_t z; 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]; uint8_t hash_value[(hfal_hash_getHashsize(hash_desc)+7)/8];
hfal_hash_mem(hash_desc, hash_value, m, m_len_b); hfal_hash_mem(hash_desc, hash_value, m, m_len_b);
z.wordv=hash_value; z.wordv=hash_value;
z.length_B=n_B; z.length_W=n_B;
bigint_changeendianess(&z); bigint_changeendianess(&z);
bigint_adjust(&z); bigint_adjust(&z);
return dsa_verify_bigint(s, &z, ctx); return dsa_verify_bigint(s, &z, ctx);

View File

@ -52,8 +52,8 @@ m = m2 + q * h
uint8_t rsa_dec_crt_mono(bigint_t* data, const rsa_privatekey_t* key){ uint8_t rsa_dec_crt_mono(bigint_t* data, const rsa_privatekey_t* key){
bigint_t m1, m2; bigint_t m1, m2;
m1.wordv = malloc((key->components[0].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_B /* + 1 */) * sizeof(bigint_word_t)); m2.wordv = malloc((key->components[1].length_W /* + 1 */) * sizeof(bigint_word_t));
if(!m1.wordv || !m2.wordv){ if(!m1.wordv || !m2.wordv){
#if DEBUG #if DEBUG
cli_putstr_P(PSTR("\r\nERROR: OOM!")); 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){ if(data){
memcpy(dest->wordv, data, length_B); memcpy(dest->wordv, data, length_B);
} }
dest->length_B = length_B; dest->length_W = length_B;
#else #else
uint8_t off; uint8_t off;
off = (sizeof(bigint_word_t) - length_B % sizeof(bigint_word_t)) % sizeof(bigint_word_t); 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); 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 #if DEBUG
cli_putstr_P(PSTR("\r\nDBG: dest->length_B = 0x")); cli_putstr_P(PSTR("\r\nDBG: dest->length_W = 0x"));
cli_hexdump_rev(&(dest->length_B), 2); cli_hexdump_rev(&(dest->length_W), 2);
#endif #endif
#endif #endif
dest->info = 0; 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){ void rsa_i2osp(void* dest, bigint_t* src, uint16_t* out_length_B){
#if BIGINT_WORD_SIZE == 8 #if BIGINT_WORD_SIZE == 8
if(dest){ if(dest){
uint8_t *e = src->wordv + src->length_B; uint8_t *e = src->wordv + src->length_W;
uint16_t i; uint16_t i;
for(i=src->length_B; i>0; --i){ for(i=src->length_W; i>0; --i){
*((uint8_t*)dest) = *--e; *((uint8_t*)dest) = *--e;
dest = (uint8_t*)dest + 1; 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); bigint_changeendianess(src);
} }
*out_length_B = src->length_B; *out_length_B = src->length_W;
#else #else
*out_length_B = bigint_get_first_set_bit(src) / 8 + 1; *out_length_B = bigint_get_first_set_bit(src) / 8 + 1;
if(dest){ if(dest){

View File

@ -72,7 +72,7 @@ uint8_t rsa_encrypt_oaep(void* dest, uint16_t* out_length,
cli_putstr("\r\n buffer_len = "); cli_putstr("\r\n buffer_len = ");
cli_hexdump_rev(&buffer_len, 2); cli_hexdump_rev(&buffer_len, 2);
cli_putstr("\r\n modulus_len = "); cli_putstr("\r\n modulus_len = ");
cli_hexdump_rev(&key->modulus.length_B, 2); cli_hexdump_rev(&key->modulus.length_W, 2);
#endif #endif
uint8_t* buffer = (uint8_t*)dest; uint8_t* buffer = (uint8_t*)dest;
uint8_t off; 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); cli_hexdump_block(dest, bigint_length_B(&key->modulus), 4, 16);
#endif #endif
x.info = 0; x.info = 0;
x.length_B = key->modulus.length_B; x.length_W = key->modulus.length_W;
x.wordv = dest; x.wordv = dest;
bigint_adjust(&x); bigint_adjust(&x);
rsa_os2ip(&x, NULL, bigint_length_B(&key->modulus)); 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); // cli_putc('a'); uart_flush(0);
x.wordv = dest; x.wordv = dest;
x.length_B = key->modulus.length_B; x.length_W = key->modulus.length_W;
x.info = 0; x.info = 0;
bigint_adjust(&x); bigint_adjust(&x);

View File

@ -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; ((uint8_t*)dest)[2+pad_length] = 0x00;
memcpy((uint8_t*)dest+3+pad_length, src, length_B); memcpy((uint8_t*)dest+3+pad_length, src, length_B);
x.wordv = dest; 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 #if DEBUG
cli_putstr_P(PSTR("\r\nx-data: ")); 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 #endif
bigint_adjust(&x); bigint_adjust(&x);
rsa_os2ip(&x, NULL, length_B+pad_length+3); rsa_os2ip(&x, NULL, length_B+pad_length+3);

View File

@ -72,7 +72,7 @@ void test_add_bigint(void){
bigint_print_hex(&b); bigint_print_hex(&b);
cli_putstr_P(PSTR(" = ")); cli_putstr_P(PSTR(" = "));
uint8_t *c_b; 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){ if(c_b==NULL){
cli_putstr_P(PSTR("\n\rERROR: Out of memory!")); cli_putstr_P(PSTR("\n\rERROR: Out of memory!"));
free(a.wordv); free(a.wordv);
@ -118,7 +118,7 @@ void test_add_scale_bigint(void){
} }
*/ */
uint8_t *c_b; 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){ if(c_b==NULL){
cli_putstr_P(PSTR("\n\rERROR: Out of memory!")); cli_putstr_P(PSTR("\n\rERROR: Out of memory!"));
free(a.wordv); free(a.wordv);
@ -164,7 +164,7 @@ void test_mul_bigint(void){
bigint_print_hex(&b); bigint_print_hex(&b);
cli_putstr_P(PSTR(" = ")); cli_putstr_P(PSTR(" = "));
uint8_t *c_b; 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){ if(c_b==NULL){
cli_putstr_P(PSTR("\n\rERROR: Out of memory!")); cli_putstr_P(PSTR("\n\rERROR: Out of memory!"));
free(a.wordv); free(a.wordv);
@ -194,7 +194,7 @@ void test_square_bigint(void){
bigint_print_hex(&a); bigint_print_hex(&a);
cli_putstr_P(PSTR("**2 = ")); cli_putstr_P(PSTR("**2 = "));
uint8_t *c_b; uint8_t *c_b;
c_b = malloc(a.length_B*2); c_b = malloc(a.length_W*2);
if(c_b==NULL){ if(c_b==NULL){
cli_putstr_P(PSTR("\n\rERROR: Out of memory!")); cli_putstr_P(PSTR("\n\rERROR: Out of memory!"));
free(a.wordv); free(a.wordv);
@ -260,7 +260,7 @@ void test_expmod_bigint(void){
cli_putstr_P(PSTR("\r\n end expmod test")); cli_putstr_P(PSTR("\r\n end expmod test"));
return; return;
} }
d_b = malloc(c.length_B); d_b = malloc(c.length_W);
if(d_b==NULL){ if(d_b==NULL){
cli_putstr_P(PSTR("\n\rERROR: Out of memory!")); cli_putstr_P(PSTR("\n\rERROR: Out of memory!"));
free(a.wordv); free(a.wordv);
@ -302,9 +302,9 @@ void test_gcdext_bigint(void){
cli_putstr_P(PSTR("\r\n end gcdext test")); cli_putstr_P(PSTR("\r\n end gcdext test"));
return; return;
} }
c.wordv = malloc((a.length_B<b.length_B)?a.length_B:b.length_B); c.wordv = malloc((a.length_W<b.length_W)?a.length_W:b.length_W);
d.wordv = malloc(1+(a.length_B>b.length_B)?a.length_B:b.length_B); d.wordv = malloc(1+(a.length_W>b.length_W)?a.length_W:b.length_W);
e.wordv = malloc(1+(a.length_B>b.length_B)?a.length_B:b.length_B); e.wordv = malloc(1+(a.length_W>b.length_W)?a.length_W:b.length_W);
cli_putstr_P(PSTR("\r\n gcdext( ")); cli_putstr_P(PSTR("\r\n gcdext( "));
bigint_print_hex(&a); bigint_print_hex(&a);
@ -334,8 +334,8 @@ void test_simple(void){
a.wordv=a_b; a.wordv=a_b;
b.wordv=b_b; b.wordv=b_b;
c.wordv=c_b; c.wordv=c_b;
a.length_B = 1; a.length_W = 1;
b.length_B = 1; b.length_W = 1;
a_b[0] = 1; a_b[0] = 1;
b_b[0] = 2; b_b[0] = 2;
bigint_add_u(&c, &a, &b); bigint_add_u(&c, &a, &b);
@ -351,8 +351,8 @@ void test_mul_simple(void){
a.wordv=a_b; a.wordv=a_b;
b.wordv=b_b; b.wordv=b_b;
c.wordv=c_b; c.wordv=c_b;
a.length_B = 5; a.length_W = 5;
b.length_B = 5; b.length_W = 5;
bigint_adjust(&a); bigint_adjust(&a);
bigint_adjust(&b); bigint_adjust(&b);
bigint_mul_s(&c, &a, &b); bigint_mul_s(&c, &a, &b);
@ -376,8 +376,8 @@ void test_mul_simple(void){
a.wordv=a_b; a.wordv=a_b;
b.wordv=b_b; b.wordv=b_b;
c.wordv=c_b; c.wordv=c_b;
a.length_B = 8; a.length_W = 8;
b.length_B = 8; b.length_W = 8;
a.info=0x80; a.info=0x80;
bigint_adjust(&a); bigint_adjust(&a);
bigint_adjust(&b); bigint_adjust(&b);
@ -418,7 +418,7 @@ void test_square_simple(void){
uint8_t c_b[22]; uint8_t c_b[22];
a.wordv=a_b; a.wordv=a_b;
c.wordv=c_b; c.wordv=c_b;
a.length_B = 11; a.length_W = 11;
a.info=0x00; a.info=0x00;
bigint_adjust(&a); bigint_adjust(&a);
bigint_square(&c, &a); bigint_square(&c, &a);
@ -436,11 +436,11 @@ void test_reduce_simple(void){
uint8_t b_b[2] = {0x52, 0x27}; uint8_t b_b[2] = {0x52, 0x27};
uint8_t c_b[2]; uint8_t c_b[2];
a.wordv=a_b; a.wordv=a_b;
a.length_B = 2; a.length_W = 2;
a.info=0x00; a.info=0x00;
bigint_adjust(&a); bigint_adjust(&a);
b.wordv=b_b; b.wordv=b_b;
b.length_B = 2; b.length_W = 2;
b.info=0x00; b.info=0x00;
bigint_adjust(&b); bigint_adjust(&b);
c.wordv = c_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 b_b[5] = {0x72, 0x7D, 0x57, 0xAC, 0X6F};
uint8_t c_b[6], d_b[6], e_b[6]; uint8_t c_b[6], d_b[6], e_b[6];
a.wordv=a_b; a.wordv=a_b;
a.length_B = 5; a.length_W = 5;
a.info=0x00; a.info=0x00;
bigint_adjust(&a); bigint_adjust(&a);
b.wordv=b_b; b.wordv=b_b;
b.length_B = 5; b.length_W = 5;
b.info=0x00; b.info=0x00;
bigint_adjust(&b); bigint_adjust(&b);
c.wordv = c_b; c.wordv = c_b;

View File

@ -53,8 +53,8 @@ void dsa_print_item(bigint_t* a, PGM_P pstr){
cli_putstr_P(pstr); cli_putstr_P(pstr);
cli_putstr_P(PSTR(": ")); cli_putstr_P(PSTR(": "));
uint16_t i; uint16_t i;
p = a->wordv + a->length_B -1; p = a->wordv + a->length_W -1;
for(i=0; i<a->length_B-1; ++i){ for(i=0; i<a->length_W-1; ++i){
if(i%16==0){ if(i%16==0){
cli_putstr_P(PSTR("\r\n ")); 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){ void dsa_print_signature_b64(dsa_signature_t* s){
uint16_t size_r, size_s, size_o, i,j; uint16_t size_r, size_s, size_o, i,j;
size_r = s->r.length_B +2; size_r = s->r.length_W +2;
size_s = s->s.length_B +2; size_s = s->s.length_W +2;
size_o = size_r + size_s +2; size_o = size_r + size_s +2;
uint8_t bin_b[size_o]; uint8_t bin_b[size_o];
bin_b[0] = 0x30; bin_b[0] = 0x30;
@ -79,12 +79,12 @@ void dsa_print_signature_b64(dsa_signature_t* s){
bin_b[2] = 0x02; bin_b[2] = 0x02;
bin_b[3] = size_r-2; bin_b[3] = size_r-2;
j=4; 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++] = s->r.wordv[i-1];
} }
bin_b[j++] = 0x02; bin_b[j++] = 0x02;
bin_b[j++] = size_s -2; 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]; bin_b[j++] = s->s.wordv[i-1];
} }
char b64_b[size_o*4/3+5]; char b64_b[size_o*4/3+5];
@ -111,8 +111,8 @@ void quick_test(void){
dsa_signature_t dsa_sig; dsa_signature_t dsa_sig;
uint8_t i, t=0, message[] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef}; uint8_t i, t=0, message[] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef};
load_fix_dsa(); load_fix_dsa();
uint8_t dsa_sig_s_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_B]; dsa_sig_r_b[dsa_ctx.domain.q.length_W];
dsa_print_ctx(&dsa_ctx); dsa_print_ctx(&dsa_ctx);
dsa_sig.r.wordv = dsa_sig_r_b; dsa_sig.r.wordv = dsa_sig_r_b;
dsa_sig.s.wordv = dsa_sig_s_b; dsa_sig.s.wordv = dsa_sig_s_b;

View File

@ -422,7 +422,7 @@ uint8_t read_bigint(bigint_t* a, char* prompt){
return 1; return 1;
} }
a->wordv = (bigint_word_t*)buffer; 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; a->info = 0;
bigint_changeendianess(a); bigint_changeendianess(a);
bigint_adjust(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){ 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)); a->wordv = malloc(BIGINT_CEIL(length_B));
if(!a->wordv){ if(!a->wordv){
cli_putstr_P(PSTR("\r\nOOM!\r\n")); cli_putstr_P(PSTR("\r\nOOM!\r\n"));
@ -534,8 +534,8 @@ void quick_test(void){
if(!keys_allocated){ if(!keys_allocated){
load_fix_rsa(); load_fix_rsa();
} }
ciphertext = malloc(clen = 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_B * sizeof(bigint_word_t)); plaintext = malloc(pub_key.modulus.length_W * sizeof(bigint_word_t));
memcpy_P(plaintext, MSG, sizeof(MSG)); memcpy_P(plaintext, MSG, sizeof(MSG));
memcpy_P(seed, SEED, sizeof(SEED)); memcpy_P(seed, SEED, sizeof(SEED));
cli_putstr_P(PSTR("\r\nplaintext:")); cli_putstr_P(PSTR("\r\nplaintext:"));

View File

@ -240,7 +240,7 @@ uint8_t read_bigint(bigint_t* a, char* prompt){
return 1; return 1;
} }
a->wordv = (bigint_word_t*)buffer; 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; a->info = 0;
bigint_changeendianess(a); bigint_changeendianess(a);
bigint_adjust(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){ 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)); a->wordv = malloc(BIGINT_CEIL(length_B));
if(!a->wordv){ if(!a->wordv){
cli_putstr_P(PSTR("\r\nOOM!\r\n")); cli_putstr_P(PSTR("\r\nOOM!\r\n"));
@ -474,8 +474,8 @@ void load_priv_conventional(void){
cli_putstr_P(PSTR("\r\nERROR: OOM!")); cli_putstr_P(PSTR("\r\nERROR: OOM!"));
return; return;
} }
epriv->length_B = (sizeof(PRIV_EXPONENT) + sizeof(bigint_word_t) - 1) / sizeof(bigint_word_t); epriv->length_W = (sizeof(PRIV_EXPONENT) + sizeof(bigint_word_t) - 1) / sizeof(bigint_word_t);
epriv->wordv = malloc(epriv->length_B * sizeof(bigint_word_t)); epriv->wordv = malloc(epriv->length_W * sizeof(bigint_word_t));
if(!epriv->wordv){ if(!epriv->wordv){
cli_putstr_P(PSTR("\r\nERROR: OOM!")); cli_putstr_P(PSTR("\r\nERROR: OOM!"));
return; return;
@ -508,8 +508,8 @@ void load_priv_crt_mono(void){
for(i=0; i<5; ++i){ for(i=0; i<5; ++i){
v[i] = malloc(sizeof(bigint_t)); v[i] = malloc(sizeof(bigint_t));
v[i]->info = 0; v[i]->info = 0;
v[i]->length_B = (sv[i] + sizeof(bigint_word_t) - 1) / 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_B , sizeof(bigint_word_t)); v[i]->wordv = calloc(v[i]->length_W , sizeof(bigint_word_t));
if(!v[i]->wordv){ if(!v[i]->wordv){
cli_putstr_P(PSTR("\r\nERROR: OOM!")); cli_putstr_P(PSTR("\r\nERROR: OOM!"));
return; 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){ 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)); a->wordv = malloc(BIGINT_CEIL(length_B));
if(!a->wordv){ if(!a->wordv){
cli_putstr_P(PSTR("\r\nOOM!\r\n")); cli_putstr_P(PSTR("\r\nOOM!\r\n"));