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 	unsigned int test_num = 0, i;
10 	union noise_counter counter;
11 	bool success = true;
12 
13 #define T_INIT do {                                               \
14 		memset(&counter, 0, sizeof(union noise_counter)); \
15 		spin_lock_init(&counter.receive.lock);            \
16 	} while (0)
17 #define T_LIM (COUNTER_WINDOW_SIZE + 1)
18 #define T(n, v) do {                                                  \
19 		++test_num;                                           \
20 		if (counter_validate(&counter, n) != (v)) {           \
21 			pr_err("nonce counter self-test %u: FAIL\n",  \
22 			       test_num);                             \
23 			success = false;                              \
24 		}                                                     \
25 	} while (0)
26 
27 	T_INIT;
28 	/*  1 */ T(0, true);
29 	/*  2 */ T(1, true);
30 	/*  3 */ T(1, false);
31 	/*  4 */ T(9, true);
32 	/*  5 */ T(8, true);
33 	/*  6 */ T(7, true);
34 	/*  7 */ T(7, false);
35 	/*  8 */ T(T_LIM, true);
36 	/*  9 */ T(T_LIM - 1, true);
37 	/* 10 */ T(T_LIM - 1, false);
38 	/* 11 */ T(T_LIM - 2, true);
39 	/* 12 */ T(2, true);
40 	/* 13 */ T(2, false);
41 	/* 14 */ T(T_LIM + 16, true);
42 	/* 15 */ T(3, false);
43 	/* 16 */ T(T_LIM + 16, false);
44 	/* 17 */ T(T_LIM * 4, true);
45 	/* 18 */ T(T_LIM * 4 - (T_LIM - 1), true);
46 	/* 19 */ T(10, false);
47 	/* 20 */ T(T_LIM * 4 - T_LIM, false);
48 	/* 21 */ T(T_LIM * 4 - (T_LIM + 1), false);
49 	/* 22 */ T(T_LIM * 4 - (T_LIM - 2), true);
50 	/* 23 */ T(T_LIM * 4 + 1 - T_LIM, false);
51 	/* 24 */ T(0, false);
52 	/* 25 */ T(REJECT_AFTER_MESSAGES, false);
53 	/* 26 */ T(REJECT_AFTER_MESSAGES - 1, true);
54 	/* 27 */ T(REJECT_AFTER_MESSAGES, false);
55 	/* 28 */ T(REJECT_AFTER_MESSAGES - 1, false);
56 	/* 29 */ T(REJECT_AFTER_MESSAGES - 2, true);
57 	/* 30 */ T(REJECT_AFTER_MESSAGES + 1, false);
58 	/* 31 */ T(REJECT_AFTER_MESSAGES + 2, false);
59 	/* 32 */ T(REJECT_AFTER_MESSAGES - 2, false);
60 	/* 33 */ T(REJECT_AFTER_MESSAGES - 3, true);
61 	/* 34 */ T(0, false);
62 
63 	T_INIT;
64 	for (i = 1; i <= COUNTER_WINDOW_SIZE; ++i)
65 		T(i, true);
66 	T(0, true);
67 	T(0, false);
68 
69 	T_INIT;
70 	for (i = 2; i <= COUNTER_WINDOW_SIZE + 1; ++i)
71 		T(i, true);
72 	T(1, true);
73 	T(0, false);
74 
75 	T_INIT;
76 	for (i = COUNTER_WINDOW_SIZE + 1; i-- > 0;)
77 		T(i, true);
78 
79 	T_INIT;
80 	for (i = COUNTER_WINDOW_SIZE + 2; i-- > 1;)
81 		T(i, true);
82 	T(0, false);
83 
84 	T_INIT;
85 	for (i = COUNTER_WINDOW_SIZE + 1; i-- > 1;)
86 		T(i, true);
87 	T(COUNTER_WINDOW_SIZE + 1, true);
88 	T(0, false);
89 
90 	T_INIT;
91 	for (i = COUNTER_WINDOW_SIZE + 1; i-- > 1;)
92 		T(i, true);
93 	T(0, true);
94 	T(COUNTER_WINDOW_SIZE + 1, true);
95 
96 #undef T
97 #undef T_LIM
98 #undef T_INIT
99 
100 	if (success)
101 		pr_info("nonce counter self-tests: pass\n");
102 	return success;
103 }
104 #endif
105