Path: blob/master/lib/crypto/tests/aes_cbc_macs_kunit.c
170891 views
// SPDX-License-Identifier: GPL-2.0-or-later1/*2* Copyright 2026 Google LLC3*/4#include <crypto/aes-cbc-macs.h>5#include "aes-cmac-testvecs.h"67/*8* A fixed key used when presenting AES-CMAC as an unkeyed hash function in9* order to reuse hash-test-template.h. At the beginning of the test suite,10* this is initialized to a key prepared from bytes generated from a fixed seed.11*/12static struct aes_cmac_key test_key;1314static void aes_cmac_init_withtestkey(struct aes_cmac_ctx *ctx)15{16aes_cmac_init(ctx, &test_key);17}1819static void aes_cmac_withtestkey(const u8 *data, size_t data_len,20u8 out[AES_BLOCK_SIZE])21{22aes_cmac(&test_key, data, data_len, out);23}2425#define HASH aes_cmac_withtestkey26#define HASH_CTX aes_cmac_ctx27#define HASH_SIZE AES_BLOCK_SIZE28#define HASH_INIT aes_cmac_init_withtestkey29#define HASH_UPDATE aes_cmac_update30#define HASH_FINAL aes_cmac_final31#include "hash-test-template.h"3233static int aes_cbc_macs_suite_init(struct kunit_suite *suite)34{35u8 raw_key[AES_KEYSIZE_256];36int err;3738rand_bytes_seeded_from_len(raw_key, sizeof(raw_key));39err = aes_cmac_preparekey(&test_key, raw_key, sizeof(raw_key));40if (err)41return err;42return hash_suite_init(suite);43}4445static void aes_cbc_macs_suite_exit(struct kunit_suite *suite)46{47hash_suite_exit(suite);48}4950/* Verify compatibility of the AES-CMAC implementation with RFC 4493. */51static void test_aes_cmac_rfc4493(struct kunit *test)52{53static const u8 raw_key[AES_KEYSIZE_128] = {540x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,550xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c,56};57static const struct {58size_t data_len;59const u8 data[40];60const u8 mac[AES_BLOCK_SIZE];61} testvecs[] = {62{63/* Example 1 from RFC 4493 */64.data_len = 0,65.mac = {660xbb, 0x1d, 0x69, 0x29, 0xe9, 0x59, 0x37, 0x28,670x7f, 0xa3, 0x7d, 0x12, 0x9b, 0x75, 0x67, 0x46,68},6970},71{72/* Example 2 from RFC 4493 */73.data = {740x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,750xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,76},77.data_len = 16,78.mac = {790x07, 0x0a, 0x16, 0xb4, 0x6b, 0x4d, 0x41, 0x44,800xf7, 0x9b, 0xdd, 0x9d, 0xd0, 0x4a, 0x28, 0x7c,81},82},83{84/* Example 3 from RFC 4493 */85.data = {860x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,870xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,880xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,890x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,900x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11,91},92.data_len = 40,93.mac = {940xdf, 0xa6, 0x67, 0x47, 0xde, 0x9a, 0xe6, 0x30,950x30, 0xca, 0x32, 0x61, 0x14, 0x97, 0xc8, 0x27,96},97},98};99struct aes_cmac_key key;100int err;101102err = aes_cmac_preparekey(&key, raw_key, sizeof(raw_key));103KUNIT_ASSERT_EQ(test, err, 0);104105for (size_t i = 0; i < ARRAY_SIZE(testvecs); i++) {106u8 mac[AES_BLOCK_SIZE];107108aes_cmac(&key, testvecs[i].data, testvecs[i].data_len, mac);109KUNIT_ASSERT_MEMEQ(test, mac, testvecs[i].mac, AES_BLOCK_SIZE);110}111}112113/*114* Verify compatibility of the AES-XCBC-MAC implementation with RFC 3566.115*116* Additional AES-XCBC-MAC tests are not necessary, since the AES-XCBC-MAC117* implementation is well covered by the AES-CMAC tests already. Only the key118* preparation function differs; the rest of the code is shared.119*/120static void test_aes_xcbcmac_rfc3566(struct kunit *test)121{122struct aes_cmac_key key;123/* AES-XCBC-MAC Test Case #4 from RFC 3566 */124static const u8 raw_key[AES_KEYSIZE_128] = {1250x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,1260x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,127};128static const u8 message[20] = {1290x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,1300x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13,131};132static const u8 expected_mac[AES_BLOCK_SIZE] = {1330x47, 0xf5, 0x1b, 0x45, 0x64, 0x96, 0x62, 0x15,1340xb8, 0x98, 0x5c, 0x63, 0x05, 0x5e, 0xd3, 0x08,135};136u8 actual_mac[AES_BLOCK_SIZE];137138aes_xcbcmac_preparekey(&key, raw_key);139aes_cmac(&key, message, sizeof(message), actual_mac);140KUNIT_ASSERT_MEMEQ(test, actual_mac, expected_mac, AES_BLOCK_SIZE);141}142143static void test_aes_cbcmac_rfc3610(struct kunit *test)144{145/*146* The following AES-CBC-MAC test vector is extracted from RFC 3610147* Packet Vector #11. It required some rearrangement to get the actual148* input to AES-CBC-MAC from the values given.149*/150static const u8 raw_key[AES_KEYSIZE_128] = {1510xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,1520xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,153};154const size_t unpadded_data_len = 52;155static const u8 data[64] = {156/* clang-format off */157/* CCM header */1580x61, 0x00, 0x00, 0x00, 0x0d, 0x0c, 0x0b, 0x0a,1590xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0x00, 0x14,160/* CCM additional authentication blocks */1610x00, 0x0c, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05,1620x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x00, 0x00,163/* CCM message blocks */1640x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13,1650x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b,1660x1c, 0x1d, 0x1e, 0x1f, 0x00, 0x00, 0x00, 0x00,1670x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,168/* clang-format on */169};170static const u8 expected_mac[AES_BLOCK_SIZE] = {1710x6b, 0x5e, 0x24, 0x34, 0x12, 0xcc, 0xc2, 0xad,1720x6f, 0x1b, 0x11, 0xc3, 0xa1, 0xa9, 0xd8, 0xbc,173};174struct aes_enckey key;175struct aes_cbcmac_ctx ctx;176u8 actual_mac[AES_BLOCK_SIZE];177int err;178179err = aes_prepareenckey(&key, raw_key, sizeof(raw_key));180KUNIT_ASSERT_EQ(test, err, 0);181182/*183* Trailing zeroes should not affect the CBC-MAC value, up to the next184* AES block boundary.185*/186for (size_t data_len = unpadded_data_len; data_len <= sizeof(data);187data_len++) {188aes_cbcmac_init(&ctx, &key);189aes_cbcmac_update(&ctx, data, data_len);190aes_cbcmac_final(&ctx, actual_mac);191KUNIT_ASSERT_MEMEQ(test, actual_mac, expected_mac,192AES_BLOCK_SIZE);193194/* Incremental computations should produce the same result. */195for (size_t part1_len = 0; part1_len <= data_len; part1_len++) {196aes_cbcmac_init(&ctx, &key);197aes_cbcmac_update(&ctx, data, part1_len);198aes_cbcmac_update(&ctx, &data[part1_len],199data_len - part1_len);200aes_cbcmac_final(&ctx, actual_mac);201KUNIT_ASSERT_MEMEQ(test, actual_mac, expected_mac,202AES_BLOCK_SIZE);203}204}205}206207static struct kunit_case aes_cbc_macs_test_cases[] = {208HASH_KUNIT_CASES,209KUNIT_CASE(test_aes_cmac_rfc4493),210KUNIT_CASE(test_aes_xcbcmac_rfc3566),211KUNIT_CASE(test_aes_cbcmac_rfc3610),212KUNIT_CASE(benchmark_hash),213{},214};215216static struct kunit_suite aes_cbc_macs_test_suite = {217.name = "aes_cbc_macs",218.test_cases = aes_cbc_macs_test_cases,219.suite_init = aes_cbc_macs_suite_init,220.suite_exit = aes_cbc_macs_suite_exit,221};222kunit_test_suite(aes_cbc_macs_test_suite);223224MODULE_DESCRIPTION(225"KUnit tests and benchmark for AES-CMAC, AES-XCBC-MAC, and AES-CBC-MAC");226MODULE_IMPORT_NS("CRYPTO_INTERNAL");227MODULE_LICENSE("GPL");228229230