blob: d972acfc15e4a5100b1771ca906e4b4182cbb162 [file] [log] [blame]
Jason A. Donenfeldfae9aac2017-01-08 13:54:00 +01001/* Test cases for siphash.c
2 *
3 * Copyright (C) 2016 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
4 *
5 * This file is provided under a dual BSD/GPLv2 license.
6 *
7 * SipHash: a fast short-input PRF
8 * https://131002.net/siphash/
9 *
10 * This implementation is specifically for SipHash2-4.
11 */
12
13#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
15#include <linux/siphash.h>
16#include <linux/kernel.h>
17#include <linux/string.h>
18#include <linux/errno.h>
19#include <linux/module.h>
20
21/* Test vectors taken from official reference source available at:
22 * https://131002.net/siphash/siphash24.c
23 */
24
25static const siphash_key_t test_key_siphash =
26 {{ 0x0706050403020100ULL, 0x0f0e0d0c0b0a0908ULL }};
27
28static const u64 test_vectors_siphash[64] = {
29 0x726fdb47dd0e0e31ULL, 0x74f839c593dc67fdULL, 0x0d6c8009d9a94f5aULL,
30 0x85676696d7fb7e2dULL, 0xcf2794e0277187b7ULL, 0x18765564cd99a68dULL,
31 0xcbc9466e58fee3ceULL, 0xab0200f58b01d137ULL, 0x93f5f5799a932462ULL,
32 0x9e0082df0ba9e4b0ULL, 0x7a5dbbc594ddb9f3ULL, 0xf4b32f46226bada7ULL,
33 0x751e8fbc860ee5fbULL, 0x14ea5627c0843d90ULL, 0xf723ca908e7af2eeULL,
34 0xa129ca6149be45e5ULL, 0x3f2acc7f57c29bdbULL, 0x699ae9f52cbe4794ULL,
35 0x4bc1b3f0968dd39cULL, 0xbb6dc91da77961bdULL, 0xbed65cf21aa2ee98ULL,
36 0xd0f2cbb02e3b67c7ULL, 0x93536795e3a33e88ULL, 0xa80c038ccd5ccec8ULL,
37 0xb8ad50c6f649af94ULL, 0xbce192de8a85b8eaULL, 0x17d835b85bbb15f3ULL,
38 0x2f2e6163076bcfadULL, 0xde4daaaca71dc9a5ULL, 0xa6a2506687956571ULL,
39 0xad87a3535c49ef28ULL, 0x32d892fad841c342ULL, 0x7127512f72f27cceULL,
40 0xa7f32346f95978e3ULL, 0x12e0b01abb051238ULL, 0x15e034d40fa197aeULL,
41 0x314dffbe0815a3b4ULL, 0x027990f029623981ULL, 0xcadcd4e59ef40c4dULL,
42 0x9abfd8766a33735cULL, 0x0e3ea96b5304a7d0ULL, 0xad0c42d6fc585992ULL,
43 0x187306c89bc215a9ULL, 0xd4a60abcf3792b95ULL, 0xf935451de4f21df2ULL,
44 0xa9538f0419755787ULL, 0xdb9acddff56ca510ULL, 0xd06c98cd5c0975ebULL,
45 0xe612a3cb9ecba951ULL, 0xc766e62cfcadaf96ULL, 0xee64435a9752fe72ULL,
46 0xa192d576b245165aULL, 0x0a8787bf8ecb74b2ULL, 0x81b3e73d20b49b6fULL,
47 0x7fa8220ba3b2eceaULL, 0x245731c13ca42499ULL, 0xb78dbfaf3a8d83bdULL,
48 0xea1ad565322a1a0bULL, 0x60e61c23a3795013ULL, 0x6606d7e446282b93ULL,
49 0x6ca4ecb15c5f91e1ULL, 0x9f626da15c9625f3ULL, 0xe51b38608ef25f57ULL,
50 0x958a324ceb064572ULL
51};
52
53static int __init siphash_test_init(void)
54{
55 u8 in[64] __aligned(SIPHASH_ALIGNMENT);
56 u8 in_unaligned[65] __aligned(SIPHASH_ALIGNMENT);
57 u8 i;
58 int ret = 0;
59
60 for (i = 0; i < 64; ++i) {
61 in[i] = i;
62 in_unaligned[i + 1] = i;
63 if (siphash(in, i, &test_key_siphash) !=
64 test_vectors_siphash[i]) {
65 pr_info("siphash self-test aligned %u: FAIL\n", i + 1);
66 ret = -EINVAL;
67 }
68 if (siphash(in_unaligned + 1, i, &test_key_siphash) !=
69 test_vectors_siphash[i]) {
70 pr_info("siphash self-test unaligned %u: FAIL\n", i + 1);
71 ret = -EINVAL;
72 }
73 }
74 if (siphash_1u64(0x0706050403020100ULL, &test_key_siphash) !=
75 test_vectors_siphash[8]) {
76 pr_info("siphash self-test 1u64: FAIL\n");
77 ret = -EINVAL;
78 }
79 if (siphash_2u64(0x0706050403020100ULL, 0x0f0e0d0c0b0a0908ULL,
80 &test_key_siphash) != test_vectors_siphash[16]) {
81 pr_info("siphash self-test 2u64: FAIL\n");
82 ret = -EINVAL;
83 }
84 if (siphash_3u64(0x0706050403020100ULL, 0x0f0e0d0c0b0a0908ULL,
85 0x1716151413121110ULL, &test_key_siphash) !=
86 test_vectors_siphash[24]) {
87 pr_info("siphash self-test 3u64: FAIL\n");
88 ret = -EINVAL;
89 }
90 if (siphash_4u64(0x0706050403020100ULL, 0x0f0e0d0c0b0a0908ULL,
91 0x1716151413121110ULL, 0x1f1e1d1c1b1a1918ULL,
92 &test_key_siphash) != test_vectors_siphash[32]) {
93 pr_info("siphash self-test 4u64: FAIL\n");
94 ret = -EINVAL;
95 }
96 if (siphash_1u32(0x03020100U, &test_key_siphash) !=
97 test_vectors_siphash[4]) {
98 pr_info("siphash self-test 1u32: FAIL\n");
99 ret = -EINVAL;
100 }
101 if (siphash_2u32(0x03020100U, 0x07060504U, &test_key_siphash) !=
102 test_vectors_siphash[8]) {
103 pr_info("siphash self-test 2u32: FAIL\n");
104 ret = -EINVAL;
105 }
106 if (siphash_3u32(0x03020100U, 0x07060504U,
107 0x0b0a0908U, &test_key_siphash) !=
108 test_vectors_siphash[12]) {
109 pr_info("siphash self-test 3u32: FAIL\n");
110 ret = -EINVAL;
111 }
112 if (siphash_4u32(0x03020100U, 0x07060504U,
113 0x0b0a0908U, 0x0f0e0d0cU, &test_key_siphash) !=
114 test_vectors_siphash[16]) {
115 pr_info("siphash self-test 4u32: FAIL\n");
116 ret = -EINVAL;
117 }
118 if (!ret)
119 pr_info("self-tests: pass\n");
120 return ret;
121}
122
123static void __exit siphash_test_exit(void)
124{
125}
126
127module_init(siphash_test_init);
128module_exit(siphash_test_exit);
129
130MODULE_AUTHOR("Jason A. Donenfeld <Jason@zx2c4.com>");
131MODULE_LICENSE("Dual BSD/GPL");