add wolfSSL_AES_ecb_encrypt function

This commit is contained in:
Jacob Barthelmeh
2018-02-16 15:08:31 -07:00
parent 223edab6d9
commit a651b08afa
3 changed files with 97 additions and 0 deletions

View File

@ -20764,6 +20764,45 @@ int wolfSSL_AES_set_decrypt_key(const unsigned char *key, const int bits,
}
#ifdef HAVE_AES_ECB
/* Encrypt/decrypt a 16 byte block of data using the key passed in.
*
* in buffer to encrypt/decyrpt
* out buffer to hold result of encryption/decryption
* key AES structure to use with encryption/decryption
* enc AES_ENCRPT for encryption and AES_DECRYPT for decryption
*/
void wolfSSL_AES_ecb_encrypt(const unsigned char *in, unsigned char* out,
AES_KEY *key, const int enc)
{
Aes* aes;
WOLFSSL_ENTER("wolfSSL_AES_ecb_encrypt");
if (key == NULL || in == NULL || out == NULL) {
WOLFSSL_MSG("Error, Null argument passed in");
return;
}
aes = (Aes*)key;
if (enc == AES_ENCRYPT) {
if (wc_AesEcbEncrypt(aes, out, in, AES_BLOCK_SIZE) != 0) {
WOLFSSL_MSG("Error with AES CBC encrypt");
}
}
else {
#ifdef HAVE_AES_DECRYPT
if (wc_AesEcbDecrypt(aes, out, in, AES_BLOCK_SIZE) != 0) {
WOLFSSL_MSG("Error with AES CBC decrypt");
}
#else
WOLFSSL_MSG("AES decryption not compiled in");
#endif
}
}
#endif /* HAVE_AES_ECB */
/* Encrypt data using key and iv passed in. iv gets updated to most recent iv
* state after encryptiond/decryption.
*

View File

@ -262,6 +262,9 @@
#include <wolfssl/openssl/crypto.h>
#include <wolfssl/openssl/hmac.h>
#include <wolfssl/openssl/objects.h>
#ifndef NO_AES
#include <wolfssl/openssl/aes.h>
#endif
#ifndef NO_DES3
#include <wolfssl/openssl/des.h>
#endif
@ -16472,6 +16475,56 @@ static void test_wolfSSL_DH_1536_prime(void)
#endif
}
static void test_wolfSSL_AES_ecb_encrypt(void)
{
#if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AES_ECB)
AES_KEY aes;
const byte msg[] =
{
0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
};
const byte verify[] =
{
0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
};
const byte key[] =
{
0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
};
byte out[AES_BLOCK_SIZE];
printf(testingFmt, "wolfSSL_AES_ecb_encrypt()");
AssertIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aes), 0);
XMEMSET(out, 0, AES_BLOCK_SIZE);
AES_ecb_encrypt(msg, out, &aes, AES_ENCRYPT);
AssertIntEQ(XMEMCMP(out, verify, AES_BLOCK_SIZE), 0);
#ifdef HAVE_AES_DECRYPT
AssertIntEQ(AES_set_decrypt_key(key, sizeof(key)*8, &aes), 0);
XMEMSET(out, 0, AES_BLOCK_SIZE);
AES_ecb_encrypt(verify, out, &aes, AES_DECRYPT);
AssertIntEQ(XMEMCMP(out, msg, AES_BLOCK_SIZE), 0);
#endif
/* test bad arguments */
AES_ecb_encrypt(NULL, out, &aes, AES_DECRYPT);
AES_ecb_encrypt(verify, NULL, &aes, AES_DECRYPT);
AES_ecb_encrypt(verify, out, NULL, AES_DECRYPT);
printf(resultFmt, passed);
#endif
}
static void test_no_op_functions(void)
{
#if defined(OPENSSL_EXTRA)
@ -17299,6 +17352,7 @@ void ApiTest(void)
test_wolfSSL_msg_callback();
test_wolfSSL_SHA();
test_wolfSSL_DH_1536_prime();
test_wolfSSL_AES_ecb_encrypt();
/* test the no op functions for compatibility */
test_no_op_functions();

View File

@ -58,11 +58,15 @@ WOLFSSL_API int wolfSSL_AES_set_decrypt_key
WOLFSSL_API void wolfSSL_AES_cbc_encrypt
(const unsigned char *in, unsigned char* out, size_t len,
AES_KEY *key, unsigned char* iv, const int enc);
WOLFSSL_API void wolfSSL_AES_ecb_encrypt
(const unsigned char *in, unsigned char* out,
AES_KEY *key, const int enc);
WOLFSSL_API void wolfSSL_AES_cfb128_encrypt
(const unsigned char *in, unsigned char* out, size_t len,
AES_KEY *key, unsigned char* iv, int* num, const int enc);
#define AES_cbc_encrypt wolfSSL_AES_cbc_encrypt
#define AES_ecb_encrypt wolfSSL_AES_ecb_encrypt
#define AES_cfb128_encrypt wolfSSL_AES_cfb128_encrypt
#define AES_set_encrypt_key wolfSSL_AES_set_encrypt_key
#define AES_set_decrypt_key wolfSSL_AES_set_decrypt_key