samples: crypto: Add AES ECB mode sample

Add sample for AES ECB mode with FIPS-197 ECB test vetors

Signed-off-by: Siddharth Chandrasekaran <siddharth@embedjournal.com>
This commit is contained in:
Siddharth Chandrasekaran 2019-11-08 13:20:54 +05:30 committed by Carles Cufí
commit bf45e32fec

View file

@ -100,6 +100,92 @@ int validate_hw_compatibility(struct device *dev)
}
void ecb_mode(struct device *dev)
{
/* from FIPS-197 test vectors */
u8_t ecb_key[16] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
};
u8_t ecb_plaintext[16] = {
0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
};
u8_t ecb_ciphertext[16] = {
0x69, 0xC4, 0xE0, 0xD8, 0x6A, 0x7B, 0x04, 0x30,
0xD8, 0xCD, 0xB7, 0x80, 0x70, 0xB4, 0xC5, 0x5A
};
u8_t encrypted[16] = {0};
u8_t decrypted[16] = {0};
struct cipher_ctx ini = {
.keylen = sizeof(ecb_key),
.key.bit_stream = ecb_key,
.flags = cap_flags,
};
struct cipher_pkt encrypt = {
.in_buf = ecb_plaintext,
.in_len = sizeof(ecb_plaintext),
.out_buf_max = sizeof(encrypted),
.out_buf = encrypted,
};
struct cipher_pkt decrypt = {
.in_buf = encrypt.out_buf,
.in_len = sizeof(encrypted),
.out_buf = decrypted,
.out_buf_max = sizeof(decrypted),
};
if (cipher_begin_session(dev, &ini, CRYPTO_CIPHER_ALGO_AES,
CRYPTO_CIPHER_MODE_ECB,
CRYPTO_CIPHER_OP_ENCRYPT)) {
return;
}
if (cipher_block_op(&ini, &encrypt)) {
LOG_ERR("ECB mode ENCRYPT - Failed");
goto out;
}
LOG_INF("Output length (encryption): %d", encrypt.out_len);
if (memcmp(encrypt.out_buf, ecb_ciphertext, sizeof(ecb_ciphertext))) {
LOG_ERR("ECB mode ENCRYPT - Mismatch between expected and "
"returned cipher text");
print_buffer_comparison(ecb_ciphertext, encrypt.out_buf,
sizeof(ecb_ciphertext));
goto out;
}
LOG_INF("ECB mode ENCRYPT - Match");
cipher_free_session(dev, &ini);
if (cipher_begin_session(dev, &ini, CRYPTO_CIPHER_ALGO_AES,
CRYPTO_CIPHER_MODE_ECB,
CRYPTO_CIPHER_OP_DECRYPT)) {
return;
}
if (cipher_block_op(&ini, &decrypt)) {
LOG_ERR("ECB mode DECRYPT - Failed");
goto out;
}
LOG_INF("Output length (decryption): %d", decrypt.out_len);
if (memcmp(decrypt.out_buf, ecb_plaintext, sizeof(ecb_plaintext))) {
LOG_ERR("ECB mode DECRYPT - Mismatch between plaintext and "
"decrypted cipher text");
print_buffer_comparison(ecb_plaintext, decrypt.out_buf,
sizeof(ecb_plaintext));
goto out;
}
LOG_INF("ECB mode DECRYPT - Match");
out:
cipher_free_session(dev, &ini);
}
static u8_t cbc_ciphertext[80] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
0x0c, 0x0d, 0x0e, 0x0f, 0x76, 0x49, 0xab, 0xac, 0x81, 0x19, 0xb2, 0x46,
@ -398,6 +484,7 @@ void main(void)
{
struct device *dev = device_get_binding(CRYPTO_DRV_NAME);
const struct mode_test modes[] = {
{ .mode = "ECB Mode", .mode_func = ecb_mode },
{ .mode = "CBC Mode", .mode_func = cbc_mode },
{ .mode = "CTR Mode", .mode_func = ctr_mode },
{ .mode = "CCM Mode", .mode_func = ccm_mode },