xref: /OK3568_Linux_fs/external/security/librkcrypto/demo/demo_hash.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * Copyright (c) 2022 Rockchip Electronics Co. Ltd.
3  */
4 #include <stdio.h>
5 #include <string.h>
6 #include "rkcrypto_core.h"
7 #include "rkcrypto_mem.h"
8 #include "rkcrypto_demo.h"
9 
10 #define HASH_MAX_LEN	64
11 static uint8_t input[16] = {
12 	0xc9, 0x07, 0x21, 0x05, 0x80, 0x1b, 0x00, 0x44,
13 	0xac, 0x13, 0xfb, 0x23, 0x93, 0x4a, 0x66, 0xe4,
14 };
15 static uint8_t expected_hash[] = {
16 	0x5e, 0xd1, 0x70, 0xb2, 0x0a, 0xcd, 0xf2, 0x8e,
17 	0xee, 0x28, 0xd7, 0x70, 0x78, 0x79, 0x2d, 0xf5,
18 	0x83, 0xba, 0xf4, 0x52, 0xce, 0x3f, 0x71, 0x70,
19 	0x15, 0x4f, 0x2c, 0x48, 0xbc, 0x51, 0x23, 0x6f,
20 };
21 static uint8_t expected_hmac[] = {
22 	0x91, 0xcb, 0x13, 0x85, 0x1e, 0xa1, 0xbb, 0xc2,
23 	0xa4, 0x0f, 0x79, 0x9f, 0xc8, 0xb1, 0x95, 0x3b,
24 	0x2d, 0x3f, 0xe7, 0xe4, 0x6d, 0x95, 0x19, 0x34,
25 	0x97, 0x7f, 0x63, 0x46, 0xff, 0x92, 0xa3, 0x51,
26 };
27 static uint8_t key[SHA256_BLOCK_SIZE] = {
28 	0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01,
29 	0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01,
30 	0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
31 	0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
32 	0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01,
33 	0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01,
34 	0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
35 	0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
36 };
37 static uint32_t hash_algo = RK_ALGO_SHA256;
38 static uint32_t hmac_algo = RK_ALGO_HMAC_SHA256;
39 
demo_hash(void)40 RK_RES demo_hash(void)
41 {
42 	RK_RES res = RK_CRYPTO_ERR_GENERIC;
43 	rk_hash_config hash_cfg;
44 	rk_handle hash_hdl = 0;
45 	uint32_t data_len = sizeof(input);
46 	rk_crypto_mem *in = NULL;
47 	uint8_t output[HASH_MAX_LEN];
48 
49 
50 	res = rk_crypto_init();
51 	if (res) {
52 		printf("rk_crypto_init error! res: 0x%08x\n", res);
53 		return res;
54 	}
55 
56 	in = rk_crypto_mem_alloc(data_len);
57 	if (!in) {
58 		printf("malloc %uByte error!\n", data_len);
59 		res = RK_CRYPTO_ERR_GENERIC;
60 		goto exit;
61 	}
62 
63 	memcpy(in->vaddr, input, data_len);
64 	memset(&hash_cfg, 0x00, sizeof(hash_cfg));
65 	hash_cfg.algo = hash_algo;
66 
67 	res = rk_hash_init(&hash_cfg, &hash_hdl);
68 	if (res) {
69 		printf("rk_hash_init error! res: 0x%08x\n", res);
70 		goto exit;
71 	}
72 
73 	res = rk_hash_update(hash_hdl, in->dma_fd, in->size);
74 	if (res) {
75 		rk_hash_final(hash_hdl, NULL);
76 		printf("rk_hash_update error = %d\n", res);
77 		goto exit;
78 	}
79 
80 	rk_hash_final(hash_hdl, output);
81 
82 	/* Verify the result */
83 	if (memcmp(output, expected_hash, sizeof(expected_hash)) != 0) {
84 		printf("HASH result not equal to expected value, error!\n");
85 		res = RK_CRYPTO_ERR_GENERIC;
86 		goto exit;
87 	}
88 
89 	printf("Test HASH success!\n");
90 
91 exit:
92 	rk_crypto_mem_free(in);
93 
94 	rk_crypto_deinit();
95 
96 	return res;
97 }
98 
demo_hash_virt(void)99 RK_RES demo_hash_virt(void)
100 {
101 	RK_RES res = RK_CRYPTO_ERR_GENERIC;
102 	rk_hash_config hash_cfg;
103 	uint8_t output[HASH_MAX_LEN];
104 	uint32_t data_len = sizeof(input);
105 	rk_handle hash_hdl = 0;
106 	uint32_t data_block = 128;
107 	uint8_t *tmp_data;
108 	uint32_t tmp_len;
109 
110 	res = rk_crypto_init();
111 	if (res) {
112 		printf("rk_crypto_init error! res: 0x%08x\n", res);
113 		return res;
114 	}
115 
116 	memset(&hash_cfg, 0x00, sizeof(hash_cfg));
117 	hash_cfg.algo = hash_algo;
118 
119 	res = rk_hash_init(&hash_cfg, &hash_hdl);
120 	if (res) {
121 		printf("rk_hash_init error! res: 0x%08x\n", res);
122 		goto exit;
123 	}
124 
125 	tmp_len  = data_len;
126 	tmp_data = input;
127 
128 	while (tmp_len) {
129 		if (tmp_len > data_block) {
130 			res = rk_hash_update_virt(hash_hdl, tmp_data, data_block);
131 			if (res) {
132 				rk_hash_final(hash_hdl, NULL);
133 				printf("rk_hash_update_virt error! res: 0x%08x\n", res);
134 				goto exit;
135 			}
136 		} else {
137 			data_block = tmp_len;
138 			res = rk_hash_update_virt(hash_hdl, tmp_data, tmp_len);
139 			if (res) {
140 				rk_hash_final(hash_hdl, NULL);
141 				printf("rk_hash_update_virt error! res: 0x%08x\n", res);
142 				goto exit;
143 			}
144 		}
145 		tmp_len -= data_block;
146 		tmp_data += data_block;
147 	}
148 
149 	rk_hash_final(hash_hdl, output);
150 
151 	if (memcmp(output, expected_hash, sizeof(expected_hash)) != 0) {
152 		printf("HASH result not equal to expected value, error!\n");
153 		res = RK_CRYPTO_ERR_GENERIC;
154 		goto exit;
155 	}
156 
157 	printf("Test HASH_VIRT success!\n");
158 
159 exit:
160 	rk_crypto_deinit();
161 	return res;
162 }
163 
demo_hmac(void)164 RK_RES demo_hmac(void)
165 {
166 	RK_RES res = RK_CRYPTO_ERR_GENERIC;
167 	rk_hash_config hash_cfg;
168 	rk_handle hash_hdl = 0;
169 	uint32_t data_len = sizeof(input);
170 	rk_crypto_mem *in = NULL;
171 	uint8_t output[HASH_MAX_LEN];
172 
173 	res = rk_crypto_init();
174 	if (res) {
175 		printf("rk_crypto_init error! res: 0x%08x\n", res);
176 		return res;
177 	}
178 
179 	in = rk_crypto_mem_alloc(data_len);
180 	if (!in) {
181 		printf("malloc %uByte error!\n", data_len);
182 		res = RK_CRYPTO_ERR_GENERIC;
183 		goto exit;
184 	}
185 
186 	memcpy(in->vaddr, input, data_len);
187 	memset(&hash_cfg, 0x00, sizeof(hash_cfg));
188 	hash_cfg.algo    = hmac_algo;
189 	hash_cfg.key     = key;
190 	hash_cfg.key_len = sizeof(key);
191 
192 	res = rk_hash_init(&hash_cfg, &hash_hdl);
193 	if (res) {
194 		printf("rk_hash_init error! res: 0x%08x\n", res);
195 		goto exit;
196 	}
197 
198 	res = rk_hash_update(hash_hdl, in->dma_fd, in->size);
199 	if (res) {
200 		rk_hash_final(hash_hdl, NULL);
201 		printf("rk_hash_update error = %d\n", res);
202 		goto exit;
203 	}
204 
205 	rk_hash_final(hash_hdl, output);
206 
207 	/* Verify the result */
208 	if (memcmp(output, expected_hmac, sizeof(expected_hmac)) != 0) {
209 		printf("HMAC result not equal to expected value, error!\n");
210 		res = RK_CRYPTO_ERR_GENERIC;
211 		goto exit;
212 	}
213 
214 	printf("Test HMAC success!\n");
215 
216 exit:
217 	rk_crypto_mem_free(in);
218 
219 	rk_crypto_deinit();
220 
221 	return res;
222 }
223 
demo_hmac_virt(void)224 RK_RES demo_hmac_virt(void)
225 {
226 	RK_RES res = RK_CRYPTO_ERR_GENERIC;
227 	rk_hash_config hash_cfg;
228 	uint8_t output[HASH_MAX_LEN];
229 	uint32_t data_len = sizeof(input);
230 	rk_handle hash_hdl = 0;
231 	uint32_t data_block = 128;
232 	uint8_t *tmp_data;
233 	uint32_t tmp_len;
234 
235 	res = rk_crypto_init();
236 	if (res) {
237 		printf("rk_crypto_init error! res: 0x%08x\n", res);
238 		return res;
239 	}
240 
241 	memset(&hash_cfg, 0x00, sizeof(hash_cfg));
242 	hash_cfg.algo    = hmac_algo;
243 	hash_cfg.key     = key;
244 	hash_cfg.key_len = sizeof(key);
245 
246 	res = rk_hash_init(&hash_cfg, &hash_hdl);
247 	if (res) {
248 		printf("rk_hash_init error! res: 0x%08x\n", res);
249 		goto exit;
250 	}
251 
252 	tmp_len  = data_len;
253 	tmp_data = input;
254 
255 	while (tmp_len) {
256 		data_block = tmp_len > data_block ? data_block : tmp_len;
257 
258 		res = rk_hash_update_virt(hash_hdl, tmp_data, data_block);
259 		if (res) {
260 			rk_hash_final(hash_hdl, NULL);
261 			printf("rk_hash_update_virt error! res: 0x%08x\n", res);
262 			goto exit;
263 		}
264 
265 		tmp_len -= data_block;
266 		tmp_data += data_block;
267 	}
268 
269 	rk_hash_final(hash_hdl, output);
270 
271 	if (memcmp(output, expected_hmac, sizeof(expected_hmac)) != 0) {
272 		printf("HASH result not equal to expected value, error!\n");
273 		res = RK_CRYPTO_ERR_GENERIC;
274 		goto exit;
275 	}
276 
277 	printf("Test HASH_VIRT success!\n");
278 
279 exit:
280 	rk_crypto_deinit();
281 	return res;
282 }
283