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 struct noise_replay_counter
*counter
;
10 unsigned int test_num
= 0, i
;
13 counter
= kmalloc(sizeof(*counter
), GFP_KERNEL
);
14 if (unlikely(!counter
)) {
15 pr_err("nonce counter self-test malloc: FAIL\n");
20 memset(counter, 0, sizeof(*counter)); \
21 spin_lock_init(&counter->lock); \
23 #define T_LIM (COUNTER_WINDOW_SIZE + 1)
24 #define T(n, v) do { \
26 if (counter_validate(counter, n) != (v)) { \
27 pr_err("nonce counter self-test %u: FAIL\n", \
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);
47 /* 14 */ T(T_LIM
+ 16, true);
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);
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);
70 for (i
= 1; i
<= COUNTER_WINDOW_SIZE
; ++i
)
76 for (i
= 2; i
<= COUNTER_WINDOW_SIZE
+ 1; ++i
)
82 for (i
= COUNTER_WINDOW_SIZE
+ 1; i
-- > 0;)
86 for (i
= COUNTER_WINDOW_SIZE
+ 2; i
-- > 1;)
91 for (i
= COUNTER_WINDOW_SIZE
+ 1; i
-- > 1;)
93 T(COUNTER_WINDOW_SIZE
+ 1, true);
97 for (i
= COUNTER_WINDOW_SIZE
+ 1; i
-- > 1;)
100 T(COUNTER_WINDOW_SIZE
+ 1, true);
107 pr_info("nonce counter self-tests: pass\n");