c99dd9e898cebe46d8e3711e117c33a0498d62ef
[openssl.git] / crypto / evp / evp_lib.c
1 /*
2  * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (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 "internal/provider.h"
16 #include "evp_locl.h"
17
18 int EVP_CIPHER_param_to_asn1(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
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) {
25         switch (EVP_CIPHER_CTX_mode(c)) {
26         case EVP_CIPH_WRAP_MODE:
27             if (EVP_CIPHER_CTX_nid(c) == NID_id_smime_alg_CMS3DESwrap)
28                 ASN1_TYPE_set(type, V_ASN1_NULL, NULL);
29             ret = 1;
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:
36             ret = -2;
37             break;
38
39         default:
40             ret = EVP_CIPHER_set_asn1_iv(c, type);
41         }
42     } else
43         ret = -1;
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;
50     return ret;
51 }
52
53 int EVP_CIPHER_asn1_to_param(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
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) {
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:
70             ret = -2;
71             break;
72
73         default:
74             ret = EVP_CIPHER_get_asn1_iv(c, type);
75             break;
76         }
77     } else
78         ret = -1;
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;
85     return ret;
86 }
87
88 int EVP_CIPHER_get_asn1_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
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)
98             return -1;
99         else if (i > 0)
100             memcpy(c->iv, c->oiv, l);
101     }
102     return i;
103 }
104
105 int EVP_CIPHER_set_asn1_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
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     }
115     return i;
116 }
117
118 /* Convert the various cipher NIDs and dummies to a proper OID NID */
119 int EVP_CIPHER_type(const EVP_CIPHER *ctx)
120 {
121     int nid;
122     ASN1_OBJECT *otmp;
123     nid = EVP_CIPHER_nid(ctx);
124
125     switch (nid) {
126
127     case NID_rc2_cbc:
128     case NID_rc2_64_cbc:
129     case NID_rc2_40_cbc:
130
131         return NID_rc2_cbc;
132
133     case NID_rc4:
134     case NID_rc4_40:
135
136         return NID_rc4;
137
138     case NID_aes_128_cfb128:
139     case NID_aes_128_cfb8:
140     case NID_aes_128_cfb1:
141
142         return NID_aes_128_cfb128;
143
144     case NID_aes_192_cfb128:
145     case NID_aes_192_cfb8:
146     case NID_aes_192_cfb1:
147
148         return NID_aes_192_cfb128;
149
150     case NID_aes_256_cfb128:
151     case NID_aes_256_cfb8:
152     case NID_aes_256_cfb1:
153
154         return NID_aes_256_cfb128;
155
156     case NID_des_cfb64:
157     case NID_des_cfb8:
158     case NID_des_cfb1:
159
160         return NID_des_cfb64;
161
162     case NID_des_ede3_cfb64:
163     case NID_des_ede3_cfb8:
164     case NID_des_ede3_cfb1:
165
166         return NID_des_cfb64;
167
168     default:
169         /* Check it has an OID and it is valid */
170         otmp = OBJ_nid2obj(nid);
171         if (OBJ_get0_data(otmp) == NULL)
172             nid = NID_undef;
173         ASN1_OBJECT_free(otmp);
174         return nid;
175     }
176 }
177
178 int EVP_CIPHER_block_size(const EVP_CIPHER *e)
179 {
180     return e->block_size;
181 }
182
183 int EVP_CIPHER_CTX_block_size(const EVP_CIPHER_CTX *ctx)
184 {
185     return ctx->cipher->block_size;
186 }
187
188 int EVP_CIPHER_impl_ctx_size(const EVP_CIPHER *e)
189 {
190     return e->ctx_size;
191 }
192
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 }
198
199 const EVP_CIPHER *EVP_CIPHER_CTX_cipher(const EVP_CIPHER_CTX *ctx)
200 {
201     return ctx->cipher;
202 }
203
204 int EVP_CIPHER_CTX_encrypting(const EVP_CIPHER_CTX *ctx)
205 {
206     return ctx->encrypt;
207 }
208
209 unsigned long EVP_CIPHER_flags(const EVP_CIPHER *cipher)
210 {
211     return cipher->flags;
212 }
213
214 void *EVP_CIPHER_CTX_get_app_data(const EVP_CIPHER_CTX *ctx)
215 {
216     return ctx->app_data;
217 }
218
219 void EVP_CIPHER_CTX_set_app_data(EVP_CIPHER_CTX *ctx, void *data)
220 {
221     ctx->app_data = data;
222 }
223
224 void *EVP_CIPHER_CTX_get_cipher_data(const EVP_CIPHER_CTX *ctx)
225 {
226     return ctx->cipher_data;
227 }
228
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
239 int EVP_CIPHER_iv_length(const EVP_CIPHER *cipher)
240 {
241     return cipher->iv_len;
242 }
243
244 int EVP_CIPHER_CTX_iv_length(const EVP_CIPHER_CTX *ctx)
245 {
246     return ctx->cipher->iv_len;
247 }
248
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
279 int EVP_CIPHER_key_length(const EVP_CIPHER *cipher)
280 {
281     if (cipher->prov != NULL) {
282         if (cipher->key_length != NULL)
283             return (int)cipher->key_length();
284         return -1;
285     }
286
287     return cipher->key_len;
288 }
289
290 int EVP_CIPHER_CTX_key_length(const EVP_CIPHER_CTX *ctx)
291 {
292     /*
293      * TODO(3.0): This may need to change if/when we introduce variable length
294      * key ciphers into the providers.
295      */
296     if (ctx->cipher != NULL && ctx->cipher->prov != NULL)
297         return EVP_CIPHER_key_length(ctx->cipher);
298     return ctx->key_len;
299 }
300
301 int EVP_CIPHER_nid(const EVP_CIPHER *cipher)
302 {
303     return cipher->nid;
304 }
305
306 int EVP_CIPHER_CTX_nid(const EVP_CIPHER_CTX *ctx)
307 {
308     return ctx->cipher->nid;
309 }
310
311 int EVP_MD_block_size(const EVP_MD *md)
312 {
313     if (md == NULL) {
314         EVPerr(EVP_F_EVP_MD_BLOCK_SIZE, EVP_R_MESSAGE_DIGEST_IS_NULL);
315         return -1;
316     }
317
318     if (md->prov != NULL && md->dblock_size != NULL)
319         return (int)md->dblock_size();
320
321     return md->block_size;
322 }
323
324 int EVP_MD_type(const EVP_MD *md)
325 {
326     return md->type;
327 }
328
329 int EVP_MD_pkey_type(const EVP_MD *md)
330 {
331     return md->pkey_type;
332 }
333
334 int EVP_MD_size(const EVP_MD *md)
335 {
336     if (!md) {
337         EVPerr(EVP_F_EVP_MD_SIZE, EVP_R_MESSAGE_DIGEST_IS_NULL);
338         return -1;
339     }
340
341     if (md->prov != NULL && md->size != NULL)
342         return (int)md->size();
343
344     return md->md_size;
345 }
346
347 unsigned long EVP_MD_flags(const EVP_MD *md)
348 {
349     return md->flags;
350 }
351
352 EVP_MD *EVP_MD_meth_new(int md_type, int pkey_type)
353 {
354     EVP_MD *md = OPENSSL_zalloc(sizeof(*md));
355
356     if (md != NULL) {
357         md->type = md_type;
358         md->pkey_type = pkey_type;
359         md->lock = CRYPTO_THREAD_lock_new();
360         if (md->lock == NULL) {
361             OPENSSL_free(md);
362             return NULL;
363         }
364         md->refcnt = 1;
365     }
366     return md;
367 }
368
369 EVP_MD *EVP_MD_meth_dup(const EVP_MD *md)
370 {
371     EVP_MD *to = EVP_MD_meth_new(md->type, md->pkey_type);
372
373     if (to != NULL) {
374         CRYPTO_RWLOCK *lock = to->lock;
375         memcpy(to, md, sizeof(*to));
376         to->lock = lock;
377     }
378     return to;
379 }
380
381 int EVP_MD_upref(EVP_MD *md)
382 {
383     int ref = 0;
384
385     CRYPTO_UP_REF(&md->refcnt, &ref, md->lock);
386     return 1;
387 }
388
389 void EVP_MD_meth_free(EVP_MD *md)
390 {
391     if (md != NULL) {
392         int i;
393
394         CRYPTO_DOWN_REF(&md->refcnt, &i, md->lock);
395         if (i > 0)
396             return;
397         ossl_provider_free(md->prov);
398         CRYPTO_THREAD_lock_free(md->lock);
399         OPENSSL_free(md);
400     }
401 }
402 int EVP_MD_meth_set_input_blocksize(EVP_MD *md, int blocksize)
403 {
404     md->block_size = blocksize;
405     return 1;
406 }
407 int EVP_MD_meth_set_result_size(EVP_MD *md, int resultsize)
408 {
409     md->md_size = resultsize;
410     return 1;
411 }
412 int EVP_MD_meth_set_app_datasize(EVP_MD *md, int datasize)
413 {
414     md->ctx_size = datasize;
415     return 1;
416 }
417 int EVP_MD_meth_set_flags(EVP_MD *md, unsigned long flags)
418 {
419     md->flags = flags;
420     return 1;
421 }
422 int EVP_MD_meth_set_init(EVP_MD *md, int (*init)(EVP_MD_CTX *ctx))
423 {
424     md->init = init;
425     return 1;
426 }
427 int EVP_MD_meth_set_update(EVP_MD *md, int (*update)(EVP_MD_CTX *ctx,
428                                                      const void *data,
429                                                      size_t count))
430 {
431     md->update = update;
432     return 1;
433 }
434 int EVP_MD_meth_set_final(EVP_MD *md, int (*final)(EVP_MD_CTX *ctx,
435                                                    unsigned char *md))
436 {
437     md->final = final;
438     return 1;
439 }
440 int EVP_MD_meth_set_copy(EVP_MD *md, int (*copy)(EVP_MD_CTX *to,
441                                                  const EVP_MD_CTX *from))
442 {
443     md->copy = copy;
444     return 1;
445 }
446 int EVP_MD_meth_set_cleanup(EVP_MD *md, int (*cleanup)(EVP_MD_CTX *ctx))
447 {
448     md->cleanup = cleanup;
449     return 1;
450 }
451 int EVP_MD_meth_set_ctrl(EVP_MD *md, int (*ctrl)(EVP_MD_CTX *ctx, int cmd,
452                                                  int p1, void *p2))
453 {
454     md->md_ctrl = ctrl;
455     return 1;
456 }
457
458 int EVP_MD_meth_get_input_blocksize(const EVP_MD *md)
459 {
460     return md->block_size;
461 }
462 int EVP_MD_meth_get_result_size(const EVP_MD *md)
463 {
464     return md->md_size;
465 }
466 int EVP_MD_meth_get_app_datasize(const EVP_MD *md)
467 {
468     return md->ctx_size;
469 }
470 unsigned long EVP_MD_meth_get_flags(const EVP_MD *md)
471 {
472     return md->flags;
473 }
474 int (*EVP_MD_meth_get_init(const EVP_MD *md))(EVP_MD_CTX *ctx)
475 {
476     return md->init;
477 }
478 int (*EVP_MD_meth_get_update(const EVP_MD *md))(EVP_MD_CTX *ctx,
479                                                 const void *data,
480                                                 size_t count)
481 {
482     return md->update;
483 }
484 int (*EVP_MD_meth_get_final(const EVP_MD *md))(EVP_MD_CTX *ctx,
485                                                unsigned char *md)
486 {
487     return md->final;
488 }
489 int (*EVP_MD_meth_get_copy(const EVP_MD *md))(EVP_MD_CTX *to,
490                                               const EVP_MD_CTX *from)
491 {
492     return md->copy;
493 }
494 int (*EVP_MD_meth_get_cleanup(const EVP_MD *md))(EVP_MD_CTX *ctx)
495 {
496     return md->cleanup;
497 }
498 int (*EVP_MD_meth_get_ctrl(const EVP_MD *md))(EVP_MD_CTX *ctx, int cmd,
499                                               int p1, void *p2)
500 {
501     return md->md_ctrl;
502 }
503
504 const EVP_MD *EVP_MD_CTX_md(const EVP_MD_CTX *ctx)
505 {
506     if (ctx == NULL)
507         return NULL;
508     return ctx->reqdigest;
509 }
510
511 EVP_PKEY_CTX *EVP_MD_CTX_pkey_ctx(const EVP_MD_CTX *ctx)
512 {
513     return ctx->pctx;
514 }
515
516 void EVP_MD_CTX_set_pkey_ctx(EVP_MD_CTX *ctx, EVP_PKEY_CTX *pctx)
517 {
518     /*
519      * it's reasonable to set NULL pctx (a.k.a clear the ctx->pctx), so
520      * we have to deal with the cleanup job here.
521      */
522     if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX))
523         EVP_PKEY_CTX_free(ctx->pctx);
524
525     ctx->pctx = pctx;
526
527     if (pctx != NULL) {
528         /* make sure pctx is not freed when destroying EVP_MD_CTX */
529         EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
530     } else {
531         EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
532     }
533 }
534
535 void *EVP_MD_CTX_md_data(const EVP_MD_CTX *ctx)
536 {
537     return ctx->md_data;
538 }
539
540 int (*EVP_MD_CTX_update_fn(EVP_MD_CTX *ctx))(EVP_MD_CTX *ctx,
541                                              const void *data, size_t count)
542 {
543     return ctx->update;
544 }
545
546 void EVP_MD_CTX_set_update_fn(EVP_MD_CTX *ctx,
547                               int (*update) (EVP_MD_CTX *ctx,
548                                              const void *data, size_t count))
549 {
550     ctx->update = update;
551 }
552
553 void EVP_MD_CTX_set_flags(EVP_MD_CTX *ctx, int flags)
554 {
555     ctx->flags |= flags;
556 }
557
558 void EVP_MD_CTX_clear_flags(EVP_MD_CTX *ctx, int flags)
559 {
560     ctx->flags &= ~flags;
561 }
562
563 int EVP_MD_CTX_test_flags(const EVP_MD_CTX *ctx, int flags)
564 {
565     return (ctx->flags & flags);
566 }
567
568 void EVP_CIPHER_CTX_set_flags(EVP_CIPHER_CTX *ctx, int flags)
569 {
570     ctx->flags |= flags;
571 }
572
573 void EVP_CIPHER_CTX_clear_flags(EVP_CIPHER_CTX *ctx, int flags)
574 {
575     ctx->flags &= ~flags;
576 }
577
578 int EVP_CIPHER_CTX_test_flags(const EVP_CIPHER_CTX *ctx, int flags)
579 {
580     return (ctx->flags & flags);
581 }
582
583 int EVP_str2ctrl(int (*cb)(void *ctx, int cmd, void *buf, size_t buflen),
584                  void *ctx, int cmd, const char *value)
585 {
586     size_t len;
587
588     len = strlen(value);
589     if (len > INT_MAX)
590         return -1;
591     return cb(ctx, cmd, (void *)value, len);
592 }
593
594 int EVP_hex2ctrl(int (*cb)(void *ctx, int cmd, void *buf, size_t buflen),
595                  void *ctx, int cmd, const char *hex)
596 {
597     unsigned char *bin;
598     long binlen;
599     int rv = -1;
600
601     bin = OPENSSL_hexstr2buf(hex, &binlen);
602     if (bin == NULL)
603         return 0;
604     if (binlen <= INT_MAX)
605         rv = cb(ctx, cmd, bin, binlen);
606     OPENSSL_free(bin);
607     return rv;
608 }