]>
Commit | Line | Data |
---|---|---|
62867571 | 1 | /* |
fecb3aae | 2 | * Copyright 1995-2022 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 | 17 | #include <string.h> |
b39fc560 | 18 | #include "internal/cryptlib.h" |
ec577822 BM |
19 | #include <openssl/evp.h> |
20 | #include <openssl/objects.h> | |
718b133a MC |
21 | #include <openssl/params.h> |
22 | #include <openssl/core_names.h> | |
f9253152 | 23 | #include <openssl/rsa.h> |
ff64702b | 24 | #include <openssl/dh.h> |
023b188c | 25 | #include <openssl/ec.h> |
25f2138b | 26 | #include "crypto/evp.h" |
b807c2fb | 27 | #include "crypto/cryptlib.h" |
3653d0c2 | 28 | #include "internal/provider.h" |
706457b7 | 29 | #include "evp_local.h" |
58964a49 | 30 | |
f844f9eb | 31 | #if !defined(FIPS_MODULE) |
3f773c91 | 32 | # include "crypto/asn1.h" |
f9253152 | 33 | |
6b691a5c | 34 | int 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 | ||
39 | int 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 | ||
44 | int 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 | ||
65 | int 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 | ||
80 | int 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 |
171 | int 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 |
242 | int 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 |
263 | int 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 | 276 | int 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 | 341 | int evp_cipher_cache_constants(EVP_CIPHER *cipher) |
0f113f3e | 342 | { |
f41fd10d | 343 | int ok, aead = 0, custom_iv = 0, cts = 0, multiblock = 0, randkey = 0; |
3c957bcd SL |
344 | size_t ivlen = 0; |
345 | size_t blksz = 0; | |
346 | size_t keylen = 0; | |
347 | unsigned int mode = 0; | |
f41fd10d | 348 | OSSL_PARAM params[10]; |
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); | |
f41fd10d SL |
360 | params[8] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_HAS_RAND_KEY, |
361 | &randkey); | |
362 | params[9] = OSSL_PARAM_construct_end(); | |
4885ecff | 363 | ok = evp_do_ciph_getparams(cipher, params) > 0; |
3c957bcd | 364 | if (ok) { |
3c957bcd SL |
365 | cipher->block_size = blksz; |
366 | cipher->iv_len = ivlen; | |
367 | cipher->key_len = keylen; | |
a054d15c SL |
368 | cipher->flags = mode; |
369 | if (aead) | |
370 | cipher->flags |= EVP_CIPH_FLAG_AEAD_CIPHER; | |
371 | if (custom_iv) | |
372 | cipher->flags |= EVP_CIPH_CUSTOM_IV; | |
373 | if (cts) | |
374 | cipher->flags |= EVP_CIPH_FLAG_CTS; | |
375 | if (multiblock) | |
376 | cipher->flags |= EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK; | |
4885ecff | 377 | if (cipher->ccipher != NULL) |
a054d15c | 378 | cipher->flags |= EVP_CIPH_FLAG_CUSTOM_CIPHER; |
f41fd10d SL |
379 | if (randkey) |
380 | cipher->flags |= EVP_CIPH_RAND_KEY; | |
592ea4ba JS |
381 | if (OSSL_PARAM_locate_const(EVP_CIPHER_gettable_ctx_params(cipher), |
382 | OSSL_CIPHER_PARAM_ALGORITHM_ID_PARAMS)) | |
383 | cipher->flags |= EVP_CIPH_FLAG_CUSTOM_ASN1; | |
3c957bcd SL |
384 | } |
385 | return ok; | |
386 | } | |
13273237 | 387 | |
ed576acd | 388 | int EVP_CIPHER_get_block_size(const EVP_CIPHER *cipher) |
3c957bcd SL |
389 | { |
390 | return cipher->block_size; | |
0f113f3e | 391 | } |
7806f3dd | 392 | |
ed576acd | 393 | int EVP_CIPHER_CTX_get_block_size(const EVP_CIPHER_CTX *ctx) |
0f113f3e | 394 | { |
ed576acd | 395 | return EVP_CIPHER_get_block_size(ctx->cipher); |
0f113f3e | 396 | } |
7806f3dd | 397 | |
e79f8773 RL |
398 | int EVP_CIPHER_impl_ctx_size(const EVP_CIPHER *e) |
399 | { | |
400 | return e->ctx_size; | |
401 | } | |
402 | ||
0f113f3e MC |
403 | int EVP_Cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
404 | const unsigned char *in, unsigned int inl) | |
405 | { | |
718b133a | 406 | if (ctx->cipher->prov != NULL) { |
f7397f0d RL |
407 | /* |
408 | * If the provided implementation has a ccipher function, we use it, | |
409 | * and translate its return value like this: 0 => -1, 1 => outlen | |
410 | * | |
411 | * Otherwise, we call the cupdate function if in != NULL, or cfinal | |
412 | * if in == NULL. Regardless of which, we return what we got. | |
413 | */ | |
414 | int ret = -1; | |
415 | size_t outl = 0; | |
ed576acd | 416 | size_t blocksize = EVP_CIPHER_CTX_get_block_size(ctx); |
f79858ac | 417 | |
718b133a | 418 | if (ctx->cipher->ccipher != NULL) |
7c14d0c1 | 419 | ret = ctx->cipher->ccipher(ctx->algctx, out, &outl, |
f7397f0d RL |
420 | inl + (blocksize == 1 ? 0 : blocksize), |
421 | in, (size_t)inl) | |
422 | ? (int)outl : -1; | |
423 | else if (in != NULL) | |
7c14d0c1 | 424 | ret = ctx->cipher->cupdate(ctx->algctx, out, &outl, |
f7397f0d RL |
425 | inl + (blocksize == 1 ? 0 : blocksize), |
426 | in, (size_t)inl); | |
427 | else | |
7c14d0c1 | 428 | ret = ctx->cipher->cfinal(ctx->algctx, out, &outl, |
f7397f0d RL |
429 | blocksize == 1 ? 0 : blocksize); |
430 | ||
431 | return ret; | |
718b133a MC |
432 | } |
433 | ||
0f113f3e MC |
434 | return ctx->cipher->do_cipher(ctx, out, in, inl); |
435 | } | |
7806f3dd | 436 | |
f6c95e46 | 437 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
7806f3dd | 438 | const EVP_CIPHER *EVP_CIPHER_CTX_cipher(const EVP_CIPHER_CTX *ctx) |
0f113f3e | 439 | { |
f6c95e46 RS |
440 | if (ctx == NULL) |
441 | return NULL; | |
442 | return ctx->cipher; | |
443 | } | |
444 | #endif | |
445 | ||
446 | const EVP_CIPHER *EVP_CIPHER_CTX_get0_cipher(const EVP_CIPHER_CTX *ctx) | |
447 | { | |
448 | if (ctx == NULL) | |
449 | return NULL; | |
0f113f3e MC |
450 | return ctx->cipher; |
451 | } | |
7806f3dd | 452 | |
f6c95e46 RS |
453 | EVP_CIPHER *EVP_CIPHER_CTX_get1_cipher(EVP_CIPHER_CTX *ctx) |
454 | { | |
455 | EVP_CIPHER *cipher; | |
456 | ||
457 | if (ctx == NULL) | |
458 | return NULL; | |
459 | cipher = (EVP_CIPHER *)ctx->cipher; | |
460 | if (!EVP_CIPHER_up_ref(cipher)) | |
461 | return NULL; | |
462 | return cipher; | |
463 | } | |
464 | ||
ed576acd | 465 | int EVP_CIPHER_CTX_is_encrypting(const EVP_CIPHER_CTX *ctx) |
83b06347 RL |
466 | { |
467 | return ctx->encrypt; | |
468 | } | |
469 | ||
ed576acd | 470 | unsigned long EVP_CIPHER_get_flags(const EVP_CIPHER *cipher) |
0f113f3e | 471 | { |
3c957bcd | 472 | return cipher->flags; |
0f113f3e | 473 | } |
7806f3dd | 474 | |
7806f3dd | 475 | void *EVP_CIPHER_CTX_get_app_data(const EVP_CIPHER_CTX *ctx) |
0f113f3e MC |
476 | { |
477 | return ctx->app_data; | |
478 | } | |
7806f3dd NL |
479 | |
480 | void EVP_CIPHER_CTX_set_app_data(EVP_CIPHER_CTX *ctx, void *data) | |
0f113f3e MC |
481 | { |
482 | ctx->app_data = data; | |
483 | } | |
7806f3dd | 484 | |
44ab2dfd | 485 | void *EVP_CIPHER_CTX_get_cipher_data(const EVP_CIPHER_CTX *ctx) |
83b06347 RL |
486 | { |
487 | return ctx->cipher_data; | |
488 | } | |
489 | ||
98ee7543 MC |
490 | void *EVP_CIPHER_CTX_set_cipher_data(EVP_CIPHER_CTX *ctx, void *cipher_data) |
491 | { | |
492 | void *old_cipher_data; | |
493 | ||
494 | old_cipher_data = ctx->cipher_data; | |
495 | ctx->cipher_data = cipher_data; | |
496 | ||
497 | return old_cipher_data; | |
498 | } | |
499 | ||
ed576acd | 500 | int EVP_CIPHER_get_iv_length(const EVP_CIPHER *cipher) |
0f113f3e | 501 | { |
3c957bcd | 502 | return cipher->iv_len; |
0f113f3e | 503 | } |
7806f3dd | 504 | |
ed576acd | 505 | int EVP_CIPHER_CTX_get_iv_length(const EVP_CIPHER_CTX *ctx) |
0f113f3e | 506 | { |
b30b45b7 P |
507 | if (ctx->iv_len < 0) { |
508 | int rv, len = EVP_CIPHER_get_iv_length(ctx->cipher); | |
509 | size_t v = len; | |
510 | OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; | |
511 | ||
512 | params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN, &v); | |
513 | rv = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params); | |
514 | if (rv != EVP_CTRL_RET_UNSUPPORTED) { | |
515 | if (rv <= 0) | |
516 | return -1; | |
517 | len = (int)v; | |
518 | } | |
519 | /* Code below to be removed when legacy support is dropped. */ | |
520 | else if ((EVP_CIPHER_get_flags(ctx->cipher) | |
521 | & EVP_CIPH_CUSTOM_IV_LENGTH) != 0) { | |
522 | rv = EVP_CIPHER_CTX_ctrl((EVP_CIPHER_CTX *)ctx, EVP_CTRL_GET_IVLEN, | |
523 | 0, &len); | |
524 | if (rv <= 0) | |
525 | return -1; | |
526 | } | |
527 | /*- | |
528 | * Casting away the const is annoying but required here. We need to | |
529 | * cache the result for performance reasons. | |
530 | */ | |
531 | ((EVP_CIPHER_CTX *)ctx)->iv_len = len; | |
7dddf2fc | 532 | } |
b30b45b7 | 533 | return ctx->iv_len; |
0f113f3e | 534 | } |
7806f3dd | 535 | |
ed576acd | 536 | int EVP_CIPHER_CTX_get_tag_length(const EVP_CIPHER_CTX *ctx) |
dc64dc2e SL |
537 | { |
538 | int ret; | |
539 | size_t v = 0; | |
540 | OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; | |
541 | ||
542 | params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_TAGLEN, &v); | |
7c14d0c1 | 543 | ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params); |
dc64dc2e SL |
544 | return ret == 1 ? (int)v : 0; |
545 | } | |
546 | ||
79f4417e | 547 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
83b06347 RL |
548 | const unsigned char *EVP_CIPHER_CTX_original_iv(const EVP_CIPHER_CTX *ctx) |
549 | { | |
089cb623 SL |
550 | int ok; |
551 | const unsigned char *v = ctx->oiv; | |
552 | OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; | |
553 | ||
554 | params[0] = | |
555 | OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_IV, | |
556 | (void **)&v, sizeof(ctx->oiv)); | |
7c14d0c1 | 557 | ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params); |
089cb623 SL |
558 | |
559 | return ok != 0 ? v : NULL; | |
83b06347 RL |
560 | } |
561 | ||
13273237 RL |
562 | /* |
563 | * OSSL_PARAM_OCTET_PTR gets us the pointer to the running IV in the provider | |
564 | */ | |
83b06347 RL |
565 | const unsigned char *EVP_CIPHER_CTX_iv(const EVP_CIPHER_CTX *ctx) |
566 | { | |
459b15d4 | 567 | int ok; |
13273237 | 568 | const unsigned char *v = ctx->iv; |
459b15d4 | 569 | OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; |
13273237 | 570 | |
459b15d4 | 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); |
459b15d4 SL |
575 | |
576 | return ok != 0 ? v : NULL; | |
83b06347 RL |
577 | } |
578 | ||
579 | unsigned char *EVP_CIPHER_CTX_iv_noconst(EVP_CIPHER_CTX *ctx) | |
580 | { | |
459b15d4 | 581 | int ok; |
13273237 | 582 | unsigned char *v = ctx->iv; |
459b15d4 SL |
583 | OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; |
584 | ||
585 | params[0] = | |
0d83b7b9 TM |
586 | OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_UPDATED_IV, |
587 | (void **)&v, sizeof(ctx->iv)); | |
7c14d0c1 | 588 | ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params); |
13273237 | 589 | |
459b15d4 | 590 | return ok != 0 ? v : NULL; |
83b06347 | 591 | } |
79f4417e BK |
592 | #endif /* OPENSSL_NO_DEPRECATED_3_0_0 */ |
593 | ||
0d83b7b9 | 594 | int EVP_CIPHER_CTX_get_updated_iv(EVP_CIPHER_CTX *ctx, void *buf, size_t len) |
79f4417e BK |
595 | { |
596 | OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; | |
597 | ||
598 | params[0] = | |
0d83b7b9 | 599 | OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_UPDATED_IV, buf, len); |
7c14d0c1 | 600 | return evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params); |
79f4417e BK |
601 | } |
602 | ||
0d83b7b9 | 603 | int EVP_CIPHER_CTX_get_original_iv(EVP_CIPHER_CTX *ctx, void *buf, size_t len) |
79f4417e BK |
604 | { |
605 | OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; | |
606 | ||
607 | params[0] = | |
608 | OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_IV, buf, len); | |
7c14d0c1 | 609 | return evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params); |
79f4417e | 610 | } |
83b06347 RL |
611 | |
612 | unsigned char *EVP_CIPHER_CTX_buf_noconst(EVP_CIPHER_CTX *ctx) | |
613 | { | |
614 | return ctx->buf; | |
615 | } | |
616 | ||
ed576acd | 617 | int EVP_CIPHER_CTX_get_num(const EVP_CIPHER_CTX *ctx) |
83b06347 | 618 | { |
1c3ace68 SL |
619 | int ok; |
620 | unsigned int v = (unsigned int)ctx->num; | |
459b15d4 | 621 | OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; |
13273237 | 622 | |
1c3ace68 | 623 | params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_NUM, &v); |
7c14d0c1 | 624 | ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params); |
459b15d4 | 625 | |
1c3ace68 | 626 | return ok != 0 ? (int)v : EVP_CTRL_RET_UNSUPPORTED; |
83b06347 RL |
627 | } |
628 | ||
13273237 | 629 | int EVP_CIPHER_CTX_set_num(EVP_CIPHER_CTX *ctx, int num) |
83b06347 | 630 | { |
459b15d4 | 631 | int ok; |
1c3ace68 | 632 | unsigned int n = (unsigned int)num; |
459b15d4 | 633 | OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; |
13273237 | 634 | |
1c3ace68 | 635 | params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_NUM, &n); |
7c14d0c1 | 636 | ok = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params); |
459b15d4 SL |
637 | |
638 | if (ok != 0) | |
1c3ace68 | 639 | ctx->num = (int)n; |
13273237 | 640 | return ok != 0; |
83b06347 RL |
641 | } |
642 | ||
ed576acd | 643 | int EVP_CIPHER_get_key_length(const EVP_CIPHER *cipher) |
0f113f3e | 644 | { |
3c957bcd | 645 | return cipher->key_len; |
0f113f3e | 646 | } |
7806f3dd | 647 | |
ed576acd | 648 | int EVP_CIPHER_CTX_get_key_length(const EVP_CIPHER_CTX *ctx) |
0f113f3e | 649 | { |
70f39a48 P |
650 | if (ctx->key_len <= 0 && ctx->cipher->prov != NULL) { |
651 | int ok; | |
652 | OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; | |
653 | size_t len; | |
459b15d4 | 654 | |
70f39a48 P |
655 | params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &len); |
656 | ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params); | |
657 | if (ok <= 0) | |
658 | return EVP_CTRL_RET_UNSUPPORTED; | |
13273237 | 659 | |
70f39a48 P |
660 | /*- |
661 | * The if branch should never be taken since EVP_MAX_KEY_LENGTH is | |
662 | * less than INT_MAX but best to be safe. | |
663 | * | |
664 | * Casting away the const is annoying but required here. We need to | |
665 | * cache the result for performance reasons. | |
666 | */ | |
667 | if (!OSSL_PARAM_get_int(params, &((EVP_CIPHER_CTX *)ctx)->key_len)) | |
668 | return -1; | |
669 | ((EVP_CIPHER_CTX *)ctx)->key_len = (int)len; | |
670 | } | |
671 | return ctx->key_len; | |
0f113f3e | 672 | } |
7806f3dd | 673 | |
ed576acd | 674 | int EVP_CIPHER_get_nid(const EVP_CIPHER *cipher) |
0f113f3e MC |
675 | { |
676 | return cipher->nid; | |
677 | } | |
7806f3dd | 678 | |
ed576acd | 679 | int EVP_CIPHER_CTX_get_nid(const EVP_CIPHER_CTX *ctx) |
0f113f3e MC |
680 | { |
681 | return ctx->cipher->nid; | |
682 | } | |
7806f3dd | 683 | |
7cfa1717 RL |
684 | int EVP_CIPHER_is_a(const EVP_CIPHER *cipher, const char *name) |
685 | { | |
ee8db8c5 P |
686 | if (cipher == NULL) |
687 | return 0; | |
e4a1d023 RL |
688 | if (cipher->prov != NULL) |
689 | return evp_is_a(cipher->prov, cipher->name_id, NULL, name); | |
ed576acd | 690 | return evp_is_a(NULL, 0, EVP_CIPHER_get0_name(cipher), name); |
7cfa1717 RL |
691 | } |
692 | ||
bcd5d3a2 | 693 | int evp_cipher_get_number(const EVP_CIPHER *cipher) |
506cb0f6 RL |
694 | { |
695 | return cipher->name_id; | |
696 | } | |
697 | ||
ed576acd | 698 | const char *EVP_CIPHER_get0_name(const EVP_CIPHER *cipher) |
c750bc08 | 699 | { |
6c9bc258 TM |
700 | if (cipher->type_name != NULL) |
701 | return cipher->type_name; | |
f844f9eb | 702 | #ifndef FIPS_MODULE |
ed576acd | 703 | return OBJ_nid2sn(EVP_CIPHER_get_nid(cipher)); |
c750bc08 RL |
704 | #else |
705 | return NULL; | |
706 | #endif | |
707 | } | |
708 | ||
ed576acd | 709 | const char *EVP_CIPHER_get0_description(const EVP_CIPHER *cipher) |
03888233 RL |
710 | { |
711 | if (cipher->description != NULL) | |
712 | return cipher->description; | |
713 | #ifndef FIPS_MODULE | |
ed576acd | 714 | return OBJ_nid2ln(EVP_CIPHER_get_nid(cipher)); |
03888233 RL |
715 | #else |
716 | return NULL; | |
717 | #endif | |
718 | } | |
719 | ||
d84f5515 MC |
720 | int EVP_CIPHER_names_do_all(const EVP_CIPHER *cipher, |
721 | void (*fn)(const char *name, void *data), | |
722 | void *data) | |
f651c727 RL |
723 | { |
724 | if (cipher->prov != NULL) | |
d84f5515 MC |
725 | return evp_names_do_all(cipher->prov, cipher->name_id, fn, data); |
726 | ||
727 | return 1; | |
f651c727 RL |
728 | } |
729 | ||
ed576acd | 730 | const OSSL_PROVIDER *EVP_CIPHER_get0_provider(const EVP_CIPHER *cipher) |
1d2622d4 RL |
731 | { |
732 | return cipher->prov; | |
733 | } | |
734 | ||
ed576acd | 735 | int EVP_CIPHER_get_mode(const EVP_CIPHER *cipher) |
718b133a | 736 | { |
ed576acd | 737 | return EVP_CIPHER_get_flags(cipher) & EVP_CIPH_MODE; |
459b15d4 | 738 | } |
718b133a | 739 | |
251e610c RL |
740 | int EVP_MD_is_a(const EVP_MD *md, const char *name) |
741 | { | |
ee8db8c5 P |
742 | if (md == NULL) |
743 | return 0; | |
e4a1d023 RL |
744 | if (md->prov != NULL) |
745 | return evp_is_a(md->prov, md->name_id, NULL, name); | |
ed576acd | 746 | return evp_is_a(NULL, 0, EVP_MD_get0_name(md), name); |
251e610c RL |
747 | } |
748 | ||
bcd5d3a2 | 749 | int evp_md_get_number(const EVP_MD *md) |
506cb0f6 RL |
750 | { |
751 | return md->name_id; | |
752 | } | |
753 | ||
ed576acd | 754 | const char *EVP_MD_get0_description(const EVP_MD *md) |
03888233 RL |
755 | { |
756 | if (md->description != NULL) | |
757 | return md->description; | |
758 | #ifndef FIPS_MODULE | |
759 | return OBJ_nid2ln(EVP_MD_nid(md)); | |
760 | #else | |
761 | return NULL; | |
762 | #endif | |
763 | } | |
764 | ||
ed576acd | 765 | const char *EVP_MD_get0_name(const EVP_MD *md) |
c750bc08 | 766 | { |
5c107243 SL |
767 | if (md == NULL) |
768 | return NULL; | |
6c9bc258 TM |
769 | if (md->type_name != NULL) |
770 | return md->type_name; | |
f844f9eb | 771 | #ifndef FIPS_MODULE |
c750bc08 RL |
772 | return OBJ_nid2sn(EVP_MD_nid(md)); |
773 | #else | |
774 | return NULL; | |
775 | #endif | |
776 | } | |
777 | ||
d84f5515 MC |
778 | int EVP_MD_names_do_all(const EVP_MD *md, |
779 | void (*fn)(const char *name, void *data), | |
780 | void *data) | |
f651c727 RL |
781 | { |
782 | if (md->prov != NULL) | |
d84f5515 MC |
783 | return evp_names_do_all(md->prov, md->name_id, fn, data); |
784 | ||
785 | return 1; | |
f651c727 RL |
786 | } |
787 | ||
ed576acd | 788 | const OSSL_PROVIDER *EVP_MD_get0_provider(const EVP_MD *md) |
1d2622d4 RL |
789 | { |
790 | return md->prov; | |
791 | } | |
792 | ||
ed576acd | 793 | int EVP_MD_get_type(const EVP_MD *md) |
0f113f3e MC |
794 | { |
795 | return md->type; | |
796 | } | |
7806f3dd | 797 | |
ed576acd | 798 | int EVP_MD_get_pkey_type(const EVP_MD *md) |
0f113f3e MC |
799 | { |
800 | return md->pkey_type; | |
801 | } | |
7806f3dd | 802 | |
ed576acd | 803 | int EVP_MD_get_block_size(const EVP_MD *md) |
a054d15c SL |
804 | { |
805 | if (md == NULL) { | |
806 | ERR_raise(ERR_LIB_EVP, EVP_R_MESSAGE_DIGEST_IS_NULL); | |
807 | return -1; | |
808 | } | |
809 | return md->block_size; | |
810 | } | |
811 | ||
ed576acd | 812 | int EVP_MD_get_size(const EVP_MD *md) |
0f113f3e | 813 | { |
6a3b7c68 | 814 | if (md == NULL) { |
9311d0c4 | 815 | ERR_raise(ERR_LIB_EVP, EVP_R_MESSAGE_DIGEST_IS_NULL); |
0f113f3e MC |
816 | return -1; |
817 | } | |
38f79314 | 818 | return md->md_size; |
0f113f3e | 819 | } |
7806f3dd | 820 | |
ed576acd | 821 | unsigned long EVP_MD_get_flags(const EVP_MD *md) |
0f113f3e | 822 | { |
38f79314 | 823 | return md->flags; |
0f113f3e | 824 | } |
e5fa864f | 825 | |
2db6bf6f RL |
826 | EVP_MD *EVP_MD_meth_new(int md_type, int pkey_type) |
827 | { | |
3fd70262 | 828 | EVP_MD *md = evp_md_new(); |
43ecb9c3 | 829 | |
2db6bf6f RL |
830 | if (md != NULL) { |
831 | md->type = md_type; | |
832 | md->pkey_type = pkey_type; | |
f6c95e46 | 833 | md->origin = EVP_ORIG_METH; |
2db6bf6f RL |
834 | } |
835 | return md; | |
836 | } | |
df05f2ce | 837 | |
2db6bf6f RL |
838 | EVP_MD *EVP_MD_meth_dup(const EVP_MD *md) |
839 | { | |
3fd70262 RL |
840 | EVP_MD *to = NULL; |
841 | ||
842 | /* | |
843 | * Non-legacy EVP_MDs can't be duplicated like this. | |
844 | * Use EVP_MD_up_ref() instead. | |
845 | */ | |
846 | if (md->prov != NULL) | |
847 | return NULL; | |
43ecb9c3 | 848 | |
3fd70262 | 849 | if ((to = EVP_MD_meth_new(md->type, md->pkey_type)) != NULL) { |
df05f2ce | 850 | CRYPTO_RWLOCK *lock = to->lock; |
3fd70262 | 851 | |
2db6bf6f | 852 | memcpy(to, md, sizeof(*to)); |
df05f2ce | 853 | to->lock = lock; |
bb98a112 | 854 | to->origin = EVP_ORIG_METH; |
df05f2ce | 855 | } |
2db6bf6f RL |
856 | return to; |
857 | } | |
3653d0c2 | 858 | |
f6c95e46 RS |
859 | void evp_md_free_int(EVP_MD *md) |
860 | { | |
6c9bc258 | 861 | OPENSSL_free(md->type_name); |
f6c95e46 RS |
862 | ossl_provider_free(md->prov); |
863 | CRYPTO_THREAD_lock_free(md->lock); | |
864 | OPENSSL_free(md); | |
865 | } | |
866 | ||
2db6bf6f RL |
867 | void EVP_MD_meth_free(EVP_MD *md) |
868 | { | |
f6c95e46 RS |
869 | if (md == NULL || md->origin != EVP_ORIG_METH) |
870 | return; | |
871 | ||
872 | evp_md_free_int(md); | |
2db6bf6f | 873 | } |
f6c95e46 | 874 | |
2db6bf6f RL |
875 | int EVP_MD_meth_set_input_blocksize(EVP_MD *md, int blocksize) |
876 | { | |
8bbc7f22 DB |
877 | if (md->block_size != 0) |
878 | return 0; | |
879 | ||
2db6bf6f RL |
880 | md->block_size = blocksize; |
881 | return 1; | |
882 | } | |
883 | int EVP_MD_meth_set_result_size(EVP_MD *md, int resultsize) | |
884 | { | |
8bbc7f22 DB |
885 | if (md->md_size != 0) |
886 | return 0; | |
887 | ||
2db6bf6f RL |
888 | md->md_size = resultsize; |
889 | return 1; | |
890 | } | |
891 | int EVP_MD_meth_set_app_datasize(EVP_MD *md, int datasize) | |
892 | { | |
8bbc7f22 DB |
893 | if (md->ctx_size != 0) |
894 | return 0; | |
895 | ||
2db6bf6f RL |
896 | md->ctx_size = datasize; |
897 | return 1; | |
898 | } | |
899 | int EVP_MD_meth_set_flags(EVP_MD *md, unsigned long flags) | |
900 | { | |
8bbc7f22 DB |
901 | if (md->flags != 0) |
902 | return 0; | |
903 | ||
2db6bf6f RL |
904 | md->flags = flags; |
905 | return 1; | |
906 | } | |
907 | int EVP_MD_meth_set_init(EVP_MD *md, int (*init)(EVP_MD_CTX *ctx)) | |
908 | { | |
8bbc7f22 DB |
909 | if (md->init != NULL) |
910 | return 0; | |
911 | ||
2db6bf6f RL |
912 | md->init = init; |
913 | return 1; | |
914 | } | |
915 | int EVP_MD_meth_set_update(EVP_MD *md, int (*update)(EVP_MD_CTX *ctx, | |
916 | const void *data, | |
917 | size_t count)) | |
918 | { | |
8bbc7f22 DB |
919 | if (md->update != NULL) |
920 | return 0; | |
921 | ||
2db6bf6f RL |
922 | md->update = update; |
923 | return 1; | |
924 | } | |
925 | int EVP_MD_meth_set_final(EVP_MD *md, int (*final)(EVP_MD_CTX *ctx, | |
926 | unsigned char *md)) | |
927 | { | |
8bbc7f22 DB |
928 | if (md->final != NULL) |
929 | return 0; | |
930 | ||
2db6bf6f RL |
931 | md->final = final; |
932 | return 1; | |
933 | } | |
934 | int EVP_MD_meth_set_copy(EVP_MD *md, int (*copy)(EVP_MD_CTX *to, | |
935 | const EVP_MD_CTX *from)) | |
936 | { | |
8bbc7f22 DB |
937 | if (md->copy != NULL) |
938 | return 0; | |
939 | ||
2db6bf6f RL |
940 | md->copy = copy; |
941 | return 1; | |
942 | } | |
943 | int EVP_MD_meth_set_cleanup(EVP_MD *md, int (*cleanup)(EVP_MD_CTX *ctx)) | |
944 | { | |
8bbc7f22 DB |
945 | if (md->cleanup != NULL) |
946 | return 0; | |
947 | ||
2db6bf6f RL |
948 | md->cleanup = cleanup; |
949 | return 1; | |
950 | } | |
951 | int EVP_MD_meth_set_ctrl(EVP_MD *md, int (*ctrl)(EVP_MD_CTX *ctx, int cmd, | |
952 | int p1, void *p2)) | |
953 | { | |
8bbc7f22 DB |
954 | if (md->md_ctrl != NULL) |
955 | return 0; | |
956 | ||
2db6bf6f RL |
957 | md->md_ctrl = ctrl; |
958 | return 1; | |
959 | } | |
960 | ||
961 | int EVP_MD_meth_get_input_blocksize(const EVP_MD *md) | |
962 | { | |
963 | return md->block_size; | |
964 | } | |
965 | int EVP_MD_meth_get_result_size(const EVP_MD *md) | |
966 | { | |
967 | return md->md_size; | |
968 | } | |
969 | int EVP_MD_meth_get_app_datasize(const EVP_MD *md) | |
970 | { | |
971 | return md->ctx_size; | |
972 | } | |
973 | unsigned long EVP_MD_meth_get_flags(const EVP_MD *md) | |
974 | { | |
8bfa99f0 | 975 | return md->flags; |
2db6bf6f RL |
976 | } |
977 | int (*EVP_MD_meth_get_init(const EVP_MD *md))(EVP_MD_CTX *ctx) | |
978 | { | |
979 | return md->init; | |
980 | } | |
981 | int (*EVP_MD_meth_get_update(const EVP_MD *md))(EVP_MD_CTX *ctx, | |
982 | const void *data, | |
983 | size_t count) | |
984 | { | |
985 | return md->update; | |
986 | } | |
987 | int (*EVP_MD_meth_get_final(const EVP_MD *md))(EVP_MD_CTX *ctx, | |
988 | unsigned char *md) | |
989 | { | |
990 | return md->final; | |
991 | } | |
992 | int (*EVP_MD_meth_get_copy(const EVP_MD *md))(EVP_MD_CTX *to, | |
993 | const EVP_MD_CTX *from) | |
994 | { | |
995 | return md->copy; | |
996 | } | |
997 | int (*EVP_MD_meth_get_cleanup(const EVP_MD *md))(EVP_MD_CTX *ctx) | |
998 | { | |
999 | return md->cleanup; | |
1000 | } | |
1001 | int (*EVP_MD_meth_get_ctrl(const EVP_MD *md))(EVP_MD_CTX *ctx, int cmd, | |
1002 | int p1, void *p2) | |
1003 | { | |
1004 | return md->md_ctrl; | |
1005 | } | |
1006 | ||
f6c95e46 | 1007 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
7806f3dd | 1008 | const EVP_MD *EVP_MD_CTX_md(const EVP_MD_CTX *ctx) |
0f113f3e | 1009 | { |
b7c913c8 | 1010 | if (ctx == NULL) |
0f113f3e | 1011 | return NULL; |
b7c913c8 | 1012 | return ctx->reqdigest; |
0f113f3e | 1013 | } |
f6c95e46 RS |
1014 | #endif |
1015 | ||
1016 | const EVP_MD *EVP_MD_CTX_get0_md(const EVP_MD_CTX *ctx) | |
1017 | { | |
1018 | if (ctx == NULL) | |
1019 | return NULL; | |
1020 | return ctx->reqdigest; | |
1021 | } | |
1022 | ||
1023 | EVP_MD *EVP_MD_CTX_get1_md(EVP_MD_CTX *ctx) | |
1024 | { | |
1025 | EVP_MD *md; | |
1026 | ||
1027 | if (ctx == NULL) | |
1028 | return NULL; | |
1029 | md = (EVP_MD *)ctx->reqdigest; | |
ab547fc0 | 1030 | if (md == NULL || !EVP_MD_up_ref(md)) |
f6c95e46 RS |
1031 | return NULL; |
1032 | return md; | |
1033 | } | |
7806f3dd | 1034 | |
ed576acd | 1035 | EVP_PKEY_CTX *EVP_MD_CTX_get_pkey_ctx(const EVP_MD_CTX *ctx) |
7638370c RL |
1036 | { |
1037 | return ctx->pctx; | |
1038 | } | |
1039 | ||
f844f9eb | 1040 | #if !defined(FIPS_MODULE) |
00902d94 PY |
1041 | void EVP_MD_CTX_set_pkey_ctx(EVP_MD_CTX *ctx, EVP_PKEY_CTX *pctx) |
1042 | { | |
81c79453 PY |
1043 | /* |
1044 | * it's reasonable to set NULL pctx (a.k.a clear the ctx->pctx), so | |
1045 | * we have to deal with the cleanup job here. | |
1046 | */ | |
1047 | if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX)) | |
1048 | EVP_PKEY_CTX_free(ctx->pctx); | |
1049 | ||
00902d94 | 1050 | ctx->pctx = pctx; |
81c79453 PY |
1051 | |
1052 | if (pctx != NULL) { | |
1053 | /* make sure pctx is not freed when destroying EVP_MD_CTX */ | |
1054 | EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX); | |
1055 | } else { | |
1056 | EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX); | |
1057 | } | |
00902d94 | 1058 | } |
f844f9eb | 1059 | #endif /* !defined(FIPS_MODULE) */ |
00902d94 | 1060 | |
ed576acd | 1061 | void *EVP_MD_CTX_get0_md_data(const EVP_MD_CTX *ctx) |
7638370c RL |
1062 | { |
1063 | return ctx->md_data; | |
1064 | } | |
1065 | ||
1066 | int (*EVP_MD_CTX_update_fn(EVP_MD_CTX *ctx))(EVP_MD_CTX *ctx, | |
1067 | const void *data, size_t count) | |
1068 | { | |
1069 | return ctx->update; | |
1070 | } | |
1071 | ||
1072 | void EVP_MD_CTX_set_update_fn(EVP_MD_CTX *ctx, | |
1073 | int (*update) (EVP_MD_CTX *ctx, | |
1074 | const void *data, size_t count)) | |
1075 | { | |
1076 | ctx->update = update; | |
1077 | } | |
1078 | ||
7806f3dd | 1079 | void EVP_MD_CTX_set_flags(EVP_MD_CTX *ctx, int flags) |
0f113f3e MC |
1080 | { |
1081 | ctx->flags |= flags; | |
1082 | } | |
7806f3dd NL |
1083 | |
1084 | void EVP_MD_CTX_clear_flags(EVP_MD_CTX *ctx, int flags) | |
0f113f3e MC |
1085 | { |
1086 | ctx->flags &= ~flags; | |
1087 | } | |
7806f3dd NL |
1088 | |
1089 | int EVP_MD_CTX_test_flags(const EVP_MD_CTX *ctx, int flags) | |
0f113f3e MC |
1090 | { |
1091 | return (ctx->flags & flags); | |
1092 | } | |
e92f9f45 | 1093 | |
e2311445 SL |
1094 | static int evp_cipher_ctx_enable_use_bits(EVP_CIPHER_CTX *ctx, |
1095 | unsigned int enable) | |
1096 | { | |
1097 | OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END }; | |
1098 | ||
1099 | params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_USE_BITS, &enable); | |
1100 | return EVP_CIPHER_CTX_set_params(ctx, params); | |
1101 | } | |
1102 | ||
e92f9f45 | 1103 | void EVP_CIPHER_CTX_set_flags(EVP_CIPHER_CTX *ctx, int flags) |
0f113f3e | 1104 | { |
e2311445 SL |
1105 | int oldflags = ctx->flags; |
1106 | ||
0f113f3e | 1107 | ctx->flags |= flags; |
e2311445 SL |
1108 | if (((oldflags ^ ctx->flags) & EVP_CIPH_FLAG_LENGTH_BITS) != 0) |
1109 | evp_cipher_ctx_enable_use_bits(ctx, 1); | |
0f113f3e | 1110 | } |
e92f9f45 DSH |
1111 | |
1112 | void EVP_CIPHER_CTX_clear_flags(EVP_CIPHER_CTX *ctx, int flags) | |
0f113f3e | 1113 | { |
e2311445 SL |
1114 | int oldflags = ctx->flags; |
1115 | ||
0f113f3e | 1116 | ctx->flags &= ~flags; |
e2311445 SL |
1117 | if (((oldflags ^ ctx->flags) & EVP_CIPH_FLAG_LENGTH_BITS) != 0) |
1118 | evp_cipher_ctx_enable_use_bits(ctx, 0); | |
0f113f3e | 1119 | } |
e92f9f45 DSH |
1120 | |
1121 | int EVP_CIPHER_CTX_test_flags(const EVP_CIPHER_CTX *ctx, int flags) | |
0f113f3e MC |
1122 | { |
1123 | return (ctx->flags & flags); | |
1124 | } | |
f842b6b2 | 1125 | |
b807c2fb TM |
1126 | #if !defined(FIPS_MODULE) |
1127 | ||
11a1b341 MC |
1128 | int EVP_PKEY_CTX_set_group_name(EVP_PKEY_CTX *ctx, const char *name) |
1129 | { | |
1130 | OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END }; | |
023b188c | 1131 | |
6fcd92d3 | 1132 | if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) { |
11a1b341 MC |
1133 | ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); |
1134 | /* Uses the same return values as EVP_PKEY_CTX_ctrl */ | |
1135 | return -2; | |
1136 | } | |
1137 | ||
1138 | if (name == NULL) | |
1139 | return -1; | |
1140 | ||
6fcd92d3 RL |
1141 | params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME, |
1142 | (char *)name, 0); | |
11a1b341 MC |
1143 | return EVP_PKEY_CTX_set_params(ctx, params); |
1144 | } | |
1145 | ||
1146 | int EVP_PKEY_CTX_get_group_name(EVP_PKEY_CTX *ctx, char *name, size_t namelen) | |
1147 | { | |
1148 | OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END }; | |
1149 | OSSL_PARAM *p = params; | |
1150 | ||
1151 | if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) { | |
023b188c | 1152 | /* There is no legacy support for this */ |
11a1b341 MC |
1153 | ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); |
1154 | /* Uses the same return values as EVP_PKEY_CTX_ctrl */ | |
1155 | return -2; | |
1156 | } | |
1157 | ||
1158 | if (name == NULL) | |
1159 | return -1; | |
1160 | ||
1161 | *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME, | |
1162 | name, namelen); | |
1163 | if (!EVP_PKEY_CTX_get_params(ctx, params)) | |
1164 | return -1; | |
1165 | return 1; | |
1166 | } | |
f9253152 DDO |
1167 | |
1168 | /* | |
1169 | * evp_pkey_keygen() abstracts from the explicit use of B<EVP_PKEY_CTX> | |
1170 | * while providing a generic way of generating a new asymmetric key pair | |
1171 | * of algorithm type I<name> (e.g., C<RSA> or C<EC>). | |
1172 | * The library context I<libctx> and property query I<propq> | |
1173 | * are used when fetching algorithms from providers. | |
1174 | * The I<params> specify algorithm-specific parameters | |
1175 | * such as the RSA modulus size or the name of an EC curve. | |
1176 | */ | |
1177 | static EVP_PKEY *evp_pkey_keygen(OSSL_LIB_CTX *libctx, const char *name, | |
56784203 | 1178 | const char *propq, const OSSL_PARAM *params) |
f9253152 DDO |
1179 | { |
1180 | EVP_PKEY *pkey = NULL; | |
1181 | EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_from_name(libctx, name, propq); | |
1182 | ||
1183 | if (ctx != NULL | |
1184 | && EVP_PKEY_keygen_init(ctx) > 0 | |
1185 | && EVP_PKEY_CTX_set_params(ctx, params)) | |
1186 | (void)EVP_PKEY_generate(ctx, &pkey); | |
1187 | ||
1188 | EVP_PKEY_CTX_free(ctx); | |
1189 | return pkey; | |
1190 | } | |
1191 | ||
1192 | EVP_PKEY *EVP_PKEY_Q_keygen(OSSL_LIB_CTX *libctx, const char *propq, | |
1193 | const char *type, ...) | |
1194 | { | |
1195 | va_list args; | |
1196 | size_t bits; | |
1197 | char *name; | |
1198 | OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END }; | |
1199 | EVP_PKEY *ret = NULL; | |
1200 | ||
1201 | va_start(args, type); | |
1202 | ||
fba140c7 | 1203 | if (OPENSSL_strcasecmp(type, "RSA") == 0) { |
f9253152 DDO |
1204 | bits = va_arg(args, size_t); |
1205 | params[0] = OSSL_PARAM_construct_size_t(OSSL_PKEY_PARAM_RSA_BITS, &bits); | |
fba140c7 | 1206 | } else if (OPENSSL_strcasecmp(type, "EC") == 0) { |
f9253152 DDO |
1207 | name = va_arg(args, char *); |
1208 | params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME, | |
1209 | name, 0); | |
fba140c7 DB |
1210 | } else if (OPENSSL_strcasecmp(type, "ED25519") != 0 |
1211 | && OPENSSL_strcasecmp(type, "X25519") != 0 | |
1212 | && OPENSSL_strcasecmp(type, "ED448") != 0 | |
1213 | && OPENSSL_strcasecmp(type, "X448") != 0) { | |
f9253152 DDO |
1214 | ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_INVALID_ARGUMENT); |
1215 | goto end; | |
1216 | } | |
1217 | ret = evp_pkey_keygen(libctx, type, propq, params); | |
1218 | ||
1219 | end: | |
1220 | va_end(args); | |
1221 | return ret; | |
1222 | } | |
b807c2fb TM |
1223 | |
1224 | #endif /* !defined(FIPS_MODULE) */ |