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