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