xref: /isa-l_crypto/aes/aes_param_test.c (revision 38e16e11defa07d24d9d593354096a7ef33c75b7)
1 /**********************************************************************
2   Copyright(c) 2024 Intel Corporation All rights reserved.
3 
4   Redistribution and use in source and binary forms, with or without
5   modification, are permitted provided that the following conditions
6   are met:
7     * Redistributions of source code must retain the above copyright
8       notice, this list of conditions and the following disclaimer.
9     * Redistributions in binary form must reproduce the above copyright
10       notice, this list of conditions and the following disclaimer in
11       the documentation and/or other materials provided with the
12       distribution.
13     * Neither the name of Intel Corporation nor the names of its
14       contributors may be used to endorse or promote products derived
15       from this software without specific prior written permission.
16 
17   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 **********************************************************************/
29 
30 #include <stdio.h>
31 #include "isal_crypto_api.h"
32 #include "aes_keyexp.h"
33 #include "aes_cbc.h"
34 #include "aes_xts.h"
35 #include "test.h"
36 
37 #ifdef SAFE_PARAM
38 #define CHECK_RETURN(state, expected, func)                                                        \
39         do {                                                                                       \
40                 if ((state) != (expected)) {                                                       \
41                         printf("test: %s() - expected return "                                     \
42                                "value %d, got %d\n",                                               \
43                                func, expected, state);                                             \
44                         return 1;                                                                  \
45                 }                                                                                  \
46         } while (0)
47 
48 typedef int (*aes_keyexp_func)(const uint8_t *, uint8_t *, uint8_t *);
49 typedef int (*aes_cbc_func)(const void *, const uint8_t *, const uint8_t *, void *, const uint64_t);
50 typedef int (*aes_xts_func)(const uint8_t *, const uint8_t *, const uint8_t *, const uint64_t,
51                             const void *, void *);
52 
53 struct test_func {
54         union {
55                 aes_keyexp_func keyexp_func_ptr;
56                 aes_cbc_func cbc_func_ptr;
57                 aes_xts_func xts_func_ptr;
58         };
59         char *func_name;
60 };
61 
62 static int
63 test_aes_keyexp_api(aes_keyexp_func aes_keyexp_func_ptr, const char *name)
64 {
65         uint8_t key[CBC_ROUND_KEY_LEN] = { 0 };
66         uint8_t enc_keys[CBC_MAX_KEYS_SIZE] = { 0 };
67         uint8_t dec_keys[CBC_MAX_KEYS_SIZE] = { 0 };
68 
69         // test null key
70         CHECK_RETURN(aes_keyexp_func_ptr(NULL, enc_keys, dec_keys), ISAL_CRYPTO_ERR_NULL_KEY, name);
71 
72         // test null exp key ptr
73         CHECK_RETURN(aes_keyexp_func_ptr(key, NULL, dec_keys), ISAL_CRYPTO_ERR_NULL_EXP_KEY, name);
74 
75         // test null exp key ptr
76         CHECK_RETURN(aes_keyexp_func_ptr(key, enc_keys, NULL), ISAL_CRYPTO_ERR_NULL_EXP_KEY, name);
77 
78         // test valid params
79         CHECK_RETURN(aes_keyexp_func_ptr(key, enc_keys, dec_keys), ISAL_CRYPTO_ERR_NONE, name);
80 
81         return 0;
82 }
83 
84 static int
85 test_aes_cbc_api(aes_cbc_func aes_cbc_func_ptr, const char *name)
86 {
87         uint8_t exp_keys[CBC_MAX_KEYS_SIZE] = { 0 };
88         uint8_t buf[16] = { 0 };
89         uint8_t iv[16] = { 0 };
90 
91         // test null input ptr
92         CHECK_RETURN(aes_cbc_func_ptr(NULL, iv, exp_keys, buf, 16), ISAL_CRYPTO_ERR_NULL_SRC, name);
93 
94         // test null IV ptr
95         CHECK_RETURN(aes_cbc_func_ptr(buf, NULL, exp_keys, buf, 16), ISAL_CRYPTO_ERR_NULL_IV, name);
96 
97         // test null exp key ptr
98         CHECK_RETURN(aes_cbc_func_ptr(buf, iv, NULL, buf, 16), ISAL_CRYPTO_ERR_NULL_EXP_KEY, name);
99 
100         // test null output ptr
101         CHECK_RETURN(aes_cbc_func_ptr(buf, iv, exp_keys, NULL, 16), ISAL_CRYPTO_ERR_NULL_DST, name);
102 
103         // test invalid length (not multiple of 16 bytes)
104         CHECK_RETURN(aes_cbc_func_ptr(buf, iv, exp_keys, buf, 15), ISAL_CRYPTO_ERR_CIPH_LEN, name);
105 
106         // test valid params
107         CHECK_RETURN(aes_cbc_func_ptr(buf, iv, exp_keys, buf, 16), ISAL_CRYPTO_ERR_NONE, name);
108 
109         return 0;
110 }
111 
112 static int
113 test_aes_xts_api(aes_xts_func aes_xts_func_ptr, const char *name, const int expanded_key)
114 {
115         uint8_t key1[32] = { 0 };
116         uint8_t exp_keys1[CBC_MAX_KEYS_SIZE] = { 0 };
117         uint8_t key2[32] = { 0 };
118         uint8_t exp_keys2[CBC_MAX_KEYS_SIZE] = { 0 };
119         uint8_t buf[16] = { 0 };
120         uint8_t tweak[16] = { 0 };
121 
122         uint8_t *key1_ptr = (expanded_key) ? exp_keys1 : key1;
123         uint8_t *key2_ptr = (expanded_key) ? exp_keys2 : key2;
124 
125         if (expanded_key) {
126                 // test null expanded key ptr
127                 CHECK_RETURN(aes_xts_func_ptr(NULL, exp_keys1, tweak, 16, buf, buf),
128                              ISAL_CRYPTO_ERR_NULL_EXP_KEY, name);
129                 CHECK_RETURN(aes_xts_func_ptr(exp_keys1, NULL, tweak, 16, buf, buf),
130                              ISAL_CRYPTO_ERR_NULL_EXP_KEY, name);
131         } else {
132                 CHECK_RETURN(aes_xts_func_ptr(NULL, key2, tweak, 16, buf, buf),
133                              ISAL_CRYPTO_ERR_NULL_KEY, name);
134                 CHECK_RETURN(aes_xts_func_ptr(key1, NULL, tweak, 16, buf, buf),
135                              ISAL_CRYPTO_ERR_NULL_KEY, name);
136         }
137 
138         // test null tweak ptr
139         CHECK_RETURN(aes_xts_func_ptr(key1_ptr, key2_ptr, NULL, 16, buf, buf),
140                      ISAL_CRYPTO_ERR_XTS_NULL_TWEAK, name);
141 
142         // test invalid length (outside range)
143         CHECK_RETURN(
144                 aes_xts_func_ptr(key1_ptr, key2_ptr, tweak, ISAL_AES_XTS_MIN_LEN - 1, buf, buf),
145                 ISAL_CRYPTO_ERR_CIPH_LEN, name);
146 
147         CHECK_RETURN(
148                 aes_xts_func_ptr(key1_ptr, key2_ptr, tweak, ISAL_AES_XTS_MAX_LEN + 1, buf, buf),
149                 ISAL_CRYPTO_ERR_CIPH_LEN, name);
150 
151         // test null input ptr
152         CHECK_RETURN(aes_xts_func_ptr(key1_ptr, key2_ptr, tweak, 16, NULL, buf),
153                      ISAL_CRYPTO_ERR_NULL_SRC, name);
154 
155         // test null output ptr
156         CHECK_RETURN(aes_xts_func_ptr(key1_ptr, key2_ptr, tweak, 16, buf, NULL),
157                      ISAL_CRYPTO_ERR_NULL_DST, name);
158 
159         // test valid params
160         CHECK_RETURN(aes_xts_func_ptr(key1_ptr, key2_ptr, tweak, 16, buf, buf),
161                      ISAL_CRYPTO_ERR_NONE, name);
162 
163         return 0;
164 }
165 
166 #endif /* SAFE_PARAM */
167 
168 int
169 main(void)
170 {
171         int fail = 0;
172 #ifdef SAFE_PARAM
173         /* Test AES key expansion API */
174         const struct test_func keyexp_test_funcs[] = {
175                 { .keyexp_func_ptr = isal_aes_keyexp_128, "isal_aes_keyexp_128" },
176                 { .keyexp_func_ptr = isal_aes_keyexp_192, "isal_aes_keyexp_192" },
177                 { .keyexp_func_ptr = isal_aes_keyexp_256, "isal_aes_keyexp_256" },
178         };
179 
180         for (int i = 0; i < DIM(keyexp_test_funcs); i++) {
181                 fail |= test_aes_keyexp_api(keyexp_test_funcs[i].keyexp_func_ptr,
182                                             keyexp_test_funcs[i].func_name);
183         }
184 
185         /* Test AES-CBC API */
186         const struct test_func cbc_test_funcs[] = {
187                 { .cbc_func_ptr = isal_aes_cbc_enc_128, "isal_aes_cbc_enc_128" },
188                 { .cbc_func_ptr = isal_aes_cbc_enc_192, "isal_aes_cbc_enc_192" },
189                 { .cbc_func_ptr = isal_aes_cbc_enc_256, "isal_aes_cbc_enc_256" },
190                 { .cbc_func_ptr = isal_aes_cbc_dec_128, "isal_aes_cbc_dec_128" },
191                 { .cbc_func_ptr = isal_aes_cbc_dec_192, "isal_aes_cbc_dec_192" },
192                 { .cbc_func_ptr = isal_aes_cbc_dec_256, "isal_aes_cbc_dec_256" },
193         };
194 
195         for (int i = 0; i < DIM(cbc_test_funcs); i++) {
196                 fail |= test_aes_cbc_api(cbc_test_funcs[i].cbc_func_ptr,
197                                          cbc_test_funcs[i].func_name);
198         }
199 
200         /* Test AES-XTS API */
201         const struct test_func xts_test_funcs[] = {
202                 { .xts_func_ptr = isal_aes_xts_enc_128, "isal_aes_xts_enc_128" },
203                 { .xts_func_ptr = isal_aes_xts_enc_256, "isal_aes_xts_enc_256" },
204                 { .xts_func_ptr = isal_aes_xts_dec_128, "isal_aes_xts_dec_128" },
205                 { .xts_func_ptr = isal_aes_xts_dec_256, "isal_aes_xts_dec_256" },
206         };
207 
208         for (int i = 0; i < DIM(xts_test_funcs); i++) {
209                 fail |= test_aes_xts_api(xts_test_funcs[i].xts_func_ptr,
210                                          xts_test_funcs[i].func_name, 0);
211         }
212         /* Test AES-XTS expanded key API */
213         const struct test_func xts_exp_test_funcs[] = {
214                 { .xts_func_ptr = isal_aes_xts_enc_128_expanded_key,
215                   "isal_aes_xts_enc_128_expanded_key" },
216                 { .xts_func_ptr = isal_aes_xts_enc_256_expanded_key,
217                   "isal_aes_xts_enc_256_expanded_key" },
218                 { .xts_func_ptr = isal_aes_xts_dec_128_expanded_key,
219                   "isal_aes_xts_dec_128_expanded_key" },
220                 { .xts_func_ptr = isal_aes_xts_dec_256_expanded_key,
221                   "isal_aes_xts_dec_256_expanded_key" },
222         };
223 
224         for (int i = 0; i < DIM(xts_exp_test_funcs); i++) {
225                 fail |= test_aes_xts_api(xts_exp_test_funcs[i].xts_func_ptr,
226                                          xts_exp_test_funcs[i].func_name, 1);
227         }
228 
229         printf(fail ? "Fail\n" : "Pass\n");
230 #else
231         printf("Not Executed\n");
232 #endif
233         return fail;
234 }
235