]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/dsa/dsa_lib.c
Rename FIPS_MODE to FIPS_MODULE
[thirdparty/openssl.git] / crypto / dsa / dsa_lib.c
CommitLineData
d2e9e320 1/*
dc8de3e6 2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
d02b48c6 3 *
3cdbea65 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
d2e9e320
RS
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
d02b48c6
RE
8 */
9
f41ac0ee
P
10/*
11 * DSA low level APIs are deprecated for public use, but still ok for
12 * internal use.
13 */
14#include "internal/deprecated.h"
15
d02b48c6 16#include <stdio.h>
b39fc560 17#include "internal/cryptlib.h"
cd420b0b 18#include "internal/refcount.h"
ec577822 19#include <openssl/bn.h>
ec577822 20#include <openssl/asn1.h>
3c27208f 21#include <openssl/engine.h>
b03ec3b5 22#include <openssl/core_names.h>
dc8de3e6 23#include "dsa_local.h"
b03ec3b5 24#include "crypto/evp.h"
e683582b 25#include "crypto/dsa.h"
dc8de3e6 26#include "crypto/dh.h" /* required by DSA_dup_DH() */
e683582b 27
8083fd3a
SL
28static DSA *dsa_new_intern(ENGINE *engine, OPENSSL_CTX *libctx);
29
f844f9eb 30#ifndef FIPS_MODULE
d02b48c6 31
e683582b
SL
32int DSA_set_ex_data(DSA *d, int idx, void *arg)
33{
34 return CRYPTO_set_ex_data(&d->ex_data, idx, arg);
35}
36
8cc86b81 37void *DSA_get_ex_data(const DSA *d, int idx)
e683582b
SL
38{
39 return CRYPTO_get_ex_data(&d->ex_data, idx);
40}
41
dc8de3e6 42# ifndef OPENSSL_NO_DH
e683582b
SL
43DH *DSA_dup_DH(const DSA *r)
44{
45 /*
dc8de3e6
SL
46 * DSA has p, q, g, optional pub_key, optional priv_key.
47 * DH has p, optional length, g, optional pub_key,
48 * optional priv_key, optional q.
e683582b 49 */
e683582b 50 DH *ret = NULL;
dc8de3e6 51 BIGNUM *pub_key = NULL, *priv_key = NULL;
e683582b
SL
52
53 if (r == NULL)
54 goto err;
55 ret = DH_new();
56 if (ret == NULL)
57 goto err;
dc8de3e6
SL
58
59 if (!ffc_params_copy(dh_get0_params(ret), &r->params))
60 goto err;
e683582b
SL
61
62 if (r->pub_key != NULL) {
63 pub_key = BN_dup(r->pub_key);
64 if (pub_key == NULL)
65 goto err;
66 if (r->priv_key != NULL) {
67 priv_key = BN_dup(r->priv_key);
68 if (priv_key == NULL)
69 goto err;
70 }
71 if (!DH_set0_key(ret, pub_key, priv_key))
72 goto err;
73 } else if (r->priv_key != NULL) {
74 /* Shouldn't happen */
75 goto err;
76 }
77
78 return ret;
79
80 err:
e683582b
SL
81 BN_free(pub_key);
82 BN_free(priv_key);
83 DH_free(ret);
84 return NULL;
85}
dc8de3e6 86# endif /* OPENSSL_NO_DH */
e683582b 87
e683582b
SL
88void DSA_clear_flags(DSA *d, int flags)
89{
90 d->flags &= ~flags;
91}
92
93int DSA_test_flags(const DSA *d, int flags)
94{
95 return d->flags & flags;
96}
97
98void DSA_set_flags(DSA *d, int flags)
99{
100 d->flags |= flags;
101}
102
103ENGINE *DSA_get0_engine(DSA *d)
104{
105 return d->engine;
106}
107
cb78486d 108int DSA_set_method(DSA *dsa, const DSA_METHOD *meth)
0f113f3e
MC
109{
110 /*
111 * NB: The caller is specifically setting a method, so it's not up to us
112 * to deal with which ENGINE it comes from.
113 */
114 const DSA_METHOD *mtmp;
115 mtmp = dsa->meth;
116 if (mtmp->finish)
117 mtmp->finish(dsa);
0b13e9f0 118#ifndef OPENSSL_NO_ENGINE
7c96dbcd
RS
119 ENGINE_finish(dsa->engine);
120 dsa->engine = NULL;
0b13e9f0 121#endif
0f113f3e
MC
122 dsa->meth = meth;
123 if (meth->init)
124 meth->init(dsa);
125 return 1;
126}
f844f9eb 127#endif /* FIPS_MODULE */
e683582b 128
c0711f7f 129
6e9fa57c
MC
130const DSA_METHOD *DSA_get_method(DSA *d)
131{
132 return d->meth;
133}
134
8083fd3a 135static DSA *dsa_new_intern(ENGINE *engine, OPENSSL_CTX *libctx)
0f113f3e 136{
2bbf0baa 137 DSA *ret = OPENSSL_zalloc(sizeof(*ret));
0f113f3e 138
0f113f3e 139 if (ret == NULL) {
8083fd3a 140 DSAerr(0, ERR_R_MALLOC_FAILURE);
d188a536 141 return NULL;
0f113f3e 142 }
2bbf0baa
F
143
144 ret->references = 1;
145 ret->lock = CRYPTO_THREAD_lock_new();
146 if (ret->lock == NULL) {
8083fd3a 147 DSAerr(0, ERR_R_MALLOC_FAILURE);
2bbf0baa
F
148 OPENSSL_free(ret);
149 return NULL;
150 }
151
8083fd3a 152 ret->libctx = libctx;
0f113f3e 153 ret->meth = DSA_get_default_method();
f844f9eb 154#if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
2bbf0baa 155 ret->flags = ret->meth->flags & ~DSA_FLAG_NON_FIPS_ALLOW; /* early default init */
0f113f3e
MC
156 if (engine) {
157 if (!ENGINE_init(engine)) {
8083fd3a 158 DSAerr(0, ERR_R_ENGINE_LIB);
2bbf0baa 159 goto err;
0f113f3e
MC
160 }
161 ret->engine = engine;
162 } else
163 ret->engine = ENGINE_get_default_DSA();
164 if (ret->engine) {
165 ret->meth = ENGINE_get_DSA(ret->engine);
7c96dbcd 166 if (ret->meth == NULL) {
8083fd3a 167 DSAerr(0, ERR_R_ENGINE_LIB);
2bbf0baa 168 goto err;
0f113f3e
MC
169 }
170 }
0b13e9f0 171#endif
0c9de428 172
0f113f3e 173 ret->flags = ret->meth->flags & ~DSA_FLAG_NON_FIPS_ALLOW;
d188a536 174
f844f9eb 175#ifndef FIPS_MODULE
e683582b 176 if (!crypto_new_ex_data_ex(libctx, CRYPTO_EX_INDEX_DSA, ret, &ret->ex_data))
2bbf0baa 177 goto err;
a3327784 178#endif
d188a536
AG
179
180 if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
8083fd3a 181 DSAerr(0, ERR_R_INIT_FAIL);
544648a8 182 goto err;
0f113f3e
MC
183 }
184
d188a536 185 return ret;
544648a8
NT
186
187 err:
188 DSA_free(ret);
189 return NULL;
0f113f3e 190}
d02b48c6 191
e683582b
SL
192DSA *DSA_new_method(ENGINE *engine)
193{
8083fd3a
SL
194 return dsa_new_intern(engine, NULL);
195}
196
197DSA *dsa_new_with_ctx(OPENSSL_CTX *libctx)
198{
199 return dsa_new_intern(NULL, libctx);
e683582b
SL
200}
201
f844f9eb 202#ifndef FIPS_MODULE
a3327784 203DSA *DSA_new(void)
e683582b 204{
8083fd3a 205 return dsa_new_intern(NULL, NULL);
e683582b 206}
8083fd3a 207#endif
e683582b 208
6b691a5c 209void DSA_free(DSA *r)
0f113f3e
MC
210{
211 int i;
d02b48c6 212
0f113f3e
MC
213 if (r == NULL)
214 return;
d02b48c6 215
2f545ae4 216 CRYPTO_DOWN_REF(&r->references, &i, r->lock);
f3f1cf84 217 REF_PRINT_COUNT("DSA", r);
0f113f3e
MC
218 if (i > 0)
219 return;
f3f1cf84 220 REF_ASSERT_ISNT(i < 0);
d02b48c6 221
0c5d725e 222 if (r->meth != NULL && r->meth->finish != NULL)
0f113f3e 223 r->meth->finish(r);
f844f9eb 224#if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
17fa4e8e 225 ENGINE_finish(r->engine);
0b13e9f0 226#endif
c0711f7f 227
f844f9eb 228#ifndef FIPS_MODULE
0f113f3e 229 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, r, &r->ex_data);
a3327784 230#endif
0f113f3e 231
d188a536
AG
232 CRYPTO_THREAD_lock_free(r->lock);
233
dc8de3e6 234 ffc_params_cleanup(&r->params);
23a1d5e9
RS
235 BN_clear_free(r->pub_key);
236 BN_clear_free(r->priv_key);
0f113f3e
MC
237 OPENSSL_free(r);
238}
d02b48c6 239
6ac4e8bd 240int DSA_up_ref(DSA *r)
0f113f3e 241{
d188a536
AG
242 int i;
243
2f545ae4 244 if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0)
d188a536 245 return 0;
f3f1cf84
RS
246
247 REF_PRINT_COUNT("DSA", r);
248 REF_ASSERT_ISNT(i < 2);
0f113f3e
MC
249 return ((i > 1) ? 1 : 0);
250}
5cbc2e8b 251
fd809cfd
RL
252void DSA_get0_pqg(const DSA *d,
253 const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
1258396d 254{
dc8de3e6 255 ffc_params_get0_pqg(&d->params, p, q, g);
1258396d
MC
256}
257
258int DSA_set0_pqg(DSA *d, BIGNUM *p, BIGNUM *q, BIGNUM *g)
259{
fd809cfd 260 /* If the fields p, q and g in d are NULL, the corresponding input
1da12e34 261 * parameters MUST be non-NULL.
1da12e34 262 */
dc8de3e6
SL
263 if ((d->params.p == NULL && p == NULL)
264 || (d->params.q == NULL && q == NULL)
265 || (d->params.g == NULL && g == NULL))
1258396d 266 return 0;
1da12e34 267
dc8de3e6 268 ffc_params_set0_pqg(&d->params, p, q, g);
4889dadc 269 d->dirty_cnt++;
1258396d
MC
270
271 return 1;
272}
273
b305452f
RL
274const BIGNUM *DSA_get0_p(const DSA *d)
275{
276 return d->params.p;
277}
278
279const BIGNUM *DSA_get0_q(const DSA *d)
280{
281 return d->params.q;
282}
283
284const BIGNUM *DSA_get0_g(const DSA *d)
285{
286 return d->params.g;
287}
288
289const BIGNUM *DSA_get0_pub_key(const DSA *d)
290{
291 return d->pub_key;
292}
293
294const BIGNUM *DSA_get0_priv_key(const DSA *d)
295{
296 return d->priv_key;
297}
298
fd809cfd
RL
299void DSA_get0_key(const DSA *d,
300 const BIGNUM **pub_key, const BIGNUM **priv_key)
1258396d 301{
6e9fa57c
MC
302 if (pub_key != NULL)
303 *pub_key = d->pub_key;
304 if (priv_key != NULL)
305 *priv_key = d->priv_key;
1258396d
MC
306}
307
6e9fa57c 308int DSA_set0_key(DSA *d, BIGNUM *pub_key, BIGNUM *priv_key)
1258396d 309{
fd809cfd 310 /* If the field pub_key in d is NULL, the corresponding input
1da12e34
RL
311 * parameters MUST be non-NULL. The priv_key field may
312 * be left NULL.
1da12e34 313 */
fd809cfd 314 if (d->pub_key == NULL && pub_key == NULL)
1258396d
MC
315 return 0;
316
1da12e34
RL
317 if (pub_key != NULL) {
318 BN_free(d->pub_key);
319 d->pub_key = pub_key;
320 }
321 if (priv_key != NULL) {
322 BN_free(d->priv_key);
323 d->priv_key = priv_key;
324 }
4889dadc 325 d->dirty_cnt++;
1258396d
MC
326
327 return 1;
328}
329
806253f3
RL
330int DSA_security_bits(const DSA *d)
331{
dc8de3e6
SL
332 if (d->params.p != NULL && d->params.q != NULL)
333 return BN_security_bits(BN_num_bits(d->params.p),
334 BN_num_bits(d->params.q));
806253f3
RL
335 return -1;
336}
337
338int DSA_bits(const DSA *dsa)
339{
dc8de3e6 340 return BN_num_bits(dsa->params.p);
806253f3 341}
2888fc15
RL
342
343FFC_PARAMS *dsa_get0_params(DSA *dsa)
344{
345 return &dsa->params;
346}
b03ec3b5
SL
347
348int dsa_ffc_params_fromdata(DSA *dsa, const OSSL_PARAM params[])
349{
350 int ret;
351 FFC_PARAMS *ffc;
352
353 if (dsa == NULL)
354 return 0;
355 ffc = dsa_get0_params(dsa);
356 if (ffc == NULL)
357 return 0;
358
359 ret = ffc_params_fromdata(ffc, params);
360 if (ret)
361 dsa->dirty_cnt++;
362 return ret;
363}
364
365static int dsa_paramgen_check(EVP_PKEY_CTX *ctx)
366{
367 if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
368 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
369 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
370 return -2;
371 }
372 /* If key type not DSA return error */
373 if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_DSA)
374 return -1;
375 return 1;
376}
377
378int EVP_PKEY_CTX_set_dsa_paramgen_type(EVP_PKEY_CTX *ctx, const char *name)
379{
380 int ret;
381 OSSL_PARAM params[2], *p = params;
382
383 if ((ret = dsa_paramgen_check(ctx)) <= 0)
384 return ret;
385
386 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_FFC_TYPE,
387 (char *)name, 0);
388 *p++ = OSSL_PARAM_construct_end();
389
390 return EVP_PKEY_CTX_set_params(ctx, params);
391}
392
393int EVP_PKEY_CTX_set_dsa_paramgen_gindex(EVP_PKEY_CTX *ctx, int gindex)
394{
395 int ret;
396 OSSL_PARAM params[2], *p = params;
397
398 if ((ret = dsa_paramgen_check(ctx)) <= 0)
399 return ret;
400
401 *p++ = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_FFC_GINDEX, &gindex);
402 *p++ = OSSL_PARAM_construct_end();
403
404 return EVP_PKEY_CTX_set_params(ctx, params);
405}
406
407int EVP_PKEY_CTX_set_dsa_paramgen_seed(EVP_PKEY_CTX *ctx,
408 const unsigned char *seed,
409 size_t seedlen)
410{
411 int ret;
412 OSSL_PARAM params[2], *p = params;
413
414 if ((ret = dsa_paramgen_check(ctx)) <= 0)
415 return ret;
416
417 *p++ = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_FFC_SEED,
418 (void *)seed, seedlen);
419 *p++ = OSSL_PARAM_construct_end();
420
421 return EVP_PKEY_CTX_set_params(ctx, params);
422}
423
424int EVP_PKEY_CTX_set_dsa_paramgen_bits(EVP_PKEY_CTX *ctx, int nbits)
425{
426 int ret;
427 OSSL_PARAM params[2], *p = params;
428 size_t bits = nbits;
429
430 if ((ret = dsa_paramgen_check(ctx)) <= 0)
431 return ret;
432
f844f9eb 433#if !defined(FIPS_MODULE)
b03ec3b5
SL
434 /* TODO(3.0): Remove this eventually when no more legacy */
435 if (ctx->op.keymgmt.genctx == NULL)
436 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DSA, EVP_PKEY_OP_PARAMGEN,
437 EVP_PKEY_CTRL_DSA_PARAMGEN_BITS, nbits, NULL);
438#endif
439
440 *p++ = OSSL_PARAM_construct_size_t(OSSL_PKEY_PARAM_FFC_PBITS, &bits);
441 *p++ = OSSL_PARAM_construct_end();
442
443 return EVP_PKEY_CTX_set_params(ctx, params);
444}
445
446int EVP_PKEY_CTX_set_dsa_paramgen_q_bits(EVP_PKEY_CTX *ctx, int qbits)
447{
448 int ret;
449 OSSL_PARAM params[2], *p = params;
450 size_t bits2 = qbits;
451
452 if ((ret = dsa_paramgen_check(ctx)) <= 0)
453 return ret;
454
f844f9eb 455#if !defined(FIPS_MODULE)
b03ec3b5
SL
456 /* TODO(3.0): Remove this eventually when no more legacy */
457 if (ctx->op.keymgmt.genctx == NULL)
458 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DSA, EVP_PKEY_OP_PARAMGEN,
459 EVP_PKEY_CTRL_DSA_PARAMGEN_Q_BITS, qbits, NULL);
460#endif
461
462 *p++ = OSSL_PARAM_construct_size_t(OSSL_PKEY_PARAM_FFC_QBITS, &bits2);
463 *p++ = OSSL_PARAM_construct_end();
464
465 return EVP_PKEY_CTX_set_params(ctx, params);
466}
467
468int EVP_PKEY_CTX_set_dsa_paramgen_md_props(EVP_PKEY_CTX *ctx,
469 const char *md_name,
470 const char *md_properties)
471{
472 int ret;
473 OSSL_PARAM params[3], *p = params;
474
475 if ((ret = dsa_paramgen_check(ctx)) <= 0)
476 return ret;
477
f844f9eb 478#if !defined(FIPS_MODULE)
b03ec3b5
SL
479 /* TODO(3.0): Remove this eventually when no more legacy */
480 if (ctx->op.keymgmt.genctx == NULL) {
481 const EVP_MD *md = EVP_get_digestbyname(md_name);
482
483 EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DSA, EVP_PKEY_OP_PARAMGEN,
484 EVP_PKEY_CTRL_DSA_PARAMGEN_MD, 0, (void *)(md));
485 }
486#endif
487
488 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_FFC_DIGEST,
489 (char *)md_name, 0);
490 if (md_properties != NULL)
491 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_FFC_DIGEST_PROPS,
492 (char *)md_properties, 0);
493 *p++ = OSSL_PARAM_construct_end();
494
495 return EVP_PKEY_CTX_set_params(ctx, params);
496}
497
f844f9eb 498#if !defined(FIPS_MODULE)
b03ec3b5
SL
499int EVP_PKEY_CTX_set_dsa_paramgen_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
500{
501 const char *md_name = (md == NULL) ? "" : EVP_MD_name(md);
502
503 return EVP_PKEY_CTX_set_dsa_paramgen_md_props(ctx, md_name, NULL);
504}
505#endif