Support AAD in AES-GCM

This commit is contained in:
Sergey Markelov 2023-05-04 15:52:34 -07:00 committed by Nathan Moinvaziri
parent 37bc3bc7de
commit 1fdcfef544
7 changed files with 129 additions and 61 deletions

View file

@ -35,14 +35,14 @@ void* mz_crypt_sha_create(void);
void mz_crypt_sha_delete(void **handle);
void mz_crypt_aes_reset(void *handle);
int32_t mz_crypt_aes_encrypt(void *handle, uint8_t *buf, int32_t size);
int32_t mz_crypt_aes_encrypt(void *handle, const void *aad, int32_t aad_size, uint8_t *buf, int32_t size);
int32_t mz_crypt_aes_encrypt_final(void *handle, uint8_t *buf, int32_t size, uint8_t *tag, int32_t tag_size);
int32_t mz_crypt_aes_decrypt(void *handle, uint8_t *buf, int32_t size);
int32_t mz_crypt_aes_decrypt_final(void *handle, uint8_t *buf, int32_t size, uint8_t *tag, int32_t tag_size);
int32_t mz_crypt_aes_decrypt(void *handle, const void *aad, int32_t aad_size, uint8_t *buf, int32_t size);
int32_t mz_crypt_aes_decrypt_final(void *handle, uint8_t *buf, int32_t size, const uint8_t *tag, int32_t tag_size);
int32_t mz_crypt_aes_set_encrypt_key(void *handle, const void *key, int32_t key_length,
const void *iv, int32_t iv_length);
const void *iv, int32_t iv_length);
int32_t mz_crypt_aes_set_decrypt_key(void *handle, const void *key, int32_t key_length,
const void *iv, int32_t iv_length);
const void *iv, int32_t iv_length);
void mz_crypt_aes_set_mode(void *handle, int32_t mode);
void* mz_crypt_aes_create(void);
void mz_crypt_aes_delete(void **handle);

View file

