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