52#include <wolfssl/options.h>
53#include <wolfssl/openssl/aes.h>
54#include <wolfssl/openssl/err.h>
55#include <wolfssl/openssl/evp.h>
57#include <openssl/aes.h>
58#include <openssl/err.h>
59#include <openssl/evp.h>
70#define MOD_NAME "[decrypt] "
76 unsigned char ivec[AES_BLOCK_SIZE];
77 unsigned char ecount[AES_BLOCK_SIZE];
82 const char *passphrase)
90 if (strstr(pass,
":cipher=")) {
91 *strstr(pass,
":cipher=") =
'\0';
97 MD5Update(&context, (
const unsigned char *) pass,
101 s->
ctx = EVP_CIPHER_CTX_new();
113 EVP_CIPHER_CTX_free(s->
ctx);
117#define CHECK(action, errmsg) do { int rc = action; if (rc != 1) { log_msg(LOG_LEVEL_ERROR, MOD_NAME errmsg ": %s\n", ERR_error_string(ERR_get_error(), NULL)); return 0; } } while(0)
118#pragma GCC diagnostic ignored "-Wcast-qual"
120 const char *ciphertext,
int ciphertext_len,
121 const char *aad,
int aad_len,
125 if (cipher == NULL) {
130 memcpy(&data_len, ciphertext,
sizeof(uint32_t));
131 assert ((
size_t) ciphertext_len >= data_len +
sizeof(uint32_t) + 16 +
sizeof(uint32_t));
132 ciphertext +=
sizeof(uint32_t);
133 const unsigned char *iv = (
const unsigned char *) ciphertext;
135 ciphertext_len -= 20;
137 CHECK(EVP_CipherInit(decrypt->
ctx, cipher, decrypt->
key_hash, iv, 0),
"Unable to initialize cipher");
138 CHECK(EVP_CIPHER_CTX_ctrl(decrypt->
ctx, EVP_CTRL_GCM_SET_IVLEN, 16, NULL),
"set IV len");
143 if (aad && aad_len > 0) {
144 if (!EVP_DecryptUpdate(decrypt->
ctx, NULL, &out_len, (
void *) aad, aad_len)) {
149 CHECK(EVP_CipherUpdate(decrypt->
ctx, (
unsigned char *) plaintext, &out_len, (
const unsigned char *) ciphertext, ciphertext_len),
"EVP_CipherUpdate");
150 int total_len = out_len;
152 CHECK(EVP_CIPHER_CTX_ctrl(decrypt->
ctx, EVP_CTRL_GCM_SET_TAG,
GCM_TAG_LEN, (
void *) (ciphertext + ciphertext_len)),
"GCM set tag");
154 CHECK(EVP_CipherFinal(decrypt->
ctx, (
unsigned char *) plaintext + out_len, &out_len),
"EVP_CipherFinal");
155 total_len += out_len;
158 uint32_t expected_crc = 0;
159 assert((
size_t) total_len >= data_len +
sizeof expected_crc);
160 memcpy(&expected_crc, plaintext + data_len,
sizeof expected_crc);
161 uint32_t crc =
crc32buf(aad, aad_len);
163 if (crc != expected_crc) {
uint32_t crc32buf(const char *buf, size_t len)
uint32_t crc32buf_with_oldcrc(const char *buf, size_t len, uint32_t oldcrc)
void log_msg(int level, const char *format,...)
#define LOG_LEVEL_INFO
normal reporting
#define LOG_LEVEL_ERROR
general errors
#define LOG_LEVEL_WARNING
less severe errors
@ LIBRARY_CLASS_UNDEFINED
#define REGISTER_MODULE(name, info, lclass, abi)
Registers module to global modules' registry.
#define snprintf_ch(str,...)
shortcut for snprintf(var, sizeof var...), var must be a char array
@ STR_LEN
"standard" string length placeholder
void MD5Update(MD5CTX *context, const unsigned char *input, unsigned int inputLen)
void MD5Final(unsigned char digest[16], MD5CTX *context)
void MD5Init(MD5CTX *context)
static int openssl_decrypt(struct openssl_decrypt *decrypt, const char *ciphertext, int ciphertext_len, const char *aad, int aad_len, char *plaintext, enum openssl_mode mode)
static int openssl_decrypt_init(struct openssl_decrypt **state, const char *passphrase)
static const struct openssl_decrypt_info functions
static void openssl_decrypt_destroy(struct openssl_decrypt *s)
#define CHECK(action, errmsg)
#define OPENSSL_DECRYPT_ABI_VERSION
const EVP_CIPHER *(* get_cipher)(void)
openssl_mode
only GCM provides autenticity, other modes only check integrity (CRC)
unsigned char ecount[AES_BLOCK_SIZE]
unsigned char ivec[AES_BLOCK_SIZE]
unsigned char key_hash[16]