]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/evp/evp_lib.c
Rename all getters to use get/get0 in name
[thirdparty/openssl.git] / crypto / evp / evp_lib.c
CommitLineData
62867571 1/*
4333b89f 2 * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
58964a49 3 *
4a8b0c55 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
62867571
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
58964a49
RE
8 */
9
41bbba53
P
10/*
11 * EVP _meth_ APIs are deprecated for public use, but still ok for
12 * internal use.
13 */
14#include "internal/deprecated.h"
15
58964a49 16#include <stdio.h>
f9253152
DDO
17#include <string.h>
18#include "e_os.h" /* strcasecmp */
b39fc560 19#include "internal/cryptlib.h"
ec577822
BM
20#include <openssl/evp.h>
21#include <openssl/objects.h>
718b133a
MC
22#include <openssl/params.h>
23#include <openssl/core_names.h>
f9253152 24#include <openssl/rsa.h>
ff64702b 25#include <openssl/dh.h>
023b188c 26#include <openssl/ec.h>
25f2138b 27#include "crypto/evp.h"
924663c3 28#include "crypto/asn1.h"
3653d0c2 29#include "internal/provider.h"
706457b7 30#include "evp_local.h"
58964a49 31
f844f9eb 32#if !defined(FIPS_MODULE)
f9253152 33
6b691a5c 34int EVP_CIPHER_param_to_asn1(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
924663c3
JZ
35{
36 return evp_cipher_param_to_asn1_ex(c, type, NULL);
37}
38
39int EVP_CIPHER_asn1_to_param(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
40{
41 return evp_cipher_asn1_to_param_ex(c, type, NULL);
42}
43
44int EVP_CIPHER_get_asn1_iv(EVP_CIPHER_CTX *ctx, ASN1_TYPE *type)
45{
46 int i = 0;
47 unsigned int l;
48
49 if (type != NULL) {
50 unsigned char iv[EVP_MAX_IV_LENGTH];
51
ed576acd 52 l = EVP_CIPHER_CTX_get_iv_length(ctx);
924663c3
JZ
53 if (!ossl_assert(l <= sizeof(iv)))
54 return -1;
55 i = ASN1_TYPE_get_octetstring(type, iv, l);
56 if (i != (int)l)
57 return -1;
58
59 if (!EVP_CipherInit_ex(ctx, NULL, NULL, NULL, iv, -1))
60 return -1;
61 }
62 return i;
63}
64
65int EVP_CIPHER_set_asn1_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
66{
67 int i = 0;
68 unsigned int j;
69 unsigned char *oiv = NULL;
70
71 if (type != NULL) {
72 oiv = (unsigned char *)EVP_CIPHER_CTX_original_iv(c);
ed576acd 73 j = EVP_CIPHER_CTX_get_iv_length(c);
924663c3
JZ
74 OPENSSL_assert(j <= sizeof(c->iv));
75 i = ASN1_TYPE_set_octetstring(type, oiv, j);
76 }
77 return i;
78}
79
80int evp_cipher_param_to_asn1_ex(EVP_CIPHER_CTX *c, ASN1_TYPE *type,
81 evp_cipher_aead_asn1_params *asn1_params)
0f113f3e 82{
c96399e2 83 int ret = -1; /* Assume the worst */
718b133a 84 const EVP_CIPHER *cipher = c->cipher;
0f113f3e 85
c96399e2
RL
86 /*
87 * For legacy implementations, we detect custom AlgorithmIdentifier
88 * parameter handling by checking if the function pointer
89 * cipher->set_asn1_parameters is set. We know that this pointer
90 * is NULL for provided implementations.
91 *
92 * Otherwise, for any implementation, we check the flag
93 * EVP_CIPH_FLAG_CUSTOM_ASN1. If it isn't set, we apply
94 * default AI parameter extraction.
95 *
96 * Otherwise, for provided implementations, we convert |type| to
97 * a DER encoded blob and pass to the implementation in OSSL_PARAM
98 * form.
99 *
100 * If none of the above applies, this operation is unsupported.
101 */
102 if (cipher->set_asn1_parameters != NULL) {
718b133a 103 ret = cipher->set_asn1_parameters(c, type);
ed576acd
TM
104 } else if ((EVP_CIPHER_get_flags(cipher) & EVP_CIPH_FLAG_CUSTOM_ASN1) == 0) {
105 switch (EVP_CIPHER_get_mode(cipher)) {
2acdef5e 106 case EVP_CIPH_WRAP_MODE:
c96399e2 107 if (EVP_CIPHER_is_a(cipher, SN_id_smime_alg_CMS3DESwrap))
4ec36aff 108 ASN1_TYPE_set(type, V_ASN1_NULL, NULL);
0f113f3e 109 ret = 1;
2acdef5e
DSH
110 break;
111
112 case EVP_CIPH_GCM_MODE:
924663c3
JZ
113 ret = evp_cipher_set_asn1_aead_params(c, type, asn1_params);
114 break;
115
2acdef5e
DSH
116 case EVP_CIPH_CCM_MODE:
117 case EVP_CIPH_XTS_MODE:
118 case EVP_CIPH_OCB_MODE:
49c9c1b3 119 ret = -2;
2acdef5e
DSH
120 break;
121
122 default:
0f113f3e 123 ret = EVP_CIPHER_set_asn1_iv(c, type);
2acdef5e 124 }
c96399e2
RL
125 } else if (cipher->prov != NULL) {
126 OSSL_PARAM params[3], *p = params;
127 unsigned char *der = NULL, *derp;
128
129 /*
130 * We make two passes, the first to get the appropriate buffer size,
131 * and the second to get the actual value.
132 */
592ea4ba
JS
133 *p++ = OSSL_PARAM_construct_octet_string(
134 OSSL_CIPHER_PARAM_ALGORITHM_ID_PARAMS,
135 NULL, 0);
c96399e2
RL
136 *p = OSSL_PARAM_construct_end();
137
138 if (!EVP_CIPHER_CTX_get_params(c, params))
139 goto err;
140
141 /* ... but, we should get a return size too! */
99ea4f02
P
142 if (OSSL_PARAM_modified(params)
143 && params[0].return_size != 0
c96399e2
RL
144 && (der = OPENSSL_malloc(params[0].return_size)) != NULL) {
145 params[0].data = der;
146 params[0].data_size = params[0].return_size;
99ea4f02 147 OSSL_PARAM_set_all_unmodified(params);
c96399e2
RL
148 derp = der;
149 if (EVP_CIPHER_CTX_get_params(c, params)
99ea4f02 150 && OSSL_PARAM_modified(params)
c96399e2
RL
151 && d2i_ASN1_TYPE(&type, (const unsigned char **)&derp,
152 params[0].return_size) != NULL) {
153 ret = 1;
154 }
155 OPENSSL_free(der);
156 }
157 } else {
158 ret = -2;
159 }
160
161 err:
51ba9ebd 162 if (ret == -2)
9311d0c4 163 ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_CIPHER);
51ba9ebd 164 else if (ret <= 0)
9311d0c4 165 ERR_raise(ERR_LIB_EVP, EVP_R_CIPHER_PARAMETER_ERROR);
49c9c1b3
DO
166 if (ret < -1)
167 ret = -1;
26a7d938 168 return ret;
0f113f3e 169}
58964a49 170
924663c3
JZ
171int evp_cipher_asn1_to_param_ex(EVP_CIPHER_CTX *c, ASN1_TYPE *type,
172 evp_cipher_aead_asn1_params *asn1_params)
0f113f3e 173{
c96399e2 174 int ret = -1; /* Assume the worst */
718b133a
MC
175 const EVP_CIPHER *cipher = c->cipher;
176
c96399e2
RL
177 /*
178 * For legacy implementations, we detect custom AlgorithmIdentifier
179 * parameter handling by checking if there the function pointer
180 * cipher->get_asn1_parameters is set. We know that this pointer
181 * is NULL for provided implementations.
182 *
183 * Otherwise, for any implementation, we check the flag
184 * EVP_CIPH_FLAG_CUSTOM_ASN1. If it isn't set, we apply
185 * default AI parameter creation.
186 *
187 * Otherwise, for provided implementations, we get the AI parameter
188 * in DER encoded form from the implementation by requesting the
189 * appropriate OSSL_PARAM and converting the result to a ASN1_TYPE.
190 *
191 * If none of the above applies, this operation is unsupported.
192 */
193 if (cipher->get_asn1_parameters != NULL) {
718b133a 194 ret = cipher->get_asn1_parameters(c, type);
ed576acd
TM
195 } else if ((EVP_CIPHER_get_flags(cipher) & EVP_CIPH_FLAG_CUSTOM_ASN1) == 0) {
196 switch (EVP_CIPHER_get_mode(cipher)) {
2acdef5e
DSH
197 case EVP_CIPH_WRAP_MODE:
198 ret = 1;
199 break;
200
201 case EVP_CIPH_GCM_MODE:
924663c3
JZ
202 ret = evp_cipher_get_asn1_aead_params(c, type, asn1_params);
203 break;
204
2acdef5e
DSH
205 case EVP_CIPH_CCM_MODE:
206 case EVP_CIPH_XTS_MODE:
207 case EVP_CIPH_OCB_MODE:
49c9c1b3 208 ret = -2;
2acdef5e
DSH
209 break;
210
211 default:
212 ret = EVP_CIPHER_get_asn1_iv(c, type);
2acdef5e 213 }
c96399e2
RL
214 } else if (cipher->prov != NULL) {
215 OSSL_PARAM params[3], *p = params;
216 unsigned char *der = NULL;
217 int derl = -1;
218
219 if ((derl = i2d_ASN1_TYPE(type, &der)) >= 0) {
220 *p++ =
592ea4ba
JS
221 OSSL_PARAM_construct_octet_string(
222 OSSL_CIPHER_PARAM_ALGORITHM_ID_PARAMS,
223 der, (size_t)derl);
c96399e2
RL
224 *p = OSSL_PARAM_construct_end();
225 if (EVP_CIPHER_CTX_set_params(c, params))
226 ret = 1;
227 OPENSSL_free(der);
228 }
229 } else {
230 ret = -2;
231 }
232
51ba9ebd 233 if (ret == -2)
9311d0c4 234 ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_CIPHER);
51ba9ebd 235 else if (ret <= 0)
9311d0c4 236 ERR_raise(ERR_LIB_EVP, EVP_R_CIPHER_PARAMETER_ERROR);
49c9c1b3
DO
237 if (ret < -1)
238 ret = -1;
26a7d938 239 return ret;
0f113f3e 240}
58964a49 241
924663c3
JZ
242int evp_cipher_get_asn1_aead_params(EVP_CIPHER_CTX *c, ASN1_TYPE *type,
243 evp_cipher_aead_asn1_params *asn1_params)
0f113f3e
MC
244{
245 int i = 0;
924663c3
JZ
246 long tl;
247 unsigned char iv[EVP_MAX_IV_LENGTH];
0f113f3e 248
924663c3
JZ
249 if (type == NULL || asn1_params == NULL)
250 return 0;
718b133a 251
adf7e6d1 252 i = ossl_asn1_type_get_octetstring_int(type, &tl, NULL, EVP_MAX_IV_LENGTH);
924663c3
JZ
253 if (i <= 0)
254 return -1;
adf7e6d1 255 ossl_asn1_type_get_octetstring_int(type, &tl, iv, i);
924663c3
JZ
256
257 memcpy(asn1_params->iv, iv, i);
258 asn1_params->iv_len = i;
718b133a 259
26a7d938 260 return i;
0f113f3e 261}
58964a49 262
924663c3
JZ
263int evp_cipher_set_asn1_aead_params(EVP_CIPHER_CTX *c, ASN1_TYPE *type,
264 evp_cipher_aead_asn1_params *asn1_params)
0f113f3e 265{
924663c3
JZ
266 if (type == NULL || asn1_params == NULL)
267 return 0;
0f113f3e 268
adf7e6d1
SL
269 return ossl_asn1_type_set_octetstring_int(type, asn1_params->tag_len,
270 asn1_params->iv,
271 asn1_params->iv_len);
0f113f3e 272}
f844f9eb 273#endif /* !defined(FIPS_MODULE) */
884e8ec6
DSH
274
275/* Convert the various cipher NIDs and dummies to a proper OID NID */
ed576acd 276int EVP_CIPHER_get_type(const EVP_CIPHER *cipher)
884e8ec6 277{
0f113f3e 278 int nid;
ed576acd 279 nid = EVP_CIPHER_get_nid(cipher);
884e8ec6 280
0f113f3e 281 switch (nid) {
884e8ec6 282
0f113f3e
MC
283 case NID_rc2_cbc:
284 case NID_rc2_64_cbc:
285 case NID_rc2_40_cbc:
884e8ec6 286
0f113f3e 287 return NID_rc2_cbc;
884e8ec6 288
0f113f3e
MC
289 case NID_rc4:
290 case NID_rc4_40:
884e8ec6 291
0f113f3e 292 return NID_rc4;
884e8ec6 293
0f113f3e
MC
294 case NID_aes_128_cfb128:
295 case NID_aes_128_cfb8:
296 case NID_aes_128_cfb1:
8d1ebe0b 297
0f113f3e 298 return NID_aes_128_cfb128;
8d1ebe0b 299
0f113f3e
MC
300 case NID_aes_192_cfb128:
301 case NID_aes_192_cfb8:
302 case NID_aes_192_cfb1:
8d1ebe0b 303
0f113f3e 304 return NID_aes_192_cfb128;
8d1ebe0b 305
0f113f3e
MC
306 case NID_aes_256_cfb128:
307 case NID_aes_256_cfb8:
308 case NID_aes_256_cfb1:
8d1ebe0b 309
0f113f3e 310 return NID_aes_256_cfb128;
8d1ebe0b 311
0f113f3e
MC
312 case NID_des_cfb64:
313 case NID_des_cfb8:
314 case NID_des_cfb1:
8d1ebe0b 315
0f113f3e 316 return NID_des_cfb64;
8d1ebe0b 317
0f113f3e
MC
318 case NID_des_ede3_cfb64:
319 case NID_des_ede3_cfb8:
320 case NID_des_ede3_cfb1:
7e765bf2 321
0f113f3e 322 return NID_des_cfb64;
7e765bf2 323
0f113f3e 324 default:
f844f9eb 325#ifdef FIPS_MODULE
319e518a
MC
326 return NID_undef;
327#else
328 {
329 /* Check it has an OID and it is valid */
330 ASN1_OBJECT *otmp = OBJ_nid2obj(nid);
331
332 if (OBJ_get0_data(otmp) == NULL)
333 nid = NID_undef;
334 ASN1_OBJECT_free(otmp);
335 return nid;
336 }
337#endif
0f113f3e 338 }
884e8ec6
DSH
339}
340
3c957bcd 341int evp_cipher_cache_constants(EVP_CIPHER *cipher)
0f113f3e 342{
a054d15c 343 int ok, aead = 0, custom_iv = 0, cts = 0, multiblock = 0;
3c957bcd
SL
344 size_t ivlen = 0;
345 size_t blksz = 0;
346 size_t keylen = 0;
347 unsigned int mode = 0;
a054d15c 348 OSSL_PARAM params[9];
3c957bcd
SL
349
350 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_BLOCK_SIZE, &blksz);
351 params[1] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN, &ivlen);
352 params[2] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &keylen);
353 params[3] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_MODE, &mode);
a054d15c
SL
354 params[4] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_AEAD, &aead);
355 params[5] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_CUSTOM_IV,
356 &custom_iv);
357 params[6] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_CTS, &cts);
358 params[7] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK,
359 &multiblock);
360 params[8] = OSSL_PARAM_construct_end();
4885ecff 361 ok = evp_do_ciph_getparams(cipher, params) > 0;
3c957bcd 362 if (ok) {
3c957bcd
SL
363 cipher->block_size = blksz;
364 cipher->iv_len = ivlen;
365 cipher->key_len = keylen;
a054d15c
SL
366 cipher->flags = mode;
367 if (aead)
368 cipher->flags |= EVP_CIPH_FLAG_AEAD_CIPHER;
369 if (custom_iv)
370 cipher->flags |= EVP_CIPH_CUSTOM_IV;
371 if (cts)
372 cipher->flags |= EVP_CIPH_FLAG_CTS;
373 if (multiblock)
374 cipher->flags |= EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK;
4885ecff 375 if (cipher->ccipher != NULL)
a054d15c 376 cipher->flags |= EVP_CIPH_FLAG_CUSTOM_CIPHER;
592ea4ba
JS
377 if (OSSL_PARAM_locate_const(EVP_CIPHER_gettable_ctx_params(cipher),
378 OSSL_CIPHER_PARAM_ALGORITHM_ID_PARAMS))
379 cipher->flags |= EVP_CIPH_FLAG_CUSTOM_ASN1;
3c957bcd
SL
380 }
381 return ok;
382}
13273237 383
ed576acd 384int EVP_CIPHER_get_block_size(const EVP_CIPHER *cipher)
3c957bcd
SL
385{
386 return cipher->block_size;
0f113f3e 387}
7806f3dd 388
ed576acd 389int EVP_CIPHER_CTX_get_block_size(const EVP_CIPHER_CTX *ctx)
0f113f3e 390{
ed576acd 391 return EVP_CIPHER_get_block_size(ctx->cipher);
0f113f3e 392}
7806f3dd 393
e79f8773
RL
394int EVP_CIPHER_impl_ctx_size(const EVP_CIPHER *e)
395{
396 return e->ctx_size;
397}
398
0f113f3e
MC
399int EVP_Cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
400 const unsigned char *in, unsigned int inl)
401{
718b133a 402 if (ctx->cipher->prov != NULL) {
f7397f0d
RL
403 /*
404 * If the provided implementation has a ccipher function, we use it,
405 * and translate its return value like this: 0 => -1, 1 => outlen
406 *
407 * Otherwise, we call the cupdate function if in != NULL, or cfinal
408 * if in == NULL. Regardless of which, we return what we got.
409 */
410 int ret = -1;
411 size_t outl = 0;
ed576acd 412 size_t blocksize = EVP_CIPHER_CTX_get_block_size(ctx);
f79858ac 413
718b133a 414 if (ctx->cipher->ccipher != NULL)
7c14d0c1 415 ret = ctx->cipher->ccipher(ctx->algctx, out, &outl,
f7397f0d
RL
416 inl + (blocksize == 1 ? 0 : blocksize),
417 in, (size_t)inl)
418 ? (int)outl : -1;
419 else if (in != NULL)
7c14d0c1 420 ret = ctx->cipher->cupdate(ctx->algctx, out, &outl,
f7397f0d
RL
421 inl + (blocksize == 1 ? 0 : blocksize),
422 in, (size_t)inl);
423 else
7c14d0c1 424 ret = ctx->cipher->cfinal(ctx->algctx, out, &outl,
f7397f0d
RL
425 blocksize == 1 ? 0 : blocksize);
426
427 return ret;
718b133a
MC
428 }
429
0f113f3e
MC
430 return ctx->cipher->do_cipher(ctx, out, in, inl);
431}
7806f3dd 432
f6c95e46 433#ifndef OPENSSL_NO_DEPRECATED_3_0
7806f3dd 434const EVP_CIPHER *EVP_CIPHER_CTX_cipher(const EVP_CIPHER_CTX *ctx)
0f113f3e 435{
f6c95e46
RS
436 if (ctx == NULL)
437 return NULL;
438 return ctx->cipher;
439}
440#endif
441
442const EVP_CIPHER *EVP_CIPHER_CTX_get0_cipher(const EVP_CIPHER_CTX *ctx)
443{
444 if (ctx == NULL)
445 return NULL;
0f113f3e
MC
446 return ctx->cipher;
447}
7806f3dd 448
f6c95e46
RS
449EVP_CIPHER *EVP_CIPHER_CTX_get1_cipher(EVP_CIPHER_CTX *ctx)
450{
451 EVP_CIPHER *cipher;
452
453 if (ctx == NULL)
454 return NULL;
455 cipher = (EVP_CIPHER *)ctx->cipher;
456 if (!EVP_CIPHER_up_ref(cipher))
457 return NULL;
458 return cipher;
459}
460
ed576acd 461int EVP_CIPHER_CTX_is_encrypting(const EVP_CIPHER_CTX *ctx)
83b06347
RL
462{
463 return ctx->encrypt;
464}
465
ed576acd 466unsigned long EVP_CIPHER_get_flags(const EVP_CIPHER *cipher)
0f113f3e 467{
3c957bcd 468 return cipher->flags;
0f113f3e 469}
7806f3dd 470
7806f3dd 471void *EVP_CIPHER_CTX_get_app_data(const EVP_CIPHER_CTX *ctx)
0f113f3e
MC
472{
473 return ctx->app_data;
474}
7806f3dd
NL
475
476void EVP_CIPHER_CTX_set_app_data(EVP_CIPHER_CTX *ctx, void *data)
0f113f3e
MC
477{
478 ctx->app_data = data;
479}
7806f3dd 480
44ab2dfd 481void *EVP_CIPHER_CTX_get_cipher_data(const EVP_CIPHER_CTX *ctx)
83b06347
RL
482{
483 return ctx->cipher_data;
484}
485
98ee7543
MC
486void *EVP_CIPHER_CTX_set_cipher_data(EVP_CIPHER_CTX *ctx, void *cipher_data)
487{
488 void *old_cipher_data;
489
490 old_cipher_data = ctx->cipher_data;
491 ctx->cipher_data = cipher_data;
492
493 return old_cipher_data;
494}
495
ed576acd 496int EVP_CIPHER_get_iv_length(const EVP_CIPHER *cipher)
0f113f3e 497{
3c957bcd 498 return cipher->iv_len;
0f113f3e 499}
7806f3dd 500
ed576acd 501int EVP_CIPHER_CTX_get_iv_length(const EVP_CIPHER_CTX *ctx)
0f113f3e 502{
ed576acd 503 int rv, len = EVP_CIPHER_get_iv_length(ctx->cipher);
88d87082 504 size_t v = len;
a672a02a
SL
505 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
506
1c3ace68 507 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN, &v);
7c14d0c1 508 rv = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
7dddf2fc
SL
509 if (rv == EVP_CTRL_RET_UNSUPPORTED)
510 goto legacy;
1c3ace68 511 return rv != 0 ? (int)v : -1;
0be6cf0c 512 /* Code below to be removed when legacy support is dropped. */
7dddf2fc 513legacy:
ed576acd 514 if ((EVP_CIPHER_get_flags(ctx->cipher) & EVP_CIPH_CUSTOM_IV_LENGTH) != 0) {
7dddf2fc
SL
515 rv = EVP_CIPHER_CTX_ctrl((EVP_CIPHER_CTX *)ctx, EVP_CTRL_GET_IVLEN,
516 0, &len);
88d87082 517 return (rv == 1) ? len : -1;
7dddf2fc 518 }
88d87082 519 return len;
0f113f3e 520}
7806f3dd 521
ed576acd 522int EVP_CIPHER_CTX_get_tag_length(const EVP_CIPHER_CTX *ctx)
dc64dc2e
SL
523{
524 int ret;
525 size_t v = 0;
526 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
527
528 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_TAGLEN, &v);
7c14d0c1 529 ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
dc64dc2e
SL
530 return ret == 1 ? (int)v : 0;
531}
532
79f4417e 533#ifndef OPENSSL_NO_DEPRECATED_3_0
83b06347
RL
534const unsigned char *EVP_CIPHER_CTX_original_iv(const EVP_CIPHER_CTX *ctx)
535{
089cb623
SL
536 int ok;
537 const unsigned char *v = ctx->oiv;
538 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
539
540 params[0] =
541 OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_IV,
542 (void **)&v, sizeof(ctx->oiv));
7c14d0c1 543 ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
089cb623
SL
544
545 return ok != 0 ? v : NULL;
83b06347
RL
546}
547
13273237
RL
548/*
549 * OSSL_PARAM_OCTET_PTR gets us the pointer to the running IV in the provider
550 */
83b06347
RL
551const unsigned char *EVP_CIPHER_CTX_iv(const EVP_CIPHER_CTX *ctx)
552{
459b15d4 553 int ok;
13273237 554 const unsigned char *v = ctx->iv;
459b15d4 555 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
13273237 556
459b15d4 557 params[0] =
0d83b7b9
TM
558 OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_UPDATED_IV,
559 (void **)&v, sizeof(ctx->iv));
7c14d0c1 560 ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
459b15d4
SL
561
562 return ok != 0 ? v : NULL;
83b06347
RL
563}
564
565unsigned char *EVP_CIPHER_CTX_iv_noconst(EVP_CIPHER_CTX *ctx)
566{
459b15d4 567 int ok;
13273237 568 unsigned char *v = ctx->iv;
459b15d4
SL
569 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
570
571 params[0] =
0d83b7b9
TM
572 OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_UPDATED_IV,
573 (void **)&v, sizeof(ctx->iv));
7c14d0c1 574 ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
13273237 575
459b15d4 576 return ok != 0 ? v : NULL;
83b06347 577}
79f4417e
BK
578#endif /* OPENSSL_NO_DEPRECATED_3_0_0 */
579
0d83b7b9 580int EVP_CIPHER_CTX_get_updated_iv(EVP_CIPHER_CTX *ctx, void *buf, size_t len)
79f4417e
BK
581{
582 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
583
584 params[0] =
0d83b7b9 585 OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_UPDATED_IV, buf, len);
7c14d0c1 586 return evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
79f4417e
BK
587}
588
0d83b7b9 589int EVP_CIPHER_CTX_get_original_iv(EVP_CIPHER_CTX *ctx, void *buf, size_t len)
79f4417e
BK
590{
591 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
592
593 params[0] =
594 OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_IV, buf, len);
7c14d0c1 595 return evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
79f4417e 596}
83b06347
RL
597
598unsigned char *EVP_CIPHER_CTX_buf_noconst(EVP_CIPHER_CTX *ctx)
599{
600 return ctx->buf;
601}
602
ed576acd 603int EVP_CIPHER_CTX_get_num(const EVP_CIPHER_CTX *ctx)
83b06347 604{
1c3ace68
SL
605 int ok;
606 unsigned int v = (unsigned int)ctx->num;
459b15d4 607 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
13273237 608
1c3ace68 609 params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_NUM, &v);
7c14d0c1 610 ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
459b15d4 611
1c3ace68 612 return ok != 0 ? (int)v : EVP_CTRL_RET_UNSUPPORTED;
83b06347
RL
613}
614
13273237 615int EVP_CIPHER_CTX_set_num(EVP_CIPHER_CTX *ctx, int num)
83b06347 616{
459b15d4 617 int ok;
1c3ace68 618 unsigned int n = (unsigned int)num;
459b15d4 619 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
13273237 620
1c3ace68 621 params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_NUM, &n);
7c14d0c1 622 ok = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
459b15d4
SL
623
624 if (ok != 0)
1c3ace68 625 ctx->num = (int)n;
13273237 626 return ok != 0;
83b06347
RL
627}
628
ed576acd 629int EVP_CIPHER_get_key_length(const EVP_CIPHER *cipher)
0f113f3e 630{
3c957bcd 631 return cipher->key_len;
0f113f3e 632}
7806f3dd 633
ed576acd 634int EVP_CIPHER_CTX_get_key_length(const EVP_CIPHER_CTX *ctx)
0f113f3e 635{
1c3ace68
SL
636 int ok;
637 size_t v = ctx->key_len;
459b15d4
SL
638 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
639
1c3ace68 640 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &v);
7c14d0c1 641 ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
13273237 642
1c3ace68 643 return ok != 0 ? (int)v : EVP_CTRL_RET_UNSUPPORTED;
0f113f3e 644}
7806f3dd 645
ed576acd 646int EVP_CIPHER_get_nid(const EVP_CIPHER *cipher)
0f113f3e
MC
647{
648 return cipher->nid;
649}
7806f3dd 650
ed576acd 651int EVP_CIPHER_CTX_get_nid(const EVP_CIPHER_CTX *ctx)
0f113f3e
MC
652{
653 return ctx->cipher->nid;
654}
7806f3dd 655
7cfa1717
RL
656int EVP_CIPHER_is_a(const EVP_CIPHER *cipher, const char *name)
657{
e4a1d023
RL
658 if (cipher->prov != NULL)
659 return evp_is_a(cipher->prov, cipher->name_id, NULL, name);
ed576acd 660 return evp_is_a(NULL, 0, EVP_CIPHER_get0_name(cipher), name);
7cfa1717
RL
661}
662
ed576acd 663int EVP_CIPHER_get_number(const EVP_CIPHER *cipher)
506cb0f6
RL
664{
665 return cipher->name_id;
666}
667
ed576acd 668const char *EVP_CIPHER_get0_name(const EVP_CIPHER *cipher)
c750bc08 669{
6c9bc258
TM
670 if (cipher->type_name != NULL)
671 return cipher->type_name;
f844f9eb 672#ifndef FIPS_MODULE
ed576acd 673 return OBJ_nid2sn(EVP_CIPHER_get_nid(cipher));
c750bc08
RL
674#else
675 return NULL;
676#endif
677}
678
ed576acd 679const char *EVP_CIPHER_get0_description(const EVP_CIPHER *cipher)
03888233
RL
680{
681 if (cipher->description != NULL)
682 return cipher->description;
683#ifndef FIPS_MODULE
ed576acd 684 return OBJ_nid2ln(EVP_CIPHER_get_nid(cipher));
03888233
RL
685#else
686 return NULL;
687#endif
688}
689
d84f5515
MC
690int EVP_CIPHER_names_do_all(const EVP_CIPHER *cipher,
691 void (*fn)(const char *name, void *data),
692 void *data)
f651c727
RL
693{
694 if (cipher->prov != NULL)
d84f5515
MC
695 return evp_names_do_all(cipher->prov, cipher->name_id, fn, data);
696
697 return 1;
f651c727
RL
698}
699
ed576acd 700const OSSL_PROVIDER *EVP_CIPHER_get0_provider(const EVP_CIPHER *cipher)
1d2622d4
RL
701{
702 return cipher->prov;
703}
704
ed576acd 705int EVP_CIPHER_get_mode(const EVP_CIPHER *cipher)
718b133a 706{
ed576acd 707 return EVP_CIPHER_get_flags(cipher) & EVP_CIPH_MODE;
459b15d4 708}
718b133a 709
251e610c
RL
710int EVP_MD_is_a(const EVP_MD *md, const char *name)
711{
e4a1d023
RL
712 if (md->prov != NULL)
713 return evp_is_a(md->prov, md->name_id, NULL, name);
ed576acd 714 return evp_is_a(NULL, 0, EVP_MD_get0_name(md), name);
251e610c
RL
715}
716
ed576acd 717int EVP_MD_get_number(const EVP_MD *md)
506cb0f6
RL
718{
719 return md->name_id;
720}
721
ed576acd 722const char *EVP_MD_get0_description(const EVP_MD *md)
03888233
RL
723{
724 if (md->description != NULL)
725 return md->description;
726#ifndef FIPS_MODULE
727 return OBJ_nid2ln(EVP_MD_nid(md));
728#else
729 return NULL;
730#endif
731}
732
ed576acd 733const char *EVP_MD_get0_name(const EVP_MD *md)
c750bc08 734{
5c107243
SL
735 if (md == NULL)
736 return NULL;
6c9bc258
TM
737 if (md->type_name != NULL)
738 return md->type_name;
f844f9eb 739#ifndef FIPS_MODULE
c750bc08
RL
740 return OBJ_nid2sn(EVP_MD_nid(md));
741#else
742 return NULL;
743#endif
744}
745
d84f5515
MC
746int EVP_MD_names_do_all(const EVP_MD *md,
747 void (*fn)(const char *name, void *data),
748 void *data)
f651c727
RL
749{
750 if (md->prov != NULL)
d84f5515
MC
751 return evp_names_do_all(md->prov, md->name_id, fn, data);
752
753 return 1;
f651c727
RL
754}
755
ed576acd 756const OSSL_PROVIDER *EVP_MD_get0_provider(const EVP_MD *md)
1d2622d4
RL
757{
758 return md->prov;
759}
760
ed576acd 761int EVP_MD_get_type(const EVP_MD *md)
0f113f3e
MC
762{
763 return md->type;
764}
7806f3dd 765
ed576acd 766int EVP_MD_get_pkey_type(const EVP_MD *md)
0f113f3e
MC
767{
768 return md->pkey_type;
769}
7806f3dd 770
ed576acd 771int EVP_MD_get_block_size(const EVP_MD *md)
a054d15c
SL
772{
773 if (md == NULL) {
774 ERR_raise(ERR_LIB_EVP, EVP_R_MESSAGE_DIGEST_IS_NULL);
775 return -1;
776 }
777 return md->block_size;
778}
779
ed576acd 780int EVP_MD_get_size(const EVP_MD *md)
0f113f3e 781{
6a3b7c68 782 if (md == NULL) {
9311d0c4 783 ERR_raise(ERR_LIB_EVP, EVP_R_MESSAGE_DIGEST_IS_NULL);
0f113f3e
MC
784 return -1;
785 }
38f79314 786 return md->md_size;
0f113f3e 787}
7806f3dd 788
ed576acd 789unsigned long EVP_MD_get_flags(const EVP_MD *md)
0f113f3e 790{
38f79314 791 return md->flags;
0f113f3e 792}
e5fa864f 793
2db6bf6f
RL
794EVP_MD *EVP_MD_meth_new(int md_type, int pkey_type)
795{
3fd70262 796 EVP_MD *md = evp_md_new();
43ecb9c3 797
2db6bf6f
RL
798 if (md != NULL) {
799 md->type = md_type;
800 md->pkey_type = pkey_type;
f6c95e46 801 md->origin = EVP_ORIG_METH;
2db6bf6f
RL
802 }
803 return md;
804}
df05f2ce 805
2db6bf6f
RL
806EVP_MD *EVP_MD_meth_dup(const EVP_MD *md)
807{
3fd70262
RL
808 EVP_MD *to = NULL;
809
810 /*
811 * Non-legacy EVP_MDs can't be duplicated like this.
812 * Use EVP_MD_up_ref() instead.
813 */
814 if (md->prov != NULL)
815 return NULL;
43ecb9c3 816
3fd70262 817 if ((to = EVP_MD_meth_new(md->type, md->pkey_type)) != NULL) {
df05f2ce 818 CRYPTO_RWLOCK *lock = to->lock;
3fd70262 819
2db6bf6f 820 memcpy(to, md, sizeof(*to));
df05f2ce
MC
821 to->lock = lock;
822 }
2db6bf6f
RL
823 return to;
824}
3653d0c2 825
f6c95e46
RS
826void evp_md_free_int(EVP_MD *md)
827{
6c9bc258 828 OPENSSL_free(md->type_name);
f6c95e46
RS
829 ossl_provider_free(md->prov);
830 CRYPTO_THREAD_lock_free(md->lock);
831 OPENSSL_free(md);
832}
833
2db6bf6f
RL
834void EVP_MD_meth_free(EVP_MD *md)
835{
f6c95e46
RS
836 if (md == NULL || md->origin != EVP_ORIG_METH)
837 return;
838
839 evp_md_free_int(md);
2db6bf6f 840}
f6c95e46 841
2db6bf6f
RL
842int EVP_MD_meth_set_input_blocksize(EVP_MD *md, int blocksize)
843{
8bbc7f22
DB
844 if (md->block_size != 0)
845 return 0;
846
2db6bf6f
RL
847 md->block_size = blocksize;
848 return 1;
849}
850int EVP_MD_meth_set_result_size(EVP_MD *md, int resultsize)
851{
8bbc7f22
DB
852 if (md->md_size != 0)
853 return 0;
854
2db6bf6f
RL
855 md->md_size = resultsize;
856 return 1;
857}
858int EVP_MD_meth_set_app_datasize(EVP_MD *md, int datasize)
859{
8bbc7f22
DB
860 if (md->ctx_size != 0)
861 return 0;
862
2db6bf6f
RL
863 md->ctx_size = datasize;
864 return 1;
865}
866int EVP_MD_meth_set_flags(EVP_MD *md, unsigned long flags)
867{
8bbc7f22
DB
868 if (md->flags != 0)
869 return 0;
870
2db6bf6f
RL
871 md->flags = flags;
872 return 1;
873}
874int EVP_MD_meth_set_init(EVP_MD *md, int (*init)(EVP_MD_CTX *ctx))
875{
8bbc7f22
DB
876 if (md->init != NULL)
877 return 0;
878
2db6bf6f
RL
879 md->init = init;
880 return 1;
881}
882int EVP_MD_meth_set_update(EVP_MD *md, int (*update)(EVP_MD_CTX *ctx,
883 const void *data,
884 size_t count))
885{
8bbc7f22
DB
886 if (md->update != NULL)
887 return 0;
888
2db6bf6f
RL
889 md->update = update;
890 return 1;
891}
892int EVP_MD_meth_set_final(EVP_MD *md, int (*final)(EVP_MD_CTX *ctx,
893 unsigned char *md))
894{
8bbc7f22
DB
895 if (md->final != NULL)
896 return 0;
897
2db6bf6f
RL
898 md->final = final;
899 return 1;
900}
901int EVP_MD_meth_set_copy(EVP_MD *md, int (*copy)(EVP_MD_CTX *to,
902 const EVP_MD_CTX *from))
903{
8bbc7f22
DB
904 if (md->copy != NULL)
905 return 0;
906
2db6bf6f
RL
907 md->copy = copy;
908 return 1;
909}
910int EVP_MD_meth_set_cleanup(EVP_MD *md, int (*cleanup)(EVP_MD_CTX *ctx))
911{
8bbc7f22
DB
912 if (md->cleanup != NULL)
913 return 0;
914
2db6bf6f
RL
915 md->cleanup = cleanup;
916 return 1;
917}
918int EVP_MD_meth_set_ctrl(EVP_MD *md, int (*ctrl)(EVP_MD_CTX *ctx, int cmd,
919 int p1, void *p2))
920{
8bbc7f22
DB
921 if (md->md_ctrl != NULL)
922 return 0;
923
2db6bf6f
RL
924 md->md_ctrl = ctrl;
925 return 1;
926}
927
928int EVP_MD_meth_get_input_blocksize(const EVP_MD *md)
929{
930 return md->block_size;
931}
932int EVP_MD_meth_get_result_size(const EVP_MD *md)
933{
934 return md->md_size;
935}
936int EVP_MD_meth_get_app_datasize(const EVP_MD *md)
937{
938 return md->ctx_size;
939}
940unsigned long EVP_MD_meth_get_flags(const EVP_MD *md)
941{
8bfa99f0 942 return md->flags;
2db6bf6f
RL
943}
944int (*EVP_MD_meth_get_init(const EVP_MD *md))(EVP_MD_CTX *ctx)
945{
946 return md->init;
947}
948int (*EVP_MD_meth_get_update(const EVP_MD *md))(EVP_MD_CTX *ctx,
949 const void *data,
950 size_t count)
951{
952 return md->update;
953}
954int (*EVP_MD_meth_get_final(const EVP_MD *md))(EVP_MD_CTX *ctx,
955 unsigned char *md)
956{
957 return md->final;
958}
959int (*EVP_MD_meth_get_copy(const EVP_MD *md))(EVP_MD_CTX *to,
960 const EVP_MD_CTX *from)
961{
962 return md->copy;
963}
964int (*EVP_MD_meth_get_cleanup(const EVP_MD *md))(EVP_MD_CTX *ctx)
965{
966 return md->cleanup;
967}
968int (*EVP_MD_meth_get_ctrl(const EVP_MD *md))(EVP_MD_CTX *ctx, int cmd,
969 int p1, void *p2)
970{
971 return md->md_ctrl;
972}
973
f6c95e46 974#ifndef OPENSSL_NO_DEPRECATED_3_0
7806f3dd 975const EVP_MD *EVP_MD_CTX_md(const EVP_MD_CTX *ctx)
0f113f3e 976{
b7c913c8 977 if (ctx == NULL)
0f113f3e 978 return NULL;
b7c913c8 979 return ctx->reqdigest;
0f113f3e 980}
f6c95e46
RS
981#endif
982
983const EVP_MD *EVP_MD_CTX_get0_md(const EVP_MD_CTX *ctx)
984{
985 if (ctx == NULL)
986 return NULL;
987 return ctx->reqdigest;
988}
989
990EVP_MD *EVP_MD_CTX_get1_md(EVP_MD_CTX *ctx)
991{
992 EVP_MD *md;
993
994 if (ctx == NULL)
995 return NULL;
996 md = (EVP_MD *)ctx->reqdigest;
997 if (!EVP_MD_up_ref(md))
998 return NULL;
999 return md;
1000}
7806f3dd 1001
ed576acd 1002EVP_PKEY_CTX *EVP_MD_CTX_get_pkey_ctx(const EVP_MD_CTX *ctx)
7638370c
RL
1003{
1004 return ctx->pctx;
1005}
1006
f844f9eb 1007#if !defined(FIPS_MODULE)
00902d94
PY
1008void EVP_MD_CTX_set_pkey_ctx(EVP_MD_CTX *ctx, EVP_PKEY_CTX *pctx)
1009{
81c79453
PY
1010 /*
1011 * it's reasonable to set NULL pctx (a.k.a clear the ctx->pctx), so
1012 * we have to deal with the cleanup job here.
1013 */
1014 if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX))
1015 EVP_PKEY_CTX_free(ctx->pctx);
1016
00902d94 1017 ctx->pctx = pctx;
81c79453
PY
1018
1019 if (pctx != NULL) {
1020 /* make sure pctx is not freed when destroying EVP_MD_CTX */
1021 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
1022 } else {
1023 EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
1024 }
00902d94 1025}
f844f9eb 1026#endif /* !defined(FIPS_MODULE) */
00902d94 1027
ed576acd 1028void *EVP_MD_CTX_get0_md_data(const EVP_MD_CTX *ctx)
7638370c
RL
1029{
1030 return ctx->md_data;
1031}
1032
1033int (*EVP_MD_CTX_update_fn(EVP_MD_CTX *ctx))(EVP_MD_CTX *ctx,
1034 const void *data, size_t count)
1035{
1036 return ctx->update;
1037}
1038
1039void EVP_MD_CTX_set_update_fn(EVP_MD_CTX *ctx,
1040 int (*update) (EVP_MD_CTX *ctx,
1041 const void *data, size_t count))
1042{
1043 ctx->update = update;
1044}
1045
7806f3dd 1046void EVP_MD_CTX_set_flags(EVP_MD_CTX *ctx, int flags)
0f113f3e
MC
1047{
1048 ctx->flags |= flags;
1049}
7806f3dd
NL
1050
1051void EVP_MD_CTX_clear_flags(EVP_MD_CTX *ctx, int flags)
0f113f3e
MC
1052{
1053 ctx->flags &= ~flags;
1054}
7806f3dd
NL
1055
1056int EVP_MD_CTX_test_flags(const EVP_MD_CTX *ctx, int flags)
0f113f3e
MC
1057{
1058 return (ctx->flags & flags);
1059}
e92f9f45 1060
e2311445
SL
1061static int evp_cipher_ctx_enable_use_bits(EVP_CIPHER_CTX *ctx,
1062 unsigned int enable)
1063{
1064 OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
1065
1066 params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_USE_BITS, &enable);
1067 return EVP_CIPHER_CTX_set_params(ctx, params);
1068}
1069
e92f9f45 1070void EVP_CIPHER_CTX_set_flags(EVP_CIPHER_CTX *ctx, int flags)
0f113f3e 1071{
e2311445
SL
1072 int oldflags = ctx->flags;
1073
0f113f3e 1074 ctx->flags |= flags;
e2311445
SL
1075 if (((oldflags ^ ctx->flags) & EVP_CIPH_FLAG_LENGTH_BITS) != 0)
1076 evp_cipher_ctx_enable_use_bits(ctx, 1);
0f113f3e 1077}
e92f9f45
DSH
1078
1079void EVP_CIPHER_CTX_clear_flags(EVP_CIPHER_CTX *ctx, int flags)
0f113f3e 1080{
e2311445
SL
1081 int oldflags = ctx->flags;
1082
0f113f3e 1083 ctx->flags &= ~flags;
e2311445
SL
1084 if (((oldflags ^ ctx->flags) & EVP_CIPH_FLAG_LENGTH_BITS) != 0)
1085 evp_cipher_ctx_enable_use_bits(ctx, 0);
0f113f3e 1086}
e92f9f45
DSH
1087
1088int EVP_CIPHER_CTX_test_flags(const EVP_CIPHER_CTX *ctx, int flags)
0f113f3e
MC
1089{
1090 return (ctx->flags & flags);
1091}
f842b6b2 1092
11a1b341
MC
1093int EVP_PKEY_CTX_set_group_name(EVP_PKEY_CTX *ctx, const char *name)
1094{
1095 OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
023b188c 1096
6fcd92d3 1097 if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
11a1b341
MC
1098 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1099 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1100 return -2;
1101 }
1102
1103 if (name == NULL)
1104 return -1;
1105
6fcd92d3
RL
1106 params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
1107 (char *)name, 0);
11a1b341
MC
1108 return EVP_PKEY_CTX_set_params(ctx, params);
1109}
1110
1111int EVP_PKEY_CTX_get_group_name(EVP_PKEY_CTX *ctx, char *name, size_t namelen)
1112{
1113 OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
1114 OSSL_PARAM *p = params;
1115
1116 if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
023b188c 1117 /* There is no legacy support for this */
11a1b341
MC
1118 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1119 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1120 return -2;
1121 }
1122
1123 if (name == NULL)
1124 return -1;
1125
1126 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
1127 name, namelen);
1128 if (!EVP_PKEY_CTX_get_params(ctx, params))
1129 return -1;
1130 return 1;
1131}
f9253152
DDO
1132
1133/*
1134 * evp_pkey_keygen() abstracts from the explicit use of B<EVP_PKEY_CTX>
1135 * while providing a generic way of generating a new asymmetric key pair
1136 * of algorithm type I<name> (e.g., C<RSA> or C<EC>).
1137 * The library context I<libctx> and property query I<propq>
1138 * are used when fetching algorithms from providers.
1139 * The I<params> specify algorithm-specific parameters
1140 * such as the RSA modulus size or the name of an EC curve.
1141 */
1142static EVP_PKEY *evp_pkey_keygen(OSSL_LIB_CTX *libctx, const char *name,
56784203 1143 const char *propq, const OSSL_PARAM *params)
f9253152
DDO
1144{
1145 EVP_PKEY *pkey = NULL;
1146 EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_from_name(libctx, name, propq);
1147
1148 if (ctx != NULL
1149 && EVP_PKEY_keygen_init(ctx) > 0
1150 && EVP_PKEY_CTX_set_params(ctx, params))
1151 (void)EVP_PKEY_generate(ctx, &pkey);
1152
1153 EVP_PKEY_CTX_free(ctx);
1154 return pkey;
1155}
1156
1157EVP_PKEY *EVP_PKEY_Q_keygen(OSSL_LIB_CTX *libctx, const char *propq,
1158 const char *type, ...)
1159{
1160 va_list args;
1161 size_t bits;
1162 char *name;
1163 OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
1164 EVP_PKEY *ret = NULL;
1165
1166 va_start(args, type);
1167
1168 if (strcasecmp(type, "RSA") == 0) {
1169 bits = va_arg(args, size_t);
1170 params[0] = OSSL_PARAM_construct_size_t(OSSL_PKEY_PARAM_RSA_BITS, &bits);
1171 } else if (strcasecmp(type, "EC") == 0) {
1172 name = va_arg(args, char *);
1173 params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
1174 name, 0);
1175 } else if (strcasecmp(type, "ED25519") != 0
1176 && strcasecmp(type, "X25519") != 0
1177 && strcasecmp(type, "ED448") != 0
1178 && strcasecmp(type, "X448") != 0) {
1179 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_INVALID_ARGUMENT);
1180 goto end;
1181 }
1182 ret = evp_pkey_keygen(libctx, type, propq, params);
1183
1184 end:
1185 va_end(args);
1186 return ret;
1187}