Clean away unnecessary length related OSSL_PARAM key names
[openssl.git] / crypto / evp / dsa_ctrl.c
1 /*
2  * Copyright 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 <stdlib.h>
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"
16
17 static int dsa_paramgen_check(EVP_PKEY_CTX *ctx)
18 {
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 */
22         return -2;
23     }
24     /* If key type not DSA return error */
25     if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_DSA)
26         return -1;
27     return 1;
28 }
29
30 int EVP_PKEY_CTX_set_dsa_paramgen_type(EVP_PKEY_CTX *ctx, const char *name)
31 {
32     int ret;
33     OSSL_PARAM params[2], *p = params;
34
35     if ((ret = dsa_paramgen_check(ctx)) <= 0)
36         return ret;
37
38     *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_FFC_TYPE,
39                                             (char *)name, 0);
40     *p++ = OSSL_PARAM_construct_end();
41
42     return EVP_PKEY_CTX_set_params(ctx, params);
43 }
44
45 int EVP_PKEY_CTX_set_dsa_paramgen_gindex(EVP_PKEY_CTX *ctx, int gindex)
46 {
47     int ret;
48     OSSL_PARAM params[2], *p = params;
49
50     if ((ret = dsa_paramgen_check(ctx)) <= 0)
51         return ret;
52
53     *p++ = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_FFC_GINDEX, &gindex);
54     *p++ = OSSL_PARAM_construct_end();
55
56     return EVP_PKEY_CTX_set_params(ctx, params);
57 }
58
59 int EVP_PKEY_CTX_set_dsa_paramgen_seed(EVP_PKEY_CTX *ctx,
60                                        const unsigned char *seed,
61                                        size_t seedlen)
62 {
63     int ret;
64     OSSL_PARAM params[2], *p = params;
65
66     if ((ret = dsa_paramgen_check(ctx)) <= 0)
67         return ret;
68
69     *p++ = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_FFC_SEED,
70                                              (void *)seed, seedlen);
71     *p++ = OSSL_PARAM_construct_end();
72
73     return EVP_PKEY_CTX_set_params(ctx, params);
74 }
75
76 int EVP_PKEY_CTX_set_dsa_paramgen_bits(EVP_PKEY_CTX *ctx, int nbits)
77 {
78     int ret;
79     OSSL_PARAM params[2], *p = params;
80     size_t bits = nbits;
81
82     if ((ret = dsa_paramgen_check(ctx)) <= 0)
83         return ret;
84
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);
90 #endif
91
92     *p++ = OSSL_PARAM_construct_size_t(OSSL_PKEY_PARAM_FFC_PBITS, &bits);
93     *p++ = OSSL_PARAM_construct_end();
94
95     return EVP_PKEY_CTX_set_params(ctx, params);
96 }
97
98 int EVP_PKEY_CTX_set_dsa_paramgen_q_bits(EVP_PKEY_CTX *ctx, int qbits)
99 {
100     int ret;
101     OSSL_PARAM params[2], *p = params;
102     size_t bits2 = qbits;
103
104     if ((ret = dsa_paramgen_check(ctx)) <= 0)
105         return ret;
106
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);
112 #endif
113
114     *p++ = OSSL_PARAM_construct_size_t(OSSL_PKEY_PARAM_FFC_QBITS, &bits2);
115     *p++ = OSSL_PARAM_construct_end();
116
117     return EVP_PKEY_CTX_set_params(ctx, params);
118 }
119
120 int EVP_PKEY_CTX_set_dsa_paramgen_md_props(EVP_PKEY_CTX *ctx,
121                                            const char *md_name,
122                                            const char *md_properties)
123 {
124     int ret;
125     OSSL_PARAM params[3], *p = params;
126
127     if ((ret = dsa_paramgen_check(ctx)) <= 0)
128         return ret;
129
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);
134
135          EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DSA, EVP_PKEY_OP_PARAMGEN,
136                            EVP_PKEY_CTRL_DSA_PARAMGEN_MD, 0, (void *)(md));
137     }
138 #endif
139
140     *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_FFC_DIGEST,
141                                             (char *)md_name, 0);
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();
146
147     return EVP_PKEY_CTX_set_params(ctx, params);
148 }
149
150 #if !defined(FIPS_MODULE)
151 int EVP_PKEY_CTX_set_dsa_paramgen_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
152 {
153     const char *md_name = (md == NULL) ? "" : EVP_MD_name(md);
154
155     return EVP_PKEY_CTX_set_dsa_paramgen_md_props(ctx, md_name, NULL);
156 }
157 #endif