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