| 1 |
|
| 2 | |
| 3 | * Test utility functions shared by the crypto library tests.
|
| 4 | *
|
| 5 | * For now this is simply a header that's included into the KUnit test suites
|
| 6 | * that need it. If this gets too large it could be made its own translation
|
| 7 | * unit and libcrypto_test_utils module, but that seems overkill for now.
|
| 8 |
|
| 9 | #ifndef LIB_CRYPTO_TEST_UTILS_H
|
| 10 | #define LIB_CRYPTO_TEST_UTILS_H
|
| 11 |
|
| 12 | #include <kunit/test.h>
|
| 13 | #include <linux/math.h>
|
| 14 | #include <linux/minmax.h>
|
| 15 | #include <linux/string.h>
|
| 16 | #include <linux/vmalloc.h>
|
| 17 |
|
| 18 | static u64 random_seed;
|
| 19 |
|
| 20 | static __maybe_unused void action_free_guarded_buf(void *buf)
|
| 21 | {
|
| 22 | vfree(buf);
|
| 23 | }
|
| 24 |
|
| 25 | |
| 26 | * Allocate a KUnit-managed buffer that has length @size bytes (> 0) immediately
|
| 27 | * followed by an unmapped page, and assert that the allocation succeeds.
|
| 28 |
|
| 29 | static __maybe_unused void *alloc_guarded_buf(struct kunit *test, size_t size)
|
| 30 | {
|
| 31 | size_t full_size = round_up(size, PAGE_SIZE);
|
| 32 | void *buf = vmalloc(full_size);
|
| 33 |
|
| 34 | KUNIT_ASSERT_NOT_NULL(test, buf);
|
| 35 | KUNIT_ASSERT_EQ(test, 0,
|
| 36 | kunit_add_action_or_reset(test, action_free_guarded_buf,
|
| 37 | buf));
|
| 38 | return buf + full_size - size;
|
| 39 | }
|
| 40 |
|
| 41 | static __maybe_unused void *alloc_buf(struct kunit *test, size_t size)
|
| 42 | {
|
| 43 | void *buf = kunit_kmalloc(test, size, GFP_KERNEL);
|
| 44 |
|
| 45 | KUNIT_ASSERT_NOT_NULL(test, buf);
|
| 46 | return buf;
|
| 47 | }
|
| 48 |
|
| 49 | static __maybe_unused void *memdup_buf(struct kunit *test, const void *src,
|
| 50 | size_t size)
|
| 51 | {
|
| 52 | void *dst = alloc_buf(test, size);
|
| 53 |
|
| 54 | return memcpy(dst, src, size);
|
| 55 | }
|
| 56 |
|
| 57 | |
| 58 | * This is a simple linear congruential generator. It is used only for testing,
|
| 59 | * which does not require cryptographically secure random numbers. A hard-coded
|
| 60 | * algorithm is used instead of <linux/prandom.h> so that it matches the
|
| 61 | * algorithm used by the test vector generation script. This allows the input
|
| 62 | * data in random test vectors to be concisely stored as just the seed.
|
| 63 |
|
| 64 | static __maybe_unused u32 rand32(void)
|
| 65 | {
|
| 66 | random_seed = (random_seed * 25214903917 + 11) & ((1ULL << 48) - 1);
|
| 67 | return random_seed >> 16;
|
| 68 | }
|
| 69 |
|
| 70 | static __maybe_unused void rand_bytes(u8 *out, size_t len)
|
| 71 | {
|
| 72 | for (size_t i = 0; i < len; i++)
|
| 73 | out[i] = rand32();
|
| 74 | }
|
| 75 |
|
| 76 | static __maybe_unused void rand_bytes_seeded_from_len(u8 *out, size_t len)
|
| 77 | {
|
| 78 | random_seed = len;
|
| 79 | rand_bytes(out, len);
|
| 80 | }
|
| 81 |
|
| 82 | static __maybe_unused bool rand_bool(void)
|
| 83 | {
|
| 84 | return rand32() % 2;
|
| 85 | }
|
| 86 |
|
| 87 |
|
| 88 | static __maybe_unused size_t rand_length(size_t max_len)
|
| 89 | {
|
| 90 | size_t len;
|
| 91 |
|
| 92 | switch (rand32() % 3) {
|
| 93 | case 0:
|
| 94 | len = rand32() % 128;
|
| 95 | break;
|
| 96 | case 1:
|
| 97 | len = rand32() % 3072;
|
| 98 | break;
|
| 99 | default:
|
| 100 | len = rand32();
|
| 101 | break;
|
| 102 | }
|
| 103 | return len % (max_len + 1);
|
| 104 | }
|
| 105 |
|
| 106 | static __maybe_unused size_t rand_offset(size_t max_offset)
|
| 107 | {
|
| 108 | return min(rand32() % 128, max_offset);
|
| 109 | }
|
| 110 |
|
| 111 | #endif
|