From a651b08afa5ca76ed87cf9215bc61639824a1c8a Mon Sep 17 00:00:00 2001 From: Jacob Barthelmeh Date: Fri, 16 Feb 2018 15:08:31 -0700 Subject: [PATCH] add wolfSSL_AES_ecb_encrypt function --- src/ssl.c | 39 +++++++++++++++++++++++++++++++ tests/api.c | 54 +++++++++++++++++++++++++++++++++++++++++++ wolfssl/openssl/aes.h | 4 ++++ 3 files changed, 97 insertions(+) diff --git a/src/ssl.c b/src/ssl.c index fb4bb034f..1b4d8401d 100644 --- a/src/ssl.c +++ b/src/ssl.c @@ -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. * diff --git a/tests/api.c b/tests/api.c index a217bc234..58e239ab9 100644 --- a/tests/api.c +++ b/tests/api.c @@ -262,6 +262,9 @@ #include #include #include +#ifndef NO_AES + #include +#endif #ifndef NO_DES3 #include #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(); diff --git a/wolfssl/openssl/aes.h b/wolfssl/openssl/aes.h index ad92dc186..58d235f8c 100644 --- a/wolfssl/openssl/aes.h +++ b/wolfssl/openssl/aes.h @@ -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