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_otp_key.h"
9 #include "rkcrypto_demo.h"
10
11 static uint8_t otp_key_0[32] = {
12 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01,
13 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01,
14 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
15 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
16 };
17 static uint8_t iv[16] = {
18 0x10, 0x44, 0x80, 0xb3, 0x88, 0x5f, 0x02, 0x03,
19 0x05, 0x21, 0x07, 0xc9, 0x44, 0x00, 0x1b, 0x80,
20 };
21 static uint8_t input[16] = {
22 0xc9, 0x07, 0x21, 0x05, 0x80, 0x1b, 0x00, 0x44,
23 0xac, 0x13, 0xfb, 0x23, 0x93, 0x4a, 0x66, 0xe4,
24 };
25 static uint8_t expected_enc[16] = {
26 0xeb, 0xe7, 0xde, 0x12, 0x8d, 0x77, 0xf4, 0xe8,
27 0x83, 0x4a, 0x63, 0x1d, 0x0e, 0xcc, 0xdb, 0x1c,
28 };
29 static uint32_t algo = RK_ALGO_AES;
30 static uint32_t mode = RK_CIPHER_MODE_CBC;
31
demo_otpkey(void)32 RK_RES demo_otpkey(void)
33 {
34 RK_RES res = RK_CRYPTO_ERR_GENERIC;
35 rk_cipher_config config;
36 uint32_t key_id = RK_OEM_OTP_KEY0;
37 uint32_t key_len = 16;
38 uint32_t len = sizeof(input);
39 rk_crypto_mem *in = NULL;
40 rk_crypto_mem *out = NULL;
41
42 /* Write keys. If written before, it will returns failure. */
43 if (rk_write_oem_otp_key(RK_OEM_OTP_KEY0, otp_key_0, sizeof(otp_key_0)))
44 printf("Check if otp key 0 is already written!\n");
45 else
46 printf("Write otp key 0, success!\n");
47
48 /* Do cipher. */
49 res = rk_crypto_init();
50 if (res) {
51 printf("rk_crypto_init error! res: 0x%08x\n", res);
52 return res;
53 }
54
55 in = rk_crypto_mem_alloc(len);
56 if (!in) {
57 printf("malloc %uByte error!\n", len);
58 res = RK_CRYPTO_ERR_GENERIC;
59 goto exit;
60 }
61
62 out = rk_crypto_mem_alloc(len);
63 if (!out) {
64 printf("malloc %uByte error!\n", len);
65 res = RK_CRYPTO_ERR_GENERIC;
66 goto exit;
67 }
68
69 config.algo = algo;
70 config.mode = mode;
71 config.operation = RK_OP_CIPHER_ENC;
72 config.key_len = key_len;
73 config.reserved = NULL;
74 memcpy(config.iv, iv, sizeof(iv));
75 memcpy(in->vaddr, input, len);
76
77 res = rk_oem_otp_key_cipher(key_id, &config, in->dma_fd, out->dma_fd, len);
78 if (res) {
79 printf("test rk_oem_otp_key_cipher fail! 0x%08x\n", res);
80 goto exit;
81 }
82
83 if (memcmp(out->vaddr, expected_enc, 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 OTPKEY_CIPHER ENC success!\n");
90
91 config.operation = RK_OP_CIPHER_DEC;
92
93 res = rk_oem_otp_key_cipher(key_id, &config, out->dma_fd, out->dma_fd, len);
94 if (res) {
95 printf("test rk_oem_otp_key_cipher fail! 0x%08x\n", res);
96 goto exit;
97 }
98
99 if (memcmp(out->vaddr, input, len)) {
100 printf("ENC result not equal to expected value, error!\n");
101 res = RK_CRYPTO_ERR_GENERIC;
102 goto exit;
103 }
104
105 printf("Test OTPKEY_CIPHER DEC success!\n");
106
107 exit:
108 rk_crypto_mem_free(in);
109 rk_crypto_mem_free(out);
110
111 rk_crypto_deinit();
112
113 return res;
114 }
115
demo_otpkey_virt(void)116 RK_RES demo_otpkey_virt(void)
117 {
118 RK_RES res = RK_CRYPTO_ERR_GENERIC;
119 rk_cipher_config config;
120 uint32_t key_id = RK_OEM_OTP_KEY0;
121 uint32_t key_len = 16;
122 uint8_t output[16];
123 uint32_t data_len = sizeof(input);
124
125 /* Write keys. If written before, it will returns failure. */
126 if (rk_write_oem_otp_key(RK_OEM_OTP_KEY0, otp_key_0, sizeof(otp_key_0)))
127 printf("Check if otp key 0 is already written!\n");
128 else
129 printf("Write otp key 0, success!\n");
130
131 /* Do cipher. */
132 memset(output, 0, sizeof(output));
133
134 config.algo = algo;
135 config.mode = mode;
136 config.operation = RK_OP_CIPHER_ENC;
137 config.key_len = key_len;
138 config.reserved = NULL;
139 memcpy(config.iv, iv, sizeof(iv));
140
141 res = rk_oem_otp_key_cipher_virt(key_id, &config, input, output, data_len);
142 if (res) {
143 printf("Do rk_oem_otp_key_cipher_virt error!\n");
144 goto exit;
145 }
146
147 if (memcmp(output, expected_enc, data_len)) {
148 printf("ENC result not equal to expected value, error!\n");
149 res = RK_CRYPTO_ERR_GENERIC;
150 goto exit;
151 }
152
153 printf("Test OTPKEY_CIPHER_VIRT ENC success!\n");
154
155 config.operation = RK_OP_CIPHER_DEC;
156
157 res = rk_oem_otp_key_cipher_virt(key_id, &config, output, output, data_len);
158 if (res) {
159 printf("Do rk_oem_otp_key_cipher_virt error!\n");
160 goto exit;
161 }
162
163 if (memcmp(output, input, data_len)) {
164 printf("DEC result not equal to expected value, error!\n");
165 res = RK_CRYPTO_ERR_GENERIC;
166 goto exit;
167 }
168
169 printf("Test OTPKEY_CIPHER_VIRT DEC success!\n");
170
171 exit:
172 return res;
173 }
174