1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2015-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
7 bool __init
wg_packet_counter_selftest(void)
9 unsigned int test_num
= 0, i
;
10 union noise_counter counter
;
14 memset(&counter, 0, sizeof(union noise_counter)); \
15 spin_lock_init(&counter.receive.lock); \
17 #define T_LIM (COUNTER_WINDOW_SIZE + 1)
18 #define T(n, v) do { \
20 if (counter_validate(&counter, n) != (v)) { \
21 pr_err("nonce counter self-test %u: FAIL\n", \
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);
41 /* 14 */ T(T_LIM
+ 16, true);
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);
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);
64 for (i
= 1; i
<= COUNTER_WINDOW_SIZE
; ++i
)
70 for (i
= 2; i
<= COUNTER_WINDOW_SIZE
+ 1; ++i
)
76 for (i
= COUNTER_WINDOW_SIZE
+ 1; i
-- > 0;)
80 for (i
= COUNTER_WINDOW_SIZE
+ 2; i
-- > 1;)
85 for (i
= COUNTER_WINDOW_SIZE
+ 1; i
-- > 1;)
87 T(COUNTER_WINDOW_SIZE
+ 1, true);
91 for (i
= COUNTER_WINDOW_SIZE
+ 1; i
-- > 1;)
94 T(COUNTER_WINDOW_SIZE
+ 1, true);
101 pr_info("nonce counter self-tests: pass\n");