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