Closed Oquirella closed 2 years ago
Cannot reproduce, using the following code. This encrypts the data, producing an encrypted result consistent with your report above. The decryption then generates the original input data. Tested using mbedtls-2.16.9.
#include <stdio.h>
#include "mbedtls/config.h"
#include "mbedtls/aes.h"
#include "mbedtls/platform_util.h"
const unsigned char aes_key[65] = {
0x62, 0x38, 0x36, 0x35, 0x36, 0x65, 0x37, 0x33, 0x33, 0x37, 0x38, 0x32,
0x35, 0x30, 0x61, 0x36, 0x32, 0x32, 0x30, 0x62, 0x35, 0x64, 0x32, 0x66,
0x34, 0x62, 0x65, 0x39, 0x33, 0x66, 0x32, 0x63, 0x62, 0x33, 0x37, 0x32,
0x66, 0x64, 0x37, 0x66, 0x61, 0x33, 0x36, 0x66, 0x37, 0x36, 0x65, 0x39,
0x66, 0x37, 0x61, 0x35, 0x36, 0x37, 0x34, 0x30, 0x34, 0x61, 0x35, 0x37,
0x65, 0x66, 0x62, 0x39, 0x0a
};
const unsigned char data1[] = { 00, 02, 00, 01, 02, 00, 00, 00, 06, 00, 01, 00, 05, 00, 00, 00 };
int main(void) {
mbedtls_aes_context aes_ctx;
size_t offset;
unsigned char data2[sizeof(data1)];
unsigned char data3[sizeof(data1)];
unsigned char stream_block[32];
unsigned char nonce_counter[16];
for ( int i = 0; i < sizeof(data1); i++ ) printf("%x, ", data1[i] ); printf( "\n" );
offset = 0;
mbedtls_platform_zeroize( stream_block, sizeof( stream_block ) );
mbedtls_platform_zeroize( nonce_counter, sizeof( nonce_counter ) );
mbedtls_aes_init( &aes_ctx );
mbedtls_aes_setkey_enc( &aes_ctx, aes_key, 256 );
mbedtls_aes_crypt_ctr( &aes_ctx, sizeof(data1), &offset, nonce_counter, stream_block, data1, data2);
mbedtls_aes_free( &aes_ctx );
for ( int i = 0; i < sizeof(data2); i++ ) printf("%x, ", data2[i] ); printf( "\n" );
offset = 0;
mbedtls_platform_zeroize( stream_block, sizeof( stream_block ) );
mbedtls_platform_zeroize( nonce_counter, sizeof( nonce_counter ) );
mbedtls_aes_init( &aes_ctx );
mbedtls_aes_setkey_enc( &aes_ctx, aes_key, 256 );
mbedtls_aes_crypt_ctr( &aes_ctx, sizeof(data2), &offset, nonce_counter, stream_block, data2, data3);
mbedtls_aes_free( &aes_ctx );
for ( int i = 0; i < sizeof(data3); i++ ) printf("%x, ", data3[i] ); printf( "\n" );
}
Output is:
0, 2, 0, 1, 2, 0, 0, 0, 6, 0, 1, 0, 5, 0, 0, 0,
21, 2b, d0, 66, 5a, eb, 1a, 11, 16, c0, ca, 22, 71, c9, 84, 19,
0, 2, 0, 1, 2, 0, 0, 0, 6, 0, 1, 0, 5, 0, 0, 0,
Hi,
What is the STM32 module used?
Summary
I am trying to encrypt and send the following 16 bytes over the uart (from an ESP32 to STM32):
I am using the following iv and key to do the encryption:
Which results in:
When i try to decrypt this on the other side (STM32) i get the following result when using the same key and IV vector:
I expected this:
System information
Mbed TLS version (number or commit id): v2.16.9 Operating system and version: FREERTOS Configuration (if not default, please attach
mbedtls_config.h
):Compiler and options (if you used a pre-built binary, please indicate how you obtained it): Additional environment information:
Expected behavior
Actual behavior
Steps to reproduce
Additional information