xref: /OK3568_Linux_fs/external/security/librkcrypto/demo/demo_ae.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright (c) 2022 Rockchip Electronics Co. Ltd.
3*4882a593Smuzhiyun  */
4*4882a593Smuzhiyun #include <stdio.h>
5*4882a593Smuzhiyun #include <string.h>
6*4882a593Smuzhiyun #include "rkcrypto_core.h"
7*4882a593Smuzhiyun #include "rkcrypto_mem.h"
8*4882a593Smuzhiyun #include "rkcrypto_demo.h"
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun static uint8_t key_0[16] = {
11*4882a593Smuzhiyun 	0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01,
12*4882a593Smuzhiyun 	0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01,
13*4882a593Smuzhiyun };
14*4882a593Smuzhiyun static uint8_t iv[12] = {
15*4882a593Smuzhiyun 	0x10, 0x44, 0x80, 0xb3, 0x88, 0x5f, 0x02, 0x03,
16*4882a593Smuzhiyun 	0x05, 0x21, 0x07, 0xc9,
17*4882a593Smuzhiyun };
18*4882a593Smuzhiyun static uint8_t aad_in[32] = {
19*4882a593Smuzhiyun 	0x10, 0x44, 0x80, 0xb3, 0x88, 0x5f, 0x02, 0x03,
20*4882a593Smuzhiyun 	0xac, 0x13, 0xfb, 0x23, 0x93, 0x4a, 0x66, 0xe4,
21*4882a593Smuzhiyun 	0x05, 0x21, 0x07, 0xc9, 0x44, 0x00, 0x1b, 0x80,
22*4882a593Smuzhiyun 	0xeb, 0xe7, 0xde, 0x12, 0x8d, 0x77, 0xf4, 0xe8,
23*4882a593Smuzhiyun };
24*4882a593Smuzhiyun static uint8_t input[32] = {
25*4882a593Smuzhiyun 	0xc9, 0x07, 0x21, 0x05, 0x80, 0x1b, 0x00, 0x44,
26*4882a593Smuzhiyun 	0xac, 0x13, 0xfb, 0x23, 0x93, 0x4a, 0x66, 0xe4,
27*4882a593Smuzhiyun 	0xc9, 0x07, 0x21, 0x05, 0x80, 0x1b, 0x00, 0x44,
28*4882a593Smuzhiyun 	0xac, 0x13, 0xfb, 0x23, 0x93, 0x4a, 0x66, 0xe4,
29*4882a593Smuzhiyun };
30*4882a593Smuzhiyun static uint8_t expected_enc[32] = {
31*4882a593Smuzhiyun 	0x92, 0x8e, 0xc6, 0x87, 0x7f, 0xca, 0xc7, 0x96,
32*4882a593Smuzhiyun 	0xbf, 0xa7, 0x86, 0xb4, 0xfe, 0xda, 0xf3, 0xec,
33*4882a593Smuzhiyun 	0xdc, 0x24, 0x4d, 0x45, 0x78, 0x3a, 0xe3, 0x3b,
34*4882a593Smuzhiyun 	0x55, 0xbf, 0x9b, 0x9f, 0xbd, 0x4c, 0xf7, 0xe7,
35*4882a593Smuzhiyun };
36*4882a593Smuzhiyun static uint8_t expected_tag[16] = {
37*4882a593Smuzhiyun 	0x07, 0xf2, 0x31, 0x06, 0xc6, 0xc1, 0x7d, 0x1e,
38*4882a593Smuzhiyun 	0xe7, 0x79, 0xe0, 0x2d, 0x27, 0x5b, 0x5d, 0x12,
39*4882a593Smuzhiyun };
40*4882a593Smuzhiyun static uint32_t algo = RK_ALGO_AES;
41*4882a593Smuzhiyun static uint32_t mode = RK_CIPHER_MODE_GCM;
42*4882a593Smuzhiyun 
set_ae_config(rk_ae_config * config,uint32_t algo,uint32_t mode,uint32_t operation,uint8_t * key,uint32_t key_len,uint8_t * iv,uint32_t iv_len,uint32_t tag_len,uint32_t aad_len)43*4882a593Smuzhiyun static RK_RES set_ae_config(rk_ae_config *config, uint32_t algo, uint32_t mode, uint32_t operation,
44*4882a593Smuzhiyun 			    uint8_t *key, uint32_t key_len, uint8_t *iv, uint32_t iv_len,
45*4882a593Smuzhiyun 			    uint32_t tag_len, uint32_t aad_len)
46*4882a593Smuzhiyun {
47*4882a593Smuzhiyun 	if (!config || !key || !iv)
48*4882a593Smuzhiyun 		return RK_CRYPTO_ERR_GENERIC;
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun 	config->algo      = algo;
51*4882a593Smuzhiyun 	config->mode      = mode;
52*4882a593Smuzhiyun 	config->operation = operation;
53*4882a593Smuzhiyun 	config->key_len   = key_len;
54*4882a593Smuzhiyun 	config->iv_len    = iv_len;
55*4882a593Smuzhiyun 	config->tag_len   = tag_len;
56*4882a593Smuzhiyun 	config->aad_len   = aad_len;
57*4882a593Smuzhiyun 	config->reserved  = NULL;
58*4882a593Smuzhiyun 	memcpy(config->key, key, key_len);
59*4882a593Smuzhiyun 	memcpy(config->iv, iv, iv_len);
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun 	return RK_CRYPTO_SUCCESS;
62*4882a593Smuzhiyun }
63*4882a593Smuzhiyun 
demo_ae(void)64*4882a593Smuzhiyun RK_RES demo_ae(void)
65*4882a593Smuzhiyun {
66*4882a593Smuzhiyun 	RK_RES res = RK_CRYPTO_ERR_GENERIC;
67*4882a593Smuzhiyun 	rk_ae_config config;
68*4882a593Smuzhiyun 	uint32_t data_len = sizeof(input);
69*4882a593Smuzhiyun 	uint32_t aad_len = sizeof(aad_in);
70*4882a593Smuzhiyun 	rk_handle handle = 0;
71*4882a593Smuzhiyun 	rk_crypto_mem *in = NULL;
72*4882a593Smuzhiyun 	rk_crypto_mem *out = NULL;
73*4882a593Smuzhiyun 	rk_crypto_mem *aad = NULL;
74*4882a593Smuzhiyun 	uint8_t tag[16];
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun 	res = rk_crypto_init();
77*4882a593Smuzhiyun 	if (res) {
78*4882a593Smuzhiyun 		printf("rk_crypto_init error! res: 0x%08x\n", res);
79*4882a593Smuzhiyun 		return res;
80*4882a593Smuzhiyun 	}
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun 	in = rk_crypto_mem_alloc(data_len);
83*4882a593Smuzhiyun 	if (!in) {
84*4882a593Smuzhiyun 		printf("malloc %uByte error!\n", data_len);
85*4882a593Smuzhiyun 		res = RK_CRYPTO_ERR_GENERIC;
86*4882a593Smuzhiyun 		goto exit;
87*4882a593Smuzhiyun 	}
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun 	out = rk_crypto_mem_alloc(data_len);
90*4882a593Smuzhiyun 	if (!out) {
91*4882a593Smuzhiyun 		printf("malloc %uByte error!\n", data_len);
92*4882a593Smuzhiyun 		res = RK_CRYPTO_ERR_GENERIC;
93*4882a593Smuzhiyun 		goto exit;
94*4882a593Smuzhiyun 	}
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun 	aad = rk_crypto_mem_alloc(aad_len);
97*4882a593Smuzhiyun 	if (!aad) {
98*4882a593Smuzhiyun 		printf("malloc %uByte error!\n", aad_len);
99*4882a593Smuzhiyun 		res = RK_CRYPTO_ERR_GENERIC;
100*4882a593Smuzhiyun 		goto exit;
101*4882a593Smuzhiyun 	}
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun 	res = set_ae_config(&config, algo, mode, RK_OP_CIPHER_ENC, key_0, sizeof(key_0), iv,
104*4882a593Smuzhiyun 			    sizeof(iv), sizeof(expected_tag), aad_len);
105*4882a593Smuzhiyun 	if (res) {
106*4882a593Smuzhiyun 		printf("set_ae_config error! res: 0x%08x\n", res);
107*4882a593Smuzhiyun 		goto exit;
108*4882a593Smuzhiyun 	}
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun 	memcpy(in->vaddr, input, data_len);
111*4882a593Smuzhiyun 	memcpy(aad->vaddr, aad_in, aad_len);
112*4882a593Smuzhiyun 	memset(tag, 0, sizeof(tag));
113*4882a593Smuzhiyun 
114*4882a593Smuzhiyun 	res = rk_ae_init(&config, &handle);
115*4882a593Smuzhiyun 	if (res) {
116*4882a593Smuzhiyun 		printf("rk_ae_init error! res: 0x%08x\n", res);
117*4882a593Smuzhiyun 		goto exit;
118*4882a593Smuzhiyun 	}
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun 	res = rk_ae_set_aad(handle, aad->dma_fd);
121*4882a593Smuzhiyun 	if (res) {
122*4882a593Smuzhiyun 		printf("rk_ae_set_aad error! res: 0x%08x\n", res);
123*4882a593Smuzhiyun 		rk_ae_final(handle);
124*4882a593Smuzhiyun 		goto exit;
125*4882a593Smuzhiyun 	}
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun 	res = rk_ae_crypt(handle, in->dma_fd, out->dma_fd, data_len, tag);
128*4882a593Smuzhiyun 	if (res) {
129*4882a593Smuzhiyun 		printf("rk_ae_crypt error! res: 0x%08x\n", res);
130*4882a593Smuzhiyun 		rk_ae_final(handle);
131*4882a593Smuzhiyun 		goto exit;
132*4882a593Smuzhiyun 	}
133*4882a593Smuzhiyun 
134*4882a593Smuzhiyun 	rk_ae_final(handle);
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun 	if (memcmp(out->vaddr, expected_enc, data_len)) {
137*4882a593Smuzhiyun 		printf("ENC result not equal to expected value, error!\n");
138*4882a593Smuzhiyun 		res = RK_CRYPTO_ERR_GENERIC;
139*4882a593Smuzhiyun 		goto exit;
140*4882a593Smuzhiyun 	}
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun 	if (memcmp(tag, expected_tag, sizeof(expected_tag))) {
143*4882a593Smuzhiyun 		printf("ENC tag not equal to expected value, error!\n");
144*4882a593Smuzhiyun 		res = RK_CRYPTO_ERR_GENERIC;
145*4882a593Smuzhiyun 		goto exit;
146*4882a593Smuzhiyun 	}
147*4882a593Smuzhiyun 
148*4882a593Smuzhiyun 	printf("Test AE ENC success!\n");
149*4882a593Smuzhiyun 
150*4882a593Smuzhiyun 	config.operation = RK_OP_CIPHER_DEC;
151*4882a593Smuzhiyun 	memcpy(config.iv, iv, sizeof(iv));
152*4882a593Smuzhiyun 
153*4882a593Smuzhiyun 	res = rk_ae_init(&config, &handle);
154*4882a593Smuzhiyun 	if (res) {
155*4882a593Smuzhiyun 		printf("rk_ae_init error! res: 0x%08x\n", res);
156*4882a593Smuzhiyun 		goto exit;
157*4882a593Smuzhiyun 	}
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun 	res = rk_ae_set_aad(handle, aad->dma_fd);
160*4882a593Smuzhiyun 	if (res) {
161*4882a593Smuzhiyun 		printf("rk_ae_set_aad error! res: 0x%08x\n", res);
162*4882a593Smuzhiyun 		rk_ae_final(handle);
163*4882a593Smuzhiyun 		goto exit;
164*4882a593Smuzhiyun 	}
165*4882a593Smuzhiyun 
166*4882a593Smuzhiyun 	/* Ensure the the tag is correct, it will be checked when decrypting */
167*4882a593Smuzhiyun 	res = rk_ae_crypt(handle, out->dma_fd, out->dma_fd, data_len, tag);
168*4882a593Smuzhiyun 	if (res) {
169*4882a593Smuzhiyun 		printf("rk_ae_crypt error! res: 0x%08x\n", res);
170*4882a593Smuzhiyun 		rk_ae_final(handle);
171*4882a593Smuzhiyun 		goto exit;
172*4882a593Smuzhiyun 	}
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun 	rk_ae_final(handle);
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun 	if (memcmp(out->vaddr, input, data_len)) {
177*4882a593Smuzhiyun 		printf("DEC result not equal to expected value, error!\n");
178*4882a593Smuzhiyun 		res = RK_CRYPTO_ERR_GENERIC;
179*4882a593Smuzhiyun 		goto exit;
180*4882a593Smuzhiyun 	}
181*4882a593Smuzhiyun 
182*4882a593Smuzhiyun 	printf("Test AE DEC success!\n");
183*4882a593Smuzhiyun 
184*4882a593Smuzhiyun exit:
185*4882a593Smuzhiyun 	rk_crypto_mem_free(in);
186*4882a593Smuzhiyun 	rk_crypto_mem_free(out);
187*4882a593Smuzhiyun 	rk_crypto_mem_free(aad);
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun 	rk_crypto_deinit();
190*4882a593Smuzhiyun 
191*4882a593Smuzhiyun 	return res;
192*4882a593Smuzhiyun }
193*4882a593Smuzhiyun 
demo_ae_virt(void)194*4882a593Smuzhiyun RK_RES demo_ae_virt(void)
195*4882a593Smuzhiyun {
196*4882a593Smuzhiyun 	RK_RES res = RK_CRYPTO_ERR_GENERIC;
197*4882a593Smuzhiyun 	rk_ae_config config;
198*4882a593Smuzhiyun 	uint32_t data_len = sizeof(input);
199*4882a593Smuzhiyun 	uint8_t output[32];
200*4882a593Smuzhiyun 	uint8_t tag[16];
201*4882a593Smuzhiyun 	rk_handle handle = 0;
202*4882a593Smuzhiyun 
203*4882a593Smuzhiyun 	res = rk_crypto_init();
204*4882a593Smuzhiyun 	if (res) {
205*4882a593Smuzhiyun 		printf("rk_crypto_init error! res: 0x%08x\n", res);
206*4882a593Smuzhiyun 		return res;
207*4882a593Smuzhiyun 	}
208*4882a593Smuzhiyun 
209*4882a593Smuzhiyun 	res = set_ae_config(&config, algo, mode, RK_OP_CIPHER_ENC, key_0, sizeof(key_0), iv,
210*4882a593Smuzhiyun 			    sizeof(iv), sizeof(expected_tag), sizeof(aad_in));
211*4882a593Smuzhiyun 	if (res) {
212*4882a593Smuzhiyun 		printf("set_ae_config error! res: 0x%08x\n", res);
213*4882a593Smuzhiyun 		goto exit;
214*4882a593Smuzhiyun 	}
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun 	memset(tag, 0, sizeof(tag));
217*4882a593Smuzhiyun 
218*4882a593Smuzhiyun 	res = rk_ae_init(&config, &handle);
219*4882a593Smuzhiyun 	if (res) {
220*4882a593Smuzhiyun 		printf("rk_ae_init error! res: 0x%08x\n", res);
221*4882a593Smuzhiyun 		goto exit;
222*4882a593Smuzhiyun 	}
223*4882a593Smuzhiyun 
224*4882a593Smuzhiyun 	res = rk_ae_set_aad_virt(handle, aad_in);
225*4882a593Smuzhiyun 	if (res) {
226*4882a593Smuzhiyun 		printf("rk_ae_set_aad error! res: 0x%08x\n", res);
227*4882a593Smuzhiyun 		rk_ae_final(handle);
228*4882a593Smuzhiyun 		goto exit;
229*4882a593Smuzhiyun 	}
230*4882a593Smuzhiyun 
231*4882a593Smuzhiyun 	res = rk_ae_crypt_virt(handle, input, output, data_len, tag);
232*4882a593Smuzhiyun 	if (res) {
233*4882a593Smuzhiyun 		rk_ae_final(handle);
234*4882a593Smuzhiyun 		printf("rk_ae_crypt_virt error[%x]\n", res);
235*4882a593Smuzhiyun 		goto exit;
236*4882a593Smuzhiyun 	}
237*4882a593Smuzhiyun 
238*4882a593Smuzhiyun 	rk_ae_final(handle);
239*4882a593Smuzhiyun 
240*4882a593Smuzhiyun 	if (memcmp(output, expected_enc, data_len)) {
241*4882a593Smuzhiyun 		printf("ENC result not equal to expected value, error!\n");
242*4882a593Smuzhiyun 		res = RK_CRYPTO_ERR_GENERIC;
243*4882a593Smuzhiyun 		goto exit;
244*4882a593Smuzhiyun 	}
245*4882a593Smuzhiyun 
246*4882a593Smuzhiyun 	if (memcmp(tag, expected_tag, sizeof(expected_tag))) {
247*4882a593Smuzhiyun 		printf("ENC result not equal to expected value, error!\n");
248*4882a593Smuzhiyun 		res = RK_CRYPTO_ERR_GENERIC;
249*4882a593Smuzhiyun 		goto exit;
250*4882a593Smuzhiyun 	}
251*4882a593Smuzhiyun 
252*4882a593Smuzhiyun 	printf("Test AE_VIRT ENC success!\n");
253*4882a593Smuzhiyun 
254*4882a593Smuzhiyun 	config.operation = RK_OP_CIPHER_DEC;
255*4882a593Smuzhiyun 	memcpy(config.iv, iv, sizeof(iv));
256*4882a593Smuzhiyun 
257*4882a593Smuzhiyun 	res = rk_ae_init(&config, &handle);
258*4882a593Smuzhiyun 	if (res) {
259*4882a593Smuzhiyun 		printf("rk_ae_init error! res: 0x%08x\n", res);
260*4882a593Smuzhiyun 		goto exit;
261*4882a593Smuzhiyun 	}
262*4882a593Smuzhiyun 
263*4882a593Smuzhiyun 	res = rk_ae_set_aad_virt(handle, aad_in);
264*4882a593Smuzhiyun 	if (res) {
265*4882a593Smuzhiyun 		printf("rk_ae_set_aad error! res: 0x%08x\n", res);
266*4882a593Smuzhiyun 		rk_ae_final(handle);
267*4882a593Smuzhiyun 		goto exit;
268*4882a593Smuzhiyun 	}
269*4882a593Smuzhiyun 
270*4882a593Smuzhiyun 	/* Ensure the the tag is correct, it will be checked when decrypting */
271*4882a593Smuzhiyun 	res = rk_ae_crypt_virt(handle, output, output, data_len, tag);
272*4882a593Smuzhiyun 	if (res) {
273*4882a593Smuzhiyun 		rk_ae_final(handle);
274*4882a593Smuzhiyun 		printf("rk_ae_crypt_virt error[%x]\n", res);
275*4882a593Smuzhiyun 		goto exit;
276*4882a593Smuzhiyun 	}
277*4882a593Smuzhiyun 
278*4882a593Smuzhiyun 	rk_ae_final(handle);
279*4882a593Smuzhiyun 
280*4882a593Smuzhiyun 	if (memcmp(output, input, data_len)) {
281*4882a593Smuzhiyun 		printf("DEC result not equal to expected value, error!\n");
282*4882a593Smuzhiyun 		res = RK_CRYPTO_ERR_GENERIC;
283*4882a593Smuzhiyun 		goto exit;
284*4882a593Smuzhiyun 	}
285*4882a593Smuzhiyun 
286*4882a593Smuzhiyun 	printf("Test AE_VIRT DEC success!\n");
287*4882a593Smuzhiyun 
288*4882a593Smuzhiyun exit:
289*4882a593Smuzhiyun 	rk_crypto_deinit();
290*4882a593Smuzhiyun 	return res;
291*4882a593Smuzhiyun }
292