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