diff options
author | Ralf S. Engelschall <rse@openssl.org> | 1998-12-21 11:00:56 +0000 |
---|---|---|
committer | Ralf S. Engelschall <rse@openssl.org> | 1998-12-21 11:00:56 +0000 |
commit | dfeab0689f69c0b4bd3480ffd37a9cacc2f17d9c (patch) | |
tree | 2f74e0cfd76a9e092548a9bf52e579aef984299b /crypto/dsa | |
parent | 58964a492275ca9a59a0cd9c8155cb2491b4b909 (diff) | |
download | openssl-dfeab0689f69c0b4bd3480ffd37a9cacc2f17d9c.tar.gz |
Import of old SSLeay release: SSLeay 0.9.1b (unreleased)
Diffstat (limited to 'crypto/dsa')
-rw-r--r-- | crypto/dsa/dsa.h | 6 | ||||
-rw-r--r-- | crypto/dsa/dsa_err.c | 4 | ||||
-rw-r--r-- | crypto/dsa/dsa_gen.c | 74 | ||||
-rw-r--r-- | crypto/dsa/dsa_lib.c | 6 | ||||
-rw-r--r-- | crypto/dsa/dsa_sign.c | 63 | ||||
-rw-r--r-- | crypto/dsa/dsa_vrf.c | 71 | ||||
-rw-r--r-- | crypto/dsa/f | 6 |
7 files changed, 143 insertions, 87 deletions
diff --git a/crypto/dsa/dsa.h b/crypto/dsa/dsa.h index 1ca87c1cbe..a231c199b7 100644 --- a/crypto/dsa/dsa.h +++ b/crypto/dsa/dsa.h @@ -71,6 +71,8 @@ extern "C" { #include "bn.h" +#define DSA_FLAG_CACHE_MONT_P 0x01 + typedef struct dsa_st { /* This first variable is used to pick up errors where @@ -88,6 +90,10 @@ typedef struct dsa_st BIGNUM *kinv; /* Signing pre-calc */ BIGNUM *r; /* Signing pre-calc */ + int flags; + /* Normally used to cache montgomery values */ + char *method_mont_p; + int references; } DSA; diff --git a/crypto/dsa/dsa_err.c b/crypto/dsa/dsa_err.c index 318e9f31aa..4cb58a8951 100644 --- a/crypto/dsa/dsa_err.c +++ b/crypto/dsa/dsa_err.c @@ -87,8 +87,8 @@ void ERR_load_DSA_strings() { static int init=1; - if (init); - {; + if (init) + { init=0; #ifndef NO_ERR ERR_load_strings(ERR_LIB_DSA,DSA_str_functs); diff --git a/crypto/dsa/dsa_gen.c b/crypto/dsa/dsa_gen.c index d7d30bf90a..8202b80292 100644 --- a/crypto/dsa/dsa_gen.c +++ b/crypto/dsa/dsa_gen.c @@ -88,6 +88,7 @@ char *cb_arg; unsigned char buf[SHA_DIGEST_LENGTH],buf2[SHA_DIGEST_LENGTH]; BIGNUM *r0,*W,*X,*c,*test; BIGNUM *g=NULL,*q=NULL,*p=NULL; + BN_MONT_CTX *mont=NULL; int k,n=0,i,b,m=0; int counter=0; BN_CTX *ctx=NULL,*ctx2=NULL; @@ -100,20 +101,20 @@ char *cb_arg; if ((seed_in != NULL) && (seed_len == 20)) memcpy(seed,seed_in,seed_len); - ctx=BN_CTX_new(); - if (ctx == NULL) goto err; - ctx2=BN_CTX_new(); - if (ctx2 == NULL) goto err; - ret=DSA_new(); - if (ret == NULL) goto err; - r0=ctx2->bn[0]; - g=ctx2->bn[1]; - W=ctx2->bn[2]; - q=ctx2->bn[3]; - X=ctx2->bn[4]; - c=ctx2->bn[5]; - p=ctx2->bn[6]; - test=ctx2->bn[7]; + if ((ctx=BN_CTX_new()) == NULL) goto err; + if ((ctx2=BN_CTX_new()) == NULL) goto err; + if ((ret=DSA_new()) == NULL) goto err; + + if ((mont=BN_MONT_CTX_new()) == NULL) goto err; + + r0= &(ctx2->bn[0]); + g= &(ctx2->bn[1]); + W= &(ctx2->bn[2]); + q= &(ctx2->bn[3]); + X= &(ctx2->bn[4]); + c= &(ctx2->bn[5]); + p= &(ctx2->bn[6]); + test= &(ctx2->bn[7]); BN_lshift(test,BN_value_one(),bits-1); @@ -220,10 +221,12 @@ end: BN_div(r0,NULL,test,q,ctx); BN_set_word(test,h); + BN_MONT_CTX_set(mont,p,ctx); + for (;;) { /* g=test^r0%p */ - BN_mod_exp(g,test,r0,p,ctx); + BN_mod_exp_mont(g,test,r0,p,ctx,mont); if (!BN_is_one(g)) break; BN_add(test,test,BN_value_one()); h++; @@ -246,8 +249,9 @@ err: if (counter_ret != NULL) *counter_ret=counter; if (h_ret != NULL) *h_ret=h; } - BN_CTX_free(ctx); - BN_CTX_free(ctx2); + if (ctx != NULL) BN_CTX_free(ctx); + if (ctx != NULL) BN_CTX_free(ctx2); + if (mont != NULL) BN_MONT_CTX_free(mont); return(ok?ret:NULL); } @@ -258,20 +262,22 @@ char *cb_arg; { int ok= -1,j,i,n; BN_CTX *ctx=NULL,*ctx2=NULL; - BIGNUM *w_1,*b,*m,*z; + BIGNUM *w_1,*b,*m,*z,*tmp,*mont_1; int a; + BN_MONT_CTX *mont=NULL; if (!BN_is_bit_set(w,0)) return(0); - ctx=BN_CTX_new(); - if (ctx == NULL) goto err; - ctx2=BN_CTX_new(); - if (ctx2 == NULL) goto err; + if ((ctx=BN_CTX_new()) == NULL) goto err; + if ((ctx2=BN_CTX_new()) == NULL) goto err; + if ((mont=BN_MONT_CTX_new()) == NULL) goto err; - m= ctx2->bn[2]; - b= ctx2->bn[3]; - z= ctx2->bn[4]; - w_1=ctx2->bn[5]; + m= &(ctx2->bn[2]); + b= &(ctx2->bn[3]); + z= &(ctx2->bn[4]); + w_1= &(ctx2->bn[5]); + tmp= &(ctx2->bn[6]); + mont_1= &(ctx2->bn[7]); /* step 1 */ n=50; @@ -282,24 +288,30 @@ char *cb_arg; ; if (!BN_rshift(m,w_1,a)) goto err; + BN_MONT_CTX_set(mont,w,ctx); + BN_to_montgomery(mont_1,BN_value_one(),mont,ctx); + BN_to_montgomery(w_1,w_1,mont,ctx); for (i=1; i < n; i++) { /* step 3 */ BN_rand(b,BN_num_bits(w)-2/*-1*/,0,0); - BN_set_word(b,0x10001L); + /* BN_set_word(b,0x10001L); */ /* step 4 */ j=0; - if (!BN_mod_exp(z,b,m,w,ctx)) goto err; + if (!BN_mod_exp_mont(z,b,m,w,ctx,mont)) goto err; + + if (!BN_to_montgomery(z,z,mont,ctx)) goto err; /* step 5 */ for (;;) { - if (((j == 0) && BN_is_one(z)) || (BN_cmp(z,w_1) == 0)) + if (((j == 0) && (BN_cmp(z,mont_1) == 0)) || + (BN_cmp(z,w_1) == 0)) break; /* step 6 */ - if ((j > 0) && BN_is_one(z)) + if ((j > 0) && (BN_cmp(z,mont_1) == 0)) { ok=0; goto err; @@ -312,7 +324,7 @@ char *cb_arg; goto err; } - if (!BN_mod_mul(z,z,z,w,ctx)) goto err; + if (!BN_mod_mul_montgomery(z,z,z,mont,ctx)) goto err; if (callback != NULL) callback(1,j,cb_arg); } } diff --git a/crypto/dsa/dsa_lib.c b/crypto/dsa/dsa_lib.c index b647257f9f..bfa9ca28d7 100644 --- a/crypto/dsa/dsa_lib.c +++ b/crypto/dsa/dsa_lib.c @@ -64,7 +64,7 @@ #include "dsa.h" #include "asn1.h" -char *DSA_version="\0DSA part of SSLeay 0.9.0b 29-Jun-1998"; +char *DSA_version="\0DSA part of SSLeay 0.9.1a 06-Jul-1998"; DSA *DSA_new() { @@ -82,12 +82,14 @@ DSA *DSA_new() ret->p=NULL; ret->q=NULL; ret->g=NULL; + ret->flags=DSA_FLAG_CACHE_MONT_P; ret->pub_key=NULL; ret->priv_key=NULL; ret->kinv=NULL; ret->r=NULL; + ret->method_mont_p=NULL; ret->references=1; return(ret); @@ -120,6 +122,8 @@ DSA *r; if (r->priv_key != NULL) BN_clear_free(r->priv_key); if (r->kinv != NULL) BN_clear_free(r->kinv); if (r->r != NULL) BN_clear_free(r->r); + if (r->method_mont_p != NULL) + BN_MONT_CTX_free((BN_MONT_CTX *)r->method_mont_p); Free(r); } diff --git a/crypto/dsa/dsa_sign.c b/crypto/dsa/dsa_sign.c index 6ca1c318f2..c4df4e5b75 100644 --- a/crypto/dsa/dsa_sign.c +++ b/crypto/dsa/dsa_sign.c @@ -77,8 +77,8 @@ unsigned int *siglen; /* out */ DSA *dsa; { BIGNUM *kinv=NULL,*r=NULL; - BIGNUM *m=NULL; - BIGNUM *xr=NULL,*s=NULL; + BIGNUM m; + BIGNUM xr,s; BN_CTX *ctx=NULL; unsigned char *p; int i,len=0,ret=0,reason=ERR_R_BN_LIB; @@ -86,6 +86,10 @@ DSA *dsa; MS_STATIC unsigned char rbuf[50]; /* assuming r is 20 bytes +extra */ MS_STATIC unsigned char sbuf[50]; /* assuming s is 20 bytes +extra */ + BN_init(&m); + BN_init(&xr); + BN_init(&s); + i=BN_num_bytes(dsa->q); /* should be 20 */ if ((dlen > i) || (dlen > 50)) { @@ -108,17 +112,14 @@ DSA *dsa; dsa->r=NULL; } - m=BN_new(); - xr=BN_new(); - s=BN_new(); - if (m == NULL || xr == NULL || s == NULL) goto err; - - if (BN_bin2bn(dgst,dlen,m) == NULL) goto err; + if (BN_bin2bn(dgst,dlen,&m) == NULL) goto err; /* Compute s = inv(k) (m + xr) mod q */ - if (!BN_mul(xr, dsa->priv_key, r)) goto err; /* s = xr */ - if (!BN_add(s, xr, m)) goto err; /* s = m + xr */ - if (!BN_mod_mul(s,s,kinv,dsa->q,ctx)) goto err; + if (!BN_mod_mul(&xr,dsa->priv_key,r,dsa->q,ctx)) goto err;/* s = xr */ + if (!BN_add(&s, &xr, &m)) goto err; /* s = m + xr */ + if (BN_cmp(&s,dsa->q) > 0) + BN_sub(&s,&s,dsa->q); + if (!BN_mod_mul(&s,&s,kinv,dsa->q,ctx)) goto err; /* * Now create a ASN.1 sequence of the integers R and S. @@ -128,7 +129,7 @@ DSA *dsa; rbs.type = V_ASN1_INTEGER; sbs.type = V_ASN1_INTEGER; rbs.length=BN_bn2bin(r,rbs.data); - sbs.length=BN_bn2bin(s,sbs.data); + sbs.length=BN_bn2bin(&s,sbs.data); len =i2d_ASN1_INTEGER(&rbs,NULL); len+=i2d_ASN1_INTEGER(&sbs,NULL); @@ -147,9 +148,9 @@ err: if (r != NULL) BN_clear_free(r); #endif if (ctx != NULL) BN_CTX_free(ctx); - if (m != NULL) BN_clear_free(m); - if (xr != NULL) BN_clear_free(xr); - if (s != NULL) BN_clear_free(s); + BN_clear_free(&m); + BN_clear_free(&xr); + BN_clear_free(&s); return(ret); } @@ -160,7 +161,7 @@ BIGNUM **kinvp; BIGNUM **rp; { BN_CTX *ctx; - BIGNUM *k=NULL,*kinv=NULL,*r=NULL; + BIGNUM k,*kinv=NULL,*r=NULL; int ret=0; if (ctx_in == NULL) @@ -170,29 +171,33 @@ BIGNUM **rp; else ctx=ctx_in; - r=BN_new(); - k=BN_new(); - if ((r == NULL) || (k == NULL)) - goto err; + BN_init(&k); + if ((r=BN_new()) == NULL) goto err; kinv=NULL; - if (r == NULL) goto err; - /* Get random k */ for (;;) { - if (!BN_rand(k, BN_num_bits(dsa->q), 1, 0)) goto err; - if (BN_cmp(k,dsa->q) >= 0) - BN_sub(k,k,dsa->q); - if (!BN_is_zero(k)) break; + if (!BN_rand(&k, BN_num_bits(dsa->q), 1, 0)) goto err; + if (BN_cmp(&k,dsa->q) >= 0) + BN_sub(&k,&k,dsa->q); + if (!BN_is_zero(&k)) break; + } + + if ((dsa->method_mont_p == NULL) && (dsa->flags & DSA_FLAG_CACHE_MONT_P)) + { + if ((dsa->method_mont_p=(char *)BN_MONT_CTX_new()) != NULL) + if (!BN_MONT_CTX_set((BN_MONT_CTX *)dsa->method_mont_p, + dsa->p,ctx)) goto err; } /* Compute r = (g^k mod p) mod q */ - if (!BN_mod_exp(r,dsa->g,k,dsa->p,ctx)) goto err; + if (!BN_mod_exp_mont(r,dsa->g,&k,dsa->p,ctx, + (BN_MONT_CTX *)dsa->method_mont_p)) goto err; if (!BN_mod(r,r,dsa->q,ctx)) goto err; /* Compute part of 's = inv(k) (m + xr) mod q' */ - if ((kinv=BN_mod_inverse(k,dsa->q,ctx)) == NULL) goto err; + if ((kinv=BN_mod_inverse(NULL,&k,dsa->q,ctx)) == NULL) goto err; if (*kinvp != NULL) BN_clear_free(*kinvp); *kinvp=kinv; @@ -208,8 +213,8 @@ err: if (r != NULL) BN_clear_free(r); } if (ctx_in == NULL) BN_CTX_free(ctx); - if (k != NULL) BN_clear_free(k); if (kinv != NULL) BN_clear_free(kinv); + BN_clear_free(&k); return(ret); } diff --git a/crypto/dsa/dsa_vrf.c b/crypto/dsa/dsa_vrf.c index 0f860984ed..71cefbeaa4 100644 --- a/crypto/dsa/dsa_vrf.c +++ b/crypto/dsa/dsa_vrf.c @@ -85,52 +85,76 @@ DSA *dsa; ASN1_CTX c; unsigned char **pp= &sigbuf; BN_CTX *ctx; - BIGNUM *r=NULL; - BIGNUM *t1=NULL,*t2=NULL; - BIGNUM *u1=NULL,*u2=NULL; + BIGNUM r,u1,u2,t1; ASN1_INTEGER *bs=NULL; + BN_MONT_CTX *mont=NULL; int ret = -1; - ctx=BN_CTX_new(); - if (ctx == NULL) goto err; + if ((ctx=BN_CTX_new()) == NULL) goto err; + if ((mont=BN_MONT_CTX_new()) == NULL) goto err; - t1=BN_new(); - t2=BN_new(); - if (t1 == NULL || t2 == NULL) goto err; + BN_init(&u1); + BN_init(&u2); + BN_init(&r); + BN_init(&t1); M_ASN1_D2I_Init(); M_ASN1_D2I_start_sequence(); M_ASN1_D2I_get(bs,d2i_ASN1_INTEGER); - if ((r=BN_bin2bn(bs->data,bs->length,NULL)) == NULL) goto err_bn; + if ((BN_bin2bn(bs->data,bs->length,&r)) == NULL) goto err_bn; M_ASN1_D2I_get(bs,d2i_ASN1_INTEGER); - if ((u1=BN_bin2bn(bs->data,bs->length,NULL)) == NULL) goto err_bn; + if ((BN_bin2bn(bs->data,bs->length,&u1)) == NULL) goto err_bn; if (!asn1_Finish(&c)) goto err; /* Calculate W = inv(S) mod Q * save W in u2 */ - if ((u2=BN_mod_inverse(u1,dsa->q,ctx)) == NULL) goto err_bn; + if ((BN_mod_inverse(&u2,&u1,dsa->q,ctx)) == NULL) goto err_bn; /* save M in u1 */ - if (BN_bin2bn(dgst,dgst_len,u1) == NULL) goto err_bn; + if (BN_bin2bn(dgst,dgst_len,&u1) == NULL) goto err_bn; /* u1 = M * w mod q */ - if (!BN_mod_mul(u1,u1,u2,dsa->q,ctx)) goto err_bn; + if (!BN_mod_mul(&u1,&u1,&u2,dsa->q,ctx)) goto err_bn; /* u2 = r * w mod q */ - if (!BN_mod_mul(u2,r,u2,dsa->q,ctx)) goto err_bn; + if (!BN_mod_mul(&u2,&r,&u2,dsa->q,ctx)) goto err_bn; + if ((dsa->method_mont_p == NULL) && (dsa->flags & DSA_FLAG_CACHE_MONT_P)) + { + if ((dsa->method_mont_p=(char *)BN_MONT_CTX_new()) != NULL) + if (!BN_MONT_CTX_set((BN_MONT_CTX *)dsa->method_mont_p, + dsa->p,ctx)) goto err; + } + mont=(BN_MONT_CTX *)dsa->method_mont_p; + +#if 0 + { + BIGNUM t2; + + BN_init(&t2); /* v = ( g^u1 * y^u2 mod p ) mod q */ /* let t1 = g ^ u1 mod p */ - if (!BN_mod_exp(t1,dsa->g,u1,dsa->p,ctx)) goto err_bn; + if (!BN_mod_exp_mont(&t1,dsa->g,&u1,dsa->p,ctx,mont)) goto err_bn; /* let t2 = y ^ u2 mod p */ - if (!BN_mod_exp(t2,dsa->pub_key,u2,dsa->p,ctx)) goto err_bn; + if (!BN_mod_exp_mont(&t2,dsa->pub_key,&u2,dsa->p,ctx,mont)) goto err_bn; /* let u1 = t1 * t2 mod p */ - if (!BN_mod_mul(u1,t1,t2,dsa->p,ctx)) goto err_bn; + if (!BN_mod_mul(&u1,&t1,&t2,dsa->p,ctx)) goto err_bn; + BN_free(&t2); + } + /* let u1 = u1 mod q */ + if (!BN_mod(&u1,&u1,dsa->q,ctx)) goto err_bn; +#else + { + if (!BN_mod_exp2_mont(&t1,dsa->g,&u1,dsa->pub_key,&u2,dsa->p,ctx,mont)) + goto err_bn; + /* BN_copy(&u1,&t1); */ /* let u1 = u1 mod q */ - if (!BN_mod(u1,u1,dsa->q,ctx)) goto err_bn; + if (!BN_mod(&u1,&t1,dsa->q,ctx)) goto err_bn; + } +#endif /* V is now in u1. If the signature is correct, it will be * equal to R. */ - ret=(BN_ucmp(u1, r) == 0); + ret=(BN_ucmp(&u1, &r) == 0); if (0) { err: /* ASN1 error */ @@ -142,11 +166,10 @@ err_bn: /* BN error */ DSAerr(DSA_F_DSA_VERIFY,ERR_R_BN_LIB); } if (ctx != NULL) BN_CTX_free(ctx); - if (r != NULL) BN_free(r); - if (t1 != NULL) BN_free(t1); - if (t2 != NULL) BN_free(t2); - if (u1 != NULL) BN_free(u1); - if (u2 != NULL) BN_free(u2); + BN_free(&r); + BN_free(&u1); + BN_free(&u2); + BN_free(&t1); if (bs != NULL) ASN1_BIT_STRING_free(bs); return(ret); } diff --git a/crypto/dsa/f b/crypto/dsa/f new file mode 100644 index 0000000000..36865a7f5d --- /dev/null +++ b/crypto/dsa/f @@ -0,0 +1,6 @@ + if ((dsa->method_mod_p == NULL) && (dsa->flags & DSA_FLAG_CACHE_MONT_P)) + { + if ((dsa->method_mod_p=(char *)BN_MONT_CTX_new()) != NULL) + if (!BN_MONT_CTX_set((BN_MONT_CTX *)dsa->method_mod_p, + dsa->p,ctx)) goto err; + } |