1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2019 Philippe Reynes <philippe.reynes@softathome.com>
5 * Unit tests for aes functions
11 #include <uboot_aes.h>
13 #include <test/test.h>
16 #define TEST_AES_ONE_BLOCK 0
17 #define TEST_AES_CBC_CHAIN 1
26 static struct test_aes_s test_aes[] = {
27 { AES128_KEY_LENGTH, AES128_EXPAND_KEY_LENGTH, TEST_AES_ONE_BLOCK, 1 },
28 { AES128_KEY_LENGTH, AES128_EXPAND_KEY_LENGTH, TEST_AES_CBC_CHAIN, 16 },
31 static void rand_buf(u8 *buf, int size)
35 for (i = 0; i < size; i++)
36 buf[i] = rand() & 0xff;
39 static int lib_test_aes_one_block(struct unit_test_state *uts, int key_len,
40 u8 *key_exp, u8 *iv, int num_block,
41 u8 *nocipher, u8 *ciphered, u8 *uncipher)
43 aes_encrypt(key_len, nocipher, key_exp, ciphered);
44 aes_decrypt(key_len, ciphered, key_exp, uncipher);
46 ut_asserteq_mem(nocipher, uncipher, AES_BLOCK_LENGTH);
48 /* corrupt the expanded key */
50 aes_decrypt(key_len, ciphered, key_exp, uncipher);
51 ut_assertf(memcmp(nocipher, uncipher, AES_BLOCK_LENGTH),
52 "nocipher and uncipher should be different\n");
57 static int lib_test_aes_cbc_chain(struct unit_test_state *uts, int key_len,
58 u8 *key_exp, u8 *iv, int num_block,
59 u8 *nocipher, u8 *ciphered, u8 *uncipher)
61 aes_cbc_encrypt_blocks(key_len, key_exp, iv,
62 nocipher, ciphered, num_block);
63 aes_cbc_decrypt_blocks(key_len, key_exp, iv,
64 ciphered, uncipher, num_block);
66 ut_asserteq_mem(nocipher, uncipher, num_block * AES_BLOCK_LENGTH);
68 /* corrupt the expanded key */
70 aes_cbc_decrypt_blocks(key_len, key_exp, iv,
71 ciphered, uncipher, num_block);
72 ut_assertf(memcmp(nocipher, uncipher, num_block * AES_BLOCK_LENGTH),
73 "nocipher and uncipher should be different\n");
78 static int _lib_test_aes_run(struct unit_test_state *uts, int key_len,
79 int key_exp_len, int type, int num_block)
81 u8 *key, *key_exp, *iv;
82 u8 *nocipher, *ciphered, *uncipher;
85 /* Allocate all the buffer */
86 key = malloc(key_len);
87 ut_assertnonnull(key);
88 key_exp = malloc(key_exp_len);
89 ut_assertnonnull(key_exp);
90 iv = malloc(AES_BLOCK_LENGTH);
92 nocipher = malloc(num_block * AES_BLOCK_LENGTH);
93 ut_assertnonnull(nocipher);
94 ciphered = malloc((num_block + 1) * AES_BLOCK_LENGTH);
95 ut_assertnonnull(ciphered);
96 uncipher = malloc((num_block + 1) * AES_BLOCK_LENGTH);
97 ut_assertnonnull(uncipher);
99 /* Initialize all buffer */
100 rand_buf(key, key_len);
101 rand_buf(iv, AES_BLOCK_LENGTH);
102 rand_buf(nocipher, num_block * AES_BLOCK_LENGTH);
103 memset(ciphered, 0, (num_block + 1) * AES_BLOCK_LENGTH);
104 memset(uncipher, 0, (num_block + 1) * AES_BLOCK_LENGTH);
107 aes_expand_key(key, key_len, key_exp);
109 /* Encrypt and decrypt */
111 case TEST_AES_ONE_BLOCK:
112 ret = lib_test_aes_one_block(uts, key_len, key_exp, iv,
116 case TEST_AES_CBC_CHAIN:
117 ret = lib_test_aes_cbc_chain(uts, key_len, key_exp, iv,
122 printf("%s: unknown type (type=%d)\n", __func__, type);
126 /* Free all the data */
137 static int lib_test_aes_run(struct unit_test_state *uts,
138 struct test_aes_s *test)
140 int key_len = test->key_len;
141 int key_exp_len = test->key_exp_len;
142 int type = test->type;
143 int num_block = test->num_block;
145 return _lib_test_aes_run(uts, key_len, key_exp_len,
149 static int lib_test_aes(struct unit_test_state *uts)
153 for (i = 0; i < ARRAY_SIZE(test_aes); i++) {
154 ret = lib_test_aes_run(uts, &test_aes[i]);
162 LIB_TEST(lib_test_aes, 0);