2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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"
12 NON_EMPTY_TRANSLATION_UNIT
16 # include <openssl/evp.h>
17 # include <openssl/objects.h>
18 # include <openssl/x509.h>
19 # include <openssl/rsa.h>
21 int EVP_OpenInit(EVP_CIPHER_CTX
*ctx
, const EVP_CIPHER
*type
,
22 const unsigned char *ek
, int ekl
, const unsigned char *iv
,
25 unsigned char *key
= NULL
;
26 int i
, size
= 0, ret
= 0;
29 EVP_CIPHER_CTX_reset(ctx
);
30 if (!EVP_DecryptInit_ex(ctx
, type
, NULL
, NULL
, NULL
))
37 if (EVP_PKEY_id(priv
) != EVP_PKEY_RSA
) {
38 EVPerr(EVP_F_EVP_OPENINIT
, EVP_R_PUBLIC_KEY_NOT_RSA
);
42 size
= EVP_PKEY_size(priv
);
43 key
= OPENSSL_malloc(size
+ 2);
46 EVPerr(EVP_F_EVP_OPENINIT
, ERR_R_MALLOC_FAILURE
);
50 i
= EVP_PKEY_decrypt_old(key
, ek
, ekl
, priv
);
51 if ((i
<= 0) || !EVP_CIPHER_CTX_set_key_length(ctx
, i
)) {
55 if (!EVP_DecryptInit_ex(ctx
, NULL
, NULL
, key
, iv
))
60 OPENSSL_clear_free(key
, size
);
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
);