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