#define MONT_WORD /* use the faster word-based algorithm */
-int BN_mod_mul_montgomery(BIGNUM *r, BIGNUM *a, BIGNUM *b,
+int BN_mod_mul_montgomery(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
BN_MONT_CTX *mont, BN_CTX *ctx)
{
- BIGNUM *tmp,*tmp2;
+ BIGNUM *tmp;
int ret=0;
BN_CTX_start(ctx);
tmp = BN_CTX_get(ctx);
- tmp2 = BN_CTX_get(ctx);
- if (tmp == NULL || tmp2 == NULL) goto err;
+ if (tmp == NULL) goto err;
bn_check_top(tmp);
- bn_check_top(tmp2);
-
if (a == b)
{
-#if 0 /* buggy -- try squaring g (after converting it to Montgomery
- representation) in the following parameters
- (but note that squaring 2 or 4 works):
-Diffie-Hellman-Parameters: (1024 bit)
- prime:
- 00:ff:ff:ff:ff:ff:ff:ff:ff:c9:0f:da:a2:21:68:
- c2:34:c4:c6:62:8b:80:dc:1c:d1:29:02:4e:08:8a:
- 67:cc:74:02:0b:be:a6:3b:13:9b:22:51:4a:08:79:
- 8e:34:04:dd:ef:95:19:b3:cd:3a:43:1b:30:2b:0a:
- 6d:f2:5f:14:37:4f:e1:35:6d:6d:51:c2:45:e4:85:
- b5:76:62:5e:7e:c6:f4:4c:42:e9:a6:37:ed:6b:0b:
- ff:5c:b6:f4:06:b7:ed:ee:38:6b:fb:5a:89:9f:a5:
- ae:9f:24:11:7c:4b:1f:e6:49:28:66:51:ec:e6:53:
- 81:ff:ff:ff:ff:ff:ff:ff:ff
- generator: 8 (0x8)
------BEGIN DH PARAMETERS-----
-MIGHAoGBAP//////////yQ/aoiFowjTExmKLgNwc0SkCTgiKZ8x0Agu+pjsTmyJR
-Sgh5jjQE3e+VGbPNOkMbMCsKbfJfFDdP4TVtbVHCReSFtXZiXn7G9ExC6aY37WsL
-/1y29Aa37e44a/taiZ+lrp8kEXxLH+ZJKGZR7OZTgf//////////AgEI
------END DH PARAMETERS-----
-*/
- bn_wexpand(tmp,a->top*2);
- bn_wexpand(tmp2,a->top*4);
- bn_sqr_recursive(tmp->d,a->d,a->top,tmp2->d);
- tmp->top=a->top*2;
- while (tmp->top > 0 && tmp->d[tmp->top-1] == 0)
- tmp->top--;
-#else
if (!BN_sqr(tmp,a,ctx)) goto err;
-#endif
}
else
{
return(ret);
}
-int BN_from_montgomery(BIGNUM *ret, BIGNUM *a, BN_MONT_CTX *mont,
+int BN_from_montgomery(BIGNUM *ret, const BIGNUM *a, BN_MONT_CTX *mont,
BN_CTX *ctx)
{
int retn=0;
n0=mont->n0;
#ifdef BN_COUNT
- printf("word BN_from_montgomery %d * %d\n",nl,nl);
+ fprintf(stderr,"word BN_from_montgomery %d * %d\n",nl,nl);
#endif
for (i=0; i<nl; i++)
{
if (BN_ucmp(ret, &(mont->N)) >= 0)
{
- BN_usub(ret,ret,&(mont->N));
+ if (!BN_usub(ret,ret,&(mont->N))) goto err;
}
retn=1;
err:
BN_init(&Ri);
R= &(mont->RR); /* grab RR as a temp */
BN_copy(&(mont->N),mod); /* Set N */
+ mont->N.neg = 0;
#ifdef MONT_WORD
{
tmod.d=buf;
tmod.top=1;
tmod.dmax=2;
- tmod.neg=mod->neg;
+ tmod.neg=0;
/* Ri = R^-1 mod N*/
if ((BN_mod_inverse(&Ri,R,&tmod,ctx)) == NULL)
goto err;
- BN_lshift(&Ri,&Ri,BN_BITS2); /* R*Ri */
+ if (!BN_lshift(&Ri,&Ri,BN_BITS2)) goto err; /* R*Ri */
if (!BN_is_zero(&Ri))
- BN_sub_word(&Ri,1);
+ {
+ if (!BN_sub_word(&Ri,1)) goto err;
+ }
else /* if N mod word size == 1 */
- BN_set_word(&Ri,BN_MASK2); /* Ri-- (mod word size) */
- BN_div(&Ri,NULL,&Ri,&tmod,ctx); /* Ni = (R*Ri-1)/N,
- * keep only least significant word: */
- mont->n0=Ri.d[0];
+ {
+ if (!BN_set_word(&Ri,BN_MASK2)) goto err; /* Ri-- (mod word size) */
+ }
+ if (!BN_div(&Ri,NULL,&Ri,&tmod,ctx)) goto err;
+ /* Ni = (R*Ri-1)/N,
+ * keep only least significant word: */
+ mont->n0 = (Ri.top > 0) ? Ri.d[0] : 0;
BN_free(&Ri);
}
#else /* !MONT_WORD */
{ /* bignum version */
- mont->ri=BN_num_bits(mod);
- BN_zero(R);
- BN_set_bit(R,mont->ri); /* R = 2^ri */
- /* Ri = R^-1 mod N*/
- if ((BN_mod_inverse(&Ri,R,mod,ctx)) == NULL)
+ mont->ri=BN_num_bits(&mont->N);
+ if (!BN_zero(R)) goto err;
+ if (!BN_set_bit(R,mont->ri)) goto err; /* R = 2^ri */
+ /* Ri = R^-1 mod N*/
+ if ((BN_mod_inverse(&Ri,R,&mont->N,ctx)) == NULL)
goto err;
- BN_lshift(&Ri,&Ri,mont->ri); /* R*Ri */
- BN_sub_word(&Ri,1);
+ if (!BN_lshift(&Ri,&Ri,mont->ri)) goto err; /* R*Ri */
+ if (!BN_sub_word(&Ri,1)) goto err;
/* Ni = (R*Ri-1) / N */
- BN_div(&(mont->Ni),NULL,&Ri,mod,ctx);
+ if (!BN_div(&(mont->Ni),NULL,&Ri,&mont->N,ctx)) goto err;
BN_free(&Ri);
}
#endif
/* setup RR for conversions */
- BN_zero(&(mont->RR));
- BN_set_bit(&(mont->RR),mont->ri*2);
- BN_mod(&(mont->RR),&(mont->RR),&(mont->N),ctx);
+ if (!BN_zero(&(mont->RR))) goto err;
+ if (!BN_set_bit(&(mont->RR),mont->ri*2)) goto err;
+ if (!BN_mod(&(mont->RR),&(mont->RR),&(mont->N),ctx)) goto err;
return(1);
err: