Rename FIPS_MODE to FIPS_MODULE
[openssl.git] / crypto / dh / dh_lib.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 <openssl/bn.h>
18 #include <openssl/engine.h>
19 #include <openssl/obj_mac.h>
20 #include <openssl/core_names.h>
21 #include "internal/cryptlib.h"
22 #include "internal/refcount.h"
23 #include "crypto/evp.h"
24 #include "crypto/dh.h"
25 #include "dh_local.h"
26
27 static DH *dh_new_intern(ENGINE *engine, OPENSSL_CTX *libctx);
28
29 #ifndef FIPS_MODULE
30 int DH_set_method(DH *dh, const DH_METHOD *meth)
31 {
32     /*
33      * NB: The caller is specifically setting a method, so it's not up to us
34      * to deal with which ENGINE it comes from.
35      */
36     const DH_METHOD *mtmp;
37     mtmp = dh->meth;
38     if (mtmp->finish)
39         mtmp->finish(dh);
40 #ifndef OPENSSL_NO_ENGINE
41     ENGINE_finish(dh->engine);
42     dh->engine = NULL;
43 #endif
44     dh->meth = meth;
45     if (meth->init)
46         meth->init(dh);
47     return 1;
48 }
49
50 const DH_METHOD *dh_get_method(const DH *dh)
51 {
52     return dh->meth;
53 }
54
55 DH *DH_new(void)
56 {
57     return dh_new_intern(NULL, NULL);
58 }
59
60 DH *DH_new_method(ENGINE *engine)
61 {
62     return dh_new_intern(engine, NULL);
63 }
64 #endif /* !FIPS_MODULE */
65
66 DH *dh_new_with_libctx(OPENSSL_CTX *libctx)
67 {
68     return dh_new_intern(NULL, libctx);
69 }
70
71 static DH *dh_new_intern(ENGINE *engine, OPENSSL_CTX *libctx)
72 {
73     DH *ret = OPENSSL_zalloc(sizeof(*ret));
74
75     if (ret == NULL) {
76         DHerr(0, ERR_R_MALLOC_FAILURE);
77         return NULL;
78     }
79
80     ret->references = 1;
81     ret->lock = CRYPTO_THREAD_lock_new();
82     if (ret->lock == NULL) {
83         DHerr(0, ERR_R_MALLOC_FAILURE);
84         OPENSSL_free(ret);
85         return NULL;
86     }
87
88     ret->libctx = libctx;
89     ret->meth = DH_get_default_method();
90 #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
91     ret->flags = ret->meth->flags;  /* early default init */
92     if (engine) {
93         if (!ENGINE_init(engine)) {
94             DHerr(0, ERR_R_ENGINE_LIB);
95             goto err;
96         }
97         ret->engine = engine;
98     } else
99         ret->engine = ENGINE_get_default_DH();
100     if (ret->engine) {
101         ret->meth = ENGINE_get_DH(ret->engine);
102         if (ret->meth == NULL) {
103             DHerr(0, ERR_R_ENGINE_LIB);
104             goto err;
105         }
106     }
107 #endif
108
109     ret->flags = ret->meth->flags;
110
111 #ifndef FIPS_MODULE
112     if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data))
113         goto err;
114 #endif /* FIPS_MODULE */
115
116     if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
117         DHerr(0, ERR_R_INIT_FAIL);
118         goto err;
119     }
120
121     return ret;
122
123  err:
124     DH_free(ret);
125     return NULL;
126 }
127
128 void DH_free(DH *r)
129 {
130     int i;
131
132     if (r == NULL)
133         return;
134
135     CRYPTO_DOWN_REF(&r->references, &i, r->lock);
136     REF_PRINT_COUNT("DH", r);
137     if (i > 0)
138         return;
139     REF_ASSERT_ISNT(i < 0);
140
141     if (r->meth != NULL && r->meth->finish != NULL)
142         r->meth->finish(r);
143 #if !defined(FIPS_MODULE)
144 # if !defined(OPENSSL_NO_ENGINE)
145     ENGINE_finish(r->engine);
146 # endif
147     CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, r, &r->ex_data);
148 #endif
149
150     CRYPTO_THREAD_lock_free(r->lock);
151
152     ffc_params_cleanup(&r->params);
153     BN_clear_free(r->pub_key);
154     BN_clear_free(r->priv_key);
155     OPENSSL_free(r);
156 }
157
158 int DH_up_ref(DH *r)
159 {
160     int i;
161
162     if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0)
163         return 0;
164
165     REF_PRINT_COUNT("DH", r);
166     REF_ASSERT_ISNT(i < 2);
167     return ((i > 1) ? 1 : 0);
168 }
169
170 #ifndef FIPS_MODULE
171 int DH_set_ex_data(DH *d, int idx, void *arg)
172 {
173     return CRYPTO_set_ex_data(&d->ex_data, idx, arg);
174 }
175
176 void *DH_get_ex_data(const DH *d, int idx)
177 {
178     return CRYPTO_get_ex_data(&d->ex_data, idx);
179 }
180 #endif
181
182 int DH_bits(const DH *dh)
183 {
184     return BN_num_bits(dh->params.p);
185 }
186
187 int DH_size(const DH *dh)
188 {
189     return BN_num_bytes(dh->params.p);
190 }
191
192 int DH_security_bits(const DH *dh)
193 {
194     int N;
195     if (dh->params.q != NULL)
196         N = BN_num_bits(dh->params.q);
197     else if (dh->length)
198         N = dh->length;
199     else
200         N = -1;
201     return BN_security_bits(BN_num_bits(dh->params.p), N);
202 }
203
204 void DH_get0_pqg(const DH *dh,
205                  const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
206 {
207     ffc_params_get0_pqg(&dh->params, p, q, g);
208 }
209
210 int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g)
211 {
212     /*
213      * If the fields p and g in d are NULL, the corresponding input
214      * parameters MUST be non-NULL.  q may remain NULL.
215      */
216     if ((dh->params.p == NULL && p == NULL)
217         || (dh->params.g == NULL && g == NULL))
218         return 0;
219
220     ffc_params_set0_pqg(&dh->params, p, q, g);
221     dh_cache_named_group(dh);
222     if (q != NULL)
223         dh->length = BN_num_bits(q);
224     /*
225      * Check if this is a named group. If it finds a named group then the
226      * 'q' and 'length' value are either already set or are set by the
227      * call.
228      */
229     if (DH_get_nid(dh) == NID_undef) {
230         /* If its not a named group then set the 'length' if q is not NULL */
231         if (q != NULL)
232             dh->length = BN_num_bits(q);
233     }
234     dh->dirty_cnt++;
235     return 1;
236 }
237
238 long DH_get_length(const DH *dh)
239 {
240     return dh->length;
241 }
242
243 int DH_set_length(DH *dh, long length)
244 {
245     dh->length = length;
246     return 1;
247 }
248
249 void DH_get0_key(const DH *dh, const BIGNUM **pub_key, const BIGNUM **priv_key)
250 {
251     if (pub_key != NULL)
252         *pub_key = dh->pub_key;
253     if (priv_key != NULL)
254         *priv_key = dh->priv_key;
255 }
256
257 int DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key)
258 {
259     if (pub_key != NULL) {
260         BN_clear_free(dh->pub_key);
261         dh->pub_key = pub_key;
262     }
263     if (priv_key != NULL) {
264         BN_clear_free(dh->priv_key);
265         dh->priv_key = priv_key;
266         dh->length = BN_num_bits(priv_key);
267     }
268
269     dh->dirty_cnt++;
270     return 1;
271 }
272
273 const BIGNUM *DH_get0_p(const DH *dh)
274 {
275     return dh->params.p;
276 }
277
278 const BIGNUM *DH_get0_q(const DH *dh)
279 {
280     return dh->params.q;
281 }
282
283 const BIGNUM *DH_get0_g(const DH *dh)
284 {
285     return dh->params.g;
286 }
287
288 const BIGNUM *DH_get0_priv_key(const DH *dh)
289 {
290     return dh->priv_key;
291 }
292
293 const BIGNUM *DH_get0_pub_key(const DH *dh)
294 {
295     return dh->pub_key;
296 }
297
298 void DH_clear_flags(DH *dh, int flags)
299 {
300     dh->flags &= ~flags;
301 }
302
303 int DH_test_flags(const DH *dh, int flags)
304 {
305     return dh->flags & flags;
306 }
307
308 void DH_set_flags(DH *dh, int flags)
309 {
310     dh->flags |= flags;
311 }
312
313 #ifndef FIPS_MODULE
314 ENGINE *DH_get0_engine(DH *dh)
315 {
316     return dh->engine;
317 }
318 #endif /*FIPS_MODULE */
319
320 FFC_PARAMS *dh_get0_params(DH *dh)
321 {
322     return &dh->params;
323 }
324 int dh_get0_nid(const DH *dh)
325 {
326     return dh->params.nid;
327 }
328
329 int dh_ffc_params_fromdata(DH *dh, const OSSL_PARAM params[])
330 {
331     int ret;
332     FFC_PARAMS *ffc;
333
334     if (dh == NULL)
335         return 0;
336     ffc = dh_get0_params(dh);
337     if (ffc == NULL)
338         return 0;
339
340     ret = ffc_params_fromdata(ffc, params);
341     if (ret) {
342         dh_cache_named_group(dh);
343         dh->dirty_cnt++;
344     }
345     return ret;
346 }
347
348 static int dh_paramgen_check(EVP_PKEY_CTX *ctx)
349 {
350     if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
351         ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
352         /* Uses the same return values as EVP_PKEY_CTX_ctrl */
353         return -2;
354     }
355     /* If key type not DH return error */
356     if (ctx->pmeth != NULL
357         && ctx->pmeth->pkey_id != EVP_PKEY_DH
358         && ctx->pmeth->pkey_id != EVP_PKEY_DHX)
359         return -1;
360     return 1;
361 }
362
363 int EVP_PKEY_CTX_set_dh_paramgen_gindex(EVP_PKEY_CTX *ctx, int gindex)
364 {
365     int ret;
366     OSSL_PARAM params[2], *p = params;
367
368     if ((ret = dh_paramgen_check(ctx)) <= 0)
369         return ret;
370
371     *p++ = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_FFC_GINDEX, &gindex);
372     *p++ = OSSL_PARAM_construct_end();
373
374     return EVP_PKEY_CTX_set_params(ctx, params);
375 }
376
377 int EVP_PKEY_CTX_set_dh_paramgen_seed(EVP_PKEY_CTX *ctx,
378                                       const unsigned char *seed,
379                                       size_t seedlen)
380 {
381     int ret;
382     OSSL_PARAM params[2], *p = params;
383
384     if ((ret = dh_paramgen_check(ctx)) <= 0)
385         return ret;
386
387     *p++ = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_FFC_SEED,
388                                              (void *)seed, seedlen);
389     *p++ = OSSL_PARAM_construct_end();
390
391     return EVP_PKEY_CTX_set_params(ctx, params);
392 }
393
394 int EVP_PKEY_CTX_set_dh_paramgen_type(EVP_PKEY_CTX *ctx, int typ)
395 {
396     int ret;
397     OSSL_PARAM params[2], *p = params;
398     const char *name;
399
400     if ((ret = dh_paramgen_check(ctx)) <= 0)
401         return ret;
402
403 #if !defined(FIPS_MODULE)
404     /* TODO(3.0): Remove this eventually when no more legacy */
405     if (ctx->op.keymgmt.genctx == NULL)
406         return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_PARAMGEN,
407                                  EVP_PKEY_CTRL_DH_PARAMGEN_TYPE, typ, NULL);
408 #endif
409
410     name = dh_gen_type_id2name(typ);
411     if (name == NULL)
412         return 0;
413     *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_FFC_TYPE,
414                                             (char *) name, 0);
415     *p++ = OSSL_PARAM_construct_end();
416
417     return EVP_PKEY_CTX_set_params(ctx, params);
418 }
419
420 int EVP_PKEY_CTX_set_dh_paramgen_prime_len(EVP_PKEY_CTX *ctx, int pbits)
421 {
422     int ret;
423     OSSL_PARAM params[2], *p = params;
424     size_t bits = pbits;
425
426     if ((ret = dh_paramgen_check(ctx)) <= 0)
427         return ret;
428
429 #if !defined(FIPS_MODULE)
430     /* TODO(3.0): Remove this eventually when no more legacy */
431     if (ctx->op.keymgmt.genctx == NULL)
432         return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_PARAMGEN,
433                                  EVP_PKEY_CTRL_DH_PARAMGEN_PRIME_LEN, pbits,
434                                  NULL);
435 #endif
436     *p++ = OSSL_PARAM_construct_size_t(OSSL_PKEY_PARAM_FFC_PBITS, &bits);
437     *p++ = OSSL_PARAM_construct_end();
438     return EVP_PKEY_CTX_set_params(ctx, params);
439 }
440
441 int EVP_PKEY_CTX_set_dh_paramgen_subprime_len(EVP_PKEY_CTX *ctx, int qbits)
442 {
443     int ret;
444     OSSL_PARAM params[2], *p = params;
445     size_t bits2 = qbits;
446
447     if ((ret = dh_paramgen_check(ctx)) <= 0)
448         return ret;
449
450 #if !defined(FIPS_MODULE)
451     /* TODO(3.0): Remove this eventually when no more legacy */
452     if (ctx->op.keymgmt.genctx == NULL)
453         return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_PARAMGEN,
454                                  EVP_PKEY_CTRL_DH_PARAMGEN_SUBPRIME_LEN, qbits,
455                                  NULL);
456 #endif
457     *p++ = OSSL_PARAM_construct_size_t(OSSL_PKEY_PARAM_FFC_QBITS, &bits2);
458     *p++ = OSSL_PARAM_construct_end();
459
460     return EVP_PKEY_CTX_set_params(ctx, params);
461 }
462
463 int EVP_PKEY_CTX_set_dh_paramgen_generator(EVP_PKEY_CTX *ctx, int gen)
464 {
465     int ret;
466     OSSL_PARAM params[2], *p = params;
467
468     if ((ret = dh_paramgen_check(ctx)) <= 0)
469         return ret;
470
471 #if !defined(FIPS_MODULE)
472     /* TODO(3.0): Remove this eventually when no more legacy */
473     if (ctx->op.keymgmt.genctx == NULL)
474         return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_PARAMGEN,
475                                  EVP_PKEY_CTRL_DH_PARAMGEN_GENERATOR, gen, NULL);
476 #endif
477
478     *p++ = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_FFC_GENERATOR, &gen);
479     *p++ = OSSL_PARAM_construct_end();
480
481     return EVP_PKEY_CTX_set_params(ctx, params);
482 }
483
484 int EVP_PKEY_CTX_set_dh_rfc5114(EVP_PKEY_CTX *ctx, int gen)
485 {
486     int ret;
487     OSSL_PARAM params[2], *p = params;
488     const char *name;
489
490     if ((ret = dh_paramgen_check(ctx)) <= 0)
491         return ret;
492
493 #if !defined(FIPS_MODULE)
494     /* TODO(3.0): Remove this eventually when no more legacy */
495     if (ctx->op.keymgmt.genctx == NULL)
496         return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, EVP_PKEY_OP_PARAMGEN,
497                                  EVP_PKEY_CTRL_DH_RFC5114, gen, NULL);
498 #endif
499     name = ffc_named_group_from_uid(gen);
500     if (name == NULL)
501         return 0;
502
503     *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_FFC_GROUP,
504                                             (void *)name, 0);
505     *p++ = OSSL_PARAM_construct_end();
506     return EVP_PKEY_CTX_set_params(ctx, params);
507 }
508
509 int EVP_PKEY_CTX_set_dhx_rfc5114(EVP_PKEY_CTX *ctx, int gen)
510 {
511     return EVP_PKEY_CTX_set_dh_rfc5114(ctx, gen);
512 }
513
514 int EVP_PKEY_CTX_set_dh_nid(EVP_PKEY_CTX *ctx, int nid)
515 {
516     int ret;
517     OSSL_PARAM params[2], *p = params;
518     const char *name;
519
520     if ((ret = dh_paramgen_check(ctx)) <= 0)
521         return ret;
522
523 #if !defined(FIPS_MODULE)
524     /* TODO(3.0): Remove this eventually when no more legacy */
525     if (ctx->op.keymgmt.genctx == NULL)
526         return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH,
527                                  EVP_PKEY_OP_PARAMGEN | EVP_PKEY_OP_KEYGEN,
528                                  EVP_PKEY_CTRL_DH_NID, nid, NULL);
529 #endif
530     name = ffc_named_group_from_uid(nid);
531     if (name == NULL)
532         return 0;
533
534     *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_FFC_GROUP,
535                                             (void *)name, 0);
536     *p++ = OSSL_PARAM_construct_end();
537     return EVP_PKEY_CTX_set_params(ctx, params);
538 }