1 /*
2 * Copyright (c) 2022 Rockchip Electronics Co. Ltd.
3 */
4 #include <stdlib.h>
5 #include "c_model.h"
6 #include "cmode_adapter.h"
7 #include "rkcrypto_core.h"
8 #include "rkcrypto_mem.h"
9 #include "rkcrypto_otp_key.h"
10 #include "test_otp_key_crypto.h"
11 #include "test_utils.h"
12
13 uint8_t otp_key0[32] = {
14 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01,
15 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01,
16 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
17 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
18 };
19
20 uint8_t otp_key1[32] = {
21 0xdf, 0x20, 0x5a, 0xb3, 0x88, 0x50, 0x9e, 0x4f,
22 0x01, 0x21, 0xe7, 0xc9, 0x24, 0x00, 0x1b, 0x84,
23 0x2a, 0xfb, 0x83, 0xac, 0xe4, 0x61, 0x4a, 0x94,
24 0x1f, 0xf4, 0x84, 0xc3, 0x1f, 0xe5, 0x52, 0xc7,
25 };
26
27 uint8_t otp_key2[32] = {
28 0xd5, 0x20, 0xaa, 0xb3, 0x88, 0x5f, 0x9e, 0x41,
29 0x05, 0x21, 0x07, 0xc9, 0x44, 0x00, 0x1b, 0x80,
30 0x23, 0xfb, 0x13, 0xac, 0xe4, 0x66, 0x4a, 0x93,
31 0x13, 0xf4, 0x04, 0xc3, 0x3f, 0xe7, 0x52, 0xc0,
32 };
33
34 uint8_t otp_key3[32] = {
35 0x10, 0x44, 0x80, 0xb3, 0x88, 0x5f, 0x02, 0x03,
36 0x05, 0x21, 0x07, 0xc9, 0x44, 0x00, 0x1b, 0x80,
37 0x5f, 0x9e, 0x41, 0xac, 0xe4, 0x64, 0x43, 0xa3,
38 0x13, 0x06, 0x07, 0x08, 0x3f, 0xe7, 0x05, 0x06,
39 };
40
41 #define DATA_BUTT 0xFFFFFFFF
42 #define TEST_DATA_MAX (500 * 1024)
43
44 static const uint32_t test_key_ids[] = {
45 RK_OEM_OTP_KEY0,
46 RK_OEM_OTP_KEY1,
47 RK_OEM_OTP_KEY2,
48 RK_OEM_OTP_KEY3,
49 };
50 struct test_otp_key_item {
51 uint32_t algo;
52 uint32_t modes[RK_CIPHER_MODE_MAX];
53 uint32_t key_lens[4];
54 uint32_t iv_len;
55 uint32_t data_len;
56 uint32_t operations[4];
57 };
58
59 static struct test_otp_key_item test_item_tbl[] = {
60 {
61 .algo = RK_ALGO_AES,
62 .modes = {
63 RK_CIPHER_MODE_ECB,
64 RK_CIPHER_MODE_CBC,
65 RK_CIPHER_MODE_CTS,
66 RK_CIPHER_MODE_CTR,
67 RK_CIPHER_MODE_CFB,
68 RK_CIPHER_MODE_OFB,
69 DATA_BUTT,
70 },
71 .key_lens = {16, 24, 32, DATA_BUTT},
72 .iv_len = AES_BLOCK_SIZE,
73 .data_len = TEST_DATA_MAX,
74 .operations = {
75 RK_OP_CIPHER_ENC,
76 RK_OP_CIPHER_DEC,
77 DATA_BUTT,
78 },
79 },
80
81 {
82 .algo = RK_ALGO_SM4,
83 .modes = {
84 RK_CIPHER_MODE_ECB,
85 RK_CIPHER_MODE_CBC,
86 RK_CIPHER_MODE_CTS,
87 RK_CIPHER_MODE_CTR,
88 RK_CIPHER_MODE_CFB,
89 RK_CIPHER_MODE_OFB,
90 DATA_BUTT,
91 },
92 .key_lens = {16, DATA_BUTT},
93 .iv_len = SM4_BLOCK_SIZE,
94 .data_len = TEST_DATA_MAX,
95 .operations = {
96 RK_OP_CIPHER_ENC,
97 RK_OP_CIPHER_DEC,
98 DATA_BUTT,
99 },
100 },
101 };
102
test_set_otp_tag(void)103 void test_set_otp_tag(void)
104 {
105 uint32_t res;
106
107 res = rk_set_oem_hr_otp_read_lock(RK_OEM_OTP_KEY0);
108 printf("trusty_set_oem_hr_otp_read_lock 0. res:%d\n", res);
109
110 res = rk_set_oem_hr_otp_read_lock(RK_OEM_OTP_KEY1);
111 printf("trusty_set_oem_hr_otp_read_lock 1. res:%d\n", res);
112
113 res = rk_set_oem_hr_otp_read_lock(RK_OEM_OTP_KEY2);
114 printf("trusty_set_oem_hr_otp_read_lock 2. res:%d\n", res);
115
116 res = rk_set_oem_hr_otp_read_lock(RK_OEM_OTP_KEY3);
117 printf("trusty_set_oem_hr_otp_read_lock 3. res:%d\n", res);
118
119 return;
120 }
121
test_write_otp_key(void)122 void test_write_otp_key(void)
123 {
124 uint32_t res;
125
126 res = rk_write_oem_otp_key(RK_OEM_OTP_KEY0,
127 otp_key0, sizeof(otp_key0));
128 printf("write otp key 0. res:%d\n", res);
129
130 res = rk_write_oem_otp_key(RK_OEM_OTP_KEY1,
131 otp_key1, sizeof(otp_key1));
132 printf("write otp key 1. res:%d\n", res);
133
134 res = rk_write_oem_otp_key(RK_OEM_OTP_KEY2,
135 otp_key2, sizeof(otp_key2));
136 printf("write otp key 2. res:%d\n", res);
137
138 res = rk_write_oem_otp_key(RK_OEM_OTP_KEY3,
139 otp_key3, sizeof(otp_key3));
140 printf("write otp key 3. res:%d\n", res);
141
142 return;
143 }
144
test_otp_key_item_virt(uint32_t key_id,const struct test_otp_key_item * item)145 static int test_otp_key_item_virt(uint32_t key_id, const struct test_otp_key_item *item)
146 {
147 int res = 0;
148 RK_RES rk_res;
149 uint32_t i, j, k;
150 uint8_t *key = NULL;
151 rk_cipher_config cipher_cfg;
152 uint8_t *plain = NULL, *cipher_soft = NULL, *cipher_hard = NULL;
153 uint32_t algo = 0, mode = 0, key_len, data_len, operation;
154
155 plain = malloc(item->data_len);
156 if (!plain) {
157 printf("plain malloc %uByte error!\n", item->data_len);
158 res = -1;
159 goto exit;
160 }
161
162 cipher_soft = malloc(item->data_len);
163 if (!cipher_soft) {
164 printf("cipher_soft malloc %uByte error!\n", item->data_len);
165 res = -1;
166 goto exit;
167 }
168
169 cipher_hard = malloc(item->data_len);
170 if (!cipher_hard) {
171 printf("cipher_hard malloc %uByte error!\n", item->data_len);
172 res = -1;
173 goto exit;
174 }
175
176 switch (key_id) {
177 case RK_OEM_OTP_KEY0:
178 key = otp_key0;
179 break;
180 case RK_OEM_OTP_KEY1:
181 key = otp_key1;
182 break;
183 case RK_OEM_OTP_KEY2:
184 key = otp_key2;
185 break;
186 case RK_OEM_OTP_KEY3:
187 key = otp_key3;
188 break;
189 default:
190 return -1;
191 }
192
193 memset(&cipher_cfg, 0x00, sizeof(cipher_cfg));
194 test_get_rng(cipher_cfg.iv, item->iv_len);
195
196 test_get_rng(plain, item->data_len);
197
198 memset(cipher_soft, 0x00, item->data_len);
199 memset(cipher_hard, 0x00, item->data_len);
200
201 for (i = 0; i < ARRAY_SIZE(item->modes); i++) {
202 if (item->modes[i] == DATA_BUTT)
203 break;
204
205 algo = item->algo;
206 mode = item->modes[i];
207
208 for (j = 0; j < ARRAY_SIZE(item->key_lens); j++) {
209 if (item->key_lens[j] == DATA_BUTT)
210 break;
211
212 key_len = item->key_lens[j];
213 data_len = item->data_len;
214 if (is_no_multi_blocksize(mode))
215 data_len -= 3;
216
217 for (k = 0; k < ARRAY_SIZE(item->operations); k++) {
218 if (item->operations[k] == DATA_BUTT)
219 break;
220
221 operation = item->operations[k];
222
223 cipher_cfg.algo = algo;
224 cipher_cfg.mode = mode;
225 cipher_cfg.operation = operation;
226 cipher_cfg.key_len = key_len;
227 cipher_cfg.reserved = NULL;
228
229 rk_res = rk_oem_otp_key_cipher_virt(key_id, &cipher_cfg,
230 plain, cipher_hard, data_len);
231 if (rk_res == RK_CRYPTO_ERR_NOT_SUPPORTED) {
232 printf("virt:\totpkey%d\t[%s-%u]\t%s\t%s\tN/A\n",
233 key_id, test_algo_name(algo), key_len * 8,
234 test_mode_name(mode), test_op_name(operation));
235 res = 0;
236 continue;
237 } else if (res) {
238 printf("rk_oem_otp_key_cipher_virt fail! 0x%08x\n", res);
239 goto exit;
240 }
241
242 rk_res = soft_cipher(algo, mode, operation, key, key_len,
243 cipher_cfg.iv, plain, data_len, cipher_soft);
244 if (res) {
245 printf("soft_cipher fail! 0x%08x\n", res);
246 res = -1;
247 goto exit;
248 }
249
250 if (memcmp(cipher_soft, cipher_hard, data_len)) {
251 printf("rk_oem_otp_key_cipher_virt compare failed.\n");
252 res = -1;
253 goto exit;
254 }
255
256 printf("virt:\totpkey%d\t[%s-%u]\t%s\t%s\tPASS\n",
257 key_id, test_algo_name(algo), key_len * 8,
258 test_mode_name(mode), test_op_name(operation));
259 }
260 }
261 }
262
263 exit:
264 if (plain)
265 free(plain);
266
267 if (cipher_soft)
268 free(cipher_soft);
269
270 if (cipher_hard)
271 free(cipher_hard);
272
273 if (res)
274 printf("virt:\totpkey%d\t[%s-%u]\t%s\t%s\tFAIL\n",
275 key_id, test_algo_name(algo), key_len * 8,
276 test_mode_name(mode), test_op_name(operation));
277
278 return res;
279 }
280
test_otp_key_item_fd(uint32_t key_id,const struct test_otp_key_item * item)281 static int test_otp_key_item_fd(uint32_t key_id, const struct test_otp_key_item *item)
282 {
283 int res = 0;
284 RK_RES rk_res;
285 uint32_t i, j, k;
286 uint8_t *key = NULL;
287 rk_cipher_config cipher_cfg;
288 uint8_t *cipher_soft = NULL;
289 rk_crypto_mem *plain = NULL;
290 rk_crypto_mem *cipher_hard = NULL;
291 uint32_t algo = 0, mode = 0, key_len, data_len, operation;
292
293 cipher_soft = malloc(item->data_len);
294 if (!cipher_soft) {
295 printf("cipher_soft malloc %uByte error!\n", item->data_len);
296 res = -1;
297 goto exit;
298 }
299
300 plain = rk_crypto_mem_alloc(item->data_len);
301 if (!plain) {
302 printf("rk_crypto_mem_alloc %uByte error!\n", item->data_len);
303 res = -1;
304 goto exit;
305 }
306
307 cipher_hard = rk_crypto_mem_alloc(item->data_len);
308 if (!cipher_hard) {
309 printf("rk_crypto_mem_alloc %uByte error!\n", item->data_len);
310 res = -1;
311 goto exit;
312 }
313
314 switch (key_id) {
315 case RK_OEM_OTP_KEY0:
316 key = otp_key0;
317 break;
318 case RK_OEM_OTP_KEY1:
319 key = otp_key1;
320 break;
321 case RK_OEM_OTP_KEY2:
322 key = otp_key2;
323 break;
324 case RK_OEM_OTP_KEY3:
325 key = otp_key3;
326 break;
327 default:
328 return -1;
329 }
330
331 memset(&cipher_cfg, 0x00, sizeof(cipher_cfg));
332 test_get_rng(cipher_cfg.iv, item->iv_len);
333
334 test_get_rng(plain->vaddr, item->data_len);
335
336 memset(cipher_soft, 0x00, item->data_len);
337 memset(cipher_hard->vaddr, 0x00, item->data_len);
338
339 for (i = 0; i < ARRAY_SIZE(item->modes); i++) {
340 if (item->modes[i] == DATA_BUTT)
341 break;
342
343 algo = item->algo;
344 mode = item->modes[i];
345
346 for (j = 0; j < ARRAY_SIZE(item->key_lens); j++) {
347 if (item->key_lens[j] == DATA_BUTT)
348 break;
349
350 key_len = item->key_lens[j];
351 data_len = item->data_len;
352 if (is_no_multi_blocksize(mode))
353 data_len -= 3;
354
355 for (k = 0; k < ARRAY_SIZE(item->operations); k++) {
356 if (item->operations[k] == DATA_BUTT)
357 break;
358
359 operation = item->operations[k];
360
361 cipher_cfg.algo = algo;
362 cipher_cfg.mode = mode;
363 cipher_cfg.operation = operation;
364 cipher_cfg.key_len = key_len;
365 cipher_cfg.reserved = NULL;
366
367 rk_res = rk_oem_otp_key_cipher(key_id, &cipher_cfg, plain->dma_fd,
368 cipher_hard->dma_fd, data_len);
369 if (rk_res == RK_CRYPTO_ERR_NOT_SUPPORTED) {
370 printf("dma_fd:\totpkey%d\t[%s-%u]\t%s\t%s\tN/A\n",
371 key_id, test_algo_name(algo), key_len * 8,
372 test_mode_name(mode), test_op_name(operation));
373 res = 0;
374 continue;
375 } else if (res) {
376 printf("rk_oem_otp_key_cipher fail! 0x%08x\n", res);
377 goto exit;
378 }
379
380 rk_res = soft_cipher(algo, mode, operation, key, key_len,
381 cipher_cfg.iv, plain->vaddr, data_len,
382 cipher_soft);
383 if (res) {
384 printf("soft_cipher fail! 0x%08x\n", res);
385 res = -1;
386 goto exit;
387 }
388
389 if (memcmp(cipher_soft, cipher_hard->vaddr, data_len)) {
390 printf("rk_oem_otp_key_cipher compare failed.\n");
391 res = -1;
392 goto exit;
393 }
394
395 printf("dma_fd:\totpkey%d\t[%s-%u]\t%s\t%s\tPASS\n",
396 key_id, test_algo_name(algo), key_len * 8,
397 test_mode_name(mode), test_op_name(operation));
398 }
399 }
400 }
401
402 exit:
403 if (cipher_soft)
404 free(cipher_soft);
405
406 if (plain)
407 rk_crypto_mem_free(plain);
408
409 if (cipher_hard)
410 rk_crypto_mem_free(cipher_hard);
411
412 if (res)
413 printf("dma_fd:\totpkey%d\t[%s-%u]\t%s\t%s\tFAIL\n",
414 key_id, test_algo_name(algo), key_len * 8,
415 test_mode_name(mode), test_op_name(operation));
416
417 return res;
418 }
419
test_otp_key_fd(void)420 static int test_otp_key_fd(void)
421 {
422 int res;
423 uint32_t i, j;
424 uint32_t key_id;
425
426 res = rk_crypto_init();
427 if (res) {
428 printf("rk_crypto_init error!\n");
429 return res;
430 }
431
432
433 for (i = 0; i < ARRAY_SIZE(test_key_ids); i++) {
434 key_id = test_key_ids[i];
435
436 for (j = 0; j < ARRAY_SIZE(test_item_tbl); j++) {
437 res = test_otp_key_item_fd(key_id, &test_item_tbl[j]);
438 if (res)
439 goto exit;
440 }
441 }
442
443 exit:
444 rk_crypto_deinit();
445 return res;
446 }
447
test_otp_key_virt(void)448 static int test_otp_key_virt(void)
449 {
450 int res;
451 uint32_t i, j;
452 uint32_t key_id;
453
454 for (i = 0; i < ARRAY_SIZE(test_key_ids); i++) {
455 key_id = test_key_ids[i];
456
457 for (j = 0; j < ARRAY_SIZE(test_item_tbl); j++) {
458 res = test_otp_key_item_virt(key_id, &test_item_tbl[j]);
459 if (res)
460 goto exit;
461 }
462 }
463
464 exit:
465 return res;
466 }
467
test_otp_key(void)468 RK_RES test_otp_key(void)
469 {
470 if (test_otp_key_virt())
471 return RK_CRYPTO_ERR_GENERIC;
472
473 if (test_otp_key_fd())
474 return RK_CRYPTO_ERR_GENERIC;
475
476 return RK_CRYPTO_SUCCESS;
477 }
478