]>
Commit | Line | Data |
---|---|---|
62867571 | 1 | /* |
fd38836b | 2 | * Copyright 1995-2018 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 | ||
10 | #include <stdio.h> | |
b39fc560 | 11 | #include "internal/cryptlib.h" |
ec577822 BM |
12 | #include <openssl/evp.h> |
13 | #include <openssl/objects.h> | |
718b133a MC |
14 | #include <openssl/params.h> |
15 | #include <openssl/core_names.h> | |
ff64702b | 16 | #include <openssl/dh.h> |
2db6bf6f | 17 | #include "internal/evp_int.h" |
3653d0c2 | 18 | #include "internal/provider.h" |
7638370c | 19 | #include "evp_locl.h" |
58964a49 | 20 | |
319e518a | 21 | #if !defined(FIPS_MODE) |
6b691a5c | 22 | int EVP_CIPHER_param_to_asn1(EVP_CIPHER_CTX *c, ASN1_TYPE *type) |
0f113f3e MC |
23 | { |
24 | int ret; | |
718b133a | 25 | const EVP_CIPHER *cipher = c->cipher; |
0f113f3e | 26 | |
718b133a MC |
27 | if (cipher->prov != NULL) { |
28 | /* | |
29 | * The cipher has come from a provider and won't have the default flags. | |
30 | * Find the implicit form so we can check the flags. | |
31 | * TODO(3.0): This won't work for 3rd party ciphers we know nothing about | |
32 | * We'll need to think of something else for those. | |
33 | */ | |
34 | cipher = EVP_get_cipherbynid(cipher->nid); | |
35 | if (cipher == NULL) { | |
36 | EVPerr(EVP_F_EVP_CIPHER_PARAM_TO_ASN1, ASN1_R_UNSUPPORTED_CIPHER); | |
37 | return -1; | |
38 | } | |
39 | } | |
40 | ||
41 | if (cipher->set_asn1_parameters != NULL) | |
42 | ret = cipher->set_asn1_parameters(c, type); | |
43 | else if (cipher->flags & EVP_CIPH_FLAG_DEFAULT_ASN1) { | |
44 | switch (EVP_CIPHER_mode(cipher)) { | |
2acdef5e | 45 | case EVP_CIPH_WRAP_MODE: |
718b133a | 46 | if (EVP_CIPHER_nid(cipher) == NID_id_smime_alg_CMS3DESwrap) |
4ec36aff | 47 | ASN1_TYPE_set(type, V_ASN1_NULL, NULL); |
0f113f3e | 48 | ret = 1; |
2acdef5e DSH |
49 | break; |
50 | ||
51 | case EVP_CIPH_GCM_MODE: | |
52 | case EVP_CIPH_CCM_MODE: | |
53 | case EVP_CIPH_XTS_MODE: | |
54 | case EVP_CIPH_OCB_MODE: | |
49c9c1b3 | 55 | ret = -2; |
2acdef5e DSH |
56 | break; |
57 | ||
58 | default: | |
0f113f3e | 59 | ret = EVP_CIPHER_set_asn1_iv(c, type); |
2acdef5e | 60 | } |
0f113f3e MC |
61 | } else |
62 | ret = -1; | |
49c9c1b3 DO |
63 | if (ret <= 0) |
64 | EVPerr(EVP_F_EVP_CIPHER_PARAM_TO_ASN1, ret == -2 ? | |
65 | ASN1_R_UNSUPPORTED_CIPHER : | |
66 | EVP_R_CIPHER_PARAMETER_ERROR); | |
67 | if (ret < -1) | |
68 | ret = -1; | |
26a7d938 | 69 | return ret; |
0f113f3e | 70 | } |
58964a49 | 71 | |
6b691a5c | 72 | int EVP_CIPHER_asn1_to_param(EVP_CIPHER_CTX *c, ASN1_TYPE *type) |
0f113f3e MC |
73 | { |
74 | int ret; | |
718b133a MC |
75 | const EVP_CIPHER *cipher = c->cipher; |
76 | ||
77 | if (cipher->prov != NULL) { | |
78 | /* | |
79 | * The cipher has come from a provider and won't have the default flags. | |
80 | * Find the implicit form so we can check the flags. | |
81 | */ | |
82 | cipher = EVP_get_cipherbynid(cipher->nid); | |
83 | if (cipher == NULL) | |
84 | return -1; | |
85 | } | |
0f113f3e | 86 | |
718b133a MC |
87 | if (cipher->get_asn1_parameters != NULL) |
88 | ret = cipher->get_asn1_parameters(c, type); | |
89 | else if (cipher->flags & EVP_CIPH_FLAG_DEFAULT_ASN1) { | |
90 | switch (EVP_CIPHER_mode(cipher)) { | |
2acdef5e DSH |
91 | |
92 | case EVP_CIPH_WRAP_MODE: | |
93 | ret = 1; | |
94 | break; | |
95 | ||
96 | case EVP_CIPH_GCM_MODE: | |
97 | case EVP_CIPH_CCM_MODE: | |
98 | case EVP_CIPH_XTS_MODE: | |
99 | case EVP_CIPH_OCB_MODE: | |
49c9c1b3 | 100 | ret = -2; |
2acdef5e DSH |
101 | break; |
102 | ||
103 | default: | |
104 | ret = EVP_CIPHER_get_asn1_iv(c, type); | |
105 | break; | |
106 | } | |
0f113f3e MC |
107 | } else |
108 | ret = -1; | |
49c9c1b3 DO |
109 | if (ret <= 0) |
110 | EVPerr(EVP_F_EVP_CIPHER_ASN1_TO_PARAM, ret == -2 ? | |
111 | EVP_R_UNSUPPORTED_CIPHER : | |
112 | EVP_R_CIPHER_PARAMETER_ERROR); | |
113 | if (ret < -1) | |
114 | ret = -1; | |
26a7d938 | 115 | return ret; |
0f113f3e | 116 | } |
58964a49 | 117 | |
718b133a | 118 | int EVP_CIPHER_get_asn1_iv(EVP_CIPHER_CTX *ctx, ASN1_TYPE *type) |
0f113f3e MC |
119 | { |
120 | int i = 0; | |
121 | unsigned int l; | |
122 | ||
123 | if (type != NULL) { | |
718b133a MC |
124 | unsigned char iv[EVP_MAX_IV_LENGTH]; |
125 | ||
126 | l = EVP_CIPHER_CTX_iv_length(ctx); | |
127 | if (!ossl_assert(l <= sizeof(iv))) | |
128 | return -1; | |
129 | i = ASN1_TYPE_get_octetstring(type, iv, l); | |
0f113f3e | 130 | if (i != (int)l) |
26a7d938 | 131 | return -1; |
718b133a MC |
132 | |
133 | if (!EVP_CipherInit_ex(ctx, NULL, NULL, NULL, iv, -1)) | |
134 | return -1; | |
0f113f3e | 135 | } |
26a7d938 | 136 | return i; |
0f113f3e | 137 | } |
58964a49 | 138 | |
6b691a5c | 139 | int EVP_CIPHER_set_asn1_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type) |
0f113f3e MC |
140 | { |
141 | int i = 0; | |
142 | unsigned int j; | |
143 | ||
144 | if (type != NULL) { | |
145 | j = EVP_CIPHER_CTX_iv_length(c); | |
146 | OPENSSL_assert(j <= sizeof(c->iv)); | |
147 | i = ASN1_TYPE_set_octetstring(type, c->oiv, j); | |
148 | } | |
26a7d938 | 149 | return i; |
0f113f3e | 150 | } |
319e518a | 151 | #endif /* !defined(FIPS_MODE) */ |
884e8ec6 DSH |
152 | |
153 | /* Convert the various cipher NIDs and dummies to a proper OID NID */ | |
84fa704c | 154 | int EVP_CIPHER_type(const EVP_CIPHER *ctx) |
884e8ec6 | 155 | { |
0f113f3e | 156 | int nid; |
0f113f3e | 157 | nid = EVP_CIPHER_nid(ctx); |
884e8ec6 | 158 | |
0f113f3e | 159 | switch (nid) { |
884e8ec6 | 160 | |
0f113f3e MC |
161 | case NID_rc2_cbc: |
162 | case NID_rc2_64_cbc: | |
163 | case NID_rc2_40_cbc: | |
884e8ec6 | 164 | |
0f113f3e | 165 | return NID_rc2_cbc; |
884e8ec6 | 166 | |
0f113f3e MC |
167 | case NID_rc4: |
168 | case NID_rc4_40: | |
884e8ec6 | 169 | |
0f113f3e | 170 | return NID_rc4; |
884e8ec6 | 171 | |
0f113f3e MC |
172 | case NID_aes_128_cfb128: |
173 | case NID_aes_128_cfb8: | |
174 | case NID_aes_128_cfb1: | |
8d1ebe0b | 175 | |
0f113f3e | 176 | return NID_aes_128_cfb128; |
8d1ebe0b | 177 | |
0f113f3e MC |
178 | case NID_aes_192_cfb128: |
179 | case NID_aes_192_cfb8: | |
180 | case NID_aes_192_cfb1: | |
8d1ebe0b | 181 | |
0f113f3e | 182 | return NID_aes_192_cfb128; |
8d1ebe0b | 183 | |
0f113f3e MC |
184 | case NID_aes_256_cfb128: |
185 | case NID_aes_256_cfb8: | |
186 | case NID_aes_256_cfb1: | |
8d1ebe0b | 187 | |
0f113f3e | 188 | return NID_aes_256_cfb128; |
8d1ebe0b | 189 | |
0f113f3e MC |
190 | case NID_des_cfb64: |
191 | case NID_des_cfb8: | |
192 | case NID_des_cfb1: | |
8d1ebe0b | 193 | |
0f113f3e | 194 | return NID_des_cfb64; |
8d1ebe0b | 195 | |
0f113f3e MC |
196 | case NID_des_ede3_cfb64: |
197 | case NID_des_ede3_cfb8: | |
198 | case NID_des_ede3_cfb1: | |
7e765bf2 | 199 | |
0f113f3e | 200 | return NID_des_cfb64; |
7e765bf2 | 201 | |
0f113f3e | 202 | default: |
319e518a MC |
203 | #ifdef FIPS_MODE |
204 | return NID_undef; | |
205 | #else | |
206 | { | |
207 | /* Check it has an OID and it is valid */ | |
208 | ASN1_OBJECT *otmp = OBJ_nid2obj(nid); | |
209 | ||
210 | if (OBJ_get0_data(otmp) == NULL) | |
211 | nid = NID_undef; | |
212 | ASN1_OBJECT_free(otmp); | |
213 | return nid; | |
214 | } | |
215 | #endif | |
0f113f3e | 216 | } |
884e8ec6 DSH |
217 | } |
218 | ||
718b133a | 219 | int EVP_CIPHER_block_size(const EVP_CIPHER *cipher) |
0f113f3e | 220 | { |
459b15d4 SL |
221 | int ok, v = cipher->block_size; |
222 | OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; | |
223 | ||
224 | params[0] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_BLOCK_SIZE, &v); | |
225 | ok = evp_do_ciph_getparams(cipher, params); | |
13273237 | 226 | |
e870791a | 227 | return ok != 0 ? v : EVP_CTRL_RET_UNSUPPORTED; |
0f113f3e | 228 | } |
7806f3dd | 229 | |
6343829a | 230 | int EVP_CIPHER_CTX_block_size(const EVP_CIPHER_CTX *ctx) |
0f113f3e | 231 | { |
718b133a | 232 | return EVP_CIPHER_block_size(ctx->cipher); |
0f113f3e | 233 | } |
7806f3dd | 234 | |
e79f8773 RL |
235 | int EVP_CIPHER_impl_ctx_size(const EVP_CIPHER *e) |
236 | { | |
237 | return e->ctx_size; | |
238 | } | |
239 | ||
0f113f3e MC |
240 | int EVP_Cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
241 | const unsigned char *in, unsigned int inl) | |
242 | { | |
718b133a | 243 | if (ctx->cipher->prov != NULL) { |
f79858ac RL |
244 | size_t outl = 0; /* ignored */ |
245 | int blocksize = EVP_CIPHER_CTX_block_size(ctx); | |
246 | ||
718b133a | 247 | if (ctx->cipher->ccipher != NULL) |
f79858ac RL |
248 | return |
249 | ctx->cipher->ccipher(ctx->provctx, out, &outl, | |
250 | inl + (blocksize == 1 ? 0 : blocksize), | |
251 | in, (size_t)inl); | |
718b133a MC |
252 | return 0; |
253 | } | |
254 | ||
0f113f3e MC |
255 | return ctx->cipher->do_cipher(ctx, out, in, inl); |
256 | } | |
7806f3dd NL |
257 | |
258 | const EVP_CIPHER *EVP_CIPHER_CTX_cipher(const EVP_CIPHER_CTX *ctx) | |
0f113f3e MC |
259 | { |
260 | return ctx->cipher; | |
261 | } | |
7806f3dd | 262 | |
83b06347 RL |
263 | int EVP_CIPHER_CTX_encrypting(const EVP_CIPHER_CTX *ctx) |
264 | { | |
265 | return ctx->encrypt; | |
266 | } | |
267 | ||
7806f3dd | 268 | unsigned long EVP_CIPHER_flags(const EVP_CIPHER *cipher) |
0f113f3e | 269 | { |
459b15d4 | 270 | int ok; |
13273237 | 271 | unsigned long v = cipher->flags; |
459b15d4 SL |
272 | OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; |
273 | ||
274 | params[0] = OSSL_PARAM_construct_ulong(OSSL_CIPHER_PARAM_FLAGS, &v); | |
275 | ok = evp_do_ciph_getparams(cipher, params); | |
13273237 RL |
276 | |
277 | return ok != 0 ? v : 0; | |
0f113f3e | 278 | } |
7806f3dd | 279 | |
7806f3dd | 280 | void *EVP_CIPHER_CTX_get_app_data(const EVP_CIPHER_CTX *ctx) |
0f113f3e MC |
281 | { |
282 | return ctx->app_data; | |
283 | } | |
7806f3dd NL |
284 | |
285 | void EVP_CIPHER_CTX_set_app_data(EVP_CIPHER_CTX *ctx, void *data) | |
0f113f3e MC |
286 | { |
287 | ctx->app_data = data; | |
288 | } | |
7806f3dd | 289 | |
44ab2dfd | 290 | void *EVP_CIPHER_CTX_get_cipher_data(const EVP_CIPHER_CTX *ctx) |
83b06347 RL |
291 | { |
292 | return ctx->cipher_data; | |
293 | } | |
294 | ||
98ee7543 MC |
295 | void *EVP_CIPHER_CTX_set_cipher_data(EVP_CIPHER_CTX *ctx, void *cipher_data) |
296 | { | |
297 | void *old_cipher_data; | |
298 | ||
299 | old_cipher_data = ctx->cipher_data; | |
300 | ctx->cipher_data = cipher_data; | |
301 | ||
302 | return old_cipher_data; | |
303 | } | |
304 | ||
6343829a | 305 | int EVP_CIPHER_iv_length(const EVP_CIPHER *cipher) |
0f113f3e | 306 | { |
459b15d4 SL |
307 | int ok, v = cipher->iv_len; |
308 | OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; | |
718b133a | 309 | |
459b15d4 SL |
310 | params[0] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_IVLEN, &v); |
311 | ok = evp_do_ciph_getparams(cipher, params); | |
312 | ||
e870791a | 313 | return ok != 0 ? v : EVP_CTRL_RET_UNSUPPORTED; |
0f113f3e | 314 | } |
7806f3dd | 315 | |
6343829a | 316 | int EVP_CIPHER_CTX_iv_length(const EVP_CIPHER_CTX *ctx) |
0f113f3e | 317 | { |
7dddf2fc | 318 | int len, rv, v = EVP_CIPHER_iv_length(ctx->cipher); |
a672a02a SL |
319 | OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; |
320 | ||
321 | params[0] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_IVLEN, &v); | |
7dddf2fc SL |
322 | rv = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params); |
323 | if (rv == EVP_CTRL_RET_UNSUPPORTED) | |
324 | goto legacy; | |
325 | return rv != 0 ? v : -1; | |
326 | /* TODO (3.0) Remove legacy support */ | |
327 | legacy: | |
328 | if ((EVP_CIPHER_flags(ctx->cipher) & EVP_CIPH_CUSTOM_IV_LENGTH) != 0) { | |
329 | rv = EVP_CIPHER_CTX_ctrl((EVP_CIPHER_CTX *)ctx, EVP_CTRL_GET_IVLEN, | |
330 | 0, &len); | |
331 | return (rv == 1) ? len : -1; | |
332 | } | |
333 | return v; | |
0f113f3e | 334 | } |
7806f3dd | 335 | |
83b06347 RL |
336 | const unsigned char *EVP_CIPHER_CTX_original_iv(const EVP_CIPHER_CTX *ctx) |
337 | { | |
338 | return ctx->oiv; | |
339 | } | |
340 | ||
13273237 RL |
341 | /* |
342 | * OSSL_PARAM_OCTET_PTR gets us the pointer to the running IV in the provider | |
343 | */ | |
83b06347 RL |
344 | const unsigned char *EVP_CIPHER_CTX_iv(const EVP_CIPHER_CTX *ctx) |
345 | { | |
459b15d4 | 346 | int ok; |
13273237 | 347 | const unsigned char *v = ctx->iv; |
459b15d4 | 348 | OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; |
13273237 | 349 | |
459b15d4 SL |
350 | params[0] = |
351 | OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_IV, (void **)&v, | |
352 | sizeof(ctx->iv)); | |
353 | ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params); | |
354 | ||
355 | return ok != 0 ? v : NULL; | |
83b06347 RL |
356 | } |
357 | ||
358 | unsigned char *EVP_CIPHER_CTX_iv_noconst(EVP_CIPHER_CTX *ctx) | |
359 | { | |
459b15d4 | 360 | int ok; |
13273237 | 361 | unsigned char *v = ctx->iv; |
459b15d4 SL |
362 | OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; |
363 | ||
364 | params[0] = | |
365 | OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_IV, (void **)&v, | |
366 | sizeof(ctx->iv)); | |
367 | ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params); | |
13273237 | 368 | |
459b15d4 | 369 | return ok != 0 ? v : NULL; |
83b06347 RL |
370 | } |
371 | ||
372 | unsigned char *EVP_CIPHER_CTX_buf_noconst(EVP_CIPHER_CTX *ctx) | |
373 | { | |
374 | return ctx->buf; | |
375 | } | |
376 | ||
377 | int EVP_CIPHER_CTX_num(const EVP_CIPHER_CTX *ctx) | |
378 | { | |
459b15d4 SL |
379 | int ok, v = ctx->num; |
380 | OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; | |
13273237 | 381 | |
459b15d4 SL |
382 | params[0] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_NUM, &v); |
383 | ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params); | |
384 | ||
e870791a | 385 | return ok != 0 ? v : EVP_CTRL_RET_UNSUPPORTED; |
83b06347 RL |
386 | } |
387 | ||
13273237 | 388 | int EVP_CIPHER_CTX_set_num(EVP_CIPHER_CTX *ctx, int num) |
83b06347 | 389 | { |
459b15d4 SL |
390 | int ok; |
391 | OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; | |
13273237 | 392 | |
459b15d4 SL |
393 | params[0] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_NUM, &num); |
394 | ok = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->provctx, params); | |
395 | ||
396 | if (ok != 0) | |
397 | ctx->num = num; | |
13273237 | 398 | return ok != 0; |
83b06347 RL |
399 | } |
400 | ||
6343829a | 401 | int EVP_CIPHER_key_length(const EVP_CIPHER *cipher) |
0f113f3e | 402 | { |
459b15d4 SL |
403 | int ok, v = cipher->key_len; |
404 | OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; | |
405 | ||
406 | params[0] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_KEYLEN, &v); | |
407 | ok = evp_do_ciph_getparams(cipher, params); | |
df05f2ce | 408 | |
e870791a | 409 | return ok != 0 ? v : EVP_CTRL_RET_UNSUPPORTED; |
0f113f3e | 410 | } |
7806f3dd | 411 | |
6343829a | 412 | int EVP_CIPHER_CTX_key_length(const EVP_CIPHER_CTX *ctx) |
0f113f3e | 413 | { |
459b15d4 SL |
414 | int ok, v = ctx->key_len; |
415 | OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; | |
416 | ||
417 | params[0] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_KEYLEN, &v); | |
418 | ok = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params); | |
13273237 | 419 | |
e870791a | 420 | return ok != 0 ? v : EVP_CTRL_RET_UNSUPPORTED; |
0f113f3e | 421 | } |
7806f3dd NL |
422 | |
423 | int EVP_CIPHER_nid(const EVP_CIPHER *cipher) | |
0f113f3e MC |
424 | { |
425 | return cipher->nid; | |
426 | } | |
7806f3dd NL |
427 | |
428 | int EVP_CIPHER_CTX_nid(const EVP_CIPHER_CTX *ctx) | |
0f113f3e MC |
429 | { |
430 | return ctx->cipher->nid; | |
431 | } | |
7806f3dd | 432 | |
c750bc08 RL |
433 | const char *EVP_CIPHER_name(const EVP_CIPHER *cipher) |
434 | { | |
435 | if (cipher->prov != NULL) | |
436 | return cipher->name; | |
437 | #ifndef FIPS_MODE | |
438 | return OBJ_nid2sn(EVP_CIPHER_nid(cipher)); | |
439 | #else | |
440 | return NULL; | |
441 | #endif | |
442 | } | |
443 | ||
1d2622d4 RL |
444 | const OSSL_PROVIDER *EVP_CIPHER_provider(const EVP_CIPHER *cipher) |
445 | { | |
446 | return cipher->prov; | |
447 | } | |
448 | ||
718b133a MC |
449 | int EVP_CIPHER_mode(const EVP_CIPHER *cipher) |
450 | { | |
459b15d4 SL |
451 | int ok, v = EVP_CIPHER_flags(cipher) & EVP_CIPH_MODE; |
452 | OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; | |
718b133a | 453 | |
459b15d4 SL |
454 | params[0] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_MODE, &v); |
455 | ok = evp_do_ciph_getparams(cipher, params); | |
718b133a | 456 | |
459b15d4 SL |
457 | return ok != 0 ? v : 0; |
458 | } | |
718b133a | 459 | |
c750bc08 RL |
460 | const char *EVP_MD_name(const EVP_MD *md) |
461 | { | |
462 | if (md->prov != NULL) | |
463 | return md->name; | |
464 | #ifndef FIPS_MODE | |
465 | return OBJ_nid2sn(EVP_MD_nid(md)); | |
466 | #else | |
467 | return NULL; | |
468 | #endif | |
469 | } | |
470 | ||
1d2622d4 RL |
471 | const OSSL_PROVIDER *EVP_MD_provider(const EVP_MD *md) |
472 | { | |
473 | return md->prov; | |
474 | } | |
475 | ||
0f113f3e MC |
476 | int EVP_MD_block_size(const EVP_MD *md) |
477 | { | |
7556b9df MC |
478 | if (md == NULL) { |
479 | EVPerr(EVP_F_EVP_MD_BLOCK_SIZE, EVP_R_MESSAGE_DIGEST_IS_NULL); | |
480 | return -1; | |
481 | } | |
482 | ||
483 | if (md->prov != NULL && md->dblock_size != NULL) | |
484 | return (int)md->dblock_size(); | |
485 | ||
0f113f3e MC |
486 | return md->block_size; |
487 | } | |
7806f3dd NL |
488 | |
489 | int EVP_MD_type(const EVP_MD *md) | |
0f113f3e MC |
490 | { |
491 | return md->type; | |
492 | } | |
7806f3dd NL |
493 | |
494 | int EVP_MD_pkey_type(const EVP_MD *md) | |
0f113f3e MC |
495 | { |
496 | return md->pkey_type; | |
497 | } | |
7806f3dd | 498 | |
6343829a | 499 | int EVP_MD_size(const EVP_MD *md) |
0f113f3e MC |
500 | { |
501 | if (!md) { | |
502 | EVPerr(EVP_F_EVP_MD_SIZE, EVP_R_MESSAGE_DIGEST_IS_NULL); | |
503 | return -1; | |
504 | } | |
8c8cf0d9 MC |
505 | |
506 | if (md->prov != NULL && md->size != NULL) | |
507 | return (int)md->size(); | |
508 | ||
0f113f3e MC |
509 | return md->md_size; |
510 | } | |
7806f3dd | 511 | |
e5fa864f | 512 | unsigned long EVP_MD_flags(const EVP_MD *md) |
0f113f3e MC |
513 | { |
514 | return md->flags; | |
515 | } | |
e5fa864f | 516 | |
2db6bf6f RL |
517 | EVP_MD *EVP_MD_meth_new(int md_type, int pkey_type) |
518 | { | |
43ecb9c3 RS |
519 | EVP_MD *md = OPENSSL_zalloc(sizeof(*md)); |
520 | ||
2db6bf6f RL |
521 | if (md != NULL) { |
522 | md->type = md_type; | |
523 | md->pkey_type = pkey_type; | |
3653d0c2 MC |
524 | md->lock = CRYPTO_THREAD_lock_new(); |
525 | if (md->lock == NULL) { | |
526 | OPENSSL_free(md); | |
527 | return NULL; | |
528 | } | |
529 | md->refcnt = 1; | |
2db6bf6f RL |
530 | } |
531 | return md; | |
532 | } | |
df05f2ce | 533 | |
2db6bf6f RL |
534 | EVP_MD *EVP_MD_meth_dup(const EVP_MD *md) |
535 | { | |
536 | EVP_MD *to = EVP_MD_meth_new(md->type, md->pkey_type); | |
43ecb9c3 | 537 | |
df05f2ce MC |
538 | if (to != NULL) { |
539 | CRYPTO_RWLOCK *lock = to->lock; | |
2db6bf6f | 540 | memcpy(to, md, sizeof(*to)); |
df05f2ce MC |
541 | to->lock = lock; |
542 | } | |
2db6bf6f RL |
543 | return to; |
544 | } | |
3653d0c2 | 545 | |
70c35fd1 | 546 | int EVP_MD_up_ref(EVP_MD *md) |
3653d0c2 MC |
547 | { |
548 | int ref = 0; | |
549 | ||
550 | CRYPTO_UP_REF(&md->refcnt, &ref, md->lock); | |
551 | return 1; | |
552 | } | |
553 | ||
2db6bf6f RL |
554 | void EVP_MD_meth_free(EVP_MD *md) |
555 | { | |
3653d0c2 MC |
556 | if (md != NULL) { |
557 | int i; | |
558 | ||
559 | CRYPTO_DOWN_REF(&md->refcnt, &i, md->lock); | |
560 | if (i > 0) | |
561 | return; | |
562 | ossl_provider_free(md->prov); | |
6b9e3724 | 563 | OPENSSL_free(md->name); |
3653d0c2 MC |
564 | CRYPTO_THREAD_lock_free(md->lock); |
565 | OPENSSL_free(md); | |
566 | } | |
2db6bf6f RL |
567 | } |
568 | int EVP_MD_meth_set_input_blocksize(EVP_MD *md, int blocksize) | |
569 | { | |
570 | md->block_size = blocksize; | |
571 | return 1; | |
572 | } | |
573 | int EVP_MD_meth_set_result_size(EVP_MD *md, int resultsize) | |
574 | { | |
575 | md->md_size = resultsize; | |
576 | return 1; | |
577 | } | |
578 | int EVP_MD_meth_set_app_datasize(EVP_MD *md, int datasize) | |
579 | { | |
580 | md->ctx_size = datasize; | |
581 | return 1; | |
582 | } | |
583 | int EVP_MD_meth_set_flags(EVP_MD *md, unsigned long flags) | |
584 | { | |
585 | md->flags = flags; | |
586 | return 1; | |
587 | } | |
588 | int EVP_MD_meth_set_init(EVP_MD *md, int (*init)(EVP_MD_CTX *ctx)) | |
589 | { | |
590 | md->init = init; | |
591 | return 1; | |
592 | } | |
593 | int EVP_MD_meth_set_update(EVP_MD *md, int (*update)(EVP_MD_CTX *ctx, | |
594 | const void *data, | |
595 | size_t count)) | |
596 | { | |
597 | md->update = update; | |
598 | return 1; | |
599 | } | |
600 | int EVP_MD_meth_set_final(EVP_MD *md, int (*final)(EVP_MD_CTX *ctx, | |
601 | unsigned char *md)) | |
602 | { | |
603 | md->final = final; | |
604 | return 1; | |
605 | } | |
606 | int EVP_MD_meth_set_copy(EVP_MD *md, int (*copy)(EVP_MD_CTX *to, | |
607 | const EVP_MD_CTX *from)) | |
608 | { | |
609 | md->copy = copy; | |
610 | return 1; | |
611 | } | |
612 | int EVP_MD_meth_set_cleanup(EVP_MD *md, int (*cleanup)(EVP_MD_CTX *ctx)) | |
613 | { | |
614 | md->cleanup = cleanup; | |
615 | return 1; | |
616 | } | |
617 | int EVP_MD_meth_set_ctrl(EVP_MD *md, int (*ctrl)(EVP_MD_CTX *ctx, int cmd, | |
618 | int p1, void *p2)) | |
619 | { | |
620 | md->md_ctrl = ctrl; | |
621 | return 1; | |
622 | } | |
623 | ||
624 | int EVP_MD_meth_get_input_blocksize(const EVP_MD *md) | |
625 | { | |
626 | return md->block_size; | |
627 | } | |
628 | int EVP_MD_meth_get_result_size(const EVP_MD *md) | |
629 | { | |
630 | return md->md_size; | |
631 | } | |
632 | int EVP_MD_meth_get_app_datasize(const EVP_MD *md) | |
633 | { | |
634 | return md->ctx_size; | |
635 | } | |
636 | unsigned long EVP_MD_meth_get_flags(const EVP_MD *md) | |
637 | { | |
8bfa99f0 | 638 | return md->flags; |
2db6bf6f RL |
639 | } |
640 | int (*EVP_MD_meth_get_init(const EVP_MD *md))(EVP_MD_CTX *ctx) | |
641 | { | |
642 | return md->init; | |
643 | } | |
644 | int (*EVP_MD_meth_get_update(const EVP_MD *md))(EVP_MD_CTX *ctx, | |
645 | const void *data, | |
646 | size_t count) | |
647 | { | |
648 | return md->update; | |
649 | } | |
650 | int (*EVP_MD_meth_get_final(const EVP_MD *md))(EVP_MD_CTX *ctx, | |
651 | unsigned char *md) | |
652 | { | |
653 | return md->final; | |
654 | } | |
655 | int (*EVP_MD_meth_get_copy(const EVP_MD *md))(EVP_MD_CTX *to, | |
656 | const EVP_MD_CTX *from) | |
657 | { | |
658 | return md->copy; | |
659 | } | |
660 | int (*EVP_MD_meth_get_cleanup(const EVP_MD *md))(EVP_MD_CTX *ctx) | |
661 | { | |
662 | return md->cleanup; | |
663 | } | |
664 | int (*EVP_MD_meth_get_ctrl(const EVP_MD *md))(EVP_MD_CTX *ctx, int cmd, | |
665 | int p1, void *p2) | |
666 | { | |
667 | return md->md_ctrl; | |
668 | } | |
669 | ||
7806f3dd | 670 | const EVP_MD *EVP_MD_CTX_md(const EVP_MD_CTX *ctx) |
0f113f3e | 671 | { |
b7c913c8 | 672 | if (ctx == NULL) |
0f113f3e | 673 | return NULL; |
b7c913c8 | 674 | return ctx->reqdigest; |
0f113f3e | 675 | } |
7806f3dd | 676 | |
7638370c RL |
677 | EVP_PKEY_CTX *EVP_MD_CTX_pkey_ctx(const EVP_MD_CTX *ctx) |
678 | { | |
679 | return ctx->pctx; | |
680 | } | |
681 | ||
319e518a MC |
682 | #if !defined(FIPS_MODE) |
683 | /* TODO(3.0): EVP_DigestSign* not yet supported in FIPS module */ | |
00902d94 PY |
684 | void EVP_MD_CTX_set_pkey_ctx(EVP_MD_CTX *ctx, EVP_PKEY_CTX *pctx) |
685 | { | |
81c79453 PY |
686 | /* |
687 | * it's reasonable to set NULL pctx (a.k.a clear the ctx->pctx), so | |
688 | * we have to deal with the cleanup job here. | |
689 | */ | |
690 | if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX)) | |
691 | EVP_PKEY_CTX_free(ctx->pctx); | |
692 | ||
00902d94 | 693 | ctx->pctx = pctx; |
81c79453 PY |
694 | |
695 | if (pctx != NULL) { | |
696 | /* make sure pctx is not freed when destroying EVP_MD_CTX */ | |
697 | EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX); | |
698 | } else { | |
699 | EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX); | |
700 | } | |
00902d94 | 701 | } |
319e518a | 702 | #endif /* !defined(FIPS_MODE) */ |
00902d94 | 703 | |
7638370c RL |
704 | void *EVP_MD_CTX_md_data(const EVP_MD_CTX *ctx) |
705 | { | |
706 | return ctx->md_data; | |
707 | } | |
708 | ||
709 | int (*EVP_MD_CTX_update_fn(EVP_MD_CTX *ctx))(EVP_MD_CTX *ctx, | |
710 | const void *data, size_t count) | |
711 | { | |
712 | return ctx->update; | |
713 | } | |
714 | ||
715 | void EVP_MD_CTX_set_update_fn(EVP_MD_CTX *ctx, | |
716 | int (*update) (EVP_MD_CTX *ctx, | |
717 | const void *data, size_t count)) | |
718 | { | |
719 | ctx->update = update; | |
720 | } | |
721 | ||
7806f3dd | 722 | void EVP_MD_CTX_set_flags(EVP_MD_CTX *ctx, int flags) |
0f113f3e MC |
723 | { |
724 | ctx->flags |= flags; | |
725 | } | |
7806f3dd NL |
726 | |
727 | void EVP_MD_CTX_clear_flags(EVP_MD_CTX *ctx, int flags) | |
0f113f3e MC |
728 | { |
729 | ctx->flags &= ~flags; | |
730 | } | |
7806f3dd NL |
731 | |
732 | int EVP_MD_CTX_test_flags(const EVP_MD_CTX *ctx, int flags) | |
0f113f3e MC |
733 | { |
734 | return (ctx->flags & flags); | |
735 | } | |
e92f9f45 DSH |
736 | |
737 | void EVP_CIPHER_CTX_set_flags(EVP_CIPHER_CTX *ctx, int flags) | |
0f113f3e MC |
738 | { |
739 | ctx->flags |= flags; | |
740 | } | |
e92f9f45 DSH |
741 | |
742 | void EVP_CIPHER_CTX_clear_flags(EVP_CIPHER_CTX *ctx, int flags) | |
0f113f3e MC |
743 | { |
744 | ctx->flags &= ~flags; | |
745 | } | |
e92f9f45 DSH |
746 | |
747 | int EVP_CIPHER_CTX_test_flags(const EVP_CIPHER_CTX *ctx, int flags) | |
0f113f3e MC |
748 | { |
749 | return (ctx->flags & flags); | |
750 | } | |
f842b6b2 RL |
751 | |
752 | int EVP_str2ctrl(int (*cb)(void *ctx, int cmd, void *buf, size_t buflen), | |
753 | void *ctx, int cmd, const char *value) | |
754 | { | |
755 | size_t len; | |
756 | ||
757 | len = strlen(value); | |
758 | if (len > INT_MAX) | |
759 | return -1; | |
760 | return cb(ctx, cmd, (void *)value, len); | |
761 | } | |
762 | ||
763 | int EVP_hex2ctrl(int (*cb)(void *ctx, int cmd, void *buf, size_t buflen), | |
764 | void *ctx, int cmd, const char *hex) | |
765 | { | |
766 | unsigned char *bin; | |
767 | long binlen; | |
768 | int rv = -1; | |
769 | ||
770 | bin = OPENSSL_hexstr2buf(hex, &binlen); | |
771 | if (bin == NULL) | |
772 | return 0; | |
773 | if (binlen <= INT_MAX) | |
774 | rv = cb(ctx, cmd, bin, binlen); | |
775 | OPENSSL_free(bin); | |
776 | return rv; | |
777 | } |