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