1

(4 replies, posted in wolfCrypt)

I am using RSA-2048 in OAEP mode with SHA-1.

In this mode, I can not encypt data larger than 214 bytes (256 bytes - 42). If I encrypt data chunks with 213 bytes or less using wolfSSL, the results can be decrypted as expected. However when I encrypt data chunks or 214 bytes, decryption with wolfSSL fails. I attached a test code which proves this behavior.

I think there is a problem with wolfSSL because I can encrypt chunks of 214 bytes and then decrypt the results with no problems when I use OpenSSL.

PS: inspecting the execution during decryption I noticed wc_RsaUnPad_OAEP returns BAD_PADDING_E. I don't understand exactly why, but this happens because 'ret' becomes non-zero in line 679 of rsa.c (I'm using wolfSSL 3.9.0).

Best regards.

PS: In the following code, setting IN_BUFFER_LENGTH  to RSA_OAEP_DECRYPTED_DATA_LENGTH causes an error, while setting it to RSA_OAEP_DECRYPTED_DATA_LENGTH-1 does not.

#include <stdio.h>
#include <wolfssl/wolfcrypt/rsa.h>
#include <wolfssl/wolfcrypt/error-crypt.h>

#define BITS_TO_BYTES(b)                (b/8)
#define MIN_OAEP_PADDING                (2*BITS_TO_BYTES(160)+2)

#define RSA_LENGTH                      (BITS_TO_BYTES(2048))

#define RSA_OAEP_DECRYPTED_DATA_LENGTH  (RSA_LENGTH-MIN_OAEP_PADDING)
#define RSA_OAEP_ENCRYPTED_DATA_LENGTH  (RSA_LENGTH)

#define IN_BUFFER_LENGTH                (RSA_OAEP_DECRYPTED_DATA_LENGTH)
#define PRIVATE_KEY_LENGTH              (1190)
#define PUBLIC_KEY_LENGTH               (294)

extern unsigned char private_key[PRIVATE_KEY_LENGTH];
extern unsigned char public_key[PUBLIC_KEY_LENGTH];

unsigned char in_buffer[IN_BUFFER_LENGTH];
unsigned char encrypted_buffer[RSA_LENGTH];
unsigned char decrypted_buffer[RSA_LENGTH];

void test_wolfcryprt_rsa();

void main()
{
    test_wolfcryprt_rsa();
    getchar();
}

// #############################################################################
//
//         TEST CODE
//
// #############################################################################

void test_wolfcryprt_rsa()
{
    RsaKey key;
    RNG rng;
    word32 index;
    int ret;

    int encrypted_len;
    int decrypted_len;

    // encrypt data.
    index = 0;
    ret = wc_InitRng(&rng);
    if (ret != 0) { printf("Error at wc_InitRng: %i.", ret); return; }
    ret = wc_InitRsaKey(&key, NULL);
    if (ret != 0) { printf("Error at wc_InitRsaKey: %i.", ret); return; }
    ret = wc_RsaPublicKeyDecode((const byte*)public_key, &index, &key, PUBLIC_KEY_LENGTH);
    if (ret != 0) { printf("Error at wc_RsaPublicKeyDecode: %i.", ret); return; }
    ret = wc_RsaPublicEncrypt_ex((const byte *)in_buffer, IN_BUFFER_LENGTH, (byte*)encrypted_buffer, RSA_LENGTH, &key, &rng, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, NULL, 0);
    if (ret < 0) { printf("Error at wc_RsaPublicEncrypt_ex: %i.", ret); return; }
    encrypted_len = ret;
    wc_FreeRng(&rng);
    wc_FreeRsaKey(&key);

    // decrypt data.
    index = 0;
    ret = wc_InitRsaKey(&key, NULL);
    if (ret != 0) { printf("Error at wc_InitRsaKey: %i.", ret); return; }
    ret = wc_RsaPrivateKeyDecode((const byte*)private_key, &index, &key, PRIVATE_KEY_LENGTH);
    if (ret != 0) { printf("Error at wc_RsaPrivateKeyDecode: %i.", ret); return; }
    ret = wc_RsaPrivateDecrypt_ex((const byte *)encrypted_buffer, encrypted_len, (byte*)decrypted_buffer, RSA_LENGTH, &key, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, NULL, 0);
    if (ret < 0) { printf("Error at wc_RsaPrivateDecrypt_ex: %i.", ret); return; }
    decrypted_len = ret;
    wc_FreeRsaKey(&key);

    // compare data.
    if (decrypted_len != IN_BUFFER_LENGTH) { printf("Decrypted length should be %i but it is %i.", IN_BUFFER_LENGTH, decrypted_len); return; }
    for (int i = 0; i < IN_BUFFER_LENGTH; i++)
    {
        if (decrypted_buffer[i] != in_buffer[i]) { printf("Byte at index %i should be %i but it is %i.", i, 0xFF & in_buffer[i], 0xFF & decrypted_buffer[i]); return; }
    }

    // got here means no error.
    printf("All went O.K.");
}

