Topic: Wolfssl esp32 aes in ctr mode not compatible with x86 openssl EVP aes
Hello,
I'm having an issue encrypting things on my esp32 using `wc_AesCtrEncrypt` with a 128 bit key and decrypting them on an x86 computer using EVP aes in ctr mode. I first thought it was an endian-ness problem so I tried the following on both systems to see if it would show a pattern of how I need the swap my byes: encrypt a plaintext of all 0's using a 128 bit key of all 0s and an iv of all 0s. Here is the code I used on the esp32:
int j;
Aes reusable_aes_key;
unsigned char key[16] = { 0 };
unsigned char aes_iv[16] = { 0 };
unsigned char temp_payload_plain[64] = { 0 };
unsigned char temp_payload_cipher[64];
wc_AesInit( &reusable_aes_key, NULL, INVALID_DEVID );
wc_AesSetKeyDirect( &reusable_aes_key, key, 16, aes_iv, AES_ENCRYPTION );
wc_AesCtrEncrypt( &reusable_aes_key, temp_payload_cipher, temp_payload_plain, 64 );
for ( j = 0; j < 64; j++ ) {
ESP_LOGE( TAG, "%.2x", temp_payload_cipher[j] );
}
From the wolfssl encryption I got:
c0
7e
66
00
c0
7e
66
00
c0
7e
66
00
c0
7e
66
00
c0
7e
66
00
c0
7e
66
00
c0
7e
66
00
c0
7e
66
00
c0
7e
66
00
c0
7e
66
00
c0
7e
66
00
c0
7e
66
00
c0
7e
66
00
c0
7e
66
00
c0
7e
66
00
c0
7e
66
00
From the openssl side using `EVP_EncryptUpdate`:
66
e9
4b
d4
ef
8a
2c
3b
88
4c
fa
59
ca
34
2b
2e
58
e2
fc
ce
fa
7e
30
61
36
7f
1d
57
a4
e7
45
5a
03
88
da
ce
60
b6
a3
92
f3
28
c2
b9
71
b2
fe
78
f7
95
aa
ab
49
4b
59
23
f7
fd
89
ff
94
8b
c1
e0
From this it looks like theres something more going on with the EVP aes ctr since its bytes are non-repeating. I looked into wolfssl and saw that there is compatibility for openssl and there are EVP libraries but the esp32 doesn't seem to support openssl extras since there is no openssl directory in the port and when I define the constant in user settings it gives me all kinds of errors. So basically I'm wondering what I would need to do to make the `wc_AesCtrEncrypt` function compatible with the EVP functions or if there is a way to include the opnessl extras into the esp32 build, assuming my assumptions aren't completely off and I haven't made a mistake setting up the aes object. Additionally I can't really change the openssl side because it isn't my system I'm just integrating with it, it's pre-existing and I have to meet its requirements.
Anyways, all help is appreciated, let me know if you need more info.