@ -9,6 +9,7 @@
*/
#include "mz.h"
#include "mz_crypt.h"
#include <CoreFoundation/CoreFoundation.h>
#include <CommonCrypto/CommonCryptor.h>
@ -25,6 +26,7 @@ enum {
CCCryptorStatus CCCryptorGCMReset(CCCryptorRef cryptorRef);
CCCryptorStatus CCCryptorGCMAddIV(CCCryptorRef cryptorRef, const void *iv, size_t ivLen);
CCCryptorStatus CCCryptorGCMAddAAD(CCCryptorRef cryptorRef, const void *aData, size_t aDataLen);
CCCryptorStatus CCCryptorGCMEncrypt(CCCryptorRef cryptorRef, const void *dataIn, size_t dataInLength, void *dataOut);
CCCryptorStatus CCCryptorGCMDecrypt(CCCryptorRef cryptorRef, const void *dataIn, size_t dataInLength, void *dataOut);
CCCryptorStatus CCCryptorGCMFinal(CCCryptorRef cryptorRef, void *tagOut, size_t *tagLength);
@ -214,17 +216,25 @@ void mz_crypt_aes_reset(void *handle) {
mz_crypt_aes_free(handle);
}
int32_t mz_crypt_aes_encrypt(void *handle, uint8_t *buf, int32_t size) {
int32_t mz_crypt_aes_encrypt(void *handle, const void *aad, int32_t aad_size, uint8_t *buf, int32_t size) {
mz_crypt_aes *aes = (mz_crypt_aes *)handle;
size_t data_moved = 0;
if (!aes || !buf || size % MZ_AES_BLOCK_SIZE != 0)
if (!aes || !buf || size % MZ_AES_BLOCK_SIZE != 0 || !aes->crypt)
return MZ_PARAM_ERROR;
if (aes->mode == MZ_AES_MODE_GCM)
if (aes->mode == MZ_AES_MODE_GCM) {
if (aad && aad_size > 0) {
aes->error = CCCryptorGCMAddAAD(aes->crypt, aad, aad_size);
if (aes->error != kCCSuccess)
return MZ_CRYPT_ERROR;
}
aes->error = CCCryptorGCMEncrypt(aes->crypt, buf, size, buf);
else
} else {
if (aad && aad_size > 0)
return MZ_PARAM_ERROR;
aes->error = CCCryptorUpdate(aes->crypt, buf, size, buf, size, &data_moved);
}
if (aes->error != kCCSuccess)
return MZ_CRYPT_ERROR;
@ -250,17 +260,25 @@ int32_t mz_crypt_aes_encrypt_final(void *handle, uint8_t *buf, int32_t size, uin
return size;
}
int32_t mz_crypt_aes_decrypt(void *handle, uint8_t *buf, int32_t size) {
int32_t mz_crypt_aes_decrypt(void *handle, const void *aad, int32_t aad_size, uint8_t *buf, int32_t size) {
mz_crypt_aes *aes = (mz_crypt_aes *)handle;
size_t data_moved = 0;
if (!aes || !buf || size % MZ_AES_BLOCK_SIZE != 0)
if (!aes || !buf || size % MZ_AES_BLOCK_SIZE != 0 || !aes->crypt)
return MZ_PARAM_ERROR;
if (aes->mode == MZ_AES_MODE_GCM)
if (aes->mode == MZ_AES_MODE_GCM) {
if (aad && aad_size > 0) {
aes->error = CCCryptorGCMAddAAD(aes->crypt, aad, aad_size);
if (aes->error != kCCSuccess)
return MZ_CRYPT_ERROR;
}
aes->error = CCCryptorGCMDecrypt(aes->crypt, buf, size, buf);
else
} else {
if (aad && aad_size > 0)
return MZ_PARAM_ERROR;
aes->error = CCCryptorUpdate(aes->crypt, buf, size, buf, size, &data_moved);
}
if (aes->error != kCCSuccess)
return MZ_CRYPT_ERROR;
@ -268,7 +286,7 @@ int32_t mz_crypt_aes_decrypt(void *handle, uint8_t *buf, int32_t size) {
return size;
}
int32_t mz_crypt_aes_decrypt_final(void *handle, uint8_t *buf, int32_t size, uint8_t *tag, int32_t tag_length) {
int32_t mz_crypt_aes_decrypt_final(void *handle, uint8_t *buf, int32_t size, const uint8_t *tag, int32_t tag_length) {
mz_crypt_aes *aes = (mz_crypt_aes *)handle;
uint8_t tag_actual_buf[MZ_AES_BLOCK_SIZE];
size_t tag_actual_len = sizeof(tag_actual_buf);

View file

@ -9,6 +9,7 @@
*/
#include "mz.h"
#include "mz_crypt.h"
#include <openssl/err.h>
#include <openssl/engine.h>
@ -287,13 +288,21 @@ void mz_crypt_aes_reset(void *handle) {
mz_crypt_aes_free(handle);
}
int32_t mz_crypt_aes_encrypt(void *handle, uint8_t *buf, int32_t size) {
int32_t mz_crypt_aes_encrypt(void *handle, const void *aad, int32_t aad_size, uint8_t *buf, int32_t size) {
mz_crypt_aes *aes = (mz_crypt_aes *)handle;
if (!aes || !buf)
return MZ_PARAM_ERROR;
if (aes->mode != MZ_AES_MODE_GCM && size % MZ_AES_BLOCK_SIZE != 0)
if (aes->mode != MZ_AES_MODE_GCM && aad && aad_size > 0)
return MZ_PARAM_ERROR;
if (aes->mode != MZ_AES_MODE_GCM && size % MZ_AES_BLOCK_SIZE != 0 || !aes->ctx)
return MZ_PARAM_ERROR;
if (aad && aad_size > 0) {
int32_t how_many = 0;
if (!EVP_EncryptUpdate(aes->ctx, NULL, &how_many, aad, aad_size))
return MZ_CRYPT_ERROR;
}
if (!EVP_EncryptUpdate(aes->ctx, buf, &size, buf, size))
return MZ_CRYPT_ERROR;
@ -306,7 +315,7 @@ int32_t mz_crypt_aes_encrypt_final(void *handle, uint8_t *buf, int32_t size, uin
int result = 0;
int out_len = 0;
if (!aes || !tag || !tag_size || aes->mode != MZ_AES_MODE_GCM)
if (!aes || !tag || !tag_size || aes->mode != MZ_AES_MODE_GCM || !aes->ctx)
return MZ_PARAM_ERROR;
if (buf && size) {
@ -328,11 +337,19 @@ int32_t mz_crypt_aes_encrypt_final(void *handle, uint8_t *buf, int32_t size, uin
return size;
}
int32_t mz_crypt_aes_decrypt(void *handle, uint8_t *buf, int32_t size) {
int32_t mz_crypt_aes_decrypt(void *handle, const void *aad, int32_t aad_size, uint8_t *buf, int32_t size) {
mz_crypt_aes *aes = (mz_crypt_aes *)handle;
if (!aes || !buf || size % MZ_AES_BLOCK_SIZE != 0)
if (aes->mode != MZ_AES_MODE_GCM && aad && aad_size > 0)
return MZ_PARAM_ERROR;
if (!aes || !buf || size % MZ_AES_BLOCK_SIZE != 0 || !aes->ctx)
return MZ_PARAM_ERROR;
if (aad && aad_size > 0) {
int32_t how_many = 0;
if (!EVP_DecryptUpdate(aes->ctx, NULL, &how_many, aad, aad_size))
return MZ_CRYPT_ERROR;
}
if (!EVP_DecryptUpdate(aes->ctx, buf, &size, buf, size))
return MZ_CRYPT_ERROR;
@ -340,11 +357,11 @@ int32_t mz_crypt_aes_decrypt(void *handle, uint8_t *buf, int32_t size) {
return size;
}
int32_t mz_crypt_aes_decrypt_final(void *handle, uint8_t *buf, int32_t size, uint8_t *tag, int32_t tag_length) {
int32_t mz_crypt_aes_decrypt_final(void *handle, uint8_t *buf, int32_t size, const uint8_t *tag, int32_t tag_length) {
mz_crypt_aes *aes = (mz_crypt_aes *)handle;
int out_len = 0;
if (!aes || !tag || !tag_length || aes->mode != MZ_AES_MODE_GCM)
if (!aes || !tag || !tag_length || aes->mode != MZ_AES_MODE_GCM || !aes->ctx)
return MZ_PARAM_ERROR;
if (buf && size) {
@ -353,7 +370,7 @@ int32_t mz_crypt_aes_decrypt_final(void *handle, uint8_t *buf, int32_t size, uin
}
/* Set expected tag */
if (!EVP_CIPHER_CTX_ctrl(aes->ctx, EVP_CTRL_GCM_SET_TAG, tag_length, tag)) {
if (!EVP_CIPHER_CTX_ctrl(aes->ctx, EVP_CTRL_GCM_SET_TAG, tag_length, (void *)tag)) {
aes->error = ERR_get_error();
return MZ_CRYPT_ERROR;
}
@ -432,7 +449,7 @@ int32_t mz_crypt_aes_set_encrypt_key(void *handle, const void *key, int32_t key_
}
int32_t mz_crypt_aes_set_decrypt_key(void *handle, const void *key, int32_t key_length,
const uint8_t *iv, int32_t iv_length) {
const void *iv, int32_t iv_length) {
mz_crypt_aes *aes = (mz_crypt_aes *)handle;
if (!aes || !key || !key_length)

View file

@ -240,19 +240,31 @@ void mz_crypt_aes_reset(void *handle) {
mz_crypt_aes_free(handle);
}
int32_t mz_crypt_aes_encrypt(void *handle, uint8_t *buf, int32_t size) {
int32_t mz_crypt_aes_encrypt(void *handle, const void *aad, int32_t aad_size, uint8_t *buf, int32_t size) {
mz_crypt_aes *aes = (mz_crypt_aes *)handle;
ULONG output_size = 0;
NTSTATUS status = 0;
if (!aes || !buf || size % MZ_AES_BLOCK_SIZE != 0)
return MZ_PARAM_ERROR;
if (aes->mode != MZ_AES_MODE_GCM && aad && aad_size > 0)
return MZ_PARAM_ERROR;
if (aes->mode == MZ_AES_MODE_GCM && !aes->auth_info)
return MZ_PARAM_ERROR;
if (aad && aes->auth_info && !(aes->auth_info->dwFlags & BCRYPT_AUTH_MODE_IN_PROGRESS_FLAG)) {
aes->auth_info->pbAuthData = (uint8_t*)aad;
aes->auth_info->cbAuthData = aad_size;
}
status = BCryptEncrypt(aes->key, buf, size, aes->auth_info, aes->iv, aes->iv_length, buf, size,
&output_size, 0);
if (aad && aes->auth_info) {
aes->auth_info->pbAuthData = NULL;
aes->auth_info->cbAuthData = 0;
}
if (!NT_SUCCESS(status)) {
aes->error = status;
return MZ_CRYPT_ERROR;
@ -284,19 +296,31 @@ int32_t mz_crypt_aes_encrypt_final(void *handle, uint8_t *buf, int32_t size, uin
return size;
}
int32_t mz_crypt_aes_decrypt(void *handle, uint8_t *buf, int32_t size) {
int32_t mz_crypt_aes_decrypt(void *handle, const void *aad, int32_t aad_size, uint8_t *buf, int32_t size) {
mz_crypt_aes *aes = (mz_crypt_aes *)handle;
ULONG output_size = 0;
NTSTATUS status = 0;
if (!aes || !buf || size % MZ_AES_BLOCK_SIZE != 0)
return MZ_PARAM_ERROR;
if (aes->mode != MZ_AES_MODE_GCM && aad && aad_size > 0)
return MZ_PARAM_ERROR;
if (aes->mode == MZ_AES_MODE_GCM && !aes->auth_info)
return MZ_PARAM_ERROR;
if (aad && aes->auth_info && !(aes->auth_info->dwFlags & BCRYPT_AUTH_MODE_IN_PROGRESS_FLAG)) {
aes->auth_info->pbAuthData = (uint8_t*)aad;
aes->auth_info->cbAuthData = aad_size;
}
status = BCryptDecrypt(aes->key, buf, size, aes->auth_info, aes->iv, aes->iv_length, buf, size,
&output_size, 0);
if (aad && aes->auth_info) {
aes->auth_info->pbAuthData = NULL;
aes->auth_info->cbAuthData = 0;
}
if (!NT_SUCCESS(status)) {
aes->error = status;
return MZ_CRYPT_ERROR;
@ -304,7 +328,7 @@ int32_t mz_crypt_aes_decrypt(void *handle, uint8_t *buf, int32_t size) {
return size;
}
int32_t mz_crypt_aes_decrypt_final(void *handle, uint8_t *buf, int32_t size, uint8_t *tag, int32_t tag_length) {
int32_t mz_crypt_aes_decrypt_final(void *handle, uint8_t *buf, int32_t size, const uint8_t *tag, int32_t tag_length) {
mz_crypt_aes *aes = (mz_crypt_aes *)handle;
NTSTATUS status = 0;
ULONG output_size = 0;
@ -312,7 +336,7 @@ int32_t mz_crypt_aes_decrypt_final(void *handle, uint8_t *buf, int32_t size, uin
if (!aes || !tag || !tag_length || aes->mode != MZ_AES_MODE_GCM || !aes->auth_info)
return MZ_PARAM_ERROR;
aes->auth_info->pbTag = tag;
aes->auth_info->pbTag = (uint8_t *)tag;
aes->auth_info->cbTag = tag_length;
aes->auth_info->dwFlags &= ~BCRYPT_AUTH_MODE_CHAIN_CALLS_FLAG;
@ -356,7 +380,10 @@ static int32_t mz_crypt_aes_set_key(void *handle, const void *key, int32_t key_l
mz_crypt_aes_reset(handle);
if (iv) {
if (iv && iv_length) {
if (aes->mode == MZ_AES_MODE_ECB)
return MZ_PARAM_ERROR;
aes->iv_length = MZ_AES_BLOCK_SIZE;
aes->iv = calloc(MZ_AES_BLOCK_SIZE, sizeof(uint8_t));
if (!aes->iv)

View file

@ -218,11 +218,11 @@ void mz_crypt_aes_reset(void *handle) {
mz_crypt_aes_free(handle);
}
int32_t mz_crypt_aes_encrypt(void *handle, uint8_t *buf, int32_t size) {
int32_t mz_crypt_aes_encrypt(void *handle, const void *aad, int32_t aad_size, uint8_t *buf, int32_t size) {
mz_crypt_aes *aes = (mz_crypt_aes *)handle;
int32_t result = 0;
if (!aes || !buf || size % MZ_AES_BLOCK_SIZE != 0)
if (!aes || !buf || size % MZ_AES_BLOCK_SIZE != 0 || (aad && aad_size > 0))
return MZ_PARAM_ERROR;
result = CryptEncrypt(aes->key, 0, 0, 0, buf, (DWORD *)&size, size);
if (!result) {
@ -236,10 +236,10 @@ int32_t mz_crypt_aes_encrypt_final(void *handle, uint8_t *buf, int32_t size, uin
return MZ_SUPPORT_ERROR;
}
int32_t mz_crypt_aes_decrypt(void *handle, uint8_t *buf, int32_t size) {
int32_t mz_crypt_aes_decrypt(void *handle, const void *aad, int32_t aad_size, uint8_t *buf, int32_t size) {
mz_crypt_aes *aes = (mz_crypt_aes *)handle;
int32_t result = 0;
if (!aes || !buf || size % MZ_AES_BLOCK_SIZE != 0)
if (!aes || !buf || size % MZ_AES_BLOCK_SIZE != 0 || (aad && aad_size > 0))
return MZ_PARAM_ERROR;
result = CryptDecrypt(aes->key, 0, 0, 0, buf, (DWORD *)&size);
if (!result) {
@ -249,7 +249,7 @@ int32_t mz_crypt_aes_decrypt(void *handle, uint8_t *buf, int32_t size) {
return size;
}
int32_t mz_crypt_aes_decrypt_final(void *handle, uint8_t *buf, int32_t size, uint8_t *tag, int32_t tag_length) {
int32_t mz_crypt_aes_decrypt_final(void *handle, uint8_t *buf, int32_t size, const uint8_t *tag, int32_t tag_length) {
return MZ_SUPPORT_ERROR;
}
@ -271,7 +271,7 @@ static int32_t mz_crypt_aes_set_key(void *handle, const void *key, int32_t key_l
if (!aes || !key || !key_length)
return MZ_PARAM_ERROR;
if (iv && iv_length != MZ_AES_BLOCK_SIZE)
if (iv && iv_length < MZ_AES_BLOCK_SIZE)
return MZ_PARAM_ERROR;
mz_crypt_aes_reset(handle);
@ -328,6 +328,9 @@ static int32_t mz_crypt_aes_set_key(void *handle, const void *key, int32_t key_l
}
if (result && err == MZ_OK && iv) {
if (aes->mode == MZ_AES_MODE_ECB)
return MZ_PARAM_ERROR;
result = CryptSetKeyParam(aes->key, KP_IV, iv, 0);
if (!result) {

View file

@ -174,7 +174,7 @@ static int32_t mz_stream_wzaes_ctr_encrypt(void *stream, uint8_t *buf, int32_t s
/* Encrypt the nonce using ECB mode to form next xor buffer */
memcpy(wzaes->crypt_block, wzaes->nonce, MZ_AES_BLOCK_SIZE);
mz_crypt_aes_encrypt(wzaes->aes, wzaes->crypt_block, sizeof(wzaes->crypt_block));
mz_crypt_aes_encrypt(wzaes->aes, NULL, 0, wzaes->crypt_block, sizeof(wzaes->crypt_block));
pos = 0;
}

View file

@ -48,7 +48,7 @@ TEST(crypt, rand) {
}
TEST(crypt, sha1) {
void *sha1 = NULL;
void *sha1 = nullptr;
uint8_t hash1[MZ_HASH_SHA1_SIZE];
char computed_hash[256];
@ -71,7 +71,7 @@ TEST(crypt, sha224) {
#if GTEST_OS_WINDOWS
GTEST_SKIP() << "SHA224 not supported on Windows";
#else
void *sha224 = NULL;
void *sha224 = nullptr;
uint8_t hash224[MZ_HASH_SHA224_SIZE];
char computed_hash[256];
@ -95,7 +95,7 @@ TEST(crypt, sha256) {
#if GTEST_OS_WINDOWS && _WIN32_WINNT <= _WIN32_WINNT_XP
GTEST_SKIP() << "SHA256 not supported on Windows XP";
#else
void *sha256 = NULL;
void *sha256 = nullptr;
uint8_t hash256[MZ_HASH_SHA256_SIZE];
char computed_hash[256];
@ -119,7 +119,7 @@ TEST(crypt, sha384) {
#if GTEST_OS_WINDOWS && _WIN32_WINNT <= _WIN32_WINNT_XP
GTEST_SKIP() << "SHA384 not supported on Windows XP";
#else
void *sha384 = NULL;
void *sha384 = nullptr;
uint8_t hash384[MZ_HASH_SHA384_SIZE];
char computed_hash[256];
@ -143,7 +143,7 @@ TEST(crypt, sha512) {
#if GTEST_OS_WINDOWS && _WIN32_WINNT <= _WIN32_WINNT_XP
GTEST_SKIP() << "SHA512 not supported on Windows XP";
#else
void *sha512 = NULL;
void *sha512 = nullptr;
uint8_t hash512[MZ_HASH_SHA512_SIZE];
char computed_hash[256];
@ -164,7 +164,7 @@ TEST(crypt, sha512) {
}
TEST(crypt, aes128) {
void *aes = NULL;
void *aes = nullptr;
const char *key = "awesomekeythisis";
const char *test = "youknowitsogrowi";
int32_t key_length = 0;
@ -178,23 +178,23 @@ TEST(crypt, aes128) {
aes = mz_crypt_aes_create();
ASSERT_NE(aes, nullptr);
mz_crypt_aes_set_encrypt_key(aes, key, key_length, NULL, 0);
EXPECT_EQ(mz_crypt_aes_encrypt(aes, buf, test_length), test_length);
mz_crypt_aes_set_encrypt_key(aes, key, key_length, nullptr, 0);
EXPECT_EQ(mz_crypt_aes_encrypt(aes, nullptr, 0, buf, test_length), test_length);
mz_crypt_aes_delete(&aes);
EXPECT_STRNE((char *)buf, test);
aes = mz_crypt_aes_create();
ASSERT_NE(aes, nullptr);
mz_crypt_aes_set_decrypt_key(aes, key, key_length, NULL, 0);
EXPECT_EQ(mz_crypt_aes_decrypt(aes, buf, test_length), test_length);
mz_crypt_aes_set_decrypt_key(aes, key, key_length, nullptr, 0);
EXPECT_EQ(mz_crypt_aes_decrypt(aes, nullptr, 0, buf, test_length), test_length);
mz_crypt_aes_delete(&aes);
EXPECT_STREQ((char *)buf, test);
}
TEST(crypt, aes128_cbc) {
void *aes = NULL;
void *aes = nullptr;
const char *key = "awesomekeythisis";
const char *test = "youknowitsogrowi";
const char *iv = "0123456789123456";
@ -213,7 +213,7 @@ TEST(crypt, aes128_cbc) {
ASSERT_NE(aes, nullptr);
mz_crypt_aes_set_mode(aes, MZ_AES_MODE_CBC);
EXPECT_EQ(mz_crypt_aes_set_encrypt_key(aes, key, key_length, iv, iv_length), MZ_OK);
EXPECT_EQ(mz_crypt_aes_encrypt(aes, buf, test_length), test_length);
EXPECT_EQ(mz_crypt_aes_encrypt(aes, nullptr, 0, buf, test_length), test_length);
mz_crypt_aes_delete(&aes);
EXPECT_STRNE((char *)buf, test);
@ -222,7 +222,7 @@ TEST(crypt, aes128_cbc) {
ASSERT_NE(aes, nullptr);
mz_crypt_aes_set_mode(aes, MZ_AES_MODE_CBC);
EXPECT_EQ(mz_crypt_aes_set_decrypt_key(aes, key, key_length, iv, iv_length), MZ_OK);
EXPECT_EQ(mz_crypt_aes_decrypt(aes, buf, test_length), test_length);
EXPECT_EQ(mz_crypt_aes_decrypt(aes, nullptr, 0, buf, test_length), test_length);
mz_crypt_aes_delete(&aes);
EXPECT_STREQ((char *)buf, test);
@ -233,19 +233,22 @@ TEST(crypt, aes128_gcm) {
#if GTEST_OS_WINDOWS && _WIN32_WINNT <= _WIN32_WINNT_XP
GTEST_SKIP() << "SHA256 not supported on Windows XP";
#else
void* aes = NULL;
void* aes = nullptr;
const char* key = "awesomekeythisis";
const char* test = "youknowitsogrowi";
const char* iv = "0123456789123456";
const char *aad = "additional authentication data";
int32_t key_length = 0;
int32_t test_length = 0;
int32_t iv_length = 0;
int32_t aad_length = 0;
uint8_t buf[120];
uint8_t tag[MZ_AES_BLOCK_SIZE] = {0};
key_length = (int32_t)strlen(key);
test_length = (int32_t)strlen(test);
iv_length = (int32_t)strlen(iv);
aad_length = (int32_t)strlen(aad);
strncpy((char*)buf, test, sizeof(buf));
strncpy((char*)buf + test_length, test, sizeof(buf) - test_length);
@ -254,7 +257,7 @@ TEST(crypt, aes128_gcm) {
ASSERT_NE(aes, nullptr);
mz_crypt_aes_set_mode(aes, MZ_AES_MODE_GCM);
EXPECT_EQ(mz_crypt_aes_set_encrypt_key(aes, key, key_length, iv, iv_length), MZ_OK);
EXPECT_EQ(mz_crypt_aes_encrypt(aes, buf, test_length), test_length);
EXPECT_EQ(mz_crypt_aes_encrypt(aes, aad, aad_length, buf, test_length), test_length);
EXPECT_EQ(mz_crypt_aes_encrypt_final(aes, buf + test_length, test_length - 1, tag, sizeof(tag)), test_length - 1);
mz_crypt_aes_delete(&aes);
@ -264,7 +267,7 @@ TEST(crypt, aes128_gcm) {
ASSERT_NE(aes, nullptr);
mz_crypt_aes_set_mode(aes, MZ_AES_MODE_GCM);
EXPECT_EQ(mz_crypt_aes_set_decrypt_key(aes, key, key_length, iv, iv_length), MZ_OK);
EXPECT_EQ(mz_crypt_aes_decrypt(aes, buf, test_length), test_length);
EXPECT_EQ(mz_crypt_aes_decrypt(aes, aad, aad_length, buf, test_length), test_length);
EXPECT_EQ(mz_crypt_aes_decrypt_final(aes, buf + test_length, test_length - 1, tag, sizeof(tag)), test_length - 1);
mz_crypt_aes_delete(&aes);
@ -274,7 +277,7 @@ TEST(crypt, aes128_gcm) {
}
TEST(crypt, aes194) {
void *aes = NULL;
void *aes = nullptr;
const char *key = "awesomekeythisisbeefyone";
const char *test = "youknowitsogrowi";
int32_t key_length = 0;
@ -288,23 +291,23 @@ TEST(crypt, aes194) {
aes = mz_crypt_aes_create();
ASSERT_NE(aes, nullptr);
EXPECT_EQ(mz_crypt_aes_set_encrypt_key(aes, key, key_length, NULL, 0), MZ_OK);
EXPECT_EQ(mz_crypt_aes_encrypt(aes, buf, test_length), test_length);
EXPECT_EQ(mz_crypt_aes_set_encrypt_key(aes, key, key_length, nullptr, 0), MZ_OK);
EXPECT_EQ(mz_crypt_aes_encrypt(aes, nullptr, 0, buf, test_length), test_length);
mz_crypt_aes_delete(&aes);
EXPECT_STRNE((char *)buf, test);
aes = mz_crypt_aes_create();
ASSERT_NE(aes, nullptr);
EXPECT_EQ(mz_crypt_aes_set_decrypt_key(aes, key, key_length, NULL, 0), MZ_OK);
EXPECT_EQ(mz_crypt_aes_decrypt(aes, buf, test_length), test_length);
EXPECT_EQ(mz_crypt_aes_set_decrypt_key(aes, key, key_length, nullptr, 0), MZ_OK);
EXPECT_EQ(mz_crypt_aes_decrypt(aes, nullptr, 0, buf, test_length), test_length);
mz_crypt_aes_delete(&aes);
EXPECT_STREQ((char *)buf, test);
}
TEST(crypt, aes256) {
void *aes = NULL;
void *aes = nullptr;
const char *key = "awesomekeythisisevenmoresolidone";
const char *test = "youknowitsogrowi";
int32_t key_length = 0;
@ -318,16 +321,16 @@ TEST(crypt, aes256) {
aes = mz_crypt_aes_create();
ASSERT_NE(aes, nullptr);
EXPECT_EQ(mz_crypt_aes_set_encrypt_key(aes, key, key_length, NULL, 0), MZ_OK);
EXPECT_EQ(mz_crypt_aes_encrypt(aes, buf, test_length), test_length);
EXPECT_EQ(mz_crypt_aes_set_encrypt_key(aes, key, key_length, nullptr, 0), MZ_OK);
EXPECT_EQ(mz_crypt_aes_encrypt(aes, nullptr, 0, buf, test_length), test_length);
mz_crypt_aes_delete(&aes);
EXPECT_STRNE((char *)buf, test);
aes = mz_crypt_aes_create();
ASSERT_NE(aes, nullptr);
EXPECT_EQ(mz_crypt_aes_set_decrypt_key(aes, key, key_length, NULL, 0), MZ_OK);
EXPECT_EQ(mz_crypt_aes_decrypt(aes, buf, test_length), test_length);
EXPECT_EQ(mz_crypt_aes_set_decrypt_key(aes, key, key_length, nullptr, 0), MZ_OK);
EXPECT_EQ(mz_crypt_aes_decrypt(aes, nullptr, 0, buf, test_length), test_length);
mz_crypt_aes_delete(&aes);
EXPECT_STREQ((char *)buf, test);