xref: /OK3568_Linux_fs/external/security/librkcrypto/demo/demo_cipher.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[16] = {
15*4882a593Smuzhiyun 	0x10, 0x44, 0x80, 0xb3, 0x88, 0x5f, 0x02, 0x03,
16*4882a593Smuzhiyun 	0x05, 0x21, 0x07, 0xc9, 0x44, 0x00, 0x1b, 0x80,
17*4882a593Smuzhiyun };
18*4882a593Smuzhiyun static uint8_t input[16] = {
19*4882a593Smuzhiyun 	0xc9, 0x07, 0x21, 0x05, 0x80, 0x1b, 0x00, 0x44,
20*4882a593Smuzhiyun 	0xac, 0x13, 0xfb, 0x23, 0x93, 0x4a, 0x66, 0xe4,
21*4882a593Smuzhiyun };
22*4882a593Smuzhiyun static uint8_t expected_enc[16] = {
23*4882a593Smuzhiyun 	0xeb, 0xe7, 0xde, 0x12, 0x8d, 0x77, 0xf4, 0xe8,
24*4882a593Smuzhiyun 	0x83, 0x4a, 0x63, 0x1d, 0x0e, 0xcc, 0xdb, 0x1c,
25*4882a593Smuzhiyun };
26*4882a593Smuzhiyun static uint32_t algo = RK_ALGO_AES;
27*4882a593Smuzhiyun static uint32_t mode = RK_CIPHER_MODE_CBC;
28*4882a593Smuzhiyun 
demo_cipher(void)29*4882a593Smuzhiyun RK_RES demo_cipher(void)
30*4882a593Smuzhiyun {
31*4882a593Smuzhiyun 	RK_RES res = RK_CRYPTO_ERR_GENERIC;
32*4882a593Smuzhiyun 	rk_cipher_config config;
33*4882a593Smuzhiyun 	uint32_t key_len = sizeof(key_0);
34*4882a593Smuzhiyun 	uint32_t data_len = sizeof(input);
35*4882a593Smuzhiyun 	rk_handle handle = 0;
36*4882a593Smuzhiyun 	rk_crypto_mem *in = NULL;
37*4882a593Smuzhiyun 	rk_crypto_mem *out = NULL;
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun 	res = rk_crypto_init();
40*4882a593Smuzhiyun 	if (res) {
41*4882a593Smuzhiyun 		printf("rk_crypto_init error! res: 0x%08x\n", res);
42*4882a593Smuzhiyun 		return res;
43*4882a593Smuzhiyun 	}
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun 	in = rk_crypto_mem_alloc(data_len);
46*4882a593Smuzhiyun 	if (!in) {
47*4882a593Smuzhiyun 		printf("malloc %uByte error!\n", data_len);
48*4882a593Smuzhiyun 		res = RK_CRYPTO_ERR_GENERIC;
49*4882a593Smuzhiyun 		goto exit;
50*4882a593Smuzhiyun 	}
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun 	out = rk_crypto_mem_alloc(data_len);
53*4882a593Smuzhiyun 	if (!out) {
54*4882a593Smuzhiyun 		printf("malloc %uByte error!\n", data_len);
55*4882a593Smuzhiyun 		res = RK_CRYPTO_ERR_GENERIC;
56*4882a593Smuzhiyun 		goto exit;
57*4882a593Smuzhiyun 	}
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun 	config.algo      = algo;
60*4882a593Smuzhiyun 	config.mode      = mode;
61*4882a593Smuzhiyun 	config.operation = RK_OP_CIPHER_ENC;
62*4882a593Smuzhiyun 	config.key_len   = key_len;
63*4882a593Smuzhiyun 	config.reserved  = NULL;
64*4882a593Smuzhiyun 	memcpy(config.key, key_0, key_len);
65*4882a593Smuzhiyun 	memcpy(config.iv, iv, sizeof(iv));
66*4882a593Smuzhiyun 	memcpy(in->vaddr, input, data_len);
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun 	res = rk_cipher_init(&config, &handle);
69*4882a593Smuzhiyun 	if (res) {
70*4882a593Smuzhiyun 		printf("rk_cipher_init error! res: 0x%08x\n", res);
71*4882a593Smuzhiyun 		goto exit;
72*4882a593Smuzhiyun 	}
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun 	res = rk_cipher_crypt(handle, in->dma_fd, out->dma_fd, data_len);
75*4882a593Smuzhiyun 	if (res) {
76*4882a593Smuzhiyun 		rk_cipher_final(handle);
77*4882a593Smuzhiyun 		printf("rk_cipher_crypt error[%x]\n", res);
78*4882a593Smuzhiyun 		goto exit;
79*4882a593Smuzhiyun 	}
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun 	rk_cipher_final(handle);
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun 	if (memcmp(out->vaddr, expected_enc, data_len)) {
84*4882a593Smuzhiyun 		printf("ENC result not equal to expected value, error!\n");
85*4882a593Smuzhiyun 		res = RK_CRYPTO_ERR_GENERIC;
86*4882a593Smuzhiyun 		goto exit;
87*4882a593Smuzhiyun 	}
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun 	printf("Test CIPHER ENC success!\n");
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun 	config.operation = RK_OP_CIPHER_DEC;
92*4882a593Smuzhiyun 	memcpy(config.iv, iv, sizeof(iv));
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun 	res = rk_cipher_init(&config, &handle);
95*4882a593Smuzhiyun 	if (res) {
96*4882a593Smuzhiyun 		printf("rk_cipher_init error! res: 0x%08x\n", res);
97*4882a593Smuzhiyun 		goto exit;
98*4882a593Smuzhiyun 	}
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun 	res = rk_cipher_crypt(handle, out->dma_fd, out->dma_fd, data_len);
101*4882a593Smuzhiyun 	if (res) {
102*4882a593Smuzhiyun 		rk_cipher_final(handle);
103*4882a593Smuzhiyun 		printf("rk_cipher_crypt error[%x]\n", res);
104*4882a593Smuzhiyun 		goto exit;
105*4882a593Smuzhiyun 	}
106*4882a593Smuzhiyun 
107*4882a593Smuzhiyun 	rk_cipher_final(handle);
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun 	if (memcmp(out->vaddr, input, data_len)) {
110*4882a593Smuzhiyun 		printf("DEC result not equal to expected value, error!\n");
111*4882a593Smuzhiyun 		res = RK_CRYPTO_ERR_GENERIC;
112*4882a593Smuzhiyun 		goto exit;
113*4882a593Smuzhiyun 	}
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun 	printf("Test CIPHER DEC success!\n");
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun exit:
118*4882a593Smuzhiyun 	rk_crypto_mem_free(in);
119*4882a593Smuzhiyun 	rk_crypto_mem_free(out);
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun 	rk_crypto_deinit();
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun 	return res;
124*4882a593Smuzhiyun }
125*4882a593Smuzhiyun 
demo_cipher_virt(void)126*4882a593Smuzhiyun RK_RES demo_cipher_virt(void)
127*4882a593Smuzhiyun {
128*4882a593Smuzhiyun 	RK_RES res = RK_CRYPTO_ERR_GENERIC;
129*4882a593Smuzhiyun 	rk_cipher_config config;
130*4882a593Smuzhiyun 	uint32_t key_len = sizeof(key_0);
131*4882a593Smuzhiyun 	uint8_t output[16];
132*4882a593Smuzhiyun 	uint32_t data_len = sizeof(input);
133*4882a593Smuzhiyun 	rk_handle handle = 0;
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun 	res = rk_crypto_init();
136*4882a593Smuzhiyun 	if (res) {
137*4882a593Smuzhiyun 		printf("rk_crypto_init error! res: 0x%08x\n", res);
138*4882a593Smuzhiyun 		return res;
139*4882a593Smuzhiyun 	}
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun 	config.algo      = algo;
142*4882a593Smuzhiyun 	config.mode      = mode;
143*4882a593Smuzhiyun 	config.operation = RK_OP_CIPHER_ENC;
144*4882a593Smuzhiyun 	config.key_len   = key_len;
145*4882a593Smuzhiyun 	config.reserved  = NULL;
146*4882a593Smuzhiyun 	memcpy(config.key, key_0, key_len);
147*4882a593Smuzhiyun 	memcpy(config.iv, iv, sizeof(iv));
148*4882a593Smuzhiyun 	memset(output, 0, sizeof(output));
149*4882a593Smuzhiyun 
150*4882a593Smuzhiyun 	res = rk_cipher_init(&config, &handle);
151*4882a593Smuzhiyun 	if (res) {
152*4882a593Smuzhiyun 		printf("rk_cipher_init error! res: 0x%08x\n", res);
153*4882a593Smuzhiyun 		goto exit;
154*4882a593Smuzhiyun 	}
155*4882a593Smuzhiyun 
156*4882a593Smuzhiyun 	res = rk_cipher_crypt_virt(handle, input, output, data_len);
157*4882a593Smuzhiyun 	if (res) {
158*4882a593Smuzhiyun 		rk_cipher_final(handle);
159*4882a593Smuzhiyun 		printf("rk_cipher_crypt_virt error[%x]\n", res);
160*4882a593Smuzhiyun 		goto exit;
161*4882a593Smuzhiyun 	}
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun 	rk_cipher_final(handle);
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun 	if (memcmp(output, expected_enc, data_len)) {
166*4882a593Smuzhiyun 		printf("ENC result not equal to expected value, error!\n");
167*4882a593Smuzhiyun 		res = RK_CRYPTO_ERR_GENERIC;
168*4882a593Smuzhiyun 		goto exit;
169*4882a593Smuzhiyun 	}
170*4882a593Smuzhiyun 
171*4882a593Smuzhiyun 	printf("Test CIPHER_VIRT ENC success!\n");
172*4882a593Smuzhiyun 
173*4882a593Smuzhiyun 	config.operation = RK_OP_CIPHER_DEC;
174*4882a593Smuzhiyun 	memcpy(config.iv, iv, sizeof(iv));
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun 	res = rk_cipher_init(&config, &handle);
177*4882a593Smuzhiyun 	if (res) {
178*4882a593Smuzhiyun 		printf("rk_cipher_init error! res: 0x%08x\n", res);
179*4882a593Smuzhiyun 		goto exit;
180*4882a593Smuzhiyun 	}
181*4882a593Smuzhiyun 
182*4882a593Smuzhiyun 	res = rk_cipher_crypt_virt(handle, output, output, data_len);
183*4882a593Smuzhiyun 	if (res) {
184*4882a593Smuzhiyun 		rk_cipher_final(handle);
185*4882a593Smuzhiyun 		printf("rk_cipher_crypt_virt error[%x]\n", res);
186*4882a593Smuzhiyun 		goto exit;
187*4882a593Smuzhiyun 	}
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun 	rk_cipher_final(handle);
190*4882a593Smuzhiyun 
191*4882a593Smuzhiyun 	if (memcmp(output, input, data_len)) {
192*4882a593Smuzhiyun 		printf("DEC result not equal to expected value, error!\n");
193*4882a593Smuzhiyun 		res = RK_CRYPTO_ERR_GENERIC;
194*4882a593Smuzhiyun 		goto exit;
195*4882a593Smuzhiyun 	}
196*4882a593Smuzhiyun 
197*4882a593Smuzhiyun 	printf("Test CIPHER_VIRT DEC success!\n");
198*4882a593Smuzhiyun 
199*4882a593Smuzhiyun exit:
200*4882a593Smuzhiyun 	rk_crypto_deinit();
201*4882a593Smuzhiyun 	return res;
202*4882a593Smuzhiyun }
203