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