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