xref: /OK3568_Linux_fs/u-boot/cmd/crypto.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2019 Fuzhou Rockchip Electronics Co., Ltd
4  */
5 
6 #include <common.h>
7 #include <command.h>
8 #include <crypto.h>
9 #include <dm.h>
10 #include <u-boot/md5.h>
11 #include <u-boot/sha1.h>
12 #include <u-boot/sha256.h>
13 #include <u-boot/sha512.h>
14 #include <rockchip/crypto_fix_test_data.h>
15 
16 #define PERF_TOTAL_SIZE			(128 * 1024 * 1024)
17 #define PERF_BUFF_SIZE			(4 * 1024 * 1024)
18 
19 #define CALC_RATE_MPBS(bytes, ms)	(((bytes) / 1024) / (ms))
20 
21 struct hash_test_data {
22 	const char	*algo_name;
23 	const char	*mode_name;
24 	u32		algo;
25 	const u8	*data;
26 	u32		data_len;
27 	const u8	*hash;
28 	u32		hash_len;
29 	const u8	*key;
30 	u32		key_len;
31 };
32 
33 struct cipher_test_data {
34 	const char	*algo_name;
35 	const char	*mode_name;
36 	u32		algo;
37 	u32		mode;
38 	const u8	*key;
39 	const u8	*twk_key;
40 	u32		key_len;
41 	const u8	*iv;
42 	u32		iv_len;
43 	const u8	*plain;
44 	u32		plain_len;
45 	const u8	*cipher;
46 	u32		cipher_len;
47 	const u8	*aad;
48 	u32		aad_len;
49 	const u8	*tag;
50 	u32		tag_len;
51 };
52 
53 struct rsa_test_data {
54 	const char	*algo_name;
55 	const char	*mode_name;
56 	u32		algo;
57 	const u8	*n;
58 	u32		n_len;
59 	const u8	*e;
60 	u32		e_len;
61 	const u8	*d;
62 	u32		d_len;
63 	const u8	*c;
64 	u32		c_len;
65 	const u8	*sign_in;
66 	u32		sign_in_len;
67 	const u8	*sign_out;
68 	u32		sign_out_len;
69 };
70 
71 #define IS_MAC_MODE(mode)	((mode) == RK_MODE_CBC_MAC || \
72 				 (mode) == RK_MODE_CMAC)
73 
74 #define IS_AE_MODE(mode)	((mode) == RK_MODE_CCM || \
75 				 (mode) == RK_MODE_GCM)
76 #define HASH_TEST(algo_type, data_in, hash_val) {\
77 	.algo_name = "HASH", \
78 	.mode_name = #algo_type, \
79 	.algo      = CRYPTO_##algo_type, \
80 	.data      = (data_in),\
81 	.data_len  = sizeof(data_in), \
82 	.hash      = (hash_val), \
83 	.hash_len  = sizeof(hash_val) \
84 }
85 
86 #define HMAC_TEST(algo_type, data_in, hash_val, hmac_key) {\
87 	.algo_name = "HMAC", \
88 	.mode_name = #algo_type, \
89 	.algo      = CRYPTO_HMAC_##algo_type, \
90 	.data      = (data_in),\
91 	.data_len  = sizeof(data_in), \
92 	.hash      = (hash_val), \
93 	.hash_len  = sizeof(hash_val), \
94 	.key       = (hmac_key), \
95 	.key_len   = sizeof(hmac_key)\
96 }
97 
98 #define CIPHER_XTS_TEST(algo_type, mode_type, key1, key2, iv_val, in, out) { \
99 	.algo_name  = #algo_type, \
100 	.mode_name  = #mode_type, \
101 	.algo       = CRYPTO_##algo_type,\
102 	.mode       = RK_MODE_##mode_type, \
103 	.key        = (key1), \
104 	.twk_key    = (key2), \
105 	.key_len    = sizeof(key1), \
106 	.iv         = (iv_val), \
107 	.iv_len     = sizeof(iv_val), \
108 	.plain      = (in), \
109 	.plain_len  = sizeof(in), \
110 	.cipher     = (out), \
111 	.cipher_len = sizeof(out) \
112 }
113 
114 #define CIPHER_TEST(algo, mode, key, iv, plain, cipher) \
115 		CIPHER_XTS_TEST(algo, mode, key, NULL, iv, plain, cipher)
116 
117 #define CIPHER_AE_TEST(algo_type, mode_type, key_val, iv_val, \
118 		       in, out, aad_val, tag_val) { \
119 	.algo_name  = #algo_type, \
120 	.mode_name  = #mode_type, \
121 	.algo       = CRYPTO_##algo_type,\
122 	.mode       = RK_MODE_##mode_type, \
123 	.key        = (key_val), \
124 	.key_len    = sizeof(key_val), \
125 	.iv         = (iv_val), \
126 	.iv_len     = sizeof(iv_val), \
127 	.plain      = (in), \
128 	.plain_len  = sizeof(in), \
129 	.cipher     = (out), \
130 	.cipher_len = sizeof(out), \
131 	.aad        = (aad_val), \
132 	.aad_len    = sizeof(aad_val), \
133 	.tag        = (tag_val), \
134 	.tag_len    = sizeof(tag_val), \
135 }
136 
137 #define RSA_TEST(nbits, bn, be, bc, bd, in, out) { \
138 	.algo_name    = "RSA", \
139 	.mode_name    = #nbits, \
140 	.algo         = CRYPTO_RSA##nbits, \
141 	.n            = (bn), \
142 	.n_len        = sizeof(bn), \
143 	.e            = (be), \
144 	.e_len        = sizeof(be), \
145 	.d            = (bd), \
146 	.d_len        = sizeof(bd), \
147 	.c            = (bc), \
148 	.c_len        = sizeof(bc), \
149 	.sign_in      = (in), \
150 	.sign_in_len  = sizeof(in), \
151 	.sign_out     = (out), \
152 	.sign_out_len = sizeof(out) \
153 }
154 
155 #define EMPTY_TEST() {}
156 
157 const struct hash_test_data hash_data_set[] = {
158 	HASH_TEST(MD5,    foo_data, hash_md5),
159 	HASH_TEST(SHA1,   foo_data, hash_sha1),
160 	HASH_TEST(SHA256, foo_data, hash_sha256),
161 	HASH_TEST(SHA512, foo_data, hash_sha512),
162 	HASH_TEST(SM3,    foo_data, hash_sm3),
163 
164 #if CONFIG_IS_ENABLED(ROCKCHIP_HMAC)
165 	EMPTY_TEST(),
166 	HMAC_TEST(MD5,    foo_data, hmac_md5,    hmac_key),
167 	HMAC_TEST(SHA1,   foo_data, hmac_sha1,   hmac_key),
168 	HMAC_TEST(SHA256, foo_data, hmac_sha256, hmac_key),
169 	HMAC_TEST(SHA512, foo_data, hmac_sha512, hmac_key),
170 	HMAC_TEST(SM3,    foo_data, hmac_sm3,    hmac_key),
171 #endif
172 };
173 
174 const struct cipher_test_data cipher_data_set[] = {
175 #if CONFIG_IS_ENABLED(ROCKCHIP_CIPHER)
176 	CIPHER_TEST(DES, ECB, des_key, des_iv, foo_data, des_ecb_cipher),
177 	CIPHER_TEST(DES, CBC, des_key, des_iv, foo_data, des_cbc_cipher),
178 	CIPHER_TEST(DES, CFB, des_key, des_iv, foo_data, des_cfb_cipher),
179 	CIPHER_TEST(DES, OFB, des_key, des_iv, foo_data, des_ofb_cipher),
180 
181 	EMPTY_TEST(),
182 	CIPHER_TEST(DES, ECB, tdes_key, tdes_iv, foo_data, tdes_ecb_cipher),
183 	CIPHER_TEST(DES, CBC, tdes_key, tdes_iv, foo_data, tdes_cbc_cipher),
184 	CIPHER_TEST(DES, CFB, tdes_key, tdes_iv, foo_data, tdes_cfb_cipher),
185 	CIPHER_TEST(DES, OFB, tdes_key, tdes_iv, foo_data, tdes_ofb_cipher),
186 
187 	EMPTY_TEST(),
188 	CIPHER_TEST(AES, ECB, aes_key, aes_iv, foo_data, aes_ecb_cipher),
189 	CIPHER_TEST(AES, CBC, aes_key, aes_iv, foo_data, aes_cbc_cipher),
190 	CIPHER_TEST(AES, CFB, aes_key, aes_iv, foo_data, aes_cfb_cipher),
191 	CIPHER_TEST(AES, OFB, aes_key, aes_iv, foo_data, aes_ofb_cipher),
192 	CIPHER_TEST(AES, CTS, aes_key, aes_iv, foo_data, aes_cts_cipher),
193 	CIPHER_TEST(AES, CTR, aes_key, aes_iv, foo_data, aes_ctr_cipher),
194 	CIPHER_XTS_TEST(AES, XTS, aes_key, aes_twk_key,
195 			aes_iv, foo_data, aes_xts_cipher),
196 	CIPHER_TEST(AES, CBC_MAC, aes_key, aes_iv, foo_data, aes_cbc_mac),
197 	CIPHER_TEST(AES, CMAC, aes_key, aes_iv, foo_data, aes_cmac),
198 	CIPHER_AE_TEST(AES, CCM, aes_key, aes_ccm_iv, foo_data, aes_ccm_cipher,
199 		       ad_data, aes_ccm_tag),
200 	CIPHER_AE_TEST(AES, GCM, aes_key, aes_iv, foo_data, aes_gcm_cipher,
201 		       ad_data, aes_gcm_tag),
202 
203 	EMPTY_TEST(),
204 	CIPHER_TEST(SM4, ECB, sm4_key, sm4_iv, foo_data, sm4_ecb_cipher),
205 	CIPHER_TEST(SM4, CBC, sm4_key, sm4_iv, foo_data, sm4_cbc_cipher),
206 	CIPHER_TEST(SM4, CFB, sm4_key, sm4_iv, foo_data, sm4_cfb_cipher),
207 	CIPHER_TEST(SM4, OFB, sm4_key, sm4_iv, foo_data, sm4_ofb_cipher),
208 	CIPHER_TEST(SM4, CTS, sm4_key, sm4_iv, foo_data, sm4_cts_cipher),
209 	CIPHER_TEST(SM4, CTR, sm4_key, sm4_iv, foo_data, sm4_ctr_cipher),
210 	CIPHER_XTS_TEST(SM4, XTS, sm4_key, sm4_twk_key,
211 			sm4_iv, foo_data, sm4_xts_cipher),
212 	CIPHER_TEST(SM4, CBC_MAC, sm4_key, sm4_iv, foo_data, sm4_cbc_mac),
213 	CIPHER_TEST(SM4, CMAC, sm4_key, sm4_iv, foo_data, sm4_cmac),
214 	CIPHER_AE_TEST(SM4, CCM, sm4_key, sm4_ccm_iv, foo_data, sm4_ccm_cipher,
215 		       ad_data, sm4_ccm_tag),
216 	CIPHER_AE_TEST(SM4, GCM, sm4_key, sm4_iv, foo_data, sm4_gcm_cipher,
217 		       ad_data, sm4_gcm_tag),
218 #else
219 	EMPTY_TEST(),
220 #endif
221 };
222 
223 const struct rsa_test_data rsa_data_set[] = {
224 #if CONFIG_IS_ENABLED(ROCKCHIP_RSA)
225 
226 #ifdef CONFIG_ROCKCHIP_CRYPTO_V1
227 	RSA_TEST(2048, rsa2048_n, rsa2048_e, rsa2048_c, rsa2048_d,
228 		 rsa2048_sign_in, rsa2048_sign_out),
229 #else
230 	RSA_TEST(4096, rsa4096_n, rsa4096_e, NULL, rsa4096_d,
231 		 rsa4096_sign_in, rsa4096_sign_out),
232 #endif
233 
234 #else
235 	EMPTY_TEST(),
236 #endif
237 };
238 
dump_hex(const char * name,const u8 * array,u32 len)239 static void dump_hex(const char *name, const u8 *array, u32 len)
240 {
241 	int i;
242 
243 	printf("[%s]: %uByte", name, len);
244 	for (i = 0; i < len; i++) {
245 		if (i % 32 == 0)
246 			printf("\n");
247 		printf("%02x ", array[i]);
248 	}
249 	printf("\n");
250 }
251 
print_result_MBps(const char * algo_name,const char * mode_name,const char * crypt,ulong MBps,const u8 * expect,const u8 * actual,u32 len)252 static inline void print_result_MBps(const char *algo_name,
253 				     const char *mode_name,
254 				     const char *crypt, ulong MBps,
255 				     const u8 *expect, const u8 *actual,
256 				     u32 len)
257 {
258 	if (memcmp(expect, actual, len) == 0) {
259 		printf("[%s] %-8s%-8s PASS    (%luMBps)\n",
260 		       algo_name, mode_name, crypt, MBps);
261 	} else {
262 		printf("[%s] %-8s%-8s FAIL\n",
263 		       algo_name, mode_name, crypt);
264 		dump_hex("expect", expect, len);
265 		dump_hex("actual", actual, len);
266 	}
267 }
268 
print_result_ms(const char * algo_name,const char * mode_name,const char * crypt,ulong time_cost,const u8 * expect,const u8 * actual,u32 len)269 static inline void print_result_ms(const char *algo_name, const char *mode_name,
270 				   const char *crypt, ulong time_cost,
271 				   const u8 *expect, const u8 *actual, u32 len)
272 {
273 	if (memcmp(expect, actual, len) == 0) {
274 		printf("[%s] %-8s%-8s PASS    (%lums)\n",
275 		       algo_name, mode_name, crypt, time_cost);
276 	} else {
277 		printf("[%s] %-8s%-8s FAIL\n",
278 		       algo_name, mode_name, crypt);
279 		dump_hex("expect", expect, len);
280 		dump_hex("actual", actual, len);
281 	}
282 }
283 
test_hash_perf(struct udevice * dev,u32 algo,const u8 * key,u32 key_len,ulong * MBps)284 int test_hash_perf(struct udevice *dev, u32 algo,
285 		   const u8 *key, u32 key_len, ulong *MBps)
286 {
287 	u32 total_size = PERF_TOTAL_SIZE;
288 	u32 data_size = PERF_BUFF_SIZE;
289 	sha_context ctx;
290 	u8 *data = NULL;
291 	u8 hash_out[64];
292 	int ret, i;
293 
294 	*MBps = 0;
295 
296 	ctx.algo = algo;
297 	ctx.length = total_size;
298 
299 	data = (u8 *)memalign(CONFIG_SYS_CACHELINE_SIZE, data_size);
300 	if (!data) {
301 		printf("%s, %d: memalign %u error!\n",
302 		       __func__, __LINE__, data_size);
303 		return -EINVAL;
304 	}
305 
306 	memset(data, 0xab, data_size);
307 
308 	ulong start = get_timer(0);
309 
310 	if (key)
311 		ret = crypto_hmac_init(dev, &ctx, (u8 *)key, key_len);
312 	else
313 		ret = crypto_sha_init(dev, &ctx);
314 
315 	if (ret) {
316 		printf("crypto_sha_init error ret = %d!\n", ret);
317 		goto exit;
318 	}
319 
320 	for (i = 0; i < total_size / data_size; i++) {
321 		ret = crypto_sha_update(dev, (u32 *)data, data_size);
322 		if (ret) {
323 			printf("crypto_sha_update error!\n");
324 			goto exit;
325 		}
326 	}
327 
328 	ret = crypto_sha_final(dev, &ctx, hash_out);
329 	if (ret) {
330 		printf("crypto_sha_final error ret = %d!\n", ret);
331 		goto exit;
332 	}
333 
334 	ulong time_cost = get_timer(start);
335 
336 	*MBps = CALC_RATE_MPBS(total_size, time_cost);
337 
338 exit:
339 	free(data);
340 
341 	return ret;
342 }
343 
test_cipher_perf(struct udevice * dev,cipher_context * ctx,ulong * MBps,bool enc)344 int test_cipher_perf(struct udevice *dev, cipher_context *ctx,
345 		     ulong *MBps, bool enc)
346 {
347 	u32 total_size = PERF_TOTAL_SIZE;
348 	u32 data_size = PERF_BUFF_SIZE;
349 	u8 *plain = NULL, *cipher = NULL;
350 	u8 aad[128], tag[16];
351 	int ret = 0, i;
352 
353 	*MBps = 0;
354 
355 	plain = (u8 *)memalign(CONFIG_SYS_CACHELINE_SIZE, data_size);
356 	if (!plain) {
357 		printf("%s, %d: memalign %u error!\n",
358 		       __func__, __LINE__, data_size);
359 		return -EINVAL;
360 	}
361 
362 	cipher = (u8 *)memalign(CONFIG_SYS_CACHELINE_SIZE, data_size);
363 	if (!cipher) {
364 		printf("%s, %d: memalign %u error!\n",
365 		       __func__, __LINE__, data_size);
366 		free(plain);
367 		return -EINVAL;
368 	}
369 
370 	memset(plain, 0xab, data_size);
371 	memset(aad, 0xcb, sizeof(aad));
372 
373 	ulong start = get_timer(0);
374 
375 	for (i = 0; i < total_size / data_size; i++) {
376 		if (IS_MAC_MODE(ctx->mode))
377 			ret = crypto_mac(dev, ctx, plain, data_size, cipher);
378 		else if (IS_AE_MODE(ctx->mode))
379 			ret = crypto_ae(dev, ctx, plain, data_size,
380 					aad, sizeof(aad), cipher, tag);
381 		else
382 			ret = crypto_cipher(dev, ctx, plain, cipher,
383 					    data_size, enc);
384 		if (ret) {
385 			printf("%s, %d:crypto calc error! ret = %d\n",
386 			       __func__, __LINE__, ret);
387 			goto exit;
388 		}
389 	}
390 
391 	ulong time_cost = get_timer(start);
392 
393 	*MBps = CALC_RATE_MPBS(total_size, time_cost);
394 exit:
395 	free(plain);
396 	free(cipher);
397 
398 	return ret;
399 }
400 
test_hash_result(void)401 int test_hash_result(void)
402 {
403 	const struct hash_test_data *test_data = NULL;
404 	sha_context csha_ctx;
405 	struct udevice *dev;
406 	unsigned int i;
407 	u8 out[64];
408 	int ret;
409 
410 	printf("\n=================== hash & hmac test ===================\n");
411 
412 	for (i = 0; i < ARRAY_SIZE(hash_data_set); i++) {
413 		test_data = &hash_data_set[i];
414 		if (test_data->algo == 0) {
415 			printf("\n");
416 			continue;
417 		}
418 
419 		dev = crypto_get_device(test_data->algo);
420 		if (!dev) {
421 			printf("[%s] %-16s unsupported!!!\n",
422 			       test_data->algo_name,
423 			       test_data->mode_name);
424 			continue;
425 		}
426 
427 		csha_ctx.algo   = test_data->algo;
428 		csha_ctx.length = test_data->data_len;
429 
430 		memset(out, 0x00, sizeof(out));
431 		if (test_data->key) {
432 			ret = crypto_hmac_init(dev, &csha_ctx,
433 					       (u8 *)test_data->key,
434 					       test_data->key_len);
435 			ret |= crypto_hmac_update(dev, (void *)test_data->data,
436 						  test_data->data_len);
437 			ret |= crypto_hmac_final(dev, &csha_ctx, out);
438 			if (ret) {
439 				printf("hmac calc error ret = %d\n", ret);
440 				goto error;
441 			}
442 		} else {
443 			ret = crypto_sha_init(dev, &csha_ctx);
444 			ret |= crypto_sha_update(dev, (void *)test_data->data,
445 						 test_data->data_len);
446 			ret |= crypto_sha_final(dev, &csha_ctx, out);
447 			if (ret) {
448 				printf("hash calc error ret = %d\n", ret);
449 				goto error;
450 			}
451 		}
452 
453 		ulong MBps = 0;
454 
455 		test_hash_perf(dev, test_data->algo,
456 			       test_data->key, test_data->key_len, &MBps);
457 		print_result_MBps(test_data->algo_name, test_data->mode_name,
458 				  "", MBps, test_data->hash, out,
459 				  test_data->hash_len);
460 		printf("+++++++++++++++++++++++++++++++++++++++++++++++++++\n");
461 	}
462 
463 	return 0;
464 error:
465 	printf("%s %s test error!\n",
466 	       test_data->algo_name, test_data->mode_name);
467 	return ret;
468 }
469 
test_cipher_result(void)470 int test_cipher_result(void)
471 {
472 	const struct cipher_test_data *test_data = NULL;
473 	struct udevice *dev;
474 	cipher_context ctx;
475 	u8 out[256], tag[16];
476 	int ret;
477 	u32 i;
478 
479 	printf("\n===================== cipher test ======================\n");
480 
481 	for (i = 0; i < ARRAY_SIZE(cipher_data_set); i++) {
482 		test_data = &cipher_data_set[i];
483 		if (test_data->algo == 0) {
484 			printf("\n");
485 			continue;
486 		}
487 
488 		dev = crypto_get_device(test_data->algo);
489 		if (!dev) {
490 			printf("[%s] %-16s unsupported!!!\n",
491 			       test_data->algo_name, test_data->mode_name);
492 			continue;
493 		}
494 
495 		memset(&ctx, 0x00, sizeof(ctx));
496 
497 		ctx.algo    = test_data->algo;
498 		ctx.mode    = test_data->mode;
499 		ctx.key     = test_data->key;
500 		ctx.twk_key = test_data->twk_key;
501 		ctx.key_len = test_data->key_len;
502 		ctx.iv      = test_data->iv;
503 		ctx.iv_len  = test_data->iv_len;
504 
505 		ulong MBps = 0;
506 
507 		test_cipher_perf(dev, &ctx, &MBps, true);
508 
509 		/* AES/SM4 mac */
510 		if (IS_MAC_MODE(ctx.mode))
511 			ret = crypto_mac(dev, &ctx, test_data->plain,
512 					 test_data->plain_len, out);
513 		else if (IS_AE_MODE(ctx.mode))
514 			ret = crypto_ae(dev, &ctx,
515 					test_data->plain, test_data->plain_len,
516 					test_data->aad, test_data->aad_len,
517 					out, tag);
518 		else
519 			ret = crypto_cipher(dev, &ctx, test_data->plain,
520 					    out, test_data->plain_len, true);
521 		if (ret)
522 			goto error;
523 
524 		if (test_data->tag &&
525 		    memcmp(test_data->tag, tag, test_data->tag_len) != 0) {
526 			printf("tag mismatch!!!\n");
527 			dump_hex("expect", test_data->tag, test_data->tag_len);
528 			dump_hex("actual", tag, test_data->tag_len);
529 			goto error;
530 		}
531 
532 		print_result_MBps(test_data->algo_name, test_data->mode_name,
533 				  "encrypt", MBps, test_data->cipher, out,
534 				  test_data->cipher_len);
535 
536 		if (!IS_MAC_MODE(ctx.mode) && !IS_AE_MODE(ctx.mode)) {
537 			test_cipher_perf(dev, &ctx, &MBps, false);
538 			ret = crypto_cipher(dev, &ctx, test_data->cipher,
539 					    out, test_data->cipher_len, false);
540 			if (ret)
541 				goto error;
542 
543 			print_result_MBps(test_data->algo_name,
544 					  test_data->mode_name,
545 					  "decrypt", MBps,
546 					  test_data->plain, out,
547 					  test_data->plain_len);
548 		}
549 		printf("+++++++++++++++++++++++++++++++++++++++++++++++++++\n");
550 	}
551 	return 0;
552 error:
553 	printf("%s %s test error, ret = %d!\n",
554 	       test_data->algo_name, test_data->mode_name, ret);
555 	return ret;
556 }
557 
test_rsa_result(void)558 int test_rsa_result(void)
559 {
560 	const struct rsa_test_data *test_data = NULL;
561 	u8 *hard_out = NULL, *e_tmp;
562 	u32 data_size = 4096 / 8;
563 	ulong start, time_cost;
564 	struct udevice *dev;
565 	rsa_key rsa_key;
566 	int ret, i;
567 
568 	hard_out = (u8 *)memalign(CONFIG_SYS_CACHELINE_SIZE, data_size);
569 	if (!hard_out) {
570 		printf("%s, %d: memalign %u error!\n",
571 		       __func__, __LINE__, data_size);
572 		return -EINVAL;
573 	}
574 
575 	e_tmp = (u8 *)memalign(CONFIG_SYS_CACHELINE_SIZE, data_size);
576 	if (!e_tmp) {
577 		printf("%s, %d: memalign %u error!\n",
578 		       __func__, __LINE__, data_size);
579 		return -EINVAL;
580 	}
581 
582 	printf("\n====================== rsa test ========================\n");
583 	for (i = 0; i < ARRAY_SIZE(rsa_data_set); i++) {
584 		test_data = &rsa_data_set[i];
585 		if (test_data->algo == 0) {
586 			printf("\n");
587 			continue;
588 		}
589 
590 		dev = crypto_get_device(test_data->algo);
591 		if (!dev) {
592 			printf("[%s] %-16s unsupported!!!\n",
593 			       test_data->algo_name, test_data->mode_name);
594 			continue;
595 		}
596 
597 		/* sign test */
598 		memset(&rsa_key, 0x00, sizeof(rsa_key));
599 		rsa_key.algo = test_data->algo;
600 		rsa_key.n = (u32 *)test_data->n;
601 		rsa_key.e = (u32 *)test_data->d;
602 #ifdef CONFIG_ROCKCHIP_CRYPTO_V1
603 		rsa_key.c = (u32 *)test_data->c;
604 #endif
605 
606 		start = get_timer(0);
607 		ret = crypto_rsa_verify(dev, &rsa_key,
608 					(u8 *)test_data->sign_in, hard_out);
609 		if (ret) {
610 			printf("sign test error, ret = %d\n", ret);
611 			goto error;
612 		}
613 		time_cost = get_timer(start);
614 		print_result_ms(test_data->algo_name, test_data->mode_name,
615 				"sign", time_cost, test_data->sign_out,
616 				hard_out, test_data->n_len);
617 
618 		/* verify test */
619 		memset(&rsa_key, 0x00, sizeof(rsa_key));
620 		memset(e_tmp, 0x00, data_size);
621 		memcpy(e_tmp, test_data->e, test_data->e_len);
622 		rsa_key.algo = test_data->algo;
623 		rsa_key.n = (u32 *)test_data->n;
624 		rsa_key.e = (u32 *)e_tmp;
625 #ifdef CONFIG_ROCKCHIP_CRYPTO_V1
626 		rsa_key.c = (u32 *)test_data->c;
627 #endif
628 
629 		start = get_timer(0);
630 		ret = crypto_rsa_verify(dev, &rsa_key,
631 					(u8 *)test_data->sign_out, hard_out);
632 		if (ret) {
633 			printf("verify test error, ret = %d\n", ret);
634 			goto error;
635 		}
636 		time_cost = get_timer(start);
637 
638 		print_result_ms(test_data->algo_name, test_data->mode_name,
639 				"verify", time_cost, test_data->sign_in,
640 				hard_out, test_data->n_len);
641 
642 		printf("+++++++++++++++++++++++++++++++++++++++++++++++++++\n");
643 	}
644 
645 	free(hard_out);
646 	free(e_tmp);
647 
648 	return 0;
649 error:
650 	free(hard_out);
651 	free(e_tmp);
652 	printf("%s %s test error!\n",
653 	       test_data->algo_name, test_data->mode_name);
654 	return ret;
655 }
656 
test_all_result(void)657 static int test_all_result(void)
658 {
659 	int ret = 0;
660 
661 	ret = test_hash_result();
662 	if (ret)
663 		goto exit;
664 
665 	ret = test_cipher_result();
666 	if (ret)
667 		goto exit;
668 
669 	ret = test_rsa_result();
670 	if (ret)
671 		goto exit;
672 
673 exit:
674 	return 0;
675 }
676 
do_crypto(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[])677 static int do_crypto(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
678 {
679 	return test_all_result();
680 }
681 
682 U_BOOT_CMD(
683 	crypto, 1, 1, do_crypto,
684 	"crypto test",
685 	""
686 );
687