samples: crypto: Add AES-GCM sample
Add sample for AES Galois/Counter Mode (GCM) of operation with a MACsec GCM-AES test vector. Also improve existing code by declaring expected ciphertext arrays as constant. Signed-off-by: Markus Fuchs <markus.fuchs@de.sauter-bc.com>
This commit is contained in:
parent
ad8f011dcf
commit
29630cc7fc
|
@ -5,6 +5,7 @@ CONFIG_MBEDTLS_BUILTIN=y
|
|||
CONFIG_MBEDTLS_CFG_FILE="config-tls-generic.h"
|
||||
CONFIG_MBEDTLS_HEAP_SIZE=512
|
||||
CONFIG_MBEDTLS_CIPHER_CCM_ENABLED=y
|
||||
CONFIG_MBEDTLS_CIPHER_MODE_GCM_ENABLED=y
|
||||
CONFIG_MAIN_STACK_SIZE=4096
|
||||
|
||||
CONFIG_CRYPTO=y
|
||||
|
|
|
@ -18,6 +18,7 @@ tests:
|
|||
- ".*: CBC Mode"
|
||||
- ".*: CTR Mode"
|
||||
- ".*: CCM Mode"
|
||||
- ".*: GCM Mode"
|
||||
sample.drivers.crypto.mbedtls.micro:
|
||||
tags: crypto
|
||||
harness_config:
|
||||
|
@ -27,6 +28,7 @@ tests:
|
|||
- ".*: CBC Mode"
|
||||
- ".*: CTR Mode"
|
||||
- ".*: CCM Mode"
|
||||
- ".*: GCM Mode"
|
||||
sample.drivers.crypto.stm32:
|
||||
tags: crypto
|
||||
platform_whitelist: mikroe_mini_m4_for_stm32
|
||||
|
|
|
@ -43,7 +43,7 @@ static u8_t plaintext[64] = {
|
|||
|
||||
u32_t cap_flags;
|
||||
|
||||
static void print_buffer_comparison(u8_t *wanted_result,
|
||||
static void print_buffer_comparison(const u8_t *wanted_result,
|
||||
u8_t *result, size_t length)
|
||||
{
|
||||
int i, j;
|
||||
|
@ -113,7 +113,7 @@ void ecb_mode(struct device *dev)
|
|||
0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
|
||||
0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
|
||||
};
|
||||
u8_t ecb_ciphertext[16] = {
|
||||
const u8_t ecb_ciphertext[16] = {
|
||||
0x69, 0xC4, 0xE0, 0xD8, 0x6A, 0x7B, 0x04, 0x30,
|
||||
0xD8, 0xCD, 0xB7, 0x80, 0x70, 0xB4, 0xC5, 0x5A
|
||||
};
|
||||
|
@ -188,7 +188,7 @@ out:
|
|||
cipher_free_session(dev, &ini);
|
||||
}
|
||||
|
||||
static u8_t cbc_ciphertext[80] = {
|
||||
static const 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,
|
||||
0xce, 0xe9, 0x8e, 0x9b, 0x12, 0xe9, 0x19, 0x7d, 0x50, 0x86, 0xcb, 0x9b,
|
||||
|
@ -276,7 +276,7 @@ out:
|
|||
cipher_free_session(dev, &ini);
|
||||
}
|
||||
|
||||
static u8_t ctr_ciphertext[64] = {
|
||||
static const u8_t ctr_ciphertext[64] = {
|
||||
0x22, 0xe5, 0x2f, 0xb1, 0x77, 0xd8, 0x65, 0xb2,
|
||||
0xf7, 0xc6, 0xb5, 0x12, 0x69, 0x2d, 0x11, 0x4d,
|
||||
0xed, 0x6c, 0x1c, 0x72, 0x25, 0xda, 0xf6, 0xa2,
|
||||
|
@ -381,7 +381,7 @@ static u8_t ccm_data[23] = {
|
|||
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13,
|
||||
0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e
|
||||
};
|
||||
static u8_t ccm_expected[31] = {
|
||||
static const u8_t ccm_expected[31] = {
|
||||
0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2, 0xf0, 0x66, 0xd0, 0xc2,
|
||||
0xc0, 0xf9, 0x89, 0x80, 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84, 0x17,
|
||||
0xe8, 0xd1, 0x2c, 0xfd, 0xf9, 0x26, 0xe0
|
||||
|
@ -477,6 +477,123 @@ out:
|
|||
cipher_free_session(dev, &ini);
|
||||
}
|
||||
|
||||
/* MACsec GCM-AES test vector 2.4.1 */
|
||||
static u8_t gcm_key[16] = {
|
||||
0x07, 0x1b, 0x11, 0x3b, 0x0c, 0xa7, 0x43, 0xfe, 0xcc, 0xcf, 0x3d, 0x05,
|
||||
0x1f, 0x73, 0x73, 0x82
|
||||
};
|
||||
static u8_t gcm_nonce[12] = {
|
||||
0xf0, 0x76, 0x1e, 0x8d, 0xcd, 0x3d, 0x00, 0x01, 0x76, 0xd4, 0x57, 0xed
|
||||
};
|
||||
static u8_t gcm_hdr[20] = {
|
||||
0xe2, 0x01, 0x06, 0xd7, 0xcd, 0x0d, 0xf0, 0x76, 0x1e, 0x8d, 0xcd, 0x3d,
|
||||
0x88, 0xe5, 0x4c, 0x2a, 0x76, 0xd4, 0x57, 0xed
|
||||
};
|
||||
static u8_t gcm_data[42] = {
|
||||
0x08, 0x00, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
|
||||
0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24,
|
||||
0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
|
||||
0x31, 0x32, 0x33, 0x34, 0x00, 0x04
|
||||
};
|
||||
static const u8_t gcm_expected[58] = {
|
||||
0x13, 0xb4, 0xc7, 0x2b, 0x38, 0x9d, 0xc5, 0x01, 0x8e, 0x72, 0xa1, 0x71,
|
||||
0xdd, 0x85, 0xa5, 0xd3, 0x75, 0x22, 0x74, 0xd3, 0xa0, 0x19, 0xfb, 0xca,
|
||||
0xed, 0x09, 0xa4, 0x25, 0xcd, 0x9b, 0x2e, 0x1c, 0x9b, 0x72, 0xee, 0xe7,
|
||||
0xc9, 0xde, 0x7d, 0x52, 0xb3, 0xf3,
|
||||
0xd6, 0xa5, 0x28, 0x4f, 0x4a, 0x6d, 0x3f, 0xe2, 0x2a, 0x5d, 0x6c, 0x2b,
|
||||
0x96, 0x04, 0x94, 0xc3
|
||||
};
|
||||
|
||||
void gcm_mode(struct device *dev)
|
||||
{
|
||||
u8_t encrypted[60] = {0};
|
||||
u8_t decrypted[44] = {0};
|
||||
struct cipher_ctx ini = {
|
||||
.keylen = sizeof(gcm_key),
|
||||
.key.bit_stream = gcm_key,
|
||||
.mode_params.gcm_info = {
|
||||
.nonce_len = sizeof(gcm_nonce),
|
||||
.tag_len = 16,
|
||||
},
|
||||
.flags = cap_flags,
|
||||
};
|
||||
struct cipher_pkt encrypt = {
|
||||
.in_buf = gcm_data,
|
||||
.in_len = sizeof(gcm_data),
|
||||
.out_buf_max = sizeof(encrypted),
|
||||
.out_buf = encrypted,
|
||||
};
|
||||
struct cipher_aead_pkt gcm_op = {
|
||||
.ad = gcm_hdr,
|
||||
.ad_len = sizeof(gcm_hdr),
|
||||
.pkt = &encrypt,
|
||||
/* TinyCrypt always puts the tag at the end of the ciphered
|
||||
* text, but other library such as mbedtls might be more
|
||||
* flexible and can take a different buffer for it. So to
|
||||
* make sure test passes on all backends: enforcing the tag
|
||||
* buffer to be after the ciphered text.
|
||||
*/
|
||||
.tag = encrypted + sizeof(gcm_data),
|
||||
};
|
||||
struct cipher_pkt decrypt = {
|
||||
.in_buf = encrypted,
|
||||
.in_len = sizeof(gcm_data),
|
||||
.out_buf = decrypted,
|
||||
.out_buf_max = sizeof(decrypted),
|
||||
};
|
||||
|
||||
if (cipher_begin_session(dev, &ini, CRYPTO_CIPHER_ALGO_AES,
|
||||
CRYPTO_CIPHER_MODE_GCM,
|
||||
CRYPTO_CIPHER_OP_ENCRYPT)) {
|
||||
return;
|
||||
}
|
||||
|
||||
gcm_op.pkt = &encrypt;
|
||||
if (cipher_gcm_op(&ini, &gcm_op, gcm_nonce)) {
|
||||
LOG_ERR("GCM mode ENCRYPT - Failed");
|
||||
goto out;
|
||||
}
|
||||
|
||||
LOG_INF("Output length (encryption): %d", encrypt.out_len);
|
||||
|
||||
if (memcmp(encrypt.out_buf, gcm_expected, sizeof(gcm_expected))) {
|
||||
LOG_ERR("GCM mode ENCRYPT - Mismatch between expected "
|
||||
"and returned cipher text");
|
||||
print_buffer_comparison(gcm_expected,
|
||||
encrypt.out_buf, sizeof(gcm_expected));
|
||||
goto out;
|
||||
}
|
||||
|
||||
LOG_INF("GCM mode ENCRYPT - Match");
|
||||
cipher_free_session(dev, &ini);
|
||||
|
||||
if (cipher_begin_session(dev, &ini, CRYPTO_CIPHER_ALGO_AES,
|
||||
CRYPTO_CIPHER_MODE_GCM,
|
||||
CRYPTO_CIPHER_OP_DECRYPT)) {
|
||||
return;
|
||||
}
|
||||
|
||||
gcm_op.pkt = &decrypt;
|
||||
if (cipher_gcm_op(&ini, &gcm_op, gcm_nonce)) {
|
||||
LOG_ERR("GCM mode DECRYPT - Failed");
|
||||
goto out;
|
||||
}
|
||||
|
||||
LOG_INF("Output length (decryption): %d", decrypt.out_len);
|
||||
|
||||
if (memcmp(decrypt.out_buf, gcm_data, sizeof(gcm_data))) {
|
||||
LOG_ERR("GCM mode DECRYPT - Mismatch between plaintext "
|
||||
"and decrypted cipher text");
|
||||
print_buffer_comparison(gcm_data,
|
||||
decrypt.out_buf, sizeof(gcm_data));
|
||||
goto out;
|
||||
}
|
||||
|
||||
LOG_INF("GCM mode DECRYPT - Match");
|
||||
out:
|
||||
cipher_free_session(dev, &ini);
|
||||
}
|
||||
|
||||
struct mode_test {
|
||||
const char *mode;
|
||||
void (*mode_func)(struct device *dev);
|
||||
|
@ -490,6 +607,7 @@ void main(void)
|
|||
{ .mode = "CBC Mode", .mode_func = cbc_mode },
|
||||
{ .mode = "CTR Mode", .mode_func = ctr_mode },
|
||||
{ .mode = "CCM Mode", .mode_func = ccm_mode },
|
||||
{ .mode = "GCM Mode", .mode_func = gcm_mode },
|
||||
{ },
|
||||
};
|
||||
int i;
|
||||
|
|
Loading…
Reference in a new issue