mirror of
https://github.com/torvalds/linux.git
synced 2026-04-27 11:02:31 -04:00
Merge tag 'libcrypto-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/ebiggers/linux
Pull crypto library updates from Eric Biggers: - Add a RISC-V optimized implementation of Poly1305. This code was written by Andy Polyakov and contributed by Zhihang Shao. - Migrate the MD5 code into lib/crypto/, and add KUnit tests for MD5. Yes, it's still the 90s, and several kernel subsystems are still using MD5 for legacy use cases. As long as that remains the case, it's helpful to clean it up in the same way as I've been doing for other algorithms. Later, I plan to convert most of these users of MD5 to use the new MD5 library API instead of the generic crypto API. - Simplify the organization of the ChaCha, Poly1305, BLAKE2s, and Curve25519 code. Consolidate these into one module per algorithm, and centralize the configuration and build process. This is the same reorganization that has already been successful for SHA-1 and SHA-2. - Remove the unused crypto_kpp API for Curve25519. - Migrate the BLAKE2s and Curve25519 self-tests to KUnit. - Always enable the architecture-optimized BLAKE2s code. * tag 'libcrypto-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/ebiggers/linux: (38 commits) crypto: md5 - Implement export_core() and import_core() wireguard: kconfig: simplify crypto kconfig selections lib/crypto: tests: Enable Curve25519 test when CRYPTO_SELFTESTS lib/crypto: curve25519: Consolidate into single module lib/crypto: curve25519: Move a couple functions out-of-line lib/crypto: tests: Add Curve25519 benchmark lib/crypto: tests: Migrate Curve25519 self-test to KUnit crypto: curve25519 - Remove unused kpp support crypto: testmgr - Remove curve25519 kpp tests crypto: x86/curve25519 - Remove unused kpp support crypto: powerpc/curve25519 - Remove unused kpp support crypto: arm/curve25519 - Remove unused kpp support crypto: hisilicon/hpre - Remove unused curve25519 kpp support lib/crypto: tests: Add KUnit tests for BLAKE2s lib/crypto: blake2s: Consolidate into single C translation unit lib/crypto: blake2s: Move generic code into blake2s.c lib/crypto: blake2s: Always enable arch-optimized BLAKE2s code lib/crypto: blake2s: Remove obsolete self-test lib/crypto: x86/blake2s: Reduce size of BLAKE2S_SIGMA2 lib/crypto: chacha: Consolidate into single module ...
This commit is contained in:
@@ -1,5 +1,34 @@
|
||||
# SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
config CRYPTO_LIB_BLAKE2S_KUNIT_TEST
|
||||
tristate "KUnit tests for BLAKE2s" if !KUNIT_ALL_TESTS
|
||||
depends on KUNIT
|
||||
default KUNIT_ALL_TESTS || CRYPTO_SELFTESTS
|
||||
select CRYPTO_LIB_BENCHMARK_VISIBLE
|
||||
# No need to select 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_CURVE25519_KUNIT_TEST
|
||||
tristate "KUnit tests for Curve25519" if !KUNIT_ALL_TESTS
|
||||
depends on KUNIT
|
||||
default KUNIT_ALL_TESTS || CRYPTO_SELFTESTS
|
||||
select CRYPTO_LIB_BENCHMARK_VISIBLE
|
||||
select CRYPTO_LIB_CURVE25519
|
||||
help
|
||||
KUnit tests for the Curve25519 Diffie-Hellman function.
|
||||
|
||||
config CRYPTO_LIB_MD5_KUNIT_TEST
|
||||
tristate "KUnit tests for MD5" if !KUNIT_ALL_TESTS
|
||||
depends on KUNIT
|
||||
default KUNIT_ALL_TESTS || CRYPTO_SELFTESTS
|
||||
select CRYPTO_LIB_BENCHMARK_VISIBLE
|
||||
select CRYPTO_LIB_MD5
|
||||
help
|
||||
KUnit tests for the MD5 cryptographic hash function and its
|
||||
corresponding HMAC.
|
||||
|
||||
config CRYPTO_LIB_POLY1305_KUNIT_TEST
|
||||
tristate "KUnit tests for Poly1305" if !KUNIT_ALL_TESTS
|
||||
depends on KUNIT
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
# SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
obj-$(CONFIG_CRYPTO_LIB_BLAKE2S_KUNIT_TEST) += blake2s_kunit.o
|
||||
obj-$(CONFIG_CRYPTO_LIB_CURVE25519_KUNIT_TEST) += curve25519_kunit.o
|
||||
obj-$(CONFIG_CRYPTO_LIB_MD5_KUNIT_TEST) += md5_kunit.o
|
||||
obj-$(CONFIG_CRYPTO_LIB_POLY1305_KUNIT_TEST) += poly1305_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
|
||||
|
||||
238
lib/crypto/tests/blake2s-testvecs.h
Normal file
238
lib/crypto/tests/blake2s-testvecs.h
Normal file
@@ -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,
|
||||
};
|
||||
134
lib/crypto/tests/blake2s_kunit.c
Normal file
134
lib/crypto/tests/blake2s_kunit.c
Normal file
@@ -0,0 +1,134 @@
|
||||
// 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(out, data, NULL, BLAKE2S_HASH_SIZE, len, 0);
|
||||
}
|
||||
|
||||
static void blake2s_init_default(struct blake2s_state *state)
|
||||
{
|
||||
blake2s_init(state, 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_state
|
||||
#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_state main_state;
|
||||
u8 main_hash[BLAKE2S_HASH_SIZE];
|
||||
|
||||
rand_bytes_seeded_from_len(data, data_len);
|
||||
blake2s_init(&main_state, 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(hash, data, key, out_len, data_len, key_len);
|
||||
blake2s_update(&main_state, hash, out_len);
|
||||
}
|
||||
}
|
||||
blake2s_final(&main_state, 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_state state;
|
||||
|
||||
rand_bytes(key, key_len);
|
||||
memcpy(guarded_key, key, key_len);
|
||||
|
||||
blake2s(hash1, test_buf, key,
|
||||
BLAKE2S_HASH_SIZE, data_len, key_len);
|
||||
blake2s(hash2, test_buf, guarded_key,
|
||||
BLAKE2S_HASH_SIZE, data_len, key_len);
|
||||
KUNIT_ASSERT_MEMEQ(test, hash1, hash2, BLAKE2S_HASH_SIZE);
|
||||
|
||||
blake2s_init_key(&state, BLAKE2S_HASH_SIZE,
|
||||
guarded_key, key_len);
|
||||
blake2s_update(&state, test_buf, data_len);
|
||||
blake2s_final(&state, 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(hash, test_buf, NULL, out_len, data_len, 0);
|
||||
blake2s(guarded_hash, test_buf, NULL, out_len, data_len, 0);
|
||||
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");
|
||||
1363
lib/crypto/tests/curve25519_kunit.c
Normal file
1363
lib/crypto/tests/curve25519_kunit.c
Normal file
File diff suppressed because it is too large
Load Diff
186
lib/crypto/tests/md5-testvecs.h
Normal file
186
lib/crypto/tests/md5-testvecs.h
Normal 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
lib/crypto/tests/md5_kunit.c
Normal file
39
lib/crypto/tests/md5_kunit.c
Normal 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");
|
||||
Reference in New Issue
Block a user