xref: /linux/drivers/net/wireguard/selftest/counter.c (revision 58d416351e6df1a41d415958ccdd8eb9c2173fed)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2015-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
4  */
5 
6 #ifdef DEBUG
7 bool __init wg_packet_counter_selftest(void)
8 {
9 	struct noise_replay_counter *counter;
10 	unsigned int test_num = 0, i;
11 	bool success = true;
12 
13 	counter = kmalloc(sizeof(*counter), GFP_KERNEL);
14 	if (unlikely(!counter)) {
15 		pr_err("nonce counter self-test malloc: FAIL\n");
16 		return false;
17 	}
18 
19 #define T_INIT do {                                    \
20 		memset(counter, 0, sizeof(*counter));  \
21 		spin_lock_init(&counter->lock);        \
22 	} while (0)
23 #define T_LIM (COUNTER_WINDOW_SIZE + 1)
24 #define T(n, v) do {                                                  \
25 		++test_num;                                           \
26 		if (counter_validate(counter, n) != (v)) {            \
27 			pr_err("nonce counter self-test %u: FAIL\n",  \
28 			       test_num);                             \
29 			success = false;                              \
30 		}                                                     \
31 	} while (0)
32 
33 	T_INIT;
34 	/*  1 */ T(0, true);
35 	/*  2 */ T(1, true);
36 	/*  3 */ T(1, false);
37 	/*  4 */ T(9, true);
38 	/*  5 */ T(8, true);
39 	/*  6 */ T(7, true);
40 	/*  7 */ T(7, false);
41 	/*  8 */ T(T_LIM, true);
42 	/*  9 */ T(T_LIM - 1, true);
43 	/* 10 */ T(T_LIM - 1, false);
44 	/* 11 */ T(T_LIM - 2, true);
45 	/* 12 */ T(2, true);
46 	/* 13 */ T(2, false);
47 	/* 14 */ T(T_LIM + 16, true);
48 	/* 15 */ T(3, false);
49 	/* 16 */ T(T_LIM + 16, false);
50 	/* 17 */ T(T_LIM * 4, true);
51 	/* 18 */ T(T_LIM * 4 - (T_LIM - 1), true);
52 	/* 19 */ T(10, false);
53 	/* 20 */ T(T_LIM * 4 - T_LIM, false);
54 	/* 21 */ T(T_LIM * 4 - (T_LIM + 1), false);
55 	/* 22 */ T(T_LIM * 4 - (T_LIM - 2), true);
56 	/* 23 */ T(T_LIM * 4 + 1 - T_LIM, false);
57 	/* 24 */ T(0, false);
58 	/* 25 */ T(REJECT_AFTER_MESSAGES, false);
59 	/* 26 */ T(REJECT_AFTER_MESSAGES - 1, true);
60 	/* 27 */ T(REJECT_AFTER_MESSAGES, false);
61 	/* 28 */ T(REJECT_AFTER_MESSAGES - 1, false);
62 	/* 29 */ T(REJECT_AFTER_MESSAGES - 2, true);
63 	/* 30 */ T(REJECT_AFTER_MESSAGES + 1, false);
64 	/* 31 */ T(REJECT_AFTER_MESSAGES + 2, false);
65 	/* 32 */ T(REJECT_AFTER_MESSAGES - 2, false);
66 	/* 33 */ T(REJECT_AFTER_MESSAGES - 3, true);
67 	/* 34 */ T(0, false);
68 
69 	T_INIT;
70 	for (i = 1; i <= COUNTER_WINDOW_SIZE; ++i)
71 		T(i, true);
72 	T(0, true);
73 	T(0, false);
74 
75 	T_INIT;
76 	for (i = 2; i <= COUNTER_WINDOW_SIZE + 1; ++i)
77 		T(i, true);
78 	T(1, true);
79 	T(0, false);
80 
81 	T_INIT;
82 	for (i = COUNTER_WINDOW_SIZE + 1; i-- > 0;)
83 		T(i, true);
84 
85 	T_INIT;
86 	for (i = COUNTER_WINDOW_SIZE + 2; i-- > 1;)
87 		T(i, true);
88 	T(0, false);
89 
90 	T_INIT;
91 	for (i = COUNTER_WINDOW_SIZE + 1; i-- > 1;)
92 		T(i, true);
93 	T(COUNTER_WINDOW_SIZE + 1, true);
94 	T(0, false);
95 
96 	T_INIT;
97 	for (i = COUNTER_WINDOW_SIZE + 1; i-- > 1;)
98 		T(i, true);
99 	T(0, true);
100 	T(COUNTER_WINDOW_SIZE + 1, true);
101 
102 #undef T
103 #undef T_LIM
104 #undef T_INIT
105 
106 	if (success)
107 		pr_info("nonce counter self-tests: pass\n");
108 	kfree(counter);
109 	return success;
110 }
111 #endif
112