64#include <wolfssl/options.h>
65#include <wolfssl/openssl/aes.h>
66#include <wolfssl/openssl/err.h>
67#include <wolfssl/openssl/evp.h>
68#include <wolfssl/openssl/rand.h>
70#include <openssl/aes.h>
71#include <openssl/err.h>
72#include <openssl/evp.h>
73#include <openssl/rand.h>
84#define DEFAULT_CIPHER_MODE MODE_AES128_GCM
86#define MOD_NAME "[encrypt] "
98 const EVP_CIPHER *(*get_cipher)(void);
100#if defined HAVE_EVP_AES_128_CFB128
103#if defined HAVE_EVP_AES_128_CTR
106#if defined HAVE_EVP_AES_128_ECB
115 for (
unsigned i = 0; i <
sizeof ciphers /
sizeof ciphers[0]; ++i) {
117 return ciphers[i].get_cipher();
129 for (
unsigned i = 0; i <
sizeof ciphers /
sizeof ciphers[0]; ++i) {
137 char *tok = strstr(str,
":cipher=");
139 char *cipher = strchr(tok,
'=') + 1;
141 for (
unsigned i = 0; i <
sizeof ciphers /
sizeof ciphers[0]; ++i) {
146 MSG(ERROR,
"Unknown/unsuppoted/not compiled cipher %s!\n", cipher);
158 if (strcmp(passphrase,
"help") == 0) {
166 if (strstr(passphrase,
":cipher=")) {
173 MD5Update(&context, (
const unsigned char *) pass,
184 s->
ctx = EVP_CIPHER_CTX_new();
194 EVP_CIPHER_CTX_free(s->
ctx);
198#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)
201 char *plaintext,
int data_len,
char *aad,
int aad_len,
char *ciphertext)
203 memcpy(ciphertext, &data_len,
sizeof(uint32_t));
204 int total_len =
sizeof(uint32_t);
206 unsigned char ivec[16];
207 if (RAND_bytes(ivec, 8) < 0) {
211 memcpy(ciphertext + total_len, ivec,
sizeof ivec);
212 total_len +=
sizeof ivec;
214 CHECK(EVP_CipherInit(encryption->
ctx, encryption->
cipher, encryption->
key_hash, ivec, 1),
"Cannot initialize cipher");
216 CHECK(EVP_CIPHER_CTX_ctrl(encryption->
ctx, EVP_CTRL_GCM_SET_IVLEN,
sizeof ivec, NULL),
"set IV len");
220 EVP_EncryptUpdate(encryption->
ctx, NULL, &out_len, (
unsigned char *) aad, aad_len);
223 CHECK(EVP_CipherUpdate(encryption->
ctx, (
unsigned char *) ciphertext + total_len, &out_len, (
unsigned char *) plaintext, data_len),
"EVP_CipherUpdate");
224 total_len += out_len;
226 uint32_t crc =
crc32buf(aad, aad_len);
228 CHECK(EVP_CipherUpdate(encryption->
ctx, (
unsigned char *) ciphertext + total_len, &out_len, (
unsigned char *) &crc,
sizeof crc),
"EVP_CipherUpdate CRC");
229 total_len += out_len;
231 CHECK(EVP_CipherFinal(encryption->
ctx, (
unsigned char *) ciphertext + total_len, &out_len),
"EVP_CipherFinal");
232 total_len += out_len;
234 CHECK(EVP_CIPHER_CTX_ctrl(encryption->
ctx, EVP_CTRL_GCM_GET_TAG,
GCM_TAG_LEN, ciphertext + total_len),
"GCM get tag");
243 return sizeof(uint32_t) +
251 return encryption->
mode;
int color_printf(const char *format,...)
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
@ 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)
const struct @17 ciphers[]
static enum openssl_mode get_cipher_from_str(char *str)
static int openssl_encrypt_init(struct openssl_encrypt **state, const char *passphrase)
#define DEFAULT_CIPHER_MODE
static enum openssl_mode openssl_get_cipher(struct openssl_encrypt *encryption)
static int openssl_encrypt(struct openssl_encrypt *encryption, char *plaintext, int data_len, char *aad, int aad_len, char *ciphertext)
static int openssl_get_overhead(struct openssl_encrypt *s)
static void openssl_encrypt_destroy(struct openssl_encrypt *s)
static const struct openssl_encrypt_info functions
#define CHECK(action, errmsg)
const EVP_CIPHER *(* get_cipher)(void)
#define OPENSSL_ENCRYPT_ABI_VERSION
openssl_mode
only GCM provides autenticity, other modes only check integrity (CRC)
const EVP_CIPHER * cipher
unsigned char key_hash[16]