Skip to content

Commit a00a582

Browse files
Paolo Abenidavem330
authored andcommitted
mptcp: move crypto test to KUNIT
currently MPTCP uses a custom hook to executed unit tests at boot time. Let's use the KUNIT framework instead. Additionally move the relevant code to a separate file and export the function needed by the test when self-tests are build as a module. Co-developed-by: Matthieu Baerts <[email protected]> Signed-off-by: Matthieu Baerts <[email protected]> Signed-off-by: Paolo Abeni <[email protected]> Reviewed-by: Mat Martineau <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 2c5ebd0 commit a00a582

File tree

4 files changed

+91
-67
lines changed

4 files changed

+91
-67
lines changed

net/mptcp/Kconfig

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,20 @@ config MPTCP_IPV6
1818
select IPV6
1919
default y
2020

21-
config MPTCP_HMAC_TEST
22-
bool "Tests for MPTCP HMAC implementation"
21+
endif
22+
23+
config MPTCP_KUNIT_TESTS
24+
tristate "This builds the MPTCP KUnit tests" if !KUNIT_ALL_TESTS
25+
select MPTCP
26+
depends on KUNIT
27+
default KUNIT_ALL_TESTS
2328
help
24-
This option enable boot time self-test for the HMAC implementation
25-
used by the MPTCP code
29+
Currently covers the MPTCP crypto helpers.
30+
Only useful for kernel devs running KUnit test harness and are not
31+
for inclusion into a production build.
2632

27-
Say N if you are unsure.
33+
For more information on KUnit and unit tests in general please refer
34+
to the KUnit documentation in Documentation/dev-tools/kunit/.
35+
36+
If unsure, say N.
2837

29-
endif

net/mptcp/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@ obj-$(CONFIG_MPTCP) += mptcp.o
33

44
mptcp-y := protocol.o subflow.o options.o token.o crypto.o ctrl.o pm.o diag.o \
55
mib.o pm_netlink.o
6+
7+
mptcp_crypto_test-objs := crypto_test.o
8+
obj-$(CONFIG_MPTCP_KUNIT_TESTS) += mptcp_crypto_test.o

net/mptcp/crypto.c

Lines changed: 2 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -87,65 +87,6 @@ void mptcp_crypto_hmac_sha(u64 key1, u64 key2, u8 *msg, int len, void *hmac)
8787
sha256_final(&state, (u8 *)hmac);
8888
}
8989

90-
#ifdef CONFIG_MPTCP_HMAC_TEST
91-
struct test_cast {
92-
char *key;
93-
char *msg;
94-
char *result;
95-
};
96-
97-
/* we can't reuse RFC 4231 test vectors, as we have constraint on the
98-
* input and key size.
99-
*/
100-
static struct test_cast tests[] = {
101-
{
102-
.key = "0b0b0b0b0b0b0b0b",
103-
.msg = "48692054",
104-
.result = "8385e24fb4235ac37556b6b886db106284a1da671699f46db1f235ec622dcafa",
105-
},
106-
{
107-
.key = "aaaaaaaaaaaaaaaa",
108-
.msg = "dddddddd",
109-
.result = "2c5e219164ff1dca1c4a92318d847bb6b9d44492984e1eb71aff9022f71046e9",
110-
},
111-
{
112-
.key = "0102030405060708",
113-
.msg = "cdcdcdcd",
114-
.result = "e73b9ba9969969cefb04aa0d6df18ec2fcc075b6f23b4d8c4da736a5dbbc6e7d",
115-
},
116-
};
117-
118-
static int __init test_mptcp_crypto(void)
119-
{
120-
char hmac[32], hmac_hex[65];
121-
u32 nonce1, nonce2;
122-
u64 key1, key2;
123-
u8 msg[8];
124-
int i, j;
125-
126-
for (i = 0; i < ARRAY_SIZE(tests); ++i) {
127-
/* mptcp hmap will convert to be before computing the hmac */
128-
key1 = be64_to_cpu(*((__be64 *)&tests[i].key[0]));
129-
key2 = be64_to_cpu(*((__be64 *)&tests[i].key[8]));
130-
nonce1 = be32_to_cpu(*((__be32 *)&tests[i].msg[0]));
131-
nonce2 = be32_to_cpu(*((__be32 *)&tests[i].msg[4]));
132-
133-
put_unaligned_be32(nonce1, &msg[0]);
134-
put_unaligned_be32(nonce2, &msg[4]);
135-
136-
mptcp_crypto_hmac_sha(key1, key2, msg, 8, hmac);
137-
for (j = 0; j < 32; ++j)
138-
sprintf(&hmac_hex[j << 1], "%02x", hmac[j] & 0xff);
139-
hmac_hex[64] = 0;
140-
141-
if (memcmp(hmac_hex, tests[i].result, 64))
142-
pr_err("test %d failed, got %s expected %s", i,
143-
hmac_hex, tests[i].result);
144-
else
145-
pr_info("test %d [ ok ]", i);
146-
}
147-
return 0;
148-
}
149-
150-
late_initcall(test_mptcp_crypto);
90+
#if IS_MODULE(CONFIG_MPTCP_KUNIT_TESTS)
91+
EXPORT_SYMBOL_GPL(mptcp_crypto_hmac_sha);
15192
#endif

