1 /*
2 * Copyright (c) 2022 Rockchip Electronics Co. Ltd.
3 */
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <unistd.h>
8 #include "rkcrypto_core.h"
9 #include "rkcrypto_mem.h"
10 #include "cmode_adapter.h"
11 #include "test_ae.h"
12 #include "test_utils.h"
13
14 #define DATA_BUTT 0xFFFFFFFF
15 #define TEST_DATA_MAX (1024 * 1024)
16 #define TEST_AAD_MAX (1 * 1024)
17
18 struct test_ae_item {
19 uint32_t algo[2];
20 uint32_t modes[RK_CIPHER_MODE_MAX];
21 uint32_t key_lens[3];
22 uint32_t data_lens[3];
23 uint32_t aad_lens[3];
24 };
25
26 static struct test_ae_item test_item_tbl[] = {
27 {
28 .algo = {RK_ALGO_AES, RK_ALGO_SM4},
29 .modes = {
30 RK_CIPHER_MODE_GCM,
31 DATA_BUTT,
32 },
33 .key_lens = {16, 24, 32},
34 .data_lens = {256, 32 * 1024, TEST_DATA_MAX},
35 .aad_lens = {20, 256, TEST_AAD_MAX},
36 },
37 };
38
test_ae_item_virt(const struct test_ae_item * item,int verbose)39 static RK_RES test_ae_item_virt(const struct test_ae_item *item, int verbose)
40 {
41 RK_RES res = RK_CRYPTO_ERR_GENERIC;
42 uint32_t h, i, k, l, n;
43 rk_handle handle = 0;
44 rk_ae_config ae_cfg;
45 uint8_t *plain = NULL, *ae_soft = NULL, *ae_hard = NULL, *aad = NULL;
46 uint8_t iv_tmp[32];
47 uint32_t algo = 0, mode = 0, key_len, iv_len, data_len, aad_len, operation;
48 uint32_t tag_len = 16;
49 uint8_t tag_hard[16], tag_soft[16];
50 size_t page_size = getpagesize();
51
52 if (posix_memalign((void *)&plain, page_size, TEST_DATA_MAX) || !plain) {
53 printf("plain malloc %dByte error!\n", TEST_DATA_MAX);
54 goto exit;
55 }
56
57 if (posix_memalign((void *)&ae_soft, page_size, TEST_DATA_MAX) || !ae_soft) {
58 printf("ae_soft malloc %dByte error!\n", TEST_DATA_MAX);
59 goto exit;
60 }
61
62 if (posix_memalign((void *)&ae_hard, page_size, TEST_DATA_MAX) || !ae_hard) {
63 printf("ae_hard malloc %dByte error!\n", TEST_DATA_MAX);
64 goto exit;
65 }
66
67 if (posix_memalign((void *)&aad, page_size, TEST_AAD_MAX) || !aad) {
68 printf("aad malloc %dByte error!\n", TEST_AAD_MAX);
69 goto exit;
70 }
71
72 iv_len = 12;
73
74 for (h = 0; h < ARRAY_SIZE(item->algo); h++) {
75 algo = item->algo[h];
76
77 for (i = 0; i < ARRAY_SIZE(item->modes); i++) {
78 mode = item->modes[i];
79
80 if (mode == DATA_BUTT)
81 break;
82
83 for (k = 0; k < ARRAY_SIZE(item->key_lens); k++) {
84 key_len = item->key_lens[k];
85
86 if (algo == RK_ALGO_SM4 && key_len !=16)
87 continue;
88
89 for (l = 0; l < ARRAY_SIZE(item->data_lens); l++) {
90 data_len = item->data_lens[l];
91 test_get_rng(plain, data_len);
92
93 for (n = 0; n < ARRAY_SIZE(item->aad_lens); n++) {
94 aad_len = item->aad_lens[n];
95
96 memset(ae_soft, 0x00, data_len);
97 memset(ae_hard, 0x00, data_len);
98 memset(tag_hard, 0x00, sizeof(tag_hard));
99 memset(tag_soft, 0x00, sizeof(tag_soft));
100
101 /* encryption */
102 operation = RK_OP_CIPHER_ENC;
103
104 memset(&ae_cfg, 0x00, sizeof(ae_cfg));
105 ae_cfg.algo = algo;
106 ae_cfg.mode = mode;
107 ae_cfg.operation = operation;
108 ae_cfg.key_len = key_len;
109 ae_cfg.iv_len = iv_len;
110 ae_cfg.aad_len = aad_len;
111 ae_cfg.tag_len = tag_len;
112
113 test_get_rng(ae_cfg.key, key_len);
114 test_get_rng(ae_cfg.iv, iv_len);
115 test_get_rng(aad, aad_len);
116 memcpy(iv_tmp, ae_cfg.iv, iv_len);
117
118 res = rk_ae_init(&ae_cfg, &handle);
119 if (res) {
120 if (res != RK_CRYPTO_ERR_NOT_SUPPORTED) {
121 printf("rk_ae_init error[%x]\n", res);
122 goto exit;
123 }
124
125 if (verbose)
126 printf("virt:\t[%s-%u]\t%s\t%s\tN/A\n",
127 test_algo_name(algo), key_len * 8,
128 test_mode_name(mode),
129 test_op_name(operation));
130 res = RK_CRYPTO_SUCCESS;
131 continue;
132 }
133
134 if (is_no_multi_blocksize(mode))
135 data_len = data_len - 3;
136
137 res = rk_ae_set_aad_virt(handle, aad);
138 if (res) {
139 rk_ae_final(handle);
140 printf("rk_ae_set_aad_virt error[%x]\n", res);
141 goto exit;
142 }
143
144 res = rk_ae_crypt_virt(handle, plain, ae_hard, data_len, tag_hard);
145 if (res) {
146 rk_ae_final(handle);
147 printf("rk_ae_crypt_virt error[%x]\n", res);
148 goto exit;
149 }
150
151 rk_ae_final(handle);
152
153 res = soft_ae(algo, mode, operation,
154 ae_cfg.key, ae_cfg.key_len, iv_tmp, iv_len,
155 aad, ae_cfg.aad_len, ae_cfg.tag_len,
156 plain, data_len, ae_soft, tag_soft);
157 if (res) {
158 printf("soft_ae error[%x]\n", res);
159 goto exit;
160 }
161
162 /* Verify the result */
163 if (memcmp(ae_hard, ae_soft, data_len) != 0) {
164 printf("rkcrypto_test_ae_virt compare data failed.\n");
165 res = RK_CRYPTO_ERR_GENERIC;
166 goto exit;
167 }
168
169 if (memcmp(tag_hard, tag_soft, ae_cfg.tag_len) != 0) {
170 printf("rkcrypto_test_ae_virt compare tag failed.\n");
171 res = RK_CRYPTO_ERR_GENERIC;
172 goto exit;
173 }
174
175 if (verbose)
176 printf("virt:\t[%s-%u]\t%s\t%s\tPASS\n",
177 test_algo_name(algo), key_len * 8,
178 test_mode_name(mode), test_op_name(operation));
179
180 /* decryption */
181 operation = RK_OP_CIPHER_DEC;
182 ae_cfg.operation = operation;
183 memcpy(ae_cfg.iv, iv_tmp, iv_len);
184
185 res = rk_ae_init(&ae_cfg, &handle);
186 if (res) {
187 if (res != RK_CRYPTO_ERR_NOT_SUPPORTED) {
188 printf("rk_ae_init error[%x]\n", res);
189 goto exit;
190 }
191
192 if (verbose)
193 printf("virt:\t[%s-%u]\t%s\t%s\tN/A\n",
194 test_algo_name(algo), key_len * 8,
195 test_mode_name(mode),
196 test_op_name(operation));
197 res = RK_CRYPTO_SUCCESS;
198 continue;
199 }
200
201 res = rk_ae_set_aad_virt(handle, aad);
202 if (res) {
203 rk_ae_final(handle);
204 printf("rk_ae_set_aad_virt error[%x]\n", res);
205 goto exit;
206 }
207
208 res = rk_ae_crypt_virt(handle, ae_hard, ae_hard, data_len, tag_hard);
209 if (res) {
210 rk_ae_final(handle);
211 printf("rk_ae_crypt_virt error[%x]\n", res);
212 goto exit;
213 }
214
215 rk_ae_final(handle);
216
217 /* Verify the result */
218 if (memcmp(ae_hard, plain, data_len) != 0) {
219 printf("rkcrypto_test_ae_virt compare data failed.\n");
220 res = RK_CRYPTO_ERR_GENERIC;
221 goto exit;
222 }
223
224 if (verbose)
225 printf("virt:\t[%s-%u]\t%s\t%s\tPASS\n",
226 test_algo_name(algo), key_len * 8,
227 test_mode_name(mode), test_op_name(operation));
228 }
229 }
230 }
231 }
232 }
233
234 res = RK_CRYPTO_SUCCESS;
235 exit:
236 if (plain)
237 free(plain);
238
239 if (ae_soft)
240 free(ae_soft);
241
242 if (ae_hard)
243 free(ae_hard);
244
245 if (aad)
246 free(aad);
247
248 if (res)
249 printf("virt:\t[%s-%u]\t%s\t%s\tFAIL\n",
250 test_algo_name(algo), key_len * 8,
251 test_mode_name(mode), test_op_name(operation));
252
253 return res;
254 }
255
test_ae_item_fd(const struct test_ae_item * item,int verbose)256 static RK_RES test_ae_item_fd(const struct test_ae_item *item, int verbose)
257 {
258 RK_RES res = RK_CRYPTO_ERR_GENERIC;
259 uint32_t h, i, k, l, n;
260 rk_handle handle = 0;
261 rk_ae_config ae_cfg;
262 rk_crypto_mem *plain = NULL, *ae_soft = NULL, *ae_hard = NULL, *aad = NULL;
263 uint8_t iv_tmp[32];
264 uint32_t algo = 0, mode = 0, key_len, iv_len, data_len, aad_len, operation;
265 uint32_t tag_len = 16;
266 uint8_t tag_hard[16], tag_soft[16];
267
268 plain = rk_crypto_mem_alloc(TEST_DATA_MAX);
269 if (!plain) {
270 printf("plain malloc %dByte error!\n", TEST_DATA_MAX);
271 goto exit;
272 }
273
274 ae_soft = rk_crypto_mem_alloc(TEST_DATA_MAX);
275 if (!ae_soft) {
276 printf("ae_soft malloc %dByte error!\n", TEST_DATA_MAX);
277 goto exit;
278 }
279
280 ae_hard = rk_crypto_mem_alloc(TEST_DATA_MAX);
281 if (!ae_hard) {
282 printf("ae_hard malloc %dByte error!\n", TEST_DATA_MAX);
283 goto exit;
284 }
285
286 aad = rk_crypto_mem_alloc(TEST_AAD_MAX);
287 if (!aad) {
288 printf("aad malloc %dByte error!\n", TEST_AAD_MAX);
289 goto exit;
290 }
291
292 memset(tag_hard, 0x00, sizeof(tag_hard));
293 memset(tag_soft, 0x00, sizeof(tag_soft));
294
295 iv_len = 12;
296 for (h = 0; h < ARRAY_SIZE(item->algo); h++) {
297 algo = item->algo[h];
298
299 for (i = 0; i < ARRAY_SIZE(item->modes); i++) {
300 mode = item->modes[i];
301
302 if (mode == DATA_BUTT)
303 break;
304
305 for (k = 0; k < ARRAY_SIZE(item->key_lens); k++) {
306 key_len = item->key_lens[k];
307
308 if (algo == RK_ALGO_SM4 && key_len !=16)
309 continue;
310
311 for (l = 0; l < ARRAY_SIZE(item->data_lens); l++) {
312 data_len = item->data_lens[l];
313 test_get_rng(plain->vaddr, data_len);
314
315 for (n = 0; n < ARRAY_SIZE(item->aad_lens); n++) {
316 aad_len = item->aad_lens[n];
317
318 /* encryption */
319 operation = RK_OP_CIPHER_ENC;
320
321 memset(&ae_cfg, 0x00, sizeof(ae_cfg));
322 ae_cfg.algo = algo;
323 ae_cfg.mode = mode;
324 ae_cfg.operation = operation;
325 ae_cfg.key_len = key_len;
326 ae_cfg.iv_len = iv_len;
327 ae_cfg.aad_len = aad_len;
328 ae_cfg.tag_len = tag_len;
329
330 test_get_rng(ae_cfg.key, key_len);
331 test_get_rng(ae_cfg.iv, iv_len);
332 test_get_rng(aad->vaddr, aad_len);
333 memcpy(iv_tmp, ae_cfg.iv, iv_len);
334
335 res = rk_ae_init(&ae_cfg, &handle);
336 if (res) {
337 if (res != RK_CRYPTO_ERR_NOT_SUPPORTED) {
338 printf("rk_ae_init error[%x]\n", res);
339 goto exit;
340 }
341
342 if (verbose)
343 printf("dma_fd:\t[%s-%u]\t%s\t%s\tN/A\n",
344 test_algo_name(algo), key_len * 8,
345 test_mode_name(mode),
346 test_op_name(operation));
347 res = RK_CRYPTO_SUCCESS;
348 continue;
349 }
350
351 if (is_no_multi_blocksize(mode))
352 data_len = data_len - 3;
353
354 res = rk_ae_set_aad(handle, aad->dma_fd);
355 if (res) {
356 rk_ae_final(handle);
357 printf("rk_ae_set_aad_virt error[%x]\n", res);
358 goto exit;
359 }
360
361 res = rk_ae_crypt(handle, plain->dma_fd, ae_hard->dma_fd, data_len, tag_hard);
362 if (res) {
363 rk_ae_final(handle);
364 printf("rk_ae_crypt error[%x]\n", res);
365 goto exit;
366 }
367
368 rk_ae_final(handle);
369
370 res = soft_ae(algo, mode, operation,
371 ae_cfg.key, ae_cfg.key_len, iv_tmp, iv_len,
372 aad->vaddr, aad_len, ae_cfg.tag_len,
373 plain->vaddr, data_len, ae_soft->vaddr, tag_soft);
374 if (res) {
375 printf("soft_ae error[%x]\n", res);
376 goto exit;
377 }
378
379 /* Verify the result */
380 if (memcmp(ae_hard->vaddr, ae_soft->vaddr, data_len) != 0) {
381 printf("rkcrypto_test_ae compare data failed.\n");
382 res = RK_CRYPTO_ERR_GENERIC;
383 goto exit;
384 }
385
386 if (memcmp(tag_hard, tag_soft, ae_cfg.tag_len) != 0) {
387 printf("rkcrypto_test_ae compare tag failed.\n");
388 res = RK_CRYPTO_ERR_GENERIC;
389 goto exit;
390 }
391
392 if (verbose)
393 printf("dma_fd:\t[%s-%u]\t%s\t%s\tPASS\n",
394 test_algo_name(algo), key_len * 8,
395 test_mode_name(mode), test_op_name(operation));
396
397 /* decryption */
398 operation = RK_OP_CIPHER_DEC;
399 ae_cfg.operation = operation;
400 memcpy(ae_cfg.iv, iv_tmp, iv_len);
401
402 res = rk_ae_init(&ae_cfg, &handle);
403 if (res) {
404 if (res != RK_CRYPTO_ERR_NOT_SUPPORTED) {
405 printf("rk_ae_init error[%x]\n", res);
406 goto exit;
407 }
408
409 if (verbose)
410 printf("dma_fd:\t[%s-%u]\t%s\t%s\tN/A\n",
411 test_algo_name(algo), key_len * 8,
412 test_mode_name(mode),
413 test_op_name(operation));
414 res = RK_CRYPTO_SUCCESS;
415 continue;
416 }
417
418 res = rk_ae_set_aad(handle, aad->dma_fd);
419 if (res) {
420 rk_ae_final(handle);
421 printf("rk_ae_set_aad_virt error[%x]\n", res);
422 goto exit;
423 }
424
425 res = rk_ae_crypt(handle, ae_hard->dma_fd, ae_hard->dma_fd, data_len, tag_hard);
426 if (res) {
427 rk_ae_final(handle);
428 printf("rk_ae_crypt error[%x]\n", res);
429 goto exit;
430 }
431
432 rk_ae_final(handle);
433
434 /* Verify the result */
435 if (memcmp(ae_hard->vaddr, plain->vaddr, data_len) != 0) {
436 printf("rkcrypto_test_ae compare data failed.\n");
437 res = RK_CRYPTO_ERR_GENERIC;
438 goto exit;
439 }
440
441 if (verbose)
442 printf("dma_fd:\t[%s-%u]\t%s\t%s\tPASS\n",
443 test_algo_name(algo), key_len * 8,
444 test_mode_name(mode), test_op_name(operation));
445 }
446 }
447 }
448 }
449 }
450
451 res = RK_CRYPTO_SUCCESS;
452 exit:
453 if (plain)
454 rk_crypto_mem_free(plain);
455
456 if (ae_soft)
457 rk_crypto_mem_free(ae_soft);
458
459 if (ae_hard)
460 rk_crypto_mem_free(ae_hard);
461
462 if (aad)
463 rk_crypto_mem_free(aad);
464
465 if (res)
466 printf("dma_fd:\t[%s-%u]\t%s\t%s\tFAIL\n",
467 test_algo_name(algo), key_len * 8,
468 test_mode_name(mode), test_op_name(operation));
469
470 return res;
471 }
472
test_ae(int verbose)473 RK_RES test_ae(int verbose)
474 {
475 RK_RES res = RK_CRYPTO_ERR_GENERIC;
476 uint32_t i;
477
478 res = rk_crypto_init();
479 if (res) {
480 printf("rk_crypto_init error %08x\n", res);
481 return res;
482 }
483
484 for (i = 0; i < ARRAY_SIZE(test_item_tbl); i++) {
485 res = test_ae_item_virt(&test_item_tbl[i], verbose);
486 if (res)
487 goto exit;
488
489 res = test_ae_item_fd(&test_item_tbl[i], verbose);
490 if (res)
491 goto exit;
492
493 if (verbose)
494 printf("\n");
495 }
496
497 exit:
498 rk_crypto_deinit();
499 return res;
500 }
501
502