Resguardo inicial

This commit is contained in:
lastdragon
2026-09-06 12:53:21 -06:00
parent 662efe3c2a
commit ad2088d178
94853 changed files with 43200404 additions and 0 deletions
+184
View File
@@ -0,0 +1,184 @@
# SPDX-License-Identifier: GPL-2.0-or-later
config CRYPTO_LIB_AES_CBC_MACS_KUNIT_TEST
tristate "KUnit tests for AES-CMAC, AES-XCBC-MAC, and AES-CBC-MAC" if !KUNIT_ALL_TESTS
depends on KUNIT && CRYPTO_LIB_AES_CBC_MACS
default KUNIT_ALL_TESTS
select CRYPTO_LIB_BENCHMARK_VISIBLE
help
KUnit tests for the AES-CMAC, AES-XCBC-MAC, and AES-CBC-MAC message
authentication codes.
config CRYPTO_LIB_BLAKE2B_KUNIT_TEST
tristate "KUnit tests for BLAKE2b" if !KUNIT_ALL_TESTS
depends on KUNIT && CRYPTO_LIB_BLAKE2B
default KUNIT_ALL_TESTS
select CRYPTO_LIB_BENCHMARK_VISIBLE
help
KUnit tests for the BLAKE2b cryptographic hash function.
config CRYPTO_LIB_BLAKE2S_KUNIT_TEST
tristate "KUnit tests for BLAKE2s" if !KUNIT_ALL_TESTS
depends on KUNIT
default KUNIT_ALL_TESTS
select CRYPTO_LIB_BENCHMARK_VISIBLE
# No need to depend on CRYPTO_LIB_BLAKE2S here, as that option doesn't
# exist; the BLAKE2s code is always built-in for the /dev/random driver.
help
KUnit tests for the BLAKE2s cryptographic hash function.
config CRYPTO_LIB_CHACHA20POLY1305_KUNIT_TEST
tristate "KUnit tests for ChaCha20Poly1305" if !KUNIT_ALL_TESTS
depends on KUNIT && CRYPTO_LIB_CHACHA20POLY1305
default KUNIT_ALL_TESTS
select CRYPTO_LIB_BENCHMARK_VISIBLE
help
KUnit tests for the ChaCha20Poly1305 authenticated encryption
algorithm.
config CRYPTO_LIB_CURVE25519_KUNIT_TEST
tristate "KUnit tests for Curve25519" if !KUNIT_ALL_TESTS
depends on KUNIT && CRYPTO_LIB_CURVE25519
default KUNIT_ALL_TESTS
select CRYPTO_LIB_BENCHMARK_VISIBLE
help
KUnit tests for the Curve25519 Diffie-Hellman function.
config CRYPTO_LIB_GHASH_KUNIT_TEST
tristate "KUnit tests for GHASH" if !KUNIT_ALL_TESTS
depends on KUNIT && CRYPTO_LIB_GF128HASH
default KUNIT_ALL_TESTS
select CRYPTO_LIB_BENCHMARK_VISIBLE
help
KUnit tests for the GHASH library functions.
config CRYPTO_LIB_MD5_KUNIT_TEST
tristate "KUnit tests for MD5" if !KUNIT_ALL_TESTS
depends on KUNIT && CRYPTO_LIB_MD5
default KUNIT_ALL_TESTS
select CRYPTO_LIB_BENCHMARK_VISIBLE
help
KUnit tests for the MD5 cryptographic hash function and its
corresponding HMAC.
config CRYPTO_LIB_MLDSA_KUNIT_TEST
tristate "KUnit tests for ML-DSA" if !KUNIT_ALL_TESTS
depends on KUNIT && CRYPTO_LIB_MLDSA
default KUNIT_ALL_TESTS
select CRYPTO_LIB_BENCHMARK_VISIBLE
help
KUnit tests for the ML-DSA digital signature algorithm.
config CRYPTO_LIB_NH_KUNIT_TEST
tristate "KUnit tests for NH" if !KUNIT_ALL_TESTS
depends on KUNIT && CRYPTO_LIB_NH
default KUNIT_ALL_TESTS
help
KUnit tests for the NH almost-universal hash function.
config CRYPTO_LIB_POLY1305_KUNIT_TEST
tristate "KUnit tests for Poly1305" if !KUNIT_ALL_TESTS
depends on KUNIT && CRYPTO_LIB_POLY1305
default KUNIT_ALL_TESTS
select CRYPTO_LIB_BENCHMARK_VISIBLE
help
KUnit tests for the Poly1305 library functions.
config CRYPTO_LIB_POLYVAL_KUNIT_TEST
tristate "KUnit tests for POLYVAL" if !KUNIT_ALL_TESTS
depends on KUNIT && CRYPTO_LIB_GF128HASH
default KUNIT_ALL_TESTS
select CRYPTO_LIB_BENCHMARK_VISIBLE
help
KUnit tests for the POLYVAL library functions.
config CRYPTO_LIB_SHA1_KUNIT_TEST
tristate "KUnit tests for SHA-1" if !KUNIT_ALL_TESTS
depends on KUNIT && CRYPTO_LIB_SHA1
default KUNIT_ALL_TESTS
select CRYPTO_LIB_BENCHMARK_VISIBLE
help
KUnit tests for the SHA-1 cryptographic hash function and its
corresponding HMAC.
# Option is named *_SHA256_KUNIT_TEST, though both SHA-224 and SHA-256 tests are
# included, for consistency with the naming used elsewhere (e.g. CRYPTO_SHA256).
config CRYPTO_LIB_SHA256_KUNIT_TEST
tristate "KUnit tests for SHA-224 and SHA-256" if !KUNIT_ALL_TESTS
depends on KUNIT && CRYPTO_LIB_SHA256
default KUNIT_ALL_TESTS
select CRYPTO_LIB_BENCHMARK_VISIBLE
help
KUnit tests for the SHA-224 and SHA-256 cryptographic hash functions
and their corresponding HMACs.
# Option is named *_SHA512_KUNIT_TEST, though both SHA-384 and SHA-512 tests are
# included, for consistency with the naming used elsewhere (e.g. CRYPTO_SHA512).
config CRYPTO_LIB_SHA512_KUNIT_TEST
tristate "KUnit tests for SHA-384 and SHA-512" if !KUNIT_ALL_TESTS
depends on KUNIT && CRYPTO_LIB_SHA512
default KUNIT_ALL_TESTS
select CRYPTO_LIB_BENCHMARK_VISIBLE
help
KUnit tests for the SHA-384 and SHA-512 cryptographic hash functions
and their corresponding HMACs.
config CRYPTO_LIB_SHA3_KUNIT_TEST
tristate "KUnit tests for SHA-3" if !KUNIT_ALL_TESTS
depends on KUNIT && CRYPTO_LIB_SHA3
default KUNIT_ALL_TESTS
select CRYPTO_LIB_BENCHMARK_VISIBLE
help
KUnit tests for the SHA3 cryptographic hash and XOF functions,
including SHA3-224, SHA3-256, SHA3-384, SHA3-512, SHAKE128 and
SHAKE256.
config CRYPTO_LIB_SM3_KUNIT_TEST
tristate "KUnit tests for SM3" if !KUNIT_ALL_TESTS
depends on KUNIT && CRYPTO_LIB_SM3
default KUNIT_ALL_TESTS
select CRYPTO_LIB_BENCHMARK_VISIBLE
help
KUnit tests for the SM3 cryptographic hash function.
config CRYPTO_LIB_ENABLE_ALL_FOR_KUNIT
tristate "Enable all crypto library code for KUnit tests"
depends on KUNIT
select CRYPTO_LIB_AES_CBC_MACS
select CRYPTO_LIB_BLAKE2B
select CRYPTO_LIB_CHACHA20POLY1305
select CRYPTO_LIB_CURVE25519
select CRYPTO_LIB_GF128HASH
select CRYPTO_LIB_MD5
select CRYPTO_LIB_MLDSA
select CRYPTO_LIB_NH
select CRYPTO_LIB_POLY1305
select CRYPTO_LIB_SHA1
select CRYPTO_LIB_SHA256
select CRYPTO_LIB_SHA512
select CRYPTO_LIB_SHA3
select CRYPTO_LIB_SM3
help
Enable all the crypto library code that has KUnit tests.
Enable this only if you'd like to test all the crypto library code,
even code that wouldn't otherwise need to be built.
You'll still need to enable the tests themselves, either individually
or using KUNIT_ALL_TESTS.
config CRYPTO_LIB_BENCHMARK_VISIBLE
bool
config CRYPTO_LIB_BENCHMARK
bool "Include benchmarks in KUnit tests for cryptographic functions"
depends on CRYPTO_LIB_BENCHMARK_VISIBLE
help
Include benchmarks in the KUnit tests for cryptographic functions.
The benchmark results are printed to the kernel log when the
corresponding KUnit test suite runs.
This is useful for evaluating the performance of the cryptographic
functions. However, it will increase the runtime of the KUnit tests.
If you're only interested in correctness testing, leave this disabled.
+18
View File
@@ -0,0 +1,18 @@
# SPDX-License-Identifier: GPL-2.0-or-later
obj-$(CONFIG_CRYPTO_LIB_AES_CBC_MACS_KUNIT_TEST) += aes_cbc_macs_kunit.o
obj-$(CONFIG_CRYPTO_LIB_BLAKE2B_KUNIT_TEST) += blake2b_kunit.o
obj-$(CONFIG_CRYPTO_LIB_BLAKE2S_KUNIT_TEST) += blake2s_kunit.o
obj-$(CONFIG_CRYPTO_LIB_CHACHA20POLY1305_KUNIT_TEST) += chacha20poly1305_kunit.o
obj-$(CONFIG_CRYPTO_LIB_CURVE25519_KUNIT_TEST) += curve25519_kunit.o
obj-$(CONFIG_CRYPTO_LIB_GHASH_KUNIT_TEST) += ghash_kunit.o
obj-$(CONFIG_CRYPTO_LIB_MD5_KUNIT_TEST) += md5_kunit.o
obj-$(CONFIG_CRYPTO_LIB_MLDSA_KUNIT_TEST) += mldsa_kunit.o
obj-$(CONFIG_CRYPTO_LIB_NH_KUNIT_TEST) += nh_kunit.o
obj-$(CONFIG_CRYPTO_LIB_POLY1305_KUNIT_TEST) += poly1305_kunit.o
obj-$(CONFIG_CRYPTO_LIB_POLYVAL_KUNIT_TEST) += polyval_kunit.o
obj-$(CONFIG_CRYPTO_LIB_SHA1_KUNIT_TEST) += sha1_kunit.o
obj-$(CONFIG_CRYPTO_LIB_SHA256_KUNIT_TEST) += sha224_kunit.o sha256_kunit.o
obj-$(CONFIG_CRYPTO_LIB_SHA512_KUNIT_TEST) += sha384_kunit.o sha512_kunit.o
obj-$(CONFIG_CRYPTO_LIB_SHA3_KUNIT_TEST) += sha3_kunit.o
obj-$(CONFIG_CRYPTO_LIB_SM3_KUNIT_TEST) += sm3_kunit.o
@@ -0,0 +1,181 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */
/* This file was generated by: ./scripts/crypto/gen-hash-testvecs.py aes-cmac */
static const struct {
size_t data_len;
u8 digest[AES_BLOCK_SIZE];
} hash_testvecs[] = {
{
.data_len = 0,
.digest = {
0x9a, 0xeb, 0x94, 0xc1, 0xe9, 0xc1, 0x57, 0x49,
0x7e, 0xba, 0x66, 0x47, 0x9f, 0x03, 0x2c, 0x5b,
},
},
{
.data_len = 1,
.digest = {
0x52, 0xef, 0x65, 0xda, 0x7b, 0x92, 0x0c, 0x0f,
0xdd, 0xd6, 0xb9, 0x68, 0x3f, 0xcc, 0x5f, 0xea,
},
},
{
.data_len = 2,
.digest = {
0xc3, 0x95, 0x15, 0xea, 0x16, 0x33, 0xbe, 0x5a,
0xd4, 0x2c, 0x25, 0x06, 0x15, 0xc6, 0x10, 0x19,
},
},
{
.data_len = 3,
.digest = {
0x82, 0x41, 0x41, 0xd5, 0x33, 0x26, 0x0b, 0xb6,
0xc8, 0xf7, 0x8d, 0x76, 0x8a, 0xfc, 0x0e, 0xe4,
},
},
{
.data_len = 16,
.digest = {
0x94, 0x09, 0x80, 0x07, 0xba, 0x7c, 0xed, 0xd2,
0x74, 0x72, 0x30, 0x26, 0xb5, 0x11, 0x64, 0xc1,
},
},
{
.data_len = 32,
.digest = {
0xeb, 0xcf, 0x1e, 0x67, 0x21, 0x64, 0x93, 0xa0,
0xea, 0xc4, 0xb9, 0x2d, 0x55, 0xc8, 0xac, 0x99,
},
},
{
.data_len = 48,
.digest = {
0xd0, 0xd6, 0xdb, 0xe2, 0x45, 0x98, 0x6a, 0x7a,
0x5f, 0xd6, 0xcd, 0x9d, 0x12, 0x26, 0x20, 0x87,
},
},
{
.data_len = 49,
.digest = {
0x63, 0x25, 0x3c, 0xe2, 0x2a, 0xfa, 0xe3, 0x1e,
0x54, 0x10, 0x18, 0x28, 0xc6, 0xb8, 0xcb, 0x58,
},
},
{
.data_len = 63,
.digest = {
0x4d, 0xab, 0xae, 0x99, 0x90, 0x13, 0x3f, 0x4f,
0x42, 0x0f, 0x19, 0x94, 0xa2, 0x52, 0xfd, 0xaf,
},
},
{
.data_len = 64,
.digest = {
0xf7, 0x49, 0xb9, 0xa7, 0xf9, 0x3e, 0xa0, 0xca,
0xb2, 0x6c, 0xd7, 0x87, 0x7d, 0x1e, 0xd2, 0xcb,
},
},
{
.data_len = 65,
.digest = {
0x27, 0x2c, 0xb7, 0xc8, 0xdd, 0x26, 0xa9, 0xfe,
0x37, 0x64, 0x84, 0x38, 0xa5, 0x7e, 0xbc, 0x04,
},
},
{
.data_len = 127,
.digest = {
0xfd, 0x1f, 0x01, 0xa4, 0xea, 0x9b, 0xbd, 0xef,
0x09, 0x97, 0x57, 0x60, 0x95, 0x23, 0xcc, 0x71,
},
},
{
.data_len = 128,
.digest = {
0x28, 0x21, 0xee, 0x56, 0x9f, 0x38, 0xd6, 0x0e,
0xe3, 0x22, 0x06, 0x20, 0xad, 0xd8, 0x33, 0x74,
},
},
{
.data_len = 129,
.digest = {
0x07, 0x28, 0x4a, 0x2a, 0xd3, 0x85, 0xa6, 0x87,
0x5c, 0x01, 0x8c, 0xb9, 0xd3, 0x4b, 0xce, 0x20,
},
},
{
.data_len = 256,
.digest = {
0xe6, 0x12, 0x25, 0x6b, 0xf9, 0x69, 0x4d, 0x5a,
0x1a, 0xb0, 0xe6, 0x11, 0x46, 0x24, 0x08, 0xdf,
},
},
{
.data_len = 511,
.digest = {
0xce, 0x28, 0x1f, 0x14, 0xb9, 0xcc, 0x7e, 0x1f,
0xb5, 0x13, 0x2b, 0x45, 0x04, 0x54, 0xe9, 0x5f,
},
},
{
.data_len = 513,
.digest = {
0x63, 0x12, 0xbd, 0x85, 0x60, 0x1b, 0x99, 0x7e,
0x0a, 0xf7, 0x0f, 0xc1, 0xb5, 0x66, 0xf8, 0x9a,
},
},
{
.data_len = 1000,
.digest = {
0xbd, 0x49, 0x5e, 0x21, 0xc6, 0x58, 0x74, 0x6b,
0x21, 0xc2, 0x62, 0x6a, 0x15, 0xca, 0x1d, 0x8a,
},
},
{
.data_len = 3333,
.digest = {
0xfe, 0x6b, 0xfa, 0xfc, 0x4c, 0x0b, 0x63, 0x0d,
0x41, 0x7f, 0xa9, 0xd8, 0xba, 0xe3, 0xce, 0xce,
},
},
{
.data_len = 4096,
.digest = {
0x41, 0x7c, 0xbc, 0x2e, 0x2f, 0xff, 0xdf, 0x09,
0x31, 0xc5, 0x79, 0x0a, 0x1d, 0x6e, 0x46, 0xec,
},
},
{
.data_len = 4128,
.digest = {
0x6a, 0x9d, 0x86, 0xa8, 0xab, 0xa5, 0xc1, 0xc5,
0x0d, 0x54, 0xf3, 0x51, 0x10, 0x46, 0x25, 0x5a,
},
},
{
.data_len = 4160,
.digest = {
0x4c, 0x50, 0xff, 0x2a, 0xe9, 0xca, 0x9e, 0x07,
0x8a, 0x86, 0x67, 0x5e, 0xe5, 0x0a, 0xfd, 0x69,
},
},
{
.data_len = 4224,
.digest = {
0x3a, 0xfa, 0x80, 0x9d, 0x80, 0xe3, 0x1e, 0x95,
0x53, 0x93, 0x39, 0x17, 0xd3, 0xda, 0x49, 0x15,
},
},
{
.data_len = 16384,
.digest = {
0x48, 0xf4, 0x4f, 0x2d, 0x5d, 0xf2, 0x02, 0xcf,
0x51, 0x3c, 0x1b, 0x12, 0x80, 0x8f, 0xb0, 0xd6,
},
},
};
static const u8 hash_testvec_consolidated[AES_BLOCK_SIZE] = {
0x41, 0xad, 0x25, 0xa1, 0xeb, 0xce, 0x6b, 0x9c,
0x06, 0xdf, 0x47, 0xc4, 0x3a, 0x59, 0x50, 0x07,
};
@@ -0,0 +1,228 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright 2026 Google LLC
*/
#include <crypto/aes-cbc-macs.h>
#include "aes-cmac-testvecs.h"
/*
* A fixed key used when presenting AES-CMAC as an unkeyed hash function in
* order to reuse hash-test-template.h. At the beginning of the test suite,
* this is initialized to a key prepared from bytes generated from a fixed seed.
*/
static struct aes_cmac_key test_key;
static void aes_cmac_init_withtestkey(struct aes_cmac_ctx *ctx)
{
aes_cmac_init(ctx, &test_key);
}
static void aes_cmac_withtestkey(const u8 *data, size_t data_len,
u8 out[AES_BLOCK_SIZE])
{
aes_cmac(&test_key, data, data_len, out);
}
#define HASH aes_cmac_withtestkey
#define HASH_CTX aes_cmac_ctx
#define HASH_SIZE AES_BLOCK_SIZE
#define HASH_INIT aes_cmac_init_withtestkey
#define HASH_UPDATE aes_cmac_update
#define HASH_FINAL aes_cmac_final
#include "hash-test-template.h"
static int aes_cbc_macs_suite_init(struct kunit_suite *suite)
{
u8 raw_key[AES_KEYSIZE_256];
int err;
rand_bytes_seeded_from_len(raw_key, sizeof(raw_key));
err = aes_cmac_preparekey(&test_key, raw_key, sizeof(raw_key));
if (err)
return err;
return hash_suite_init(suite);
}
static void aes_cbc_macs_suite_exit(struct kunit_suite *suite)
{
hash_suite_exit(suite);
}
/* Verify compatibility of the AES-CMAC implementation with RFC 4493. */
static void test_aes_cmac_rfc4493(struct kunit *test)
{
static const u8 raw_key[AES_KEYSIZE_128] = {
0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c,
};
static const struct {
size_t data_len;
const u8 data[40];
const u8 mac[AES_BLOCK_SIZE];
} testvecs[] = {
{
/* Example 1 from RFC 4493 */
.data_len = 0,
.mac = {
0xbb, 0x1d, 0x69, 0x29, 0xe9, 0x59, 0x37, 0x28,
0x7f, 0xa3, 0x7d, 0x12, 0x9b, 0x75, 0x67, 0x46,
},
},
{
/* Example 2 from RFC 4493 */
.data = {
0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
},
.data_len = 16,
.mac = {
0x07, 0x0a, 0x16, 0xb4, 0x6b, 0x4d, 0x41, 0x44,
0xf7, 0x9b, 0xdd, 0x9d, 0xd0, 0x4a, 0x28, 0x7c,
},
},
{
/* Example 3 from RFC 4493 */
.data = {
0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11,
},
.data_len = 40,
.mac = {
0xdf, 0xa6, 0x67, 0x47, 0xde, 0x9a, 0xe6, 0x30,
0x30, 0xca, 0x32, 0x61, 0x14, 0x97, 0xc8, 0x27,
},
},
};
struct aes_cmac_key key;
int err;
err = aes_cmac_preparekey(&key, raw_key, sizeof(raw_key));
KUNIT_ASSERT_EQ(test, err, 0);
for (size_t i = 0; i < ARRAY_SIZE(testvecs); i++) {
u8 mac[AES_BLOCK_SIZE];
aes_cmac(&key, testvecs[i].data, testvecs[i].data_len, mac);
KUNIT_ASSERT_MEMEQ(test, mac, testvecs[i].mac, AES_BLOCK_SIZE);
}
}
/*
* Verify compatibility of the AES-XCBC-MAC implementation with RFC 3566.
*
* Additional AES-XCBC-MAC tests are not necessary, since the AES-XCBC-MAC
* implementation is well covered by the AES-CMAC tests already. Only the key
* preparation function differs; the rest of the code is shared.
*/
static void test_aes_xcbcmac_rfc3566(struct kunit *test)
{
struct aes_cmac_key key;
/* AES-XCBC-MAC Test Case #4 from RFC 3566 */
static const u8 raw_key[AES_KEYSIZE_128] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
};
static const u8 message[20] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13,
};
static const u8 expected_mac[AES_BLOCK_SIZE] = {
0x47, 0xf5, 0x1b, 0x45, 0x64, 0x96, 0x62, 0x15,
0xb8, 0x98, 0x5c, 0x63, 0x05, 0x5e, 0xd3, 0x08,
};
u8 actual_mac[AES_BLOCK_SIZE];
aes_xcbcmac_preparekey(&key, raw_key);
aes_cmac(&key, message, sizeof(message), actual_mac);
KUNIT_ASSERT_MEMEQ(test, actual_mac, expected_mac, AES_BLOCK_SIZE);
}
static void test_aes_cbcmac_rfc3610(struct kunit *test)
{
/*
* The following AES-CBC-MAC test vector is extracted from RFC 3610
* Packet Vector #11. It required some rearrangement to get the actual
* input to AES-CBC-MAC from the values given.
*/
static const u8 raw_key[AES_KEYSIZE_128] = {
0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
};
const size_t unpadded_data_len = 52;
static const u8 data[64] = {
/* clang-format off */
/* CCM header */
0x61, 0x00, 0x00, 0x00, 0x0d, 0x0c, 0x0b, 0x0a,
0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0x00, 0x14,
/* CCM additional authentication blocks */
0x00, 0x0c, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x00, 0x00,
/* CCM message blocks */
0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13,
0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b,
0x1c, 0x1d, 0x1e, 0x1f, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* clang-format on */
};
static const u8 expected_mac[AES_BLOCK_SIZE] = {
0x6b, 0x5e, 0x24, 0x34, 0x12, 0xcc, 0xc2, 0xad,
0x6f, 0x1b, 0x11, 0xc3, 0xa1, 0xa9, 0xd8, 0xbc,
};
struct aes_enckey key;
struct aes_cbcmac_ctx ctx;
u8 actual_mac[AES_BLOCK_SIZE];
int err;
err = aes_prepareenckey(&key, raw_key, sizeof(raw_key));
KUNIT_ASSERT_EQ(test, err, 0);
/*
* Trailing zeroes should not affect the CBC-MAC value, up to the next
* AES block boundary.
*/
for (size_t data_len = unpadded_data_len; data_len <= sizeof(data);
data_len++) {
aes_cbcmac_init(&ctx, &key);
aes_cbcmac_update(&ctx, data, data_len);
aes_cbcmac_final(&ctx, actual_mac);
KUNIT_ASSERT_MEMEQ(test, actual_mac, expected_mac,
AES_BLOCK_SIZE);
/* Incremental computations should produce the same result. */
for (size_t part1_len = 0; part1_len <= data_len; part1_len++) {
aes_cbcmac_init(&ctx, &key);
aes_cbcmac_update(&ctx, data, part1_len);
aes_cbcmac_update(&ctx, &data[part1_len],
data_len - part1_len);
aes_cbcmac_final(&ctx, actual_mac);
KUNIT_ASSERT_MEMEQ(test, actual_mac, expected_mac,
AES_BLOCK_SIZE);
}
}
}
static struct kunit_case aes_cbc_macs_test_cases[] = {
HASH_KUNIT_CASES,
KUNIT_CASE(test_aes_cmac_rfc4493),
KUNIT_CASE(test_aes_xcbcmac_rfc3566),
KUNIT_CASE(test_aes_cbcmac_rfc3610),
KUNIT_CASE(benchmark_hash),
{},
};
static struct kunit_suite aes_cbc_macs_test_suite = {
.name = "aes_cbc_macs",
.test_cases = aes_cbc_macs_test_cases,
.suite_init = aes_cbc_macs_suite_init,
.suite_exit = aes_cbc_macs_suite_exit,
};
kunit_test_suite(aes_cbc_macs_test_suite);
MODULE_DESCRIPTION(
"KUnit tests and benchmark for AES-CMAC, AES-XCBC-MAC, and AES-CBC-MAC");
MODULE_IMPORT_NS("CRYPTO_INTERNAL");
MODULE_LICENSE("GPL");
@@ -0,0 +1,342 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */
/* This file was generated by: ./scripts/crypto/gen-hash-testvecs.py blake2b */
static const struct {
size_t data_len;
u8 digest[BLAKE2B_HASH_SIZE];
} hash_testvecs[] = {
{
.data_len = 0,
.digest = {
0x78, 0x6a, 0x02, 0xf7, 0x42, 0x01, 0x59, 0x03,
0xc6, 0xc6, 0xfd, 0x85, 0x25, 0x52, 0xd2, 0x72,
0x91, 0x2f, 0x47, 0x40, 0xe1, 0x58, 0x47, 0x61,
0x8a, 0x86, 0xe2, 0x17, 0xf7, 0x1f, 0x54, 0x19,
0xd2, 0x5e, 0x10, 0x31, 0xaf, 0xee, 0x58, 0x53,
0x13, 0x89, 0x64, 0x44, 0x93, 0x4e, 0xb0, 0x4b,
0x90, 0x3a, 0x68, 0x5b, 0x14, 0x48, 0xb7, 0x55,
0xd5, 0x6f, 0x70, 0x1a, 0xfe, 0x9b, 0xe2, 0xce,
},
},
{
.data_len = 1,
.digest = {
0x6f, 0x2e, 0xcc, 0x83, 0x53, 0xa3, 0x20, 0x16,
0x5b, 0xda, 0xd0, 0x04, 0xd3, 0xcb, 0xe4, 0x37,
0x5b, 0xf0, 0x84, 0x36, 0xe1, 0xad, 0x45, 0xcc,
0x4d, 0x7f, 0x09, 0x68, 0xb2, 0x62, 0x93, 0x7f,
0x72, 0x32, 0xe8, 0xa7, 0x2f, 0x1f, 0x6f, 0xc6,
0x14, 0xd6, 0x70, 0xae, 0x0c, 0xf0, 0xf3, 0xce,
0x64, 0x4d, 0x22, 0xdf, 0xc7, 0xa7, 0xf8, 0xa8,
0x18, 0x23, 0xd8, 0x6c, 0xaf, 0x65, 0xa2, 0x54,
},
},
{
.data_len = 2,
.digest = {
0x04, 0x13, 0xe2, 0x10, 0xbe, 0x65, 0xde, 0xce,
0x61, 0xa8, 0xe0, 0xd6, 0x35, 0xb1, 0xb8, 0x88,
0xd2, 0xea, 0x45, 0x3a, 0xe1, 0x8d, 0x94, 0xb5,
0x66, 0x06, 0x98, 0x96, 0x39, 0xf8, 0x0e, 0xcb,
0x34, 0xa6, 0xa8, 0x17, 0xfe, 0x56, 0xbc, 0xa9,
0x5e, 0x1b, 0xb1, 0xde, 0x3c, 0xc7, 0x78, 0x4f,
0x39, 0xc6, 0xfc, 0xa8, 0xb3, 0x27, 0x66, 0x3e,
0x4e, 0xb5, 0x5d, 0x08, 0x89, 0xee, 0xd1, 0xe0,
},
},
{
.data_len = 3,
.digest = {
0x2b, 0x4a, 0xa3, 0x4e, 0x2b, 0x7a, 0x47, 0x20,
0x30, 0x5b, 0x09, 0x17, 0x3a, 0xf4, 0xcc, 0xf0,
0xf7, 0x7b, 0x97, 0x68, 0x98, 0x9f, 0x4f, 0x09,
0x46, 0x25, 0xe7, 0xd6, 0x53, 0x6b, 0xf9, 0x68,
0x48, 0x12, 0x44, 0x8c, 0x9a, 0xc8, 0xd4, 0x42,
0xeb, 0x2c, 0x5f, 0x41, 0xba, 0x17, 0xd0, 0xc3,
0xad, 0xfd, 0xfb, 0x42, 0x33, 0xcb, 0x08, 0x5d,
0xd2, 0x5c, 0x3d, 0xde, 0x87, 0x4d, 0xd6, 0xe4,
},
},
{
.data_len = 16,
.digest = {
0xbf, 0x40, 0xf2, 0x38, 0x44, 0x8e, 0x24, 0x5e,
0xbc, 0x67, 0xbb, 0xf0, 0x10, 0x9a, 0x79, 0xbb,
0x36, 0x55, 0xce, 0xd2, 0xba, 0x04, 0x0d, 0xe8,
0x30, 0x29, 0x5c, 0x2a, 0xa6, 0x3a, 0x4f, 0x37,
0xac, 0x5f, 0xd4, 0x13, 0xa2, 0xf4, 0xfe, 0x80,
0x61, 0xd7, 0x58, 0x66, 0x0c, 0x7f, 0xa2, 0x56,
0x6b, 0x52, 0x7c, 0x22, 0x73, 0x7f, 0x17, 0xaa,
0x91, 0x5a, 0x22, 0x06, 0xd9, 0x00, 0x48, 0x12,
},
},
{
.data_len = 32,
.digest = {
0x41, 0x04, 0x65, 0x93, 0x81, 0x9a, 0x20, 0x0a,
0x00, 0x60, 0x00, 0x64, 0x4c, 0x04, 0x3d, 0xe0,
0x6b, 0x17, 0x0c, 0xe1, 0x0e, 0x28, 0x8b, 0xa0,
0x76, 0xd2, 0x79, 0xb0, 0x33, 0x60, 0x61, 0x27,
0xf2, 0x64, 0xf1, 0x8a, 0xe5, 0x3e, 0xaa, 0x37,
0x60, 0xad, 0x2d, 0x75, 0x13, 0xae, 0xd8, 0x9e,
0xec, 0xe0, 0xe4, 0x40, 0x2f, 0x59, 0x44, 0xb0,
0x66, 0x7a, 0x68, 0x38, 0xce, 0x21, 0x99, 0x2a,
},
},
{
.data_len = 48,
.digest = {
0x19, 0x6f, 0x9d, 0xc7, 0x87, 0x12, 0x5c, 0xa3,
0xe2, 0xd3, 0xf1, 0x82, 0xec, 0xf3, 0x55, 0x9c,
0x86, 0xd1, 0x6d, 0xde, 0xcf, 0x5b, 0xec, 0x4c,
0x43, 0x25, 0x85, 0x90, 0xef, 0xe8, 0xe3, 0x5f,
0x2c, 0x3a, 0x84, 0x07, 0xb8, 0x55, 0xfd, 0x5e,
0xa4, 0x45, 0xf2, 0xac, 0xe4, 0xbd, 0xc7, 0x96,
0x80, 0x59, 0x3e, 0xc9, 0xb1, 0x60, 0xb1, 0x2b,
0x17, 0x49, 0x7d, 0x3e, 0x7d, 0x4d, 0x70, 0x24,
},
},
{
.data_len = 49,
.digest = {
0x73, 0x72, 0xd5, 0x0a, 0x97, 0xb4, 0x7d, 0xdb,
0x05, 0x14, 0x8e, 0x40, 0xc2, 0x9a, 0x8a, 0x74,
0x4b, 0xda, 0x7e, 0xfc, 0x97, 0x57, 0x23, 0x39,
0xdc, 0x57, 0x09, 0x13, 0x24, 0xfc, 0xf3, 0x23,
0x55, 0x48, 0xdd, 0xe5, 0x07, 0x9a, 0x6f, 0x7b,
0x62, 0xea, 0x4d, 0x79, 0xb4, 0xb9, 0xc5, 0x86,
0xc0, 0x34, 0xd6, 0xd2, 0x6c, 0xc3, 0x94, 0xfb,
0x34, 0xd6, 0x62, 0xae, 0xb8, 0x99, 0xf1, 0x38,
},
},
{
.data_len = 63,
.digest = {
0x42, 0x3a, 0xe3, 0xa2, 0xae, 0x5a, 0x28, 0xce,
0xf1, 0x3c, 0x97, 0xc2, 0x34, 0xf6, 0xb5, 0x1e,
0xfc, 0x31, 0xb4, 0x04, 0x61, 0xb7, 0x54, 0x0b,
0x0d, 0x1a, 0x22, 0x9c, 0x04, 0x67, 0x5c, 0x4c,
0x75, 0x1b, 0x10, 0x0b, 0x99, 0xe2, 0xb1, 0x5e,
0x5d, 0x4b, 0x7a, 0xe6, 0xf6, 0xb5, 0x62, 0xee,
0x2d, 0x44, 0x57, 0xb2, 0x96, 0x73, 0x5e, 0xb9,
0x6a, 0xb2, 0xb3, 0x16, 0xa3, 0xd9, 0x6a, 0x60,
},
},
{
.data_len = 64,
.digest = {
0x50, 0xb9, 0xbe, 0xb2, 0x69, 0x07, 0x45, 0x5b,
0x59, 0xde, 0x8d, 0xbf, 0x08, 0xdc, 0x2e, 0x7f,
0x93, 0x29, 0xc1, 0x91, 0xe8, 0x74, 0x03, 0x89,
0x20, 0xfb, 0xb2, 0x4b, 0xe8, 0x68, 0x6f, 0xe1,
0xb4, 0x30, 0xbe, 0x11, 0x3c, 0x43, 0x19, 0x66,
0x72, 0x78, 0xb7, 0xf4, 0xe9, 0x09, 0x18, 0x4e,
0xae, 0x4a, 0x24, 0xe0, 0x6f, 0x44, 0x02, 0xe3,
0xfd, 0xda, 0xb3, 0x3e, 0x3c, 0x6d, 0x54, 0x2e,
},
},
{
.data_len = 65,
.digest = {
0xd6, 0xf2, 0xa9, 0x61, 0x3f, 0xce, 0x2a, 0x68,
0x19, 0x86, 0xff, 0xd1, 0xee, 0x89, 0x3b, 0xa4,
0x10, 0x9a, 0x91, 0x50, 0x35, 0x48, 0x9e, 0xf5,
0x9c, 0x95, 0xe0, 0xfb, 0x92, 0x0f, 0xa8, 0xf7,
0x6c, 0x43, 0x85, 0xf1, 0x6e, 0x11, 0x4e, 0x67,
0x78, 0xd7, 0x53, 0x25, 0x0c, 0xf8, 0xce, 0x38,
0x74, 0x08, 0xb0, 0x3c, 0x53, 0x20, 0x4d, 0xc4,
0x9a, 0xf5, 0x78, 0xe8, 0x41, 0x8f, 0xed, 0x1f,
},
},
{
.data_len = 127,
.digest = {
0xe8, 0xb2, 0xc5, 0xa7, 0xf5, 0xfa, 0xee, 0xa0,
0x57, 0xba, 0x58, 0xf9, 0x0a, 0xf2, 0x64, 0x16,
0xa8, 0xa6, 0x03, 0x85, 0x3b, 0xb8, 0x6f, 0xca,
0x76, 0xc3, 0xa1, 0x2b, 0xec, 0xef, 0xc4, 0x66,
0x11, 0xdf, 0x03, 0x85, 0x9d, 0x0c, 0x37, 0x7b,
0xa9, 0x7b, 0x44, 0xfb, 0x11, 0x8f, 0x3f, 0x71,
0xcd, 0x81, 0x43, 0x2e, 0x71, 0x5c, 0x54, 0x9f,
0xca, 0x0f, 0x01, 0x91, 0xca, 0xaa, 0x93, 0xe9,
},
},
{
.data_len = 128,
.digest = {
0x05, 0x8e, 0x9d, 0xdc, 0xe9, 0x36, 0x3e, 0x73,
0x63, 0x59, 0x69, 0x81, 0x0b, 0x8c, 0xc7, 0x9e,
0xcc, 0xe7, 0x9c, 0x19, 0x54, 0xa7, 0x2f, 0x86,
0xb5, 0xea, 0xae, 0x6d, 0xfe, 0x4e, 0x6e, 0x83,
0x8d, 0x1a, 0x1c, 0x70, 0x3f, 0x34, 0xa1, 0x04,
0x59, 0xd1, 0xbb, 0xaa, 0x58, 0xf7, 0xce, 0xfb,
0x86, 0x66, 0x22, 0xfc, 0x78, 0x74, 0x6e, 0x85,
0xf1, 0x59, 0x7d, 0x9e, 0x1c, 0x3b, 0xc6, 0x65,
},
},
{
.data_len = 129,
.digest = {
0x6b, 0x1f, 0x7c, 0x9a, 0x65, 0x7f, 0x09, 0x61,
0xe5, 0x04, 0x9a, 0xf1, 0x4b, 0x36, 0x8e, 0x41,
0x86, 0xcf, 0x86, 0x19, 0xd8, 0xc9, 0x34, 0x70,
0x67, 0xd1, 0x03, 0x72, 0x12, 0xf7, 0x27, 0x92,
0x2e, 0x3d, 0x2b, 0x54, 0x9a, 0x48, 0xa4, 0xc2,
0x61, 0xea, 0x6a, 0xe8, 0xdd, 0x07, 0x41, 0x85,
0x58, 0x6d, 0xcd, 0x12, 0x0d, 0xbc, 0xb1, 0x23,
0xb2, 0xdb, 0x24, 0x1f, 0xc4, 0xa7, 0xae, 0xda,
},
},
{
.data_len = 256,
.digest = {
0x50, 0xd8, 0xdc, 0xb2, 0x50, 0x24, 0x7a, 0x49,
0xb1, 0x00, 0x73, 0x16, 0x1f, 0xce, 0xf9, 0xe8,
0x77, 0x0a, 0x27, 0x74, 0xc7, 0xeb, 0xf0, 0x62,
0xb9, 0xf3, 0x24, 0xa6, 0x03, 0x18, 0x40, 0xde,
0x9b, 0x1d, 0xa8, 0xd0, 0xbf, 0x66, 0xa3, 0xc1,
0x31, 0x04, 0x95, 0xc7, 0xc3, 0xb7, 0x11, 0xe2,
0x1e, 0x31, 0x49, 0x98, 0x06, 0xab, 0xf0, 0xe6,
0x5c, 0xac, 0x88, 0x28, 0x0b, 0x3d, 0xb2, 0xc2,
},
},
{
.data_len = 511,
.digest = {
0xd4, 0x2b, 0x6b, 0x9e, 0xfc, 0x44, 0xc0, 0x90,
0x64, 0x77, 0x5d, 0xf3, 0x44, 0xb6, 0x92, 0x8f,
0x80, 0xe2, 0xe4, 0x9b, 0xaf, 0x49, 0x04, 0xea,
0x29, 0xf7, 0x4a, 0x33, 0x3f, 0xc7, 0x3b, 0xab,
0xa1, 0x71, 0x7f, 0xa2, 0x8e, 0x03, 0xa0, 0xd6,
0xa7, 0xcd, 0xe0, 0xf8, 0xd7, 0x3b, 0xa4, 0x0d,
0x84, 0x79, 0x12, 0x72, 0x3f, 0x8e, 0x48, 0x35,
0x76, 0x4f, 0x56, 0xe9, 0x21, 0x40, 0x19, 0xbe,
},
},
{
.data_len = 513,
.digest = {
0x84, 0xd4, 0xd8, 0x6c, 0x60, 0x3d, 0x6e, 0xfd,
0x84, 0xb7, 0xdf, 0xba, 0x13, 0x5e, 0x07, 0x94,
0x5b, 0x6b, 0x62, 0x1d, 0x82, 0x02, 0xa7, 0xb3,
0x21, 0xdf, 0x42, 0x20, 0x85, 0xa8, 0x6f, 0x30,
0xf7, 0x03, 0xba, 0x66, 0x0e, 0xa6, 0x42, 0x21,
0x37, 0xe8, 0xed, 0x5b, 0x22, 0xf5, 0x4e, 0xa5,
0xe5, 0x80, 0x1b, 0x47, 0xf0, 0x49, 0xb3, 0xe5,
0x6e, 0xd9, 0xd9, 0x95, 0x3d, 0x2e, 0x42, 0x13,
},
},
{
.data_len = 1000,
.digest = {
0x71, 0x17, 0xab, 0x93, 0xfe, 0x3b, 0xa4, 0xe6,
0xcb, 0xb0, 0xea, 0x95, 0xe7, 0x1a, 0x01, 0xc0,
0x12, 0x33, 0xfe, 0xcc, 0x79, 0x15, 0xae, 0x56,
0xd2, 0x70, 0x44, 0x60, 0x54, 0x42, 0xa8, 0x69,
0x7e, 0xc3, 0x90, 0xa0, 0x0c, 0x63, 0x39, 0xff,
0x55, 0x53, 0xb8, 0x46, 0xef, 0x06, 0xcb, 0xba,
0x73, 0xf4, 0x76, 0x22, 0xf1, 0x60, 0x98, 0xbc,
0xbf, 0x76, 0x95, 0x85, 0x13, 0x1d, 0x11, 0x3b,
},
},
{
.data_len = 3333,
.digest = {
0x3a, 0xaa, 0x85, 0xa0, 0x8c, 0x8e, 0xe1, 0x9c,
0x9b, 0x43, 0x72, 0x7f, 0x40, 0x88, 0x3b, 0xd1,
0xc4, 0xd8, 0x2b, 0x69, 0xa6, 0x74, 0x47, 0x69,
0x5f, 0x7d, 0xab, 0x75, 0xa9, 0xf9, 0x88, 0x54,
0xce, 0x57, 0xcc, 0x9d, 0xac, 0x13, 0x91, 0xdb,
0x6d, 0x5c, 0xd8, 0xf4, 0x35, 0xc9, 0x30, 0xf0,
0x4b, 0x91, 0x25, 0xab, 0x92, 0xa8, 0xc8, 0x6f,
0xa0, 0xeb, 0x71, 0x56, 0x95, 0xab, 0xfd, 0xd7,
},
},
{
.data_len = 4096,
.digest = {
0xe1, 0xe9, 0xbe, 0x6c, 0x96, 0xe2, 0xe8, 0xa6,
0x53, 0xcd, 0x79, 0x77, 0x57, 0x51, 0x2f, 0xb2,
0x9f, 0xfc, 0x09, 0xaa, 0x2c, 0xbc, 0x6c, 0x5f,
0xb0, 0xf2, 0x12, 0x39, 0x54, 0xd7, 0x27, 0xf8,
0x33, 0x5d, 0xd4, 0x8a, 0xca, 0xd8, 0x2e, 0xbb,
0x02, 0x82, 0xca, 0x1b, 0x54, 0xfa, 0xd6, 0xf4,
0x49, 0x63, 0xfc, 0xc8, 0x73, 0xd4, 0x26, 0x8d,
0x4f, 0x1c, 0x56, 0xa7, 0xf4, 0x58, 0x6f, 0x51,
},
},
{
.data_len = 4128,
.digest = {
0xf2, 0xf6, 0xe1, 0x16, 0x98, 0x69, 0x74, 0x5f,
0x6c, 0xc4, 0x9d, 0x34, 0xa2, 0x84, 0x5d, 0x47,
0xac, 0x39, 0xe0, 0x14, 0x2d, 0x78, 0xfa, 0x27,
0xd5, 0x18, 0xaf, 0x26, 0x89, 0xa4, 0x69, 0xd3,
0x56, 0xde, 0xfe, 0x4b, 0x9f, 0x0c, 0x9d, 0x5a,
0x9a, 0x73, 0x3e, 0x3c, 0x76, 0x4b, 0x96, 0xca,
0x49, 0xda, 0x05, 0x8c, 0x53, 0xbb, 0x85, 0x89,
0x60, 0xc7, 0xe0, 0xb3, 0x51, 0x18, 0xd2, 0xd2,
},
},
{
.data_len = 4160,
.digest = {
0xfc, 0x5c, 0xcf, 0xbf, 0x29, 0xe3, 0x01, 0xef,
0x4b, 0x40, 0x70, 0x01, 0xca, 0x4d, 0x46, 0xce,
0xa9, 0x95, 0x5d, 0xb4, 0xf1, 0x79, 0x29, 0xdb,
0xac, 0x32, 0x3d, 0xd9, 0x60, 0x9e, 0x6b, 0xb8,
0x28, 0x62, 0xb7, 0x4a, 0xbb, 0x33, 0xb9, 0xd0,
0x83, 0xe0, 0xd7, 0x5a, 0x2d, 0x01, 0x4c, 0x61,
0x9e, 0x7d, 0x2d, 0x2d, 0x60, 0x29, 0x5e, 0x60,
0x10, 0xb7, 0x41, 0x00, 0x3f, 0xe5, 0xf7, 0x52,
},
},
{
.data_len = 4224,
.digest = {
0xf8, 0xe5, 0x4b, 0xe5, 0x89, 0xf9, 0x1b, 0x43,
0xbb, 0x65, 0x3d, 0xa0, 0xb4, 0xdc, 0x04, 0x26,
0x68, 0x15, 0xae, 0x4d, 0xd6, 0x03, 0xb7, 0x27,
0x06, 0x8c, 0x2a, 0x82, 0x51, 0x96, 0xbf, 0x83,
0x38, 0x96, 0x21, 0x8a, 0xd9, 0xf9, 0x4e, 0x38,
0xc6, 0xb3, 0xbd, 0xfe, 0xd3, 0x49, 0x90, 0xbc,
0xa1, 0x77, 0xd0, 0xa0, 0x3c, 0x2b, 0x4e, 0x10,
0x34, 0xc3, 0x17, 0x85, 0x3d, 0xec, 0xa8, 0x05,
},
},
{
.data_len = 16384,
.digest = {
0x38, 0x56, 0xaf, 0x83, 0x68, 0x9c, 0xba, 0xe3,
0xec, 0x51, 0xf5, 0xf4, 0x93, 0x48, 0x1d, 0xe6,
0xad, 0xa8, 0x8c, 0x70, 0x2a, 0xd9, 0xaa, 0x43,
0x04, 0x40, 0x95, 0xc1, 0xe6, 0x8a, 0xf5, 0x01,
0x6b, 0x79, 0xd9, 0xb4, 0xd0, 0x1d, 0x93, 0x26,
0xfe, 0xf5, 0x07, 0x57, 0xda, 0x08, 0x0a, 0x82,
0xc9, 0x17, 0x13, 0x5b, 0x9e, 0x11, 0x96, 0xa5,
0xd0, 0x92, 0xcd, 0xf1, 0xa3, 0x5b, 0x43, 0x21,
},
},
};
static const u8 hash_testvec_consolidated[BLAKE2B_HASH_SIZE] = {
0xa4, 0xf8, 0xf6, 0xa1, 0x36, 0x89, 0xc0, 0x2a,
0xc3, 0x42, 0x32, 0x71, 0xe5, 0xea, 0x14, 0x77,
0xf3, 0x99, 0x91, 0x87, 0x49, 0xc2, 0x8d, 0xa5,
0x2f, 0xed, 0x01, 0x35, 0x39, 0x64, 0x09, 0x25,
0xe3, 0xa8, 0x50, 0x97, 0x35, 0x8b, 0xf5, 0x19,
0x1e, 0xd5, 0x9f, 0x03, 0x0b, 0x65, 0x55, 0x0e,
0xa0, 0xb7, 0xda, 0x18, 0x7b, 0x7f, 0x88, 0x55,
0x1f, 0xdb, 0x82, 0x6b, 0x98, 0x90, 0x1c, 0xdd,
};
static const u8 blake2b_keyed_testvec_consolidated[BLAKE2B_HASH_SIZE] = {
0x2b, 0x89, 0x36, 0x3a, 0x36, 0xe4, 0x18, 0x38,
0xc4, 0x5b, 0x5c, 0xa5, 0x9a, 0xed, 0xf2, 0xee,
0x5a, 0xb6, 0x82, 0x6c, 0x63, 0xf2, 0x29, 0x57,
0xc7, 0xd5, 0x32, 0x27, 0xba, 0x88, 0xb1, 0xab,
0xf2, 0x2a, 0xc1, 0xea, 0xf3, 0x91, 0x89, 0x66,
0x47, 0x1e, 0x5b, 0xc6, 0x98, 0x12, 0xe9, 0x25,
0xbf, 0x72, 0xd2, 0x3f, 0x88, 0x97, 0x17, 0x51,
0xed, 0x96, 0xfb, 0xe9, 0xca, 0x52, 0x42, 0xc9,
};
@@ -0,0 +1,133 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright 2025 Google LLC
*/
#include <crypto/blake2b.h>
#include "blake2b-testvecs.h"
/*
* The following are compatibility functions that present BLAKE2b as an unkeyed
* hash function that produces hashes of fixed length BLAKE2B_HASH_SIZE, so that
* hash-test-template.h can be reused to test it.
*/
static void blake2b_default(const u8 *data, size_t len,
u8 out[BLAKE2B_HASH_SIZE])
{
blake2b(NULL, 0, data, len, out, BLAKE2B_HASH_SIZE);
}
static void blake2b_init_default(struct blake2b_ctx *ctx)
{
blake2b_init(ctx, BLAKE2B_HASH_SIZE);
}
/*
* Generate the HASH_KUNIT_CASES using hash-test-template.h. These test BLAKE2b
* with a key length of 0 and a hash length of BLAKE2B_HASH_SIZE.
*/
#define HASH blake2b_default
#define HASH_CTX blake2b_ctx
#define HASH_SIZE BLAKE2B_HASH_SIZE
#define HASH_INIT blake2b_init_default
#define HASH_UPDATE blake2b_update
#define HASH_FINAL blake2b_final
#include "hash-test-template.h"
/*
* BLAKE2b specific test case which tests all possible combinations of key
* length and hash length.
*/
static void test_blake2b_all_key_and_hash_lens(struct kunit *test)
{
const size_t data_len = 100;
u8 *data = &test_buf[0];
u8 *key = data + data_len;
u8 *hash = key + BLAKE2B_KEY_SIZE;
struct blake2b_ctx main_ctx;
u8 main_hash[BLAKE2B_HASH_SIZE];
rand_bytes_seeded_from_len(data, data_len);
blake2b_init(&main_ctx, BLAKE2B_HASH_SIZE);
for (int key_len = 0; key_len <= BLAKE2B_KEY_SIZE; key_len++) {
rand_bytes_seeded_from_len(key, key_len);
for (int out_len = 1; out_len <= BLAKE2B_HASH_SIZE; out_len++) {
blake2b(key, key_len, data, data_len, hash, out_len);
blake2b_update(&main_ctx, hash, out_len);
}
}
blake2b_final(&main_ctx, main_hash);
KUNIT_ASSERT_MEMEQ(test, main_hash, blake2b_keyed_testvec_consolidated,
BLAKE2B_HASH_SIZE);
}
/*
* BLAKE2b specific test case which tests using a guarded buffer for all allowed
* key lengths. Also tests both blake2b() and blake2b_init_key().
*/
static void test_blake2b_with_guarded_key_buf(struct kunit *test)
{
const size_t data_len = 100;
rand_bytes(test_buf, data_len);
for (int key_len = 0; key_len <= BLAKE2B_KEY_SIZE; key_len++) {
u8 key[BLAKE2B_KEY_SIZE];
u8 *guarded_key = &test_buf[TEST_BUF_LEN - key_len];
u8 hash1[BLAKE2B_HASH_SIZE];
u8 hash2[BLAKE2B_HASH_SIZE];
struct blake2b_ctx ctx;
rand_bytes(key, key_len);
memcpy(guarded_key, key, key_len);
blake2b(key, key_len, test_buf, data_len,
hash1, BLAKE2B_HASH_SIZE);
blake2b(guarded_key, key_len, test_buf, data_len,
hash2, BLAKE2B_HASH_SIZE);
KUNIT_ASSERT_MEMEQ(test, hash1, hash2, BLAKE2B_HASH_SIZE);
blake2b_init_key(&ctx, BLAKE2B_HASH_SIZE, guarded_key, key_len);
blake2b_update(&ctx, test_buf, data_len);
blake2b_final(&ctx, hash2);
KUNIT_ASSERT_MEMEQ(test, hash1, hash2, BLAKE2B_HASH_SIZE);
}
}
/*
* BLAKE2b specific test case which tests using a guarded output buffer for all
* allowed output lengths.
*/
static void test_blake2b_with_guarded_out_buf(struct kunit *test)
{
const size_t data_len = 100;
rand_bytes(test_buf, data_len);
for (int out_len = 1; out_len <= BLAKE2B_HASH_SIZE; out_len++) {
u8 hash[BLAKE2B_HASH_SIZE];
u8 *guarded_hash = &test_buf[TEST_BUF_LEN - out_len];
blake2b(NULL, 0, test_buf, data_len, hash, out_len);
blake2b(NULL, 0, test_buf, data_len, guarded_hash, out_len);
KUNIT_ASSERT_MEMEQ(test, hash, guarded_hash, out_len);
}
}
static struct kunit_case blake2b_test_cases[] = {
HASH_KUNIT_CASES,
KUNIT_CASE(test_blake2b_all_key_and_hash_lens),
KUNIT_CASE(test_blake2b_with_guarded_key_buf),
KUNIT_CASE(test_blake2b_with_guarded_out_buf),
KUNIT_CASE(benchmark_hash),
{},
};
static struct kunit_suite blake2b_test_suite = {
.name = "blake2b",
.test_cases = blake2b_test_cases,
.suite_init = hash_suite_init,
.suite_exit = hash_suite_exit,
};
kunit_test_suite(blake2b_test_suite);
MODULE_DESCRIPTION("KUnit tests and benchmark for BLAKE2b");
MODULE_LICENSE("GPL");
@@ -0,0 +1,238 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */
/* This file was generated by: ./scripts/crypto/gen-hash-testvecs.py blake2s */
static const struct {
size_t data_len;
u8 digest[BLAKE2S_HASH_SIZE];
} hash_testvecs[] = {
{
.data_len = 0,
.digest = {
0x69, 0x21, 0x7a, 0x30, 0x79, 0x90, 0x80, 0x94,
0xe1, 0x11, 0x21, 0xd0, 0x42, 0x35, 0x4a, 0x7c,
0x1f, 0x55, 0xb6, 0x48, 0x2c, 0xa1, 0xa5, 0x1e,
0x1b, 0x25, 0x0d, 0xfd, 0x1e, 0xd0, 0xee, 0xf9,
},
},
{
.data_len = 1,
.digest = {
0x7c, 0xab, 0x53, 0xe2, 0x48, 0x87, 0xdf, 0x64,
0x98, 0x6a, 0xc1, 0x7e, 0xf0, 0x01, 0x4d, 0xc9,
0x07, 0x4f, 0xb8, 0x2f, 0x46, 0xd7, 0xee, 0xa9,
0xad, 0xe5, 0xf8, 0x21, 0xac, 0xfe, 0x17, 0x58,
},
},
{
.data_len = 2,
.digest = {
0x5e, 0x63, 0x2c, 0xd0, 0xf8, 0x7b, 0xf5, 0xae,
0x61, 0x97, 0x94, 0x57, 0xc8, 0x76, 0x22, 0xd9,
0x8b, 0x04, 0x5e, 0xf1, 0x5d, 0xd0, 0xfc, 0xd9,
0x0c, 0x19, 0x2e, 0xe2, 0xc5, 0xd9, 0x73, 0x51,
},
},
{
.data_len = 3,
.digest = {
0x33, 0x65, 0xa6, 0x37, 0xbf, 0xf8, 0x4f, 0x15,
0x4c, 0xac, 0x9e, 0xa4, 0x3b, 0x02, 0x07, 0x0c,
0x80, 0x86, 0x0d, 0x6c, 0xe4, 0xaf, 0x1c, 0xbc,
0x0b, 0x9c, 0x0a, 0x98, 0xc2, 0x99, 0x71, 0xcd,
},
},
{
.data_len = 16,
.digest = {
0x59, 0xd2, 0x10, 0xd3, 0x75, 0xac, 0x48, 0x32,
0xb1, 0xea, 0xee, 0xcf, 0x0a, 0xd2, 0x8b, 0x15,
0x5d, 0x72, 0x71, 0x4c, 0xa7, 0x29, 0xb0, 0x7a,
0x44, 0x48, 0x8a, 0x54, 0x54, 0x54, 0x41, 0xf5,
},
},
{
.data_len = 32,
.digest = {
0xdc, 0xfc, 0x46, 0x81, 0xc6, 0x1b, 0x2b, 0x47,
0x8b, 0xed, 0xe0, 0x73, 0x34, 0x38, 0x53, 0x92,
0x97, 0x2f, 0xfb, 0x51, 0xab, 0x4f, 0x2d, 0x9d,
0x69, 0x04, 0xa9, 0x5d, 0x33, 0xef, 0xcb, 0x1c,
},
},
{
.data_len = 48,
.digest = {
0xd6, 0x2a, 0x7f, 0x96, 0x04, 0x4d, 0x16, 0xc8,
0x49, 0xe0, 0x37, 0x33, 0xe3, 0x7b, 0x34, 0x56,
0x99, 0xc5, 0x78, 0x57, 0x06, 0x02, 0xb4, 0xea,
0x80, 0xc4, 0xf8, 0x8f, 0x8d, 0x2b, 0xe4, 0x05,
},
},
{
.data_len = 49,
.digest = {
0x8b, 0x58, 0x62, 0xb5, 0x85, 0xf6, 0x83, 0x36,
0xf5, 0x34, 0xb8, 0xd4, 0xbc, 0x5c, 0x8b, 0x38,
0xfd, 0x15, 0xcd, 0x44, 0x83, 0x25, 0x71, 0xe1,
0xd5, 0xe8, 0xa1, 0xa4, 0x36, 0x98, 0x7e, 0x68,
},
},
{
.data_len = 63,
.digest = {
0x7e, 0xeb, 0x06, 0x87, 0xdf, 0x1a, 0xdc, 0xe5,
0xfb, 0x64, 0xd4, 0xd1, 0x5d, 0x9e, 0x75, 0xc0,
0xb9, 0xad, 0x55, 0x6c, 0xe6, 0xba, 0x4d, 0x98,
0x2f, 0xbf, 0x72, 0xad, 0x61, 0x37, 0xf6, 0x11,
},
},
{
.data_len = 64,
.digest = {
0x72, 0xdb, 0x43, 0x16, 0x57, 0x8e, 0x3a, 0x96,
0xf3, 0x98, 0x19, 0x24, 0x17, 0x3b, 0xe8, 0xad,
0xa1, 0x9b, 0xa4, 0x1b, 0x74, 0x85, 0x2e, 0x24,
0x70, 0xea, 0x31, 0x5a, 0x1c, 0xbe, 0x43, 0xb5,
},
},
{
.data_len = 65,
.digest = {
0x32, 0x48, 0xb0, 0xf0, 0x3f, 0xbb, 0xd2, 0xa3,
0xfd, 0xf6, 0x28, 0x4a, 0x2a, 0xc5, 0xbe, 0x4b,
0x73, 0x50, 0x63, 0xd6, 0x16, 0x00, 0xef, 0xed,
0xfe, 0x97, 0x41, 0x29, 0xb2, 0x84, 0xc4, 0xa3,
},
},
{
.data_len = 127,
.digest = {
0x17, 0xda, 0x6b, 0x96, 0x6a, 0xa6, 0xa4, 0xa6,
0xa6, 0xf3, 0x9d, 0x18, 0x19, 0x8d, 0x98, 0x7c,
0x66, 0x38, 0xe8, 0x99, 0xe7, 0x0a, 0x50, 0x92,
0xaf, 0x11, 0x80, 0x05, 0x66, 0xed, 0xab, 0x74,
},
},
{
.data_len = 128,
.digest = {
0x13, 0xd5, 0x8b, 0x22, 0xae, 0x90, 0x7b, 0x67,
0x87, 0x4e, 0x3c, 0x35, 0x4e, 0x01, 0xf0, 0xb1,
0xd3, 0xd1, 0x67, 0xbb, 0x43, 0xdb, 0x7c, 0x75,
0xa4, 0xc7, 0x64, 0x83, 0x1e, 0x9b, 0x98, 0xad,
},
},
{
.data_len = 129,
.digest = {
0x6f, 0xe0, 0x5d, 0x9d, 0xd5, 0x78, 0x29, 0xfb,
0xd0, 0x77, 0xd1, 0x8a, 0xf0, 0x80, 0xcb, 0x81,
0x71, 0x9e, 0x4d, 0x49, 0xde, 0x74, 0x2a, 0x37,
0xc0, 0xd5, 0xf0, 0xfa, 0x50, 0xe6, 0x23, 0xfe,
},
},
{
.data_len = 256,
.digest = {
0x89, 0xac, 0xf6, 0xe7, 0x5e, 0xba, 0x53, 0xf4,
0x92, 0x32, 0xd5, 0x64, 0xfb, 0xc4, 0x08, 0xac,
0x2c, 0x19, 0x6e, 0x63, 0x13, 0x75, 0xd0, 0x60,
0x54, 0x35, 0x82, 0xc4, 0x6d, 0x03, 0x1a, 0x05,
},
},
{
.data_len = 511,
.digest = {
0x1c, 0xaf, 0x94, 0x7d, 0x9c, 0xce, 0x57, 0x64,
0xf8, 0xa8, 0x25, 0x45, 0x32, 0x86, 0x2b, 0x04,
0xb3, 0x2e, 0x67, 0xca, 0x73, 0x04, 0x2f, 0xab,
0xcc, 0xda, 0x9e, 0x42, 0xa1, 0xaf, 0x83, 0x5a,
},
},
{
.data_len = 513,
.digest = {
0x21, 0xdf, 0xdc, 0x29, 0xd9, 0xfc, 0x7b, 0xe7,
0x3a, 0xc4, 0xe1, 0x61, 0xc5, 0xb5, 0xe1, 0xee,
0x7a, 0x9d, 0x0c, 0x66, 0x36, 0x63, 0xe4, 0x12,
0x62, 0xe2, 0xf5, 0x68, 0x72, 0xfc, 0x1e, 0x18,
},
},
{
.data_len = 1000,
.digest = {
0x6e, 0xc7, 0x2e, 0xac, 0xd0, 0xbb, 0x22, 0xe0,
0xc2, 0x40, 0xb2, 0xfe, 0x8c, 0xaf, 0x9e, 0xcf,
0x32, 0x06, 0xc6, 0x45, 0x29, 0xbd, 0xe0, 0x7f,
0x53, 0x32, 0xc3, 0x2b, 0x2f, 0x68, 0x12, 0xcd,
},
},
{
.data_len = 3333,
.digest = {
0x76, 0xba, 0x52, 0xb5, 0x09, 0xf5, 0x19, 0x09,
0x70, 0x1c, 0x09, 0x28, 0xb4, 0xaa, 0x98, 0x6a,
0x79, 0xe7, 0x5e, 0xcd, 0xe8, 0xa4, 0x73, 0x69,
0x1f, 0xf8, 0x05, 0x0a, 0xb4, 0xfe, 0xf9, 0x63,
},
},
{
.data_len = 4096,
.digest = {
0xf7, 0xad, 0xf9, 0xc8, 0x0e, 0x04, 0x2f, 0xdf,
0xbe, 0x39, 0x79, 0x07, 0x0d, 0xd8, 0x1b, 0x06,
0x42, 0x3a, 0x43, 0x93, 0xf6, 0x7c, 0xc4, 0xe5,
0xc2, 0xd5, 0xd0, 0xa6, 0x35, 0x6c, 0xbd, 0x17,
},
},
{
.data_len = 4128,
.digest = {
0x38, 0xd7, 0xab, 0x7e, 0x08, 0xdc, 0x1e, 0xab,
0x55, 0xbb, 0x3b, 0x7b, 0x6a, 0x17, 0xcc, 0x79,
0xa7, 0x02, 0x62, 0x66, 0x9b, 0xca, 0xee, 0xc0,
0x3d, 0x75, 0x34, 0x2e, 0x55, 0x82, 0x26, 0x3c,
},
},
{
.data_len = 4160,
.digest = {
0xf7, 0xeb, 0x2f, 0x24, 0x98, 0x54, 0x04, 0x5a,
0x19, 0xe4, 0x12, 0x9d, 0x97, 0xbc, 0x87, 0xa5,
0x0b, 0x85, 0x29, 0xa1, 0x36, 0x89, 0xc9, 0xba,
0xa0, 0xe0, 0xac, 0x99, 0x7d, 0xa4, 0x51, 0x9f,
},
},
{
.data_len = 4224,
.digest = {
0x8f, 0xe8, 0xa7, 0x79, 0x02, 0xbb, 0x4a, 0x56,
0x66, 0x91, 0xef, 0x22, 0xd1, 0x09, 0x26, 0x6c,
0xa9, 0x13, 0xd7, 0x44, 0xc7, 0x19, 0x9c, 0x0b,
0xfb, 0x4f, 0xca, 0x72, 0x8f, 0x34, 0xf7, 0x82,
},
},
{
.data_len = 16384,
.digest = {
0xaa, 0x21, 0xbb, 0x25, 0x4b, 0x66, 0x6e, 0x29,
0x71, 0xc1, 0x44, 0x67, 0x19, 0xed, 0xe6, 0xe6,
0x61, 0x13, 0xf4, 0xb7, 0x02, 0x94, 0x81, 0x0f,
0xa7, 0x4d, 0xbb, 0x2c, 0xb8, 0xeb, 0x41, 0x0e,
},
},
};
static const u8 hash_testvec_consolidated[BLAKE2S_HASH_SIZE] = {
0x84, 0x21, 0xbb, 0x73, 0x64, 0x47, 0x45, 0xe0,
0xc1, 0x83, 0x78, 0xf1, 0xea, 0xe5, 0xfd, 0xdb,
0x01, 0xda, 0xb7, 0x86, 0x70, 0x3b, 0x83, 0xb3,
0xbc, 0xd9, 0xfd, 0x96, 0xbd, 0x50, 0x06, 0x67,
};
static const u8 blake2s_keyed_testvec_consolidated[BLAKE2S_HASH_SIZE] = {
0xa6, 0xad, 0xcd, 0xb8, 0xd9, 0xdd, 0xc7, 0x70,
0x07, 0x09, 0x7f, 0x9f, 0x41, 0xa9, 0x70, 0xa4,
0x1c, 0xca, 0x61, 0xbb, 0x58, 0xb5, 0xb2, 0x1d,
0xd1, 0x71, 0x16, 0xb0, 0x49, 0x4f, 0x9e, 0x1b,
};
@@ -0,0 +1,133 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright 2025 Google LLC
*/
#include <crypto/blake2s.h>
#include "blake2s-testvecs.h"
/*
* The following are compatibility functions that present BLAKE2s as an unkeyed
* hash function that produces hashes of fixed length BLAKE2S_HASH_SIZE, so that
* hash-test-template.h can be reused to test it.
*/
static void blake2s_default(const u8 *data, size_t len,
u8 out[BLAKE2S_HASH_SIZE])
{
blake2s(NULL, 0, data, len, out, BLAKE2S_HASH_SIZE);
}
static void blake2s_init_default(struct blake2s_ctx *ctx)
{
blake2s_init(ctx, BLAKE2S_HASH_SIZE);
}
/*
* Generate the HASH_KUNIT_CASES using hash-test-template.h. These test BLAKE2s
* with a key length of 0 and a hash length of BLAKE2S_HASH_SIZE.
*/
#define HASH blake2s_default
#define HASH_CTX blake2s_ctx
#define HASH_SIZE BLAKE2S_HASH_SIZE
#define HASH_INIT blake2s_init_default
#define HASH_UPDATE blake2s_update
#define HASH_FINAL blake2s_final
#include "hash-test-template.h"
/*
* BLAKE2s specific test case which tests all possible combinations of key
* length and hash length.
*/
static void test_blake2s_all_key_and_hash_lens(struct kunit *test)
{
const size_t data_len = 100;
u8 *data = &test_buf[0];
u8 *key = data + data_len;
u8 *hash = key + BLAKE2S_KEY_SIZE;
struct blake2s_ctx main_ctx;
u8 main_hash[BLAKE2S_HASH_SIZE];
rand_bytes_seeded_from_len(data, data_len);
blake2s_init(&main_ctx, BLAKE2S_HASH_SIZE);
for (int key_len = 0; key_len <= BLAKE2S_KEY_SIZE; key_len++) {
rand_bytes_seeded_from_len(key, key_len);
for (int out_len = 1; out_len <= BLAKE2S_HASH_SIZE; out_len++) {
blake2s(key, key_len, data, data_len, hash, out_len);
blake2s_update(&main_ctx, hash, out_len);
}
}
blake2s_final(&main_ctx, main_hash);
KUNIT_ASSERT_MEMEQ(test, main_hash, blake2s_keyed_testvec_consolidated,
BLAKE2S_HASH_SIZE);
}
/*
* BLAKE2s specific test case which tests using a guarded buffer for all allowed
* key lengths. Also tests both blake2s() and blake2s_init_key().
*/
static void test_blake2s_with_guarded_key_buf(struct kunit *test)
{
const size_t data_len = 100;
rand_bytes(test_buf, data_len);
for (int key_len = 0; key_len <= BLAKE2S_KEY_SIZE; key_len++) {
u8 key[BLAKE2S_KEY_SIZE];
u8 *guarded_key = &test_buf[TEST_BUF_LEN - key_len];
u8 hash1[BLAKE2S_HASH_SIZE];
u8 hash2[BLAKE2S_HASH_SIZE];
struct blake2s_ctx ctx;
rand_bytes(key, key_len);
memcpy(guarded_key, key, key_len);
blake2s(key, key_len, test_buf, data_len,
hash1, BLAKE2S_HASH_SIZE);
blake2s(guarded_key, key_len, test_buf, data_len,
hash2, BLAKE2S_HASH_SIZE);
KUNIT_ASSERT_MEMEQ(test, hash1, hash2, BLAKE2S_HASH_SIZE);
blake2s_init_key(&ctx, BLAKE2S_HASH_SIZE, guarded_key, key_len);
blake2s_update(&ctx, test_buf, data_len);
blake2s_final(&ctx, hash2);
KUNIT_ASSERT_MEMEQ(test, hash1, hash2, BLAKE2S_HASH_SIZE);
}
}
/*
* BLAKE2s specific test case which tests using a guarded output buffer for all
* allowed output lengths.
*/
static void test_blake2s_with_guarded_out_buf(struct kunit *test)
{
const size_t data_len = 100;
rand_bytes(test_buf, data_len);
for (int out_len = 1; out_len <= BLAKE2S_HASH_SIZE; out_len++) {
u8 hash[BLAKE2S_HASH_SIZE];
u8 *guarded_hash = &test_buf[TEST_BUF_LEN - out_len];
blake2s(NULL, 0, test_buf, data_len, hash, out_len);
blake2s(NULL, 0, test_buf, data_len, guarded_hash, out_len);
KUNIT_ASSERT_MEMEQ(test, hash, guarded_hash, out_len);
}
}
static struct kunit_case blake2s_test_cases[] = {
HASH_KUNIT_CASES,
KUNIT_CASE(test_blake2s_all_key_and_hash_lens),
KUNIT_CASE(test_blake2s_with_guarded_key_buf),
KUNIT_CASE(test_blake2s_with_guarded_out_buf),
KUNIT_CASE(benchmark_hash),
{},
};
static struct kunit_suite blake2s_test_suite = {
.name = "blake2s",
.test_cases = blake2s_test_cases,
.suite_init = hash_suite_init,
.suite_exit = hash_suite_exit,
};
kunit_test_suite(blake2s_test_suite);
MODULE_DESCRIPTION("KUnit tests and benchmark for BLAKE2s");
MODULE_LICENSE("GPL");
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,186 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */
/* This file was generated by: ./scripts/crypto/gen-hash-testvecs.py ghash */
static const struct {
size_t data_len;
u8 digest[GHASH_DIGEST_SIZE];
} hash_testvecs[] = {
{
.data_len = 0,
.digest = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
},
},
{
.data_len = 1,
.digest = {
0x13, 0x91, 0xa1, 0x11, 0x08, 0xc3, 0x7e, 0xeb,
0x21, 0x42, 0x4a, 0xd6, 0x45, 0x0f, 0x41, 0xa7,
},
},
{
.data_len = 2,
.digest = {
0xde, 0x00, 0x63, 0x3f, 0x71, 0x0f, 0xc6, 0x29,
0x53, 0x2e, 0x49, 0xd9, 0xc2, 0xb7, 0x73, 0xce,
},
},
{
.data_len = 3,
.digest = {
0xcf, 0xc7, 0xa8, 0x20, 0x24, 0xe9, 0x7a, 0x6c,
0x2c, 0x2a, 0x34, 0x70, 0x26, 0xba, 0xd5, 0x9a,
},
},
{
.data_len = 16,
.digest = {
0xaa, 0xe0, 0xdc, 0x7f, 0xcf, 0x8b, 0xe6, 0x0c,
0x2e, 0x93, 0x89, 0x7d, 0x68, 0x4e, 0xc2, 0x63,
},
},
{
.data_len = 32,
.digest = {
0x4b, 0x8b, 0x93, 0x5c, 0x79, 0xad, 0x85, 0x08,
0xd3, 0x8a, 0xcd, 0xdd, 0x4c, 0x6e, 0x0e, 0x6f,
},
},
{
.data_len = 48,
.digest = {
0xfa, 0xa0, 0x25, 0xdd, 0x61, 0x9a, 0x52, 0x9a,
0xea, 0xee, 0xc6, 0x62, 0xb2, 0xba, 0x11, 0x49,
},
},
{
.data_len = 49,
.digest = {
0x23, 0xf1, 0x05, 0xeb, 0x30, 0x40, 0xb9, 0x1d,
0xe6, 0x35, 0x51, 0x4e, 0x0f, 0xc0, 0x1b, 0x9e,
},
},
{
.data_len = 63,
.digest = {
0x8d, 0xcf, 0xa0, 0xc8, 0x83, 0x21, 0x06, 0x81,
0xc6, 0x36, 0xd5, 0x62, 0xbf, 0xa0, 0xcd, 0x9c,
},
},
{
.data_len = 64,
.digest = {
0xe7, 0xca, 0xbe, 0xe7, 0x66, 0xc8, 0x85, 0xad,
0xbc, 0xaf, 0x58, 0x21, 0xd7, 0x67, 0x82, 0x15,
},
},
{
.data_len = 65,
.digest = {
0x9f, 0x48, 0x10, 0xd9, 0xa2, 0x6b, 0x9d, 0xe0,
0xb1, 0x87, 0xe1, 0x39, 0xc3, 0xd7, 0xee, 0x09,
},
},
{
.data_len = 127,
.digest = {
0xa4, 0x36, 0xb7, 0x82, 0xd2, 0x67, 0x7e, 0xaf,
0x5d, 0xfd, 0x67, 0x9c, 0x1d, 0x9f, 0xe4, 0xf7,
},
},
{
.data_len = 128,
.digest = {
0x57, 0xe7, 0x1d, 0x78, 0xf0, 0x8e, 0xc7, 0x0c,
0x15, 0xee, 0x18, 0xc4, 0xd1, 0x75, 0x90, 0xaa,
},
},
{
.data_len = 129,
.digest = {
0x9b, 0xad, 0x81, 0xa9, 0x22, 0xb2, 0x19, 0x53,
0x60, 0x30, 0xe7, 0xa0, 0x4f, 0xd6, 0x72, 0x42,
},
},
{
.data_len = 256,
.digest = {
0xf7, 0x33, 0x42, 0xbf, 0x58, 0xde, 0x88, 0x0f,
0x8d, 0x3d, 0xa6, 0x11, 0x14, 0xc3, 0xf1, 0xdc,
},
},
{
.data_len = 511,
.digest = {
0x59, 0xdc, 0xa9, 0xc0, 0x4e, 0xd6, 0x97, 0xb3,
0x60, 0xaf, 0xa8, 0xa0, 0xea, 0x54, 0x8e, 0xc3,
},
},
{
.data_len = 513,
.digest = {
0xa2, 0x23, 0x37, 0xcc, 0x97, 0xec, 0xea, 0xbe,
0xd6, 0xc7, 0x13, 0xf7, 0x93, 0x73, 0xc0, 0x64,
},
},
{
.data_len = 1000,
.digest = {
0x46, 0x8b, 0x43, 0x77, 0x9b, 0xc2, 0xfc, 0xa4,
0x68, 0x6a, 0x6c, 0x07, 0xa4, 0x6f, 0x47, 0x65,
},
},
{
.data_len = 3333,
.digest = {
0x69, 0x7f, 0x19, 0xc3, 0xb9, 0xa4, 0xff, 0x40,
0xe3, 0x03, 0x71, 0xa3, 0x88, 0x8a, 0xf1, 0xbd,
},
},
{
.data_len = 4096,
.digest = {
0x4d, 0x65, 0xe6, 0x9c, 0xeb, 0x6a, 0x46, 0x8d,
0xe9, 0x32, 0x96, 0x72, 0xb3, 0x0d, 0x08, 0xa9,
},
},
{
.data_len = 4128,
.digest = {
0xfc, 0xa1, 0x74, 0x46, 0x21, 0x64, 0xa7, 0x64,
0xbe, 0x47, 0x03, 0x1e, 0x05, 0xf7, 0xd8, 0x37,
},
},
{
.data_len = 4160,
.digest = {
0x70, 0x5b, 0xe9, 0x17, 0xab, 0xd5, 0xa2, 0xee,
0xcb, 0x39, 0xa4, 0x81, 0x2f, 0x41, 0x70, 0xae,
},
},
{
.data_len = 4224,
.digest = {
0x07, 0xbd, 0xb6, 0x52, 0xe2, 0x75, 0x2c, 0x33,
0x6d, 0x1b, 0x63, 0x56, 0x58, 0xda, 0x98, 0x55,
},
},
{
.data_len = 16384,
.digest = {
0x9c, 0xb5, 0xf4, 0x14, 0xe8, 0xa8, 0x4a, 0xde,
0xee, 0x7b, 0xbb, 0xd6, 0x21, 0x6d, 0x6a, 0x69,
},
},
};
static const u8 hash_testvec_consolidated[GHASH_DIGEST_SIZE] = {
0x08, 0xef, 0xf5, 0x27, 0xb1, 0xca, 0xd4, 0x1d,
0xad, 0x38, 0x69, 0x88, 0x6b, 0x16, 0xdf, 0xa8,
};
static const u8 ghash_allones_hashofhashes[GHASH_DIGEST_SIZE] = {
0xef, 0x85, 0x58, 0xf8, 0x54, 0x9c, 0x5e, 0x54,
0xd9, 0xbe, 0x04, 0x1f, 0xff, 0xff, 0xff, 0xff,
};
+194
View File
@@ -0,0 +1,194 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright 2026 Google LLC
*/
#include <crypto/gf128hash.h>
#include "ghash-testvecs.h"
/*
* A fixed key used when presenting GHASH as an unkeyed hash function in order
* to reuse hash-test-template.h. At the beginning of the test suite, this is
* initialized to a key prepared from bytes generated from a fixed seed.
*/
static struct ghash_key test_key;
static void ghash_init_withtestkey(struct ghash_ctx *ctx)
{
ghash_init(ctx, &test_key);
}
static void ghash_withtestkey(const u8 *data, size_t len,
u8 out[GHASH_BLOCK_SIZE])
{
ghash(&test_key, data, len, out);
}
/* Generate the HASH_KUNIT_CASES using hash-test-template.h. */
#define HASH ghash_withtestkey
#define HASH_CTX ghash_ctx
#define HASH_SIZE GHASH_BLOCK_SIZE
#define HASH_INIT ghash_init_withtestkey
#define HASH_UPDATE ghash_update
#define HASH_FINAL ghash_final
#include "hash-test-template.h"
/*
* Test a key and messages containing all one bits. This is useful to detect
* overflow bugs in implementations that emulate carryless multiplication using
* a series of standard multiplications with the bits spread out.
*/
static void test_ghash_allones_key_and_message(struct kunit *test)
{
struct ghash_key key;
struct ghash_ctx hashofhashes_ctx;
u8 hash[GHASH_BLOCK_SIZE];
static_assert(TEST_BUF_LEN >= 4096);
memset(test_buf, 0xff, 4096);
ghash_preparekey(&key, test_buf);
ghash_init(&hashofhashes_ctx, &key);
for (size_t len = 0; len <= 4096; len += 16) {
ghash(&key, test_buf, len, hash);
ghash_update(&hashofhashes_ctx, hash, sizeof(hash));
}
ghash_final(&hashofhashes_ctx, hash);
KUNIT_ASSERT_MEMEQ(test, hash, ghash_allones_hashofhashes,
sizeof(hash));
}
#define MAX_LEN_FOR_KEY_CHECK 1024
/*
* Given two prepared keys which should be identical (but may differ in
* alignment and/or whether they are followed by a guard page or not), verify
* that they produce consistent results on various data lengths.
*/
static void check_key_consistency(struct kunit *test,
const struct ghash_key *key1,
const struct ghash_key *key2)
{
u8 *data = test_buf;
u8 hash1[GHASH_BLOCK_SIZE];
u8 hash2[GHASH_BLOCK_SIZE];
rand_bytes(data, MAX_LEN_FOR_KEY_CHECK);
KUNIT_ASSERT_MEMEQ(test, key1, key2, sizeof(*key1));
for (int i = 0; i < 100; i++) {
size_t len = rand_length(MAX_LEN_FOR_KEY_CHECK);
ghash(key1, data, len, hash1);
ghash(key2, data, len, hash2);
KUNIT_ASSERT_MEMEQ(test, hash1, hash2, sizeof(hash1));
}
}
/* Test that no buffer overreads occur on either raw_key or ghash_key. */
static void test_ghash_with_guarded_key(struct kunit *test)
{
u8 raw_key[GHASH_BLOCK_SIZE];
u8 *guarded_raw_key = &test_buf[TEST_BUF_LEN - sizeof(raw_key)];
struct ghash_key key1, key2;
struct ghash_key *guarded_key =
(struct ghash_key *)&test_buf[TEST_BUF_LEN - sizeof(key1)];
/* Prepare with regular buffers. */
rand_bytes(raw_key, sizeof(raw_key));
ghash_preparekey(&key1, raw_key);
/* Prepare with guarded raw_key, then check that it works. */
memcpy(guarded_raw_key, raw_key, sizeof(raw_key));
ghash_preparekey(&key2, guarded_raw_key);
check_key_consistency(test, &key1, &key2);
/* Prepare guarded ghash_key, then check that it works. */
ghash_preparekey(guarded_key, raw_key);
check_key_consistency(test, &key1, guarded_key);
}
/*
* Test that ghash_key only needs to be aligned to
* __alignof__(struct ghash_key), i.e. 8 bytes. The assembly code may prefer
* 16-byte or higher alignment, but it mustn't require it.
*/
static void test_ghash_with_minimally_aligned_key(struct kunit *test)
{
u8 raw_key[GHASH_BLOCK_SIZE];
struct ghash_key key;
struct ghash_key *minaligned_key =
(struct ghash_key *)&test_buf[MAX_LEN_FOR_KEY_CHECK +
__alignof__(struct ghash_key)];
KUNIT_ASSERT_TRUE(test, IS_ALIGNED((uintptr_t)minaligned_key,
__alignof__(struct ghash_key)));
KUNIT_ASSERT_TRUE(test, !IS_ALIGNED((uintptr_t)minaligned_key,
2 * __alignof__(struct ghash_key)));
rand_bytes(raw_key, sizeof(raw_key));
ghash_preparekey(&key, raw_key);
ghash_preparekey(minaligned_key, raw_key);
check_key_consistency(test, &key, minaligned_key);
}
struct ghash_irq_test_state {
struct ghash_key expected_key;
u8 raw_key[GHASH_BLOCK_SIZE];
};
static bool ghash_irq_test_func(void *state_)
{
struct ghash_irq_test_state *state = state_;
struct ghash_key key;
ghash_preparekey(&key, state->raw_key);
return memcmp(&key, &state->expected_key, sizeof(key)) == 0;
}
/*
* Test that ghash_preparekey() produces the same output regardless of whether
* FPU or vector registers are usable when it is called.
*/
static void test_ghash_preparekey_in_irqs(struct kunit *test)
{
struct ghash_irq_test_state state;
rand_bytes(state.raw_key, sizeof(state.raw_key));
ghash_preparekey(&state.expected_key, state.raw_key);
kunit_run_irq_test(test, ghash_irq_test_func, 200000, &state);
}
static int ghash_suite_init(struct kunit_suite *suite)
{
u8 raw_key[GHASH_BLOCK_SIZE];
rand_bytes_seeded_from_len(raw_key, sizeof(raw_key));
ghash_preparekey(&test_key, raw_key);
return hash_suite_init(suite);
}
static void ghash_suite_exit(struct kunit_suite *suite)
{
hash_suite_exit(suite);
}
static struct kunit_case ghash_test_cases[] = {
HASH_KUNIT_CASES,
KUNIT_CASE(test_ghash_allones_key_and_message),
KUNIT_CASE(test_ghash_with_guarded_key),
KUNIT_CASE(test_ghash_with_minimally_aligned_key),
KUNIT_CASE(test_ghash_preparekey_in_irqs),
KUNIT_CASE(benchmark_hash),
{},
};
static struct kunit_suite ghash_test_suite = {
.name = "ghash",
.test_cases = ghash_test_cases,
.suite_init = ghash_suite_init,
.suite_exit = ghash_suite_exit,
};
kunit_test_suite(ghash_test_suite);
MODULE_DESCRIPTION("KUnit tests and benchmark for GHASH");
MODULE_LICENSE("GPL");
@@ -0,0 +1,568 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
* Test cases for hash functions, including a benchmark. This is included by
* KUnit test suites that want to use it. See sha512_kunit.c for an example.
*
* Copyright 2025 Google LLC
*/
#include <kunit/run-in-irq-context.h>
#include <kunit/test.h>
#include <linux/vmalloc.h>
/* test_buf is a guarded buffer, i.e. &test_buf[TEST_BUF_LEN] is not mapped. */
#define TEST_BUF_LEN 16384
static u8 *test_buf;
static u8 *orig_test_buf;
static u64 random_seed;
/*
* This is a simple linear congruential generator. It is used only for testing,
* which does not require cryptographically secure random numbers. A hard-coded
* algorithm is used instead of <linux/prandom.h> so that it matches the
* algorithm used by the test vector generation script. This allows the input
* data in random test vectors to be concisely stored as just the seed.
*/
static u32 rand32(void)
{
random_seed = (random_seed * 25214903917 + 11) & ((1ULL << 48) - 1);
return random_seed >> 16;
}
static void rand_bytes(u8 *out, size_t len)
{
for (size_t i = 0; i < len; i++)
out[i] = rand32();
}
static void rand_bytes_seeded_from_len(u8 *out, size_t len)
{
random_seed = len;
rand_bytes(out, len);
}
static bool rand_bool(void)
{
return rand32() % 2;
}
/* Generate a random length, preferring small lengths. */
static size_t rand_length(size_t max_len)
{
size_t len;
switch (rand32() % 3) {
case 0:
len = rand32() % 128;
break;
case 1:
len = rand32() % 3072;
break;
default:
len = rand32();
break;
}
return len % (max_len + 1);
}
static size_t rand_offset(size_t max_offset)
{
return min(rand32() % 128, max_offset);
}
static int hash_suite_init(struct kunit_suite *suite)
{
/*
* Allocate the test buffer using vmalloc() with a page-aligned length
* so that it is immediately followed by a guard page. This allows
* buffer overreads to be detected, even in assembly code.
*/
size_t alloc_len = round_up(TEST_BUF_LEN, PAGE_SIZE);
orig_test_buf = vmalloc(alloc_len);
if (!orig_test_buf)
return -ENOMEM;
test_buf = orig_test_buf + alloc_len - TEST_BUF_LEN;
return 0;
}
static void hash_suite_exit(struct kunit_suite *suite)
{
vfree(orig_test_buf);
orig_test_buf = NULL;
test_buf = NULL;
}
/*
* Test the hash function against a list of test vectors.
*
* Note that it's only necessary to run each test vector in one way (e.g.,
* one-shot instead of incremental), since consistency between different ways of
* using the APIs is verified by other test cases.
*/
static void test_hash_test_vectors(struct kunit *test)
{
for (size_t i = 0; i < ARRAY_SIZE(hash_testvecs); i++) {
size_t data_len = hash_testvecs[i].data_len;
u8 actual_hash[HASH_SIZE];
KUNIT_ASSERT_LE(test, data_len, TEST_BUF_LEN);
rand_bytes_seeded_from_len(test_buf, data_len);
HASH(test_buf, data_len, actual_hash);
KUNIT_ASSERT_MEMEQ_MSG(
test, actual_hash, hash_testvecs[i].digest, HASH_SIZE,
"Wrong result with test vector %zu; data_len=%zu", i,
data_len);
}
}
/*
* Test that the hash function produces correct results for *every* length up to
* 4096 bytes. To do this, generate seeded random data, then calculate a hash
* value for each length 0..4096, then hash the hash values. Verify just the
* final hash value, which should match only when all hash values were correct.
*/
static void test_hash_all_lens_up_to_4096(struct kunit *test)
{
struct HASH_CTX ctx;
u8 hash[HASH_SIZE];
static_assert(TEST_BUF_LEN >= 4096);
rand_bytes_seeded_from_len(test_buf, 4096);
HASH_INIT(&ctx);
for (size_t len = 0; len <= 4096; len++) {
HASH(test_buf, len, hash);
HASH_UPDATE(&ctx, hash, HASH_SIZE);
}
HASH_FINAL(&ctx, hash);
KUNIT_ASSERT_MEMEQ(test, hash, hash_testvec_consolidated, HASH_SIZE);
}
/*
* Test that the hash function produces the same result with a one-shot
* computation as it does with an incremental computation.
*/
static void test_hash_incremental_updates(struct kunit *test)
{
for (int i = 0; i < 1000; i++) {
size_t total_len, offset;
struct HASH_CTX ctx;
u8 hash1[HASH_SIZE];
u8 hash2[HASH_SIZE];
size_t num_parts = 0;
size_t remaining_len, cur_offset;
total_len = rand_length(TEST_BUF_LEN);
offset = rand_offset(TEST_BUF_LEN - total_len);
rand_bytes(&test_buf[offset], total_len);
/* Compute the hash value in one shot. */
HASH(&test_buf[offset], total_len, hash1);
/*
* Compute the hash value incrementally, using a randomly
* selected sequence of update lengths that sum to total_len.
*/
HASH_INIT(&ctx);
remaining_len = total_len;
cur_offset = offset;
while (rand_bool()) {
size_t part_len = rand_length(remaining_len);
HASH_UPDATE(&ctx, &test_buf[cur_offset], part_len);
num_parts++;
cur_offset += part_len;
remaining_len -= part_len;
}
if (remaining_len != 0 || rand_bool()) {
HASH_UPDATE(&ctx, &test_buf[cur_offset], remaining_len);
num_parts++;
}
HASH_FINAL(&ctx, hash2);
/* Verify that the two hash values are the same. */
KUNIT_ASSERT_MEMEQ_MSG(
test, hash1, hash2, HASH_SIZE,
"Incremental test failed with total_len=%zu num_parts=%zu offset=%zu",
total_len, num_parts, offset);
}
}
/*
* Test that the hash function does not overrun any buffers. Uses a guard page
* to catch buffer overruns even if they occur in assembly code.
*/
static void test_hash_buffer_overruns(struct kunit *test)
{
const size_t max_tested_len = TEST_BUF_LEN - sizeof(struct HASH_CTX);
void *const buf_end = &test_buf[TEST_BUF_LEN];
struct HASH_CTX *guarded_ctx = buf_end - sizeof(*guarded_ctx);
rand_bytes(test_buf, TEST_BUF_LEN);
for (int i = 0; i < 100; i++) {
size_t len = rand_length(max_tested_len);
struct HASH_CTX ctx;
u8 hash[HASH_SIZE];
/* Check for overruns of the data buffer. */
HASH(buf_end - len, len, hash);
HASH_INIT(&ctx);
HASH_UPDATE(&ctx, buf_end - len, len);
HASH_FINAL(&ctx, hash);
/* Check for overruns of the hash value buffer. */
HASH(test_buf, len, buf_end - HASH_SIZE);
HASH_INIT(&ctx);
HASH_UPDATE(&ctx, test_buf, len);
HASH_FINAL(&ctx, buf_end - HASH_SIZE);
/* Check for overuns of the hash context. */
HASH_INIT(guarded_ctx);
HASH_UPDATE(guarded_ctx, test_buf, len);
HASH_FINAL(guarded_ctx, hash);
}
}
/*
* Test that the caller is permitted to alias the output digest and source data
* buffer, and also modify the source data buffer after it has been used.
*/
static void test_hash_overlaps(struct kunit *test)
{
const size_t max_tested_len = TEST_BUF_LEN - HASH_SIZE;
struct HASH_CTX ctx;
u8 hash[HASH_SIZE];
rand_bytes(test_buf, TEST_BUF_LEN);
for (int i = 0; i < 100; i++) {
size_t len = rand_length(max_tested_len);
size_t offset = HASH_SIZE + rand_offset(max_tested_len - len);
bool left_end = rand_bool();
u8 *ovl_hash = left_end ? &test_buf[offset] :
&test_buf[offset + len - HASH_SIZE];
HASH(&test_buf[offset], len, hash);
HASH(&test_buf[offset], len, ovl_hash);
KUNIT_ASSERT_MEMEQ_MSG(
test, hash, ovl_hash, HASH_SIZE,
"Overlap test 1 failed with len=%zu offset=%zu left_end=%d",
len, offset, left_end);
/* Repeat the above test, but this time use init+update+final */
HASH(&test_buf[offset], len, hash);
HASH_INIT(&ctx);
HASH_UPDATE(&ctx, &test_buf[offset], len);
HASH_FINAL(&ctx, ovl_hash);
KUNIT_ASSERT_MEMEQ_MSG(
test, hash, ovl_hash, HASH_SIZE,
"Overlap test 2 failed with len=%zu offset=%zu left_end=%d",
len, offset, left_end);
/* Test modifying the source data after it was used. */
HASH(&test_buf[offset], len, hash);
HASH_INIT(&ctx);
HASH_UPDATE(&ctx, &test_buf[offset], len);
rand_bytes(&test_buf[offset], len);
HASH_FINAL(&ctx, ovl_hash);
KUNIT_ASSERT_MEMEQ_MSG(
test, hash, ovl_hash, HASH_SIZE,
"Overlap test 3 failed with len=%zu offset=%zu left_end=%d",
len, offset, left_end);
}
}
/*
* Test that if the same data is hashed at different alignments in memory, the
* results are the same.
*/
static void test_hash_alignment_consistency(struct kunit *test)
{
u8 hash1[128 + HASH_SIZE];
u8 hash2[128 + HASH_SIZE];
for (int i = 0; i < 100; i++) {
size_t len = rand_length(TEST_BUF_LEN);
size_t data_offs1 = rand_offset(TEST_BUF_LEN - len);
size_t data_offs2 = rand_offset(TEST_BUF_LEN - len);
size_t hash_offs1 = rand_offset(128);
size_t hash_offs2 = rand_offset(128);
rand_bytes(&test_buf[data_offs1], len);
HASH(&test_buf[data_offs1], len, &hash1[hash_offs1]);
memmove(&test_buf[data_offs2], &test_buf[data_offs1], len);
HASH(&test_buf[data_offs2], len, &hash2[hash_offs2]);
KUNIT_ASSERT_MEMEQ_MSG(
test, &hash1[hash_offs1], &hash2[hash_offs2], HASH_SIZE,
"Alignment consistency test failed with len=%zu data_offs=(%zu,%zu) hash_offs=(%zu,%zu)",
len, data_offs1, data_offs2, hash_offs1, hash_offs2);
}
}
/* Test that HASH_FINAL zeroizes the context. */
static void test_hash_ctx_zeroization(struct kunit *test)
{
static const u8 zeroes[sizeof(struct HASH_CTX)];
struct HASH_CTX ctx;
rand_bytes(test_buf, 128);
HASH_INIT(&ctx);
HASH_UPDATE(&ctx, test_buf, 128);
HASH_FINAL(&ctx, test_buf);
KUNIT_ASSERT_MEMEQ_MSG(test, &ctx, zeroes, sizeof(ctx),
"Hash context was not zeroized by finalization");
}
#define IRQ_TEST_DATA_LEN 256
#define IRQ_TEST_NUM_BUFFERS 3 /* matches max concurrency level */
struct hash_irq_test1_state {
u8 expected_hashes[IRQ_TEST_NUM_BUFFERS][HASH_SIZE];
atomic_t seqno;
};
/*
* Compute the hash of one of the test messages and verify that it matches the
* expected hash from @state->expected_hashes. To increase the chance of
* detecting problems, cycle through multiple messages.
*/
static bool hash_irq_test1_func(void *state_)
{
struct hash_irq_test1_state *state = state_;
u32 i = (u32)atomic_inc_return(&state->seqno) % IRQ_TEST_NUM_BUFFERS;
u8 actual_hash[HASH_SIZE];
HASH(&test_buf[i * IRQ_TEST_DATA_LEN], IRQ_TEST_DATA_LEN, actual_hash);
return memcmp(actual_hash, state->expected_hashes[i], HASH_SIZE) == 0;
}
/*
* Test that if hashes are computed in task, softirq, and hardirq context
* concurrently, then all results are as expected.
*/
static void test_hash_interrupt_context_1(struct kunit *test)
{
struct hash_irq_test1_state state = {};
/* Prepare some test messages and compute the expected hash of each. */
rand_bytes(test_buf, IRQ_TEST_NUM_BUFFERS * IRQ_TEST_DATA_LEN);
for (int i = 0; i < IRQ_TEST_NUM_BUFFERS; i++)
HASH(&test_buf[i * IRQ_TEST_DATA_LEN], IRQ_TEST_DATA_LEN,
state.expected_hashes[i]);
kunit_run_irq_test(test, hash_irq_test1_func, 100000, &state);
}
struct hash_irq_test2_hash_ctx {
struct HASH_CTX hash_ctx;
atomic_t in_use;
int offset;
int step;
};
struct hash_irq_test2_state {
struct hash_irq_test2_hash_ctx ctxs[IRQ_TEST_NUM_BUFFERS];
u8 expected_hash[HASH_SIZE];
u16 update_lens[32];
int num_steps;
};
static bool hash_irq_test2_func(void *state_)
{
struct hash_irq_test2_state *state = state_;
struct hash_irq_test2_hash_ctx *ctx;
bool ret = true;
for (ctx = &state->ctxs[0]; ctx < &state->ctxs[ARRAY_SIZE(state->ctxs)];
ctx++) {
if (atomic_cmpxchg(&ctx->in_use, 0, 1) == 0)
break;
}
if (WARN_ON_ONCE(ctx == &state->ctxs[ARRAY_SIZE(state->ctxs)])) {
/*
* This should never happen, as the number of contexts is equal
* to the maximum concurrency level of kunit_run_irq_test().
*/
return false;
}
if (ctx->step == 0) {
/* Init step */
HASH_INIT(&ctx->hash_ctx);
ctx->offset = 0;
ctx->step++;
} else if (ctx->step < state->num_steps - 1) {
/* Update step */
HASH_UPDATE(&ctx->hash_ctx, &test_buf[ctx->offset],
state->update_lens[ctx->step - 1]);
ctx->offset += state->update_lens[ctx->step - 1];
ctx->step++;
} else {
/* Final step */
u8 actual_hash[HASH_SIZE];
if (WARN_ON_ONCE(ctx->offset != TEST_BUF_LEN))
ret = false;
HASH_FINAL(&ctx->hash_ctx, actual_hash);
if (memcmp(actual_hash, state->expected_hash, HASH_SIZE) != 0)
ret = false;
ctx->step = 0;
}
atomic_set_release(&ctx->in_use, 0);
return ret;
}
/*
* Test that if hashes are computed in task, softirq, and hardirq context
* concurrently, *including doing different parts of the same incremental
* computation in different contexts*, then all results are as expected.
* Besides detecting bugs similar to those that test_hash_interrupt_context_1
* can detect, this test case can also detect bugs where hash function
* implementations don't correctly handle these mixed incremental computations.
*/
static void test_hash_interrupt_context_2(struct kunit *test)
{
struct hash_irq_test2_state *state;
int remaining = TEST_BUF_LEN;
state = kunit_kzalloc(test, sizeof(*state), GFP_KERNEL);
KUNIT_ASSERT_NOT_NULL(test, state);
rand_bytes(test_buf, TEST_BUF_LEN);
HASH(test_buf, TEST_BUF_LEN, state->expected_hash);
/*
* Generate a list of update lengths to use. Ensure that it contains
* multiple entries but is limited to a maximum length.
*/
static_assert(TEST_BUF_LEN / 4096 > 1);
for (state->num_steps = 0;
state->num_steps < ARRAY_SIZE(state->update_lens) - 1 && remaining;
state->num_steps++) {
state->update_lens[state->num_steps] =
rand_length(min(remaining, 4096));
remaining -= state->update_lens[state->num_steps];
}
if (remaining)
state->update_lens[state->num_steps++] = remaining;
state->num_steps += 2; /* for init and final */
kunit_run_irq_test(test, hash_irq_test2_func, 250000, state);
}
#define UNKEYED_HASH_KUNIT_CASES \
KUNIT_CASE(test_hash_test_vectors), \
KUNIT_CASE(test_hash_all_lens_up_to_4096), \
KUNIT_CASE(test_hash_incremental_updates), \
KUNIT_CASE(test_hash_buffer_overruns), \
KUNIT_CASE(test_hash_overlaps), \
KUNIT_CASE(test_hash_alignment_consistency), \
KUNIT_CASE(test_hash_ctx_zeroization), \
KUNIT_CASE(test_hash_interrupt_context_1), \
KUNIT_CASE(test_hash_interrupt_context_2)
/* benchmark_hash is omitted so that the suites can put it last. */
#ifdef HMAC
/*
* Test the corresponding HMAC variant.
*
* This test case is fairly short, since HMAC is just a simple C wrapper around
* the underlying unkeyed hash function, which is already well-tested by the
* other test cases. It's not useful to test things like data alignment or
* interrupt context again for HMAC, nor to have a long list of test vectors.
*
* Thus, just do a single consolidated test, which covers all data lengths up to
* 4096 bytes and all key lengths up to 292 bytes. For each data length, select
* a key length, generate the inputs from a seed, and compute the HMAC value.
* Concatenate all these HMAC values together, and compute the HMAC of that.
* Verify that value. If this fails, then the HMAC implementation is wrong.
* This won't show which specific input failed, but that should be fine. Any
* failure would likely be non-input-specific or also show in the unkeyed tests.
*/
static void test_hmac(struct kunit *test)
{
static const u8 zeroes[sizeof(struct HMAC_CTX)];
u8 *raw_key;
struct HMAC_KEY key;
struct HMAC_CTX ctx;
u8 mac[HASH_SIZE];
u8 mac2[HASH_SIZE];
static_assert(TEST_BUF_LEN >= 4096 + 293);
rand_bytes_seeded_from_len(test_buf, 4096);
raw_key = &test_buf[4096];
rand_bytes_seeded_from_len(raw_key, 32);
HMAC_PREPAREKEY(&key, raw_key, 32);
HMAC_INIT(&ctx, &key);
for (size_t data_len = 0; data_len <= 4096; data_len++) {
/*
* Cycle through key lengths as well. Somewhat arbitrarily go
* up to 293, which is somewhat larger than the largest hash
* block size (which is the size at which the key starts being
* hashed down to one block); going higher would not be useful.
* To reduce correlation with data_len, use a prime number here.
*/
size_t key_len = data_len % 293;
HMAC_UPDATE(&ctx, test_buf, data_len);
rand_bytes_seeded_from_len(raw_key, key_len);
HMAC_USINGRAWKEY(raw_key, key_len, test_buf, data_len, mac);
HMAC_UPDATE(&ctx, mac, HASH_SIZE);
/* Verify that HMAC() is consistent with HMAC_USINGRAWKEY(). */
HMAC_PREPAREKEY(&key, raw_key, key_len);
HMAC(&key, test_buf, data_len, mac2);
KUNIT_ASSERT_MEMEQ_MSG(
test, mac, mac2, HASH_SIZE,
"HMAC gave different results with raw and prepared keys");
}
HMAC_FINAL(&ctx, mac);
KUNIT_EXPECT_MEMEQ_MSG(test, mac, hmac_testvec_consolidated, HASH_SIZE,
"HMAC gave wrong result");
KUNIT_EXPECT_MEMEQ_MSG(test, &ctx, zeroes, sizeof(ctx),
"HMAC context was not zeroized by finalization");
}
#define HASH_KUNIT_CASES UNKEYED_HASH_KUNIT_CASES, KUNIT_CASE(test_hmac)
#else
#define HASH_KUNIT_CASES UNKEYED_HASH_KUNIT_CASES
#endif
/* Benchmark the hash function on various data lengths. */
static void benchmark_hash(struct kunit *test)
{
static const size_t lens_to_test[] = {
1, 16, 64, 127, 128, 200, 256,
511, 512, 1024, 3173, 4096, 16384,
};
u8 hash[HASH_SIZE];
if (!IS_ENABLED(CONFIG_CRYPTO_LIB_BENCHMARK))
kunit_skip(test, "not enabled");
/* Warm-up */
for (size_t i = 0; i < 10000000; i += TEST_BUF_LEN)
HASH(test_buf, TEST_BUF_LEN, hash);
for (size_t i = 0; i < ARRAY_SIZE(lens_to_test); i++) {
size_t len = lens_to_test[i];
/* The '+ 128' tries to account for per-message overhead. */
size_t num_iters = 10000000 / (len + 128);
u64 t;
KUNIT_ASSERT_LE(test, len, TEST_BUF_LEN);
preempt_disable();
t = ktime_get_ns();
for (size_t j = 0; j < num_iters; j++)
HASH(test_buf, len, hash);
t = ktime_get_ns() - t;
preempt_enable();
kunit_info(test, "len=%zu: %llu MB/s", len,
div64_u64((u64)len * num_iters * 1000, t ?: 1));
}
}
+186
View File
@@ -0,0 +1,186 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */
/* This file was generated by: ./scripts/crypto/gen-hash-testvecs.py md5 */
static const struct {
size_t data_len;
u8 digest[MD5_DIGEST_SIZE];
} hash_testvecs[] = {
{
.data_len = 0,
.digest = {
0xd4, 0x1d, 0x8c, 0xd9, 0x8f, 0x00, 0xb2, 0x04,
0xe9, 0x80, 0x09, 0x98, 0xec, 0xf8, 0x42, 0x7e,
},
},
{
.data_len = 1,
.digest = {
0x16, 0x7b, 0x86, 0xf2, 0x1d, 0xf3, 0x76, 0xc9,
0x6f, 0x10, 0xa0, 0x61, 0x5b, 0x14, 0x20, 0x0b,
},
},
{
.data_len = 2,
.digest = {
0x2d, 0x30, 0x96, 0xc7, 0x43, 0x40, 0xed, 0xb2,
0xfb, 0x84, 0x63, 0x9a, 0xec, 0xc7, 0x3c, 0x3c,
},
},
{
.data_len = 3,
.digest = {
0xe5, 0x0f, 0xce, 0xe0, 0xc8, 0xff, 0x4e, 0x08,
0x5e, 0x19, 0xe5, 0xf2, 0x08, 0x11, 0x19, 0x16,
},
},
{
.data_len = 16,
.digest = {
0xe8, 0xca, 0x29, 0x05, 0x2f, 0xd1, 0xf3, 0x99,
0x40, 0x71, 0xf5, 0xc2, 0xf7, 0xf8, 0x17, 0x3e,
},
},
{
.data_len = 32,
.digest = {
0xe3, 0x20, 0xc1, 0xd8, 0x21, 0x14, 0x44, 0x59,
0x1a, 0xf5, 0x91, 0xaf, 0x69, 0xbe, 0x93, 0x9d,
},
},
{
.data_len = 48,
.digest = {
0xfb, 0x06, 0xb0, 0xf0, 0x00, 0x10, 0x4b, 0x68,
0x3d, 0x75, 0xf9, 0x70, 0xde, 0xbb, 0x32, 0x16,
},
},
{
.data_len = 49,
.digest = {
0x52, 0x86, 0x48, 0x8b, 0xae, 0x91, 0x7c, 0x4e,
0xc2, 0x2a, 0x69, 0x07, 0x35, 0xcc, 0xb2, 0x88,
},
},
{
.data_len = 63,
.digest = {
0xfa, 0xd3, 0xf6, 0xe6, 0x7b, 0x1a, 0xc6, 0x05,
0x73, 0x35, 0x02, 0xab, 0xc7, 0xb3, 0x47, 0xcb,
},
},
{
.data_len = 64,
.digest = {
0xc5, 0x59, 0x29, 0xe9, 0x0a, 0x4a, 0x86, 0x43,
0x7c, 0xaf, 0xdf, 0x83, 0xd3, 0xb8, 0x33, 0x5f,
},
},
{
.data_len = 65,
.digest = {
0x80, 0x05, 0x75, 0x39, 0xec, 0x44, 0x8a, 0x81,
0xe7, 0x6e, 0x8d, 0xd1, 0xc6, 0xeb, 0xc2, 0xf0,
},
},
{
.data_len = 127,
.digest = {
0x3f, 0x02, 0xe8, 0xc6, 0xb8, 0x6a, 0x39, 0xc3,
0xa4, 0x1c, 0xd9, 0x8f, 0x4a, 0x71, 0x40, 0x30,
},
},
{
.data_len = 128,
.digest = {
0x89, 0x4f, 0x79, 0x3e, 0xff, 0x0c, 0x22, 0x60,
0xa2, 0xdc, 0x10, 0x5f, 0x23, 0x0a, 0xe7, 0xc6,
},
},
{
.data_len = 129,
.digest = {
0x06, 0x56, 0x61, 0xb8, 0x8a, 0x82, 0x77, 0x1b,
0x2c, 0x35, 0xb8, 0x9f, 0xd6, 0xf7, 0xbd, 0x5a,
},
},
{
.data_len = 256,
.digest = {
0x5d, 0xdf, 0x7d, 0xc8, 0x43, 0x96, 0x3b, 0xdb,
0xc7, 0x0e, 0x44, 0x42, 0x23, 0xf7, 0xed, 0xdf,
},
},
{
.data_len = 511,
.digest = {
0xf6, 0x5f, 0x26, 0x51, 0x8a, 0x5a, 0x46, 0x8f,
0x48, 0x72, 0x90, 0x74, 0x9d, 0x87, 0xbd, 0xdf,
},
},
{
.data_len = 513,
.digest = {
0xd8, 0x2c, 0xc9, 0x76, 0xfa, 0x67, 0x2e, 0xa6,
0xc8, 0x12, 0x4a, 0x64, 0xaa, 0x0b, 0x3d, 0xbd,
},
},
{
.data_len = 1000,
.digest = {
0xe2, 0x7e, 0xb4, 0x5f, 0xe1, 0x74, 0x51, 0xfc,
0xe0, 0xc8, 0xd5, 0xe6, 0x8b, 0x40, 0xd2, 0x0e,
},
},
{
.data_len = 3333,
.digest = {
0xcd, 0x7d, 0x56, 0xa9, 0x4c, 0x47, 0xea, 0xc2,
0x34, 0x0b, 0x84, 0x05, 0xf9, 0xad, 0xbb, 0x46,
},
},
{
.data_len = 4096,
.digest = {
0x63, 0x6e, 0x58, 0xb3, 0x94, 0x6b, 0x83, 0x5f,
0x1f, 0x0e, 0xd3, 0x66, 0x78, 0x71, 0x98, 0x42,
},
},
{
.data_len = 4128,
.digest = {
0x9d, 0x68, 0xfc, 0x26, 0x8b, 0x4c, 0xa8, 0xe7,
0x30, 0x0b, 0x19, 0x52, 0x6e, 0xa5, 0x65, 0x1c,
},
},
{
.data_len = 4160,
.digest = {
0x1c, 0xaa, 0x7d, 0xee, 0x91, 0x01, 0xe2, 0x5a,
0xec, 0xe9, 0xde, 0x57, 0x0a, 0xb6, 0x4c, 0x2f,
},
},
{
.data_len = 4224,
.digest = {
0x1b, 0x31, 0xe3, 0x14, 0x07, 0x16, 0x17, 0xc6,
0x98, 0x79, 0x88, 0x23, 0xb6, 0x3b, 0x25, 0xc4,
},
},
{
.data_len = 16384,
.digest = {
0xc6, 0x3d, 0x56, 0x90, 0xf0, 0xf6, 0xe6, 0x50,
0xf4, 0x76, 0x78, 0x67, 0xa3, 0xdd, 0x62, 0x7b,
},
},
};
static const u8 hash_testvec_consolidated[MD5_DIGEST_SIZE] = {
0x70, 0x86, 0x9e, 0x6c, 0xa4, 0xc6, 0x71, 0x43,
0x26, 0x02, 0x1b, 0x3f, 0xfd, 0x56, 0x9f, 0xa6,
};
static const u8 hmac_testvec_consolidated[MD5_DIGEST_SIZE] = {
0x10, 0x02, 0x74, 0xf6, 0x4d, 0xb3, 0x3c, 0xc7,
0xa1, 0xf7, 0xe6, 0xd4, 0x32, 0x64, 0xfa, 0x6d,
};
+39
View File
@@ -0,0 +1,39 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright 2025 Google LLC
*/
#include <crypto/md5.h>
#include "md5-testvecs.h"
#define HASH md5
#define HASH_CTX md5_ctx
#define HASH_SIZE MD5_DIGEST_SIZE
#define HASH_INIT md5_init
#define HASH_UPDATE md5_update
#define HASH_FINAL md5_final
#define HMAC_KEY hmac_md5_key
#define HMAC_CTX hmac_md5_ctx
#define HMAC_PREPAREKEY hmac_md5_preparekey
#define HMAC_INIT hmac_md5_init
#define HMAC_UPDATE hmac_md5_update
#define HMAC_FINAL hmac_md5_final
#define HMAC hmac_md5
#define HMAC_USINGRAWKEY hmac_md5_usingrawkey
#include "hash-test-template.h"
static struct kunit_case hash_test_cases[] = {
HASH_KUNIT_CASES,
KUNIT_CASE(benchmark_hash),
{},
};
static struct kunit_suite hash_test_suite = {
.name = "md5",
.test_cases = hash_test_cases,
.suite_init = hash_suite_init,
.suite_exit = hash_suite_exit,
};
kunit_test_suite(hash_test_suite);
MODULE_DESCRIPTION("KUnit tests and benchmark for MD5 and HMAC-MD5");
MODULE_LICENSE("GPL");
File diff suppressed because it is too large Load Diff
+438
View File
@@ -0,0 +1,438 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* KUnit tests and benchmark for ML-DSA
*
* Copyright 2025 Google LLC
*/
#include <crypto/mldsa.h>
#include <kunit/test.h>
#include <linux/random.h>
#include <linux/unaligned.h>
#define Q 8380417 /* The prime q = 2^23 - 2^13 + 1 */
/* ML-DSA parameters that the tests use */
static const struct {
int sig_len;
int pk_len;
int k;
int lambda;
int gamma1;
int beta;
int omega;
} params[] = {
[MLDSA44] = {
.sig_len = MLDSA44_SIGNATURE_SIZE,
.pk_len = MLDSA44_PUBLIC_KEY_SIZE,
.k = 4,
.lambda = 128,
.gamma1 = 1 << 17,
.beta = 78,
.omega = 80,
},
[MLDSA65] = {
.sig_len = MLDSA65_SIGNATURE_SIZE,
.pk_len = MLDSA65_PUBLIC_KEY_SIZE,
.k = 6,
.lambda = 192,
.gamma1 = 1 << 19,
.beta = 196,
.omega = 55,
},
[MLDSA87] = {
.sig_len = MLDSA87_SIGNATURE_SIZE,
.pk_len = MLDSA87_PUBLIC_KEY_SIZE,
.k = 8,
.lambda = 256,
.gamma1 = 1 << 19,
.beta = 120,
.omega = 75,
},
};
#include "mldsa-testvecs.h"
static void do_mldsa_and_assert_success(struct kunit *test,
const struct mldsa_testvector *tv)
{
int err = mldsa_verify(tv->alg, tv->sig, tv->sig_len, tv->msg,
tv->msg_len, tv->pk, tv->pk_len);
KUNIT_ASSERT_EQ(test, err, 0);
}
static u8 *kunit_kmemdup_or_fail(struct kunit *test, const u8 *src, size_t len)
{
u8 *dst = kunit_kmalloc(test, len, GFP_KERNEL);
KUNIT_ASSERT_NOT_NULL(test, dst);
return memcpy(dst, src, len);
}
/*
* Test that changing coefficients in a valid signature's z vector results in
* the following behavior from mldsa_verify():
*
* * -EBADMSG if a coefficient is changed to have an out-of-range value, i.e.
* absolute value >= gamma1 - beta, corresponding to the verifier detecting
* the out-of-range coefficient and rejecting the signature as malformed
*
* * -EKEYREJECTED if a coefficient is changed to a different in-range value,
* i.e. absolute value < gamma1 - beta, corresponding to the verifier
* continuing to the "real" signature check and that check failing
*/
static void test_mldsa_z_range(struct kunit *test,
const struct mldsa_testvector *tv)
{
u8 *sig = kunit_kmemdup_or_fail(test, tv->sig, tv->sig_len);
const int lambda = params[tv->alg].lambda;
const s32 gamma1 = params[tv->alg].gamma1;
const int beta = params[tv->alg].beta;
/*
* We just modify the first coefficient. The coefficient is gamma1
* minus either the first 18 or 20 bits of the u32, depending on gamma1.
*
* The layout of ML-DSA signatures is ctilde || z || h. ctilde is
* lambda / 4 bytes, so z starts at &sig[lambda / 4].
*/
u8 *z_ptr = &sig[lambda / 4];
const u32 z_data = get_unaligned_le32(z_ptr);
const u32 mask = (gamma1 << 1) - 1;
/* These are the four boundaries of the out-of-range values. */
const s32 out_of_range_coeffs[] = {
-gamma1 + 1,
-(gamma1 - beta),
gamma1,
gamma1 - beta,
};
/*
* These are the two boundaries of the valid range, along with 0. We
* assume that none of these matches the original coefficient.
*/
const s32 in_range_coeffs[] = {
-(gamma1 - beta - 1),
0,
gamma1 - beta - 1,
};
/* Initially the signature is valid. */
do_mldsa_and_assert_success(test, tv);
/* Test some out-of-range coefficients. */
for (int i = 0; i < ARRAY_SIZE(out_of_range_coeffs); i++) {
const s32 c = out_of_range_coeffs[i];
put_unaligned_le32((z_data & ~mask) | (mask & (gamma1 - c)),
z_ptr);
KUNIT_ASSERT_EQ(test, -EBADMSG,
mldsa_verify(tv->alg, sig, tv->sig_len, tv->msg,
tv->msg_len, tv->pk, tv->pk_len));
}
/* Test some in-range coefficients. */
for (int i = 0; i < ARRAY_SIZE(in_range_coeffs); i++) {
const s32 c = in_range_coeffs[i];
put_unaligned_le32((z_data & ~mask) | (mask & (gamma1 - c)),
z_ptr);
KUNIT_ASSERT_EQ(test, -EKEYREJECTED,
mldsa_verify(tv->alg, sig, tv->sig_len, tv->msg,
tv->msg_len, tv->pk, tv->pk_len));
}
}
/* Test that mldsa_verify() rejects malformed hint vectors with -EBADMSG. */
static void test_mldsa_bad_hints(struct kunit *test,
const struct mldsa_testvector *tv)
{
const int omega = params[tv->alg].omega;
const int k = params[tv->alg].k;
u8 *sig = kunit_kmemdup_or_fail(test, tv->sig, tv->sig_len);
/* Pointer to the encoded hint vector in the signature */
u8 *hintvec = &sig[tv->sig_len - omega - k];
u8 h;
/* Initially the signature is valid. */
do_mldsa_and_assert_success(test, tv);
/* Cumulative hint count exceeds omega */
memcpy(sig, tv->sig, tv->sig_len);
hintvec[omega + k - 1] = omega + 1;
KUNIT_ASSERT_EQ(test, -EBADMSG,
mldsa_verify(tv->alg, sig, tv->sig_len, tv->msg,
tv->msg_len, tv->pk, tv->pk_len));
/* Cumulative hint count decreases */
memcpy(sig, tv->sig, tv->sig_len);
KUNIT_ASSERT_GE(test, hintvec[omega + k - 2], 1);
hintvec[omega + k - 1] = hintvec[omega + k - 2] - 1;
KUNIT_ASSERT_EQ(test, -EBADMSG,
mldsa_verify(tv->alg, sig, tv->sig_len, tv->msg,
tv->msg_len, tv->pk, tv->pk_len));
/*
* Hint indices out of order. To test this, swap hintvec[0] and
* hintvec[1]. This assumes that the original valid signature had at
* least two nonzero hints in the first element (asserted below).
*/
memcpy(sig, tv->sig, tv->sig_len);
KUNIT_ASSERT_GE(test, hintvec[omega], 2);
h = hintvec[0];
hintvec[0] = hintvec[1];
hintvec[1] = h;
KUNIT_ASSERT_EQ(test, -EBADMSG,
mldsa_verify(tv->alg, sig, tv->sig_len, tv->msg,
tv->msg_len, tv->pk, tv->pk_len));
/*
* Extra hint indices given. For this test to work, the original valid
* signature must have fewer than omega nonzero hints (asserted below).
*/
memcpy(sig, tv->sig, tv->sig_len);
KUNIT_ASSERT_LT(test, hintvec[omega + k - 1], omega);
hintvec[omega - 1] = 0xff;
KUNIT_ASSERT_EQ(test, -EBADMSG,
mldsa_verify(tv->alg, sig, tv->sig_len, tv->msg,
tv->msg_len, tv->pk, tv->pk_len));
}
static void test_mldsa_mutation(struct kunit *test,
const struct mldsa_testvector *tv)
{
const int sig_len = tv->sig_len;
const int msg_len = tv->msg_len;
const int pk_len = tv->pk_len;
const int num_iter = 200;
u8 *sig = kunit_kmemdup_or_fail(test, tv->sig, sig_len);
u8 *msg = kunit_kmemdup_or_fail(test, tv->msg, msg_len);
u8 *pk = kunit_kmemdup_or_fail(test, tv->pk, pk_len);
/* Initially the signature is valid. */
do_mldsa_and_assert_success(test, tv);
/* Changing any bit in the signature should invalidate the signature */
for (int i = 0; i < num_iter; i++) {
size_t pos = get_random_u32_below(sig_len);
u8 b = 1 << get_random_u32_below(8);
sig[pos] ^= b;
KUNIT_ASSERT_NE(test, 0,
mldsa_verify(tv->alg, sig, sig_len, msg,
msg_len, pk, pk_len));
sig[pos] ^= b;
}
/* Changing any bit in the message should invalidate the signature */
for (int i = 0; i < num_iter; i++) {
size_t pos = get_random_u32_below(msg_len);
u8 b = 1 << get_random_u32_below(8);
msg[pos] ^= b;
KUNIT_ASSERT_NE(test, 0,
mldsa_verify(tv->alg, sig, sig_len, msg,
msg_len, pk, pk_len));
msg[pos] ^= b;
}
/* Changing any bit in the public key should invalidate the signature */
for (int i = 0; i < num_iter; i++) {
size_t pos = get_random_u32_below(pk_len);
u8 b = 1 << get_random_u32_below(8);
pk[pos] ^= b;
KUNIT_ASSERT_NE(test, 0,
mldsa_verify(tv->alg, sig, sig_len, msg,
msg_len, pk, pk_len));
pk[pos] ^= b;
}
/* All changes should have been undone. */
KUNIT_ASSERT_EQ(test, 0,
mldsa_verify(tv->alg, sig, sig_len, msg, msg_len, pk,
pk_len));
}
static void test_mldsa(struct kunit *test, const struct mldsa_testvector *tv)
{
/* Valid signature */
KUNIT_ASSERT_EQ(test, tv->sig_len, params[tv->alg].sig_len);
KUNIT_ASSERT_EQ(test, tv->pk_len, params[tv->alg].pk_len);
do_mldsa_and_assert_success(test, tv);
/* Signature too short */
KUNIT_ASSERT_EQ(test, -EBADMSG,
mldsa_verify(tv->alg, tv->sig, tv->sig_len - 1, tv->msg,
tv->msg_len, tv->pk, tv->pk_len));
/* Signature too long */
KUNIT_ASSERT_EQ(test, -EBADMSG,
mldsa_verify(tv->alg, tv->sig, tv->sig_len + 1, tv->msg,
tv->msg_len, tv->pk, tv->pk_len));
/* Public key too short */
KUNIT_ASSERT_EQ(test, -EBADMSG,
mldsa_verify(tv->alg, tv->sig, tv->sig_len, tv->msg,
tv->msg_len, tv->pk, tv->pk_len - 1));
/* Public key too long */
KUNIT_ASSERT_EQ(test, -EBADMSG,
mldsa_verify(tv->alg, tv->sig, tv->sig_len, tv->msg,
tv->msg_len, tv->pk, tv->pk_len + 1));
/*
* Message too short. Error is EKEYREJECTED because it gets rejected by
* the "real" signature check rather than the well-formedness checks.
*/
KUNIT_ASSERT_EQ(test, -EKEYREJECTED,
mldsa_verify(tv->alg, tv->sig, tv->sig_len, tv->msg,
tv->msg_len - 1, tv->pk, tv->pk_len));
/*
* Can't simply try (tv->msg, tv->msg_len + 1) too, as tv->msg would be
* accessed out of bounds. However, ML-DSA just hashes the message and
* doesn't handle different message lengths differently anyway.
*/
/* Test the validity checks on the z vector. */
test_mldsa_z_range(test, tv);
/* Test the validity checks on the hint vector. */
test_mldsa_bad_hints(test, tv);
/* Test randomly mutating the inputs. */
test_mldsa_mutation(test, tv);
}
static void test_mldsa44(struct kunit *test)
{
test_mldsa(test, &mldsa44_testvector);
}
static void test_mldsa65(struct kunit *test)
{
test_mldsa(test, &mldsa65_testvector);
}
static void test_mldsa87(struct kunit *test)
{
test_mldsa(test, &mldsa87_testvector);
}
static s32 mod(s32 a, s32 m)
{
a %= m;
if (a < 0)
a += m;
return a;
}
static s32 symmetric_mod(s32 a, s32 m)
{
a = mod(a, m);
if (a > m / 2)
a -= m;
return a;
}
/* Mechanical, inefficient translation of FIPS 204 Algorithm 36, Decompose */
static void decompose_ref(s32 r, s32 gamma2, s32 *r0, s32 *r1)
{
s32 rplus = mod(r, Q);
*r0 = symmetric_mod(rplus, 2 * gamma2);
if (rplus - *r0 == Q - 1) {
*r1 = 0;
*r0 = *r0 - 1;
} else {
*r1 = (rplus - *r0) / (2 * gamma2);
}
}
/* Mechanical, inefficient translation of FIPS 204 Algorithm 40, UseHint */
static s32 use_hint_ref(u8 h, s32 r, s32 gamma2)
{
s32 m = (Q - 1) / (2 * gamma2);
s32 r0, r1;
decompose_ref(r, gamma2, &r0, &r1);
if (h == 1 && r0 > 0)
return mod(r1 + 1, m);
if (h == 1 && r0 <= 0)
return mod(r1 - 1, m);
return r1;
}
/*
* Test that for all possible inputs, mldsa_use_hint() gives the same output as
* a mechanical translation of the pseudocode from FIPS 204.
*/
static void test_mldsa_use_hint(struct kunit *test)
{
for (int i = 0; i < 2; i++) {
const s32 gamma2 = (Q - 1) / (i == 0 ? 88 : 32);
for (u8 h = 0; h < 2; h++) {
for (s32 r = 0; r < Q; r++) {
KUNIT_ASSERT_EQ(test,
mldsa_use_hint(h, r, gamma2),
use_hint_ref(h, r, gamma2));
}
}
}
}
static void benchmark_mldsa(struct kunit *test,
const struct mldsa_testvector *tv)
{
const int warmup_niter = 200;
const int benchmark_niter = 200;
u64 t0, t1;
if (!IS_ENABLED(CONFIG_CRYPTO_LIB_BENCHMARK))
kunit_skip(test, "not enabled");
for (int i = 0; i < warmup_niter; i++)
do_mldsa_and_assert_success(test, tv);
t0 = ktime_get_ns();
for (int i = 0; i < benchmark_niter; i++)
do_mldsa_and_assert_success(test, tv);
t1 = ktime_get_ns();
kunit_info(test, "%llu ops/s",
div64_u64((u64)benchmark_niter * NSEC_PER_SEC,
t1 - t0 ?: 1));
}
static void benchmark_mldsa44(struct kunit *test)
{
benchmark_mldsa(test, &mldsa44_testvector);
}
static void benchmark_mldsa65(struct kunit *test)
{
benchmark_mldsa(test, &mldsa65_testvector);
}
static void benchmark_mldsa87(struct kunit *test)
{
benchmark_mldsa(test, &mldsa87_testvector);
}
static struct kunit_case mldsa_kunit_cases[] = {
KUNIT_CASE(test_mldsa44),
KUNIT_CASE(test_mldsa65),
KUNIT_CASE(test_mldsa87),
KUNIT_CASE(test_mldsa_use_hint),
KUNIT_CASE(benchmark_mldsa44),
KUNIT_CASE(benchmark_mldsa65),
KUNIT_CASE(benchmark_mldsa87),
{},
};
static struct kunit_suite mldsa_kunit_suite = {
.name = "mldsa",
.test_cases = mldsa_kunit_cases,
};
kunit_test_suite(mldsa_kunit_suite);
MODULE_DESCRIPTION("KUnit tests and benchmark for ML-DSA");
MODULE_IMPORT_NS("EXPORTED_FOR_KUNIT_TESTING");
MODULE_LICENSE("GPL");
+298
View File
@@ -0,0 +1,298 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */
/* This file was generated by: ./scripts/crypto/gen-hash-testvecs.py nh */
static const u8 nh_test_key[NH_KEY_BYTES] = {
0x04, 0x59, 0x66, 0x92, 0x81, 0xd7, 0xe9, 0x25,
0x68, 0xfa, 0xb0, 0xca, 0x9f, 0xea, 0x98, 0xca,
0xcd, 0xbf, 0x6d, 0xa5, 0x0c, 0x22, 0xc3, 0x57,
0xdc, 0x35, 0x05, 0xdd, 0x5b, 0xb0, 0xce, 0xf6,
0xb2, 0x4c, 0x77, 0x2e, 0xd2, 0x63, 0xf0, 0x17,
0x60, 0xd8, 0xd3, 0xd9, 0xed, 0x34, 0xb6, 0xed,
0x6a, 0x11, 0xc0, 0x25, 0xda, 0xba, 0x7e, 0xef,
0x49, 0x13, 0xf7, 0xd9, 0xfc, 0xb6, 0xfd, 0x58,
0xe9, 0x5f, 0xc5, 0xc4, 0x69, 0x89, 0xba, 0xa6,
0x2b, 0x58, 0x8d, 0x36, 0x6c, 0xb9, 0x90, 0x1e,
0x64, 0xc7, 0x44, 0x84, 0x03, 0x70, 0x30, 0x47,
0xdd, 0x58, 0xf4, 0x87, 0x61, 0xfd, 0x9c, 0x6b,
0x51, 0x1b, 0x39, 0x1d, 0x6d, 0x50, 0xae, 0x19,
0x71, 0x03, 0xc7, 0xa7, 0x42, 0x82, 0x8f, 0xa5,
0x63, 0x6a, 0xe2, 0x8a, 0xad, 0x4b, 0x40, 0xa7,
0x3f, 0x8b, 0xe4, 0xae, 0xb2, 0x8a, 0x14, 0x78,
0x91, 0x07, 0xba, 0x02, 0x08, 0xc1, 0x34, 0xb8,
0xda, 0x61, 0x67, 0xf6, 0x98, 0x97, 0x1a, 0xcb,
0x0f, 0x82, 0x80, 0xff, 0x02, 0x54, 0x16, 0x57,
0x18, 0x35, 0xaf, 0x16, 0x17, 0x68, 0xcc, 0xc7,
0x52, 0xac, 0x31, 0x39, 0x60, 0xe4, 0xb4, 0xcb,
0x0e, 0xf9, 0x57, 0xe9, 0x96, 0xff, 0x99, 0xd6,
0x10, 0x96, 0x09, 0xab, 0x28, 0x92, 0x1b, 0x9f,
0x10, 0xde, 0x3e, 0x87, 0xb8, 0x9d, 0x2d, 0xa0,
0x3c, 0x91, 0x85, 0x8c, 0x9e, 0xc0, 0x97, 0x9a,
0xb4, 0x54, 0x7f, 0x4a, 0x63, 0xc2, 0x75, 0x0f,
0x0d, 0x2f, 0x62, 0x56, 0x48, 0x0e, 0xb6, 0xc7,
0xcf, 0x0d, 0x78, 0xca, 0xbd, 0x31, 0x9e, 0x4c,
0xf7, 0x3f, 0x9e, 0xc2, 0xea, 0x5e, 0x44, 0x6d,
0x76, 0xf9, 0xc5, 0xe0, 0x29, 0xea, 0x15, 0xbf,
0xaf, 0xd4, 0x75, 0xc8, 0x89, 0xcf, 0x4f, 0x17,
0xfd, 0x4a, 0x45, 0xa5, 0x4d, 0x2d, 0x87, 0x11,
0x2b, 0x3e, 0x64, 0xa2, 0x6b, 0xc5, 0x23, 0x8c,
0xfa, 0x71, 0x13, 0x72, 0x0e, 0x7c, 0xe1, 0x2c,
0x9f, 0x0e, 0x29, 0xc9, 0x15, 0xde, 0x4e, 0xd7,
0x42, 0x1f, 0x8e, 0xe1, 0x91, 0x99, 0x50, 0x38,
0x7f, 0x15, 0xc0, 0xf6, 0x4b, 0xfd, 0x9d, 0x40,
0xe9, 0x44, 0x51, 0xca, 0x3b, 0x83, 0x41, 0x9f,
0x82, 0x64, 0x66, 0x22, 0x12, 0x43, 0x1c, 0x4f,
0x45, 0x11, 0x3a, 0x46, 0xb1, 0x7c, 0x62, 0x0a,
0x9d, 0x4c, 0x99, 0x85, 0xb0, 0x10, 0x19, 0xcf,
0xeb, 0xf9, 0x65, 0xaf, 0xd8, 0x05, 0x9e, 0x61,
0x03, 0x5f, 0x15, 0x99, 0xa9, 0x05, 0x20, 0xc8,
0xaf, 0xab, 0x31, 0x9d, 0xd5, 0xdf, 0x24, 0xce,
0x2b, 0x6d, 0xd7, 0x17, 0xc3, 0x04, 0xff, 0x82,
0xa7, 0x18, 0x39, 0xe9, 0x0d, 0x0a, 0x5f, 0xb9,
0xc9, 0x86, 0x1d, 0xf8, 0x02, 0x2d, 0xc3, 0x88,
0x28, 0x73, 0x5c, 0xac, 0x25, 0xc9, 0xfe, 0xcb,
0xd2, 0xfd, 0x63, 0x74, 0xac, 0xe1, 0xb8, 0xa2,
0xc6, 0x2b, 0xb5, 0x40, 0x01, 0x9b, 0xed, 0xee,
0x7b, 0x63, 0x66, 0x05, 0x45, 0xc2, 0x6c, 0xd8,
0x58, 0xf1, 0xa1, 0x3d, 0xc8, 0x43, 0x59, 0x4b,
0x39, 0x87, 0x24, 0x64, 0x92, 0xb0, 0xab, 0x75,
0xf1, 0xb7, 0xbf, 0x7c, 0xde, 0xc0, 0xaf, 0x4a,
0xc2, 0x7b, 0xd9, 0x8a, 0x99, 0xcd, 0x83, 0x01,
0xe6, 0xae, 0xeb, 0x16, 0xe7, 0x54, 0x9c, 0x95,
0x0a, 0x91, 0x02, 0xaf, 0x9f, 0x79, 0x40, 0x45,
0xce, 0x47, 0x41, 0x65, 0xca, 0x80, 0x0d, 0x14,
0x46, 0x58, 0x5d, 0x4d, 0x28, 0x55, 0x70, 0x49,
0x7c, 0x32, 0x1f, 0x01, 0xaa, 0x05, 0x2f, 0xf1,
0xeb, 0xa3, 0xe6, 0x1d, 0xf9, 0x43, 0xe0, 0x58,
0x05, 0x61, 0x22, 0xc3, 0xee, 0xe4, 0x6f, 0x94,
0xaf, 0x82, 0xda, 0x18, 0x18, 0x63, 0x9c, 0xfa,
0xc0, 0x04, 0x27, 0xc5, 0x39, 0x5e, 0x7a, 0xa6,
0x85, 0x46, 0xb7, 0x76, 0xc9, 0x16, 0xf2, 0xf8,
0x40, 0x8d, 0x4b, 0x5e, 0x72, 0xf3, 0x3e, 0x12,
0xa4, 0x80, 0x39, 0xb2, 0x92, 0xfe, 0x6e, 0x5b,
0x5b, 0xad, 0xea, 0x29, 0xbc, 0x66, 0xe6, 0xfe,
0x80, 0x02, 0x5d, 0x83, 0x37, 0xfc, 0xde, 0x6c,
0x25, 0x54, 0xa2, 0xff, 0x7d, 0xb6, 0xe1, 0xd6,
0xcf, 0xdb, 0x60, 0xe3, 0xbe, 0x2f, 0x4e, 0xb4,
0xf5, 0xb4, 0x51, 0xf7, 0x5a, 0x25, 0xda, 0x40,
0x84, 0x5e, 0xc0, 0x0a, 0x6b, 0xfa, 0x0c, 0xfb,
0x5e, 0x3e, 0x12, 0x6c, 0x39, 0x35, 0xc0, 0x28,
0xd6, 0x1b, 0x3a, 0x72, 0xc3, 0xfe, 0xa5, 0x4c,
0x35, 0xa2, 0x42, 0xf6, 0x3d, 0xa5, 0xbf, 0xb5,
0x39, 0xe3, 0xc9, 0xd5, 0x8c, 0x1b, 0xe5, 0xef,
0x91, 0xd2, 0x80, 0x6f, 0xcc, 0x77, 0x44, 0x50,
0x62, 0xc7, 0xac, 0x29, 0xcb, 0x72, 0xda, 0x6d,
0xc5, 0xfe, 0xa7, 0xee, 0x8b, 0xeb, 0xfc, 0xa3,
0x46, 0x18, 0x5f, 0xaa, 0xc3, 0x65, 0xd0, 0x8f,
0x67, 0x98, 0xd6, 0xce, 0x5f, 0x84, 0xd4, 0x96,
0x1b, 0x67, 0xa0, 0xcf, 0xfc, 0x94, 0x55, 0x5e,
0x4b, 0x51, 0x68, 0xa7, 0x6d, 0x02, 0xf9, 0x53,
0x54, 0x86, 0x6b, 0x53, 0x39, 0xe0, 0x36, 0x23,
0x87, 0x1a, 0xfb, 0x53, 0x1a, 0x65, 0xd8, 0x42,
0xa8, 0x85, 0xfd, 0x2c, 0x7f, 0x6b, 0x7f, 0x67,
0x70, 0x23, 0x6c, 0xe9, 0x0b, 0xf0, 0x1e, 0x0d,
0x0b, 0xb4, 0xd4, 0x96, 0x14, 0x95, 0x7e, 0xf3,
0x9b, 0xdd, 0xd7, 0xc4, 0x24, 0x22, 0xb9, 0x9d,
0xb3, 0xa6, 0xac, 0x09, 0x7c, 0x00, 0xbf, 0xd0,
0xdc, 0xfb, 0x9b, 0x7c, 0x8c, 0xbd, 0xd4, 0x1a,
0x13, 0x2b, 0x82, 0x3d, 0x7c, 0x8c, 0x10, 0x47,
0x49, 0x6c, 0x53, 0xeb, 0xa7, 0xc2, 0xde, 0xed,
0xe2, 0x55, 0x93, 0x2c, 0x1a, 0x5a, 0x7d, 0xe1,
0x37, 0x62, 0xdd, 0x29, 0x1a, 0x72, 0x82, 0xc0,
0x14, 0x73, 0x5d, 0x0e, 0x9b, 0xcc, 0x54, 0x68,
0x3a, 0x4d, 0x56, 0x8f, 0xc9, 0x4e, 0xaf, 0x7b,
0xde, 0x17, 0x9c, 0x5e, 0x83, 0x82, 0x22, 0xe3,
0x28, 0xdf, 0x1b, 0xb6, 0xdb, 0x17, 0x90, 0x48,
0xb5, 0x13, 0x4e, 0xd3, 0x97, 0x5e, 0xb3, 0x9c,
0x16, 0x08, 0xc8, 0x77, 0xb3, 0xcd, 0x94, 0x90,
0x4f, 0x77, 0xaf, 0x67, 0xdd, 0x80, 0x15, 0x1c,
0x59, 0xfb, 0x3c, 0xec, 0xf8, 0xb3, 0x67, 0xfb,
0xa0, 0x94, 0x3c, 0x53, 0x99, 0x49, 0x94, 0x2c,
0x85, 0x26, 0x92, 0x6d, 0x8d, 0x48, 0xf6, 0x72,
0xdd, 0xfb, 0xb2, 0x10, 0x51, 0x5b, 0xbe, 0xd5,
0x70, 0x3d, 0x28, 0x94, 0x98, 0x4f, 0x6e, 0x20,
0x7b, 0x7d, 0x0f, 0x56, 0xc9, 0x96, 0x5f, 0x60,
0x2e, 0x2f, 0x9b, 0x38, 0x7f, 0xc7, 0x3c, 0x6b,
0x2f, 0x2b, 0x8f, 0x1f, 0x07, 0x1c, 0x85, 0x57,
0x16, 0x2e, 0xc7, 0x74, 0xe5, 0xf2, 0x0d, 0xfe,
0xef, 0x57, 0xb0, 0xa4, 0x4f, 0x4c, 0x7d, 0x81,
0xbb, 0xaa, 0xcb, 0xa0, 0xb0, 0x51, 0xcf, 0xc2,
0xee, 0x90, 0x2e, 0x5e, 0x27, 0xca, 0xd3, 0xe8,
0xf3, 0x55, 0x02, 0x56, 0x06, 0xa5, 0xad, 0xdf,
0xa3, 0xa9, 0x06, 0x05, 0x53, 0x74, 0x55, 0xd5,
0xd2, 0x20, 0x0a, 0x6d, 0x4a, 0xef, 0x16, 0xbf,
0xc3, 0xb2, 0x75, 0x93, 0xd8, 0x6e, 0x0f, 0xd2,
0xae, 0x3b, 0xc0, 0x00, 0x22, 0x6f, 0xb5, 0x0a,
0x41, 0xfc, 0xf9, 0x41, 0xfc, 0x16, 0x4f, 0xa6,
0x1c, 0x18, 0x41, 0x67, 0x73, 0xa8, 0x79, 0xa9,
0x54, 0x18, 0x4e, 0x88, 0x44, 0x0f, 0xa1, 0x5b,
0xf0, 0x68, 0xea, 0x3c, 0x62, 0x59, 0x8d, 0xc7,
0x6f, 0xd7, 0x72, 0x20, 0x74, 0x39, 0xd4, 0x3a,
0x41, 0x1b, 0x58, 0x57, 0x54, 0x85, 0x60, 0xca,
0x49, 0x4b, 0xa1, 0x04, 0x91, 0xb6, 0xf2, 0xcd,
0x62, 0x63, 0x67, 0xd1, 0xee, 0x6b, 0x9e, 0x5d,
0xd6, 0xc4, 0x58, 0x6b, 0xe1, 0xe6, 0x4a, 0xdb,
0xe8, 0xb1, 0x35, 0x03, 0x15, 0x8d, 0x34, 0x69,
0x4c, 0xd2, 0x54, 0xce, 0xe8, 0x6a, 0x69, 0x6f,
0xaa, 0xb5, 0x1f, 0x86, 0xed, 0xac, 0x4f, 0x16,
0x1e, 0x48, 0x93, 0xe8, 0x6c, 0x24, 0x1c, 0xd0,
0xbb, 0x61, 0xc2, 0x34, 0xdd, 0xc9, 0x5c, 0xce,
};
static const u8 nh_test_msg[NH_MESSAGE_BYTES] = {
0x99, 0x57, 0x61, 0x41, 0xad, 0x08, 0x7e, 0x17,
0xd4, 0xef, 0x0b, 0x23, 0xff, 0x0b, 0x96, 0x0a,
0x6c, 0x98, 0xac, 0x78, 0x5e, 0xb6, 0xb2, 0x67,
0x0f, 0x48, 0xf4, 0xa1, 0xe5, 0x1e, 0xfe, 0x83,
0xe4, 0x56, 0x2a, 0x03, 0x64, 0xff, 0x7a, 0xf3,
0x03, 0xfe, 0xa7, 0x86, 0xdc, 0x35, 0x79, 0x13,
0xf8, 0xe1, 0x59, 0x19, 0x04, 0x43, 0x24, 0x82,
0x44, 0x82, 0x41, 0x2b, 0xc7, 0xcf, 0xf5, 0xa4,
0xdc, 0xca, 0xf5, 0x34, 0xc4, 0x23, 0x3c, 0x1f,
0xa8, 0x84, 0x1f, 0x2a, 0xcd, 0xae, 0x9d, 0x5e,
0x05, 0xe2, 0xfb, 0x0c, 0x68, 0x81, 0x90, 0x11,
0x44, 0xf6, 0xdd, 0x5b, 0x51, 0xd3, 0xe0, 0xab,
0x29, 0x3a, 0xa9, 0x9c, 0xf6, 0x7e, 0x2d, 0xe3,
0x6c, 0x09, 0x59, 0xd7, 0xfa, 0x7f, 0x6a, 0x33,
0x3b, 0x23, 0x7b, 0x1b, 0xb2, 0x79, 0x5f, 0x5c,
0xb6, 0x2d, 0xb0, 0xf8, 0xab, 0x33, 0x28, 0xe0,
0x72, 0x2e, 0x2f, 0x03, 0x22, 0x16, 0xb4, 0x87,
0xf7, 0x14, 0x3f, 0x55, 0x8a, 0xb0, 0x47, 0xdb,
0x42, 0x2d, 0xc0, 0x0c, 0x0a, 0x33, 0xf8, 0xab,
0x44, 0xae, 0xa3, 0xc9, 0xfc, 0xf6, 0x34, 0x8c,
0x60, 0x30, 0x6d, 0x31, 0x70, 0xf3, 0x39, 0x53,
0xf1, 0x2d, 0xb9, 0x6c, 0xa6, 0x48, 0x9c, 0x9c,
0xc2, 0x88, 0xb3, 0xa9, 0x98, 0xb6, 0xc3, 0x47,
0x94, 0x02, 0x9d, 0x98, 0x6e, 0x25, 0x6c, 0xf5,
0x9b, 0xc6, 0x4d, 0xee, 0x07, 0x1e, 0x25, 0x8f,
0x01, 0xde, 0xad, 0xe5, 0x77, 0x4f, 0xd1, 0xc0,
0x62, 0xbb, 0x3a, 0xb9, 0x83, 0x0b, 0x29, 0x76,
0x4f, 0xb1, 0x86, 0x2c, 0x27, 0xc7, 0x38, 0x65,
0xcb, 0x78, 0xb7, 0x02, 0x10, 0x9e, 0xde, 0x83,
0xd1, 0xac, 0x05, 0x86, 0x23, 0xce, 0x4f, 0x8d,
0xcc, 0x4e, 0x3f, 0x04, 0xf4, 0x39, 0x91, 0x81,
0x1c, 0x42, 0x47, 0x4d, 0x50, 0xe5, 0x01, 0x22,
0x98, 0xcf, 0x91, 0x36, 0xb3, 0x7c, 0xcf, 0x78,
0x07, 0x22, 0xa9, 0x18, 0xd2, 0xcd, 0x7d, 0x4d,
0xa6, 0xcb, 0xaa, 0x52, 0x13, 0x49, 0x64, 0xb0,
0xa5, 0x3d, 0xc7, 0xc3, 0x10, 0x87, 0x2e, 0x76,
0xa9, 0x52, 0xc5, 0x50, 0x18, 0xc0, 0x5d, 0xb4,
0x4c, 0xc6, 0x7f, 0x64, 0xae, 0x53, 0xc3, 0x46,
0x99, 0xb7, 0x61, 0x6b, 0x08, 0x43, 0x08, 0x4c,
0x90, 0x2c, 0xee, 0x56, 0x91, 0xb4, 0x28, 0xa8,
0xa8, 0x8b, 0x3b, 0x1a, 0x67, 0x71, 0xf2, 0x81,
0x48, 0x20, 0x71, 0x30, 0xdd, 0x69, 0x8a, 0xc2,
0x4c, 0x9d, 0x4e, 0x17, 0xfb, 0x2e, 0xe7, 0x9b,
0x86, 0x94, 0xa5, 0xce, 0xf9, 0x74, 0x56, 0xff,
0x3b, 0xff, 0xd9, 0x5a, 0xc8, 0x98, 0xf5, 0x25,
0xa2, 0xb9, 0x66, 0x46, 0x89, 0x17, 0x39, 0x08,
0x69, 0x03, 0x59, 0x1e, 0x13, 0x12, 0x68, 0xe7,
0x2f, 0x00, 0xd3, 0xf3, 0x71, 0xd1, 0x20, 0xc5,
0x0b, 0x38, 0x89, 0xda, 0x62, 0x3c, 0xce, 0xea,
0x04, 0x19, 0x47, 0x6d, 0xd8, 0x64, 0x38, 0x60,
0x96, 0x71, 0x68, 0x48, 0x79, 0xf8, 0xf4, 0x76,
0x33, 0xf6, 0x60, 0x8d, 0x21, 0xd0, 0xee, 0x41,
0xc0, 0xbe, 0x33, 0x61, 0x5e, 0x66, 0xe6, 0x16,
0x14, 0xc7, 0xfb, 0x6c, 0xf3, 0x58, 0xef, 0x12,
0x7c, 0x70, 0x65, 0x5d, 0x55, 0xe8, 0xf2, 0x92,
0x3a, 0xfe, 0x34, 0x64, 0x31, 0x7c, 0x29, 0xbb,
0x01, 0x18, 0xbd, 0xb6, 0xe4, 0x1e, 0xa4, 0xf3,
0x7b, 0x4c, 0x6a, 0x0d, 0x01, 0xfc, 0xc7, 0x66,
0xc3, 0x88, 0x37, 0x25, 0xcf, 0xe9, 0xca, 0x82,
0xeb, 0xa1, 0x38, 0x40, 0xc9, 0xdb, 0x38, 0x7b,
0x78, 0xcf, 0x11, 0xa3, 0x1c, 0x6b, 0x70, 0xc8,
0xe1, 0x2f, 0x7c, 0x17, 0x2c, 0x58, 0x28, 0xa4,
0x13, 0x40, 0xc7, 0x69, 0x0f, 0x04, 0xe5, 0x8e,
0xf0, 0x67, 0x53, 0xea, 0x10, 0xf5, 0x83, 0xc9,
0xcb, 0x6b, 0x16, 0xef, 0x2e, 0x55, 0xb3, 0xdd,
0xed, 0xf9, 0x1a, 0x52, 0x9a, 0x73, 0x78, 0x14,
0x14, 0x21, 0xfc, 0xef, 0x3c, 0x40, 0xa9, 0xfe,
0xef, 0xd7, 0x6e, 0x28, 0x2f, 0xd3, 0x73, 0xed,
0xa3, 0x73, 0xb5, 0x62, 0x41, 0xe6, 0xd4, 0x79,
0x49, 0x31, 0x2b, 0x86, 0x74, 0x56, 0x21, 0xfe,
0x6d, 0xb2, 0xbe, 0x81, 0x80, 0xa6, 0x81, 0x19,
0x90, 0x79, 0x6f, 0xc4, 0x4e, 0x7d, 0x6f, 0x2f,
0xa8, 0x6f, 0xd5, 0xc4, 0x7e, 0x23, 0x3b, 0xe6,
0x9b, 0x60, 0x97, 0x7b, 0xe2, 0x08, 0x8a, 0xaa,
0xc7, 0x7c, 0xf6, 0xe5, 0x01, 0x3e, 0xd2, 0x29,
0x7d, 0xd7, 0x40, 0x84, 0x95, 0xfa, 0xdf, 0xd8,
0x81, 0xe9, 0x5e, 0xdd, 0x0d, 0x17, 0x51, 0x6b,
0x8c, 0x0e, 0x47, 0xf9, 0x0c, 0x92, 0x1b, 0x60,
0xca, 0x06, 0x8a, 0xe5, 0xe8, 0x0f, 0x06, 0x75,
0x5d, 0x76, 0xc9, 0x32, 0x2c, 0x52, 0x2c, 0x2e,
0xd8, 0x66, 0x38, 0x75, 0x16, 0xc7, 0x7d, 0x51,
0xc4, 0xc2, 0x22, 0xc8, 0x19, 0xfc, 0x3d, 0x69,
0x1e, 0xd9, 0x64, 0x47, 0x5d, 0x21, 0x84, 0x46,
0xd7, 0xe1, 0xf0, 0x95, 0x3a, 0x8f, 0xbd, 0x7a,
0x53, 0x71, 0x4c, 0x54, 0xc1, 0x3e, 0x27, 0xde,
0xeb, 0x04, 0x11, 0xb0, 0x33, 0x4d, 0x57, 0x0b,
0x6b, 0x7d, 0x6c, 0xd5, 0x87, 0x7e, 0xb4, 0xe2,
0x94, 0x9e, 0x9f, 0x74, 0xe8, 0xb7, 0xfa, 0x05,
0x9b, 0x8f, 0x81, 0x43, 0x35, 0x82, 0xb8, 0x5b,
0xa8, 0x5e, 0xfa, 0x7a, 0x80, 0x8d, 0xd2, 0x90,
0x58, 0x79, 0x89, 0x56, 0x90, 0x2b, 0xff, 0x92,
0x3c, 0x35, 0xbe, 0x99, 0x5f, 0xd2, 0x4b, 0x15,
0x58, 0x4b, 0xbf, 0x08, 0x9b, 0x9b, 0x97, 0x10,
0xa4, 0x55, 0xc7, 0xec, 0x29, 0xc5, 0x14, 0x3e,
0x8f, 0x56, 0xa3, 0x92, 0x9e, 0x33, 0xcc, 0x9e,
0x77, 0x2f, 0x33, 0xcb, 0xc4, 0xe9, 0x19, 0xf4,
0x32, 0x2b, 0xef, 0x6c, 0x1c, 0x92, 0x2c, 0x45,
0x88, 0x74, 0x5f, 0xcf, 0x56, 0xfd, 0x87, 0x5f,
0xb6, 0x9b, 0xa2, 0x51, 0xda, 0x9b, 0x83, 0x4f,
0xec, 0x14, 0xe8, 0xd2, 0x42, 0x03, 0xcb, 0xe8,
0xd0, 0xb7, 0xf8, 0x38, 0xde, 0x6f, 0xdf, 0x43,
0xfa, 0x41, 0xab, 0xec, 0x2e, 0x3c, 0x93, 0x39,
0x76, 0xd1, 0x6f, 0x5b, 0x6c, 0x6e, 0x8d, 0xeb,
0x45, 0x6b, 0xc5, 0x76, 0x00, 0x29, 0xca, 0x3b,
0xdb, 0x78, 0xc2, 0x32, 0x09, 0x39, 0x19, 0x50,
0xa2, 0x44, 0x92, 0x09, 0xdb, 0x8b, 0x9e, 0x16,
0x76, 0x7f, 0xf1, 0x78, 0x7b, 0xb2, 0x51, 0xbc,
0x28, 0xbd, 0xb0, 0x7f, 0x25, 0x63, 0x7d, 0x34,
0xfb, 0xf6, 0x36, 0x24, 0xc7, 0xf9, 0x41, 0xb6,
0x2a, 0x06, 0xfc, 0xf0, 0x83, 0xf2, 0x12, 0x3d,
0x60, 0x2e, 0x10, 0x70, 0x31, 0x6f, 0x37, 0x08,
0x3e, 0x91, 0x93, 0xb5, 0xda, 0xb8, 0x4c, 0x1b,
0xd8, 0xb8, 0x3b, 0xd5, 0x3e, 0xb6, 0xc0, 0xbb,
0x38, 0x0f, 0xd2, 0x68, 0x4f, 0x78, 0x56, 0xf6,
0xda, 0x65, 0xb4, 0x0b, 0xb4, 0xaf, 0xa8, 0x19,
0x2f, 0x70, 0x55, 0xe0, 0x47, 0x31, 0x9f, 0x37,
0x1a, 0x47, 0xb9, 0x0c, 0x97, 0x79, 0xfc, 0xa9,
0x76, 0xe6, 0xfa, 0x38, 0x67, 0x25, 0xd3, 0x89,
0x8d, 0xad, 0xc6, 0x11, 0x2d, 0x77, 0x0b, 0x35,
0xa2, 0xe2, 0xdf, 0xc8, 0x94, 0xd5, 0xdf, 0xd2,
0x69, 0x2a, 0x99, 0x93, 0xfa, 0x4a, 0x5f, 0xc7,
0x8a, 0x14, 0x5f, 0x2a, 0xf3, 0x02, 0xf0, 0x3e,
0x21, 0x8e, 0x2e, 0x4b, 0xc4, 0xd2, 0xc8, 0xa6,
0x41, 0x6e, 0x17, 0x36, 0xe9, 0xad, 0x73, 0x33,
0x6c, 0xea, 0xc2, 0x31, 0x8f, 0x30, 0x51, 0x5c,
0x1c, 0x20, 0xe6, 0x05, 0x1a, 0x17, 0x15, 0x5d,
0x3e, 0x8f, 0xd2, 0x7f, 0xa1, 0xc5, 0x47, 0xb3,
0xb2, 0x9c, 0xe8, 0xf0, 0x6d, 0xc1, 0xc3, 0xa2,
};
static const u8 nh_test_val16[NH_HASH_BYTES] = {
0x30, 0x77, 0x55, 0x7c, 0x45, 0xd8, 0xce, 0xf7,
0x2a, 0xb5, 0x14, 0x8c, 0x35, 0x7e, 0xaa, 0x00,
0x50, 0xbc, 0x50, 0x7c, 0xd3, 0x20, 0x7c, 0x9c,
0xb4, 0xf1, 0x91, 0x26, 0x81, 0x03, 0xa5, 0x68,
};
static const u8 nh_test_val96[NH_HASH_BYTES] = {
0xd2, 0x19, 0xca, 0xa5, 0x6c, 0x0c, 0xdf, 0x2f,
0x69, 0xfa, 0x75, 0xc1, 0x63, 0xdb, 0xfa, 0x4d,
0x45, 0x2b, 0xb8, 0xdb, 0xac, 0xee, 0x61, 0xc6,
0x7a, 0x83, 0xb6, 0x0f, 0x32, 0x82, 0xe4, 0xd0,
};
static const u8 nh_test_val256[NH_HASH_BYTES] = {
0x33, 0x8f, 0xb4, 0x96, 0xf1, 0xb6, 0xf1, 0xb5,
0x05, 0x19, 0xbb, 0x6b, 0xda, 0xd9, 0x95, 0x75,
0x96, 0x3f, 0x8b, 0x42, 0xb6, 0xcd, 0xb7, 0xb7,
0xe7, 0x97, 0xb5, 0xa9, 0x0b, 0xd7, 0xdd, 0x33,
};
static const u8 nh_test_val1024[NH_HASH_BYTES] = {
0x32, 0x3d, 0x51, 0xe1, 0x77, 0xb6, 0xac, 0x06,
0x84, 0x67, 0xb7, 0xf2, 0x24, 0xe7, 0xec, 0xfd,
0x96, 0x64, 0xff, 0x55, 0xc7, 0x1b, 0xf9, 0xdc,
0xa3, 0xc7, 0x32, 0x06, 0x79, 0xcf, 0xca, 0xb6,
};
+43
View File
@@ -0,0 +1,43 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright 2025 Google LLC
*/
#include <crypto/nh.h>
#include <kunit/test.h>
#include "nh-testvecs.h"
static void test_nh(struct kunit *test)
{
u32 *key = kunit_kmalloc(test, NH_KEY_BYTES, GFP_KERNEL);
__le64 hash[NH_NUM_PASSES];
KUNIT_ASSERT_NOT_NULL(test, key);
memcpy(key, nh_test_key, NH_KEY_BYTES);
le32_to_cpu_array(key, NH_KEY_WORDS);
nh(key, nh_test_msg, 16, hash);
KUNIT_ASSERT_MEMEQ(test, hash, nh_test_val16, NH_HASH_BYTES);
nh(key, nh_test_msg, 96, hash);
KUNIT_ASSERT_MEMEQ(test, hash, nh_test_val96, NH_HASH_BYTES);
nh(key, nh_test_msg, 256, hash);
KUNIT_ASSERT_MEMEQ(test, hash, nh_test_val256, NH_HASH_BYTES);
nh(key, nh_test_msg, 1024, hash);
KUNIT_ASSERT_MEMEQ(test, hash, nh_test_val1024, NH_HASH_BYTES);
}
static struct kunit_case nh_test_cases[] = {
KUNIT_CASE(test_nh),
{},
};
static struct kunit_suite nh_test_suite = {
.name = "nh",
.test_cases = nh_test_cases,
};
kunit_test_suite(nh_test_suite);
MODULE_DESCRIPTION("KUnit tests for NH");
MODULE_LICENSE("GPL");
@@ -0,0 +1,186 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */
/* This file was generated by: ./scripts/crypto/gen-hash-testvecs.py poly1305 */
static const struct {
size_t data_len;
u8 digest[POLY1305_DIGEST_SIZE];
} hash_testvecs[] = {
{
.data_len = 0,
.digest = {
0xe8, 0x2d, 0x67, 0x2c, 0x01, 0x48, 0xf9, 0xb7,
0x87, 0x85, 0x3f, 0xcf, 0x18, 0x66, 0x8c, 0xd3,
},
},
{
.data_len = 1,
.digest = {
0xb8, 0xad, 0xca, 0x6b, 0x32, 0xba, 0x34, 0x42,
0x54, 0x10, 0x28, 0xf5, 0x0f, 0x7e, 0x8e, 0xe3,
},
},
{
.data_len = 2,
.digest = {
0xb8, 0xf7, 0xf4, 0xc2, 0x85, 0x33, 0x80, 0x63,
0xd1, 0x45, 0xda, 0xf8, 0x7c, 0x79, 0x42, 0xd1,
},
},
{
.data_len = 3,
.digest = {
0xf3, 0x73, 0x7b, 0x60, 0x24, 0xcc, 0x5d, 0x3e,
0xd1, 0x95, 0x86, 0xce, 0x89, 0x0a, 0x33, 0xba,
},
},
{
.data_len = 16,
.digest = {
0x0a, 0x1a, 0x2d, 0x39, 0xea, 0x49, 0x8f, 0xb7,
0x90, 0xb6, 0x74, 0x3b, 0x41, 0x3b, 0x96, 0x11,
},
},
{
.data_len = 32,
.digest = {
0x99, 0x05, 0xe3, 0xa7, 0x9e, 0x2a, 0xd2, 0x42,
0xb9, 0x45, 0x0c, 0x08, 0xe7, 0x10, 0xe4, 0xe1,
},
},
{
.data_len = 48,
.digest = {
0xe1, 0xb2, 0x15, 0xee, 0xa2, 0xf3, 0x04, 0xac,
0xdd, 0x27, 0x57, 0x95, 0x2f, 0x45, 0xa8, 0xd3,
},
},
{
.data_len = 49,
.digest = {
0x1c, 0xf3, 0xab, 0x39, 0xc0, 0x69, 0x49, 0x69,
0x89, 0x6f, 0x1f, 0x03, 0x16, 0xe7, 0xc0, 0xf0,
},
},
{
.data_len = 63,
.digest = {
0x30, 0xb0, 0x32, 0x87, 0x51, 0x55, 0x9c, 0x39,
0x38, 0x42, 0x06, 0xe9, 0x2a, 0x3e, 0x2c, 0x92,
},
},
{
.data_len = 64,
.digest = {
0x2c, 0x04, 0x16, 0x36, 0x55, 0x25, 0x2d, 0xc6,
0x3d, 0x70, 0x5b, 0x88, 0x46, 0xb6, 0x71, 0x77,
},
},
{
.data_len = 65,
.digest = {
0x03, 0x87, 0xdd, 0xbe, 0xe8, 0x30, 0xf2, 0x15,
0x40, 0x44, 0x29, 0x7b, 0xb1, 0xe9, 0x9d, 0xe7,
},
},
{
.data_len = 127,
.digest = {
0x29, 0x83, 0x4f, 0xcb, 0x5a, 0x93, 0x25, 0xad,
0x05, 0xa4, 0xb3, 0x24, 0x77, 0x62, 0x2d, 0x3d,
},
},
{
.data_len = 128,
.digest = {
0x20, 0x0e, 0x2c, 0x05, 0xe2, 0x0b, 0x85, 0xa0,
0x24, 0x73, 0x7f, 0x65, 0x70, 0x6c, 0x3e, 0xb0,
},
},
{
.data_len = 129,
.digest = {
0xef, 0x2f, 0x98, 0x42, 0xc2, 0x90, 0x55, 0xea,
0xba, 0x28, 0x76, 0xfd, 0x9e, 0x3e, 0x4d, 0x53,
},
},
{
.data_len = 256,
.digest = {
0x9e, 0x75, 0x4b, 0xc7, 0x69, 0x68, 0x51, 0x90,
0xdc, 0x29, 0xc8, 0xfa, 0x86, 0xf1, 0xc9, 0xb3,
},
},
{
.data_len = 511,
.digest = {
0x9d, 0x13, 0xf5, 0x54, 0xe6, 0xe3, 0x45, 0x38,
0x8b, 0x6d, 0x5c, 0xc4, 0x50, 0xeb, 0x90, 0xcb,
},
},
{
.data_len = 513,
.digest = {
0xaa, 0xb2, 0x3e, 0x3c, 0x2a, 0xfc, 0x62, 0x0e,
0xd4, 0xe6, 0xe5, 0x5c, 0x6b, 0x9f, 0x3d, 0xc7,
},
},
{
.data_len = 1000,
.digest = {
0xd6, 0x8c, 0xea, 0x8a, 0x0f, 0x68, 0xa9, 0xa8,
0x67, 0x86, 0xf9, 0xc1, 0x4c, 0x26, 0x10, 0x6d,
},
},
{
.data_len = 3333,
.digest = {
0xdc, 0xc1, 0x54, 0xe7, 0x38, 0xc6, 0xdb, 0x24,
0xa7, 0x0b, 0xff, 0xd3, 0x1b, 0x93, 0x01, 0xa6,
},
},
{
.data_len = 4096,
.digest = {
0x8f, 0x88, 0x3e, 0x9c, 0x7b, 0x2e, 0x82, 0x5a,
0x1d, 0x31, 0x82, 0xcc, 0x69, 0xb4, 0x16, 0x26,
},
},
{
.data_len = 4128,
.digest = {
0x23, 0x45, 0x94, 0xa8, 0x11, 0x54, 0x9d, 0xf2,
0xa1, 0x9a, 0xca, 0xf9, 0x3e, 0x65, 0x52, 0xfd,
},
},
{
.data_len = 4160,
.digest = {
0x7b, 0xfc, 0xa9, 0x1e, 0x03, 0xad, 0xef, 0x03,
0xe2, 0x20, 0x92, 0xc7, 0x54, 0x83, 0xfa, 0x37,
},
},
{
.data_len = 4224,
.digest = {
0x46, 0xab, 0x8c, 0x75, 0xb3, 0x10, 0xa6, 0x3f,
0x74, 0x55, 0x42, 0x6d, 0x69, 0x35, 0xd2, 0xf5,
},
},
{
.data_len = 16384,
.digest = {
0xd0, 0xfe, 0x26, 0xc2, 0xca, 0x94, 0x2d, 0x52,
0x2d, 0xe1, 0x11, 0xdd, 0x42, 0x28, 0x83, 0xa6,
},
},
};
static const u8 hash_testvec_consolidated[POLY1305_DIGEST_SIZE] = {
0x9d, 0x07, 0x5d, 0xc9, 0x6c, 0xeb, 0x62, 0x5d,
0x02, 0x5f, 0xe1, 0xe3, 0xd1, 0x71, 0x69, 0x34,
};
static const u8 poly1305_allones_macofmacs[POLY1305_DIGEST_SIZE] = {
0x0c, 0x26, 0x6b, 0x45, 0x87, 0x06, 0xcf, 0xc4,
0x3f, 0x70, 0x7d, 0xb3, 0x50, 0xdd, 0x81, 0x25,
};
@@ -0,0 +1,165 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright 2025 Google LLC
*/
#include <crypto/poly1305.h>
#include "poly1305-testvecs.h"
/*
* A fixed key used when presenting Poly1305 as an unkeyed hash function in
* order to reuse hash-test-template.h. At the beginning of the test suite,
* this is initialized to bytes generated from a fixed seed.
*/
static u8 test_key[POLY1305_KEY_SIZE];
/* This probably should be in the actual API, but just define it here for now */
static void poly1305(const u8 key[POLY1305_KEY_SIZE], const u8 *data,
size_t len, u8 out[POLY1305_DIGEST_SIZE])
{
struct poly1305_desc_ctx ctx;
poly1305_init(&ctx, key);
poly1305_update(&ctx, data, len);
poly1305_final(&ctx, out);
}
static void poly1305_init_withtestkey(struct poly1305_desc_ctx *ctx)
{
poly1305_init(ctx, test_key);
}
static void poly1305_withtestkey(const u8 *data, size_t len,
u8 out[POLY1305_DIGEST_SIZE])
{
poly1305(test_key, data, len, out);
}
/* Generate the HASH_KUNIT_CASES using hash-test-template.h. */
#define HASH poly1305_withtestkey
#define HASH_CTX poly1305_desc_ctx
#define HASH_SIZE POLY1305_DIGEST_SIZE
#define HASH_INIT poly1305_init_withtestkey
#define HASH_UPDATE poly1305_update
#define HASH_FINAL poly1305_final
#include "hash-test-template.h"
static int poly1305_suite_init(struct kunit_suite *suite)
{
rand_bytes_seeded_from_len(test_key, POLY1305_KEY_SIZE);
return hash_suite_init(suite);
}
static void poly1305_suite_exit(struct kunit_suite *suite)
{
hash_suite_exit(suite);
}
/*
* Poly1305 test case which uses a key and message consisting only of one bits:
*
* - Using an all-one-bits r_key tests the key clamping.
* - Using an all-one-bits s_key tests carries in implementations of the
* addition mod 2**128 during finalization.
* - Using all-one-bits message, and to a lesser extent r_key, tends to maximize
* any intermediate accumulator values. This increases the chance of
* detecting bugs that occur only in rare cases where the accumulator values
* get very large, for example the bug fixed by commit 678cce4019d746da
* ("crypto: x86/poly1305 - fix overflow during partial reduction").
*
* Accumulator overflow bugs may be specific to particular update lengths (in
* blocks) and/or particular values of the previous acculumator. Note that the
* accumulator starts at 0 which gives the lowest chance of an overflow. Thus,
* a single all-one-bits test vector may be insufficient.
*
* Considering that, do the following test: continuously update a single
* Poly1305 context with all-one-bits data of varying lengths (0, 16, 32, ...,
* 4096 bytes). After each update, generate the MAC from the current context,
* and feed that MAC into a separate Poly1305 context. Repeat that entire
* sequence of updates 32 times without re-initializing either context,
* resulting in a total of 8224 MAC computations from a long-running, cumulative
* context. Finally, generate and verify the MAC of all the MACs.
*/
static void test_poly1305_allones_keys_and_message(struct kunit *test)
{
struct poly1305_desc_ctx mac_ctx, macofmacs_ctx;
u8 mac[POLY1305_DIGEST_SIZE];
static_assert(TEST_BUF_LEN >= 4096);
memset(test_buf, 0xff, 4096);
poly1305_init(&mac_ctx, test_buf);
poly1305_init(&macofmacs_ctx, test_buf);
for (int i = 0; i < 32; i++) {
for (size_t len = 0; len <= 4096; len += 16) {
struct poly1305_desc_ctx tmp_ctx;
poly1305_update(&mac_ctx, test_buf, len);
tmp_ctx = mac_ctx;
poly1305_final(&tmp_ctx, mac);
poly1305_update(&macofmacs_ctx, mac,
POLY1305_DIGEST_SIZE);
}
}
poly1305_final(&macofmacs_ctx, mac);
KUNIT_ASSERT_MEMEQ(test, mac, poly1305_allones_macofmacs,
POLY1305_DIGEST_SIZE);
}
/*
* Poly1305 test case which uses r_key=1, s_key=0, and a 48-byte message
* consisting of three blocks with integer values [2**128 - i, 0, 0]. In this
* case, the result of the polynomial evaluation is 2**130 - i. For small
* values of i, this is very close to the modulus 2**130 - 5, which helps catch
* edge case bugs in the modular reduction logic.
*/
static void test_poly1305_reduction_edge_cases(struct kunit *test)
{
static const u8 key[POLY1305_KEY_SIZE] = { 1 }; /* r_key=1, s_key=0 */
u8 data[3 * POLY1305_BLOCK_SIZE] = {};
u8 expected_mac[POLY1305_DIGEST_SIZE];
u8 actual_mac[POLY1305_DIGEST_SIZE];
for (int i = 1; i <= 10; i++) {
/* Set the first data block to 2**128 - i. */
data[0] = -i;
memset(&data[1], 0xff, POLY1305_BLOCK_SIZE - 1);
/*
* Assuming s_key=0, the expected MAC as an integer is
* (2**130 - i mod 2**130 - 5) + 0 mod 2**128. If 1 <= i <= 5,
* that's 5 - i. If 6 <= i <= 10, that's 2**128 - i.
*/
if (i <= 5) {
expected_mac[0] = 5 - i;
memset(&expected_mac[1], 0, POLY1305_DIGEST_SIZE - 1);
} else {
expected_mac[0] = -i;
memset(&expected_mac[1], 0xff,
POLY1305_DIGEST_SIZE - 1);
}
/* Compute and verify the MAC. */
poly1305(key, data, sizeof(data), actual_mac);
KUNIT_ASSERT_MEMEQ(test, actual_mac, expected_mac,
POLY1305_DIGEST_SIZE);
}
}
static struct kunit_case poly1305_test_cases[] = {
HASH_KUNIT_CASES,
KUNIT_CASE(test_poly1305_allones_keys_and_message),
KUNIT_CASE(test_poly1305_reduction_edge_cases),
KUNIT_CASE(benchmark_hash),
{},
};
static struct kunit_suite poly1305_test_suite = {
.name = "poly1305",
.test_cases = poly1305_test_cases,
.suite_init = poly1305_suite_init,
.suite_exit = poly1305_suite_exit,
};
kunit_test_suite(poly1305_test_suite);
MODULE_DESCRIPTION("KUnit tests and benchmark for Poly1305");
MODULE_LICENSE("GPL");
@@ -0,0 +1,186 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */
/* This file was generated by: ./scripts/crypto/gen-hash-testvecs.py polyval */
static const struct {
size_t data_len;
u8 digest[POLYVAL_DIGEST_SIZE];
} hash_testvecs[] = {
{
.data_len = 0,
.digest = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
},
},
{
.data_len = 1,
.digest = {
0xb5, 0x51, 0x69, 0x89, 0xd4, 0x3c, 0x59, 0xca,
0x6a, 0x1c, 0x2a, 0xe9, 0xa1, 0x9c, 0x6c, 0x83,
},
},
{
.data_len = 2,
.digest = {
0xf4, 0x50, 0xaf, 0x07, 0xda, 0x42, 0xa7, 0x41,
0x4d, 0x24, 0x88, 0x87, 0xe3, 0x40, 0x73, 0x7c,
},
},
{
.data_len = 3,
.digest = {
0x9e, 0x88, 0x78, 0x71, 0x4c, 0x55, 0x87, 0xe8,
0xb4, 0x96, 0x3d, 0x56, 0xc8, 0xb2, 0xe1, 0x68,
},
},
{
.data_len = 16,
.digest = {
0x9e, 0x81, 0x37, 0x8f, 0x49, 0xf7, 0xa2, 0xe4,
0x04, 0x45, 0x12, 0x78, 0x45, 0x42, 0x27, 0xad,
},
},
{
.data_len = 32,
.digest = {
0x60, 0x19, 0xd0, 0xa4, 0xf0, 0xde, 0x9e, 0xe7,
0x6a, 0x89, 0x1a, 0xea, 0x80, 0x14, 0xa9, 0xa3,
},
},
{
.data_len = 48,
.digest = {
0x0c, 0xa2, 0x70, 0x4d, 0x7c, 0x89, 0xac, 0x41,
0xc2, 0x9e, 0x0d, 0x07, 0x07, 0x6a, 0x7f, 0xd5,
},
},
{
.data_len = 49,
.digest = {
0x91, 0xd3, 0xa9, 0x5c, 0x79, 0x3d, 0x6b, 0x84,
0x99, 0x54, 0xa7, 0xb4, 0x06, 0x66, 0xfd, 0x1c,
},
},
{
.data_len = 63,
.digest = {
0x29, 0x37, 0xb8, 0xe5, 0xd8, 0x27, 0x4d, 0xfb,
0x83, 0x4f, 0x67, 0xf7, 0xf9, 0xc1, 0x0a, 0x9d,
},
},
{
.data_len = 64,
.digest = {
0x17, 0xa9, 0x06, 0x2c, 0xf3, 0xe8, 0x2e, 0xa6,
0x6b, 0xb2, 0x1f, 0x5d, 0x94, 0x3c, 0x02, 0xa2,
},
},
{
.data_len = 65,
.digest = {
0x7c, 0x80, 0x74, 0xd7, 0xa1, 0x37, 0x30, 0x64,
0x3b, 0xa4, 0xa3, 0x98, 0xde, 0x47, 0x10, 0x23,
},
},
{
.data_len = 127,
.digest = {
0x27, 0x3a, 0xcf, 0xf5, 0xaf, 0x9f, 0xd8, 0xd8,
0x2d, 0x6a, 0x91, 0xfb, 0xb8, 0xfa, 0xbe, 0x0c,
},
},
{
.data_len = 128,
.digest = {
0x97, 0x6e, 0xc4, 0xbe, 0x6b, 0x15, 0xa6, 0x7c,
0xc4, 0xa2, 0xb8, 0x0a, 0x0e, 0x9c, 0xc7, 0x3a,
},
},
{
.data_len = 129,
.digest = {
0x2b, 0xc3, 0x98, 0xba, 0x6e, 0x42, 0xf8, 0x18,
0x85, 0x69, 0x15, 0x37, 0x10, 0x60, 0xe6, 0xac,
},
},
{
.data_len = 256,
.digest = {
0x88, 0x21, 0x77, 0x89, 0xd7, 0x93, 0x90, 0xfc,
0xf3, 0xb0, 0xe3, 0xfb, 0x14, 0xe2, 0xcf, 0x74,
},
},
{
.data_len = 511,
.digest = {
0x66, 0x3d, 0x3e, 0x08, 0xa0, 0x49, 0x81, 0x68,
0x3e, 0x3b, 0xc8, 0x80, 0x55, 0xd4, 0x15, 0xe9,
},
},
{
.data_len = 513,
.digest = {
0x05, 0xf5, 0x06, 0x66, 0xe7, 0x11, 0x08, 0x84,
0xff, 0x94, 0x50, 0x85, 0x65, 0x95, 0x2a, 0x20,
},
},
{
.data_len = 1000,
.digest = {
0xd3, 0xa0, 0x51, 0x69, 0xb5, 0x38, 0xae, 0x1b,
0xe1, 0xa2, 0x89, 0xc6, 0x8d, 0x2b, 0x62, 0x37,
},
},
{
.data_len = 3333,
.digest = {
0x37, 0x6d, 0x6a, 0x14, 0xdc, 0xa5, 0x37, 0xfc,
0xfe, 0x67, 0x76, 0xb2, 0x64, 0x68, 0x64, 0x05,
},
},
{
.data_len = 4096,
.digest = {
0xe3, 0x12, 0x0c, 0x58, 0x46, 0x45, 0x27, 0x7a,
0x0e, 0xa2, 0xfa, 0x2c, 0x35, 0x73, 0x6c, 0x94,
},
},
{
.data_len = 4128,
.digest = {
0x63, 0x0d, 0xa1, 0xbc, 0x6e, 0x3e, 0xd3, 0x1d,
0x28, 0x52, 0xd2, 0xf4, 0x30, 0x2d, 0xff, 0xc4,
},
},
{
.data_len = 4160,
.digest = {
0xb2, 0x91, 0x49, 0xe2, 0x02, 0x98, 0x00, 0x79,
0x71, 0xb9, 0xd7, 0xd4, 0xb5, 0x94, 0x6d, 0x7d,
},
},
{
.data_len = 4224,
.digest = {
0x58, 0x96, 0x48, 0x69, 0x05, 0x17, 0xe1, 0x6d,
0xbc, 0xf2, 0x3d, 0x10, 0x96, 0x00, 0x74, 0x58,
},
},
{
.data_len = 16384,
.digest = {
0x99, 0x3c, 0xcb, 0x4d, 0x64, 0xc9, 0xa9, 0x41,
0x52, 0x93, 0xfd, 0x65, 0xc4, 0xcc, 0xa5, 0xe5,
},
},
};
static const u8 hash_testvec_consolidated[POLYVAL_DIGEST_SIZE] = {
0xdf, 0x68, 0x52, 0x99, 0x92, 0xc3, 0xe8, 0x88,
0x29, 0x13, 0xc8, 0x35, 0x67, 0xa3, 0xd3, 0xad,
};
static const u8 polyval_allones_hashofhashes[POLYVAL_DIGEST_SIZE] = {
0xd5, 0xf7, 0xfd, 0xb2, 0xa6, 0xef, 0x0b, 0x85,
0x0d, 0x0a, 0x06, 0x10, 0xbc, 0x64, 0x94, 0x73,
};
@@ -0,0 +1,223 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright 2025 Google LLC
*/
#include <crypto/gf128hash.h>
#include "polyval-testvecs.h"
/*
* A fixed key used when presenting POLYVAL as an unkeyed hash function in order
* to reuse hash-test-template.h. At the beginning of the test suite, this is
* initialized to a key prepared from bytes generated from a fixed seed.
*/
static struct polyval_key test_key;
static void polyval_init_withtestkey(struct polyval_ctx *ctx)
{
polyval_init(ctx, &test_key);
}
static void polyval_withtestkey(const u8 *data, size_t len,
u8 out[POLYVAL_BLOCK_SIZE])
{
polyval(&test_key, data, len, out);
}
/* Generate the HASH_KUNIT_CASES using hash-test-template.h. */
#define HASH polyval_withtestkey
#define HASH_CTX polyval_ctx
#define HASH_SIZE POLYVAL_BLOCK_SIZE
#define HASH_INIT polyval_init_withtestkey
#define HASH_UPDATE polyval_update
#define HASH_FINAL polyval_final
#include "hash-test-template.h"
/*
* Test an example from RFC8452 ("AES-GCM-SIV: Nonce Misuse-Resistant
* Authenticated Encryption") to ensure compatibility with that.
*/
static void test_polyval_rfc8452_testvec(struct kunit *test)
{
static const u8 raw_key[POLYVAL_BLOCK_SIZE] =
"\x31\x07\x28\xd9\x91\x1f\x1f\x38"
"\x37\xb2\x43\x16\xc3\xfa\xb9\xa0";
static const u8 data[48] =
"\x65\x78\x61\x6d\x70\x6c\x65\x00"
"\x00\x00\x00\x00\x00\x00\x00\x00"
"\x48\x65\x6c\x6c\x6f\x20\x77\x6f"
"\x72\x6c\x64\x00\x00\x00\x00\x00"
"\x38\x00\x00\x00\x00\x00\x00\x00"
"\x58\x00\x00\x00\x00\x00\x00\x00";
static const u8 expected_hash[POLYVAL_BLOCK_SIZE] =
"\xad\x7f\xcf\x0b\x51\x69\x85\x16"
"\x62\x67\x2f\x3c\x5f\x95\x13\x8f";
u8 hash[POLYVAL_BLOCK_SIZE];
struct polyval_key key;
polyval_preparekey(&key, raw_key);
polyval(&key, data, sizeof(data), hash);
KUNIT_ASSERT_MEMEQ(test, hash, expected_hash, sizeof(hash));
}
/*
* Test a key and messages containing all one bits. This is useful to detect
* overflow bugs in implementations that emulate carryless multiplication using
* a series of standard multiplications with the bits spread out.
*/
static void test_polyval_allones_key_and_message(struct kunit *test)
{
struct polyval_key key;
struct polyval_ctx hashofhashes_ctx;
u8 hash[POLYVAL_BLOCK_SIZE];
static_assert(TEST_BUF_LEN >= 4096);
memset(test_buf, 0xff, 4096);
polyval_preparekey(&key, test_buf);
polyval_init(&hashofhashes_ctx, &key);
for (size_t len = 0; len <= 4096; len += 16) {
polyval(&key, test_buf, len, hash);
polyval_update(&hashofhashes_ctx, hash, sizeof(hash));
}
polyval_final(&hashofhashes_ctx, hash);
KUNIT_ASSERT_MEMEQ(test, hash, polyval_allones_hashofhashes,
sizeof(hash));
}
#define MAX_LEN_FOR_KEY_CHECK 1024
/*
* Given two prepared keys which should be identical (but may differ in
* alignment and/or whether they are followed by a guard page or not), verify
* that they produce consistent results on various data lengths.
*/
static void check_key_consistency(struct kunit *test,
const struct polyval_key *key1,
const struct polyval_key *key2)
{
u8 *data = test_buf;
u8 hash1[POLYVAL_BLOCK_SIZE];
u8 hash2[POLYVAL_BLOCK_SIZE];
rand_bytes(data, MAX_LEN_FOR_KEY_CHECK);
KUNIT_ASSERT_MEMEQ(test, key1, key2, sizeof(*key1));
for (int i = 0; i < 100; i++) {
size_t len = rand_length(MAX_LEN_FOR_KEY_CHECK);
polyval(key1, data, len, hash1);
polyval(key2, data, len, hash2);
KUNIT_ASSERT_MEMEQ(test, hash1, hash2, sizeof(hash1));
}
}
/* Test that no buffer overreads occur on either raw_key or polyval_key. */
static void test_polyval_with_guarded_key(struct kunit *test)
{
u8 raw_key[POLYVAL_BLOCK_SIZE];
u8 *guarded_raw_key = &test_buf[TEST_BUF_LEN - sizeof(raw_key)];
struct polyval_key key1, key2;
struct polyval_key *guarded_key =
(struct polyval_key *)&test_buf[TEST_BUF_LEN - sizeof(key1)];
/* Prepare with regular buffers. */
rand_bytes(raw_key, sizeof(raw_key));
polyval_preparekey(&key1, raw_key);
/* Prepare with guarded raw_key, then check that it works. */
memcpy(guarded_raw_key, raw_key, sizeof(raw_key));
polyval_preparekey(&key2, guarded_raw_key);
check_key_consistency(test, &key1, &key2);
/* Prepare guarded polyval_key, then check that it works. */
polyval_preparekey(guarded_key, raw_key);
check_key_consistency(test, &key1, guarded_key);
}
/*
* Test that polyval_key only needs to be aligned to
* __alignof__(struct polyval_key), i.e. 8 bytes. The assembly code may prefer
* 16-byte or higher alignment, but it musn't require it.
*/
static void test_polyval_with_minimally_aligned_key(struct kunit *test)
{
u8 raw_key[POLYVAL_BLOCK_SIZE];
struct polyval_key key;
struct polyval_key *minaligned_key =
(struct polyval_key *)&test_buf[MAX_LEN_FOR_KEY_CHECK +
__alignof__(struct polyval_key)];
KUNIT_ASSERT_TRUE(test, IS_ALIGNED((uintptr_t)minaligned_key,
__alignof__(struct polyval_key)));
KUNIT_ASSERT_TRUE(test,
!IS_ALIGNED((uintptr_t)minaligned_key,
2 * __alignof__(struct polyval_key)));
rand_bytes(raw_key, sizeof(raw_key));
polyval_preparekey(&key, raw_key);
polyval_preparekey(minaligned_key, raw_key);
check_key_consistency(test, &key, minaligned_key);
}
struct polyval_irq_test_state {
struct polyval_key expected_key;
u8 raw_key[POLYVAL_BLOCK_SIZE];
};
static bool polyval_irq_test_func(void *state_)
{
struct polyval_irq_test_state *state = state_;
struct polyval_key key;
polyval_preparekey(&key, state->raw_key);
return memcmp(&key, &state->expected_key, sizeof(key)) == 0;
}
/*
* Test that polyval_preparekey() produces the same output regardless of whether
* FPU or vector registers are usable when it is called.
*/
static void test_polyval_preparekey_in_irqs(struct kunit *test)
{
struct polyval_irq_test_state state;
rand_bytes(state.raw_key, sizeof(state.raw_key));
polyval_preparekey(&state.expected_key, state.raw_key);
kunit_run_irq_test(test, polyval_irq_test_func, 200000, &state);
}
static int polyval_suite_init(struct kunit_suite *suite)
{
u8 raw_key[POLYVAL_BLOCK_SIZE];
rand_bytes_seeded_from_len(raw_key, sizeof(raw_key));
polyval_preparekey(&test_key, raw_key);
return hash_suite_init(suite);
}
static void polyval_suite_exit(struct kunit_suite *suite)
{
hash_suite_exit(suite);
}
static struct kunit_case polyval_test_cases[] = {
HASH_KUNIT_CASES,
KUNIT_CASE(test_polyval_rfc8452_testvec),
KUNIT_CASE(test_polyval_allones_key_and_message),
KUNIT_CASE(test_polyval_with_guarded_key),
KUNIT_CASE(test_polyval_with_minimally_aligned_key),
KUNIT_CASE(test_polyval_preparekey_in_irqs),
KUNIT_CASE(benchmark_hash),
{},
};
static struct kunit_suite polyval_test_suite = {
.name = "polyval",
.test_cases = polyval_test_cases,
.suite_init = polyval_suite_init,
.suite_exit = polyval_suite_exit,
};
kunit_test_suite(polyval_test_suite);
MODULE_DESCRIPTION("KUnit tests and benchmark for POLYVAL");
MODULE_LICENSE("GPL");
@@ -0,0 +1,212 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */
/* This file was generated by: ./scripts/crypto/gen-hash-testvecs.py sha1 */
static const struct {
size_t data_len;
u8 digest[SHA1_DIGEST_SIZE];
} hash_testvecs[] = {
{
.data_len = 0,
.digest = {
0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d,
0x32, 0x55, 0xbf, 0xef, 0x95, 0x60, 0x18, 0x90,
0xaf, 0xd8, 0x07, 0x09,
},
},
{
.data_len = 1,
.digest = {
0x0a, 0xd0, 0x52, 0xdd, 0x9f, 0x32, 0x40, 0x55,
0x21, 0xe4, 0x3c, 0x6e, 0xbd, 0xc5, 0x2f, 0x5a,
0x02, 0x54, 0x93, 0xb2,
},
},
{
.data_len = 2,
.digest = {
0x13, 0x83, 0x82, 0x03, 0x23, 0xff, 0x46, 0xd6,
0x12, 0x7f, 0xad, 0x05, 0x2b, 0xc3, 0x4a, 0x42,
0x49, 0x6a, 0xf8, 0x84,
},
},
{
.data_len = 3,
.digest = {
0xe4, 0xdf, 0x7b, 0xdc, 0xe8, 0x6e, 0x81, 0x97,
0x1e, 0x0f, 0xe8, 0x8b, 0x76, 0xa8, 0x59, 0x04,
0xae, 0x92, 0x1a, 0x7c,
},
},
{
.data_len = 16,
.digest = {
0x8c, 0x1c, 0x30, 0xd8, 0xbc, 0xc4, 0xc3, 0xf5,
0xf8, 0x83, 0x0d, 0x1e, 0x04, 0x5d, 0x29, 0xb5,
0x68, 0x89, 0xc1, 0xe9,
},
},
{
.data_len = 32,
.digest = {
0x6c, 0x1d, 0x72, 0x31, 0xa5, 0x03, 0x4f, 0xdc,
0xff, 0x2d, 0x06, 0x3e, 0x24, 0x26, 0x34, 0x8d,
0x60, 0xa4, 0x67, 0x16,
},
},
{
.data_len = 48,
.digest = {
0x37, 0x53, 0x33, 0xfa, 0xd0, 0x21, 0xad, 0xe7,
0xa5, 0x43, 0xf1, 0x94, 0x64, 0x11, 0x47, 0x9c,
0x72, 0xb5, 0x78, 0xb4,
},
},
{
.data_len = 49,
.digest = {
0x51, 0x5c, 0xd8, 0x5a, 0xa9, 0xde, 0x7b, 0x2a,
0xa2, 0xff, 0x70, 0x09, 0x56, 0x88, 0x40, 0x2b,
0x50, 0x93, 0x82, 0x47,
},
},
{
.data_len = 63,
.digest = {
0xbc, 0x9c, 0xab, 0x93, 0x06, 0xd5, 0xdb, 0xac,
0x2c, 0x33, 0x15, 0x83, 0x56, 0xf6, 0x91, 0x20,
0x09, 0xc7, 0xb2, 0x6b,
},
},
{
.data_len = 64,
.digest = {
0x26, 0x90, 0x3b, 0x47, 0xe1, 0x92, 0x42, 0xd0,
0x85, 0x63, 0x2e, 0x6b, 0x68, 0xa4, 0xc4, 0x4c,
0xe6, 0xf4, 0xb0, 0x52,
},
},
{
.data_len = 65,
.digest = {
0x55, 0x6f, 0x87, 0xdc, 0x34, 0x3d, 0xe2, 0x4f,
0xc3, 0x81, 0xa4, 0x82, 0x79, 0x84, 0x64, 0x01,
0x55, 0xa0, 0x1e, 0x36,
},
},
{
.data_len = 127,
.digest = {
0xb7, 0xd5, 0x5f, 0xa4, 0xef, 0xbf, 0x4f, 0x96,
0x01, 0xc1, 0x06, 0xe3, 0x75, 0xa8, 0x90, 0x92,
0x4c, 0x5f, 0xf1, 0x21,
},
},
{
.data_len = 128,
.digest = {
0x70, 0x0c, 0xea, 0xa4, 0x93, 0xd0, 0x56, 0xf0,
0x6f, 0xbb, 0x53, 0x42, 0x5b, 0xe3, 0xf2, 0xb0,
0x30, 0x66, 0x8e, 0x75,
},
},
{
.data_len = 129,
.digest = {
0x15, 0x01, 0xbc, 0xb0, 0xee, 0xd8, 0xeb, 0xa8,
0x7d, 0xd9, 0x4d, 0x50, 0x2e, 0x41, 0x30, 0xba,
0x41, 0xaa, 0x7b, 0x02,
},
},
{
.data_len = 256,
.digest = {
0x98, 0x05, 0x52, 0xf5, 0x0f, 0xf0, 0xd3, 0x97,
0x15, 0x8c, 0xa3, 0x9a, 0x2b, 0x4d, 0x67, 0x57,
0x29, 0xa0, 0xac, 0x61,
},
},
{
.data_len = 511,
.digest = {
0x1f, 0x47, 0xf0, 0xcc, 0xd7, 0xda, 0xa5, 0x3b,
0x39, 0xb4, 0x5b, 0xa8, 0x33, 0xd4, 0xca, 0x2f,
0xdd, 0xf2, 0x39, 0x89,
},
},
{
.data_len = 513,
.digest = {
0xb9, 0x75, 0xe6, 0x57, 0x42, 0x7f, 0x8b, 0x0a,
0xcc, 0x53, 0x10, 0x69, 0x45, 0xac, 0xfd, 0x11,
0xf7, 0x1f, 0x4e, 0x6f,
},
},
{
.data_len = 1000,
.digest = {
0x63, 0x66, 0xcb, 0x44, 0xc1, 0x2c, 0xa2, 0x06,
0x5d, 0xb9, 0x8e, 0x31, 0xcb, 0x4f, 0x4e, 0x49,
0xe0, 0xfb, 0x3c, 0x4e,
},
},
{
.data_len = 3333,
.digest = {
0x35, 0xbc, 0x74, 0xfb, 0x31, 0x9c, 0xd4, 0xdd,
0xe8, 0x87, 0xa7, 0x56, 0x3b, 0x08, 0xe5, 0x49,
0xe1, 0xe9, 0xc9, 0xa8,
},
},
{
.data_len = 4096,
.digest = {
0x43, 0x00, 0xea, 0xcd, 0x4e, 0x7c, 0xe9, 0xe4,
0x32, 0xce, 0x25, 0xa8, 0xcd, 0x20, 0xa8, 0xaa,
0x7b, 0x63, 0x2c, 0x3c,
},
},
{
.data_len = 4128,
.digest = {
0xd0, 0x67, 0x26, 0x0e, 0x22, 0x72, 0xaa, 0x63,
0xfc, 0x34, 0x55, 0x07, 0xab, 0xc8, 0x64, 0xb6,
0xc4, 0xea, 0xd5, 0x7c,
},
},
{
.data_len = 4160,
.digest = {
0x6b, 0xc9, 0x5e, 0xb9, 0x41, 0x19, 0x50, 0x35,
0xf1, 0x39, 0xfe, 0xd9, 0x72, 0x6d, 0xd0, 0x55,
0xb8, 0x1f, 0x1a, 0x95,
},
},
{
.data_len = 4224,
.digest = {
0x70, 0x5d, 0x10, 0x2e, 0x4e, 0x44, 0xc9, 0x80,
0x8f, 0xba, 0x13, 0xbc, 0xd0, 0x77, 0x78, 0xc7,
0x84, 0xe3, 0x24, 0x43,
},
},
{
.data_len = 16384,
.digest = {
0xa8, 0x82, 0xca, 0x08, 0xb4, 0x84, 0x09, 0x13,
0xc0, 0x9c, 0x26, 0x18, 0xcf, 0x0f, 0xf3, 0x08,
0xff, 0xa1, 0xe4, 0x5d,
},
},
};
static const u8 hash_testvec_consolidated[SHA1_DIGEST_SIZE] = {
0xe1, 0x72, 0xa5, 0x3c, 0xda, 0xf2, 0xe5, 0x56,
0xb8, 0xb5, 0x35, 0x6e, 0xce, 0xc8, 0x37, 0x57,
0x31, 0xb4, 0x05, 0xdd,
};
static const u8 hmac_testvec_consolidated[SHA1_DIGEST_SIZE] = {
0x9d, 0xe5, 0xb1, 0x43, 0x97, 0x95, 0x16, 0x52,
0xa0, 0x7a, 0xc0, 0xe2, 0xc1, 0x60, 0x64, 0x7c,
0x24, 0xf9, 0x34, 0xd7,
};
+39
View File
@@ -0,0 +1,39 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright 2025 Google LLC
*/
#include <crypto/sha1.h>
#include "sha1-testvecs.h"
#define HASH sha1
#define HASH_CTX sha1_ctx
#define HASH_SIZE SHA1_DIGEST_SIZE
#define HASH_INIT sha1_init
#define HASH_UPDATE sha1_update
#define HASH_FINAL sha1_final
#define HMAC_KEY hmac_sha1_key
#define HMAC_CTX hmac_sha1_ctx
#define HMAC_PREPAREKEY hmac_sha1_preparekey
#define HMAC_INIT hmac_sha1_init
#define HMAC_UPDATE hmac_sha1_update
#define HMAC_FINAL hmac_sha1_final
#define HMAC hmac_sha1
#define HMAC_USINGRAWKEY hmac_sha1_usingrawkey
#include "hash-test-template.h"
static struct kunit_case hash_test_cases[] = {
HASH_KUNIT_CASES,
KUNIT_CASE(benchmark_hash),
{},
};
static struct kunit_suite hash_test_suite = {
.name = "sha1",
.test_cases = hash_test_cases,
.suite_init = hash_suite_init,
.suite_exit = hash_suite_exit,
};
kunit_test_suite(hash_test_suite);
MODULE_DESCRIPTION("KUnit tests and benchmark for SHA-1 and HMAC-SHA1");
MODULE_LICENSE("GPL");
@@ -0,0 +1,238 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */
/* This file was generated by: ./scripts/crypto/gen-hash-testvecs.py sha224 */
static const struct {
size_t data_len;
u8 digest[SHA224_DIGEST_SIZE];
} hash_testvecs[] = {
{
.data_len = 0,
.digest = {
0xd1, 0x4a, 0x02, 0x8c, 0x2a, 0x3a, 0x2b, 0xc9,
0x47, 0x61, 0x02, 0xbb, 0x28, 0x82, 0x34, 0xc4,
0x15, 0xa2, 0xb0, 0x1f, 0x82, 0x8e, 0xa6, 0x2a,
0xc5, 0xb3, 0xe4, 0x2f,
},
},
{
.data_len = 1,
.digest = {
0xe3, 0x4d, 0x79, 0x17, 0x75, 0x35, 0xdc, 0xd2,
0x27, 0xc9, 0x9d, 0x0b, 0x90, 0x0f, 0x21, 0x5d,
0x95, 0xfb, 0x9c, 0x6d, 0xa8, 0xec, 0x19, 0x15,
0x12, 0xef, 0xf5, 0x0f,
},
},
{
.data_len = 2,
.digest = {
0x81, 0xc7, 0x60, 0x0d, 0x6d, 0x13, 0x75, 0x70,
0x4b, 0xc0, 0xab, 0xea, 0x04, 0xe3, 0x78, 0x7e,
0x73, 0xb9, 0x0f, 0xb6, 0xae, 0x90, 0xf3, 0x94,
0xb2, 0x56, 0xda, 0xc8,
},
},
{
.data_len = 3,
.digest = {
0x24, 0xf0, 0x8c, 0x6e, 0x9d, 0xd6, 0x06, 0x80,
0x0a, 0x03, 0xee, 0x9b, 0x33, 0xec, 0x83, 0x42,
0x2c, 0x8b, 0xe7, 0xc7, 0xc6, 0x04, 0xfb, 0xc6,
0xa3, 0x3a, 0x4d, 0xc9,
},
},
{
.data_len = 16,
.digest = {
0x1c, 0x08, 0xa8, 0x55, 0x8f, 0xc6, 0x0a, 0xea,
0x2f, 0x1b, 0x54, 0xff, 0x8d, 0xd2, 0xa3, 0xc7,
0x42, 0xc2, 0x93, 0x3d, 0x73, 0x18, 0x84, 0xba,
0x75, 0x49, 0x34, 0xfd,
},
},
{
.data_len = 32,
.digest = {
0x45, 0xdd, 0xb5, 0xf0, 0x3c, 0xda, 0xe6, 0xd4,
0x6c, 0x86, 0x91, 0x29, 0x11, 0x2f, 0x88, 0x7d,
0xd8, 0x3c, 0xa3, 0xd6, 0xdd, 0x1e, 0xac, 0x98,
0xff, 0xf0, 0x14, 0x69,
},
},
{
.data_len = 48,
.digest = {
0x0b, 0xfb, 0x71, 0x4c, 0x06, 0x7a, 0xd5, 0x89,
0x76, 0x0a, 0x43, 0x8b, 0x2b, 0x47, 0x12, 0x56,
0xa7, 0x64, 0x33, 0x1d, 0xd3, 0x44, 0x17, 0x95,
0x23, 0xe7, 0x53, 0x01,
},
},
{
.data_len = 49,
.digest = {
0xc4, 0xae, 0x9c, 0x33, 0xd5, 0x1d, 0xf4, 0xa7,
0xfd, 0xb7, 0xd4, 0x6b, 0xc3, 0xeb, 0xa8, 0xbf,
0xfb, 0x07, 0x89, 0x4b, 0x07, 0x15, 0x22, 0xec,
0xe1, 0x45, 0x84, 0xba,
},
},
{
.data_len = 63,
.digest = {
0xad, 0x01, 0x34, 0x2a, 0xe2, 0x3b, 0x58, 0x06,
0x9f, 0x20, 0xc8, 0xfb, 0xf3, 0x20, 0x82, 0xa6,
0x9f, 0xee, 0x7a, 0xbe, 0xdf, 0xf3, 0x5d, 0x57,
0x9b, 0xce, 0x79, 0x96,
},
},
{
.data_len = 64,
.digest = {
0xa7, 0xa6, 0x47, 0xf7, 0xed, 0x2a, 0xe5, 0xe3,
0xc0, 0x1e, 0x7b, 0x40, 0xe4, 0xf7, 0x40, 0x65,
0x42, 0xc1, 0x6f, 0x7d, 0x8d, 0x0d, 0x17, 0x4f,
0xd3, 0xbc, 0x0d, 0x85,
},
},
{
.data_len = 65,
.digest = {
0xc4, 0x9c, 0xb5, 0x6a, 0x01, 0x2d, 0x10, 0xa9,
0x5f, 0xa4, 0x5a, 0xe1, 0xba, 0x40, 0x12, 0x09,
0x7b, 0xea, 0xdb, 0xa6, 0x7b, 0xcb, 0x56, 0xf0,
0xfd, 0x5b, 0xe2, 0xe7,
},
},
{
.data_len = 127,
.digest = {
0x14, 0xda, 0x0e, 0x01, 0xca, 0x78, 0x7d, 0x2d,
0x85, 0xa3, 0xca, 0x0e, 0x80, 0xf9, 0x95, 0x10,
0xa1, 0x7b, 0xa5, 0xaa, 0xfc, 0x95, 0x05, 0x08,
0x53, 0xda, 0x52, 0xee,
},
},
{
.data_len = 128,
.digest = {
0xa5, 0x24, 0xc4, 0x54, 0xe1, 0x50, 0xab, 0xee,
0x22, 0xc1, 0xa7, 0x27, 0x15, 0x2c, 0x6f, 0xf7,
0x4c, 0x31, 0xe5, 0x15, 0x25, 0x4e, 0x71, 0xc6,
0x7e, 0xa0, 0x11, 0x5d,
},
},
{
.data_len = 129,
.digest = {
0x73, 0xd0, 0x8c, 0xce, 0xed, 0xed, 0x9f, 0xaa,
0x21, 0xaf, 0xa2, 0x08, 0x80, 0x16, 0x15, 0x59,
0x3f, 0x1d, 0x7f, 0x0a, 0x79, 0x3d, 0x7b, 0x58,
0xf8, 0xc8, 0x5c, 0x27,
},
},
{
.data_len = 256,
.digest = {
0x31, 0xa7, 0xa1, 0xca, 0x49, 0x72, 0x75, 0xcc,
0x6e, 0x02, 0x9e, 0xad, 0xea, 0x86, 0x5c, 0x91,
0x02, 0xe4, 0xc9, 0xf9, 0xd3, 0x9e, 0x74, 0x50,
0xd8, 0x43, 0x6b, 0x85,
},
},
{
.data_len = 511,
.digest = {
0x40, 0x60, 0x8b, 0xb0, 0x03, 0xa9, 0x75, 0xab,
0x2d, 0x5b, 0x20, 0x9a, 0x05, 0x72, 0xb7, 0xa8,
0xce, 0xf2, 0x4f, 0x66, 0x62, 0xe3, 0x7e, 0x24,
0xd6, 0xe2, 0xea, 0xfa,
},
},
{
.data_len = 513,
.digest = {
0x4f, 0x5f, 0x9f, 0x1e, 0xb3, 0x66, 0x81, 0xdb,
0x41, 0x5d, 0x65, 0x97, 0x00, 0x8d, 0xdc, 0x62,
0x03, 0xb0, 0x4d, 0x6b, 0x5c, 0x7f, 0x1e, 0xa0,
0xfe, 0xfc, 0x0e, 0xb8,
},
},
{
.data_len = 1000,
.digest = {
0x08, 0xa8, 0xa1, 0xc0, 0xd8, 0xf9, 0xb4, 0xaa,
0x53, 0x22, 0xa1, 0x73, 0x0b, 0x45, 0xa0, 0x20,
0x72, 0xf3, 0xa9, 0xbc, 0x51, 0xd0, 0x20, 0x79,
0x69, 0x97, 0xf7, 0xe3,
},
},
{
.data_len = 3333,
.digest = {
0xe8, 0x60, 0x5f, 0xb9, 0x12, 0xe1, 0x6b, 0x24,
0xc5, 0xe8, 0x43, 0xa9, 0x5c, 0x3f, 0x65, 0xed,
0xbe, 0xfd, 0x77, 0xf5, 0x47, 0xf2, 0x75, 0x21,
0xc2, 0x8f, 0x54, 0x8f,
},
},
{
.data_len = 4096,
.digest = {
0xc7, 0xdf, 0x50, 0x16, 0x10, 0x01, 0xb7, 0xdf,
0x34, 0x1d, 0x18, 0xa2, 0xd5, 0xad, 0x1f, 0x50,
0xf7, 0xa8, 0x9a, 0x72, 0xfb, 0xfd, 0xd9, 0x1c,
0x57, 0xac, 0x08, 0x97,
},
},
{
.data_len = 4128,
.digest = {
0xdf, 0x16, 0x76, 0x7f, 0xc0, 0x16, 0x84, 0x63,
0xac, 0xcf, 0xd0, 0x78, 0x1e, 0x96, 0x67, 0xc5,
0x3c, 0x06, 0xe9, 0xdb, 0x6e, 0x7d, 0xd0, 0x07,
0xaa, 0xb1, 0x56, 0xc9,
},
},
{
.data_len = 4160,
.digest = {
0x49, 0xec, 0x5c, 0x18, 0xd7, 0x5b, 0xda, 0xed,
0x5b, 0x59, 0xde, 0x09, 0x34, 0xb2, 0x49, 0x43,
0x62, 0x6a, 0x0a, 0x63, 0x6a, 0x51, 0x08, 0x37,
0x8c, 0xb6, 0x29, 0x84,
},
},
{
.data_len = 4224,
.digest = {
0x3d, 0xc2, 0xc8, 0x43, 0xcf, 0xb7, 0x33, 0x14,
0x04, 0x93, 0xed, 0xe2, 0xcd, 0x8a, 0x69, 0x5c,
0x5a, 0xd5, 0x9b, 0x52, 0xdf, 0x48, 0xa7, 0xaa,
0x28, 0x2b, 0x5d, 0x27,
},
},
{
.data_len = 16384,
.digest = {
0xa7, 0xaf, 0xda, 0x92, 0xe2, 0xe7, 0x61, 0xdc,
0xa1, 0x32, 0x53, 0x2a, 0x3f, 0x41, 0x5c, 0x7e,
0xc9, 0x89, 0xda, 0x1c, 0xf7, 0x8d, 0x00, 0xbd,
0x21, 0x73, 0xb1, 0x69,
},
},
};
static const u8 hash_testvec_consolidated[SHA224_DIGEST_SIZE] = {
0x9e, 0xb8, 0x82, 0xab, 0x83, 0x37, 0xe4, 0x63,
0x84, 0xee, 0x21, 0x15, 0xc2, 0xbb, 0xa3, 0x17,
0x8f, 0xc4, 0x99, 0x33, 0xa0, 0x2c, 0x9f, 0xec,
0xca, 0xd0, 0xf3, 0x73,
};
static const u8 hmac_testvec_consolidated[SHA224_DIGEST_SIZE] = {
0x66, 0x34, 0x79, 0x92, 0x47, 0x0e, 0xcd, 0x70,
0xb0, 0x8b, 0x91, 0xcb, 0x94, 0x2f, 0x67, 0x65,
0x2f, 0xc9, 0xd2, 0x91, 0x32, 0xaf, 0xf7, 0x5f,
0xb6, 0x01, 0x5b, 0xf2,
};
@@ -0,0 +1,39 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright 2025 Google LLC
*/
#include <crypto/sha2.h>
#include "sha224-testvecs.h"
#define HASH sha224
#define HASH_CTX sha224_ctx
#define HASH_SIZE SHA224_DIGEST_SIZE
#define HASH_INIT sha224_init
#define HASH_UPDATE sha224_update
#define HASH_FINAL sha224_final
#define HMAC_KEY hmac_sha224_key
#define HMAC_CTX hmac_sha224_ctx
#define HMAC_PREPAREKEY hmac_sha224_preparekey
#define HMAC_INIT hmac_sha224_init
#define HMAC_UPDATE hmac_sha224_update
#define HMAC_FINAL hmac_sha224_final
#define HMAC hmac_sha224
#define HMAC_USINGRAWKEY hmac_sha224_usingrawkey
#include "hash-test-template.h"
static struct kunit_case hash_test_cases[] = {
HASH_KUNIT_CASES,
KUNIT_CASE(benchmark_hash),
{},
};
static struct kunit_suite hash_test_suite = {
.name = "sha224",
.test_cases = hash_test_cases,
.suite_init = hash_suite_init,
.suite_exit = hash_suite_exit,
};
kunit_test_suite(hash_test_suite);
MODULE_DESCRIPTION("KUnit tests and benchmark for SHA-224 and HMAC-SHA224");
MODULE_LICENSE("GPL");
@@ -0,0 +1,238 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */
/* This file was generated by: ./scripts/crypto/gen-hash-testvecs.py sha256 */
static const struct {
size_t data_len;
u8 digest[SHA256_DIGEST_SIZE];
} hash_testvecs[] = {
{
.data_len = 0,
.digest = {
0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14,
0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,
0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55,
},
},
{
.data_len = 1,
.digest = {
0x45, 0xf8, 0x3d, 0x17, 0xe1, 0x0b, 0x34, 0xfc,
0xa0, 0x1e, 0xb8, 0xf4, 0x45, 0x4d, 0xac, 0x34,
0xa7, 0x77, 0xd9, 0x40, 0x4a, 0x46, 0x4e, 0x73,
0x2c, 0xf4, 0xab, 0xf2, 0xc0, 0xda, 0x94, 0xc4,
},
},
{
.data_len = 2,
.digest = {
0xf9, 0xd3, 0x52, 0x2f, 0xd5, 0xe0, 0x99, 0x15,
0x1c, 0xd6, 0xa9, 0x24, 0x4f, 0x40, 0xba, 0x25,
0x33, 0x43, 0x3e, 0xe1, 0x78, 0x6a, 0xfe, 0x7d,
0x07, 0xe2, 0x29, 0x7b, 0x6d, 0xc5, 0x73, 0xf5,
},
},
{
.data_len = 3,
.digest = {
0x71, 0xf7, 0xa1, 0xef, 0x69, 0x86, 0x0e, 0xe4,
0x87, 0x25, 0x58, 0x4c, 0x07, 0x2c, 0xfc, 0x60,
0xc5, 0xf6, 0xe2, 0x44, 0xaa, 0xfb, 0x41, 0xc7,
0x2b, 0xc5, 0x01, 0x8c, 0x39, 0x98, 0x30, 0x37,
},
},
{
.data_len = 16,
.digest = {
0x09, 0x95, 0x9a, 0xfa, 0x25, 0x18, 0x86, 0x06,
0xfe, 0x65, 0xc9, 0x2f, 0x91, 0x15, 0x74, 0x06,
0x6c, 0xbf, 0xef, 0x7b, 0x0b, 0xc7, 0x2c, 0x05,
0xdd, 0x17, 0x5d, 0x6f, 0x8a, 0xa5, 0xde, 0x3c,
},
},
{
.data_len = 32,
.digest = {
0xe5, 0x52, 0x3c, 0x85, 0xea, 0x1b, 0xe1, 0x6c,
0xe0, 0xdb, 0xc3, 0xef, 0xf0, 0xca, 0xc2, 0xe1,
0xb9, 0x36, 0xa1, 0x28, 0xb6, 0x9e, 0xf5, 0x6e,
0x70, 0xf7, 0xf9, 0xa7, 0x1c, 0xd3, 0x22, 0xd0,
},
},
{
.data_len = 48,
.digest = {
0x5f, 0x84, 0xd4, 0xd7, 0x2e, 0x80, 0x09, 0xef,
0x1c, 0x77, 0x7c, 0x25, 0x59, 0x63, 0x88, 0x64,
0xfd, 0x56, 0xea, 0x23, 0xf4, 0x4f, 0x2e, 0x49,
0xcd, 0xb4, 0xaa, 0xc7, 0x5c, 0x8b, 0x75, 0x84,
},
},
{
.data_len = 49,
.digest = {
0x22, 0x6e, 0xca, 0xda, 0x00, 0x2d, 0x90, 0x96,
0x24, 0xf8, 0x55, 0x17, 0x11, 0xda, 0x42, 0x1c,
0x78, 0x4e, 0xbf, 0xd9, 0xc5, 0xcf, 0xf3, 0xe3,
0xaf, 0xd3, 0x60, 0xcd, 0xaa, 0xe2, 0xc7, 0x22,
},
},
{
.data_len = 63,
.digest = {
0x97, 0xe2, 0x74, 0xdc, 0x6b, 0xa4, 0xaf, 0x32,
0x3b, 0x50, 0x6d, 0x80, 0xb5, 0xd3, 0x0c, 0x36,
0xea, 0x3f, 0x5d, 0x36, 0xa7, 0x49, 0x51, 0xf3,
0xbd, 0x69, 0x68, 0x60, 0x9b, 0xde, 0x73, 0xf5,
},
},
{
.data_len = 64,
.digest = {
0x13, 0x74, 0xb1, 0x72, 0xd6, 0x53, 0x48, 0x28,
0x42, 0xd8, 0xba, 0x64, 0x20, 0x60, 0xb6, 0x4c,
0xc3, 0xac, 0x5d, 0x93, 0x8c, 0xb9, 0xd4, 0xcc,
0xb4, 0x9f, 0x31, 0x1f, 0xeb, 0x68, 0x35, 0x58,
},
},
{
.data_len = 65,
.digest = {
0xda, 0xbe, 0xd7, 0xbc, 0x6e, 0xe6, 0x5a, 0x57,
0xeb, 0x9a, 0x93, 0xaa, 0x66, 0xd0, 0xe0, 0xc4,
0x29, 0x7f, 0xe9, 0x3b, 0x8e, 0xdf, 0x81, 0x82,
0x8d, 0x15, 0x11, 0x59, 0x4e, 0x13, 0xa5, 0x58,
},
},
{
.data_len = 127,
.digest = {
0x8c, 0x1a, 0xba, 0x40, 0x66, 0x94, 0x19, 0xf4,
0x2e, 0xa2, 0xae, 0x94, 0x53, 0x18, 0xb6, 0xfd,
0xa0, 0x12, 0xc5, 0xef, 0xd5, 0xd6, 0x1b, 0xa1,
0x37, 0xea, 0x19, 0x44, 0x35, 0x54, 0x85, 0x74,
},
},
{
.data_len = 128,
.digest = {
0xfd, 0x07, 0xd8, 0x77, 0x7d, 0x8b, 0x4f, 0xee,
0x60, 0x60, 0x26, 0xef, 0x2a, 0x86, 0xfb, 0x67,
0xeb, 0x31, 0x27, 0x03, 0x99, 0x3c, 0xde, 0xe5,
0x84, 0x72, 0x71, 0x4c, 0x33, 0x7b, 0x87, 0x13,
},
},
{
.data_len = 129,
.digest = {
0x97, 0xc5, 0x58, 0x38, 0x20, 0xc7, 0xde, 0xfa,
0xdd, 0x9b, 0x10, 0xc6, 0xc2, 0x2f, 0x94, 0xb5,
0xc0, 0x33, 0xc0, 0x20, 0x1c, 0x2f, 0xb4, 0x28,
0x5e, 0x36, 0xfa, 0x8c, 0x24, 0x1c, 0x18, 0x27,
},
},
{
.data_len = 256,
.digest = {
0x62, 0x17, 0x84, 0x26, 0x98, 0x30, 0x57, 0xca,
0x4f, 0x32, 0xd9, 0x09, 0x09, 0x34, 0xe2, 0xcb,
0x92, 0x45, 0xd5, 0xeb, 0x8b, 0x9b, 0x3c, 0xd8,
0xaa, 0xc7, 0xd2, 0x2b, 0x04, 0xab, 0xb3, 0x35,
},
},
{
.data_len = 511,
.digest = {
0x7f, 0xe1, 0x09, 0x78, 0x5d, 0x61, 0xfa, 0x5e,
0x9b, 0x8c, 0xb1, 0xa9, 0x09, 0x69, 0xb4, 0x24,
0x54, 0xf2, 0x1c, 0xc9, 0x5f, 0xfb, 0x59, 0x9d,
0x36, 0x1b, 0x37, 0x44, 0xfc, 0x64, 0x79, 0xb6,
},
},
{
.data_len = 513,
.digest = {
0xd2, 0x3b, 0x3a, 0xe7, 0x13, 0x4f, 0xbd, 0x29,
0x6b, 0xd2, 0x79, 0x26, 0x6c, 0xd2, 0x22, 0x43,
0x25, 0x34, 0x9b, 0x9b, 0x22, 0xb0, 0x9f, 0x61,
0x1d, 0xf4, 0xe2, 0x65, 0x68, 0x95, 0x02, 0x6c,
},
},
{
.data_len = 1000,
.digest = {
0x0c, 0x34, 0x53, 0x3f, 0x0f, 0x8a, 0x39, 0x8d,
0x63, 0xe4, 0x83, 0x6e, 0x11, 0x7d, 0x14, 0x8e,
0x5b, 0xf0, 0x4d, 0xca, 0x23, 0x24, 0xb5, 0xd2,
0x13, 0x3f, 0xd9, 0xde, 0x84, 0x74, 0x26, 0x59,
},
},
{
.data_len = 3333,
.digest = {
0xa8, 0xb8, 0x83, 0x01, 0x1b, 0x38, 0x7a, 0xca,
0x59, 0xe9, 0x5b, 0x37, 0x6a, 0xab, 0xb4, 0x85,
0x94, 0x73, 0x26, 0x04, 0xef, 0xed, 0xf4, 0x0d,
0xd6, 0x09, 0x21, 0x09, 0x96, 0x78, 0xe3, 0xcf,
},
},
{
.data_len = 4096,
.digest = {
0x0b, 0x12, 0x66, 0x96, 0x78, 0x4f, 0x2c, 0x35,
0xa4, 0xed, 0xbc, 0xb8, 0x30, 0xa6, 0x37, 0x9b,
0x94, 0x13, 0xae, 0x86, 0xf0, 0x20, 0xfb, 0x49,
0x8f, 0x5d, 0x20, 0x70, 0x60, 0x2b, 0x02, 0x70,
},
},
{
.data_len = 4128,
.digest = {
0xe4, 0xbd, 0xe4, 0x3b, 0x85, 0xf4, 0x6f, 0x11,
0xad, 0xc4, 0x79, 0xcc, 0x8e, 0x6d, 0x8b, 0x15,
0xbb, 0xf9, 0xd3, 0x65, 0xe1, 0xf8, 0x8d, 0x22,
0x65, 0x66, 0x66, 0xb3, 0xf5, 0xd0, 0x9c, 0xaf,
},
},
{
.data_len = 4160,
.digest = {
0x90, 0x5f, 0xe0, 0xfc, 0xb1, 0xdc, 0x38, 0x1b,
0xe5, 0x37, 0x3f, 0xd2, 0xcc, 0x48, 0xc4, 0xbc,
0xb4, 0xfd, 0xf7, 0x71, 0x5f, 0x6b, 0xf4, 0xc4,
0xa6, 0x08, 0x7e, 0xfc, 0x4e, 0x96, 0xf7, 0xc2,
},
},
{
.data_len = 4224,
.digest = {
0x1f, 0x34, 0x0a, 0x3b, 0xdb, 0xf7, 0x7a, 0xdb,
0x3d, 0x89, 0x85, 0x0c, 0xd2, 0xf0, 0x0c, 0xbd,
0x25, 0x39, 0x14, 0x06, 0x28, 0x0f, 0x6b, 0x5f,
0xe3, 0x1f, 0x2a, 0xb6, 0xca, 0x56, 0x41, 0xa1,
},
},
{
.data_len = 16384,
.digest = {
0x7b, 0x01, 0x2d, 0x84, 0x70, 0xee, 0xe0, 0x77,
0x3c, 0x17, 0x63, 0xfe, 0x40, 0xd7, 0xfd, 0xa1,
0x75, 0x90, 0xb8, 0x3e, 0x50, 0xcd, 0x06, 0xb7,
0xb9, 0xb9, 0x2b, 0x91, 0x4f, 0xba, 0xe4, 0x4c,
},
},
};
static const u8 hash_testvec_consolidated[SHA256_DIGEST_SIZE] = {
0x78, 0x1c, 0xb1, 0x9f, 0xe5, 0xe1, 0xcb, 0x41,
0x8b, 0x34, 0x00, 0x33, 0x57, 0xc3, 0x1c, 0x8f,
0x5c, 0x84, 0xc7, 0x8b, 0x87, 0x6a, 0x13, 0x29,
0x2f, 0xc4, 0x1a, 0x70, 0x5e, 0x40, 0xf2, 0xfe,
};
static const u8 hmac_testvec_consolidated[SHA256_DIGEST_SIZE] = {
0x56, 0x96, 0x2e, 0x23, 0x3f, 0x94, 0x89, 0x0d,
0x0f, 0x24, 0x36, 0x2e, 0x19, 0x3d, 0xb5, 0xac,
0xb8, 0xcd, 0xf1, 0xc9, 0xca, 0xac, 0xee, 0x9d,
0x62, 0xe6, 0x81, 0xe5, 0x96, 0xf9, 0x38, 0xf5,
};
+224
View File
@@ -0,0 +1,224 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright 2025 Google LLC
*/
#include <crypto/sha2.h>
#include "sha256-testvecs.h"
/* Generate the HASH_KUNIT_CASES using hash-test-template.h. */
#define HASH sha256
#define HASH_CTX sha256_ctx
#define HASH_SIZE SHA256_DIGEST_SIZE
#define HASH_INIT sha256_init
#define HASH_UPDATE sha256_update
#define HASH_FINAL sha256_final
#define HMAC_KEY hmac_sha256_key
#define HMAC_CTX hmac_sha256_ctx
#define HMAC_PREPAREKEY hmac_sha256_preparekey
#define HMAC_INIT hmac_sha256_init
#define HMAC_UPDATE hmac_sha256_update
#define HMAC_FINAL hmac_sha256_final
#define HMAC hmac_sha256
#define HMAC_USINGRAWKEY hmac_sha256_usingrawkey
#include "hash-test-template.h"
static void free_guarded_buf(void *buf)
{
vfree(buf);
}
/*
* Allocate a KUnit-managed buffer that has length @len bytes immediately
* followed by an unmapped page, and assert that the allocation succeeds.
*/
static void *alloc_guarded_buf(struct kunit *test, size_t len)
{
size_t full_len = round_up(len, PAGE_SIZE);
void *buf = vmalloc(full_len);
KUNIT_ASSERT_NOT_NULL(test, buf);
KUNIT_ASSERT_EQ(test, 0,
kunit_add_action_or_reset(test, free_guarded_buf, buf));
return buf + full_len - len;
}
/*
* Test for sha256_finup_2x(). Specifically, choose various data lengths and
* salt lengths, and for each one, verify that sha256_finup_2x() produces the
* same results as sha256_update() and sha256_final().
*
* Use guarded buffers for all inputs and outputs to reliably detect any
* out-of-bounds reads or writes, even if they occur in assembly code.
*/
static void test_sha256_finup_2x(struct kunit *test)
{
const size_t max_data_len = 16384;
u8 *data1_buf, *data2_buf, *hash1, *hash2;
u8 expected_hash1[SHA256_DIGEST_SIZE];
u8 expected_hash2[SHA256_DIGEST_SIZE];
u8 salt[SHA256_BLOCK_SIZE];
struct sha256_ctx *ctx;
data1_buf = alloc_guarded_buf(test, max_data_len);
data2_buf = alloc_guarded_buf(test, max_data_len);
hash1 = alloc_guarded_buf(test, SHA256_DIGEST_SIZE);
hash2 = alloc_guarded_buf(test, SHA256_DIGEST_SIZE);
ctx = alloc_guarded_buf(test, sizeof(*ctx));
rand_bytes(data1_buf, max_data_len);
rand_bytes(data2_buf, max_data_len);
rand_bytes(salt, sizeof(salt));
memset(ctx, 0, sizeof(*ctx));
for (size_t i = 0; i < 500; i++) {
size_t salt_len = rand_length(sizeof(salt));
size_t data_len = rand_length(max_data_len);
const u8 *data1 = data1_buf + max_data_len - data_len;
const u8 *data2 = data2_buf + max_data_len - data_len;
struct sha256_ctx orig_ctx;
sha256_init(ctx);
sha256_update(ctx, salt, salt_len);
orig_ctx = *ctx;
sha256_finup_2x(ctx, data1, data2, data_len, hash1, hash2);
KUNIT_ASSERT_MEMEQ_MSG(
test, ctx, &orig_ctx, sizeof(*ctx),
"sha256_finup_2x() modified its ctx argument");
sha256_update(ctx, data1, data_len);
sha256_final(ctx, expected_hash1);
sha256_update(&orig_ctx, data2, data_len);
sha256_final(&orig_ctx, expected_hash2);
KUNIT_ASSERT_MEMEQ_MSG(
test, hash1, expected_hash1, SHA256_DIGEST_SIZE,
"Wrong hash1 with salt_len=%zu data_len=%zu", salt_len,
data_len);
KUNIT_ASSERT_MEMEQ_MSG(
test, hash2, expected_hash2, SHA256_DIGEST_SIZE,
"Wrong hash2 with salt_len=%zu data_len=%zu", salt_len,
data_len);
}
}
/* Test sha256_finup_2x() with ctx == NULL */
static void test_sha256_finup_2x_defaultctx(struct kunit *test)
{
const size_t data_len = 128;
struct sha256_ctx ctx;
u8 hash1_a[SHA256_DIGEST_SIZE];
u8 hash2_a[SHA256_DIGEST_SIZE];
u8 hash1_b[SHA256_DIGEST_SIZE];
u8 hash2_b[SHA256_DIGEST_SIZE];
rand_bytes(test_buf, 2 * data_len);
sha256_init(&ctx);
sha256_finup_2x(&ctx, test_buf, &test_buf[data_len], data_len, hash1_a,
hash2_a);
sha256_finup_2x(NULL, test_buf, &test_buf[data_len], data_len, hash1_b,
hash2_b);
KUNIT_ASSERT_MEMEQ(test, hash1_a, hash1_b, SHA256_DIGEST_SIZE);
KUNIT_ASSERT_MEMEQ(test, hash2_a, hash2_b, SHA256_DIGEST_SIZE);
}
/*
* Test that sha256_finup_2x() and sha256_update/final() produce consistent
* results with total message lengths that require more than 32 bits.
*/
static void test_sha256_finup_2x_hugelen(struct kunit *test)
{
const size_t data_len = 4 * SHA256_BLOCK_SIZE;
struct sha256_ctx ctx = {};
u8 expected_hash[SHA256_DIGEST_SIZE];
u8 hash[SHA256_DIGEST_SIZE];
rand_bytes(test_buf, data_len);
for (size_t align = 0; align < SHA256_BLOCK_SIZE; align++) {
sha256_init(&ctx);
ctx.ctx.bytecount = 0x123456789abcd00 + align;
sha256_finup_2x(&ctx, test_buf, test_buf, data_len, hash, hash);
sha256_update(&ctx, test_buf, data_len);
sha256_final(&ctx, expected_hash);
KUNIT_ASSERT_MEMEQ(test, hash, expected_hash,
SHA256_DIGEST_SIZE);
}
}
/* Benchmark for sha256_finup_2x() */
static void benchmark_sha256_finup_2x(struct kunit *test)
{
/*
* Try a few different salt lengths, since sha256_finup_2x() performance
* may vary slightly for the same data_len depending on how many bytes
* were already processed in the initial context.
*/
static const size_t salt_lens_to_test[] = { 0, 32, 64 };
const size_t data_len = 4096;
const size_t num_iters = 4096;
struct sha256_ctx ctx;
u8 hash1[SHA256_DIGEST_SIZE];
u8 hash2[SHA256_DIGEST_SIZE];
if (!IS_ENABLED(CONFIG_CRYPTO_LIB_BENCHMARK))
kunit_skip(test, "not enabled");
if (!sha256_finup_2x_is_optimized())
kunit_skip(test, "not relevant");
rand_bytes(test_buf, data_len * 2);
/* Warm-up */
for (size_t i = 0; i < num_iters; i++)
sha256_finup_2x(NULL, &test_buf[0], &test_buf[data_len],
data_len, hash1, hash2);
for (size_t i = 0; i < ARRAY_SIZE(salt_lens_to_test); i++) {
size_t salt_len = salt_lens_to_test[i];
u64 t0, t1;
/*
* Prepare the initial context. The time to process the salt is
* not measured; we're just interested in sha256_finup_2x().
*/
sha256_init(&ctx);
sha256_update(&ctx, test_buf, salt_len);
preempt_disable();
t0 = ktime_get_ns();
for (size_t j = 0; j < num_iters; j++)
sha256_finup_2x(&ctx, &test_buf[0], &test_buf[data_len],
data_len, hash1, hash2);
t1 = ktime_get_ns();
preempt_enable();
kunit_info(test, "data_len=%zu salt_len=%zu: %llu MB/s",
data_len, salt_len,
div64_u64((u64)data_len * 2 * num_iters * 1000,
t1 - t0 ?: 1));
}
}
static struct kunit_case hash_test_cases[] = {
HASH_KUNIT_CASES,
KUNIT_CASE(test_sha256_finup_2x),
KUNIT_CASE(test_sha256_finup_2x_defaultctx),
KUNIT_CASE(test_sha256_finup_2x_hugelen),
KUNIT_CASE(benchmark_hash),
KUNIT_CASE(benchmark_sha256_finup_2x),
{},
};
static struct kunit_suite hash_test_suite = {
.name = "sha256",
.test_cases = hash_test_cases,
.suite_init = hash_suite_init,
.suite_exit = hash_suite_exit,
};
kunit_test_suite(hash_test_suite);
MODULE_DESCRIPTION("KUnit tests and benchmark for SHA-256 and HMAC-SHA256");
MODULE_LICENSE("GPL");
@@ -0,0 +1,249 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */
/* This file was generated by: ./scripts/crypto/gen-hash-testvecs.py sha3 */
/* SHA3-256 test vectors */
static const struct {
size_t data_len;
u8 digest[SHA3_256_DIGEST_SIZE];
} hash_testvecs[] = {
{
.data_len = 0,
.digest = {
0xa7, 0xff, 0xc6, 0xf8, 0xbf, 0x1e, 0xd7, 0x66,
0x51, 0xc1, 0x47, 0x56, 0xa0, 0x61, 0xd6, 0x62,
0xf5, 0x80, 0xff, 0x4d, 0xe4, 0x3b, 0x49, 0xfa,
0x82, 0xd8, 0x0a, 0x4b, 0x80, 0xf8, 0x43, 0x4a,
},
},
{
.data_len = 1,
.digest = {
0x11, 0x03, 0xe7, 0x84, 0x51, 0x50, 0x86, 0x35,
0x71, 0x8a, 0x70, 0xe3, 0xc4, 0x26, 0x7b, 0x21,
0x02, 0x13, 0xa0, 0x81, 0xe8, 0xe6, 0x14, 0x25,
0x07, 0x34, 0xe5, 0xc5, 0x40, 0x06, 0xf2, 0x8b,
},
},
{
.data_len = 2,
.digest = {
0x2f, 0x6f, 0x6d, 0x47, 0x48, 0x52, 0x11, 0xb9,
0xe4, 0x3d, 0xc8, 0x71, 0xcf, 0xb2, 0xee, 0xae,
0x5b, 0xf4, 0x12, 0x84, 0x5b, 0x1c, 0xec, 0x6c,
0xc1, 0x66, 0x88, 0xaa, 0xc3, 0x40, 0xbd, 0x7e,
},
},
{
.data_len = 3,
.digest = {
0xec, 0x02, 0xe8, 0x81, 0x4f, 0x84, 0x41, 0x69,
0x06, 0xd8, 0xdc, 0x1d, 0x01, 0x78, 0xd7, 0xcb,
0x39, 0xdf, 0xd3, 0x12, 0x1c, 0x99, 0xfd, 0xf3,
0x5c, 0x83, 0xc9, 0xc2, 0x7a, 0x7b, 0x6a, 0x05,
},
},
{
.data_len = 16,
.digest = {
0xff, 0x6f, 0xc3, 0x41, 0xc3, 0x5f, 0x34, 0x6d,
0xa7, 0xdf, 0x3e, 0xc2, 0x8b, 0x29, 0xb6, 0xf1,
0xf8, 0x67, 0xfd, 0xcd, 0xb1, 0x9f, 0x38, 0x08,
0x1d, 0x8d, 0xd9, 0xc2, 0x43, 0x66, 0x18, 0x6c,
},
},
{
.data_len = 32,
.digest = {
0xe4, 0xb1, 0x06, 0x17, 0xf8, 0x8b, 0x91, 0x95,
0xe7, 0x57, 0x66, 0xac, 0x08, 0xb2, 0x03, 0x3e,
0xf7, 0x84, 0x1f, 0xe3, 0x25, 0xa3, 0x11, 0xd2,
0x11, 0xa4, 0x78, 0x74, 0x2a, 0x43, 0x20, 0xa5,
},
},
{
.data_len = 48,
.digest = {
0xeb, 0x57, 0x5f, 0x20, 0xa3, 0x6b, 0xc7, 0xb4,
0x66, 0x2a, 0xa0, 0x30, 0x3b, 0x52, 0x00, 0xc9,
0xce, 0x6a, 0xd8, 0x1e, 0xbe, 0xed, 0xa1, 0xd1,
0xbe, 0x63, 0xc7, 0xe1, 0xe2, 0x66, 0x67, 0x0c,
},
},
{
.data_len = 49,
.digest = {
0xf0, 0x67, 0xad, 0x66, 0xbe, 0xec, 0x5a, 0xfd,
0x29, 0xd2, 0x4f, 0x1d, 0xb2, 0x24, 0xb8, 0x90,
0x05, 0x28, 0x0e, 0x66, 0x67, 0x74, 0x2d, 0xee,
0x66, 0x25, 0x11, 0xd1, 0x76, 0xa2, 0xfc, 0x3a,
},
},
{
.data_len = 63,
.digest = {
0x57, 0x56, 0x21, 0xb3, 0x2d, 0x2d, 0xe1, 0x9d,
0xbf, 0x2c, 0x82, 0xa8, 0xad, 0x7e, 0x6c, 0x46,
0xfb, 0x30, 0xeb, 0xce, 0xcf, 0xed, 0x2d, 0x65,
0xe7, 0xe4, 0x96, 0x69, 0xe0, 0x48, 0xd2, 0xb6,
},
},
{
.data_len = 64,
.digest = {
0x7b, 0xba, 0x67, 0x15, 0xe5, 0x21, 0xc4, 0x69,
0xd3, 0xef, 0x5c, 0x97, 0x9f, 0x5b, 0xba, 0x9c,
0xfa, 0x55, 0x64, 0xec, 0xb5, 0x37, 0x53, 0x1b,
0x3f, 0x4c, 0x0a, 0xed, 0x51, 0x98, 0x2b, 0x52,
},
},
{
.data_len = 65,
.digest = {
0x44, 0xb6, 0x6b, 0x83, 0x09, 0x83, 0x55, 0x83,
0xde, 0x1f, 0xcc, 0x33, 0xef, 0xdc, 0x05, 0xbb,
0x3b, 0x63, 0x76, 0x45, 0xe4, 0x8e, 0x14, 0x7a,
0x2d, 0xae, 0x90, 0xce, 0x68, 0xc3, 0xa4, 0xf2,
},
},
{
.data_len = 127,
.digest = {
0x50, 0x3e, 0x99, 0x4e, 0x28, 0x2b, 0xc9, 0xf4,
0xf5, 0xeb, 0x2b, 0x16, 0x04, 0x2d, 0xf5, 0xbe,
0xc0, 0x91, 0x41, 0x2a, 0x8e, 0x69, 0x5e, 0x39,
0x53, 0x2c, 0xc1, 0x18, 0xa5, 0xeb, 0xd8, 0xda,
},
},
{
.data_len = 128,
.digest = {
0x90, 0x0b, 0xa6, 0x92, 0x84, 0x30, 0xaf, 0xee,
0x38, 0x59, 0x83, 0x83, 0xe9, 0xfe, 0xab, 0x86,
0x79, 0x1b, 0xcd, 0xe7, 0x0a, 0x0f, 0x58, 0x53,
0x36, 0xab, 0x12, 0xe1, 0x5c, 0x97, 0xc1, 0xfb,
},
},
{
.data_len = 129,
.digest = {
0x2b, 0x52, 0x1e, 0x54, 0xbe, 0x38, 0x4c, 0x3e,
0x73, 0x37, 0x18, 0xf5, 0x25, 0x2c, 0xc8, 0xc7,
0xda, 0x7e, 0xb6, 0x47, 0x9d, 0xf4, 0x46, 0xce,
0xfa, 0x80, 0x20, 0x6b, 0xbd, 0xfd, 0x2a, 0xd8,
},
},
{
.data_len = 256,
.digest = {
0x45, 0xf0, 0xf5, 0x9b, 0xd9, 0x91, 0x26, 0xd5,
0x91, 0x3b, 0xf8, 0x87, 0x8b, 0x34, 0x02, 0x31,
0x64, 0xab, 0xf4, 0x1c, 0x6e, 0x34, 0x72, 0xdf,
0x32, 0x6d, 0xe5, 0xd2, 0x67, 0x5e, 0x86, 0x93,
},
},
{
.data_len = 511,
.digest = {
0xb3, 0xaf, 0x71, 0x64, 0xfa, 0xd4, 0xf1, 0x07,
0x38, 0xef, 0x04, 0x8e, 0x89, 0xf4, 0x02, 0xd2,
0xa5, 0xaf, 0x3b, 0xf5, 0x67, 0x56, 0xcf, 0xa9,
0x8e, 0x43, 0xf5, 0xb5, 0xe3, 0x91, 0x8e, 0xe7,
},
},
{
.data_len = 513,
.digest = {
0x51, 0xac, 0x0a, 0x65, 0xb7, 0x96, 0x20, 0xcf,
0x88, 0xf6, 0x97, 0x35, 0x89, 0x0d, 0x31, 0x0f,
0xbe, 0x17, 0xbe, 0x62, 0x03, 0x67, 0xc0, 0xee,
0x4f, 0xc1, 0xe3, 0x7f, 0x6f, 0xab, 0xac, 0xb4,
},
},
{
.data_len = 1000,
.digest = {
0x7e, 0xea, 0xa8, 0xd7, 0xde, 0x20, 0x1b, 0x58,
0x24, 0xd8, 0x26, 0x40, 0x36, 0x5f, 0x3f, 0xaa,
0xe5, 0x5a, 0xea, 0x98, 0x58, 0xd4, 0xd6, 0xfc,
0x20, 0x4c, 0x5c, 0x4f, 0xaf, 0x56, 0xc7, 0xc3,
},
},
{
.data_len = 3333,
.digest = {
0x61, 0xb1, 0xb1, 0x3e, 0x0e, 0x7e, 0x90, 0x3d,
0x31, 0x54, 0xbd, 0xc9, 0x0d, 0x53, 0x62, 0xf1,
0xcd, 0x18, 0x80, 0xf9, 0x91, 0x75, 0x41, 0xb3,
0x51, 0x39, 0x57, 0xa7, 0xa8, 0x1e, 0xfb, 0xc9,
},
},
{
.data_len = 4096,
.digest = {
0xab, 0x29, 0xda, 0x10, 0xc4, 0x11, 0x2d, 0x5c,
0xd1, 0xce, 0x1c, 0x95, 0xfa, 0xc6, 0xc7, 0xb0,
0x1b, 0xd1, 0xdc, 0x6f, 0xa0, 0x9d, 0x1b, 0x23,
0xfb, 0x6e, 0x90, 0x97, 0xd0, 0x75, 0x44, 0x7a,
},
},
{
.data_len = 4128,
.digest = {
0x02, 0x45, 0x95, 0xf4, 0x19, 0xb5, 0x93, 0x29,
0x90, 0xf2, 0x63, 0x3f, 0x89, 0xe8, 0xa5, 0x31,
0x76, 0xf2, 0x89, 0x79, 0x66, 0xd3, 0x96, 0xdf,
0x33, 0xd1, 0xa6, 0x17, 0x73, 0xb1, 0xd0, 0x45,
},
},
{
.data_len = 4160,
.digest = {
0xd1, 0x8e, 0x22, 0xea, 0x44, 0x87, 0x6e, 0x9d,
0xfb, 0x36, 0x02, 0x20, 0x63, 0xb7, 0x69, 0x45,
0x25, 0x41, 0x69, 0xe0, 0x9b, 0x87, 0xcf, 0xa3,
0x51, 0xbb, 0xfc, 0x8d, 0xf7, 0x29, 0xa7, 0xea,
},
},
{
.data_len = 4224,
.digest = {
0x11, 0x86, 0x7d, 0x84, 0xf9, 0x8c, 0x6e, 0xc4,
0x64, 0x36, 0xc6, 0xf3, 0x42, 0x92, 0x31, 0x2b,
0x1e, 0x12, 0xe6, 0x4d, 0xbe, 0xfa, 0x77, 0x3f,
0x89, 0x41, 0x33, 0x58, 0x1c, 0x98, 0x16, 0x0a,
},
},
{
.data_len = 16384,
.digest = {
0xb2, 0xba, 0x0c, 0x8c, 0x9d, 0xbb, 0x1e, 0xb0,
0x03, 0xb5, 0xdf, 0x4f, 0xf5, 0x35, 0xdb, 0xec,
0x60, 0xf2, 0x5b, 0xb6, 0xd0, 0x49, 0xd3, 0xed,
0x55, 0xc0, 0x7a, 0xd7, 0xaf, 0xa1, 0xea, 0x53,
},
},
};
static const u8 hash_testvec_consolidated[SHA3_256_DIGEST_SIZE] = {
0x3b, 0x33, 0x67, 0xf8, 0xea, 0x92, 0x78, 0x62,
0xdd, 0xbe, 0x72, 0x15, 0xbd, 0x6f, 0xfa, 0xe5,
0x5e, 0xab, 0x9f, 0xb1, 0xe4, 0x23, 0x7c, 0x2c,
0x80, 0xcf, 0x09, 0x75, 0xf8, 0xe2, 0xfa, 0x30,
};
/* SHAKE test vectors */
static const u8 shake128_testvec_consolidated[SHA3_256_DIGEST_SIZE] = {
0x89, 0x88, 0x3a, 0x44, 0xec, 0xfe, 0x3c, 0xeb,
0x2f, 0x1c, 0x1d, 0xda, 0x9e, 0x36, 0x64, 0xf0,
0x85, 0x4c, 0x49, 0x12, 0x76, 0x5a, 0x4d, 0xe7,
0xa8, 0xfd, 0xcd, 0xbe, 0x45, 0xb4, 0x6f, 0xb0,
};
static const u8 shake256_testvec_consolidated[SHA3_256_DIGEST_SIZE] = {
0x5a, 0xfd, 0x66, 0x62, 0x5c, 0x37, 0x2b, 0x41,
0x77, 0x1c, 0x01, 0x5d, 0x64, 0x7c, 0x63, 0x7a,
0x7c, 0x76, 0x9e, 0xa8, 0xd1, 0xb0, 0x8e, 0x02,
0x16, 0x9b, 0xfe, 0x0e, 0xb5, 0xd8, 0x6a, 0xb5,
};
@@ -0,0 +1,290 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */
/* This file was generated by: ./scripts/crypto/gen-hash-testvecs.py sha384 */
static const struct {
size_t data_len;
u8 digest[SHA384_DIGEST_SIZE];
} hash_testvecs[] = {
{
.data_len = 0,
.digest = {
0x38, 0xb0, 0x60, 0xa7, 0x51, 0xac, 0x96, 0x38,
0x4c, 0xd9, 0x32, 0x7e, 0xb1, 0xb1, 0xe3, 0x6a,
0x21, 0xfd, 0xb7, 0x11, 0x14, 0xbe, 0x07, 0x43,
0x4c, 0x0c, 0xc7, 0xbf, 0x63, 0xf6, 0xe1, 0xda,
0x27, 0x4e, 0xde, 0xbf, 0xe7, 0x6f, 0x65, 0xfb,
0xd5, 0x1a, 0xd2, 0xf1, 0x48, 0x98, 0xb9, 0x5b,
},
},
{
.data_len = 1,
.digest = {
0x07, 0x34, 0x9d, 0x74, 0x48, 0x76, 0xa5, 0x72,
0x78, 0x02, 0xb8, 0x6e, 0x21, 0x59, 0xb0, 0x75,
0x09, 0x68, 0x11, 0x39, 0x53, 0x61, 0xee, 0x8d,
0xf2, 0x01, 0xf3, 0x90, 0x53, 0x7c, 0xd3, 0xde,
0x13, 0x9f, 0xd2, 0x74, 0x28, 0xfe, 0xe1, 0xc8,
0x2e, 0x95, 0xc6, 0x7d, 0x69, 0x4d, 0x04, 0xc6,
},
},
{
.data_len = 2,
.digest = {
0xc4, 0xef, 0x6e, 0x8c, 0x19, 0x1c, 0xaa, 0x0e,
0x86, 0xf2, 0x68, 0xa1, 0xa0, 0x2d, 0x2e, 0xb2,
0x84, 0xbc, 0x5d, 0x53, 0x31, 0xf8, 0x03, 0x75,
0x56, 0xf4, 0x8b, 0x23, 0x1a, 0x68, 0x15, 0x9a,
0x60, 0xb2, 0xec, 0x05, 0xe1, 0xd4, 0x5e, 0x9e,
0xe8, 0x7c, 0x9d, 0xe4, 0x0f, 0x9c, 0x3a, 0xdd,
},
},
{
.data_len = 3,
.digest = {
0x29, 0xd2, 0x02, 0xa2, 0x77, 0x24, 0xc7, 0xa7,
0x23, 0x0c, 0x3e, 0x30, 0x56, 0x47, 0xdb, 0x75,
0xd4, 0x41, 0xf8, 0xb3, 0x8e, 0x26, 0xf6, 0x92,
0xbc, 0x20, 0x2e, 0x96, 0xcc, 0x81, 0x5f, 0x32,
0x82, 0x60, 0xe2, 0xcf, 0x23, 0xd7, 0x3c, 0x90,
0xb2, 0x56, 0x8f, 0xb6, 0x0f, 0xf0, 0x6b, 0x80,
},
},
{
.data_len = 16,
.digest = {
0x21, 0x4c, 0xac, 0xfe, 0xbd, 0x40, 0x74, 0x1f,
0xa2, 0x2d, 0x2f, 0x35, 0x91, 0xfd, 0xc9, 0x97,
0x88, 0x12, 0x6c, 0x0c, 0x6e, 0xd8, 0x50, 0x0b,
0x4b, 0x2c, 0x89, 0xa6, 0xa6, 0x4a, 0xad, 0xd7,
0x72, 0x62, 0x2c, 0x62, 0x81, 0xcd, 0x24, 0x74,
0xf5, 0x44, 0x05, 0xa0, 0x97, 0xea, 0xf1, 0x78,
},
},
{
.data_len = 32,
.digest = {
0x06, 0x8b, 0x92, 0x9f, 0x8b, 0x64, 0xb2, 0x80,
0xde, 0xcc, 0xde, 0xc3, 0x2f, 0x22, 0x27, 0xe8,
0x3b, 0x6e, 0x16, 0x21, 0x14, 0x81, 0xbe, 0x5b,
0xa7, 0xa7, 0x14, 0x8a, 0x00, 0x8f, 0x0d, 0x38,
0x11, 0x63, 0xe8, 0x3e, 0xb9, 0xf1, 0xcf, 0x87,
0xb1, 0x28, 0xe5, 0xa1, 0x89, 0xa8, 0x7a, 0xde,
},
},
{
.data_len = 48,
.digest = {
0x9e, 0x37, 0x76, 0x62, 0x98, 0x39, 0xbe, 0xfd,
0x2b, 0x91, 0x20, 0x54, 0x8f, 0x21, 0xe7, 0x30,
0x0a, 0x01, 0x7a, 0x65, 0x0b, 0xc9, 0xb3, 0x89,
0x3c, 0xb6, 0xd3, 0xa8, 0xff, 0xc9, 0x1b, 0x5c,
0xd4, 0xac, 0xb4, 0x7e, 0xba, 0x94, 0xc3, 0x8a,
0x26, 0x41, 0xf6, 0xd5, 0xed, 0x6f, 0x27, 0xa7,
},
},
{
.data_len = 49,
.digest = {
0x03, 0x1f, 0xef, 0x5a, 0x16, 0x28, 0x78, 0x10,
0x29, 0xe8, 0xe2, 0xe4, 0x84, 0x36, 0x19, 0x10,
0xaa, 0xea, 0xde, 0x06, 0x39, 0x5f, 0xb2, 0x36,
0xca, 0x24, 0x4f, 0x7b, 0x66, 0xf7, 0xe7, 0x31,
0xf3, 0x9b, 0x74, 0x1e, 0x17, 0x20, 0x88, 0x62,
0x50, 0xeb, 0x5f, 0x9a, 0xa7, 0x2c, 0xf4, 0xc9,
},
},
{
.data_len = 63,
.digest = {
0x10, 0xce, 0xed, 0x26, 0xb8, 0xac, 0xc1, 0x1b,
0xe6, 0xb9, 0xeb, 0x7c, 0xae, 0xcd, 0x55, 0x5a,
0x20, 0x2a, 0x7b, 0x43, 0xe6, 0x3e, 0xf0, 0x3f,
0xd9, 0x2f, 0x8c, 0x52, 0xe2, 0xf0, 0xb6, 0x24,
0x2e, 0xa4, 0xac, 0x24, 0x3a, 0x54, 0x99, 0x71,
0x65, 0xab, 0x97, 0x2d, 0xb6, 0xe6, 0x94, 0x20,
},
},
{
.data_len = 64,
.digest = {
0x24, 0x6d, 0x9f, 0x59, 0x42, 0x36, 0xca, 0x34,
0x36, 0x41, 0xa2, 0xcd, 0x69, 0xdf, 0x3d, 0xcb,
0x64, 0x94, 0x54, 0xb2, 0xed, 0xc1, 0x1c, 0x31,
0xe3, 0x26, 0xcb, 0x71, 0xe6, 0x98, 0xb2, 0x56,
0x74, 0x30, 0xa9, 0x15, 0x98, 0x9d, 0xb3, 0x07,
0xcc, 0xa8, 0xcc, 0x6f, 0x42, 0xb0, 0x9d, 0x2b,
},
},
{
.data_len = 65,
.digest = {
0x85, 0x1f, 0xbc, 0x5e, 0x2a, 0x00, 0x7d, 0xc2,
0x21, 0x4c, 0x28, 0x14, 0xc5, 0xd8, 0x0c, 0xe8,
0x55, 0xa5, 0xa0, 0x77, 0xda, 0x8f, 0xce, 0xd4,
0xf0, 0xcb, 0x30, 0xb8, 0x9c, 0x47, 0xe1, 0x33,
0x92, 0x18, 0xc5, 0x1f, 0xf2, 0xef, 0xb5, 0xe5,
0xbc, 0x63, 0xa6, 0xe5, 0x9a, 0xc9, 0xcc, 0xf1,
},
},
{
.data_len = 127,
.digest = {
0x26, 0xd2, 0x4c, 0xb6, 0xce, 0xd8, 0x22, 0x2b,
0x44, 0x10, 0x6f, 0x59, 0xf7, 0x0d, 0xb9, 0x3f,
0x7d, 0x29, 0x75, 0xf1, 0x71, 0xb2, 0x71, 0x23,
0xef, 0x68, 0xb7, 0x25, 0xae, 0xb8, 0x45, 0xf8,
0xa3, 0xb2, 0x2d, 0x7a, 0x83, 0x0a, 0x05, 0x61,
0xbc, 0x73, 0xf1, 0xf9, 0xba, 0xfb, 0x3d, 0xc2,
},
},
{
.data_len = 128,
.digest = {
0x7c, 0xe5, 0x7f, 0x5e, 0xea, 0xd9, 0x7e, 0x54,
0x14, 0x30, 0x6f, 0x37, 0x02, 0x71, 0x0f, 0xf1,
0x14, 0x16, 0xfa, 0xeb, 0x6e, 0x1e, 0xf0, 0xbe,
0x10, 0xed, 0x01, 0xbf, 0xa0, 0x9d, 0xcb, 0x07,
0x5f, 0x8b, 0x7f, 0x44, 0xe1, 0xd9, 0x13, 0xf0,
0x29, 0xa2, 0x54, 0x32, 0xd9, 0xb0, 0x69, 0x69,
},
},
{
.data_len = 129,
.digest = {
0xc5, 0x54, 0x1f, 0xcb, 0x9d, 0x8f, 0xdf, 0xbf,
0xab, 0x55, 0x92, 0x1d, 0x3b, 0x93, 0x79, 0x26,
0xdf, 0xba, 0x9a, 0x28, 0xff, 0xa0, 0x6c, 0xae,
0x7b, 0x53, 0x8d, 0xfa, 0xef, 0x35, 0x88, 0x19,
0x16, 0xb8, 0x72, 0x86, 0x76, 0x2a, 0xf5, 0xe6,
0xec, 0xb2, 0xd7, 0xd4, 0xbe, 0x1a, 0xe4, 0x9f,
},
},
{
.data_len = 256,
.digest = {
0x74, 0x9d, 0x77, 0xfb, 0xe8, 0x0f, 0x0c, 0x2d,
0x86, 0x0d, 0x49, 0xea, 0x2b, 0xd0, 0x13, 0xd1,
0xe8, 0xb8, 0xe1, 0xa3, 0x7b, 0x48, 0xab, 0x6a,
0x21, 0x2b, 0x4c, 0x48, 0x32, 0xb5, 0xdc, 0x31,
0x7f, 0xd0, 0x32, 0x67, 0x9a, 0xc0, 0x85, 0x53,
0xef, 0xe9, 0xfb, 0xe1, 0x8b, 0xd8, 0xcc, 0xc2,
},
},
{
.data_len = 511,
.digest = {
0x7b, 0xa9, 0xde, 0xa3, 0x07, 0x5c, 0x4c, 0xaa,
0x31, 0xc6, 0x9e, 0x55, 0xd4, 0x3f, 0x52, 0xdd,
0xde, 0x36, 0x70, 0x96, 0x59, 0x6e, 0x90, 0x78,
0x4c, 0x6a, 0x27, 0xde, 0x83, 0x84, 0xc3, 0x35,
0x53, 0x76, 0x1d, 0xbf, 0x83, 0x64, 0xcf, 0xf2,
0xb0, 0x3e, 0x07, 0x27, 0xe4, 0x25, 0x6c, 0x56,
},
},
{
.data_len = 513,
.digest = {
0x53, 0x50, 0xf7, 0x3b, 0x86, 0x1d, 0x7a, 0xe2,
0x5d, 0x9b, 0x71, 0xfa, 0x25, 0x23, 0x5a, 0xfe,
0x8c, 0xb9, 0xac, 0x8a, 0x9d, 0x6c, 0x99, 0xbc,
0x01, 0x9e, 0xa0, 0xd6, 0x3c, 0x03, 0x46, 0x21,
0xb6, 0xd0, 0xb0, 0xb3, 0x23, 0x23, 0x58, 0xf1,
0xea, 0x4e, 0xf2, 0x1a, 0x2f, 0x14, 0x2b, 0x5a,
},
},
{
.data_len = 1000,
.digest = {
0x06, 0x03, 0xb3, 0xba, 0x14, 0xe0, 0x28, 0x07,
0xd5, 0x15, 0x97, 0x1f, 0x87, 0xef, 0x80, 0xba,
0x48, 0x03, 0xb6, 0xc5, 0x47, 0xca, 0x8c, 0x95,
0xed, 0x95, 0xfd, 0x27, 0xb6, 0x83, 0xda, 0x6d,
0xa7, 0xb2, 0x1a, 0xd2, 0xb5, 0x89, 0xbb, 0xb4,
0x00, 0xbc, 0x86, 0x54, 0x7d, 0x5a, 0x91, 0x63,
},
},
{
.data_len = 3333,
.digest = {
0xd3, 0xe0, 0x6e, 0x7d, 0x80, 0x08, 0x53, 0x07,
0x8c, 0x0f, 0xc2, 0xce, 0x9f, 0x09, 0x86, 0x31,
0x28, 0x24, 0x3c, 0x3e, 0x2d, 0x36, 0xb4, 0x28,
0xc7, 0x1b, 0x70, 0xf9, 0x35, 0x9b, 0x10, 0xfa,
0xc8, 0x5e, 0x2b, 0x32, 0x7f, 0x65, 0xd2, 0x68,
0xb2, 0x84, 0x90, 0xf6, 0xc8, 0x6e, 0xb8, 0xdb,
},
},
{
.data_len = 4096,
.digest = {
0x39, 0xeb, 0xc4, 0xb3, 0x08, 0xe2, 0xdd, 0xf3,
0x9f, 0x5e, 0x44, 0x93, 0x63, 0x8b, 0x39, 0x57,
0xd7, 0xe8, 0x7e, 0x3d, 0x74, 0xf8, 0xf6, 0xab,
0xfe, 0x74, 0x51, 0xe4, 0x1b, 0x4a, 0x23, 0xbc,
0x69, 0xfc, 0xbb, 0xa7, 0x71, 0xa7, 0x86, 0x24,
0xcc, 0x85, 0x70, 0xf2, 0x31, 0x0d, 0x47, 0xc0,
},
},
{
.data_len = 4128,
.digest = {
0x23, 0xc3, 0x97, 0x06, 0x79, 0xbe, 0x8a, 0xe9,
0x1f, 0x1a, 0x43, 0xad, 0xe6, 0x76, 0x23, 0x13,
0x64, 0xae, 0xda, 0xe7, 0x8b, 0x88, 0x96, 0xb6,
0xa9, 0x1a, 0xb7, 0x80, 0x8e, 0x1c, 0x94, 0x98,
0x09, 0x08, 0xdb, 0x8e, 0x4d, 0x0a, 0x09, 0x65,
0xe5, 0x21, 0x1c, 0xd9, 0xab, 0x64, 0xbb, 0xea,
},
},
{
.data_len = 4160,
.digest = {
0x4f, 0x4a, 0x88, 0x9f, 0x40, 0x89, 0xfe, 0xb6,
0xda, 0x9d, 0xcd, 0xa5, 0x27, 0xd2, 0x29, 0x71,
0x58, 0x60, 0xd4, 0x55, 0xfe, 0x92, 0xcd, 0x51,
0x8b, 0xec, 0x3b, 0xd3, 0xd1, 0x3e, 0x8d, 0x36,
0x7b, 0xb1, 0x41, 0xef, 0xec, 0x9d, 0xdf, 0xcd,
0x4e, 0xde, 0x5a, 0xe5, 0xe5, 0x16, 0x14, 0x54,
},
},
{
.data_len = 4224,
.digest = {
0xb5, 0xa5, 0x3e, 0x86, 0x39, 0x20, 0x49, 0x4c,
0xcd, 0xb6, 0xdd, 0x03, 0xfe, 0x36, 0x6e, 0xa6,
0xfc, 0xff, 0x19, 0x33, 0x0c, 0x52, 0xea, 0x37,
0x94, 0xda, 0x5b, 0x27, 0xd1, 0x99, 0x5a, 0x89,
0x40, 0x78, 0xfa, 0x96, 0xb9, 0x2f, 0xa0, 0x48,
0xc9, 0xf8, 0x5c, 0xf0, 0x95, 0xf4, 0xea, 0x61,
},
},
{
.data_len = 16384,
.digest = {
0x6f, 0x48, 0x6f, 0x21, 0xb9, 0xc1, 0xcc, 0x92,
0x4e, 0xed, 0x6b, 0xef, 0x51, 0x88, 0xdf, 0xfd,
0xcb, 0x3d, 0x44, 0x9c, 0x37, 0x85, 0xb4, 0xc5,
0xeb, 0x60, 0x55, 0x58, 0x01, 0x47, 0xbf, 0x75,
0x9b, 0xa8, 0x82, 0x8c, 0xec, 0xe8, 0x0e, 0x58,
0xc1, 0x26, 0xa2, 0x45, 0x87, 0x3e, 0xfb, 0x8d,
},
},
};
static const u8 hash_testvec_consolidated[SHA384_DIGEST_SIZE] = {
0xfc, 0xcb, 0xe6, 0x42, 0xf0, 0x9e, 0x2b, 0x77,
0x7b, 0x62, 0xe8, 0x70, 0x86, 0xbf, 0xaf, 0x3c,
0xbb, 0x02, 0xd9, 0x86, 0xdc, 0xba, 0xd3, 0xa4,
0x0d, 0x8d, 0xb3, 0x2d, 0x0b, 0xa3, 0x84, 0x04,
0x7c, 0x16, 0x37, 0xaf, 0xba, 0x1e, 0xd4, 0x2f,
0x4c, 0x57, 0x55, 0x86, 0x52, 0x47, 0x9a, 0xec,
};
static const u8 hmac_testvec_consolidated[SHA384_DIGEST_SIZE] = {
0x82, 0xcf, 0x7d, 0x80, 0x71, 0xdb, 0x91, 0x09,
0x67, 0xe8, 0x44, 0x4a, 0x0d, 0x03, 0xb1, 0xf9,
0x62, 0xde, 0x4e, 0xbb, 0x1f, 0x41, 0xcd, 0x62,
0x39, 0x6b, 0x2d, 0x44, 0x0e, 0xde, 0x98, 0x73,
0xdd, 0xeb, 0x9d, 0x53, 0xfb, 0xee, 0xd1, 0xc3,
0x96, 0xdb, 0xfc, 0x2a, 0x38, 0x90, 0x02, 0x53,
};
@@ -0,0 +1,39 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright 2025 Google LLC
*/
#include <crypto/sha2.h>
#include "sha384-testvecs.h"
#define HASH sha384
#define HASH_CTX sha384_ctx
#define HASH_SIZE SHA384_DIGEST_SIZE
#define HASH_INIT sha384_init
#define HASH_UPDATE sha384_update
#define HASH_FINAL sha384_final
#define HMAC_KEY hmac_sha384_key
#define HMAC_CTX hmac_sha384_ctx
#define HMAC_PREPAREKEY hmac_sha384_preparekey
#define HMAC_INIT hmac_sha384_init
#define HMAC_UPDATE hmac_sha384_update
#define HMAC_FINAL hmac_sha384_final
#define HMAC hmac_sha384
#define HMAC_USINGRAWKEY hmac_sha384_usingrawkey
#include "hash-test-template.h"
static struct kunit_case hash_test_cases[] = {
HASH_KUNIT_CASES,
KUNIT_CASE(benchmark_hash),
{},
};
static struct kunit_suite hash_test_suite = {
.name = "sha384",
.test_cases = hash_test_cases,
.suite_init = hash_suite_init,
.suite_exit = hash_suite_exit,
};
kunit_test_suite(hash_test_suite);
MODULE_DESCRIPTION("KUnit tests and benchmark for SHA-384 and HMAC-SHA384");
MODULE_LICENSE("GPL");
+422
View File
@@ -0,0 +1,422 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2025 Red Hat, Inc. All Rights Reserved.
* Written by David Howells (dhowells@redhat.com)
*/
#include <crypto/sha3.h>
#include "sha3-testvecs.h"
#define HASH sha3_256
#define HASH_CTX sha3_ctx
#define HASH_SIZE SHA3_256_DIGEST_SIZE
#define HASH_INIT sha3_256_init
#define HASH_UPDATE sha3_update
#define HASH_FINAL sha3_final
#include "hash-test-template.h"
/*
* Sample message and the output generated for various algorithms by passing it
* into "openssl sha3-224" etc..
*/
static const u8 test_sha3_sample[] =
"The quick red fox jumped over the lazy brown dog!\n"
"The quick red fox jumped over the lazy brown dog!\n"
"The quick red fox jumped over the lazy brown dog!\n"
"The quick red fox jumped over the lazy brown dog!\n";
static const u8 test_sha3_224[8 + SHA3_224_DIGEST_SIZE + 8] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* Write-before guard */
0xd6, 0xe8, 0xd8, 0x80, 0xfa, 0x42, 0x80, 0x70,
0x7e, 0x7f, 0xd7, 0xd2, 0xd7, 0x7a, 0x35, 0x65,
0xf0, 0x0b, 0x4f, 0x9f, 0x2a, 0x33, 0xca, 0x0a,
0xef, 0xa6, 0x4c, 0xb8,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* Write-after guard */
};
static const u8 test_sha3_256[8 + SHA3_256_DIGEST_SIZE + 8] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* Write-before guard */
0xdb, 0x3b, 0xb0, 0xb8, 0x8d, 0x15, 0x78, 0xe5,
0x78, 0x76, 0x8e, 0x39, 0x7e, 0x89, 0x86, 0xb9,
0x14, 0x3a, 0x1e, 0xe7, 0x96, 0x7c, 0xf3, 0x25,
0x70, 0xbd, 0xc3, 0xa9, 0xae, 0x63, 0x71, 0x1d,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* Write-after guard */
};
static const u8 test_sha3_384[8 + SHA3_384_DIGEST_SIZE + 8] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* Write-before guard */
0x2d, 0x4b, 0x29, 0x85, 0x19, 0x94, 0xaa, 0x31,
0x9b, 0x04, 0x9d, 0x6e, 0x79, 0x66, 0xc7, 0x56,
0x8a, 0x2e, 0x99, 0x84, 0x06, 0xcf, 0x10, 0x2d,
0xec, 0xf0, 0x03, 0x04, 0x1f, 0xd5, 0x99, 0x63,
0x2f, 0xc3, 0x2b, 0x0d, 0xd9, 0x45, 0xf7, 0xbb,
0x0a, 0xc3, 0x46, 0xab, 0xfe, 0x4d, 0x94, 0xc2,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* Write-after guard */
};
static const u8 test_sha3_512[8 + SHA3_512_DIGEST_SIZE + 8] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* Write-before guard */
0xdd, 0x71, 0x3b, 0x44, 0xb6, 0x6c, 0xd7, 0x78,
0xe7, 0x93, 0xa1, 0x4c, 0xd7, 0x24, 0x16, 0xf1,
0xfd, 0xa2, 0x82, 0x4e, 0xed, 0x59, 0xe9, 0x83,
0x15, 0x38, 0x89, 0x7d, 0x39, 0x17, 0x0c, 0xb2,
0xcf, 0x12, 0x80, 0x78, 0xa1, 0x78, 0x41, 0xeb,
0xed, 0x21, 0x4c, 0xa4, 0x4a, 0x5f, 0x30, 0x1a,
0x70, 0x98, 0x4f, 0x14, 0xa2, 0xd1, 0x64, 0x1b,
0xc2, 0x0a, 0xff, 0x3b, 0xe8, 0x26, 0x41, 0x8f,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* Write-after guard */
};
static const u8 test_shake128[8 + SHAKE128_DEFAULT_SIZE + 8] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* Write-before guard */
0x41, 0xd6, 0xb8, 0x9c, 0xf8, 0xe8, 0x54, 0xf2,
0x5c, 0xde, 0x51, 0x12, 0xaf, 0x9e, 0x0d, 0x91,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* Write-after guard */
};
static const u8 test_shake256[8 + SHAKE256_DEFAULT_SIZE + 8] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* Write-before guard */
0xab, 0x06, 0xd4, 0xf9, 0x8b, 0xfd, 0xb2, 0xc4,
0xfe, 0xf1, 0xcc, 0xe2, 0x40, 0x45, 0xdd, 0x15,
0xcb, 0xdd, 0x02, 0x8d, 0xb7, 0x9f, 0x1e, 0x67,
0xd6, 0x7f, 0x98, 0x5e, 0x1b, 0x19, 0xf8, 0x01,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* Write-after guard */
};
static void test_sha3_224_basic(struct kunit *test)
{
u8 out[8 + SHA3_224_DIGEST_SIZE + 8];
BUILD_BUG_ON(sizeof(out) != sizeof(test_sha3_224));
memset(out, 0, sizeof(out));
sha3_224(test_sha3_sample, sizeof(test_sha3_sample) - 1, out + 8);
KUNIT_ASSERT_MEMEQ_MSG(test, out, test_sha3_224, sizeof(test_sha3_224),
"SHA3-224 gives wrong output");
}
static void test_sha3_256_basic(struct kunit *test)
{
u8 out[8 + SHA3_256_DIGEST_SIZE + 8];
BUILD_BUG_ON(sizeof(out) != sizeof(test_sha3_256));
memset(out, 0, sizeof(out));
sha3_256(test_sha3_sample, sizeof(test_sha3_sample) - 1, out + 8);
KUNIT_ASSERT_MEMEQ_MSG(test, out, test_sha3_256, sizeof(test_sha3_256),
"SHA3-256 gives wrong output");
}
static void test_sha3_384_basic(struct kunit *test)
{
u8 out[8 + SHA3_384_DIGEST_SIZE + 8];
BUILD_BUG_ON(sizeof(out) != sizeof(test_sha3_384));
memset(out, 0, sizeof(out));
sha3_384(test_sha3_sample, sizeof(test_sha3_sample) - 1, out + 8);
KUNIT_ASSERT_MEMEQ_MSG(test, out, test_sha3_384, sizeof(test_sha3_384),
"SHA3-384 gives wrong output");
}
static void test_sha3_512_basic(struct kunit *test)
{
u8 out[8 + SHA3_512_DIGEST_SIZE + 8];
BUILD_BUG_ON(sizeof(out) != sizeof(test_sha3_512));
memset(out, 0, sizeof(out));
sha3_512(test_sha3_sample, sizeof(test_sha3_sample) - 1, out + 8);
KUNIT_ASSERT_MEMEQ_MSG(test, out, test_sha3_512, sizeof(test_sha3_512),
"SHA3-512 gives wrong output");
}
static void test_shake128_basic(struct kunit *test)
{
u8 out[8 + SHAKE128_DEFAULT_SIZE + 8];
BUILD_BUG_ON(sizeof(out) != sizeof(test_shake128));
memset(out, 0, sizeof(out));
shake128(test_sha3_sample, sizeof(test_sha3_sample) - 1,
out + 8, sizeof(out) - 16);
KUNIT_ASSERT_MEMEQ_MSG(test, out, test_shake128, sizeof(test_shake128),
"SHAKE128 gives wrong output");
}
static void test_shake256_basic(struct kunit *test)
{
u8 out[8 + SHAKE256_DEFAULT_SIZE + 8];
BUILD_BUG_ON(sizeof(out) != sizeof(test_shake256));
memset(out, 0, sizeof(out));
shake256(test_sha3_sample, sizeof(test_sha3_sample) - 1,
out + 8, sizeof(out) - 16);
KUNIT_ASSERT_MEMEQ_MSG(test, out, test_shake256, sizeof(test_shake256),
"SHAKE256 gives wrong output");
}
/*
* Usable NIST tests.
*
* From: https://csrc.nist.gov/projects/cryptographic-standards-and-guidelines/example-values
*/
static const u8 test_nist_1600_sample[] = {
0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3,
0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3,
0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3,
0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3,
0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3,
0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3,
0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3,
0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3,
0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3,
0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3,
0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3,
0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3,
0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3,
0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3,
0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3,
0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3,
0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3,
0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3,
0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3,
0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3,
0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3,
0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3,
0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3,
0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3,
0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3
};
static const u8 test_shake128_nist_0[] = {
0x7f, 0x9c, 0x2b, 0xa4, 0xe8, 0x8f, 0x82, 0x7d,
0x61, 0x60, 0x45, 0x50, 0x76, 0x05, 0x85, 0x3e
};
static const u8 test_shake128_nist_1600[] = {
0x13, 0x1a, 0xb8, 0xd2, 0xb5, 0x94, 0x94, 0x6b,
0x9c, 0x81, 0x33, 0x3f, 0x9b, 0xb6, 0xe0, 0xce,
};
static const u8 test_shake256_nist_0[] = {
0x46, 0xb9, 0xdd, 0x2b, 0x0b, 0xa8, 0x8d, 0x13,
0x23, 0x3b, 0x3f, 0xeb, 0x74, 0x3e, 0xeb, 0x24,
0x3f, 0xcd, 0x52, 0xea, 0x62, 0xb8, 0x1b, 0x82,
0xb5, 0x0c, 0x27, 0x64, 0x6e, 0xd5, 0x76, 0x2f
};
static const u8 test_shake256_nist_1600[] = {
0xcd, 0x8a, 0x92, 0x0e, 0xd1, 0x41, 0xaa, 0x04,
0x07, 0xa2, 0x2d, 0x59, 0x28, 0x86, 0x52, 0xe9,
0xd9, 0xf1, 0xa7, 0xee, 0x0c, 0x1e, 0x7c, 0x1c,
0xa6, 0x99, 0x42, 0x4d, 0xa8, 0x4a, 0x90, 0x4d,
};
static void test_shake128_nist(struct kunit *test)
{
u8 out[SHAKE128_DEFAULT_SIZE];
shake128("", 0, out, sizeof(out));
KUNIT_ASSERT_MEMEQ_MSG(test, out, test_shake128_nist_0, sizeof(out),
"SHAKE128 gives wrong output for NIST.0");
shake128(test_nist_1600_sample, sizeof(test_nist_1600_sample),
out, sizeof(out));
KUNIT_ASSERT_MEMEQ_MSG(test, out, test_shake128_nist_1600, sizeof(out),
"SHAKE128 gives wrong output for NIST.1600");
}
static void test_shake256_nist(struct kunit *test)
{
u8 out[SHAKE256_DEFAULT_SIZE];
shake256("", 0, out, sizeof(out));
KUNIT_ASSERT_MEMEQ_MSG(test, out, test_shake256_nist_0, sizeof(out),
"SHAKE256 gives wrong output for NIST.0");
shake256(test_nist_1600_sample, sizeof(test_nist_1600_sample),
out, sizeof(out));
KUNIT_ASSERT_MEMEQ_MSG(test, out, test_shake256_nist_1600, sizeof(out),
"SHAKE256 gives wrong output for NIST.1600");
}
static void shake(int alg, const u8 *in, size_t in_len, u8 *out, size_t out_len)
{
if (alg == 0)
shake128(in, in_len, out, out_len);
else
shake256(in, in_len, out, out_len);
}
static void shake_init(struct shake_ctx *ctx, int alg)
{
if (alg == 0)
shake128_init(ctx);
else
shake256_init(ctx);
}
/*
* Test each of SHAKE128 and SHAKE256 with all input lengths 0 through 4096, for
* both input and output. The input and output lengths cycle through the values
* together, so we do 4096 tests total. To verify all the SHAKE outputs,
* compute and verify the SHA3-256 digest of all of them concatenated together.
*/
static void test_shake_all_lens_up_to_4096(struct kunit *test)
{
struct sha3_ctx main_ctx;
const size_t max_len = 4096;
u8 *const in = test_buf;
u8 *const out = &test_buf[TEST_BUF_LEN - max_len];
u8 main_hash[SHA3_256_DIGEST_SIZE];
KUNIT_ASSERT_LE(test, 2 * max_len, TEST_BUF_LEN);
rand_bytes_seeded_from_len(in, max_len);
for (int alg = 0; alg < 2; alg++) {
sha3_256_init(&main_ctx);
for (size_t in_len = 0; in_len <= max_len; in_len++) {
size_t out_len = (in_len * 293) % (max_len + 1);
shake(alg, in, in_len, out, out_len);
sha3_update(&main_ctx, out, out_len);
}
sha3_final(&main_ctx, main_hash);
if (alg == 0)
KUNIT_ASSERT_MEMEQ_MSG(test, main_hash,
shake128_testvec_consolidated,
sizeof(main_hash),
"shake128() gives wrong output");
else
KUNIT_ASSERT_MEMEQ_MSG(test, main_hash,
shake256_testvec_consolidated,
sizeof(main_hash),
"shake256() gives wrong output");
}
}
/*
* Test that a sequence of SHAKE squeezes gives the same output as a single
* squeeze of the same total length.
*/
static void test_shake_multiple_squeezes(struct kunit *test)
{
const size_t max_len = 512;
u8 *ref_out;
KUNIT_ASSERT_GE(test, TEST_BUF_LEN, 2 * max_len);
ref_out = kunit_kzalloc(test, max_len, GFP_KERNEL);
KUNIT_ASSERT_NOT_NULL(test, ref_out);
for (int i = 0; i < 2000; i++) {
const int alg = rand32() % 2;
const size_t in_len = rand_length(max_len);
const size_t out_len = rand_length(max_len);
const size_t in_offs = rand_offset(max_len - in_len);
const size_t out_offs = rand_offset(max_len - out_len);
u8 *const in = &test_buf[in_offs];
u8 *const out = &test_buf[out_offs];
struct shake_ctx ctx;
size_t remaining_len, j, num_parts;
rand_bytes(in, in_len);
rand_bytes(out, out_len);
/* Compute the output using the one-shot function. */
shake(alg, in, in_len, ref_out, out_len);
/* Compute the output using a random sequence of squeezes. */
shake_init(&ctx, alg);
shake_update(&ctx, in, in_len);
remaining_len = out_len;
j = 0;
num_parts = 0;
while (rand_bool()) {
size_t part_len = rand_length(remaining_len);
shake_squeeze(&ctx, &out[j], part_len);
num_parts++;
j += part_len;
remaining_len -= part_len;
}
if (remaining_len != 0 || rand_bool()) {
shake_squeeze(&ctx, &out[j], remaining_len);
num_parts++;
}
/* Verify that the outputs are the same. */
KUNIT_ASSERT_MEMEQ_MSG(
test, out, ref_out, out_len,
"Multi-squeeze test failed with in_len=%zu in_offs=%zu out_len=%zu out_offs=%zu num_parts=%zu alg=%d",
in_len, in_offs, out_len, out_offs, num_parts, alg);
}
}
/*
* Test that SHAKE operations on buffers immediately followed by an unmapped
* page work as expected. This catches out-of-bounds memory accesses even if
* they occur in assembly code.
*/
static void test_shake_with_guarded_bufs(struct kunit *test)
{
const size_t max_len = 512;
u8 *reg_buf;
KUNIT_ASSERT_GE(test, TEST_BUF_LEN, max_len);
reg_buf = kunit_kzalloc(test, max_len, GFP_KERNEL);
KUNIT_ASSERT_NOT_NULL(test, reg_buf);
for (int alg = 0; alg < 2; alg++) {
for (size_t len = 0; len <= max_len; len++) {
u8 *guarded_buf = &test_buf[TEST_BUF_LEN - len];
rand_bytes(reg_buf, len);
memcpy(guarded_buf, reg_buf, len);
shake(alg, reg_buf, len, reg_buf, len);
shake(alg, guarded_buf, len, guarded_buf, len);
KUNIT_ASSERT_MEMEQ_MSG(
test, reg_buf, guarded_buf, len,
"Guard page test failed with len=%zu alg=%d",
len, alg);
}
}
}
static struct kunit_case sha3_test_cases[] = {
HASH_KUNIT_CASES,
KUNIT_CASE(test_sha3_224_basic),
KUNIT_CASE(test_sha3_256_basic),
KUNIT_CASE(test_sha3_384_basic),
KUNIT_CASE(test_sha3_512_basic),
KUNIT_CASE(test_shake128_basic),
KUNIT_CASE(test_shake256_basic),
KUNIT_CASE(test_shake128_nist),
KUNIT_CASE(test_shake256_nist),
KUNIT_CASE(test_shake_all_lens_up_to_4096),
KUNIT_CASE(test_shake_multiple_squeezes),
KUNIT_CASE(test_shake_with_guarded_bufs),
KUNIT_CASE(benchmark_hash),
{},
};
static struct kunit_suite sha3_test_suite = {
.name = "sha3",
.test_cases = sha3_test_cases,
.suite_init = hash_suite_init,
.suite_exit = hash_suite_exit,
};
kunit_test_suite(sha3_test_suite);
MODULE_DESCRIPTION("KUnit tests and benchmark for SHA3");
MODULE_LICENSE("GPL");
@@ -0,0 +1,342 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */
/* This file was generated by: ./scripts/crypto/gen-hash-testvecs.py sha512 */
static const struct {
size_t data_len;
u8 digest[SHA512_DIGEST_SIZE];
} hash_testvecs[] = {
{
.data_len = 0,
.digest = {
0xcf, 0x83, 0xe1, 0x35, 0x7e, 0xef, 0xb8, 0xbd,
0xf1, 0x54, 0x28, 0x50, 0xd6, 0x6d, 0x80, 0x07,
0xd6, 0x20, 0xe4, 0x05, 0x0b, 0x57, 0x15, 0xdc,
0x83, 0xf4, 0xa9, 0x21, 0xd3, 0x6c, 0xe9, 0xce,
0x47, 0xd0, 0xd1, 0x3c, 0x5d, 0x85, 0xf2, 0xb0,
0xff, 0x83, 0x18, 0xd2, 0x87, 0x7e, 0xec, 0x2f,
0x63, 0xb9, 0x31, 0xbd, 0x47, 0x41, 0x7a, 0x81,
0xa5, 0x38, 0x32, 0x7a, 0xf9, 0x27, 0xda, 0x3e,
},
},
{
.data_len = 1,
.digest = {
0x12, 0xf2, 0xb6, 0xec, 0x84, 0xa0, 0x8e, 0xcf,
0x0d, 0xec, 0x17, 0xfd, 0x1f, 0x91, 0x15, 0x69,
0xd2, 0xb9, 0x89, 0xff, 0x89, 0x9d, 0xd9, 0x0b,
0x7a, 0x0f, 0x82, 0x94, 0x57, 0x5b, 0xf3, 0x08,
0x42, 0x45, 0x23, 0x08, 0x44, 0x54, 0x35, 0x36,
0xed, 0x4b, 0xb3, 0xa5, 0xbf, 0x17, 0xc1, 0x3c,
0xdd, 0x25, 0x4a, 0x30, 0x64, 0xed, 0x66, 0x06,
0x72, 0x05, 0xc2, 0x71, 0x5a, 0x6c, 0xd0, 0x75,
},
},
{
.data_len = 2,
.digest = {
0x01, 0x37, 0x97, 0xcc, 0x0a, 0xcb, 0x61, 0xa4,
0x93, 0x26, 0x36, 0x4b, 0xd2, 0x27, 0xea, 0xaf,
0xda, 0xfa, 0x8f, 0x86, 0x12, 0x99, 0x7b, 0xc8,
0x94, 0xa9, 0x1c, 0x70, 0x3f, 0x43, 0xf3, 0x9a,
0x02, 0xc5, 0x0d, 0x8e, 0x01, 0xf8, 0x3a, 0xa6,
0xec, 0xaa, 0xc5, 0xc7, 0x9d, 0x3d, 0x7f, 0x9d,
0x47, 0x0e, 0x58, 0x2d, 0x9a, 0x2d, 0x51, 0x1d,
0xc3, 0x77, 0xb2, 0x7f, 0x69, 0x9a, 0xc3, 0x50,
},
},
{
.data_len = 3,
.digest = {
0xd4, 0xa3, 0xc2, 0x50, 0xa0, 0x33, 0xc6, 0xe4,
0x50, 0x08, 0xea, 0xc9, 0xb8, 0x35, 0x55, 0x34,
0x61, 0xb8, 0x2e, 0xa2, 0xe5, 0xdc, 0x04, 0x70,
0x99, 0x86, 0x5f, 0xee, 0x2e, 0x1e, 0xd4, 0x40,
0xf8, 0x88, 0x01, 0x84, 0x97, 0x16, 0x6a, 0xd3,
0x0a, 0xb4, 0xe2, 0x34, 0xca, 0x1f, 0xfc, 0x6b,
0x61, 0xf3, 0x7f, 0x72, 0xfa, 0x8d, 0x22, 0xcf,
0x7f, 0x2d, 0x87, 0x9d, 0xbb, 0x59, 0xac, 0x53,
},
},
{
.data_len = 16,
.digest = {
0x3b, 0xae, 0x66, 0x48, 0x0f, 0x35, 0x3c, 0xcd,
0x92, 0xa9, 0xb6, 0xb9, 0xfe, 0x19, 0x90, 0x92,
0x52, 0xa5, 0x02, 0xd1, 0x89, 0xcf, 0x98, 0x86,
0x29, 0x28, 0xab, 0xc4, 0x9e, 0xcc, 0x75, 0x38,
0x95, 0xa7, 0x59, 0x43, 0xef, 0x8c, 0x3a, 0xeb,
0x40, 0xa3, 0xbe, 0x2b, 0x75, 0x0d, 0xfd, 0xc3,
0xaf, 0x69, 0x08, 0xad, 0x9f, 0xc9, 0xf4, 0x96,
0xa9, 0xc2, 0x2b, 0x1b, 0x66, 0x6f, 0x1d, 0x28,
},
},
{
.data_len = 32,
.digest = {
0x9c, 0xfb, 0x3c, 0x40, 0xd5, 0x3b, 0xc4, 0xff,
0x07, 0xa7, 0xf0, 0x24, 0xb7, 0xd6, 0x5e, 0x12,
0x5b, 0x85, 0xb5, 0xa5, 0xe0, 0x82, 0xa6, 0xda,
0x30, 0x13, 0x2f, 0x1a, 0xe3, 0xd0, 0x55, 0xcb,
0x14, 0x19, 0xe2, 0x09, 0x91, 0x96, 0x26, 0xf9,
0x38, 0xd7, 0xfa, 0x4a, 0xfb, 0x2f, 0x6f, 0xc0,
0xf4, 0x95, 0xc3, 0x40, 0xf6, 0xdb, 0xe7, 0xc2,
0x79, 0x23, 0xa4, 0x20, 0x96, 0x3a, 0x00, 0xbb,
},
},
{
.data_len = 48,
.digest = {
0x92, 0x1a, 0x21, 0x06, 0x6e, 0x08, 0x84, 0x09,
0x23, 0x8d, 0x63, 0xec, 0xd6, 0x72, 0xd3, 0x21,
0x51, 0xe8, 0x65, 0x94, 0xf8, 0x1f, 0x5f, 0xa7,
0xab, 0x6b, 0xae, 0x1c, 0x2c, 0xaf, 0xf9, 0x0c,
0x7c, 0x5a, 0x74, 0x1d, 0x90, 0x26, 0x4a, 0xc3,
0xa1, 0x60, 0xf4, 0x1d, 0xd5, 0x3c, 0x86, 0xe8,
0x00, 0xb3, 0x99, 0x27, 0xb8, 0x9d, 0x3e, 0x17,
0x32, 0x5a, 0x34, 0x3e, 0xc2, 0xb2, 0x6e, 0xbd,
},
},
{
.data_len = 49,
.digest = {
0x5a, 0x1f, 0x40, 0x5f, 0xee, 0xf2, 0xdd, 0x67,
0x01, 0xcb, 0x26, 0x58, 0xf5, 0x1b, 0xe8, 0x7e,
0xeb, 0x7d, 0x9d, 0xef, 0xd3, 0x55, 0xd6, 0x89,
0x2e, 0xfc, 0x14, 0xe2, 0x98, 0x4c, 0x31, 0xaa,
0x69, 0x00, 0xf9, 0x4e, 0xb0, 0x75, 0x1b, 0x71,
0x93, 0x60, 0xdf, 0xa1, 0xaf, 0xba, 0xc2, 0xd3,
0x6a, 0x22, 0xa0, 0xff, 0xb5, 0x66, 0x15, 0x66,
0xd2, 0x24, 0x9a, 0x7e, 0xe4, 0xe5, 0x84, 0xdb,
},
},
{
.data_len = 63,
.digest = {
0x32, 0xbd, 0xcf, 0x72, 0xa9, 0x74, 0x87, 0xe6,
0x2a, 0x53, 0x7e, 0x6d, 0xac, 0xc2, 0xdd, 0x2c,
0x87, 0xb3, 0xf7, 0x90, 0x29, 0xc9, 0x16, 0x59,
0xd2, 0x7e, 0x6e, 0x84, 0x1d, 0xa6, 0xaf, 0x3c,
0xca, 0xd6, 0x1a, 0x24, 0xa4, 0xcd, 0x59, 0x44,
0x20, 0xd7, 0xd2, 0x5b, 0x97, 0xda, 0xd5, 0xa9,
0x23, 0xb1, 0xa4, 0x60, 0xb8, 0x05, 0x98, 0xdc,
0xef, 0x89, 0x81, 0xe3, 0x3a, 0xf9, 0x24, 0x37,
},
},
{
.data_len = 64,
.digest = {
0x96, 0x3a, 0x1a, 0xdd, 0x1b, 0xeb, 0x1a, 0x55,
0x24, 0x52, 0x3d, 0xec, 0x9d, 0x52, 0x2e, 0xa6,
0xfe, 0x81, 0xd6, 0x98, 0xac, 0xcc, 0x60, 0x56,
0x04, 0x9d, 0xa3, 0xf3, 0x56, 0x05, 0xe4, 0x8a,
0x61, 0xaf, 0x6f, 0x6e, 0x8e, 0x75, 0x67, 0x3a,
0xd2, 0xb0, 0x85, 0x2d, 0x17, 0xd2, 0x86, 0x8c,
0x50, 0x4b, 0xdd, 0xef, 0x35, 0x00, 0xde, 0x29,
0x3d, 0x4b, 0x04, 0x12, 0x8a, 0x81, 0xe2, 0xcc,
},
},
{
.data_len = 65,
.digest = {
0x9c, 0x6e, 0xf0, 0x6f, 0x71, 0x77, 0xd5, 0xd0,
0xbb, 0x70, 0x1f, 0xcb, 0xbd, 0xd3, 0xfe, 0x23,
0x71, 0x78, 0xad, 0x3a, 0xd2, 0x1e, 0x34, 0xf4,
0x6d, 0xb4, 0xa2, 0x0a, 0x24, 0xcb, 0xe1, 0x99,
0x07, 0xd0, 0x79, 0x8f, 0x7e, 0x69, 0x31, 0x68,
0x29, 0xb5, 0x85, 0x82, 0x67, 0xdc, 0x4a, 0x8d,
0x44, 0x04, 0x02, 0xc0, 0xfb, 0xd2, 0x19, 0x66,
0x1e, 0x25, 0x8b, 0xd2, 0x5a, 0x59, 0x68, 0xc0,
},
},
{
.data_len = 127,
.digest = {
0xb8, 0x8f, 0xa8, 0x29, 0x4d, 0xcf, 0x5f, 0x73,
0x3c, 0x55, 0x43, 0xd9, 0x1c, 0xbc, 0x0c, 0x17,
0x75, 0x0b, 0xc7, 0xb1, 0x1d, 0x9f, 0x7b, 0x2f,
0x4c, 0x3d, 0x2a, 0x71, 0xfb, 0x1b, 0x0e, 0xca,
0x4e, 0x96, 0xa0, 0x95, 0xee, 0xf4, 0x93, 0x76,
0x36, 0xfb, 0x5d, 0xd3, 0x46, 0xc4, 0x1d, 0x41,
0x32, 0x92, 0x9d, 0xed, 0xdb, 0x7f, 0xfa, 0xb3,
0x91, 0x61, 0x3e, 0xd6, 0xb2, 0xca, 0x8d, 0x81,
},
},
{
.data_len = 128,
.digest = {
0x54, 0xac, 0x1a, 0xa1, 0xa6, 0xa3, 0x47, 0x2a,
0x5a, 0xac, 0x1a, 0x3a, 0x4b, 0xa1, 0x11, 0x08,
0xa7, 0x90, 0xec, 0x52, 0xcb, 0xaf, 0x68, 0x41,
0x38, 0x44, 0x53, 0x94, 0x93, 0x30, 0xaf, 0x3a,
0xec, 0x99, 0x3a, 0x7d, 0x2a, 0xd5, 0xb6, 0x05,
0xf5, 0xa6, 0xbb, 0x9b, 0x82, 0xc2, 0xbd, 0x98,
0x28, 0x62, 0x98, 0x3e, 0xe4, 0x27, 0x9b, 0xaa,
0xce, 0x0a, 0x6f, 0xab, 0x1b, 0x16, 0xf3, 0xdd,
},
},
{
.data_len = 129,
.digest = {
0x04, 0x37, 0x60, 0xbc, 0xb3, 0xb1, 0xc6, 0x2d,
0x42, 0xc5, 0xd7, 0x7e, 0xd9, 0x86, 0x82, 0xe0,
0xf4, 0x62, 0xad, 0x75, 0x68, 0x0b, 0xc7, 0xa8,
0xd6, 0x9a, 0x76, 0xe5, 0x29, 0xb8, 0x37, 0x30,
0x0f, 0xc0, 0xbc, 0x81, 0x94, 0x7c, 0x13, 0xf4,
0x9c, 0x27, 0xbc, 0x59, 0xa1, 0x70, 0x6a, 0x87,
0x20, 0x12, 0x0a, 0x2a, 0x62, 0x5e, 0x6f, 0xca,
0x91, 0x6b, 0x34, 0x7e, 0x4c, 0x0d, 0xf0, 0x6c,
},
},
{
.data_len = 256,
.digest = {
0x4b, 0x7c, 0x1f, 0x53, 0x52, 0xcc, 0x30, 0xed,
0x91, 0x44, 0x6f, 0x0d, 0xb5, 0x41, 0x79, 0x99,
0xaf, 0x82, 0x65, 0x52, 0x03, 0xf8, 0x55, 0x74,
0x7c, 0xd9, 0x41, 0xd6, 0xe8, 0x91, 0xa4, 0x85,
0xcb, 0x0a, 0x60, 0x08, 0x76, 0x07, 0x60, 0x99,
0x89, 0x76, 0xba, 0x84, 0xbd, 0x0b, 0xf2, 0xb3,
0xdc, 0xf3, 0x33, 0xd1, 0x9b, 0x0b, 0x2e, 0x5d,
0xf6, 0x9d, 0x0f, 0x67, 0xf4, 0x86, 0xb3, 0xd5,
},
},
{
.data_len = 511,
.digest = {
0x7d, 0x83, 0x78, 0x6a, 0x5d, 0x52, 0x42, 0x2a,
0xb1, 0x97, 0xc6, 0x62, 0xa2, 0x2a, 0x7c, 0x8b,
0xcd, 0x4f, 0xa4, 0x86, 0x19, 0xa4, 0x5b, 0x1d,
0xc7, 0x6f, 0x2f, 0x9c, 0x03, 0xc3, 0x45, 0x2e,
0xa7, 0x8e, 0x38, 0xf2, 0x57, 0x55, 0x89, 0x47,
0xed, 0xeb, 0x81, 0xe2, 0xe0, 0x55, 0x9f, 0xe6,
0xca, 0x03, 0x59, 0xd3, 0xd4, 0xba, 0xc9, 0x2d,
0xaf, 0xbb, 0x62, 0x2e, 0xe6, 0x89, 0xe4, 0x11,
},
},
{
.data_len = 513,
.digest = {
0xe9, 0x14, 0xe7, 0x01, 0xd0, 0x81, 0x09, 0x51,
0x78, 0x1c, 0x8e, 0x6c, 0x00, 0xd3, 0x28, 0xa0,
0x2a, 0x7b, 0xd6, 0x25, 0xca, 0xd0, 0xf9, 0xb8,
0xd8, 0xcf, 0xd0, 0xb7, 0x48, 0x25, 0xb7, 0x6a,
0x53, 0x8e, 0xf8, 0x52, 0x9c, 0x1f, 0x7d, 0xae,
0x4c, 0x22, 0xd5, 0x9d, 0xf0, 0xaf, 0x98, 0x91,
0x19, 0x1f, 0x99, 0xbd, 0xa6, 0xc2, 0x0f, 0x05,
0xa5, 0x9f, 0x3e, 0x87, 0xed, 0xc3, 0xab, 0x92,
},
},
{
.data_len = 1000,
.digest = {
0x2e, 0xf4, 0x72, 0xd2, 0xd9, 0x4a, 0xd5, 0xf9,
0x20, 0x03, 0x4a, 0xad, 0xed, 0xa9, 0x1b, 0x64,
0x73, 0x38, 0xc6, 0x30, 0xa8, 0x7f, 0xf9, 0x3b,
0x8c, 0xbc, 0xa1, 0x2d, 0x22, 0x7b, 0x84, 0x37,
0xf5, 0xba, 0xee, 0xf0, 0x80, 0x9d, 0xe3, 0x82,
0xbd, 0x07, 0x68, 0x15, 0x01, 0x22, 0xf6, 0x88,
0x07, 0x0b, 0xfd, 0xb7, 0xb1, 0xc0, 0x68, 0x4b,
0x8d, 0x05, 0xec, 0xfb, 0xcd, 0xde, 0xa4, 0x2a,
},
},
{
.data_len = 3333,
.digest = {
0x73, 0xe3, 0xe5, 0x87, 0x01, 0x0a, 0x29, 0x4d,
0xad, 0x92, 0x67, 0x64, 0xc7, 0x71, 0x0b, 0x22,
0x80, 0x8a, 0x6e, 0x8b, 0x20, 0x73, 0xb2, 0xd7,
0x98, 0x20, 0x35, 0x42, 0x42, 0x5d, 0x85, 0x12,
0xb0, 0x06, 0x69, 0x63, 0x5f, 0x5b, 0xe7, 0x63,
0x6f, 0xe6, 0x18, 0xa6, 0xc1, 0xa6, 0xae, 0x27,
0xa7, 0x6a, 0x73, 0x6b, 0x27, 0xd5, 0x47, 0xe1,
0xa2, 0x7d, 0xe4, 0x0d, 0xbd, 0x23, 0x7b, 0x7a,
},
},
{
.data_len = 4096,
.digest = {
0x11, 0x5b, 0x77, 0x36, 0x6b, 0x3b, 0xe4, 0x42,
0xe4, 0x92, 0x23, 0xcb, 0x0c, 0x06, 0xff, 0xb7,
0x0c, 0x71, 0x64, 0xd9, 0x8a, 0x57, 0x75, 0x7b,
0xa2, 0xd2, 0x17, 0x19, 0xbb, 0xb5, 0x3c, 0xb3,
0x5f, 0xae, 0x35, 0x75, 0x8e, 0xa8, 0x97, 0x43,
0xce, 0xfe, 0x41, 0x84, 0xfe, 0xcb, 0x18, 0x70,
0x68, 0x2e, 0x16, 0x19, 0xd5, 0x10, 0x0d, 0x2f,
0x61, 0x87, 0x79, 0xee, 0x5f, 0x24, 0xdd, 0x76,
},
},
{
.data_len = 4128,
.digest = {
0x9e, 0x96, 0xe1, 0x0a, 0xb2, 0xd5, 0xba, 0xcf,
0x27, 0xba, 0x6f, 0x85, 0xe9, 0xbf, 0x96, 0xb9,
0x5a, 0x00, 0x00, 0x06, 0xdc, 0xb7, 0xaf, 0x0a,
0x8f, 0x1d, 0x31, 0xf6, 0xce, 0xc3, 0x50, 0x2e,
0x61, 0x3a, 0x8b, 0x28, 0xaf, 0xb2, 0x50, 0x0d,
0x00, 0x98, 0x02, 0x11, 0x6b, 0xfa, 0x51, 0xc1,
0xde, 0xe1, 0x34, 0x9f, 0xda, 0x11, 0x63, 0xfa,
0x0a, 0xa0, 0xa2, 0x67, 0x39, 0xeb, 0x9b, 0xf1,
},
},
{
.data_len = 4160,
.digest = {
0x46, 0x4e, 0x81, 0xd1, 0x08, 0x2a, 0x46, 0x12,
0x4e, 0xae, 0x1f, 0x5d, 0x57, 0xe5, 0x19, 0xbc,
0x76, 0x38, 0xb6, 0xa7, 0xe3, 0x72, 0x6d, 0xaf,
0x80, 0x3b, 0xd0, 0xbc, 0x06, 0xe8, 0xab, 0xab,
0x86, 0x4b, 0x0b, 0x7a, 0x61, 0xa6, 0x13, 0xff,
0x64, 0x47, 0x89, 0xb7, 0x63, 0x8a, 0xa5, 0x4c,
0x9f, 0x52, 0x70, 0xeb, 0x21, 0xe5, 0x2d, 0xe9,
0xe7, 0xab, 0x1c, 0x0e, 0x74, 0xf5, 0x72, 0xec,
},
},
{
.data_len = 4224,
.digest = {
0xfa, 0x6e, 0xff, 0x3c, 0xc1, 0x98, 0x49, 0x42,
0x34, 0x67, 0xd4, 0xd3, 0xfa, 0xae, 0x27, 0xe4,
0x77, 0x11, 0x84, 0xd2, 0x57, 0x99, 0xf8, 0xfd,
0x41, 0x50, 0x84, 0x80, 0x7f, 0xf7, 0xb2, 0xd3,
0x88, 0x21, 0x9c, 0xe8, 0xb9, 0x05, 0xd3, 0x48,
0x64, 0xc5, 0xb7, 0x29, 0xd9, 0x21, 0x17, 0xad,
0x89, 0x9c, 0x79, 0x55, 0x51, 0x0b, 0x96, 0x3e,
0x10, 0x40, 0xe1, 0xdd, 0x7b, 0x39, 0x40, 0x86,
},
},
{
.data_len = 16384,
.digest = {
0x41, 0xb3, 0xd2, 0x93, 0xcd, 0x79, 0x84, 0xc2,
0xf5, 0xea, 0xf3, 0xb3, 0x94, 0x23, 0xaa, 0x76,
0x87, 0x5f, 0xe3, 0xd2, 0x03, 0xd8, 0x00, 0xbb,
0xa1, 0x55, 0xe4, 0xcb, 0x16, 0x04, 0x5b, 0xdf,
0xf8, 0xd2, 0x63, 0x51, 0x02, 0x22, 0xc6, 0x0f,
0x98, 0x2b, 0x12, 0x52, 0x25, 0x64, 0x93, 0xd9,
0xab, 0xe9, 0x4d, 0x16, 0x4b, 0xf6, 0x09, 0x83,
0x5c, 0x63, 0x1c, 0x41, 0x19, 0xf6, 0x76, 0xe3,
},
},
};
static const u8 hash_testvec_consolidated[SHA512_DIGEST_SIZE] = {
0x5b, 0x9d, 0xf9, 0xab, 0x8c, 0x8e, 0x52, 0xdb,
0x02, 0xa0, 0x4c, 0x24, 0x2d, 0xc4, 0xa8, 0x4e,
0x9c, 0x93, 0x2f, 0x72, 0xa8, 0x75, 0xfb, 0xb5,
0xdb, 0xef, 0x52, 0xc6, 0xa3, 0xfe, 0xeb, 0x6b,
0x92, 0x79, 0x18, 0x05, 0xf6, 0xd7, 0xaf, 0x7b,
0x36, 0xfc, 0x83, 0x2c, 0x7e, 0x7b, 0x59, 0x8b,
0xf9, 0x81, 0xaa, 0x98, 0x38, 0x11, 0x97, 0x56,
0x34, 0xe5, 0x2a, 0x4b, 0xf2, 0x9e, 0xf3, 0xdb,
};
static const u8 hmac_testvec_consolidated[SHA512_DIGEST_SIZE] = {
0x40, 0xe7, 0xbc, 0x03, 0xdf, 0x22, 0xd4, 0x76,
0x66, 0x45, 0xf8, 0x1d, 0x25, 0xdf, 0xbe, 0xa2,
0x93, 0x06, 0x8c, 0x1d, 0x14, 0x23, 0x9b, 0x5c,
0xfa, 0xac, 0xdf, 0xbd, 0xa2, 0x24, 0xe5, 0xf7,
0xdc, 0xf7, 0xae, 0x96, 0xc1, 0x34, 0xe5, 0x24,
0x16, 0x24, 0xdc, 0xee, 0x4f, 0x62, 0x1c, 0x67,
0x4e, 0x02, 0x31, 0x4b, 0x9b, 0x65, 0x25, 0xeb,
0x32, 0x2e, 0x24, 0xfb, 0xcd, 0x2b, 0x59, 0xd8,
};
@@ -0,0 +1,39 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright 2025 Google LLC
*/
#include <crypto/sha2.h>
#include "sha512-testvecs.h"
#define HASH sha512
#define HASH_CTX sha512_ctx
#define HASH_SIZE SHA512_DIGEST_SIZE
#define HASH_INIT sha512_init
#define HASH_UPDATE sha512_update
#define HASH_FINAL sha512_final
#define HMAC_KEY hmac_sha512_key
#define HMAC_CTX hmac_sha512_ctx
#define HMAC_PREPAREKEY hmac_sha512_preparekey
#define HMAC_INIT hmac_sha512_init
#define HMAC_UPDATE hmac_sha512_update
#define HMAC_FINAL hmac_sha512_final
#define HMAC hmac_sha512
#define HMAC_USINGRAWKEY hmac_sha512_usingrawkey
#include "hash-test-template.h"
static struct kunit_case hash_test_cases[] = {
HASH_KUNIT_CASES,
KUNIT_CASE(benchmark_hash),
{},
};
static struct kunit_suite hash_test_suite = {
.name = "sha512",
.test_cases = hash_test_cases,
.suite_init = hash_suite_init,
.suite_exit = hash_suite_exit,
};
kunit_test_suite(hash_test_suite);
MODULE_DESCRIPTION("KUnit tests and benchmark for SHA-512 and HMAC-SHA512");
MODULE_LICENSE("GPL");
+231
View File
@@ -0,0 +1,231 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */
/* This file was generated by: ./scripts/crypto/gen-hash-testvecs.py sm3 */
static const struct {
size_t data_len;
u8 digest[SM3_DIGEST_SIZE];
} hash_testvecs[] = {
{
.data_len = 0,
.digest = {
0x1a, 0xb2, 0x1d, 0x83, 0x55, 0xcf, 0xa1, 0x7f,
0x8e, 0x61, 0x19, 0x48, 0x31, 0xe8, 0x1a, 0x8f,
0x22, 0xbe, 0xc8, 0xc7, 0x28, 0xfe, 0xfb, 0x74,
0x7e, 0xd0, 0x35, 0xeb, 0x50, 0x82, 0xaa, 0x2b,
},
},
{
.data_len = 1,
.digest = {
0xb6, 0x22, 0x2c, 0x39, 0xdc, 0x14, 0x9a, 0xee,
0x01, 0x9a, 0xcb, 0x0d, 0xe6, 0xc6, 0x75, 0x6e,
0x8f, 0x18, 0x7b, 0x0e, 0xe8, 0x98, 0x61, 0x71,
0x2b, 0xd8, 0x38, 0xa9, 0xee, 0x2c, 0x1e, 0x93,
},
},
{
.data_len = 2,
.digest = {
0x62, 0x0c, 0x66, 0x77, 0x67, 0x28, 0x74, 0x8a,
0xe3, 0x64, 0xea, 0x44, 0x6a, 0x3f, 0x34, 0x61,
0x55, 0xc5, 0xaa, 0xb2, 0x6c, 0x67, 0x97, 0x68,
0x68, 0xae, 0x4d, 0x64, 0xa8, 0xb6, 0x72, 0x3e,
},
},
{
.data_len = 3,
.digest = {
0x71, 0xd4, 0x63, 0xb1, 0xfa, 0x27, 0xc7, 0xae,
0x65, 0xed, 0x5c, 0x93, 0x70, 0xe0, 0x09, 0x34,
0x2f, 0x42, 0xe6, 0x71, 0x16, 0x8e, 0x90, 0x90,
0x9a, 0xdd, 0xa6, 0x44, 0x66, 0x71, 0x18, 0xf9,
},
},
{
.data_len = 16,
.digest = {
0x79, 0x0b, 0x68, 0xb5, 0x41, 0xc1, 0x97, 0xa0,
0x50, 0xe6, 0x93, 0x70, 0xf6, 0x98, 0x49, 0xea,
0x92, 0xc9, 0xd0, 0xb1, 0x46, 0xbd, 0x4a, 0x0c,
0x8e, 0xe8, 0xf3, 0xe4, 0x8f, 0x90, 0x33, 0x3c,
},
},
{
.data_len = 32,
.digest = {
0x32, 0x9f, 0xa3, 0x18, 0x18, 0x45, 0xe0, 0x28,
0xd3, 0xa4, 0x41, 0x3a, 0x25, 0x62, 0x9c, 0x95,
0xab, 0xfe, 0x02, 0xe0, 0x37, 0x7d, 0x3c, 0xc4,
0xce, 0x69, 0x57, 0x5a, 0x07, 0x0e, 0xb9, 0xf5,
},
},
{
.data_len = 48,
.digest = {
0x0c, 0xcf, 0x7c, 0x48, 0x44, 0xa0, 0xb0, 0x8d,
0xdf, 0xbe, 0x22, 0x14, 0x7e, 0xd4, 0xc3, 0x8d,
0x6a, 0x23, 0xfc, 0x44, 0x0e, 0x0f, 0xde, 0xa5,
0x7c, 0x8b, 0xc4, 0x8b, 0xab, 0x8c, 0x87, 0x41,
},
},
{
.data_len = 49,
.digest = {
0xb3, 0x76, 0x8b, 0x19, 0xf9, 0x10, 0xa9, 0x56,
0x4f, 0xce, 0x27, 0xaa, 0x65, 0x96, 0xe5, 0xdb,
0x90, 0x9b, 0x92, 0xcd, 0x32, 0x0d, 0x16, 0xac,
0xf8, 0xd0, 0x66, 0x62, 0x10, 0xf0, 0x44, 0xdf,
},
},
{
.data_len = 63,
.digest = {
0x07, 0xc9, 0x45, 0x65, 0x9f, 0x68, 0x75, 0xc3,
0x74, 0xb2, 0x3b, 0x0c, 0x97, 0x05, 0xd3, 0x13,
0xc0, 0xb6, 0x21, 0xed, 0xf6, 0x10, 0x7a, 0xed,
0xec, 0xd8, 0x10, 0x29, 0xbf, 0x7a, 0x78, 0x37,
},
},
{
.data_len = 64,
.digest = {
0x3e, 0x69, 0x18, 0x45, 0xd8, 0x25, 0x6f, 0x44,
0xc0, 0x02, 0xe5, 0xcf, 0xcd, 0x94, 0x42, 0xa9,
0xd5, 0x12, 0x62, 0x10, 0x15, 0xa0, 0xf9, 0x16,
0x19, 0x2d, 0x8d, 0x63, 0x31, 0xf2, 0x2f, 0x36,
},
},
{
.data_len = 65,
.digest = {
0x6b, 0x3e, 0xc0, 0x20, 0xb7, 0x74, 0x30, 0xa0,
0xc6, 0x5c, 0xee, 0xbe, 0xdc, 0xe6, 0xe5, 0x4f,
0x3c, 0x61, 0x8d, 0x91, 0xac, 0x31, 0x4b, 0x2a,
0xdf, 0x1c, 0xef, 0x24, 0xdc, 0x0a, 0x10, 0xe8,
},
},
{
.data_len = 127,
.digest = {
0xab, 0xd6, 0xa1, 0xbf, 0x39, 0x43, 0x75, 0xda,
0xbf, 0xc7, 0x22, 0xcc, 0x4e, 0xfc, 0xe4, 0x42,
0x6d, 0x1b, 0x87, 0x25, 0x64, 0x7f, 0x88, 0xf7,
0xc3, 0x0a, 0x0a, 0x4c, 0xd6, 0xa7, 0x68, 0xae,
},
},
{
.data_len = 128,
.digest = {
0x1b, 0x70, 0xd4, 0x5f, 0x6c, 0xe4, 0x2d, 0x58,
0x2d, 0x0f, 0x9a, 0x12, 0x34, 0xbb, 0x5e, 0x38,
0xd8, 0x1f, 0x6a, 0x46, 0x8a, 0xef, 0xdb, 0x68,
0x18, 0x62, 0xbb, 0x85, 0xfc, 0xc4, 0x6e, 0x2e,
},
},
{
.data_len = 129,
.digest = {
0x33, 0x62, 0xba, 0xa7, 0x4a, 0xbc, 0xd7, 0x7b,
0xd4, 0x67, 0x6d, 0x3e, 0xea, 0xe8, 0xb0, 0x64,
0x0d, 0xf3, 0xae, 0x1d, 0x52, 0x24, 0x11, 0x9f,
0xda, 0xa9, 0x7f, 0xd5, 0x22, 0x1a, 0xde, 0x8a,
},
},
{
.data_len = 256,
.digest = {
0x70, 0xa8, 0xa6, 0x2b, 0xfb, 0x1f, 0x3b, 0x5a,
0xcc, 0x71, 0x76, 0x9e, 0x25, 0x4c, 0xfa, 0x8f,
0x39, 0x4a, 0x21, 0x8a, 0x9d, 0x74, 0x8d, 0x2c,
0x31, 0xa5, 0xb5, 0xff, 0x30, 0xc1, 0x14, 0xc4,
},
},
{
.data_len = 511,
.digest = {
0x39, 0xd0, 0x8c, 0x5f, 0xfc, 0x36, 0xc2, 0x7c,
0xdb, 0x8b, 0x2e, 0xdc, 0x9d, 0x1b, 0xd1, 0xba,
0x9b, 0x52, 0x6b, 0x35, 0x46, 0x46, 0x75, 0x73,
0xe5, 0x62, 0x96, 0x6e, 0xf3, 0xba, 0xd9, 0x19,
},
},
{
.data_len = 513,
.digest = {
0x76, 0xa0, 0x3d, 0xa2, 0x5f, 0xd4, 0xa6, 0xbe,
0x6b, 0xdb, 0xed, 0x14, 0x9e, 0xa8, 0x15, 0x77,
0xa9, 0x38, 0x30, 0x6b, 0x68, 0xfa, 0xb6, 0xe2,
0x93, 0x19, 0x24, 0x72, 0x67, 0x20, 0x72, 0xc3,
},
},
{
.data_len = 1000,
.digest = {
0x16, 0xbc, 0x33, 0x77, 0x0b, 0xcf, 0x93, 0x5e,
0xec, 0x7d, 0x8d, 0x3c, 0xae, 0xd9, 0x75, 0xdf,
0x46, 0x24, 0x17, 0x7e, 0x03, 0x88, 0xf2, 0x75,
0xa9, 0x18, 0xa6, 0x1c, 0x7a, 0x74, 0x0d, 0xf3,
},
},
{
.data_len = 3333,
.digest = {
0xdb, 0x54, 0x89, 0xe7, 0x1c, 0x50, 0xf2, 0xbf,
0xde, 0x3a, 0xbf, 0x5b, 0xee, 0x5a, 0x46, 0x62,
0x20, 0xb1, 0x80, 0x48, 0xac, 0x56, 0x33, 0xb3,
0xbb, 0x3f, 0xfa, 0x02, 0xc6, 0x43, 0xb5, 0x8c,
},
},
{
.data_len = 4096,
.digest = {
0xdf, 0x0d, 0xed, 0x3b, 0x8f, 0xea, 0x81, 0xfd,
0xd6, 0x34, 0xae, 0x74, 0x24, 0x3a, 0x15, 0x38,
0xe7, 0xcf, 0x45, 0x7e, 0x8f, 0xf5, 0x50, 0x6c,
0xaa, 0x27, 0x23, 0x92, 0x6d, 0xab, 0x3b, 0xde,
},
},
{
.data_len = 4128,
.digest = {
0x6a, 0xbd, 0x56, 0x5a, 0xf1, 0xc6, 0x40, 0x4d,
0xf3, 0x50, 0x77, 0x87, 0x86, 0x63, 0x1b, 0x4d,
0x21, 0x99, 0x96, 0xad, 0x24, 0x62, 0xce, 0xc0,
0x3e, 0xb7, 0x35, 0x52, 0x56, 0x0e, 0x55, 0x85,
},
},
{
.data_len = 4160,
.digest = {
0x5b, 0xc1, 0x1f, 0x25, 0x43, 0xa3, 0x1c, 0xa0,
0x8c, 0xfc, 0x41, 0xf1, 0xcc, 0xb3, 0x95, 0x95,
0xe0, 0xb9, 0xd3, 0x29, 0xf4, 0x08, 0x31, 0x47,
0x6d, 0x09, 0xa8, 0x2e, 0xa5, 0xf4, 0xf1, 0x8d,
},
},
{
.data_len = 4224,
.digest = {
0xec, 0x56, 0x1e, 0xa6, 0x1f, 0xb2, 0x87, 0xb2,
0x7e, 0x15, 0xd6, 0x30, 0x08, 0x74, 0xb0, 0x48,
0x90, 0x2a, 0xbe, 0x2f, 0x80, 0x3a, 0x88, 0xcc,
0xd7, 0xc5, 0x87, 0x8c, 0x04, 0xef, 0x78, 0x71,
},
},
{
.data_len = 16384,
.digest = {
0xe7, 0xb8, 0x84, 0x20, 0xff, 0xd5, 0x53, 0xe6,
0x14, 0x31, 0x12, 0x75, 0xb7, 0x9a, 0x4f, 0x63,
0x63, 0x00, 0xfe, 0x2c, 0x54, 0xee, 0x06, 0xfc,
0x12, 0x16, 0xe5, 0xdc, 0xa4, 0x40, 0x62, 0x12,
},
},
};
static const u8 hash_testvec_consolidated[SM3_DIGEST_SIZE] = {
0xe6, 0x58, 0xd4, 0x8e, 0x74, 0x92, 0xdf, 0xfe,
0x58, 0x05, 0xe5, 0x29, 0x83, 0xfb, 0xb7, 0x51,
0x7e, 0x66, 0x0c, 0x49, 0x3e, 0x11, 0x7e, 0x9b,
0xb1, 0x83, 0x3a, 0xa6, 0xb0, 0x3c, 0xf5, 0xe0,
};
+31
View File
@@ -0,0 +1,31 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright 2026 Google LLC
*/
#include <crypto/sm3.h>
#include "sm3-testvecs.h"
#define HASH sm3
#define HASH_CTX sm3_ctx
#define HASH_SIZE SM3_DIGEST_SIZE
#define HASH_INIT sm3_init
#define HASH_UPDATE sm3_update
#define HASH_FINAL sm3_final
#include "hash-test-template.h"
static struct kunit_case sm3_test_cases[] = {
HASH_KUNIT_CASES,
KUNIT_CASE(benchmark_hash),
{},
};
static struct kunit_suite sm3_test_suite = {
.name = "sm3",
.test_cases = sm3_test_cases,
.suite_init = hash_suite_init,
.suite_exit = hash_suite_exit,
};
kunit_test_suite(sm3_test_suite);
MODULE_DESCRIPTION("KUnit tests and benchmark for SM3");
MODULE_LICENSE("GPL");