e46946153be195eb6fca577cd805db013e413242
[openssl.git] / crypto / dh / dh_key.c
1 /*
2  * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (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 /*
11  * DH low level APIs are deprecated for public use, but still ok for
12  * internal use.
13  */
14 #include "internal/deprecated.h"
15
16 #include <stdio.h>
17 #include "internal/cryptlib.h"
18 #include "dh_local.h"
19 #include "crypto/bn.h"
20 #include "crypto/dh.h"
21
22 #ifdef FIPS_MODE
23 # define MIN_STRENGTH 112
24 #else
25 # define MIN_STRENGTH 80
26 #endif
27
28 static int generate_key(DH *dh);
29 static int dh_bn_mod_exp(const DH *dh, BIGNUM *r,
30                          const BIGNUM *a, const BIGNUM *p,
31                          const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
32 static int dh_init(DH *dh);
33 static int dh_finish(DH *dh);
34
35 static int compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
36 {
37     BN_CTX *ctx = NULL;
38     BN_MONT_CTX *mont = NULL;
39     BIGNUM *tmp;
40     int ret = -1;
41 #ifndef FIPS_MODE
42     int check_result;
43 #endif
44
45     if (BN_num_bits(dh->params.p) > OPENSSL_DH_MAX_MODULUS_BITS) {
46         DHerr(0, DH_R_MODULUS_TOO_LARGE);
47         goto err;
48     }
49
50     if (BN_num_bits(dh->params.p) < DH_MIN_MODULUS_BITS) {
51         DHerr(0, DH_R_MODULUS_TOO_SMALL);
52         return 0;
53     }
54
55     ctx = BN_CTX_new_ex(dh->libctx);
56     if (ctx == NULL)
57         goto err;
58     BN_CTX_start(ctx);
59     tmp = BN_CTX_get(ctx);
60     if (tmp == NULL)
61         goto err;
62
63     if (dh->priv_key == NULL) {
64         DHerr(0, DH_R_NO_PRIVATE_VALUE);
65         goto err;
66     }
67
68     if (dh->flags & DH_FLAG_CACHE_MONT_P) {
69         mont = BN_MONT_CTX_set_locked(&dh->method_mont_p,
70                                       dh->lock, dh->params.p, ctx);
71         BN_set_flags(dh->priv_key, BN_FLG_CONSTTIME);
72         if (!mont)
73             goto err;
74     }
75 /* TODO(3.0) : Solve in a PR related to Key validation for DH */
76 #ifndef FIPS_MODE
77     if (!DH_check_pub_key(dh, pub_key, &check_result) || check_result) {
78         DHerr(0, DH_R_INVALID_PUBKEY);
79         goto err;
80     }
81 #endif
82     if (!dh->meth->bn_mod_exp(dh, tmp, pub_key, dh->priv_key, dh->params.p, ctx,
83                               mont)) {
84         DHerr(0, ERR_R_BN_LIB);
85         goto err;
86     }
87
88     ret = BN_bn2bin(tmp, key);
89  err:
90     BN_CTX_end(ctx);
91     BN_CTX_free(ctx);
92     return ret;
93 }
94
95 int DH_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
96 {
97 #ifdef FIPS_MODE
98     return compute_key(key, pub_key, dh);
99 #else
100     return dh->meth->compute_key(key, pub_key, dh);
101 #endif
102 }
103
104 int DH_compute_key_padded(unsigned char *key, const BIGNUM *pub_key, DH *dh)
105 {
106     int rv, pad;
107
108 #ifdef FIPS_MODE
109     rv = compute_key(key, pub_key, dh);
110 #else
111     rv = dh->meth->compute_key(key, pub_key, dh);
112 #endif
113     if (rv <= 0)
114         return rv;
115     pad = BN_num_bytes(dh->params.p) - rv;
116     if (pad > 0) {
117         memmove(key + pad, key, rv);
118         memset(key, 0, pad);
119     }
120     return rv + pad;
121 }
122
123 static DH_METHOD dh_ossl = {
124     "OpenSSL DH Method",
125     generate_key,
126     compute_key,
127     dh_bn_mod_exp,
128     dh_init,
129     dh_finish,
130     DH_FLAG_FIPS_METHOD,
131     NULL,
132     NULL
133 };
134
135 static const DH_METHOD *default_DH_method = &dh_ossl;
136
137 const DH_METHOD *DH_OpenSSL(void)
138 {
139     return &dh_ossl;
140 }
141
142 const DH_METHOD *DH_get_default_method(void)
143 {
144     return default_DH_method;
145 }
146
147 static int dh_bn_mod_exp(const DH *dh, BIGNUM *r,
148                          const BIGNUM *a, const BIGNUM *p,
149                          const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
150 {
151     return BN_mod_exp_mont(r, a, p, m, ctx, m_ctx);
152 }
153
154 static int dh_init(DH *dh)
155 {
156     dh->flags |= DH_FLAG_CACHE_MONT_P;
157     ffc_params_init(&dh->params);
158     dh->dirty_cnt++;
159     return 1;
160 }
161
162 static int dh_finish(DH *dh)
163 {
164     BN_MONT_CTX_free(dh->method_mont_p);
165     return 1;
166 }
167
168 #ifndef FIPS_MODE
169 void DH_set_default_method(const DH_METHOD *meth)
170 {
171     default_DH_method = meth;
172 }
173 #endif /* FIPS_MODE */
174
175 int DH_generate_key(DH *dh)
176 {
177 #ifdef FIPS_MODE
178     return generate_key(dh);
179 #else
180     return dh->meth->generate_key(dh);
181 #endif
182 }
183
184 int dh_generate_public_key(BN_CTX *ctx, DH *dh, const BIGNUM *priv_key,
185                            BIGNUM *pub_key)
186 {
187     int ret = 0;
188     BIGNUM *prk = BN_new();
189     BN_MONT_CTX *mont = NULL;
190
191     if (prk == NULL)
192         return 0;
193
194     if (dh->flags & DH_FLAG_CACHE_MONT_P) {
195         mont = BN_MONT_CTX_set_locked(&dh->method_mont_p,
196                                       dh->lock, dh->params.p, ctx);
197         if (mont == NULL)
198             goto err;
199     }
200     BN_with_flags(prk, priv_key, BN_FLG_CONSTTIME);
201
202     /* pub_key = g^priv_key mod p */
203     if (!dh->meth->bn_mod_exp(dh, pub_key, dh->params.g, prk, dh->params.p,
204                               ctx, mont))
205         goto err;
206     ret = 1;
207 err:
208     BN_clear_free(prk);
209     return ret;
210 }
211
212 static int generate_key(DH *dh)
213 {
214     int ok = 0;
215     int generate_new_key = 0;
216 #ifndef FIPS_MODE
217     unsigned l;
218 #endif
219     BN_CTX *ctx = NULL;
220     BIGNUM *pub_key = NULL, *priv_key = NULL;
221
222     if (BN_num_bits(dh->params.p) > OPENSSL_DH_MAX_MODULUS_BITS) {
223         DHerr(0, DH_R_MODULUS_TOO_LARGE);
224         return 0;
225     }
226
227     if (BN_num_bits(dh->params.p) < DH_MIN_MODULUS_BITS) {
228         DHerr(0, DH_R_MODULUS_TOO_SMALL);
229         return 0;
230     }
231
232     ctx = BN_CTX_new_ex(dh->libctx);
233     if (ctx == NULL)
234         goto err;
235
236     if (dh->priv_key == NULL) {
237         priv_key = BN_secure_new();
238         if (priv_key == NULL)
239             goto err;
240         generate_new_key = 1;
241     } else {
242         priv_key = dh->priv_key;
243     }
244
245     if (dh->pub_key == NULL) {
246         pub_key = BN_new();
247         if (pub_key == NULL)
248             goto err;
249     } else {
250         pub_key = dh->pub_key;
251     }
252     if (generate_new_key) {
253         /* Is it an approved safe prime ?*/
254         if (DH_get_nid(dh) != NID_undef) {
255             /*
256              * The safe prime group code sets N = 2*s
257              * (where s = max security strength supported).
258              * N = dh->length (N = maximum bit length of private key)
259              */
260             if (dh->params.q == NULL
261                 || dh->length > BN_num_bits(dh->params.q))
262                 goto err;
263             if (!ffc_generate_private_key(ctx, &dh->params, dh->length,
264                                           dh->length / 2, priv_key))
265                 goto err;
266         } else {
267 #ifdef FIPS_MODE
268             if (dh->params.q == NULL)
269                 goto err;
270 #else
271             if (dh->params.q == NULL) {
272                 /* secret exponent length */
273                 l = dh->length ? dh->length : BN_num_bits(dh->params.p) - 1;
274                 if (!BN_priv_rand_ex(priv_key, l, BN_RAND_TOP_ONE,
275                                      BN_RAND_BOTTOM_ANY, ctx))
276                     goto err;
277                 /*
278                  * We handle just one known case where g is a quadratic non-residue:
279                  * for g = 2: p % 8 == 3
280                  */
281                 if (BN_is_word(dh->params.g, DH_GENERATOR_2)
282                     && !BN_is_bit_set(dh->params.p, 2)) {
283                     /* clear bit 0, since it won't be a secret anyway */
284                     if (!BN_clear_bit(priv_key, 0))
285                         goto err;
286                 }
287             } else
288 #endif
289             {
290                 /*
291                  * For FFC FIPS 186-4 keygen
292                  * security strength s = 112,
293                  * Max Private key size N = len(q)
294                  */
295                 if (!ffc_generate_private_key(ctx, &dh->params,
296                                               BN_num_bits(dh->params.q),
297                                               MIN_STRENGTH,
298                                               priv_key))
299                     goto err;
300             }
301         }
302     }
303
304     if (!dh_generate_public_key(ctx, dh, priv_key, pub_key))
305         goto err;
306
307     dh->pub_key = pub_key;
308     dh->priv_key = priv_key;
309     dh->dirty_cnt++;
310     ok = 1;
311  err:
312     if (ok != 1)
313         DHerr(0, ERR_R_BN_LIB);
314
315     if (pub_key != dh->pub_key)
316         BN_free(pub_key);
317     if (priv_key != dh->priv_key)
318         BN_free(priv_key);
319     BN_CTX_free(ctx);
320     return ok;
321 }
322
323 int dh_buf2key(DH *dh, const unsigned char *buf, size_t len)
324 {
325     int err_reason = DH_R_BN_ERROR;
326     BIGNUM *pubkey = NULL;
327     const BIGNUM *p;
328     size_t p_size;
329
330     if ((pubkey = BN_bin2bn(buf, len, NULL)) == NULL)
331         goto err;
332     DH_get0_pqg(dh, &p, NULL, NULL);
333     if (p == NULL || (p_size = BN_num_bytes(p)) == 0) {
334         err_reason = DH_R_NO_PARAMETERS_SET;
335         goto err;
336     }
337     /*
338      * As per Section 4.2.8.1 of RFC 8446 fail if DHE's
339      * public key is of size not equal to size of p
340      */
341     if (BN_is_zero(pubkey) || p_size != len) {
342         err_reason = DH_R_INVALID_PUBKEY;
343         goto err;
344     }
345     if (DH_set0_key(dh, pubkey, NULL) != 1)
346         goto err;
347     return 1;
348 err:
349     DHerr(DH_F_DH_BUF2KEY, err_reason);
350     BN_free(pubkey);
351     return 0;
352 }
353
354 size_t dh_key2buf(const DH *dh, unsigned char **pbuf_out)
355 {
356     const BIGNUM *pubkey;
357     unsigned char *pbuf;
358     const BIGNUM *p;
359     int p_size;
360
361     DH_get0_pqg(dh, &p, NULL, NULL);
362     DH_get0_key(dh, &pubkey, NULL);
363     if (p == NULL || pubkey == NULL
364             || (p_size = BN_num_bytes(p)) == 0
365             || BN_num_bytes(pubkey) == 0) {
366         DHerr(DH_F_DH_KEY2BUF, DH_R_INVALID_PUBKEY);
367         return 0;
368     }
369     if ((pbuf = OPENSSL_malloc(p_size)) == NULL) {
370         DHerr(DH_F_DH_KEY2BUF, ERR_R_MALLOC_FAILURE);
371         return 0;
372     }
373     /*
374      * As per Section 4.2.8.1 of RFC 8446 left pad public
375      * key with zeros to the size of p
376      */
377     if (BN_bn2binpad(pubkey, pbuf, p_size) < 0) {
378         OPENSSL_free(pbuf);
379         DHerr(DH_F_DH_KEY2BUF, DH_R_BN_ERROR);
380         return 0;
381     }
382     *pbuf_out = pbuf;
383     return p_size;
384 }