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 int EVP_CIPHER_param_to_asn1(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
23 const EVP_CIPHER *cipher = c->cipher;
25 if (cipher->prov != NULL) {
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.
32 cipher = EVP_get_cipherbynid(cipher->nid);
34 EVPerr(EVP_F_EVP_CIPHER_PARAM_TO_ASN1, ASN1_R_UNSUPPORTED_CIPHER);
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);
49 case EVP_CIPH_GCM_MODE:
50 case EVP_CIPH_CCM_MODE:
51 case EVP_CIPH_XTS_MODE:
52 case EVP_CIPH_OCB_MODE:
57 ret = EVP_CIPHER_set_asn1_iv(c, type);
62 EVPerr(EVP_F_EVP_CIPHER_PARAM_TO_ASN1, ret == -2 ?
63 ASN1_R_UNSUPPORTED_CIPHER :
64 EVP_R_CIPHER_PARAMETER_ERROR);
70 int EVP_CIPHER_asn1_to_param(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
73 const EVP_CIPHER *cipher = c->cipher;
75 if (cipher->prov != NULL) {
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.
80 cipher = EVP_get_cipherbynid(cipher->nid);
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)) {
90 case EVP_CIPH_WRAP_MODE:
94 case EVP_CIPH_GCM_MODE:
95 case EVP_CIPH_CCM_MODE:
96 case EVP_CIPH_XTS_MODE:
97 case EVP_CIPH_OCB_MODE:
102 ret = EVP_CIPHER_get_asn1_iv(c, type);
108 EVPerr(EVP_F_EVP_CIPHER_ASN1_TO_PARAM, ret == -2 ?
109 EVP_R_UNSUPPORTED_CIPHER :
110 EVP_R_CIPHER_PARAMETER_ERROR);
116 int EVP_CIPHER_get_asn1_iv(EVP_CIPHER_CTX *ctx, ASN1_TYPE *type)
122 unsigned char iv[EVP_MAX_IV_LENGTH];
124 l = EVP_CIPHER_CTX_iv_length(ctx);
125 if (!ossl_assert(l <= sizeof(iv)))
127 i = ASN1_TYPE_get_octetstring(type, iv, l);
131 if (!EVP_CipherInit_ex(ctx, NULL, NULL, NULL, iv, -1))
137 int EVP_CIPHER_set_asn1_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
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);
150 /* Convert the various cipher NIDs and dummies to a proper OID NID */
151 int EVP_CIPHER_type(const EVP_CIPHER *ctx)
155 nid = EVP_CIPHER_nid(ctx);
170 case NID_aes_128_cfb128:
171 case NID_aes_128_cfb8:
172 case NID_aes_128_cfb1:
174 return NID_aes_128_cfb128;
176 case NID_aes_192_cfb128:
177 case NID_aes_192_cfb8:
178 case NID_aes_192_cfb1:
180 return NID_aes_192_cfb128;
182 case NID_aes_256_cfb128:
183 case NID_aes_256_cfb8:
184 case NID_aes_256_cfb1:
186 return NID_aes_256_cfb128;
192 return NID_des_cfb64;
194 case NID_des_ede3_cfb64:
195 case NID_des_ede3_cfb8:
196 case NID_des_ede3_cfb1:
198 return NID_des_cfb64;
201 /* Check it has an OID and it is valid */
202 otmp = OBJ_nid2obj(nid);
203 if (OBJ_get0_data(otmp) == NULL)
205 ASN1_OBJECT_free(otmp);
210 int EVP_CIPHER_block_size(const EVP_CIPHER *cipher)
212 if (cipher->prov != NULL) {
213 if (cipher->blocksize != NULL)
214 return cipher->blocksize();
215 /* We default to a block size of 1 */
218 return cipher->block_size;
221 int EVP_CIPHER_CTX_block_size(const EVP_CIPHER_CTX *ctx)
223 return EVP_CIPHER_block_size(ctx->cipher);
226 int EVP_CIPHER_impl_ctx_size(const EVP_CIPHER *e)
231 int EVP_Cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
232 const unsigned char *in, unsigned int inl)
234 if (ctx->cipher->prov != NULL) {
235 size_t outl = 0; /* ignored */
236 int blocksize = EVP_CIPHER_CTX_block_size(ctx);
238 if (ctx->cipher->ccipher != NULL)
240 ctx->cipher->ccipher(ctx->provctx, out, &outl,
241 inl + (blocksize == 1 ? 0 : blocksize),
246 return ctx->cipher->do_cipher(ctx, out, in, inl);
249 const EVP_CIPHER *EVP_CIPHER_CTX_cipher(const EVP_CIPHER_CTX *ctx)
254 int EVP_CIPHER_CTX_encrypting(const EVP_CIPHER_CTX *ctx)
259 unsigned long EVP_CIPHER_flags(const EVP_CIPHER *cipher)
261 return cipher->flags;
264 void *EVP_CIPHER_CTX_get_app_data(const EVP_CIPHER_CTX *ctx)
266 return ctx->app_data;
269 void EVP_CIPHER_CTX_set_app_data(EVP_CIPHER_CTX *ctx, void *data)
271 ctx->app_data = data;
274 void *EVP_CIPHER_CTX_get_cipher_data(const EVP_CIPHER_CTX *ctx)
276 return ctx->cipher_data;
279 void *EVP_CIPHER_CTX_set_cipher_data(EVP_CIPHER_CTX *ctx, void *cipher_data)
281 void *old_cipher_data;
283 old_cipher_data = ctx->cipher_data;
284 ctx->cipher_data = cipher_data;
286 return old_cipher_data;
289 int EVP_CIPHER_iv_length(const EVP_CIPHER *cipher)
291 if (cipher->prov != NULL) {
292 if (cipher->iv_length != NULL)
293 return (int)cipher->iv_length();
297 return cipher->iv_len;
300 int EVP_CIPHER_CTX_iv_length(const EVP_CIPHER_CTX *ctx)
302 return EVP_CIPHER_iv_length(ctx->cipher);
305 const unsigned char *EVP_CIPHER_CTX_original_iv(const EVP_CIPHER_CTX *ctx)
310 const unsigned char *EVP_CIPHER_CTX_iv(const EVP_CIPHER_CTX *ctx)
315 unsigned char *EVP_CIPHER_CTX_iv_noconst(EVP_CIPHER_CTX *ctx)
320 unsigned char *EVP_CIPHER_CTX_buf_noconst(EVP_CIPHER_CTX *ctx)
325 int EVP_CIPHER_CTX_num(const EVP_CIPHER_CTX *ctx)
330 void EVP_CIPHER_CTX_set_num(EVP_CIPHER_CTX *ctx, int num)
335 int EVP_CIPHER_key_length(const EVP_CIPHER *cipher)
337 if (cipher->prov != NULL) {
338 if (cipher->key_length != NULL)
339 return (int)cipher->key_length();
343 return cipher->key_len;
346 int EVP_CIPHER_CTX_key_length(const EVP_CIPHER_CTX *ctx)
349 * TODO(3.0): This may need to change if/when we introduce variable length
350 * key ciphers into the providers.
352 if (ctx->cipher != NULL && ctx->cipher->prov != NULL)
353 return EVP_CIPHER_key_length(ctx->cipher);
357 int EVP_CIPHER_nid(const EVP_CIPHER *cipher)
362 int EVP_CIPHER_CTX_nid(const EVP_CIPHER_CTX *ctx)
364 return ctx->cipher->nid;
367 int EVP_CIPHER_mode(const EVP_CIPHER *cipher)
369 if (cipher->prov != NULL) {
372 /* Cipher comes from a provider - so ask the provider for the mode */
373 OSSL_PARAM params[] = {
374 OSSL_PARAM_int(OSSL_CIPHER_PARAM_MODE, NULL),
378 params[0].data = &mode;
380 if (cipher->get_params == NULL) {
381 EVPerr(EVP_F_EVP_CIPHER_MODE, EVP_R_CTRL_NOT_IMPLEMENTED);
385 if (!cipher->get_params(params))
390 return EVP_CIPHER_flags(cipher) & EVP_CIPH_MODE;
394 int EVP_MD_block_size(const EVP_MD *md)
397 EVPerr(EVP_F_EVP_MD_BLOCK_SIZE, EVP_R_MESSAGE_DIGEST_IS_NULL);
401 if (md->prov != NULL && md->dblock_size != NULL)
402 return (int)md->dblock_size();
404 return md->block_size;
407 int EVP_MD_type(const EVP_MD *md)
412 int EVP_MD_pkey_type(const EVP_MD *md)
414 return md->pkey_type;
417 int EVP_MD_size(const EVP_MD *md)
420 EVPerr(EVP_F_EVP_MD_SIZE, EVP_R_MESSAGE_DIGEST_IS_NULL);
424 if (md->prov != NULL && md->size != NULL)
425 return (int)md->size();
430 unsigned long EVP_MD_flags(const EVP_MD *md)
435 EVP_MD *EVP_MD_meth_new(int md_type, int pkey_type)
437 EVP_MD *md = OPENSSL_zalloc(sizeof(*md));
441 md->pkey_type = pkey_type;
442 md->lock = CRYPTO_THREAD_lock_new();
443 if (md->lock == NULL) {
452 EVP_MD *EVP_MD_meth_dup(const EVP_MD *md)
454 EVP_MD *to = EVP_MD_meth_new(md->type, md->pkey_type);
457 CRYPTO_RWLOCK *lock = to->lock;
458 memcpy(to, md, sizeof(*to));
464 int EVP_MD_upref(EVP_MD *md)
468 CRYPTO_UP_REF(&md->refcnt, &ref, md->lock);
472 void EVP_MD_meth_free(EVP_MD *md)
477 CRYPTO_DOWN_REF(&md->refcnt, &i, md->lock);
480 ossl_provider_free(md->prov);
481 CRYPTO_THREAD_lock_free(md->lock);
485 int EVP_MD_meth_set_input_blocksize(EVP_MD *md, int blocksize)
487 md->block_size = blocksize;
490 int EVP_MD_meth_set_result_size(EVP_MD *md, int resultsize)
492 md->md_size = resultsize;
495 int EVP_MD_meth_set_app_datasize(EVP_MD *md, int datasize)
497 md->ctx_size = datasize;
500 int EVP_MD_meth_set_flags(EVP_MD *md, unsigned long flags)
505 int EVP_MD_meth_set_init(EVP_MD *md, int (*init)(EVP_MD_CTX *ctx))
510 int EVP_MD_meth_set_update(EVP_MD *md, int (*update)(EVP_MD_CTX *ctx,
517 int EVP_MD_meth_set_final(EVP_MD *md, int (*final)(EVP_MD_CTX *ctx,
523 int EVP_MD_meth_set_copy(EVP_MD *md, int (*copy)(EVP_MD_CTX *to,
524 const EVP_MD_CTX *from))
529 int EVP_MD_meth_set_cleanup(EVP_MD *md, int (*cleanup)(EVP_MD_CTX *ctx))
531 md->cleanup = cleanup;
534 int EVP_MD_meth_set_ctrl(EVP_MD *md, int (*ctrl)(EVP_MD_CTX *ctx, int cmd,
541 int EVP_MD_meth_get_input_blocksize(const EVP_MD *md)
543 return md->block_size;
545 int EVP_MD_meth_get_result_size(const EVP_MD *md)
549 int EVP_MD_meth_get_app_datasize(const EVP_MD *md)
553 unsigned long EVP_MD_meth_get_flags(const EVP_MD *md)
557 int (*EVP_MD_meth_get_init(const EVP_MD *md))(EVP_MD_CTX *ctx)
561 int (*EVP_MD_meth_get_update(const EVP_MD *md))(EVP_MD_CTX *ctx,
567 int (*EVP_MD_meth_get_final(const EVP_MD *md))(EVP_MD_CTX *ctx,
572 int (*EVP_MD_meth_get_copy(const EVP_MD *md))(EVP_MD_CTX *to,
573 const EVP_MD_CTX *from)
577 int (*EVP_MD_meth_get_cleanup(const EVP_MD *md))(EVP_MD_CTX *ctx)
581 int (*EVP_MD_meth_get_ctrl(const EVP_MD *md))(EVP_MD_CTX *ctx, int cmd,
587 const EVP_MD *EVP_MD_CTX_md(const EVP_MD_CTX *ctx)
591 return ctx->reqdigest;
594 EVP_PKEY_CTX *EVP_MD_CTX_pkey_ctx(const EVP_MD_CTX *ctx)
599 void EVP_MD_CTX_set_pkey_ctx(EVP_MD_CTX *ctx, EVP_PKEY_CTX *pctx)
602 * it's reasonable to set NULL pctx (a.k.a clear the ctx->pctx), so
603 * we have to deal with the cleanup job here.
605 if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX))
606 EVP_PKEY_CTX_free(ctx->pctx);
611 /* make sure pctx is not freed when destroying EVP_MD_CTX */
612 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
614 EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
618 void *EVP_MD_CTX_md_data(const EVP_MD_CTX *ctx)
623 int (*EVP_MD_CTX_update_fn(EVP_MD_CTX *ctx))(EVP_MD_CTX *ctx,
624 const void *data, size_t count)
629 void EVP_MD_CTX_set_update_fn(EVP_MD_CTX *ctx,
630 int (*update) (EVP_MD_CTX *ctx,
631 const void *data, size_t count))
633 ctx->update = update;
636 void EVP_MD_CTX_set_flags(EVP_MD_CTX *ctx, int flags)
641 void EVP_MD_CTX_clear_flags(EVP_MD_CTX *ctx, int flags)
643 ctx->flags &= ~flags;
646 int EVP_MD_CTX_test_flags(const EVP_MD_CTX *ctx, int flags)
648 return (ctx->flags & flags);
651 void EVP_CIPHER_CTX_set_flags(EVP_CIPHER_CTX *ctx, int flags)
656 void EVP_CIPHER_CTX_clear_flags(EVP_CIPHER_CTX *ctx, int flags)
658 ctx->flags &= ~flags;
661 int EVP_CIPHER_CTX_test_flags(const EVP_CIPHER_CTX *ctx, int flags)
663 return (ctx->flags & flags);
666 int EVP_str2ctrl(int (*cb)(void *ctx, int cmd, void *buf, size_t buflen),
667 void *ctx, int cmd, const char *value)
674 return cb(ctx, cmd, (void *)value, len);
677 int EVP_hex2ctrl(int (*cb)(void *ctx, int cmd, void *buf, size_t buflen),
678 void *ctx, int cmd, const char *hex)
684 bin = OPENSSL_hexstr2buf(hex, &binlen);
687 if (binlen <= INT_MAX)
688 rv = cb(ctx, cmd, bin, binlen);