#include <crypto/poly1305.h>
#include "poly1305-testvecs.h"
static u8 test_key[POLY1305_KEY_SIZE];
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);
}
#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);
}
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);
}
static void test_poly1305_reduction_edge_cases(struct kunit *test)
{
static const u8 key[POLY1305_KEY_SIZE] = { 1 };
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++) {
data[0] = -i;
memset(&data[1], 0xff, POLY1305_BLOCK_SIZE - 1);
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);
}
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");