STM32 Cryptographic library (cmox)
hi i'm new in stm32.
i'm using stm32 cryptographic library (cmox) for encrypt my data.
while using the library, i'm experiencing some strange.
I'm trying to encrypt data after initializing the library and generating the encryption key, but the encryption is getting weird on the very first call to the encrypt function.
After the initial call, both encryption/decryption are working fine.
What can I do to resolve it?
- mcu: STM32F401RDTx
struct crypto_ {
uint8_t key[16];
uint8_t iv[16];
} crypto;
void crypto_init(void) {
cmox_init_arg_t cmox_arg;
cmox_arg.target = CMOX_INIT_TARGET_F4;
if (cmox_initialize(&cmox_arg) != CMOX_INIT_SUCCESS) {
return;
}
crypto_generate_key();
uint8_t data[16] = { 0x00, };
for (uint8_t i = 0; i < sizeof(data); i++) {
data[i] = 10 + i;
}
printf("data: ");
for (uint8_t i = 0; i < sizeof(data); i++) {
printf("0x%02x ", data[i]);
}
printf("\n");
uint8_t enc_1[16] = { 0x00, };
uint8_t enc_2[16] = { 0x00, };
size_t enc_1_bytes = 0;
size_t enc_2_bytes = 0;
uint8_t dec_1[16] = { 0x00, };
uint8_t dec_2[16] = { 0x00, };
size_t dec_1_bytes = 0;
size_t dec_2_bytes = 0;
for (uint8_t i = 0; i < 3; i++) {
memset(enc_1, 0x00, sizeof(enc_1));
memset(dec_1, 0x00, sizeof(dec_1));
cmox_cipher_encrypt(
CMOX_AES_CBC_ENC_ALGO,
data, sizeof(data),
crypto.key, sizeof(crypto.key),
crypto.iv, sizeof(crypto.iv),
enc_1, &enc_1_bytes
);
cmox_cipher_decrypt(
CMOX_AES_CBC_DEC_ALGO,
enc_1, sizeof(enc_1),
crypto.key, sizeof(crypto.key),
crypto.iv, sizeof(crypto.iv),
dec_1, &dec_1_bytes
);
printf("enc_1: ");
for (uint8_t i = 0; i < enc_1_bytes; i++) {
printf("0x%02x ", enc_1[i]);
}
printf("\n");
printf("dec_1: ");
for (uint8_t i = 0; i < dec_1_bytes; i++) {
printf("0x%02x ", dec_1[i]);
}
printf("\n");
}
crypto_generate_key();
for (uint8_t i = 0; i < 3; i++) {
memset(enc_2, 0x00, sizeof(enc_2));
memset(dec_2, 0x00, sizeof(dec_2));
cmox_cipher_encrypt(
CMOX_AES_CBC_ENC_ALGO,
data, sizeof(data),
crypto.key, sizeof(crypto.key),
crypto.iv, sizeof(crypto.iv),
enc_2, &enc_2_bytes
);
cmox_cipher_decrypt(
CMOX_AES_CBC_DEC_ALGO,
enc_2, sizeof(enc_2),
crypto.key, sizeof(crypto.key),
crypto.iv, sizeof(crypto.iv),
dec_2, &dec_2_bytes
);
printf("enc_2: ");
for (uint8_t i = 0; i < enc_2_bytes; i++) {
printf("0x%02x ", enc_2[i]);
}
printf("\n");
printf("dec_2: ");
for (uint8_t i = 0; i < dec_2_bytes; i++) {
printf("0x%02x ", dec_2[i]);
}
printf("\n");
}
}
result
key: 0x9c 0x7d 0x28 0xc1 0x84 0x15 0x30 0xcc 0xe3 0x4a 0xe0 0xac 0xd1 0x71 0x28 0x01
iv: 0xa1 0x87 0x1e 0x54 0xa9 0xe2 0x66 0xba 0x87 0xd4 0xc0 0xc9 0x60 0xb4 0x93 0xb7
data: 0x0a 0x0b 0x0c 0x0d 0x0e 0x0f 0x10 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19
enc_1: 0x75 0x36 0xf0 0xe0 0xa8 0x2b 0x7d 0xd9 0xd4 0x39 0x95 0xff 0x9d 0xdd 0x8b 0x4c
dec_1: 0x2c 0xa5 0xe2 0x94 0x7e 0x98 0x90 0xc2 0x88 0x4c 0xf4 0x5d 0x8d 0xac 0xb2 0x11
enc_1: 0x84 0x36 0x7c 0xe2 0x63 0xd9 0x61 0x36 0x9f 0xfb 0x57 0x14 0x41 0x02 0x62 0xb6
dec_1: 0x0a 0x0b 0x0c 0x0d 0x0e 0x0f 0x10 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19
enc_1: 0x84 0x36 0x7c 0xe2 0x63 0xd9 0x61 0x36 0x9f 0xfb 0x57 0x14 0x41 0x02 0x62 0xb6
dec_1: 0x0a 0x0b 0x0c 0x0d 0x0e 0x0f 0x10 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19
key: 0x31 0xf8 0xc7 0x3d 0x80 0xd7 0xb4 0xd5 0x3b 0x8b 0x40 0xc0 0x39 0xb8 0xdd 0xa6
iv: 0xb5 0x41 0x24 0xb3 0x68 0xd7 0x68 0xf2 0x68 0x0f 0x8a 0xf6 0x5a 0xd7 0xf6 0x4b
enc_2: 0x38 0xfa 0x13 0x60 0xd4 0xfe 0x7e 0xad 0x3e 0x7d 0xca 0x0d 0x8c 0xe2 0x08 0xdf
dec_2: 0x0a 0x0b 0x0c 0x0d 0x0e 0x0f 0x10 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19
enc_2: 0x38 0xfa 0x13 0x60 0xd4 0xfe 0x7e 0xad 0x3e 0x7d 0xca 0x0d 0x8c 0xe2 0x08 0xdf
dec_2: 0x0a 0x0b 0x0c 0x0d 0x0e 0x0f 0x10 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19
enc_2: 0x38 0xfa 0x13 0x60 0xd4 0xfe 0x7e 0xad 0x3e 0x7d 0xca 0x0d 0x8c 0xe2 0x08 0xdf
dec_2: 0x0a 0x0b 0x0c 0x0d 0x0e 0x0f 0x10 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19
