ea09afd9534be107ca1860c111ea1e15e2e98718
[openssl.git] / crypto / dsa / dsa_ossl.c
1 /*
2  * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 /* Original version from Steven Schoch <schoch@sheba.arc.nasa.gov> */
11
12 #include <stdio.h>
13 #include "internal/cryptlib.h"
14 #include <openssl/bn.h>
15 #include <openssl/sha.h>
16 #include "dsa_locl.h"
17 #include <openssl/rand.h>
18 #include <openssl/asn1.h>
19
20 static DSA_SIG *dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa);
21 static int dsa_sign_setup_no_digest(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp,
22                                     BIGNUM **rp);
23 static int dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp,
24                           BIGNUM **rp, const unsigned char *dgst, int dlen);
25 static int dsa_do_verify(const unsigned char *dgst, int dgst_len,
26                          DSA_SIG *sig, DSA *dsa);
27 static int dsa_init(DSA *dsa);
28 static int dsa_finish(DSA *dsa);
29
30 static DSA_METHOD openssl_dsa_meth = {
31     "OpenSSL DSA method",
32     dsa_do_sign,
33     dsa_sign_setup_no_digest,
34     dsa_do_verify,
35     NULL,                       /* dsa_mod_exp, */
36     NULL,                       /* dsa_bn_mod_exp, */
37     dsa_init,
38     dsa_finish,
39     DSA_FLAG_FIPS_METHOD,
40     NULL,
41     NULL,
42     NULL
43 };
44
45 const DSA_METHOD *DSA_OpenSSL(void)
46 {
47     return &openssl_dsa_meth;
48 }
49
50 static DSA_SIG *dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)
51 {
52     BIGNUM *kinv = NULL;
53     BIGNUM *m;
54     BIGNUM *xr;
55     BIGNUM *r, *s;
56     BN_CTX *ctx = NULL;
57     int reason = ERR_R_BN_LIB;
58     DSA_SIG *ret = NULL;
59     int rv = 0;
60
61     m = BN_new();
62     xr = BN_new();
63     if (m == NULL || xr == NULL)
64         goto err;
65
66     if (!dsa->p || !dsa->q || !dsa->g) {
67         reason = DSA_R_MISSING_PARAMETERS;
68         goto err;
69     }
70
71     ret = DSA_SIG_new();
72     if (ret == NULL)
73         goto err;
74
75     DSA_SIG_get0(&r, &s, ret);
76
77     ctx = BN_CTX_new();
78     if (ctx == NULL)
79         goto err;
80  redo:
81     if (!dsa_sign_setup(dsa, ctx, &kinv, &r, dgst, dlen))
82         goto err;
83
84     if (dlen > BN_num_bytes(dsa->q))
85         /*
86          * if the digest length is greater than the size of q use the
87          * BN_num_bits(dsa->q) leftmost bits of the digest, see fips 186-3,
88          * 4.2
89          */
90         dlen = BN_num_bytes(dsa->q);
91     if (BN_bin2bn(dgst, dlen, m) == NULL)
92         goto err;
93
94     /* Compute  s = inv(k) (m + xr) mod q */
95     if (!BN_mod_mul(xr, dsa->priv_key, r, dsa->q, ctx))
96         goto err;               /* s = xr */
97     if (!BN_add(s, xr, m))
98         goto err;               /* s = m + xr */
99     if (BN_cmp(s, dsa->q) > 0)
100         if (!BN_sub(s, s, dsa->q))
101             goto err;
102     if (!BN_mod_mul(s, s, kinv, dsa->q, ctx))
103         goto err;
104
105     /*
106      * Redo if r or s is zero as required by FIPS 186-3: this is very
107      * unlikely.
108      */
109     if (BN_is_zero(r) || BN_is_zero(s))
110         goto redo;
111
112     rv = 1;
113
114  err:
115     if (rv == 0) {
116         DSAerr(DSA_F_DSA_DO_SIGN, reason);
117         DSA_SIG_free(ret);
118         ret = NULL;
119     }
120     BN_CTX_free(ctx);
121     BN_clear_free(m);
122     BN_clear_free(xr);
123     BN_clear_free(kinv);
124     return ret;
125 }
126
127 static int dsa_sign_setup_no_digest(DSA *dsa, BN_CTX *ctx_in,
128                                     BIGNUM **kinvp, BIGNUM **rp)
129 {
130     return dsa_sign_setup(dsa, ctx_in, kinvp, rp, NULL, 0);
131 }
132
133 static int dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in,
134                           BIGNUM **kinvp, BIGNUM **rp,
135                           const unsigned char *dgst, int dlen)
136 {
137     BN_CTX *ctx = NULL;
138     BIGNUM *k, *kq, *kinv = NULL, *r = *rp;
139     int ret = 0;
140
141     if (!dsa->p || !dsa->q || !dsa->g) {
142         DSAerr(DSA_F_DSA_SIGN_SETUP, DSA_R_MISSING_PARAMETERS);
143         return 0;
144     }
145
146     k = BN_new();
147     kq = BN_new();
148     if (k == NULL || kq == NULL)
149         goto err;
150
151     if (ctx_in == NULL) {
152         if ((ctx = BN_CTX_new()) == NULL)
153             goto err;
154     } else
155         ctx = ctx_in;
156
157     /* Get random k */
158     do {
159         if (dgst != NULL) {
160             /*
161              * We calculate k from SHA512(private_key + H(message) + random).
162              * This protects the private key from a weak PRNG.
163              */
164             if (!BN_generate_dsa_nonce(k, dsa->q, dsa->priv_key, dgst,
165                                        dlen, ctx))
166                 goto err;
167         } else if (!BN_rand_range(k, dsa->q))
168             goto err;
169     } while (BN_is_zero(k));
170
171     if (dsa->flags & DSA_FLAG_CACHE_MONT_P) {
172         if (!BN_MONT_CTX_set_locked(&dsa->method_mont_p,
173                                     dsa->lock, dsa->p, ctx))
174             goto err;
175     }
176
177     /* Compute r = (g^k mod p) mod q */
178
179     if (!BN_copy(kq, k))
180         goto err;
181
182     /*
183      * We do not want timing information to leak the length of k, so we
184      * compute g^k using an equivalent exponent of fixed length. (This
185      * is a kludge that we need because the BN_mod_exp_mont() does not
186      * let us specify the desired timing behaviour.)
187      */
188
189     if (!BN_add(kq, kq, dsa->q))
190         goto err;
191     if (BN_num_bits(kq) <= BN_num_bits(dsa->q)) {
192         if (!BN_add(kq, kq, dsa->q))
193             goto err;
194     }
195
196     BN_set_flags(kq, BN_FLG_CONSTTIME);
197
198     if ((dsa)->meth->bn_mod_exp != NULL) {
199             if (!dsa->meth->bn_mod_exp(dsa, r, dsa->g, kq, dsa->p, ctx,
200                                        dsa->method_mont_p))
201                 goto err;
202     } else {
203             if (!BN_mod_exp_mont(r, dsa->g, kq, dsa->p, ctx, dsa->method_mont_p))
204                 goto err;
205     }
206
207
208     if (!BN_mod(r, r, dsa->q, ctx))
209         goto err;
210
211     /* Compute  part of 's = inv(k) (m + xr) mod q' */
212     if ((kinv = BN_mod_inverse(NULL, k, dsa->q, ctx)) == NULL)
213         goto err;
214
215     BN_clear_free(*kinvp);
216     *kinvp = kinv;
217     kinv = NULL;
218     ret = 1;
219  err:
220     if (!ret)
221         DSAerr(DSA_F_DSA_SIGN_SETUP, ERR_R_BN_LIB);
222     if (ctx != ctx_in)
223         BN_CTX_free(ctx);
224     BN_clear_free(k);
225     BN_clear_free(kq);
226     return ret;
227 }
228
229 static int dsa_do_verify(const unsigned char *dgst, int dgst_len,
230                          DSA_SIG *sig, DSA *dsa)
231 {
232     BN_CTX *ctx;
233     BIGNUM *u1, *u2, *t1;
234     BN_MONT_CTX *mont = NULL;
235     BIGNUM *r, *s;
236     int ret = -1, i;
237     if (!dsa->p || !dsa->q || !dsa->g) {
238         DSAerr(DSA_F_DSA_DO_VERIFY, DSA_R_MISSING_PARAMETERS);
239         return -1;
240     }
241
242     i = BN_num_bits(dsa->q);
243     /* fips 186-3 allows only different sizes for q */
244     if (i != 160 && i != 224 && i != 256) {
245         DSAerr(DSA_F_DSA_DO_VERIFY, DSA_R_BAD_Q_VALUE);
246         return -1;
247     }
248
249     if (BN_num_bits(dsa->p) > OPENSSL_DSA_MAX_MODULUS_BITS) {
250         DSAerr(DSA_F_DSA_DO_VERIFY, DSA_R_MODULUS_TOO_LARGE);
251         return -1;
252     }
253     u1 = BN_new();
254     u2 = BN_new();
255     t1 = BN_new();
256     ctx = BN_CTX_new();
257     if (u1 == NULL || u2 == NULL || t1 == NULL || ctx == NULL)
258         goto err;
259
260     DSA_SIG_get0(&r, &s, sig);
261
262     if (BN_is_zero(r) || BN_is_negative(r) ||
263         BN_ucmp(r, dsa->q) >= 0) {
264         ret = 0;
265         goto err;
266     }
267     if (BN_is_zero(s) || BN_is_negative(s) ||
268         BN_ucmp(s, dsa->q) >= 0) {
269         ret = 0;
270         goto err;
271     }
272
273     /*
274      * Calculate W = inv(S) mod Q save W in u2
275      */
276     if ((BN_mod_inverse(u2, s, dsa->q, ctx)) == NULL)
277         goto err;
278
279     /* save M in u1 */
280     if (dgst_len > (i >> 3))
281         /*
282          * if the digest length is greater than the size of q use the
283          * BN_num_bits(dsa->q) leftmost bits of the digest, see fips 186-3,
284          * 4.2
285          */
286         dgst_len = (i >> 3);
287     if (BN_bin2bn(dgst, dgst_len, u1) == NULL)
288         goto err;
289
290     /* u1 = M * w mod q */
291     if (!BN_mod_mul(u1, u1, u2, dsa->q, ctx))
292         goto err;
293
294     /* u2 = r * w mod q */
295     if (!BN_mod_mul(u2, r, u2, dsa->q, ctx))
296         goto err;
297
298     if (dsa->flags & DSA_FLAG_CACHE_MONT_P) {
299         mont = BN_MONT_CTX_set_locked(&dsa->method_mont_p,
300                                       dsa->lock, dsa->p, ctx);
301         if (!mont)
302             goto err;
303     }
304
305     if (dsa->meth->dsa_mod_exp != NULL) {
306         if (!dsa->meth->dsa_mod_exp(dsa, t1, dsa->g, u1, dsa->pub_key, u2,
307                                     dsa->p, ctx, mont))
308             goto err;
309     } else {
310         if (!BN_mod_exp2_mont(t1, dsa->g, u1, dsa->pub_key, u2, dsa->p, ctx,
311                               mont))
312             goto err;
313     }
314
315     /* let u1 = u1 mod q */
316     if (!BN_mod(u1, t1, dsa->q, ctx))
317         goto err;
318
319     /*
320      * V is now in u1.  If the signature is correct, it will be equal to R.
321      */
322     ret = (BN_ucmp(u1, r) == 0);
323
324  err:
325     if (ret < 0)
326         DSAerr(DSA_F_DSA_DO_VERIFY, ERR_R_BN_LIB);
327     BN_CTX_free(ctx);
328     BN_free(u1);
329     BN_free(u2);
330     BN_free(t1);
331     return (ret);
332 }
333
334 static int dsa_init(DSA *dsa)
335 {
336     dsa->flags |= DSA_FLAG_CACHE_MONT_P;
337     return (1);
338 }
339
340 static int dsa_finish(DSA *dsa)
341 {
342     BN_MONT_CTX_free(dsa->method_mont_p);
343     return (1);
344 }