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