bn_mont.c (59191) | bn_mont.c (68651) |
---|---|
1/* crypto/bn/bn_mont.c */ 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 3 * All rights reserved. 4 * 5 * This package is an SSL implementation written 6 * by Eric Young (eay@cryptsoft.com). 7 * The implementation was written so as to conform with Netscapes SSL. 8 * --- 71 unchanged lines hidden (view full) --- 80 tmp2 = BN_CTX_get(ctx); 81 if (tmp == NULL || tmp2 == NULL) goto err; 82 83 bn_check_top(tmp); 84 bn_check_top(tmp2); 85 86 if (a == b) 87 { | 1/* crypto/bn/bn_mont.c */ 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 3 * All rights reserved. 4 * 5 * This package is an SSL implementation written 6 * by Eric Young (eay@cryptsoft.com). 7 * The implementation was written so as to conform with Netscapes SSL. 8 * --- 71 unchanged lines hidden (view full) --- 80 tmp2 = BN_CTX_get(ctx); 81 if (tmp == NULL || tmp2 == NULL) goto err; 82 83 bn_check_top(tmp); 84 bn_check_top(tmp2); 85 86 if (a == b) 87 { |
88#if 0 89 bn_wexpand(tmp,a->top*2); 90 bn_wexpand(tmp2,a->top*4); 91 bn_sqr_recursive(tmp->d,a->d,a->top,tmp2->d); 92 tmp->top=a->top*2; 93 if (tmp->d[tmp->top-1] == 0) 94 tmp->top--; 95#else | |
96 if (!BN_sqr(tmp,a,ctx)) goto err; | 88 if (!BN_sqr(tmp,a,ctx)) goto err; |
97#endif | |
98 } 99 else 100 { 101 if (!BN_mul(tmp,a,b,ctx)) goto err; 102 } 103 /* reduce from aRR to aR */ 104 if (!BN_from_montgomery(r,tmp,mont,ctx)) goto err; 105 ret=1; --- 46 unchanged lines hidden (view full) --- 152 r->top=max; 153 n0=mont->n0; 154 155#ifdef BN_COUNT 156 printf("word BN_from_montgomery %d * %d\n",nl,nl); 157#endif 158 for (i=0; i<nl; i++) 159 { | 89 } 90 else 91 { 92 if (!BN_mul(tmp,a,b,ctx)) goto err; 93 } 94 /* reduce from aRR to aR */ 95 if (!BN_from_montgomery(r,tmp,mont,ctx)) goto err; 96 ret=1; --- 46 unchanged lines hidden (view full) --- 143 r->top=max; 144 n0=mont->n0; 145 146#ifdef BN_COUNT 147 printf("word BN_from_montgomery %d * %d\n",nl,nl); 148#endif 149 for (i=0; i<nl; i++) 150 { |
151#ifdef __TANDEM 152 { 153 long long t1; 154 long long t2; 155 long long t3; 156 t1 = rp[0] * (n0 & 0177777); 157 t2 = 037777600000l; 158 t2 = n0 & t2; 159 t3 = rp[0] & 0177777; 160 t2 = (t3 * t2) & BN_MASK2; 161 t1 = t1 + t2; 162 v=bn_mul_add_words(rp,np,nl,(BN_ULONG) t1); 163 } 164#else |
|
160 v=bn_mul_add_words(rp,np,nl,(rp[0]*n0)&BN_MASK2); | 165 v=bn_mul_add_words(rp,np,nl,(rp[0]*n0)&BN_MASK2); |
166#endif |
|
161 nrp++; 162 rp++; 163 if (((nrp[-1]+=v)&BN_MASK2) >= v) 164 continue; 165 else 166 { 167 if (((++nrp[0])&BN_MASK2) != 0) continue; 168 if (((++nrp[1])&BN_MASK2) != 0) continue; 169 for (x=2; (((++nrp[x])&BN_MASK2) == 0); x++) ; 170 } 171 } 172 bn_fix_top(r); 173 174 /* mont->ri will be a multiple of the word size */ 175#if 0 176 BN_rshift(ret,r,mont->ri); 177#else | 167 nrp++; 168 rp++; 169 if (((nrp[-1]+=v)&BN_MASK2) >= v) 170 continue; 171 else 172 { 173 if (((++nrp[0])&BN_MASK2) != 0) continue; 174 if (((++nrp[1])&BN_MASK2) != 0) continue; 175 for (x=2; (((++nrp[x])&BN_MASK2) == 0); x++) ; 176 } 177 } 178 bn_fix_top(r); 179 180 /* mont->ri will be a multiple of the word size */ 181#if 0 182 BN_rshift(ret,r,mont->ri); 183#else |
184 ret->neg = r->neg; |
|
178 x=ri; 179 rp=ret->d; 180 ap= &(r->d[x]); 181 if (r->top < x) 182 al=0; 183 else 184 al=r->top-x; 185 ret->top=al; --- 43 unchanged lines hidden (view full) --- 229 BN_CTX_end(ctx); 230 return(retn); 231 } 232 233BN_MONT_CTX *BN_MONT_CTX_new(void) 234 { 235 BN_MONT_CTX *ret; 236 | 185 x=ri; 186 rp=ret->d; 187 ap= &(r->d[x]); 188 if (r->top < x) 189 al=0; 190 else 191 al=r->top-x; 192 ret->top=al; --- 43 unchanged lines hidden (view full) --- 236 BN_CTX_end(ctx); 237 return(retn); 238 } 239 240BN_MONT_CTX *BN_MONT_CTX_new(void) 241 { 242 BN_MONT_CTX *ret; 243 |
237 if ((ret=(BN_MONT_CTX *)Malloc(sizeof(BN_MONT_CTX))) == NULL) | 244 if ((ret=(BN_MONT_CTX *)OPENSSL_malloc(sizeof(BN_MONT_CTX))) == NULL) |
238 return(NULL); 239 240 BN_MONT_CTX_init(ret); 241 ret->flags=BN_FLG_MALLOCED; 242 return(ret); 243 } 244 245void BN_MONT_CTX_init(BN_MONT_CTX *ctx) --- 9 unchanged lines hidden (view full) --- 255 { 256 if(mont == NULL) 257 return; 258 259 BN_free(&(mont->RR)); 260 BN_free(&(mont->N)); 261 BN_free(&(mont->Ni)); 262 if (mont->flags & BN_FLG_MALLOCED) | 245 return(NULL); 246 247 BN_MONT_CTX_init(ret); 248 ret->flags=BN_FLG_MALLOCED; 249 return(ret); 250 } 251 252void BN_MONT_CTX_init(BN_MONT_CTX *ctx) --- 9 unchanged lines hidden (view full) --- 262 { 263 if(mont == NULL) 264 return; 265 266 BN_free(&(mont->RR)); 267 BN_free(&(mont->N)); 268 BN_free(&(mont->Ni)); 269 if (mont->flags & BN_FLG_MALLOCED) |
263 Free(mont); | 270 OPENSSL_free(mont); |
264 } 265 266int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx) 267 { 268 BIGNUM Ri,*R; 269 270 BN_init(&Ri); 271 R= &(mont->RR); /* grab RR as a temp */ --- 7 unchanged lines hidden (view full) --- 279 mont->ri=(BN_num_bits(mod)+(BN_BITS2-1))/BN_BITS2*BN_BITS2; 280 BN_zero(R); 281 BN_set_bit(R,BN_BITS2); /* R */ 282 283 buf[0]=mod->d[0]; /* tmod = N mod word size */ 284 buf[1]=0; 285 tmod.d=buf; 286 tmod.top=1; | 271 } 272 273int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx) 274 { 275 BIGNUM Ri,*R; 276 277 BN_init(&Ri); 278 R= &(mont->RR); /* grab RR as a temp */ --- 7 unchanged lines hidden (view full) --- 286 mont->ri=(BN_num_bits(mod)+(BN_BITS2-1))/BN_BITS2*BN_BITS2; 287 BN_zero(R); 288 BN_set_bit(R,BN_BITS2); /* R */ 289 290 buf[0]=mod->d[0]; /* tmod = N mod word size */ 291 buf[1]=0; 292 tmod.d=buf; 293 tmod.top=1; |
287 tmod.max=2; | 294 tmod.dmax=2; |
288 tmod.neg=mod->neg; 289 /* Ri = R^-1 mod N*/ 290 if ((BN_mod_inverse(&Ri,R,&tmod,ctx)) == NULL) 291 goto err; 292 BN_lshift(&Ri,&Ri,BN_BITS2); /* R*Ri */ 293 if (!BN_is_zero(&Ri)) 294 BN_sub_word(&Ri,1); 295 else /* if N mod word size == 1 */ --- 44 unchanged lines hidden --- | 295 tmod.neg=mod->neg; 296 /* Ri = R^-1 mod N*/ 297 if ((BN_mod_inverse(&Ri,R,&tmod,ctx)) == NULL) 298 goto err; 299 BN_lshift(&Ri,&Ri,BN_BITS2); /* R*Ri */ 300 if (!BN_is_zero(&Ri)) 301 BN_sub_word(&Ri,1); 302 else /* if N mod word size == 1 */ --- 44 unchanged lines hidden --- |