// #############################################################################
//
//         KEYS
//
// #############################################################################

unsigned char private_key[PRIVATE_KEY_LENGTH] = {
    0x30, 0x82, 0x04, 0xa2, 0x02, 0x01, 0x00, 0x02,
    0x82, 0x01, 0x01, 0x00, 0xc8, 0x40, 0x6f, 0xc7,
    0xd0, 0xc9, 0xfb, 0xd2, 0x5c, 0xf3, 0xc5, 0xbc,
    0x77, 0x0e, 0x68, 0x5a, 0x87, 0x4a, 0xb9, 0x57,
    0x08, 0xd4, 0x6b, 0x3e, 0x9a, 0x89, 0x8a, 0x9f,
    0xdd, 0xad, 0x16, 0xa8, 0xa3, 0x82, 0x42, 0x22,
    0x5b, 0x69, 0x77, 0x28, 0xba, 0x15, 0x2b, 0xb3,
    0xf3, 0x24, 0xea, 0xe4, 0x86, 0x34, 0x73, 0xc1,
    0xe5, 0x2b, 0x0b, 0xdb, 0xcd, 0x54, 0x35, 0x55,
    0xda, 0xf1, 0xfd, 0x61, 0x3f, 0x1e, 0xe7, 0x1e,
    0xf1, 0xc0, 0x38, 0xcb, 0xfc, 0x0d, 0x9d, 0x65,
    0xba, 0x50, 0x1f, 0xb7, 0x8d, 0xb3, 0x59, 0x8c,
    0x48, 0xf2, 0x34, 0xf1, 0x46, 0x86, 0xab, 0xf2,
    0xc0, 0xdd, 0x1c, 0x6d, 0xbe, 0xf9, 0x8f, 0x79,
    0xa8, 0x6f, 0x02, 0x25, 0x07, 0xba, 0xc0, 0x91,
    0x27, 0x46, 0xfb, 0xc8, 0xe8, 0x78, 0xc0, 0xb4,
    0x91, 0xb6, 0xd6, 0x5d, 0xbe, 0x74, 0x00, 0x60,
    0x73, 0xe9, 0x0e, 0x10, 0x60, 0x48, 0x82, 0xbf,
    0x50, 0x58, 0xc4, 0x6f, 0xf0, 0x4e, 0xcf, 0x92,
    0x43, 0x36, 0x75, 0xe5, 0x79, 0xb9, 0x78, 0x81,
    0xbc, 0xb6, 0xf5, 0x74, 0xfe, 0x0f, 0x3f, 0xd4,
    0x88, 0xfb, 0xe6, 0x4f, 0xd8, 0x8a, 0x60, 0xc9,
    0x25, 0xed, 0xa1, 0xef, 0x4a, 0x57, 0x81, 0xb1,
    0xce, 0xaf, 0x3d, 0xcf, 0x2a, 0x44, 0x51, 0x76,
    0x16, 0x54, 0x3d, 0x4b, 0x77, 0x09, 0x39, 0x6c,
    0x85, 0xc5, 0x0c, 0x59, 0x49, 0x12, 0xba, 0x3f,
    0x98, 0x1f, 0x29, 0x16, 0xc6, 0xed, 0x08, 0x09,
    0xa9, 0xdc, 0xe4, 0x92, 0x70, 0x71, 0x57, 0x1c,
    0xcb, 0xf2, 0xfa, 0x03, 0x84, 0xf0, 0xd8, 0x27,
    0xbc, 0xa5, 0x0a, 0x95, 0x21, 0xbc, 0x87, 0x61,
    0x9a, 0xfd, 0x78, 0xab, 0xe3, 0x2f, 0xef, 0x16,
    0x86, 0x5b, 0xe7, 0x8e, 0x48, 0xef, 0x3c, 0xa1,
    0x6e, 0xd6, 0xe7, 0xda, 0x4c, 0x69, 0x5c, 0x4f,
    0x7a, 0x20, 0x58, 0x1d, 0x02, 0x03, 0x01, 0x00,
    0x01, 0x02, 0x82, 0x01, 0x00, 0x78, 0xd8, 0xda,
    0x1c, 0x5d, 0xd5, 0xe7, 0x10, 0x96, 0x63, 0xce,
    0x8a, 0xe3, 0xd6, 0x60, 0x07, 0x71, 0xea, 0x18,
    0x5b, 0x7b, 0xca, 0xa5, 0x45, 0xcc, 0x81, 0x00,
    0x95, 0x65, 0x73, 0xd5, 0x5e, 0xc3, 0xfe, 0x11,
    0xe7, 0x25, 0xff, 0x49, 0x97, 0xdc, 0x64, 0x76,
    0x51, 0x4c, 0x84, 0x94, 0xf4, 0x80, 0x41, 0x1b,
    0x32, 0x82, 0x18, 0x2e, 0x39, 0xe1, 0x79, 0xd6,
    0x0e, 0x0f, 0xe9, 0x45, 0x9d, 0xf0, 0x37, 0xb8,
    0x06, 0xa6, 0xa1, 0xf8, 0x24, 0xb1, 0xe1, 0x8d,
    0x81, 0x1c, 0xa4, 0xc9, 0xdf, 0x3d, 0xb6, 0x64,
    0x6e, 0x12, 0x7f, 0x88, 0x8f, 0xaa, 0x9e, 0x0f,
    0x1a, 0x9a, 0x65, 0x55, 0x88, 0xad, 0x5d, 0x71,
    0xc6, 0x5b, 0x6d, 0x52, 0x80, 0x02, 0x60, 0x23,
    0x61, 0xf5, 0xb0, 0x12, 0xb6, 0xb6, 0x04, 0x59,
    0x57, 0x1f, 0x30, 0x95, 0xc1, 0x50, 0xf4, 0x34,
    0x5e, 0x00, 0xd5, 0x3e, 0x54, 0x76, 0x5e, 0xd4,
    0x26, 0xf8, 0xa7, 0x93, 0xf8, 0xe9, 0x67, 0xcc,
    0xf9, 0x04, 0x8e, 0xcb, 0x3f, 0x5e, 0xde, 0x89,
    0xc5, 0x9b, 0x80, 0x88, 0xfc, 0xef, 0xc1, 0x30,
    0xc4, 0x69, 0xb4, 0xde, 0xfc, 0x2c, 0x29, 0x18,
    0x89, 0x8e, 0xca, 0x93, 0xfd, 0x4a, 0x2c, 0x2e,
    0x75, 0x7f, 0x61, 0xd6, 0xcb, 0xd0, 0x8a, 0xfe,
    0x79, 0xf6, 0x47, 0x47, 0x9a, 0x6d, 0xb7, 0x27,
    0xf0, 0x75, 0x9e, 0x26, 0xd3, 0xd0, 0x3e, 0x54,
    0x3c, 0x19, 0x94, 0xa7, 0x9a, 0x79, 0xb8, 0x8e,
    0x6f, 0xa6, 0x2a, 0xba, 0x84, 0x89, 0x04, 0xc3,
    0x92, 0x16, 0xd1, 0x21, 0x5b, 0x0b, 0x59, 0x00,
    0xe7, 0x98, 0x63, 0x21, 0x85, 0x36, 0x88, 0x9b,
    0x7d, 0x8f, 0x9b, 0x41, 0x20, 0x52, 0x79, 0x2d,
    0x33, 0xb6, 0x85, 0xd1, 0xf4, 0x2e, 0x86, 0x88,
    0x60, 0xa7, 0xda, 0xa1, 0x2b, 0x2f, 0x82, 0xe1,
    0x3e, 0xba, 0x49, 0x31, 0xc9, 0x02, 0x81, 0x81,
    0x00, 0xe3, 0x51, 0xb6, 0x11, 0xb7, 0x61, 0x34,
    0x60, 0x73, 0xe1, 0xa0, 0x92, 0x25, 0x96, 0x36,
    0x79, 0x89, 0xbc, 0x22, 0x28, 0xcb, 0xcd, 0x2f,
    0x51, 0x15, 0xa4, 0x44, 0xb8, 0x2f, 0xf4, 0xea,
    0x07, 0x5f, 0xf0, 0x54, 0x7e, 0x72, 0x5f, 0xe9,
    0xe1, 0xec, 0xa2, 0xc9, 0x34, 0x12, 0x30, 0xa7,
    0xe1, 0xd0, 0x63, 0xbe, 0x64, 0xcc, 0x97, 0x98,
    0xdc, 0xff, 0xbe, 0xd7, 0x24, 0xab, 0x7c, 0x27,
    0x3d, 0x4f, 0x76, 0x46, 0x10, 0xb2, 0x29, 0xc5,
    0x6e, 0xbe, 0x27, 0x40, 0xf0, 0xfe, 0x33, 0xbe,
    0x84, 0x98, 0xe0, 0x5a, 0x6c, 0x17, 0xbf, 0xa1,
    0x1d, 0x07, 0x52, 0xb0, 0x28, 0x3c, 0xa6, 0x51,
    0x39, 0xc3, 0xb7, 0xb5, 0x6b, 0xcf, 0x8a, 0xa1,
    0x99, 0x94, 0x4d, 0xe1, 0x76, 0x17, 0x09, 0x18,
    0xe8, 0x5e, 0x5f, 0xfa, 0x76, 0x18, 0x70, 0x77,
    0x6c, 0x04, 0x9c, 0x80, 0x48, 0x37, 0x7c, 0xfa,
    0x17, 0x02, 0x81, 0x81, 0x00, 0xe1, 0x84, 0x75,
    0xd3, 0xbe, 0x3b, 0xe6, 0x11, 0x71, 0xe2, 0x56,
    0xd4, 0x31, 0xb8, 0x04, 0x66, 0xc2, 0x29, 0xa2,
    0x14, 0x16, 0x81, 0xa2, 0xd7, 0x47, 0x20, 0x9a,
    0xd6, 0x2a, 0x98, 0x8e, 0x01, 0x61, 0x12, 0x41,
    0xb6, 0xd7, 0x34, 0x7a, 0xc8, 0x07, 0x34, 0xe4,
    0x2f, 0x4c, 0xb9, 0xe3, 0x72, 0xa8, 0x16, 0xed,
    0x36, 0xfb, 0x18, 0xd7, 0x87, 0xa2, 0xff, 0x6a,
    0xfe, 0xde, 0x37, 0x5d, 0x1a, 0x45, 0xb1, 0x16,
    0x0a, 0x2c, 0x35, 0xab, 0x6e, 0xc1, 0x12, 0xac,
    0x7d, 0xe3, 0x7a, 0xd9, 0xc1, 0xda, 0xaa, 0x36,
    0xdc, 0xc8, 0x03, 0x30, 0x39, 0x59, 0xe6, 0x85,
    0x4e, 0x6b, 0xd2, 0x2d, 0xbf, 0xb8, 0xb4, 0x45,
    0xb1, 0x6b, 0xf4, 0xcf, 0x41, 0x4d, 0xab, 0x5c,
    0x29, 0x81, 0x4b, 0x87, 0x57, 0xf1, 0x0a, 0x6e,
    0x2d, 0x40, 0x80, 0x31, 0xc3, 0x1b, 0xdc, 0xc0,
    0x78, 0x3a, 0x1b, 0x83, 0xeb, 0x02, 0x81, 0x80,
    0x78, 0x37, 0x87, 0x65, 0x39, 0x28, 0xf4, 0x0d,
    0x2a, 0x5b, 0xa1, 0x92, 0x88, 0xc4, 0x37, 0x0c,
    0xf1, 0x95, 0x88, 0x2f, 0x31, 0x10, 0xd3, 0x3c,
    0x3b, 0x88, 0xc3, 0x3a, 0xf1, 0x49, 0xc1, 0xd6,
    0xa2, 0x9b, 0x33, 0xe4, 0x27, 0x52, 0xa8, 0x1a,
    0xee, 0x0d, 0x6d, 0x00, 0xd7, 0xb9, 0xd9, 0x9f,
    0x27, 0x99, 0x08, 0x60, 0xc0, 0x7e, 0x4f, 0xbe,
    0x58, 0x96, 0x31, 0xab, 0x57, 0xf1, 0x71, 0xc3,
    0x0f, 0xda, 0x09, 0xd5, 0xdc, 0x93, 0x10, 0xb1,
    0xaf, 0x68, 0x8d, 0x04, 0xa6, 0x3a, 0xf1, 0x3f,
    0xa8, 0xa5, 0xc5, 0xcc, 0x32, 0x87, 0x0a, 0x8a,
    0x92, 0x8b, 0xdd, 0x53, 0x7a, 0x37, 0xae, 0xef,
    0x30, 0x9d, 0x60, 0x19, 0xa3, 0x09, 0xba, 0xca,
    0xc0, 0xce, 0xab, 0x34, 0xcb, 0x9b, 0xe9, 0x0b,
    0x42, 0x95, 0xd9, 0x9c, 0x48, 0xf2, 0x79, 0x85,
    0xab, 0xae, 0xa4, 0x7d, 0x0c, 0xb3, 0x50, 0x83,
    0x02, 0x81, 0x80, 0x35, 0xc9, 0x91, 0x0c, 0xca,
    0xaf, 0xa0, 0xa5, 0x02, 0x83, 0x98, 0x70, 0x0d,
    0xd7, 0xb4, 0xfd, 0x09, 0x4c, 0x42, 0xc3, 0x05,
    0xc7, 0x2f, 0x9e, 0xa6, 0xf1, 0x48, 0xdc, 0xd1,
    0xd6, 0x06, 0xf0, 0x9f, 0x45, 0x6a, 0x75, 0x00,
    0x89, 0x1c, 0xcb, 0xbe, 0xa4, 0x47, 0xd4, 0x5c,
    0x39, 0x6d, 0xdd, 0x37, 0xe8, 0x17, 0xf5, 0xe8,
    0x17, 0xb9, 0xb8, 0x39, 0x11, 0x30, 0x64, 0xcf,
    0x7d, 0x66, 0xf0, 0x50, 0x34, 0xf7, 0x6b, 0xbf,
    0xb5, 0xa1, 0x48, 0xce, 0x35, 0xf4, 0xfc, 0x25,
    0x98, 0x74, 0x7d, 0x7e, 0xf8, 0xe0, 0x12, 0xf2,
    0x85, 0x88, 0x27, 0xf5, 0xa0, 0x3c, 0xa5, 0x42,
    0xa4, 0x23, 0x93, 0x39, 0xab, 0x8d, 0x7f, 0xce,
    0x9e, 0xda, 0x1b, 0xda, 0x39, 0x87, 0xc6, 0xc2,
    0x76, 0xd0, 0x36, 0x12, 0x60, 0x89, 0x7c, 0xb3,
    0x88, 0x9f, 0xd5, 0xc8, 0x3c, 0x73, 0x8f, 0x79,
    0x54, 0x7c, 0xb7, 0x02, 0x81, 0x80, 0x20, 0x31,
    0x41, 0x4c, 0xa4, 0xc9, 0x99, 0xd1, 0x0c, 0x83,
    0x2b, 0x94, 0x30, 0x1c, 0x25, 0x92, 0x84, 0x2c,
    0x16, 0x0e, 0xcf, 0x2b, 0x3b, 0x7b, 0x92, 0x2b,
    0x5d, 0xae, 0x46, 0x82, 0xf1, 0x7f, 0xc1, 0x42,
    0x1b, 0x96, 0x12, 0x01, 0x1d, 0x62, 0x29, 0xe5,
    0x8d, 0x4c, 0xa8, 0xf4, 0x47, 0x02, 0x9a, 0x92,
    0x65, 0x27, 0xbd, 0x49, 0x12, 0xd2, 0xc6, 0xcc,
    0xc7, 0x2b, 0x18, 0x02, 0x90, 0x4a, 0xd6, 0x65,
    0x6f, 0x2a, 0x3c, 0x40, 0x68, 0xf5, 0x36, 0x70,
    0xd4, 0x52, 0x82, 0xae, 0xa8, 0xa2, 0x38, 0xc0,
    0x00, 0x13, 0x5f, 0x15, 0x45, 0x1a, 0x95, 0x17,
    0xc1, 0x62, 0x9e, 0xc8, 0xe3, 0xe2, 0xc4, 0xf7,
    0xbf, 0xaa, 0xef, 0xfb, 0x15, 0xde, 0xa8, 0xa9,
    0x64, 0x3e, 0x0e, 0x5a, 0xa0, 0x12, 0x7d, 0x0d,
    0x5b, 0xb1, 0xef, 0xf3, 0xaf, 0xed, 0x8f, 0x5b,
    0xd8, 0xb3, 0xbc, 0xa1, 0x35, 0xd1
};

