xref: /linux/net/rxrpc/tests/rxrpc_kunit.c (revision f10e73dffd2abf51c5ba6f5dc724a0df690cd783)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Unit tests for RxRPC crypto functions
4  *
5  * Copyright 2026 Google LLC
6  */
7 #include "../ar-internal.h"
8 #include <kunit/test.h>
9 
10 struct fcrypt_pcbc_testvec {
11 	u8 key[FCRYPT_BSIZE];
12 	u8 iv[FCRYPT_BSIZE];
13 	const u8 *ptext; /* plaintext */
14 	const u8 *ctext; /* ciphertext */
15 	size_t nblocks; /* length of ptext and ctext in blocks */
16 };
17 
18 /* FCrypt-PCBC test vectors */
19 static const struct fcrypt_pcbc_testvec fcrypt_pcbc_testvecs[] = {
20 	{
21 		/* http://www.openafs.org/pipermail/openafs-devel/2000-December/005320.html */
22 		.key = "\x00\x00\x00\x00\x00\x00\x00\x00",
23 		.iv = "\x00\x00\x00\x00\x00\x00\x00\x00",
24 		.ptext = "\x00\x00\x00\x00\x00\x00\x00\x00",
25 		.ctext = "\x0E\x09\x00\xC7\x3E\xF7\xED\x41",
26 		.nblocks = 1,
27 	},
28 	{
29 		.key = "\x11\x44\x77\xAA\xDD\x00\x33\x66",
30 		.iv = "\x00\x00\x00\x00\x00\x00\x00\x00",
31 		.ptext = "\x12\x34\x56\x78\x9A\xBC\xDE\xF0",
32 		.ctext = "\xD8\xED\x78\x74\x77\xEC\x06\x80",
33 		.nblocks = 1,
34 	},
35 	{
36 		/* From Arla */
37 		.key = "\xf0\xe1\xd2\xc3\xb4\xa5\x96\x87",
38 		.iv = "\xfe\xdc\xba\x98\x76\x54\x32\x10",
39 		.ptext = "The quick brown fox jumps over the lazy dogs.\0\0",
40 		.ctext = "\x00\xf0\x0e\x11\x75\xe6\x23\x82"
41 			 "\xee\xac\x98\x62\x44\x51\xe4\x84"
42 			 "\xc3\x59\xd8\xaa\x64\x60\xae\xf7"
43 			 "\xd2\xd9\x13\x79\x72\xa3\x45\x03"
44 			 "\x23\xb5\x62\xd7\x0c\xf5\x27\xd1"
45 			 "\xf8\x91\x3c\xac\x44\x22\x92\xef",
46 		.nblocks = 6,
47 	},
48 	{
49 		.key = "\xfe\xdc\xba\x98\x76\x54\x32\x10",
50 		.iv = "\xf0\xe1\xd2\xc3\xb4\xa5\x96\x87",
51 		.ptext = "The quick brown fox jumps over the lazy dogs.\0\0",
52 		.ctext = "\xca\x90\xf5\x9d\xcb\xd4\xd2\x3c"
53 			 "\x01\x88\x7f\x3e\x31\x6e\x62\x9d"
54 			 "\xd8\xe0\x57\xa3\x06\x3a\x42\x58"
55 			 "\x2a\x28\xfe\x72\x52\x2f\xdd\xe0"
56 			 "\x19\x89\x09\x1c\x2a\x8e\x8c\x94"
57 			 "\xfc\xc7\x68\xe4\x88\xaa\xde\x0f",
58 		.nblocks = 6,
59 	}
60 };
61 
62 static void test_fcrypt_pcbc(struct kunit *test)
63 {
64 	u8 data[48];
65 
66 	for (size_t i = 0; i < ARRAY_SIZE(fcrypt_pcbc_testvecs); i++) {
67 		const struct fcrypt_pcbc_testvec *tv = &fcrypt_pcbc_testvecs[i];
68 		const size_t nblocks = tv->nblocks;
69 		const size_t len = nblocks * FCRYPT_BSIZE;
70 		struct fcrypt_key key;
71 
72 		KUNIT_ASSERT_GE(test, sizeof(data), len);
73 
74 		fcrypt_preparekey(&key, tv->key);
75 
76 		/* out-of-place encryption */
77 		fcrypt_pcbc_encrypt(&key, tv->iv, tv->ptext, data, nblocks);
78 		KUNIT_ASSERT_MEMEQ(test, tv->ctext, data, len);
79 
80 		/* in-place encryption */
81 		memcpy(data, tv->ptext, len);
82 		fcrypt_pcbc_encrypt(&key, tv->iv, data, data, nblocks);
83 		KUNIT_ASSERT_MEMEQ(test, tv->ctext, data, len);
84 
85 		/* out-of-place decryption */
86 		fcrypt_pcbc_decrypt(&key, tv->iv, tv->ctext, data, nblocks);
87 		KUNIT_ASSERT_MEMEQ(test, tv->ptext, data, len);
88 
89 		/* in-place decryption */
90 		memcpy(data, tv->ctext, len);
91 		fcrypt_pcbc_decrypt(&key, tv->iv, data, data, nblocks);
92 		KUNIT_ASSERT_MEMEQ(test, tv->ptext, data, len);
93 	}
94 }
95 
96 static struct kunit_case rxrpc_test_cases[] = {
97 	KUNIT_CASE(test_fcrypt_pcbc),
98 	{},
99 };
100 
101 static struct kunit_suite rxrpc_test_suite = {
102 	.name = "rxrpc",
103 	.test_cases = rxrpc_test_cases,
104 };
105 kunit_test_suite(rxrpc_test_suite);
106 
107 MODULE_DESCRIPTION("Unit tests for RxRPC crypto functions");
108 MODULE_IMPORT_NS("EXPORTED_FOR_KUNIT_TESTING");
109 MODULE_LICENSE("GPL");
110