2 * Copyright 2020 The OpenSSL Project Authors. All Rights Reserved.
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
11 #include <openssl/core_names.h>
12 #include <openssl/err.h>
13 #include <openssl/dsa.h>
14 #include <openssl/evp.h>
15 #include "crypto/evp.h"
17 static int dsa_paramgen_check(EVP_PKEY_CTX
*ctx
)
19 if (ctx
== NULL
|| !EVP_PKEY_CTX_IS_GEN_OP(ctx
)) {
20 ERR_raise(ERR_LIB_EVP
, EVP_R_COMMAND_NOT_SUPPORTED
);
21 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
24 /* If key type not DSA return error */
25 if (ctx
->pmeth
!= NULL
&& ctx
->pmeth
->pkey_id
!= EVP_PKEY_DSA
)
30 int EVP_PKEY_CTX_set_dsa_paramgen_type(EVP_PKEY_CTX
*ctx
, const char *name
)
33 OSSL_PARAM params
[2], *p
= params
;
35 if ((ret
= dsa_paramgen_check(ctx
)) <= 0)
38 *p
++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_FFC_TYPE
,
40 *p
++ = OSSL_PARAM_construct_end();
42 return EVP_PKEY_CTX_set_params(ctx
, params
);
45 int EVP_PKEY_CTX_set_dsa_paramgen_gindex(EVP_PKEY_CTX
*ctx
, int gindex
)
48 OSSL_PARAM params
[2], *p
= params
;
50 if ((ret
= dsa_paramgen_check(ctx
)) <= 0)
53 *p
++ = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_FFC_GINDEX
, &gindex
);
54 *p
++ = OSSL_PARAM_construct_end();
56 return EVP_PKEY_CTX_set_params(ctx
, params
);
59 int EVP_PKEY_CTX_set_dsa_paramgen_seed(EVP_PKEY_CTX
*ctx
,
60 const unsigned char *seed
,
64 OSSL_PARAM params
[2], *p
= params
;
66 if ((ret
= dsa_paramgen_check(ctx
)) <= 0)
69 *p
++ = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_FFC_SEED
,
70 (void *)seed
, seedlen
);
71 *p
++ = OSSL_PARAM_construct_end();
73 return EVP_PKEY_CTX_set_params(ctx
, params
);
76 int EVP_PKEY_CTX_set_dsa_paramgen_bits(EVP_PKEY_CTX
*ctx
, int nbits
)
79 OSSL_PARAM params
[2], *p
= params
;
82 if ((ret
= dsa_paramgen_check(ctx
)) <= 0)
85 #if !defined(FIPS_MODULE)
86 /* TODO(3.0): Remove this eventually when no more legacy */
87 if (ctx
->op
.keymgmt
.genctx
== NULL
)
88 return EVP_PKEY_CTX_ctrl(ctx
, EVP_PKEY_DSA
, EVP_PKEY_OP_PARAMGEN
,
89 EVP_PKEY_CTRL_DSA_PARAMGEN_BITS
, nbits
, NULL
);
92 *p
++ = OSSL_PARAM_construct_size_t(OSSL_PKEY_PARAM_FFC_PBITS
, &bits
);
93 *p
++ = OSSL_PARAM_construct_end();
95 return EVP_PKEY_CTX_set_params(ctx
, params
);
98 int EVP_PKEY_CTX_set_dsa_paramgen_q_bits(EVP_PKEY_CTX
*ctx
, int qbits
)
101 OSSL_PARAM params
[2], *p
= params
;
102 size_t bits2
= qbits
;
104 if ((ret
= dsa_paramgen_check(ctx
)) <= 0)
107 #if !defined(FIPS_MODULE)
108 /* TODO(3.0): Remove this eventually when no more legacy */
109 if (ctx
->op
.keymgmt
.genctx
== NULL
)
110 return EVP_PKEY_CTX_ctrl(ctx
, EVP_PKEY_DSA
, EVP_PKEY_OP_PARAMGEN
,
111 EVP_PKEY_CTRL_DSA_PARAMGEN_Q_BITS
, qbits
, NULL
);
114 *p
++ = OSSL_PARAM_construct_size_t(OSSL_PKEY_PARAM_FFC_QBITS
, &bits2
);
115 *p
++ = OSSL_PARAM_construct_end();
117 return EVP_PKEY_CTX_set_params(ctx
, params
);
120 int EVP_PKEY_CTX_set_dsa_paramgen_md_props(EVP_PKEY_CTX
*ctx
,
122 const char *md_properties
)
125 OSSL_PARAM params
[3], *p
= params
;
127 if ((ret
= dsa_paramgen_check(ctx
)) <= 0)
130 #if !defined(FIPS_MODULE)
131 /* TODO(3.0): Remove this eventually when no more legacy */
132 if (ctx
->op
.keymgmt
.genctx
== NULL
) {
133 const EVP_MD
*md
= EVP_get_digestbyname(md_name
);
135 EVP_PKEY_CTX_ctrl(ctx
, EVP_PKEY_DSA
, EVP_PKEY_OP_PARAMGEN
,
136 EVP_PKEY_CTRL_DSA_PARAMGEN_MD
, 0, (void *)(md
));
140 *p
++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_FFC_DIGEST
,
142 if (md_properties
!= NULL
)
143 *p
++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_FFC_DIGEST_PROPS
,
144 (char *)md_properties
, 0);
145 *p
++ = OSSL_PARAM_construct_end();
147 return EVP_PKEY_CTX_set_params(ctx
, params
);
150 #if !defined(FIPS_MODULE)
151 int EVP_PKEY_CTX_set_dsa_paramgen_md(EVP_PKEY_CTX
*ctx
, const EVP_MD
*md
)
153 const char *md_name
= (md
== NULL
) ? "" : EVP_MD_name(md
);
155 return EVP_PKEY_CTX_set_dsa_paramgen_md_props(ctx
, md_name
, NULL
);