2 * Copyright 1995-2020 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
10 #include "internal/cryptlib.h"
13 #include <openssl/evp.h>
14 #include <openssl/objects.h>
15 #include <openssl/x509.h>
16 #include <openssl/rsa.h>
18 int EVP_OpenInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
19 const unsigned char *ek, int ekl, const unsigned char *iv,
22 unsigned char *key = NULL;
25 EVP_PKEY_CTX *pctx = NULL;
28 EVP_CIPHER_CTX_reset(ctx);
29 if (!EVP_DecryptInit_ex(ctx, type, NULL, NULL, NULL))
36 if ((pctx = EVP_PKEY_CTX_new(priv, NULL)) == NULL) {
37 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
41 if (EVP_PKEY_decrypt_init(pctx) <= 0
42 || EVP_PKEY_decrypt(pctx, NULL, &keylen, ek, ekl) <= 0)
45 if ((key = OPENSSL_malloc(keylen)) == NULL) {
46 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
50 if (EVP_PKEY_decrypt(pctx, key, &keylen, ek, ekl) <= 0)
53 if (!EVP_CIPHER_CTX_set_key_length(ctx, keylen)
54 || !EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv))
59 EVP_PKEY_CTX_free(pctx);
60 OPENSSL_clear_free(key, keylen);
64 int EVP_OpenFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
68 i = EVP_DecryptFinal_ex(ctx, out, outl);
70 i = EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, NULL);