xref: /OK3568_Linux_fs/external/security/librkcrypto/test/test_multi.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright (c) 2022 Rockchip Electronics Co. Ltd.
3*4882a593Smuzhiyun  */
4*4882a593Smuzhiyun #include <stdlib.h>
5*4882a593Smuzhiyun #include <string.h>
6*4882a593Smuzhiyun #include <unistd.h>
7*4882a593Smuzhiyun #include <fcntl.h>
8*4882a593Smuzhiyun #include <error.h>
9*4882a593Smuzhiyun #include <errno.h>
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun #include "rkcrypto_core.h"
12*4882a593Smuzhiyun #include "rkcrypto_mem.h"
13*4882a593Smuzhiyun #include "rkcrypto_trace.h"
14*4882a593Smuzhiyun #include "test_multi.h"
15*4882a593Smuzhiyun #include "test_cipher.h"
16*4882a593Smuzhiyun #include "test_hash.h"
17*4882a593Smuzhiyun 
test_support_multi(void)18*4882a593Smuzhiyun static RK_RES test_support_multi(void)
19*4882a593Smuzhiyun {
20*4882a593Smuzhiyun 	printf("=============== Multithreaded testing start =============\n");
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun 	///TODO: test multi thread
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun 	printf("=============== Multithreaded testing pass ==============\n");
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun 	return RK_CRYPTO_SUCCESS;
27*4882a593Smuzhiyun }
28*4882a593Smuzhiyun 
test_nosupport_multi(void)29*4882a593Smuzhiyun static RK_RES test_nosupport_multi(void)
30*4882a593Smuzhiyun {
31*4882a593Smuzhiyun 	RK_RES res;
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun 	printf("=============== Non-multithreaded testing start =============\n");
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun 	/* Test normal calculation */
36*4882a593Smuzhiyun 	res = test_cipher(1);
37*4882a593Smuzhiyun 	if (res) {
38*4882a593Smuzhiyun 		E_TRACE("test_cipher error[%x]\n", res);
39*4882a593Smuzhiyun 		goto error;
40*4882a593Smuzhiyun 	}
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun 	res = test_hash(1);
43*4882a593Smuzhiyun 	if (res) {
44*4882a593Smuzhiyun 		E_TRACE("test_hash error[%x]\n", res);
45*4882a593Smuzhiyun 		goto error;
46*4882a593Smuzhiyun 	}
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun 	res = test_hmac(1);
49*4882a593Smuzhiyun 	if (res) {
50*4882a593Smuzhiyun 		E_TRACE("test_hash error[%x]\n", res);
51*4882a593Smuzhiyun 		goto error;
52*4882a593Smuzhiyun 	}
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun 	/* Test abnormal operation */
55*4882a593Smuzhiyun 	rk_cipher_config cipher_cfg;
56*4882a593Smuzhiyun 	rk_hash_config hash_cfg;
57*4882a593Smuzhiyun 	rk_handle hdl_cipher1, hdl_cipher2, hdl_hash1, hdl_hash2;
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun 	memset(&cipher_cfg, 0x00, sizeof(cipher_cfg));
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun 	cipher_cfg.algo      = RK_ALGO_AES;
62*4882a593Smuzhiyun 	cipher_cfg.mode      = RK_CIPHER_MODE_ECB;
63*4882a593Smuzhiyun 	cipher_cfg.operation = RK_OP_CIPHER_ENC;
64*4882a593Smuzhiyun 	cipher_cfg.key_len   = 16;
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun 	res = rk_crypto_init();
67*4882a593Smuzhiyun 	if (res) {
68*4882a593Smuzhiyun 		E_TRACE("rk_crypto_init error[%x]\n", res);
69*4882a593Smuzhiyun 		goto error;
70*4882a593Smuzhiyun 	}
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun 	res = rk_cipher_init(&cipher_cfg, &hdl_cipher1);
73*4882a593Smuzhiyun 	if (res) {
74*4882a593Smuzhiyun 		E_TRACE("rk_cipher_init error[%x]\n", res);
75*4882a593Smuzhiyun 		goto error;
76*4882a593Smuzhiyun 	}
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun 	res = rk_cipher_init(&cipher_cfg, &hdl_cipher2);
79*4882a593Smuzhiyun 	if (res != RK_CRYPTO_ERR_BUSY) {
80*4882a593Smuzhiyun 		E_TRACE("rk_cipher_init should be busy[%x]\n", res);
81*4882a593Smuzhiyun 		goto error;
82*4882a593Smuzhiyun 	}
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun 	memset(&hash_cfg, 0x00, sizeof(hash_cfg));
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun 	hash_cfg.algo = RK_ALGO_HMAC_SHA256;
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun 	res = rk_hash_init(&hash_cfg, &hdl_hash1);
89*4882a593Smuzhiyun 	if (res != RK_CRYPTO_ERR_BUSY) {
90*4882a593Smuzhiyun 		E_TRACE("rk_hash_init should be busy[%x]\n", res);
91*4882a593Smuzhiyun 		goto error;
92*4882a593Smuzhiyun 	}
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun 	rk_cipher_final(hdl_cipher1);
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun 	res = rk_hash_init(&hash_cfg, &hdl_hash2);
97*4882a593Smuzhiyun 	if (res) {
98*4882a593Smuzhiyun 		E_TRACE("rk_cipher_init error[%x]\n", res);
99*4882a593Smuzhiyun 		goto error;
100*4882a593Smuzhiyun 	}
101*4882a593Smuzhiyun 
102*4882a593Smuzhiyun 	res = rk_cipher_init(&cipher_cfg, &hdl_cipher2);
103*4882a593Smuzhiyun 	if (res != RK_CRYPTO_ERR_BUSY) {
104*4882a593Smuzhiyun 		E_TRACE("rk_cipher_init should be busy[%x]\n", res);
105*4882a593Smuzhiyun 		goto error;
106*4882a593Smuzhiyun 	}
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun 	rk_hash_final(hdl_hash2, NULL);
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun 	rk_crypto_deinit();
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun 	/* Test normal calculation again */
113*4882a593Smuzhiyun 	res = test_cipher(1);
114*4882a593Smuzhiyun 	if (res) {
115*4882a593Smuzhiyun 		E_TRACE("test_cipher error[%x]\n", res);
116*4882a593Smuzhiyun 		goto error;
117*4882a593Smuzhiyun 	}
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun 	res = test_hash(1);
120*4882a593Smuzhiyun 	if (res) {
121*4882a593Smuzhiyun 		E_TRACE("test_hash error[%x]\n", res);
122*4882a593Smuzhiyun 		goto error;
123*4882a593Smuzhiyun 	}
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun 	res = test_hmac(1);
126*4882a593Smuzhiyun 	if (res) {
127*4882a593Smuzhiyun 		E_TRACE("test_hash error[%x]\n", res);
128*4882a593Smuzhiyun 		goto error;
129*4882a593Smuzhiyun 	}
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun 	printf("=============== Non-multithreaded testing pass =============\n");
132*4882a593Smuzhiyun 
133*4882a593Smuzhiyun 	return RK_CRYPTO_SUCCESS;
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun error:
136*4882a593Smuzhiyun 	printf("=============== Non-multithreaded testing fail =============\n");
137*4882a593Smuzhiyun 	return res;
138*4882a593Smuzhiyun }
139*4882a593Smuzhiyun 
test_multi(void)140*4882a593Smuzhiyun RK_RES test_multi(void)
141*4882a593Smuzhiyun {
142*4882a593Smuzhiyun 	int cfd;
143*4882a593Smuzhiyun 	RK_RES res = RK_CRYPTO_ERR_GENERIC;
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun 	res = rk_crypto_init();
146*4882a593Smuzhiyun 	if (res) {
147*4882a593Smuzhiyun 		printf("rk_crypto_init error %08x\n", res);
148*4882a593Smuzhiyun 		return res;
149*4882a593Smuzhiyun 	}
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun 	/* Open the crypto device */
152*4882a593Smuzhiyun 	cfd = open("/dev/crypto", O_RDWR, 0);
153*4882a593Smuzhiyun 	if (cfd < 0) {
154*4882a593Smuzhiyun 		if (errno != EBUSY) {
155*4882a593Smuzhiyun 			printf("An unexpected return value of init[%x]\n", res);
156*4882a593Smuzhiyun 			res = RK_CRYPTO_ERR_GENERIC;
157*4882a593Smuzhiyun 			goto exit;
158*4882a593Smuzhiyun 		}
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun 		res = test_nosupport_multi();
161*4882a593Smuzhiyun 	} else {
162*4882a593Smuzhiyun 		close(cfd);
163*4882a593Smuzhiyun 		res = test_support_multi();
164*4882a593Smuzhiyun 	}
165*4882a593Smuzhiyun 
166*4882a593Smuzhiyun exit:
167*4882a593Smuzhiyun 	return res;
168*4882a593Smuzhiyun }
169*4882a593Smuzhiyun 
170