]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/evp/evp_lib.c
Convert all {NAME}err() in crypto/ to their corresponding ERR_raise() call
[thirdparty/openssl.git] / crypto / evp / evp_lib.c
CommitLineData
62867571 1/*
33388b44 2 * Copyright 1995-2020 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>
b39fc560 17#include "internal/cryptlib.h"
ec577822
BM
18#include <openssl/evp.h>
19#include <openssl/objects.h>
718b133a
MC
20#include <openssl/params.h>
21#include <openssl/core_names.h>
ff64702b 22#include <openssl/dh.h>
023b188c 23#include <openssl/ec.h>
25f2138b 24#include "crypto/evp.h"
924663c3 25#include "crypto/asn1.h"
3653d0c2 26#include "internal/provider.h"
706457b7 27#include "evp_local.h"
58964a49 28
f844f9eb 29#if !defined(FIPS_MODULE)
6b691a5c 30int EVP_CIPHER_param_to_asn1(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
924663c3
JZ
31{
32 return evp_cipher_param_to_asn1_ex(c, type, NULL);
33}
34
35int EVP_CIPHER_asn1_to_param(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
36{
37 return evp_cipher_asn1_to_param_ex(c, type, NULL);
38}
39
40int EVP_CIPHER_get_asn1_iv(EVP_CIPHER_CTX *ctx, ASN1_TYPE *type)
41{
42 int i = 0;
43 unsigned int l;
44
45 if (type != NULL) {
46 unsigned char iv[EVP_MAX_IV_LENGTH];
47
48 l = EVP_CIPHER_CTX_iv_length(ctx);
49 if (!ossl_assert(l <= sizeof(iv)))
50 return -1;
51 i = ASN1_TYPE_get_octetstring(type, iv, l);
52 if (i != (int)l)
53 return -1;
54
55 if (!EVP_CipherInit_ex(ctx, NULL, NULL, NULL, iv, -1))
56 return -1;
57 }
58 return i;
59}
60
61int EVP_CIPHER_set_asn1_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
62{
63 int i = 0;
64 unsigned int j;
65 unsigned char *oiv = NULL;
66
67 if (type != NULL) {
68 oiv = (unsigned char *)EVP_CIPHER_CTX_original_iv(c);
69 j = EVP_CIPHER_CTX_iv_length(c);
70 OPENSSL_assert(j <= sizeof(c->iv));
71 i = ASN1_TYPE_set_octetstring(type, oiv, j);
72 }
73 return i;
74}
75
76int evp_cipher_param_to_asn1_ex(EVP_CIPHER_CTX *c, ASN1_TYPE *type,
77 evp_cipher_aead_asn1_params *asn1_params)
0f113f3e 78{
c96399e2 79 int ret = -1; /* Assume the worst */
718b133a 80 const EVP_CIPHER *cipher = c->cipher;
0f113f3e 81
c96399e2
RL
82 /*
83 * For legacy implementations, we detect custom AlgorithmIdentifier
84 * parameter handling by checking if the function pointer
85 * cipher->set_asn1_parameters is set. We know that this pointer
86 * is NULL for provided implementations.
87 *
88 * Otherwise, for any implementation, we check the flag
89 * EVP_CIPH_FLAG_CUSTOM_ASN1. If it isn't set, we apply
90 * default AI parameter extraction.
91 *
92 * Otherwise, for provided implementations, we convert |type| to
93 * a DER encoded blob and pass to the implementation in OSSL_PARAM
94 * form.
95 *
96 * If none of the above applies, this operation is unsupported.
97 */
98 if (cipher->set_asn1_parameters != NULL) {
718b133a 99 ret = cipher->set_asn1_parameters(c, type);
c96399e2 100 } else if ((EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_CUSTOM_ASN1) == 0) {
718b133a 101 switch (EVP_CIPHER_mode(cipher)) {
2acdef5e 102 case EVP_CIPH_WRAP_MODE:
c96399e2 103 if (EVP_CIPHER_is_a(cipher, SN_id_smime_alg_CMS3DESwrap))
4ec36aff 104 ASN1_TYPE_set(type, V_ASN1_NULL, NULL);
0f113f3e 105 ret = 1;
2acdef5e
DSH
106 break;
107
108 case EVP_CIPH_GCM_MODE:
924663c3
JZ
109 ret = evp_cipher_set_asn1_aead_params(c, type, asn1_params);
110 break;
111
2acdef5e
DSH
112 case EVP_CIPH_CCM_MODE:
113 case EVP_CIPH_XTS_MODE:
114 case EVP_CIPH_OCB_MODE:
49c9c1b3 115 ret = -2;
2acdef5e
DSH
116 break;
117
118 default:
0f113f3e 119 ret = EVP_CIPHER_set_asn1_iv(c, type);
2acdef5e 120 }
c96399e2
RL
121 } else if (cipher->prov != NULL) {
122 OSSL_PARAM params[3], *p = params;
123 unsigned char *der = NULL, *derp;
124
125 /*
126 * We make two passes, the first to get the appropriate buffer size,
127 * and the second to get the actual value.
128 */
129 *p++ = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_ALG_ID,
130 NULL, 0);
131 *p = OSSL_PARAM_construct_end();
132
133 if (!EVP_CIPHER_CTX_get_params(c, params))
134 goto err;
135
136 /* ... but, we should get a return size too! */
99ea4f02
P
137 if (OSSL_PARAM_modified(params)
138 && params[0].return_size != 0
c96399e2
RL
139 && (der = OPENSSL_malloc(params[0].return_size)) != NULL) {
140 params[0].data = der;
141 params[0].data_size = params[0].return_size;
99ea4f02 142 OSSL_PARAM_set_all_unmodified(params);
c96399e2
RL
143 derp = der;
144 if (EVP_CIPHER_CTX_get_params(c, params)
99ea4f02 145 && OSSL_PARAM_modified(params)
c96399e2
RL
146 && d2i_ASN1_TYPE(&type, (const unsigned char **)&derp,
147 params[0].return_size) != NULL) {
148 ret = 1;
149 }
150 OPENSSL_free(der);
151 }
152 } else {
153 ret = -2;
154 }
155
156 err:
51ba9ebd 157 if (ret == -2)
9311d0c4 158 ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_CIPHER);
51ba9ebd 159 else if (ret <= 0)
9311d0c4 160 ERR_raise(ERR_LIB_EVP, EVP_R_CIPHER_PARAMETER_ERROR);
49c9c1b3
DO
161 if (ret < -1)
162 ret = -1;
26a7d938 163 return ret;
0f113f3e 164}
58964a49 165
924663c3
JZ
166int evp_cipher_asn1_to_param_ex(EVP_CIPHER_CTX *c, ASN1_TYPE *type,
167 evp_cipher_aead_asn1_params *asn1_params)
0f113f3e 168{
c96399e2 169 int ret = -1; /* Assume the worst */
718b133a
MC
170 const EVP_CIPHER *cipher = c->cipher;
171
c96399e2
RL
172 /*
173 * For legacy implementations, we detect custom AlgorithmIdentifier
174 * parameter handling by checking if there the function pointer
175 * cipher->get_asn1_parameters is set. We know that this pointer
176 * is NULL for provided implementations.
177 *
178 * Otherwise, for any implementation, we check the flag
179 * EVP_CIPH_FLAG_CUSTOM_ASN1. If it isn't set, we apply
180 * default AI parameter creation.
181 *
182 * Otherwise, for provided implementations, we get the AI parameter
183 * in DER encoded form from the implementation by requesting the
184 * appropriate OSSL_PARAM and converting the result to a ASN1_TYPE.
185 *
186 * If none of the above applies, this operation is unsupported.
187 */
188 if (cipher->get_asn1_parameters != NULL) {
718b133a 189 ret = cipher->get_asn1_parameters(c, type);
c96399e2 190 } else if ((EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_CUSTOM_ASN1) == 0) {
718b133a 191 switch (EVP_CIPHER_mode(cipher)) {
2acdef5e
DSH
192 case EVP_CIPH_WRAP_MODE:
193 ret = 1;
194 break;
195
196 case EVP_CIPH_GCM_MODE:
924663c3
JZ
197 ret = evp_cipher_get_asn1_aead_params(c, type, asn1_params);
198 break;
199
2acdef5e
DSH
200 case EVP_CIPH_CCM_MODE:
201 case EVP_CIPH_XTS_MODE:
202 case EVP_CIPH_OCB_MODE:
49c9c1b3 203 ret = -2;
2acdef5e
DSH
204 break;
205
206 default:
207 ret = EVP_CIPHER_get_asn1_iv(c, type);
2acdef5e 208 }
c96399e2
RL
209 } else if (cipher->prov != NULL) {
210 OSSL_PARAM params[3], *p = params;
211 unsigned char *der = NULL;
212 int derl = -1;
213
214 if ((derl = i2d_ASN1_TYPE(type, &der)) >= 0) {
215 *p++ =
216 OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_ALG_ID,
217 der, (size_t)derl);
218 *p = OSSL_PARAM_construct_end();
219 if (EVP_CIPHER_CTX_set_params(c, params))
220 ret = 1;
221 OPENSSL_free(der);
222 }
223 } else {
224 ret = -2;
225 }
226
51ba9ebd 227 if (ret == -2)
9311d0c4 228 ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_CIPHER);
51ba9ebd 229 else if (ret <= 0)
9311d0c4 230 ERR_raise(ERR_LIB_EVP, EVP_R_CIPHER_PARAMETER_ERROR);
49c9c1b3
DO
231 if (ret < -1)
232 ret = -1;
26a7d938 233 return ret;
0f113f3e 234}
58964a49 235
924663c3
JZ
236int evp_cipher_get_asn1_aead_params(EVP_CIPHER_CTX *c, ASN1_TYPE *type,
237 evp_cipher_aead_asn1_params *asn1_params)
0f113f3e
MC
238{
239 int i = 0;
924663c3
JZ
240 long tl;
241 unsigned char iv[EVP_MAX_IV_LENGTH];
0f113f3e 242
924663c3
JZ
243 if (type == NULL || asn1_params == NULL)
244 return 0;
718b133a 245
924663c3
JZ
246 i = asn1_type_get_octetstring_int(type, &tl, NULL, EVP_MAX_IV_LENGTH);
247 if (i <= 0)
248 return -1;
249 asn1_type_get_octetstring_int(type, &tl, iv, i);
250
251 memcpy(asn1_params->iv, iv, i);
252 asn1_params->iv_len = i;
718b133a 253
26a7d938 254 return i;
0f113f3e 255}
58964a49 256
924663c3
JZ
257int evp_cipher_set_asn1_aead_params(EVP_CIPHER_CTX *c, ASN1_TYPE *type,
258 evp_cipher_aead_asn1_params *asn1_params)
0f113f3e 259{
924663c3
JZ
260 if (type == NULL || asn1_params == NULL)
261 return 0;
0f113f3e 262
924663c3
JZ
263 return asn1_type_set_octetstring_int(type, asn1_params->tag_len,
264 asn1_params->iv, asn1_params->iv_len);
0f113f3e 265}
f844f9eb 266#endif /* !defined(FIPS_MODULE) */
884e8ec6
DSH
267
268/* Convert the various cipher NIDs and dummies to a proper OID NID */
84fa704c 269int EVP_CIPHER_type(const EVP_CIPHER *ctx)
884e8ec6 270{
0f113f3e 271 int nid;
0f113f3e 272 nid = EVP_CIPHER_nid(ctx);
884e8ec6 273
0f113f3e 274 switch (nid) {
884e8ec6 275
0f113f3e
MC
276 case NID_rc2_cbc:
277 case NID_rc2_64_cbc:
278 case NID_rc2_40_cbc:
884e8ec6 279
0f113f3e 280 return NID_rc2_cbc;
884e8ec6 281
0f113f3e
MC
282 case NID_rc4:
283 case NID_rc4_40:
884e8ec6 284
0f113f3e 285 return NID_rc4;
884e8ec6 286
0f113f3e
MC
287 case NID_aes_128_cfb128:
288 case NID_aes_128_cfb8:
289 case NID_aes_128_cfb1:
8d1ebe0b 290
0f113f3e 291 return NID_aes_128_cfb128;
8d1ebe0b 292
0f113f3e
MC
293 case NID_aes_192_cfb128:
294 case NID_aes_192_cfb8:
295 case NID_aes_192_cfb1:
8d1ebe0b 296
0f113f3e 297 return NID_aes_192_cfb128;
8d1ebe0b 298
0f113f3e
MC
299 case NID_aes_256_cfb128:
300 case NID_aes_256_cfb8:
301 case NID_aes_256_cfb1:
8d1ebe0b 302
0f113f3e 303 return NID_aes_256_cfb128;
8d1ebe0b 304
0f113f3e
MC
305 case NID_des_cfb64:
306 case NID_des_cfb8:
307 case NID_des_cfb1:
8d1ebe0b 308
0f113f3e 309 return NID_des_cfb64;
8d1ebe0b 310
0f113f3e
MC
311 case NID_des_ede3_cfb64:
312 case NID_des_ede3_cfb8:
313 case NID_des_ede3_cfb1:
7e765bf2 314
0f113f3e 315 return NID_des_cfb64;
7e765bf2 316
0f113f3e 317 default:
f844f9eb 318#ifdef FIPS_MODULE
319e518a
MC
319 return NID_undef;
320#else
321 {
322 /* Check it has an OID and it is valid */
323 ASN1_OBJECT *otmp = OBJ_nid2obj(nid);
324
325 if (OBJ_get0_data(otmp) == NULL)
326 nid = NID_undef;
327 ASN1_OBJECT_free(otmp);
328 return nid;
329 }
330#endif
0f113f3e 331 }
884e8ec6
DSH
332}
333
3c957bcd 334int evp_cipher_cache_constants(EVP_CIPHER *cipher)
0f113f3e 335{
1c3ace68 336 int ok;
3c957bcd
SL
337 size_t ivlen = 0;
338 size_t blksz = 0;
339 size_t keylen = 0;
340 unsigned int mode = 0;
341 unsigned long flags = 0;
342 OSSL_PARAM params[6];
343
344 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_BLOCK_SIZE, &blksz);
345 params[1] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN, &ivlen);
346 params[2] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &keylen);
347 params[3] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_MODE, &mode);
348 params[4] = OSSL_PARAM_construct_ulong(OSSL_CIPHER_PARAM_FLAGS, &flags);
349 params[5] = OSSL_PARAM_construct_end();
459b15d4 350 ok = evp_do_ciph_getparams(cipher, params);
3c957bcd
SL
351 if (ok) {
352 /* Provided implementations may have a custom cipher_cipher */
353 if (cipher->prov != NULL && cipher->ccipher != NULL)
354 flags |= EVP_CIPH_FLAG_CUSTOM_CIPHER;
355 cipher->block_size = blksz;
356 cipher->iv_len = ivlen;
357 cipher->key_len = keylen;
358 cipher->flags = flags | mode;
359 }
360 return ok;
361}
13273237 362
3c957bcd
SL
363int EVP_CIPHER_block_size(const EVP_CIPHER *cipher)
364{
365 return cipher->block_size;
0f113f3e 366}
7806f3dd 367
6343829a 368int EVP_CIPHER_CTX_block_size(const EVP_CIPHER_CTX *ctx)
0f113f3e 369{
718b133a 370 return EVP_CIPHER_block_size(ctx->cipher);
0f113f3e 371}
7806f3dd 372
e79f8773
RL
373int EVP_CIPHER_impl_ctx_size(const EVP_CIPHER *e)
374{
375 return e->ctx_size;
376}
377
0f113f3e
MC
378int EVP_Cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
379 const unsigned char *in, unsigned int inl)
380{
718b133a 381 if (ctx->cipher->prov != NULL) {
f7397f0d
RL
382 /*
383 * If the provided implementation has a ccipher function, we use it,
384 * and translate its return value like this: 0 => -1, 1 => outlen
385 *
386 * Otherwise, we call the cupdate function if in != NULL, or cfinal
387 * if in == NULL. Regardless of which, we return what we got.
388 */
389 int ret = -1;
390 size_t outl = 0;
391 size_t blocksize = EVP_CIPHER_CTX_block_size(ctx);
f79858ac 392
718b133a 393 if (ctx->cipher->ccipher != NULL)
f7397f0d
RL
394 ret = ctx->cipher->ccipher(ctx->provctx, out, &outl,
395 inl + (blocksize == 1 ? 0 : blocksize),
396 in, (size_t)inl)
397 ? (int)outl : -1;
398 else if (in != NULL)
399 ret = ctx->cipher->cupdate(ctx->provctx, out, &outl,
400 inl + (blocksize == 1 ? 0 : blocksize),
401 in, (size_t)inl);
402 else
403 ret = ctx->cipher->cfinal(ctx->provctx, out, &outl,
404 blocksize == 1 ? 0 : blocksize);
405
406 return ret;
718b133a
MC
407 }
408
0f113f3e
MC
409 return ctx->cipher->do_cipher(ctx, out, in, inl);
410}
7806f3dd
NL
411
412const EVP_CIPHER *EVP_CIPHER_CTX_cipher(const EVP_CIPHER_CTX *ctx)
0f113f3e
MC
413{
414 return ctx->cipher;
415}
7806f3dd 416
83b06347
RL
417int EVP_CIPHER_CTX_encrypting(const EVP_CIPHER_CTX *ctx)
418{
419 return ctx->encrypt;
420}
421
7806f3dd 422unsigned long EVP_CIPHER_flags(const EVP_CIPHER *cipher)
0f113f3e 423{
3c957bcd 424 return cipher->flags;
0f113f3e 425}
7806f3dd 426
7806f3dd 427void *EVP_CIPHER_CTX_get_app_data(const EVP_CIPHER_CTX *ctx)
0f113f3e
MC
428{
429 return ctx->app_data;
430}
7806f3dd
NL
431
432void EVP_CIPHER_CTX_set_app_data(EVP_CIPHER_CTX *ctx, void *data)
0f113f3e
MC
433{
434 ctx->app_data = data;
435}
7806f3dd 436
44ab2dfd 437void *EVP_CIPHER_CTX_get_cipher_data(const EVP_CIPHER_CTX *ctx)
83b06347
RL
438{
439 return ctx->cipher_data;
440}
441
98ee7543
MC
442void *EVP_CIPHER_CTX_set_cipher_data(EVP_CIPHER_CTX *ctx, void *cipher_data)
443{
444 void *old_cipher_data;
445
446 old_cipher_data = ctx->cipher_data;
447 ctx->cipher_data = cipher_data;
448
449 return old_cipher_data;
450}
451
6343829a 452int EVP_CIPHER_iv_length(const EVP_CIPHER *cipher)
0f113f3e 453{
3c957bcd 454 return cipher->iv_len;
0f113f3e 455}
7806f3dd 456
6343829a 457int EVP_CIPHER_CTX_iv_length(const EVP_CIPHER_CTX *ctx)
0f113f3e 458{
88d87082
SL
459 int rv, len = EVP_CIPHER_iv_length(ctx->cipher);
460 size_t v = len;
a672a02a
SL
461 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
462
1c3ace68 463 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN, &v);
7dddf2fc
SL
464 rv = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
465 if (rv == EVP_CTRL_RET_UNSUPPORTED)
466 goto legacy;
1c3ace68 467 return rv != 0 ? (int)v : -1;
7dddf2fc
SL
468 /* TODO (3.0) Remove legacy support */
469legacy:
470 if ((EVP_CIPHER_flags(ctx->cipher) & EVP_CIPH_CUSTOM_IV_LENGTH) != 0) {
471 rv = EVP_CIPHER_CTX_ctrl((EVP_CIPHER_CTX *)ctx, EVP_CTRL_GET_IVLEN,
472 0, &len);
88d87082 473 return (rv == 1) ? len : -1;
7dddf2fc 474 }
88d87082 475 return len;
0f113f3e 476}
7806f3dd 477
dc64dc2e
SL
478int EVP_CIPHER_CTX_tag_length(const EVP_CIPHER_CTX *ctx)
479{
480 int ret;
481 size_t v = 0;
482 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
483
484 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_TAGLEN, &v);
485 ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
486 return ret == 1 ? (int)v : 0;
487}
488
79f4417e 489#ifndef OPENSSL_NO_DEPRECATED_3_0
83b06347
RL
490const unsigned char *EVP_CIPHER_CTX_original_iv(const EVP_CIPHER_CTX *ctx)
491{
089cb623
SL
492 int ok;
493 const unsigned char *v = ctx->oiv;
494 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
495
496 params[0] =
497 OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_IV,
498 (void **)&v, sizeof(ctx->oiv));
499 ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
500
501 return ok != 0 ? v : NULL;
83b06347
RL
502}
503
13273237
RL
504/*
505 * OSSL_PARAM_OCTET_PTR gets us the pointer to the running IV in the provider
506 */
83b06347
RL
507const unsigned char *EVP_CIPHER_CTX_iv(const EVP_CIPHER_CTX *ctx)
508{
459b15d4 509 int ok;
13273237 510 const unsigned char *v = ctx->iv;
459b15d4 511 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
13273237 512
459b15d4 513 params[0] =
84890268 514 OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_IV_STATE, (void **)&v,
459b15d4
SL
515 sizeof(ctx->iv));
516 ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
517
518 return ok != 0 ? v : NULL;
83b06347
RL
519}
520
521unsigned char *EVP_CIPHER_CTX_iv_noconst(EVP_CIPHER_CTX *ctx)
522{
459b15d4 523 int ok;
13273237 524 unsigned char *v = ctx->iv;
459b15d4
SL
525 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
526
527 params[0] =
84890268 528 OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_IV_STATE, (void **)&v,
459b15d4
SL
529 sizeof(ctx->iv));
530 ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
13273237 531
459b15d4 532 return ok != 0 ? v : NULL;
83b06347 533}
79f4417e
BK
534#endif /* OPENSSL_NO_DEPRECATED_3_0_0 */
535
536int EVP_CIPHER_CTX_get_iv_state(EVP_CIPHER_CTX *ctx, void *buf, size_t len)
537{
538 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
539
540 params[0] =
541 OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_IV_STATE, buf, len);
542 return evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
543}
544
545int EVP_CIPHER_CTX_get_iv(EVP_CIPHER_CTX *ctx, void *buf, size_t len)
546{
547 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
548
549 params[0] =
550 OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_IV, buf, len);
551 return evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
552}
83b06347
RL
553
554unsigned char *EVP_CIPHER_CTX_buf_noconst(EVP_CIPHER_CTX *ctx)
555{
556 return ctx->buf;
557}
558
559int EVP_CIPHER_CTX_num(const EVP_CIPHER_CTX *ctx)
560{
1c3ace68
SL
561 int ok;
562 unsigned int v = (unsigned int)ctx->num;
459b15d4 563 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
13273237 564
1c3ace68 565 params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_NUM, &v);
459b15d4
SL
566 ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
567
1c3ace68 568 return ok != 0 ? (int)v : EVP_CTRL_RET_UNSUPPORTED;
83b06347
RL
569}
570
13273237 571int EVP_CIPHER_CTX_set_num(EVP_CIPHER_CTX *ctx, int num)
83b06347 572{
459b15d4 573 int ok;
1c3ace68 574 unsigned int n = (unsigned int)num;
459b15d4 575 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
13273237 576
1c3ace68 577 params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_NUM, &n);
459b15d4
SL
578 ok = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->provctx, params);
579
580 if (ok != 0)
1c3ace68 581 ctx->num = (int)n;
13273237 582 return ok != 0;
83b06347
RL
583}
584
6343829a 585int EVP_CIPHER_key_length(const EVP_CIPHER *cipher)
0f113f3e 586{
3c957bcd 587 return cipher->key_len;
0f113f3e 588}
7806f3dd 589
6343829a 590int EVP_CIPHER_CTX_key_length(const EVP_CIPHER_CTX *ctx)
0f113f3e 591{
1c3ace68
SL
592 int ok;
593 size_t v = ctx->key_len;
459b15d4
SL
594 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
595
1c3ace68 596 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &v);
459b15d4 597 ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
13273237 598
1c3ace68 599 return ok != 0 ? (int)v : EVP_CTRL_RET_UNSUPPORTED;
0f113f3e 600}
7806f3dd
NL
601
602int EVP_CIPHER_nid(const EVP_CIPHER *cipher)
0f113f3e
MC
603{
604 return cipher->nid;
605}
7806f3dd
NL
606
607int EVP_CIPHER_CTX_nid(const EVP_CIPHER_CTX *ctx)
0f113f3e
MC
608{
609 return ctx->cipher->nid;
610}
7806f3dd 611
7cfa1717
RL
612int EVP_CIPHER_is_a(const EVP_CIPHER *cipher, const char *name)
613{
e4a1d023
RL
614 if (cipher->prov != NULL)
615 return evp_is_a(cipher->prov, cipher->name_id, NULL, name);
616 return evp_is_a(NULL, 0, EVP_CIPHER_name(cipher), name);
7cfa1717
RL
617}
618
506cb0f6
RL
619int EVP_CIPHER_number(const EVP_CIPHER *cipher)
620{
621 return cipher->name_id;
622}
623
c750bc08
RL
624const char *EVP_CIPHER_name(const EVP_CIPHER *cipher)
625{
626 if (cipher->prov != NULL)
f7c16d48 627 return evp_first_name(cipher->prov, cipher->name_id);
f844f9eb 628#ifndef FIPS_MODULE
c750bc08
RL
629 return OBJ_nid2sn(EVP_CIPHER_nid(cipher));
630#else
631 return NULL;
632#endif
633}
634
f651c727
RL
635void EVP_CIPHER_names_do_all(const EVP_CIPHER *cipher,
636 void (*fn)(const char *name, void *data),
637 void *data)
638{
639 if (cipher->prov != NULL)
640 evp_names_do_all(cipher->prov, cipher->name_id, fn, data);
641}
642
1d2622d4
RL
643const OSSL_PROVIDER *EVP_CIPHER_provider(const EVP_CIPHER *cipher)
644{
645 return cipher->prov;
646}
647
718b133a
MC
648int EVP_CIPHER_mode(const EVP_CIPHER *cipher)
649{
3c957bcd 650 return EVP_CIPHER_flags(cipher) & EVP_CIPH_MODE;
459b15d4 651}
718b133a 652
251e610c
RL
653int EVP_MD_is_a(const EVP_MD *md, const char *name)
654{
e4a1d023
RL
655 if (md->prov != NULL)
656 return evp_is_a(md->prov, md->name_id, NULL, name);
657 return evp_is_a(NULL, 0, EVP_MD_name(md), name);
251e610c
RL
658}
659
506cb0f6
RL
660int EVP_MD_number(const EVP_MD *md)
661{
662 return md->name_id;
663}
664
c750bc08
RL
665const char *EVP_MD_name(const EVP_MD *md)
666{
667 if (md->prov != NULL)
f7c16d48 668 return evp_first_name(md->prov, md->name_id);
f844f9eb 669#ifndef FIPS_MODULE
c750bc08
RL
670 return OBJ_nid2sn(EVP_MD_nid(md));
671#else
672 return NULL;
673#endif
674}
675
f651c727
RL
676void EVP_MD_names_do_all(const EVP_MD *md,
677 void (*fn)(const char *name, void *data),
678 void *data)
679{
680 if (md->prov != NULL)
681 evp_names_do_all(md->prov, md->name_id, fn, data);
682}
683
1d2622d4
RL
684const OSSL_PROVIDER *EVP_MD_provider(const EVP_MD *md)
685{
686 return md->prov;
687}
688
0f113f3e
MC
689int EVP_MD_block_size(const EVP_MD *md)
690{
1c3ace68 691 int ok;
1f9ad4f9 692 size_t v;
6a3b7c68
RL
693 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
694
7556b9df 695 if (md == NULL) {
9311d0c4 696 ERR_raise(ERR_LIB_EVP, EVP_R_MESSAGE_DIGEST_IS_NULL);
7556b9df
MC
697 return -1;
698 }
1f9ad4f9 699 v = md->block_size;
1c3ace68 700 params[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_BLOCK_SIZE, &v);
6a3b7c68 701 ok = evp_do_md_getparams(md, params);
7556b9df 702
1c3ace68 703 return ok != 0 ? (int)v : -1;
0f113f3e 704}
7806f3dd
NL
705
706int EVP_MD_type(const EVP_MD *md)
0f113f3e
MC
707{
708 return md->type;
709}
7806f3dd
NL
710
711int EVP_MD_pkey_type(const EVP_MD *md)
0f113f3e
MC
712{
713 return md->pkey_type;
714}
7806f3dd 715
6343829a 716int EVP_MD_size(const EVP_MD *md)
0f113f3e 717{
1c3ace68 718 int ok;
1f9ad4f9 719 size_t v;
6a3b7c68
RL
720 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
721
722 if (md == NULL) {
9311d0c4 723 ERR_raise(ERR_LIB_EVP, EVP_R_MESSAGE_DIGEST_IS_NULL);
0f113f3e
MC
724 return -1;
725 }
1f9ad4f9 726 v = md->md_size;
1c3ace68 727 params[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_SIZE, &v);
6a3b7c68 728 ok = evp_do_md_getparams(md, params);
8c8cf0d9 729
1c3ace68 730 return ok != 0 ? (int)v : -1;
0f113f3e 731}
7806f3dd 732
e5fa864f 733unsigned long EVP_MD_flags(const EVP_MD *md)
0f113f3e 734{
6a3b7c68
RL
735 int ok;
736 unsigned long v = md->flags;
737 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
738
739 params[0] = OSSL_PARAM_construct_ulong(OSSL_CIPHER_PARAM_FLAGS, &v);
740 ok = evp_do_md_getparams(md, params);
741
742 return ok != 0 ? v : 0;
0f113f3e 743}
e5fa864f 744
2db6bf6f
RL
745EVP_MD *EVP_MD_meth_new(int md_type, int pkey_type)
746{
3fd70262 747 EVP_MD *md = evp_md_new();
43ecb9c3 748
2db6bf6f
RL
749 if (md != NULL) {
750 md->type = md_type;
751 md->pkey_type = pkey_type;
752 }
753 return md;
754}
df05f2ce 755
2db6bf6f
RL
756EVP_MD *EVP_MD_meth_dup(const EVP_MD *md)
757{
3fd70262
RL
758 EVP_MD *to = NULL;
759
760 /*
761 * Non-legacy EVP_MDs can't be duplicated like this.
762 * Use EVP_MD_up_ref() instead.
763 */
764 if (md->prov != NULL)
765 return NULL;
43ecb9c3 766
3fd70262 767 if ((to = EVP_MD_meth_new(md->type, md->pkey_type)) != NULL) {
df05f2ce 768 CRYPTO_RWLOCK *lock = to->lock;
3fd70262 769
2db6bf6f 770 memcpy(to, md, sizeof(*to));
df05f2ce
MC
771 to->lock = lock;
772 }
2db6bf6f
RL
773 return to;
774}
3653d0c2 775
2db6bf6f
RL
776void EVP_MD_meth_free(EVP_MD *md)
777{
3fd70262 778 EVP_MD_free(md);
2db6bf6f
RL
779}
780int EVP_MD_meth_set_input_blocksize(EVP_MD *md, int blocksize)
781{
8bbc7f22
DB
782 if (md->block_size != 0)
783 return 0;
784
2db6bf6f
RL
785 md->block_size = blocksize;
786 return 1;
787}
788int EVP_MD_meth_set_result_size(EVP_MD *md, int resultsize)
789{
8bbc7f22
DB
790 if (md->md_size != 0)
791 return 0;
792
2db6bf6f
RL
793 md->md_size = resultsize;
794 return 1;
795}
796int EVP_MD_meth_set_app_datasize(EVP_MD *md, int datasize)
797{
8bbc7f22
DB
798 if (md->ctx_size != 0)
799 return 0;
800
2db6bf6f
RL
801 md->ctx_size = datasize;
802 return 1;
803}
804int EVP_MD_meth_set_flags(EVP_MD *md, unsigned long flags)
805{
8bbc7f22
DB
806 if (md->flags != 0)
807 return 0;
808
2db6bf6f
RL
809 md->flags = flags;
810 return 1;
811}
812int EVP_MD_meth_set_init(EVP_MD *md, int (*init)(EVP_MD_CTX *ctx))
813{
8bbc7f22
DB
814 if (md->init != NULL)
815 return 0;
816
2db6bf6f
RL
817 md->init = init;
818 return 1;
819}
820int EVP_MD_meth_set_update(EVP_MD *md, int (*update)(EVP_MD_CTX *ctx,
821 const void *data,
822 size_t count))
823{
8bbc7f22
DB
824 if (md->update != NULL)
825 return 0;
826
2db6bf6f
RL
827 md->update = update;
828 return 1;
829}
830int EVP_MD_meth_set_final(EVP_MD *md, int (*final)(EVP_MD_CTX *ctx,
831 unsigned char *md))
832{
8bbc7f22
DB
833 if (md->final != NULL)
834 return 0;
835
2db6bf6f
RL
836 md->final = final;
837 return 1;
838}
839int EVP_MD_meth_set_copy(EVP_MD *md, int (*copy)(EVP_MD_CTX *to,
840 const EVP_MD_CTX *from))
841{
8bbc7f22
DB
842 if (md->copy != NULL)
843 return 0;
844
2db6bf6f
RL
845 md->copy = copy;
846 return 1;
847}
848int EVP_MD_meth_set_cleanup(EVP_MD *md, int (*cleanup)(EVP_MD_CTX *ctx))
849{
8bbc7f22
DB
850 if (md->cleanup != NULL)
851 return 0;
852
2db6bf6f
RL
853 md->cleanup = cleanup;
854 return 1;
855}
856int EVP_MD_meth_set_ctrl(EVP_MD *md, int (*ctrl)(EVP_MD_CTX *ctx, int cmd,
857 int p1, void *p2))
858{
8bbc7f22
DB
859 if (md->md_ctrl != NULL)
860 return 0;
861
2db6bf6f
RL
862 md->md_ctrl = ctrl;
863 return 1;
864}
865
866int EVP_MD_meth_get_input_blocksize(const EVP_MD *md)
867{
868 return md->block_size;
869}
870int EVP_MD_meth_get_result_size(const EVP_MD *md)
871{
872 return md->md_size;
873}
874int EVP_MD_meth_get_app_datasize(const EVP_MD *md)
875{
876 return md->ctx_size;
877}
878unsigned long EVP_MD_meth_get_flags(const EVP_MD *md)
879{
8bfa99f0 880 return md->flags;
2db6bf6f
RL
881}
882int (*EVP_MD_meth_get_init(const EVP_MD *md))(EVP_MD_CTX *ctx)
883{
884 return md->init;
885}
886int (*EVP_MD_meth_get_update(const EVP_MD *md))(EVP_MD_CTX *ctx,
887 const void *data,
888 size_t count)
889{
890 return md->update;
891}
892int (*EVP_MD_meth_get_final(const EVP_MD *md))(EVP_MD_CTX *ctx,
893 unsigned char *md)
894{
895 return md->final;
896}
897int (*EVP_MD_meth_get_copy(const EVP_MD *md))(EVP_MD_CTX *to,
898 const EVP_MD_CTX *from)
899{
900 return md->copy;
901}
902int (*EVP_MD_meth_get_cleanup(const EVP_MD *md))(EVP_MD_CTX *ctx)
903{
904 return md->cleanup;
905}
906int (*EVP_MD_meth_get_ctrl(const EVP_MD *md))(EVP_MD_CTX *ctx, int cmd,
907 int p1, void *p2)
908{
909 return md->md_ctrl;
910}
911
7806f3dd 912const EVP_MD *EVP_MD_CTX_md(const EVP_MD_CTX *ctx)
0f113f3e 913{
b7c913c8 914 if (ctx == NULL)
0f113f3e 915 return NULL;
b7c913c8 916 return ctx->reqdigest;
0f113f3e 917}
7806f3dd 918
7638370c
RL
919EVP_PKEY_CTX *EVP_MD_CTX_pkey_ctx(const EVP_MD_CTX *ctx)
920{
921 return ctx->pctx;
922}
923
f844f9eb 924#if !defined(FIPS_MODULE)
319e518a 925/* TODO(3.0): EVP_DigestSign* not yet supported in FIPS module */
00902d94
PY
926void EVP_MD_CTX_set_pkey_ctx(EVP_MD_CTX *ctx, EVP_PKEY_CTX *pctx)
927{
81c79453
PY
928 /*
929 * it's reasonable to set NULL pctx (a.k.a clear the ctx->pctx), so
930 * we have to deal with the cleanup job here.
931 */
932 if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX))
933 EVP_PKEY_CTX_free(ctx->pctx);
934
00902d94 935 ctx->pctx = pctx;
81c79453
PY
936
937 if (pctx != NULL) {
938 /* make sure pctx is not freed when destroying EVP_MD_CTX */
939 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
940 } else {
941 EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
942 }
00902d94 943}
f844f9eb 944#endif /* !defined(FIPS_MODULE) */
00902d94 945
7638370c
RL
946void *EVP_MD_CTX_md_data(const EVP_MD_CTX *ctx)
947{
948 return ctx->md_data;
949}
950
951int (*EVP_MD_CTX_update_fn(EVP_MD_CTX *ctx))(EVP_MD_CTX *ctx,
952 const void *data, size_t count)
953{
954 return ctx->update;
955}
956
957void EVP_MD_CTX_set_update_fn(EVP_MD_CTX *ctx,
958 int (*update) (EVP_MD_CTX *ctx,
959 const void *data, size_t count))
960{
961 ctx->update = update;
962}
963
7806f3dd 964void EVP_MD_CTX_set_flags(EVP_MD_CTX *ctx, int flags)
0f113f3e
MC
965{
966 ctx->flags |= flags;
967}
7806f3dd
NL
968
969void EVP_MD_CTX_clear_flags(EVP_MD_CTX *ctx, int flags)
0f113f3e
MC
970{
971 ctx->flags &= ~flags;
972}
7806f3dd
NL
973
974int EVP_MD_CTX_test_flags(const EVP_MD_CTX *ctx, int flags)
0f113f3e
MC
975{
976 return (ctx->flags & flags);
977}
e92f9f45
DSH
978
979void EVP_CIPHER_CTX_set_flags(EVP_CIPHER_CTX *ctx, int flags)
0f113f3e
MC
980{
981 ctx->flags |= flags;
982}
e92f9f45
DSH
983
984void EVP_CIPHER_CTX_clear_flags(EVP_CIPHER_CTX *ctx, int flags)
0f113f3e
MC
985{
986 ctx->flags &= ~flags;
987}
e92f9f45
DSH
988
989int EVP_CIPHER_CTX_test_flags(const EVP_CIPHER_CTX *ctx, int flags)
0f113f3e
MC
990{
991 return (ctx->flags & flags);
992}
f842b6b2 993
11a1b341
MC
994int EVP_PKEY_CTX_set_group_name(EVP_PKEY_CTX *ctx, const char *name)
995{
996 OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
997 OSSL_PARAM *p = params;
998
023b188c
MC
999 if (ctx == NULL) {
1000 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1001 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1002 return -2;
1003 }
1004
1005 if (!EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
1006#ifndef FIPS_MODULE
1007 int nid;
1008
1009 /* Could be a legacy key, try and convert to a ctrl */
1010 if (ctx->pmeth != NULL && (nid = OBJ_txt2nid(name)) != NID_undef) {
1011# ifndef OPENSSL_NO_DH
1012 if (ctx->pmeth->pkey_id == EVP_PKEY_DH)
1013 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH,
1014 EVP_PKEY_OP_PARAMGEN
1015 | EVP_PKEY_OP_KEYGEN,
1016 EVP_PKEY_CTRL_DH_NID, nid, NULL);
1017# endif
1018# ifndef OPENSSL_NO_EC
1019 if (ctx->pmeth->pkey_id == EVP_PKEY_EC)
1020 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC,
1021 EVP_PKEY_OP_PARAMGEN|EVP_PKEY_OP_KEYGEN,
1022 EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID,
1023 nid, NULL);
1024# endif
1025 }
1026#endif
11a1b341
MC
1027 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1028 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1029 return -2;
1030 }
1031
1032 if (name == NULL)
1033 return -1;
1034
1035 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
1036 (char *)name, 0);
1037 return EVP_PKEY_CTX_set_params(ctx, params);
1038}
1039
1040int EVP_PKEY_CTX_get_group_name(EVP_PKEY_CTX *ctx, char *name, size_t namelen)
1041{
1042 OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
1043 OSSL_PARAM *p = params;
1044
1045 if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
023b188c 1046 /* There is no legacy support for this */
11a1b341
MC
1047 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1048 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
1049 return -2;
1050 }
1051
1052 if (name == NULL)
1053 return -1;
1054
1055 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
1056 name, namelen);
1057 if (!EVP_PKEY_CTX_get_params(ctx, params))
1058 return -1;
1059 return 1;
1060}