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 },
29 { AES192_KEY_LENGTH, AES192_EXPAND_KEY_LENGTH, TEST_AES_ONE_BLOCK, 1 },
30 { AES192_KEY_LENGTH, AES192_EXPAND_KEY_LENGTH, TEST_AES_CBC_CHAIN, 16 },
31 { AES256_KEY_LENGTH, AES256_EXPAND_KEY_LENGTH, TEST_AES_ONE_BLOCK, 1 },
32 { AES256_KEY_LENGTH, AES256_EXPAND_KEY_LENGTH, TEST_AES_CBC_CHAIN, 16 },
35 static void rand_buf(u8 *buf, int size)
39 for (i = 0; i < size; i++)
40 buf[i] = rand() & 0xff;
43 static int lib_test_aes_one_block(struct unit_test_state *uts, int key_len,
44 u8 *key_exp, u8 *iv, int num_block,
45 u8 *nocipher, u8 *ciphered, u8 *uncipher)
47 aes_encrypt(key_len, nocipher, key_exp, ciphered);
48 aes_decrypt(key_len, ciphered, key_exp, uncipher);
50 ut_asserteq_mem(nocipher, uncipher, AES_BLOCK_LENGTH);
52 /* corrupt the expanded key */
54 aes_decrypt(key_len, ciphered, key_exp, uncipher);
55 ut_assertf(memcmp(nocipher, uncipher, AES_BLOCK_LENGTH),
56 "nocipher and uncipher should be different\n");
61 static int lib_test_aes_cbc_chain(struct unit_test_state *uts, int key_len,
62 u8 *key_exp, u8 *iv, int num_block,
63 u8 *nocipher, u8 *ciphered, u8 *uncipher)
65 aes_cbc_encrypt_blocks(key_len, key_exp, iv,
66 nocipher, ciphered, num_block);
67 aes_cbc_decrypt_blocks(key_len, key_exp, iv,
68 ciphered, uncipher, num_block);
70 ut_asserteq_mem(nocipher, uncipher, num_block * AES_BLOCK_LENGTH);
72 /* corrupt the expanded key */
74 aes_cbc_decrypt_blocks(key_len, key_exp, iv,
75 ciphered, uncipher, num_block);
76 ut_assertf(memcmp(nocipher, uncipher, num_block * AES_BLOCK_LENGTH),
77 "nocipher and uncipher should be different\n");
82 static int _lib_test_aes_run(struct unit_test_state *uts, int key_len,
83 int key_exp_len, int type, int num_block)
85 u8 *key, *key_exp, *iv;
86 u8 *nocipher, *ciphered, *uncipher;
89 /* Allocate all the buffer */
90 key = malloc(key_len);
91 ut_assertnonnull(key);
92 key_exp = malloc(key_exp_len);
93 ut_assertnonnull(key_exp);
94 iv = malloc(AES_BLOCK_LENGTH);
96 nocipher = malloc(num_block * AES_BLOCK_LENGTH);
97 ut_assertnonnull(nocipher);
98 ciphered = malloc((num_block + 1) * AES_BLOCK_LENGTH);
99 ut_assertnonnull(ciphered);
100 uncipher = malloc((num_block + 1) * AES_BLOCK_LENGTH);
101 ut_assertnonnull(uncipher);
103 /* Initialize all buffer */
104 rand_buf(key, key_len);
105 rand_buf(iv, AES_BLOCK_LENGTH);
106 rand_buf(nocipher, num_block * AES_BLOCK_LENGTH);
107 memset(ciphered, 0, (num_block + 1) * AES_BLOCK_LENGTH);
108 memset(uncipher, 0, (num_block + 1) * AES_BLOCK_LENGTH);
111 aes_expand_key(key, key_len, key_exp);
113 /* Encrypt and decrypt */
115 case TEST_AES_ONE_BLOCK:
116 ret = lib_test_aes_one_block(uts, key_len, key_exp, iv,
120 case TEST_AES_CBC_CHAIN:
121 ret = lib_test_aes_cbc_chain(uts, key_len, key_exp, iv,
126 printf("%s: unknown type (type=%d)\n", __func__, type);
130 /* Free all the data */
141 static int lib_test_aes_run(struct unit_test_state *uts,
142 struct test_aes_s *test)
144 int key_len = test->key_len;
145 int key_exp_len = test->key_exp_len;
146 int type = test->type;
147 int num_block = test->num_block;
149 return _lib_test_aes_run(uts, key_len, key_exp_len,
153 static int lib_test_aes(struct unit_test_state *uts)
157 for (i = 0; i < ARRAY_SIZE(test_aes); i++) {
158 ret = lib_test_aes_run(uts, &test_aes[i]);
166 LIB_TEST(lib_test_aes, 0);