unsigned char public_key[PUBLIC_KEY_LENGTH] = {
    0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09,
    0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
    0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00,
    0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01,
    0x00, 0xc8, 0x40, 0x6f, 0xc7, 0xd0, 0xc9, 0xfb,
    0xd2, 0x5c, 0xf3, 0xc5, 0xbc, 0x77, 0x0e, 0x68,
    0x5a, 0x87, 0x4a, 0xb9, 0x57, 0x08, 0xd4, 0x6b,
    0x3e, 0x9a, 0x89, 0x8a, 0x9f, 0xdd, 0xad, 0x16,
    0xa8, 0xa3, 0x82, 0x42, 0x22, 0x5b, 0x69, 0x77,
    0x28, 0xba, 0x15, 0x2b, 0xb3, 0xf3, 0x24, 0xea,
    0xe4, 0x86, 0x34, 0x73, 0xc1, 0xe5, 0x2b, 0x0b,
    0xdb, 0xcd, 0x54, 0x35, 0x55, 0xda, 0xf1, 0xfd,
    0x61, 0x3f, 0x1e, 0xe7, 0x1e, 0xf1, 0xc0, 0x38,
    0xcb, 0xfc, 0x0d, 0x9d, 0x65, 0xba, 0x50, 0x1f,
    0xb7, 0x8d, 0xb3, 0x59, 0x8c, 0x48, 0xf2, 0x34,
    0xf1, 0x46, 0x86, 0xab, 0xf2, 0xc0, 0xdd, 0x1c,
    0x6d, 0xbe, 0xf9, 0x8f, 0x79, 0xa8, 0x6f, 0x02,
    0x25, 0x07, 0xba, 0xc0, 0x91, 0x27, 0x46, 0xfb,
    0xc8, 0xe8, 0x78, 0xc0, 0xb4, 0x91, 0xb6, 0xd6,
    0x5d, 0xbe, 0x74, 0x00, 0x60, 0x73, 0xe9, 0x0e,
    0x10, 0x60, 0x48, 0x82, 0xbf, 0x50, 0x58, 0xc4,
    0x6f, 0xf0, 0x4e, 0xcf, 0x92, 0x43, 0x36, 0x75,
    0xe5, 0x79, 0xb9, 0x78, 0x81, 0xbc, 0xb6, 0xf5,
    0x74, 0xfe, 0x0f, 0x3f, 0xd4, 0x88, 0xfb, 0xe6,
    0x4f, 0xd8, 0x8a, 0x60, 0xc9, 0x25, 0xed, 0xa1,
    0xef, 0x4a, 0x57, 0x81, 0xb1, 0xce, 0xaf, 0x3d,
    0xcf, 0x2a, 0x44, 0x51, 0x76, 0x16, 0x54, 0x3d,
    0x4b, 0x77, 0x09, 0x39, 0x6c, 0x85, 0xc5, 0x0c,
    0x59, 0x49, 0x12, 0xba, 0x3f, 0x98, 0x1f, 0x29,
    0x16, 0xc6, 0xed, 0x08, 0x09, 0xa9, 0xdc, 0xe4,
    0x92, 0x70, 0x71, 0x57, 0x1c, 0xcb, 0xf2, 0xfa,
    0x03, 0x84, 0xf0, 0xd8, 0x27, 0xbc, 0xa5, 0x0a,
    0x95, 0x21, 0xbc, 0x87, 0x61, 0x9a, 0xfd, 0x78,
    0xab, 0xe3, 0x2f, 0xef, 0x16, 0x86, 0x5b, 0xe7,
    0x8e, 0x48, 0xef, 0x3c, 0xa1, 0x6e, 0xd6, 0xe7,
    0xda, 0x4c, 0x69, 0x5c, 0x4f, 0x7a, 0x20, 0x58,
    0x1d, 0x02, 0x03, 0x01, 0x00, 0x01
};

