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