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