net/mptcp/crypto_test.c

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
#include <kunit/test.h>
3+
4+
#include "protocol.h"
5+
6+
struct test_case {
7+
char *key;
8+
char *msg;
9+
char *result;
10+
};
11+
12+
/* we can't reuse RFC 4231 test vectors, as we have constraint on the
13+
* input and key size.
14+
*/
15+
static struct test_case tests[] = {
16+
{
17+
.key = "0b0b0b0b0b0b0b0b",
18+
.msg = "48692054",
19+
.result = "8385e24fb4235ac37556b6b886db106284a1da671699f46db1f235ec622dcafa",
20+
},
21+
{
22+
.key = "aaaaaaaaaaaaaaaa",
23+
.msg = "dddddddd",
24+
.result = "2c5e219164ff1dca1c4a92318d847bb6b9d44492984e1eb71aff9022f71046e9",
25+
},
26+
{
27+
.key = "0102030405060708",
28+
.msg = "cdcdcdcd",
29+
.result = "e73b9ba9969969cefb04aa0d6df18ec2fcc075b6f23b4d8c4da736a5dbbc6e7d",
30+
},
31+
};
32+
33+
static void mptcp_crypto_test_basic(struct kunit *test)
34+
{
35+
char hmac[32], hmac_hex[65];
36+
u32 nonce1, nonce2;
37+
u64 key1, key2;
38+
u8 msg[8];
39+
int i, j;
40+
41+
for (i = 0; i < ARRAY_SIZE(tests); ++i) {
42+
/* mptcp hmap will convert to be before computing the hmac */
43+
key1 = be64_to_cpu(*((__be64 *)&tests[i].key[0]));
44+
key2 = be64_to_cpu(*((__be64 *)&tests[i].key[8]));
45+
nonce1 = be32_to_cpu(*((__be32 *)&tests[i].msg[0]));
46+
nonce2 = be32_to_cpu(*((__be32 *)&tests[i].msg[4]));
47+
48+
put_unaligned_be32(nonce1, &msg[0]);
49+
put_unaligned_be32(nonce2, &msg[4]);
50+
51+
mptcp_crypto_hmac_sha(key1, key2, msg, 8, hmac);
52+
for (j = 0; j < 32; ++j)
53+
sprintf(&hmac_hex[j << 1], "%02x", hmac[j] & 0xff);
54+
hmac_hex[64] = 0;
55+
56+
KUNIT_EXPECT_STREQ(test, &hmac_hex[0], tests[i].result);
57+
}
58+
}
59+
60+
static struct kunit_case mptcp_crypto_test_cases[] = {
61+
KUNIT_CASE(mptcp_crypto_test_basic),
62+
{}
63+
};
64+
65+
static struct kunit_suite mptcp_crypto_suite = {
66+
.name = "mptcp-crypto",
67+
.test_cases = mptcp_crypto_test_cases,
68+
};
69+
70+
kunit_test_suite(mptcp_crypto_suite);
71+
72+
MODULE_LICENSE("GPL");

0 commit comments

Comments
 (0)