1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Tests for the driver model AES API
4  *
5  * Copyright (c) 2025 Svyatoslav Ryhel <clamor95@gmail.com>
6  */
7 
8 #include <dm.h>
9 #include <dm/test.h>
10 #include <uboot_aes.h>
11 #include <test/test.h>
12 #include <test/ut.h>
13 
14 #define AES128_KEYSIZE		128
15 
dm_test_aes(struct unit_test_state * uts)16 static int dm_test_aes(struct unit_test_state *uts)
17 {
18 	struct udevice *dev;
19 	u8 test_key[AES128_KEY_LENGTH] = { 0x63, 0x68, 0x69, 0x63, 0x6b, 0x65, 0x6e, 0x20,
20 					   0x74, 0x65, 0x72, 0x69, 0x79, 0x61, 0x6b, 0x69 };
21 	u8 test_iv[AES128_KEY_LENGTH] = { 0 };
22 
23 	u8 test_input[AES_BLOCK_LENGTH] = { 0x49, 0x20, 0x77, 0x6f, 0x75, 0x6c, 0x64, 0x20,
24 					    0x6c, 0x69, 0x6b, 0x65, 0x20, 0x74, 0x68, 0x65 };
25 	u8 exp_output[AES_BLOCK_LENGTH] = { 0x97, 0x68, 0x72, 0x68, 0xd6, 0xec, 0xcc, 0xc0,
26 					    0xc0, 0x7b, 0x25, 0xe2, 0x5e, 0xcf, 0xe5, 0x84 };
27 	u8 exp_cmac[AES_BLOCK_LENGTH] = { 0xfc, 0x89, 0x20, 0xc8, 0x46, 0x97, 0xb1, 0x3d,
28 					  0x31, 0x2c, 0xc2, 0x49, 0x5c, 0x5a, 0x0b, 0x9f };
29 	u8 test_output[AES_BLOCK_LENGTH];
30 
31 	ut_assertok(uclass_first_device_err(UCLASS_AES, &dev));
32 
33 	/* software AES exposes 2 key slots */
34 	ut_asserteq(2, dm_aes_get_available_key_slots(dev));
35 
36 	ut_assertok(dm_aes_select_key_slot(dev, AES128_KEYSIZE, 0));
37 	ut_assertok(dm_aes_set_key_for_key_slot(dev, AES128_KEYSIZE, test_key, 0));
38 
39 	ut_assertok(dm_aes_ecb_encrypt(dev, test_input, test_output, 1));
40 	ut_assertok(memcmp(exp_output, test_output, 16));
41 
42 	ut_assertok(dm_aes_ecb_decrypt(dev, test_output, test_output, 1));
43 	ut_assertok(memcmp(test_input, test_output, 16));
44 
45 	ut_assertok(dm_aes_cbc_encrypt(dev, test_iv, test_input, test_output, 1));
46 	ut_assertok(memcmp(exp_output, test_output, 16));
47 
48 	ut_assertok(dm_aes_cbc_decrypt(dev, test_iv, test_output, test_output, 1));
49 	ut_assertok(memcmp(test_input, test_output, 16));
50 
51 	ut_assertok(dm_aes_cmac(dev, test_input, test_output, 1));
52 	ut_assertok(memcmp(exp_cmac, test_output, 16));
53 
54 	return 0;
55 }
56 
57 DM_TEST(dm_test_aes, UTF_SCAN_FDT);
58