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