18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci#include <kunit/test.h>
38c2ecf20Sopenharmony_ci
48c2ecf20Sopenharmony_ci#include "protocol.h"
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_cistruct test_case {
78c2ecf20Sopenharmony_ci	char *key;
88c2ecf20Sopenharmony_ci	char *msg;
98c2ecf20Sopenharmony_ci	char *result;
108c2ecf20Sopenharmony_ci};
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci/* we can't reuse RFC 4231 test vectors, as we have constraint on the
138c2ecf20Sopenharmony_ci * input and key size.
148c2ecf20Sopenharmony_ci */
158c2ecf20Sopenharmony_cistatic struct test_case tests[] = {
168c2ecf20Sopenharmony_ci	{
178c2ecf20Sopenharmony_ci		.key = "0b0b0b0b0b0b0b0b",
188c2ecf20Sopenharmony_ci		.msg = "48692054",
198c2ecf20Sopenharmony_ci		.result = "8385e24fb4235ac37556b6b886db106284a1da671699f46db1f235ec622dcafa",
208c2ecf20Sopenharmony_ci	},
218c2ecf20Sopenharmony_ci	{
228c2ecf20Sopenharmony_ci		.key = "aaaaaaaaaaaaaaaa",
238c2ecf20Sopenharmony_ci		.msg = "dddddddd",
248c2ecf20Sopenharmony_ci		.result = "2c5e219164ff1dca1c4a92318d847bb6b9d44492984e1eb71aff9022f71046e9",
258c2ecf20Sopenharmony_ci	},
268c2ecf20Sopenharmony_ci	{
278c2ecf20Sopenharmony_ci		.key = "0102030405060708",
288c2ecf20Sopenharmony_ci		.msg = "cdcdcdcd",
298c2ecf20Sopenharmony_ci		.result = "e73b9ba9969969cefb04aa0d6df18ec2fcc075b6f23b4d8c4da736a5dbbc6e7d",
308c2ecf20Sopenharmony_ci	},
318c2ecf20Sopenharmony_ci};
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_cistatic void mptcp_crypto_test_basic(struct kunit *test)
348c2ecf20Sopenharmony_ci{
358c2ecf20Sopenharmony_ci	char hmac[32], hmac_hex[65];
368c2ecf20Sopenharmony_ci	u32 nonce1, nonce2;
378c2ecf20Sopenharmony_ci	u64 key1, key2;
388c2ecf20Sopenharmony_ci	u8 msg[8];
398c2ecf20Sopenharmony_ci	int i, j;
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(tests); ++i) {
428c2ecf20Sopenharmony_ci		/* mptcp hmap will convert to be before computing the hmac */
438c2ecf20Sopenharmony_ci		key1 = be64_to_cpu(*((__be64 *)&tests[i].key[0]));
448c2ecf20Sopenharmony_ci		key2 = be64_to_cpu(*((__be64 *)&tests[i].key[8]));
458c2ecf20Sopenharmony_ci		nonce1 = be32_to_cpu(*((__be32 *)&tests[i].msg[0]));
468c2ecf20Sopenharmony_ci		nonce2 = be32_to_cpu(*((__be32 *)&tests[i].msg[4]));
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci		put_unaligned_be32(nonce1, &msg[0]);
498c2ecf20Sopenharmony_ci		put_unaligned_be32(nonce2, &msg[4]);
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci		mptcp_crypto_hmac_sha(key1, key2, msg, 8, hmac);
528c2ecf20Sopenharmony_ci		for (j = 0; j < 32; ++j)
538c2ecf20Sopenharmony_ci			sprintf(&hmac_hex[j << 1], "%02x", hmac[j] & 0xff);
548c2ecf20Sopenharmony_ci		hmac_hex[64] = 0;
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci		KUNIT_EXPECT_STREQ(test, &hmac_hex[0], tests[i].result);
578c2ecf20Sopenharmony_ci	}
588c2ecf20Sopenharmony_ci}
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_cistatic struct kunit_case mptcp_crypto_test_cases[] = {
618c2ecf20Sopenharmony_ci	KUNIT_CASE(mptcp_crypto_test_basic),
628c2ecf20Sopenharmony_ci	{}
638c2ecf20Sopenharmony_ci};
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_cistatic struct kunit_suite mptcp_crypto_suite = {
668c2ecf20Sopenharmony_ci	.name = "mptcp-crypto",
678c2ecf20Sopenharmony_ci	.test_cases = mptcp_crypto_test_cases,
688c2ecf20Sopenharmony_ci};
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_cikunit_test_suite(mptcp_crypto_suite);
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
73