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