1 // SPDX-License-Identifier: GPL-2.0+
5 * Unit tests for aes functions
12 #include <uboot_aes.h>
14 #include <test/test.h>
17 #define TEST_AES_ONE_BLOCK 0
18 #define TEST_AES_CBC_CHAIN 1
27 static struct test_aes_s test_aes[] = {
28 { AES128_KEY_LENGTH, AES128_EXPAND_KEY_LENGTH, TEST_AES_ONE_BLOCK, 1 },
29 { AES128_KEY_LENGTH, AES128_EXPAND_KEY_LENGTH, TEST_AES_CBC_CHAIN, 16 },
30 { AES192_KEY_LENGTH, AES192_EXPAND_KEY_LENGTH, TEST_AES_ONE_BLOCK, 1 },
31 { AES192_KEY_LENGTH, AES192_EXPAND_KEY_LENGTH, TEST_AES_CBC_CHAIN, 16 },
32 { AES256_KEY_LENGTH, AES256_EXPAND_KEY_LENGTH, TEST_AES_ONE_BLOCK, 1 },
33 { AES256_KEY_LENGTH, AES256_EXPAND_KEY_LENGTH, TEST_AES_CBC_CHAIN, 16 },
36 static void rand_buf(u8 *buf, int size)
40 for (i = 0; i < size; i++)
41 buf[i] = rand() & 0xff;
44 static int lib_test_aes_one_block(struct unit_test_state *uts, int key_len,
45 u8 *key_exp, u8 *iv, int num_block,
46 u8 *nocipher, u8 *ciphered, u8 *uncipher)
48 aes_encrypt(key_len, nocipher, key_exp, ciphered);
49 aes_decrypt(key_len, ciphered, key_exp, uncipher);
51 ut_asserteq_mem(nocipher, uncipher, AES_BLOCK_LENGTH);
53 /* corrupt the expanded key */
55 aes_decrypt(key_len, ciphered, key_exp, uncipher);
56 ut_assertf(memcmp(nocipher, uncipher, AES_BLOCK_LENGTH),
57 "nocipher and uncipher should be different\n");
62 static int lib_test_aes_cbc_chain(struct unit_test_state *uts, int key_len,
63 u8 *key_exp, u8 *iv, int num_block,
64 u8 *nocipher, u8 *ciphered, u8 *uncipher)
66 aes_cbc_encrypt_blocks(key_len, key_exp, iv,
67 nocipher, ciphered, num_block);
68 aes_cbc_decrypt_blocks(key_len, key_exp, iv,
69 ciphered, uncipher, num_block);
71 ut_asserteq_mem(nocipher, uncipher, num_block * AES_BLOCK_LENGTH);
73 /* corrupt the expanded key */
75 aes_cbc_decrypt_blocks(key_len, key_exp, iv,
76 ciphered, uncipher, num_block);
77 ut_assertf(memcmp(nocipher, uncipher, num_block * AES_BLOCK_LENGTH),
78 "nocipher and uncipher should be different\n");
83 static int _lib_test_aes_run(struct unit_test_state *uts, int key_len,
84 int key_exp_len, int type, int num_block)
86 u8 *key, *key_exp, *iv;
87 u8 *nocipher, *ciphered, *uncipher;
90 /* Allocate all the buffer */
91 key = malloc(key_len);
92 key_exp = malloc(key_exp_len);
93 iv = malloc(AES_BLOCK_LENGTH);
94 nocipher = malloc(num_block * AES_BLOCK_LENGTH);
95 ciphered = malloc((num_block + 1) * AES_BLOCK_LENGTH);
96 uncipher = malloc((num_block + 1) * AES_BLOCK_LENGTH);
98 if (!key || !key_exp || !iv || !nocipher || !ciphered || !uncipher) {
99 printf("%s: can't allocate memory\n", __func__);
104 /* Initialize all buffer */
105 rand_buf(key, key_len);
106 rand_buf(iv, AES_BLOCK_LENGTH);
107 rand_buf(nocipher, num_block * AES_BLOCK_LENGTH);
108 memset(ciphered, 0, (num_block + 1) * AES_BLOCK_LENGTH);
109 memset(uncipher, 0, (num_block + 1) * AES_BLOCK_LENGTH);
112 aes_expand_key(key, key_len, key_exp);
114 /* Encrypt and decrypt */
116 case TEST_AES_ONE_BLOCK:
117 ret = lib_test_aes_one_block(uts, key_len, key_exp, iv,
121 case TEST_AES_CBC_CHAIN:
122 ret = lib_test_aes_cbc_chain(uts, key_len, key_exp, iv,
127 printf("%s: unknown type (type=%d)\n", __func__, type);
132 /* Free all the data */
143 static int lib_test_aes_run(struct unit_test_state *uts,
144 struct test_aes_s *test)
146 int key_len = test->key_len;
147 int key_exp_len = test->key_exp_len;
148 int type = test->type;
149 int num_block = test->num_block;
151 return _lib_test_aes_run(uts, key_len, key_exp_len,
155 static int lib_test_aes(struct unit_test_state *uts)
159 for (i = 0; i < ARRAY_SIZE(test_aes); i++) {
160 ret = lib_test_aes_run(uts, &test_aes[i]);
168 LIB_TEST(lib_test_aes, 0);