summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/dsa/dsa_vrf.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/dsa/dsa_vrf.c')
-rw-r--r--src/lib/libcrypto/dsa/dsa_vrf.c140
1 files changed, 74 insertions, 66 deletions
diff --git a/src/lib/libcrypto/dsa/dsa_vrf.c b/src/lib/libcrypto/dsa/dsa_vrf.c
index 0f860984ed..ff552208aa 100644
--- a/src/lib/libcrypto/dsa/dsa_vrf.c
+++ b/src/lib/libcrypto/dsa/dsa_vrf.c
@@ -56,97 +56,105 @@
56 * [including the GNU Public Licence.] 56 * [including the GNU Public Licence.]
57 */ 57 */
58 58
59/* Origional version from Steven Schoch <schoch@sheba.arc.nasa.gov> */ 59/* Original version from Steven Schoch <schoch@sheba.arc.nasa.gov> */
60 60
61#include <stdio.h> 61#include <stdio.h>
62#include "cryptlib.h" 62#include "cryptlib.h"
63#include "bn.h" 63#include <openssl/bn.h>
64#include "dsa.h" 64#include <openssl/dsa.h>
65#include "rand.h" 65#include <openssl/rand.h>
66#include "asn1.h" 66#include <openssl/asn1.h>
67#include "asn1_mac.h" 67#include <openssl/asn1_mac.h>
68 68
69/* data has already been hashed (probably with SHA or SHA-1). */ 69int DSA_do_verify(const unsigned char *dgst, int dgst_len, DSA_SIG *sig,
70/* returns 70 DSA *dsa)
71 * 1: correct signature
72 * 0: incorrect signature
73 * -1: error
74 */
75int DSA_verify(type,dgst,dgst_len,sigbuf,siglen, dsa)
76int type;
77unsigned char *dgst;
78int dgst_len;
79unsigned char *sigbuf;
80int siglen;
81DSA *dsa;
82 { 71 {
83 /* The next 3 are used by the M_ASN1 macros */
84 long length=siglen;
85 ASN1_CTX c;
86 unsigned char **pp= &sigbuf;
87 BN_CTX *ctx; 72 BN_CTX *ctx;
88 BIGNUM *r=NULL; 73 BIGNUM u1,u2,t1;
89 BIGNUM *t1=NULL,*t2=NULL; 74 BN_MONT_CTX *mont=NULL;
90 BIGNUM *u1=NULL,*u2=NULL;
91 ASN1_INTEGER *bs=NULL;
92 int ret = -1; 75 int ret = -1;
93 76
94 ctx=BN_CTX_new(); 77 if ((ctx=BN_CTX_new()) == NULL) goto err;
95 if (ctx == NULL) goto err; 78 BN_init(&u1);
96 79 BN_init(&u2);
97 t1=BN_new(); 80 BN_init(&t1);
98 t2=BN_new();
99 if (t1 == NULL || t2 == NULL) goto err;
100
101 M_ASN1_D2I_Init();
102 M_ASN1_D2I_start_sequence();
103 M_ASN1_D2I_get(bs,d2i_ASN1_INTEGER);
104 if ((r=BN_bin2bn(bs->data,bs->length,NULL)) == NULL) goto err_bn;
105 M_ASN1_D2I_get(bs,d2i_ASN1_INTEGER);
106 if ((u1=BN_bin2bn(bs->data,bs->length,NULL)) == NULL) goto err_bn;
107 if (!asn1_Finish(&c)) goto err;
108 81
109 /* Calculate W = inv(S) mod Q 82 /* Calculate W = inv(S) mod Q
110 * save W in u2 */ 83 * save W in u2 */
111 if ((u2=BN_mod_inverse(u1,dsa->q,ctx)) == NULL) goto err_bn; 84 if ((BN_mod_inverse(&u2,sig->s,dsa->q,ctx)) == NULL) goto err;
112 85
113 /* save M in u1 */ 86 /* save M in u1 */
114 if (BN_bin2bn(dgst,dgst_len,u1) == NULL) goto err_bn; 87 if (BN_bin2bn(dgst,dgst_len,&u1) == NULL) goto err;
115 88
116 /* u1 = M * w mod q */ 89 /* u1 = M * w mod q */
117 if (!BN_mod_mul(u1,u1,u2,dsa->q,ctx)) goto err_bn; 90 if (!BN_mod_mul(&u1,&u1,&u2,dsa->q,ctx)) goto err;
118 91
119 /* u2 = r * w mod q */ 92 /* u2 = r * w mod q */
120 if (!BN_mod_mul(u2,r,u2,dsa->q,ctx)) goto err_bn; 93 if (!BN_mod_mul(&u2,sig->r,&u2,dsa->q,ctx)) goto err;
94
95 if ((dsa->method_mont_p == NULL) && (dsa->flags & DSA_FLAG_CACHE_MONT_P))
96 {
97 if ((dsa->method_mont_p=(char *)BN_MONT_CTX_new()) != NULL)
98 if (!BN_MONT_CTX_set((BN_MONT_CTX *)dsa->method_mont_p,
99 dsa->p,ctx)) goto err;
100 }
101 mont=(BN_MONT_CTX *)dsa->method_mont_p;
102
103#if 0
104 {
105 BIGNUM t2;
121 106
107 BN_init(&t2);
122 /* v = ( g^u1 * y^u2 mod p ) mod q */ 108 /* v = ( g^u1 * y^u2 mod p ) mod q */
123 /* let t1 = g ^ u1 mod p */ 109 /* let t1 = g ^ u1 mod p */
124 if (!BN_mod_exp(t1,dsa->g,u1,dsa->p,ctx)) goto err_bn; 110 if (!BN_mod_exp_mont(&t1,dsa->g,&u1,dsa->p,ctx,mont)) goto err;
125 /* let t2 = y ^ u2 mod p */ 111 /* let t2 = y ^ u2 mod p */
126 if (!BN_mod_exp(t2,dsa->pub_key,u2,dsa->p,ctx)) goto err_bn; 112 if (!BN_mod_exp_mont(&t2,dsa->pub_key,&u2,dsa->p,ctx,mont)) goto err;
127 /* let u1 = t1 * t2 mod p */ 113 /* let u1 = t1 * t2 mod p */
128 if (!BN_mod_mul(u1,t1,t2,dsa->p,ctx)) goto err_bn; 114 if (!BN_mod_mul(&u1,&t1,&t2,dsa->p,ctx)) goto err_bn;
115 BN_free(&t2);
116 }
129 /* let u1 = u1 mod q */ 117 /* let u1 = u1 mod q */
130 if (!BN_mod(u1,u1,dsa->q,ctx)) goto err_bn; 118 if (!BN_mod(&u1,&u1,dsa->q,ctx)) goto err;
119#else
120 {
121 if (!BN_mod_exp2_mont(&t1,dsa->g,&u1,dsa->pub_key,&u2,dsa->p,ctx,mont))
122 goto err;
123 /* BN_copy(&u1,&t1); */
124 /* let u1 = u1 mod q */
125 if (!BN_mod(&u1,&t1,dsa->q,ctx)) goto err;
126 }
127#endif
131 /* V is now in u1. If the signature is correct, it will be 128 /* V is now in u1. If the signature is correct, it will be
132 * equal to R. */ 129 * equal to R. */
133 ret=(BN_ucmp(u1, r) == 0); 130 ret=(BN_ucmp(&u1, sig->r) == 0);
134 if (0) 131
135 { 132 err:
136err: /* ASN1 error */ 133 if (ret != 1) DSAerr(DSA_F_DSA_DO_VERIFY,ERR_R_BN_LIB);
137 DSAerr(DSA_F_DSA_VERIFY,c.error);
138 }
139 if (0)
140 {
141err_bn: /* BN error */
142 DSAerr(DSA_F_DSA_VERIFY,ERR_R_BN_LIB);
143 }
144 if (ctx != NULL) BN_CTX_free(ctx); 134 if (ctx != NULL) BN_CTX_free(ctx);
145 if (r != NULL) BN_free(r); 135 BN_free(&u1);
146 if (t1 != NULL) BN_free(t1); 136 BN_free(&u2);
147 if (t2 != NULL) BN_free(t2); 137 BN_free(&t1);
148 if (u1 != NULL) BN_free(u1); 138 return(ret);
149 if (u2 != NULL) BN_free(u2); 139 }
150 if (bs != NULL) ASN1_BIT_STRING_free(bs); 140
141/* data has already been hashed (probably with SHA or SHA-1). */
142/* returns
143 * 1: correct signature
144 * 0: incorrect signature
145 * -1: error
146 */
147int DSA_verify(int type, const unsigned char *dgst, int dgst_len,
148 unsigned char *sigbuf, int siglen, DSA *dsa)
149 {
150 DSA_SIG *s;
151 int ret=-1;
152
153 s = DSA_SIG_new();
154 if (s == NULL) return(ret);
155 if (d2i_DSA_SIG(&s,&sigbuf,siglen) == NULL) goto err;
156 ret=DSA_do_verify(dgst,dgst_len,s,dsa);
157err:
158 DSA_SIG_free(s);
151 return(ret); 159 return(ret);
152 } 160 }