2

(1 replies, posted in wolfCrypt)

I am using OpenSSL in a Desktop software and WolfCrypt in its embedded counterpart. For this project I must use AES in ECB mode, even though I know ECB is not the most secure mode of operation for AES. According to http://www.yassl.com/forums/topic411-aes-ecb.html, WolfCrypt supports ECB mode, even though it is not properly documented.

I could encode and decode data in OpenSSL without a problem, but I could not do so in WolfCrypt. It seems wolfCrypt is buggy in ECB mode with 192 and 256 bits-long keys (but it seems to work with 128 bits-long keys). I noticed this behavior using the following code. This code encrypts a chunk of data, decrypts it and compares the results with the original data. If the data match, a success message is displayed. Only 128 bits-long keys seem to yield correct results.

I tested this code in VS 2013 (Windows 7) using WolfSSL 3.8.0.

Am I doing something wrong here or is WolfCrypt really buggy?

#include <stdlib.h>
#include <stdio.h>

#include <wolfssl/wolfcrypt/aes.h>
                                
#define POINTER_TO_INDEX(v, i)  ( &( ( v )[ i ] ) )
#define BITS_TO_BYTES(x)        ( ( x ) / 8 )

#define MAX_KEY_BITS            ( 256 )
#define MAX_KEY_LENGTH          BITS_TO_BYTES( MAX_KEY_BITS )
#define DATA_LENGTH             ( 768 )

