2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
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
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"
20 #if !defined(FIPS_MODE)
21 int EVP_CIPHER_param_to_asn1(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
24 const EVP_CIPHER *cipher = c->cipher;
26 if (cipher->prov != NULL) {
28 * The cipher has come from a provider and won't have the default flags.
29 * Find the implicit form so we can check the flags.
30 * TODO(3.0): This won't work for 3rd party ciphers we know nothing about
31 * We'll need to think of something else for those.
33 cipher = EVP_get_cipherbynid(cipher->nid);
35 EVPerr(EVP_F_EVP_CIPHER_PARAM_TO_ASN1, ASN1_R_UNSUPPORTED_CIPHER);
40 if (cipher->set_asn1_parameters != NULL)
41 ret = cipher->set_asn1_parameters(c, type);
42 else if (cipher->flags & EVP_CIPH_FLAG_DEFAULT_ASN1) {
43 switch (EVP_CIPHER_mode(cipher)) {
44 case EVP_CIPH_WRAP_MODE:
45 if (EVP_CIPHER_nid(cipher) == NID_id_smime_alg_CMS3DESwrap)
46 ASN1_TYPE_set(type, V_ASN1_NULL, NULL);
50 case EVP_CIPH_GCM_MODE:
51 case EVP_CIPH_CCM_MODE:
52 case EVP_CIPH_XTS_MODE:
53 case EVP_CIPH_OCB_MODE:
58 ret = EVP_CIPHER_set_asn1_iv(c, type);
63 EVPerr(EVP_F_EVP_CIPHER_PARAM_TO_ASN1, ret == -2 ?
64 ASN1_R_UNSUPPORTED_CIPHER :
65 EVP_R_CIPHER_PARAMETER_ERROR);
71 int EVP_CIPHER_asn1_to_param(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
74 const EVP_CIPHER *cipher = c->cipher;
76 if (cipher->prov != NULL) {
78 * The cipher has come from a provider and won't have the default flags.
79 * Find the implicit form so we can check the flags.
81 cipher = EVP_get_cipherbynid(cipher->nid);
86 if (cipher->get_asn1_parameters != NULL)
87 ret = cipher->get_asn1_parameters(c, type);
88 else if (cipher->flags & EVP_CIPH_FLAG_DEFAULT_ASN1) {
89 switch (EVP_CIPHER_mode(cipher)) {
91 case EVP_CIPH_WRAP_MODE:
95 case EVP_CIPH_GCM_MODE:
96 case EVP_CIPH_CCM_MODE:
97 case EVP_CIPH_XTS_MODE:
98 case EVP_CIPH_OCB_MODE:
103 ret = EVP_CIPHER_get_asn1_iv(c, type);
109 EVPerr(EVP_F_EVP_CIPHER_ASN1_TO_PARAM, ret == -2 ?
110 EVP_R_UNSUPPORTED_CIPHER :
111 EVP_R_CIPHER_PARAMETER_ERROR);
117 int EVP_CIPHER_get_asn1_iv(EVP_CIPHER_CTX *ctx, ASN1_TYPE *type)
123 unsigned char iv[EVP_MAX_IV_LENGTH];
125 l = EVP_CIPHER_CTX_iv_length(ctx);
126 if (!ossl_assert(l <= sizeof(iv)))
128 i = ASN1_TYPE_get_octetstring(type, iv, l);
132 if (!EVP_CipherInit_ex(ctx, NULL, NULL, NULL, iv, -1))
138 int EVP_CIPHER_set_asn1_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
144 j = EVP_CIPHER_CTX_iv_length(c);
145 OPENSSL_assert(j <= sizeof(c->iv));
146 i = ASN1_TYPE_set_octetstring(type, c->oiv, j);
150 #endif /* !defined(FIPS_MODE) */
152 /* Convert the various cipher NIDs and dummies to a proper OID NID */
153 int EVP_CIPHER_type(const EVP_CIPHER *ctx)
156 nid = EVP_CIPHER_nid(ctx);
171 case NID_aes_128_cfb128:
172 case NID_aes_128_cfb8:
173 case NID_aes_128_cfb1:
175 return NID_aes_128_cfb128;
177 case NID_aes_192_cfb128:
178 case NID_aes_192_cfb8:
179 case NID_aes_192_cfb1:
181 return NID_aes_192_cfb128;
183 case NID_aes_256_cfb128:
184 case NID_aes_256_cfb8:
185 case NID_aes_256_cfb1:
187 return NID_aes_256_cfb128;
193 return NID_des_cfb64;
195 case NID_des_ede3_cfb64:
196 case NID_des_ede3_cfb8:
197 case NID_des_ede3_cfb1:
199 return NID_des_cfb64;
206 /* Check it has an OID and it is valid */
207 ASN1_OBJECT *otmp = OBJ_nid2obj(nid);
209 if (OBJ_get0_data(otmp) == NULL)
211 ASN1_OBJECT_free(otmp);
218 int EVP_CIPHER_block_size(const EVP_CIPHER *cipher)
220 if (cipher->prov != NULL) {
221 if (cipher->blocksize != NULL)
222 return cipher->blocksize();
223 /* We default to a block size of 1 */
226 return cipher->block_size;
229 int EVP_CIPHER_CTX_block_size(const EVP_CIPHER_CTX *ctx)
231 return EVP_CIPHER_block_size(ctx->cipher);
234 int EVP_CIPHER_impl_ctx_size(const EVP_CIPHER *e)
239 int EVP_Cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
240 const unsigned char *in, unsigned int inl)
242 if (ctx->cipher->prov != NULL) {
243 size_t outl = 0; /* ignored */
244 int blocksize = EVP_CIPHER_CTX_block_size(ctx);
246 if (ctx->cipher->ccipher != NULL)
248 ctx->cipher->ccipher(ctx->provctx, out, &outl,
249 inl + (blocksize == 1 ? 0 : blocksize),
254 return ctx->cipher->do_cipher(ctx, out, in, inl);
257 const EVP_CIPHER *EVP_CIPHER_CTX_cipher(const EVP_CIPHER_CTX *ctx)
262 int EVP_CIPHER_CTX_encrypting(const EVP_CIPHER_CTX *ctx)
267 unsigned long EVP_CIPHER_flags(const EVP_CIPHER *cipher)
269 return cipher->flags;
272 void *EVP_CIPHER_CTX_get_app_data(const EVP_CIPHER_CTX *ctx)
274 return ctx->app_data;
277 void EVP_CIPHER_CTX_set_app_data(EVP_CIPHER_CTX *ctx, void *data)
279 ctx->app_data = data;
282 void *EVP_CIPHER_CTX_get_cipher_data(const EVP_CIPHER_CTX *ctx)
284 return ctx->cipher_data;
287 void *EVP_CIPHER_CTX_set_cipher_data(EVP_CIPHER_CTX *ctx, void *cipher_data)
289 void *old_cipher_data;
291 old_cipher_data = ctx->cipher_data;
292 ctx->cipher_data = cipher_data;
294 return old_cipher_data;
297 int EVP_CIPHER_iv_length(const EVP_CIPHER *cipher)
299 if (cipher->prov != NULL) {
300 if (cipher->iv_length != NULL)
301 return (int)cipher->iv_length();
305 return cipher->iv_len;
308 int EVP_CIPHER_CTX_iv_length(const EVP_CIPHER_CTX *ctx)
310 return EVP_CIPHER_iv_length(ctx->cipher);
313 const unsigned char *EVP_CIPHER_CTX_original_iv(const EVP_CIPHER_CTX *ctx)
318 const unsigned char *EVP_CIPHER_CTX_iv(const EVP_CIPHER_CTX *ctx)
323 unsigned char *EVP_CIPHER_CTX_iv_noconst(EVP_CIPHER_CTX *ctx)
328 unsigned char *EVP_CIPHER_CTX_buf_noconst(EVP_CIPHER_CTX *ctx)
333 int EVP_CIPHER_CTX_num(const EVP_CIPHER_CTX *ctx)
338 void EVP_CIPHER_CTX_set_num(EVP_CIPHER_CTX *ctx, int num)
343 int EVP_CIPHER_key_length(const EVP_CIPHER *cipher)
345 if (cipher->prov != NULL) {
346 if (cipher->key_length != NULL)
347 return (int)cipher->key_length();
351 return cipher->key_len;
354 int EVP_CIPHER_CTX_key_length(const EVP_CIPHER_CTX *ctx)
357 * TODO(3.0): This may need to change if/when we introduce variable length
358 * key ciphers into the providers.
360 if (ctx->cipher != NULL && ctx->cipher->prov != NULL)
361 return EVP_CIPHER_key_length(ctx->cipher);
365 int EVP_CIPHER_nid(const EVP_CIPHER *cipher)
370 int EVP_CIPHER_CTX_nid(const EVP_CIPHER_CTX *ctx)
372 return ctx->cipher->nid;
375 int EVP_CIPHER_mode(const EVP_CIPHER *cipher)
377 if (cipher->prov != NULL) {
380 /* Cipher comes from a provider - so ask the provider for the mode */
381 OSSL_PARAM params[] = {
382 OSSL_PARAM_int(OSSL_CIPHER_PARAM_MODE, NULL),
386 params[0].data = &mode;
388 if (cipher->get_params == NULL) {
389 EVPerr(EVP_F_EVP_CIPHER_MODE, EVP_R_CTRL_NOT_IMPLEMENTED);
393 if (!cipher->get_params(params))
398 return EVP_CIPHER_flags(cipher) & EVP_CIPH_MODE;
402 int EVP_MD_block_size(const EVP_MD *md)
405 EVPerr(EVP_F_EVP_MD_BLOCK_SIZE, EVP_R_MESSAGE_DIGEST_IS_NULL);
409 if (md->prov != NULL && md->dblock_size != NULL)
410 return (int)md->dblock_size();
412 return md->block_size;
415 int EVP_MD_type(const EVP_MD *md)
420 int EVP_MD_pkey_type(const EVP_MD *md)
422 return md->pkey_type;
425 int EVP_MD_size(const EVP_MD *md)
428 EVPerr(EVP_F_EVP_MD_SIZE, EVP_R_MESSAGE_DIGEST_IS_NULL);
432 if (md->prov != NULL && md->size != NULL)
433 return (int)md->size();
438 unsigned long EVP_MD_flags(const EVP_MD *md)
443 EVP_MD *EVP_MD_meth_new(int md_type, int pkey_type)
445 EVP_MD *md = OPENSSL_zalloc(sizeof(*md));
449 md->pkey_type = pkey_type;
450 md->lock = CRYPTO_THREAD_lock_new();
451 if (md->lock == NULL) {
460 EVP_MD *EVP_MD_meth_dup(const EVP_MD *md)
462 EVP_MD *to = EVP_MD_meth_new(md->type, md->pkey_type);
465 CRYPTO_RWLOCK *lock = to->lock;
466 memcpy(to, md, sizeof(*to));
472 int EVP_MD_upref(EVP_MD *md)
476 CRYPTO_UP_REF(&md->refcnt, &ref, md->lock);
480 void EVP_MD_meth_free(EVP_MD *md)
485 CRYPTO_DOWN_REF(&md->refcnt, &i, md->lock);
488 ossl_provider_free(md->prov);
489 CRYPTO_THREAD_lock_free(md->lock);
493 int EVP_MD_meth_set_input_blocksize(EVP_MD *md, int blocksize)
495 md->block_size = blocksize;
498 int EVP_MD_meth_set_result_size(EVP_MD *md, int resultsize)
500 md->md_size = resultsize;
503 int EVP_MD_meth_set_app_datasize(EVP_MD *md, int datasize)
505 md->ctx_size = datasize;
508 int EVP_MD_meth_set_flags(EVP_MD *md, unsigned long flags)
513 int EVP_MD_meth_set_init(EVP_MD *md, int (*init)(EVP_MD_CTX *ctx))
518 int EVP_MD_meth_set_update(EVP_MD *md, int (*update)(EVP_MD_CTX *ctx,
525 int EVP_MD_meth_set_final(EVP_MD *md, int (*final)(EVP_MD_CTX *ctx,
531 int EVP_MD_meth_set_copy(EVP_MD *md, int (*copy)(EVP_MD_CTX *to,
532 const EVP_MD_CTX *from))
537 int EVP_MD_meth_set_cleanup(EVP_MD *md, int (*cleanup)(EVP_MD_CTX *ctx))
539 md->cleanup = cleanup;
542 int EVP_MD_meth_set_ctrl(EVP_MD *md, int (*ctrl)(EVP_MD_CTX *ctx, int cmd,
549 int EVP_MD_meth_get_input_blocksize(const EVP_MD *md)
551 return md->block_size;
553 int EVP_MD_meth_get_result_size(const EVP_MD *md)
557 int EVP_MD_meth_get_app_datasize(const EVP_MD *md)
561 unsigned long EVP_MD_meth_get_flags(const EVP_MD *md)
565 int (*EVP_MD_meth_get_init(const EVP_MD *md))(EVP_MD_CTX *ctx)
569 int (*EVP_MD_meth_get_update(const EVP_MD *md))(EVP_MD_CTX *ctx,
575 int (*EVP_MD_meth_get_final(const EVP_MD *md))(EVP_MD_CTX *ctx,
580 int (*EVP_MD_meth_get_copy(const EVP_MD *md))(EVP_MD_CTX *to,
581 const EVP_MD_CTX *from)
585 int (*EVP_MD_meth_get_cleanup(const EVP_MD *md))(EVP_MD_CTX *ctx)
589 int (*EVP_MD_meth_get_ctrl(const EVP_MD *md))(EVP_MD_CTX *ctx, int cmd,
595 const EVP_MD *EVP_MD_CTX_md(const EVP_MD_CTX *ctx)
599 return ctx->reqdigest;
602 EVP_PKEY_CTX *EVP_MD_CTX_pkey_ctx(const EVP_MD_CTX *ctx)
607 #if !defined(FIPS_MODE)
608 /* TODO(3.0): EVP_DigestSign* not yet supported in FIPS module */
609 void EVP_MD_CTX_set_pkey_ctx(EVP_MD_CTX *ctx, EVP_PKEY_CTX *pctx)
612 * it's reasonable to set NULL pctx (a.k.a clear the ctx->pctx), so
613 * we have to deal with the cleanup job here.
615 if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX))
616 EVP_PKEY_CTX_free(ctx->pctx);
621 /* make sure pctx is not freed when destroying EVP_MD_CTX */
622 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
624 EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
627 #endif /* !defined(FIPS_MODE) */
629 void *EVP_MD_CTX_md_data(const EVP_MD_CTX *ctx)
634 int (*EVP_MD_CTX_update_fn(EVP_MD_CTX *ctx))(EVP_MD_CTX *ctx,
635 const void *data, size_t count)
640 void EVP_MD_CTX_set_update_fn(EVP_MD_CTX *ctx,
641 int (*update) (EVP_MD_CTX *ctx,
642 const void *data, size_t count))
644 ctx->update = update;
647 void EVP_MD_CTX_set_flags(EVP_MD_CTX *ctx, int flags)
652 void EVP_MD_CTX_clear_flags(EVP_MD_CTX *ctx, int flags)
654 ctx->flags &= ~flags;
657 int EVP_MD_CTX_test_flags(const EVP_MD_CTX *ctx, int flags)
659 return (ctx->flags & flags);
662 void EVP_CIPHER_CTX_set_flags(EVP_CIPHER_CTX *ctx, int flags)
667 void EVP_CIPHER_CTX_clear_flags(EVP_CIPHER_CTX *ctx, int flags)
669 ctx->flags &= ~flags;
672 int EVP_CIPHER_CTX_test_flags(const EVP_CIPHER_CTX *ctx, int flags)
674 return (ctx->flags & flags);
677 int EVP_str2ctrl(int (*cb)(void *ctx, int cmd, void *buf, size_t buflen),
678 void *ctx, int cmd, const char *value)
685 return cb(ctx, cmd, (void *)value, len);
688 int EVP_hex2ctrl(int (*cb)(void *ctx, int cmd, void *buf, size_t buflen),
689 void *ctx, int cmd, const char *hex)
695 bin = OPENSSL_hexstr2buf(hex, &binlen);
698 if (binlen <= INT_MAX)
699 rv = cb(ctx, cmd, bin, binlen);