Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include <kunit/test.h>
0003 
0004 #include "protocol.h"
0005 
0006 struct test_case {
0007     char *key;
0008     char *msg;
0009     char *result;
0010 };
0011 
0012 /* we can't reuse RFC 4231 test vectors, as we have constraint on the
0013  * input and key size.
0014  */
0015 static struct test_case tests[] = {
0016     {
0017         .key = "0b0b0b0b0b0b0b0b",
0018         .msg = "48692054",
0019         .result = "8385e24fb4235ac37556b6b886db106284a1da671699f46db1f235ec622dcafa",
0020     },
0021     {
0022         .key = "aaaaaaaaaaaaaaaa",
0023         .msg = "dddddddd",
0024         .result = "2c5e219164ff1dca1c4a92318d847bb6b9d44492984e1eb71aff9022f71046e9",
0025     },
0026     {
0027         .key = "0102030405060708",
0028         .msg = "cdcdcdcd",
0029         .result = "e73b9ba9969969cefb04aa0d6df18ec2fcc075b6f23b4d8c4da736a5dbbc6e7d",
0030     },
0031 };
0032 
0033 static void mptcp_crypto_test_basic(struct kunit *test)
0034 {
0035     char hmac[32], hmac_hex[65];
0036     u32 nonce1, nonce2;
0037     u64 key1, key2;
0038     u8 msg[8];
0039     int i, j;
0040 
0041     for (i = 0; i < ARRAY_SIZE(tests); ++i) {
0042         /* mptcp hmap will convert to be before computing the hmac */
0043         key1 = be64_to_cpu(*((__be64 *)&tests[i].key[0]));
0044         key2 = be64_to_cpu(*((__be64 *)&tests[i].key[8]));
0045         nonce1 = be32_to_cpu(*((__be32 *)&tests[i].msg[0]));
0046         nonce2 = be32_to_cpu(*((__be32 *)&tests[i].msg[4]));
0047 
0048         put_unaligned_be32(nonce1, &msg[0]);
0049         put_unaligned_be32(nonce2, &msg[4]);
0050 
0051         mptcp_crypto_hmac_sha(key1, key2, msg, 8, hmac);
0052         for (j = 0; j < 32; ++j)
0053             sprintf(&hmac_hex[j << 1], "%02x", hmac[j] & 0xff);
0054         hmac_hex[64] = 0;
0055 
0056         KUNIT_EXPECT_STREQ(test, &hmac_hex[0], tests[i].result);
0057     }
0058 }
0059 
0060 static struct kunit_case mptcp_crypto_test_cases[] = {
0061     KUNIT_CASE(mptcp_crypto_test_basic),
0062     {}
0063 };
0064 
0065 static struct kunit_suite mptcp_crypto_suite = {
0066     .name = "mptcp-crypto",
0067     .test_cases = mptcp_crypto_test_cases,
0068 };
0069 
0070 kunit_test_suite(mptcp_crypto_suite);
0071 
0072 MODULE_LICENSE("GPL");