byte aes_key[MAX_KEY_LENGTH];
byte aes_iv[MAX_KEY_LENGTH];

byte original_data[DATA_LENGTH];
byte encrypted_data[DATA_LENGTH];
byte decrypted_data[DATA_LENGTH];

Aes aes_encrypt;
Aes aes_decrypt;

void wait_before_exit(void)
{
    printf("\nPress 'q' to quit.\n");
    while (1)
    {
        char c = getchar();
        if (c == 'q' || c == 'Q') return;
    }
}

int main(int argc, char* argv[])
{
    int actual_key_length = 0;
    printf("Choose key length:\n ( A ) 128 bits\n ( B ) 192 bits\n ( C ) 256 bits\n");
    while (actual_key_length == 0)
    {
        char c = getchar();
        switch (c)
        {
            case 'A': case 'a':
                actual_key_length = BITS_TO_BYTES(128);
                break;
            case 'B': case 'b':
                actual_key_length = BITS_TO_BYTES(192);
                break;
            case 'C': case 'c':
                actual_key_length = BITS_TO_BYTES(256);
                break;
        }
    }

    // generate aes_key and aes_iv.
    for (int i = 0; i < actual_key_length; i++)
    {
        aes_key[i] = (byte)rand();
        aes_iv[i] = (byte)rand();
    }

    // initialize AES engines.
    if (wc_AesSetKeyDirect(&aes_encrypt, (const byte *)aes_key, actual_key_length, (const byte *)aes_iv, AES_ENCRYPTION))
    {
        printf("Cannot create AES engine for encryption.\n");
        wait_before_exit();
        return 0;
    }
    if (wc_AesSetKeyDirect(&aes_decrypt, (const byte *)aes_key, actual_key_length, (const byte *)aes_iv, AES_DECRYPTION))
    {
        printf("Cannot create AES engine for decryption.\n");
        wait_before_exit();
        return 0;
    }

    // generate original data.
    for (int i = 0; i < DATA_LENGTH; i++)
        original_data[i] = (byte)rand();

    // encrypt data.
    for (int i = 0; i < DATA_LENGTH; i += actual_key_length)
        wc_AesEncryptDirect(&aes_encrypt, POINTER_TO_INDEX(encrypted_data, i), (const byte*)POINTER_TO_INDEX(original_data, i));

    // decrypt data.
    for (int i = 0; i < DATA_LENGTH; i += actual_key_length)
        wc_AesDecryptDirect(&aes_decrypt, POINTER_TO_INDEX(decrypted_data, i), (const byte*)POINTER_TO_INDEX(encrypted_data, i));

    // check data.
    for (int i = 0; i < DATA_LENGTH; i++)
        if (original_data[i] != decrypted_data[i])
        {
            printf("Data mismatch at index %i: original value was %i but decrypted value is %i.\n", i, original_data[i], decrypted_data[i]);
            wait_before_exit();
            return 0;
        }

    printf("Decrypted data matches original data.\n");
    wait_before_exit();
    return 0;
}