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 <stdlib.h>
7*4882a593Smuzhiyun #include "rkcrypto_core.h"
8*4882a593Smuzhiyun #include "rkcrypto_mem.h"
9*4882a593Smuzhiyun #include "rkcrypto_demo.h"
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun #define TEST_END_PASS(padding_name) \
12*4882a593Smuzhiyun printf("****************** %-20s %-16s test PASS !!! ******************\n", \
13*4882a593Smuzhiyun __func__, padding_name)
14*4882a593Smuzhiyun
15*4882a593Smuzhiyun #define TEST_END_FAIL(padding_name) \
16*4882a593Smuzhiyun printf("****************** %-20s %-16s test FAIL !!! ******************\n", \
17*4882a593Smuzhiyun __func__, padding_name)
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun #define TEST_END_NA(padding_name) \
20*4882a593Smuzhiyun printf("****************** %-20s %-16s test N/A !!! ******************\n", \
21*4882a593Smuzhiyun __func__, padding_name)
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun static const uint8_t rsa_2048_n[] = {
24*4882a593Smuzhiyun 0xe9, 0xd7, 0x82, 0x24, 0xf8, 0x69, 0x5e, 0x85,
25*4882a593Smuzhiyun 0xea, 0x79, 0x84, 0x5d, 0x3d, 0x85, 0xf3, 0x7e,
26*4882a593Smuzhiyun 0x28, 0x6e, 0xab, 0x2d, 0x76, 0x4f, 0xd4, 0x72,
27*4882a593Smuzhiyun 0x63, 0xa1, 0x6a, 0x17, 0x60, 0xbd, 0xfc, 0x29,
28*4882a593Smuzhiyun 0xb3, 0x6d, 0x1b, 0xaf, 0x5c, 0xb2, 0x58, 0x62,
29*4882a593Smuzhiyun 0x08, 0xb1, 0xed, 0x04, 0xf3, 0x2e, 0x7d, 0x9d,
30*4882a593Smuzhiyun 0xa6, 0x3b, 0xbd, 0x26, 0x64, 0x62, 0x49, 0x3e,
31*4882a593Smuzhiyun 0xf9, 0x92, 0x79, 0x25, 0x97, 0xd6, 0x8c, 0xff,
32*4882a593Smuzhiyun 0x5c, 0x0f, 0xbb, 0x75, 0x22, 0xea, 0xe5, 0x6b,
33*4882a593Smuzhiyun 0xcb, 0x49, 0x46, 0xcf, 0xe3, 0x8d, 0x6b, 0x69,
34*4882a593Smuzhiyun 0xd1, 0x32, 0xd0, 0x9d, 0xf4, 0xc9, 0x45, 0x77,
35*4882a593Smuzhiyun 0xed, 0x61, 0x5a, 0xea, 0x32, 0x2f, 0x8c, 0xf7,
36*4882a593Smuzhiyun 0x7e, 0xaf, 0xc6, 0xf7, 0x1f, 0xe0, 0x92, 0xdf,
37*4882a593Smuzhiyun 0x6f, 0x8c, 0xba, 0x82, 0xfc, 0x17, 0x61, 0xec,
38*4882a593Smuzhiyun 0x1d, 0x36, 0x26, 0x93, 0xa2, 0x8a, 0xe4, 0x45,
39*4882a593Smuzhiyun 0xd2, 0x84, 0x1f, 0x3e, 0xfe, 0xb4, 0x05, 0xf8,
40*4882a593Smuzhiyun 0xac, 0x7a, 0x6b, 0x6a, 0x7b, 0x61, 0x9d, 0x30,
41*4882a593Smuzhiyun 0x72, 0xd5, 0xfe, 0x3b, 0xee, 0x30, 0x65, 0x74,
42*4882a593Smuzhiyun 0x98, 0xcc, 0x84, 0xb2, 0xaa, 0x42, 0xe0, 0xba,
43*4882a593Smuzhiyun 0xb7, 0x78, 0x7e, 0x62, 0x9a, 0x84, 0x00, 0x34,
44*4882a593Smuzhiyun 0x5b, 0x28, 0xf5, 0xe6, 0x6a, 0x23, 0xfe, 0x83,
45*4882a593Smuzhiyun 0x78, 0xef, 0x84, 0x71, 0x2b, 0x99, 0xa2, 0xd4,
46*4882a593Smuzhiyun 0xb8, 0x4a, 0x65, 0xd1, 0xa7, 0xf2, 0xfd, 0xdb,
47*4882a593Smuzhiyun 0x70, 0xbb, 0xe7, 0x5d, 0x70, 0x3c, 0xd4, 0xf6,
48*4882a593Smuzhiyun 0x68, 0x4b, 0xf1, 0x8f, 0xd4, 0x18, 0xaa, 0x76,
49*4882a593Smuzhiyun 0x1e, 0x40, 0x4d, 0x6f, 0x11, 0x09, 0x6f, 0x2e,
50*4882a593Smuzhiyun 0x3e, 0xa1, 0x0b, 0xc8, 0xb6, 0x16, 0x1a, 0xa3,
51*4882a593Smuzhiyun 0x4b, 0x81, 0x77, 0x68, 0xee, 0x70, 0x26, 0xf9,
52*4882a593Smuzhiyun 0xfd, 0x0f, 0x1c, 0x39, 0xa8, 0x07, 0xf3, 0xe3,
53*4882a593Smuzhiyun 0xd7, 0x6b, 0x20, 0x2e, 0x24, 0xa5, 0xba, 0x67,
54*4882a593Smuzhiyun 0xc0, 0x3f, 0x9a, 0x73, 0x74, 0x2c, 0x92, 0x39,
55*4882a593Smuzhiyun 0xfe, 0x75, 0xce, 0x3d, 0x39, 0x4c, 0x7f, 0x11,
56*4882a593Smuzhiyun };
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun static const uint8_t rsa_2048_e[] = {
59*4882a593Smuzhiyun 0x01, 0x00, 0x01,
60*4882a593Smuzhiyun };
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun static const uint8_t rsa_2048_d[] = {
63*4882a593Smuzhiyun 0x8e, 0xef, 0x1e, 0xea, 0xf0, 0x38, 0xa0, 0x01,
64*4882a593Smuzhiyun 0x65, 0x38, 0x4f, 0xd3, 0x1a, 0x77, 0x15, 0x45,
65*4882a593Smuzhiyun 0xc8, 0xfb, 0x14, 0x80, 0xde, 0x6f, 0x9f, 0x61,
66*4882a593Smuzhiyun 0x28, 0x4b, 0x12, 0xe9, 0x25, 0x46, 0x1b, 0xab,
67*4882a593Smuzhiyun 0x75, 0xb4, 0x3c, 0x96, 0xed, 0x27, 0x4a, 0x64,
68*4882a593Smuzhiyun 0xea, 0x01, 0xaf, 0xea, 0x35, 0xa6, 0xf6, 0x3b,
69*4882a593Smuzhiyun 0x75, 0xcf, 0xd0, 0x27, 0x76, 0x76, 0xa9, 0x64,
70*4882a593Smuzhiyun 0xe7, 0xf2, 0x43, 0x40, 0xda, 0x55, 0xd7, 0xab,
71*4882a593Smuzhiyun 0x83, 0x1f, 0x4e, 0xab, 0x23, 0xe3, 0xc6, 0xc6,
72*4882a593Smuzhiyun 0x23, 0xe5, 0x2f, 0x76, 0x85, 0x15, 0xfd, 0x51,
73*4882a593Smuzhiyun 0x1a, 0x01, 0x60, 0x89, 0x1c, 0x86, 0x26, 0x83,
74*4882a593Smuzhiyun 0x4e, 0xc3, 0x12, 0x6a, 0x52, 0xb0, 0xc4, 0x0e,
75*4882a593Smuzhiyun 0xc8, 0x0e, 0x33, 0xa4, 0xfc, 0x84, 0x2d, 0xe2,
76*4882a593Smuzhiyun 0x8b, 0x71, 0xce, 0xd3, 0x9f, 0xb9, 0xbf, 0x2e,
77*4882a593Smuzhiyun 0x2f, 0x10, 0xd9, 0x75, 0xa6, 0x86, 0x15, 0x50,
78*4882a593Smuzhiyun 0x24, 0x84, 0x84, 0x00, 0xe2, 0x79, 0x31, 0xa8,
79*4882a593Smuzhiyun 0x0a, 0x90, 0xc1, 0x1e, 0x1c, 0x54, 0x4a, 0x51,
80*4882a593Smuzhiyun 0x74, 0x54, 0x33, 0x91, 0x99, 0x4e, 0x3b, 0xcc,
81*4882a593Smuzhiyun 0x6f, 0xdd, 0xd6, 0x73, 0x0c, 0x2a, 0x6c, 0xa5,
82*4882a593Smuzhiyun 0xcd, 0x89, 0xa7, 0x69, 0xde, 0x66, 0xb9, 0x60,
83*4882a593Smuzhiyun 0xe7, 0x49, 0x17, 0x34, 0x1d, 0xbc, 0x32, 0x92,
84*4882a593Smuzhiyun 0xfb, 0x6f, 0x89, 0x45, 0xee, 0x12, 0x7c, 0x54,
85*4882a593Smuzhiyun 0x0b, 0xdf, 0x0e, 0xb4, 0xdb, 0x55, 0xbf, 0x3f,
86*4882a593Smuzhiyun 0x11, 0x11, 0xf4, 0xd1, 0x3e, 0xcf, 0xfa, 0xf5,
87*4882a593Smuzhiyun 0xe0, 0x2f, 0x72, 0xe4, 0x73, 0x62, 0xe8, 0x19,
88*4882a593Smuzhiyun 0xbe, 0xeb, 0x8d, 0xe0, 0x77, 0x07, 0x54, 0x91,
89*4882a593Smuzhiyun 0x64, 0x63, 0x1a, 0x46, 0x47, 0x28, 0x10, 0xda,
90*4882a593Smuzhiyun 0x4c, 0xd3, 0xe7, 0x4f, 0x0c, 0xd5, 0x4b, 0xeb,
91*4882a593Smuzhiyun 0x7f, 0x27, 0x0c, 0xf6, 0x25, 0x09, 0x6d, 0xbc,
92*4882a593Smuzhiyun 0x34, 0xfb, 0x91, 0xab, 0x59, 0x49, 0xb4, 0x1c,
93*4882a593Smuzhiyun 0x1a, 0xbb, 0x85, 0xae, 0x13, 0x61, 0x5c, 0xa2,
94*4882a593Smuzhiyun 0xc5, 0x53, 0xaf, 0x29, 0x15, 0xf3, 0x35, 0x51,
95*4882a593Smuzhiyun };
96*4882a593Smuzhiyun
test_init_pubkey(rk_rsa_pub_key_pack * pub)97*4882a593Smuzhiyun static void test_init_pubkey(rk_rsa_pub_key_pack *pub)
98*4882a593Smuzhiyun {
99*4882a593Smuzhiyun memset(pub, 0x00, sizeof(*pub));
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun pub->key_type = RK_RSA_KEY_TYPE_PLAIN;
102*4882a593Smuzhiyun
103*4882a593Smuzhiyun pub->key.n = rsa_2048_n;
104*4882a593Smuzhiyun pub->key.n_len = sizeof(rsa_2048_n);
105*4882a593Smuzhiyun pub->key.e = rsa_2048_e;
106*4882a593Smuzhiyun pub->key.e_len = sizeof(rsa_2048_e);
107*4882a593Smuzhiyun }
108*4882a593Smuzhiyun
test_init_privkey(rk_rsa_priv_key_pack * priv)109*4882a593Smuzhiyun static void test_init_privkey(rk_rsa_priv_key_pack *priv)
110*4882a593Smuzhiyun {
111*4882a593Smuzhiyun memset(priv, 0x00, sizeof(*priv));
112*4882a593Smuzhiyun
113*4882a593Smuzhiyun priv->key_type = RK_RSA_KEY_TYPE_PLAIN;
114*4882a593Smuzhiyun
115*4882a593Smuzhiyun priv->key.n = rsa_2048_n;
116*4882a593Smuzhiyun priv->key.n_len = sizeof(rsa_2048_n);
117*4882a593Smuzhiyun priv->key.e = rsa_2048_e;
118*4882a593Smuzhiyun priv->key.e_len = sizeof(rsa_2048_e);
119*4882a593Smuzhiyun priv->key.d = rsa_2048_d;
120*4882a593Smuzhiyun priv->key.d_len = sizeof(rsa_2048_d);
121*4882a593Smuzhiyun }
122*4882a593Smuzhiyun
demo_rsa_pub_enc(void)123*4882a593Smuzhiyun static RK_RES demo_rsa_pub_enc(void)
124*4882a593Smuzhiyun {
125*4882a593Smuzhiyun RK_RES res = RK_CRYPTO_SUCCESS;
126*4882a593Smuzhiyun uint8_t plain[SHA256_HASH_SIZE];
127*4882a593Smuzhiyun uint8_t *enc_buf = NULL;
128*4882a593Smuzhiyun uint8_t *dec_buf = NULL;
129*4882a593Smuzhiyun uint32_t nbytes = RSA_BITS_2048 / 8;
130*4882a593Smuzhiyun uint32_t out_len, padding = RK_RSA_CRYPT_PADDING_PKCS1_V1_5;
131*4882a593Smuzhiyun const char *padding_name = "PKCS1_V1_5";
132*4882a593Smuzhiyun rk_rsa_pub_key_pack pub_key;
133*4882a593Smuzhiyun rk_rsa_priv_key_pack priv_key;
134*4882a593Smuzhiyun
135*4882a593Smuzhiyun res = rk_crypto_init();
136*4882a593Smuzhiyun if (res) {
137*4882a593Smuzhiyun printf("rk_crypto_init error %08x\n", res);
138*4882a593Smuzhiyun return res;
139*4882a593Smuzhiyun }
140*4882a593Smuzhiyun
141*4882a593Smuzhiyun enc_buf = (uint8_t *)malloc(nbytes);
142*4882a593Smuzhiyun if (!enc_buf) {
143*4882a593Smuzhiyun printf("malloc for enc_buf failed\n");
144*4882a593Smuzhiyun res = RK_CRYPTO_ERR_OUT_OF_MEMORY;
145*4882a593Smuzhiyun goto exit;
146*4882a593Smuzhiyun }
147*4882a593Smuzhiyun
148*4882a593Smuzhiyun dec_buf = (uint8_t *)malloc(nbytes);
149*4882a593Smuzhiyun if (!dec_buf) {
150*4882a593Smuzhiyun printf("malloc for dec_buf failed\n");
151*4882a593Smuzhiyun res = RK_CRYPTO_ERR_OUT_OF_MEMORY;
152*4882a593Smuzhiyun goto exit;
153*4882a593Smuzhiyun }
154*4882a593Smuzhiyun
155*4882a593Smuzhiyun memset(plain, 0xab, sizeof(plain));
156*4882a593Smuzhiyun
157*4882a593Smuzhiyun test_init_pubkey(&pub_key);
158*4882a593Smuzhiyun test_init_privkey(&priv_key);
159*4882a593Smuzhiyun
160*4882a593Smuzhiyun res = rk_rsa_pub_encrypt(&pub_key, padding, plain, sizeof(plain), enc_buf, &out_len);
161*4882a593Smuzhiyun if (res) {
162*4882a593Smuzhiyun if (res != RK_CRYPTO_ERR_NOT_SUPPORTED)
163*4882a593Smuzhiyun printf("rk_rsa_pub_encrypt failed %x\n", res);
164*4882a593Smuzhiyun goto exit;
165*4882a593Smuzhiyun }
166*4882a593Smuzhiyun
167*4882a593Smuzhiyun res = rk_rsa_priv_decrypt(&priv_key, padding, enc_buf, out_len, dec_buf, &out_len);
168*4882a593Smuzhiyun if (res) {
169*4882a593Smuzhiyun printf("rk_rsa_priv_decrypt failed %x\n", res);
170*4882a593Smuzhiyun goto exit;
171*4882a593Smuzhiyun }
172*4882a593Smuzhiyun
173*4882a593Smuzhiyun if (sizeof(plain) != out_len || memcmp(dec_buf, plain, out_len)) {
174*4882a593Smuzhiyun printf("rk_rsa_priv_decrypt compare failed\n");
175*4882a593Smuzhiyun res = RK_CRYPTO_ERR_GENERIC;
176*4882a593Smuzhiyun goto exit;
177*4882a593Smuzhiyun }
178*4882a593Smuzhiyun
179*4882a593Smuzhiyun exit:
180*4882a593Smuzhiyun if (enc_buf)
181*4882a593Smuzhiyun free(enc_buf);
182*4882a593Smuzhiyun
183*4882a593Smuzhiyun if (dec_buf)
184*4882a593Smuzhiyun free(dec_buf);
185*4882a593Smuzhiyun
186*4882a593Smuzhiyun if (res == RK_CRYPTO_ERR_NOT_SUPPORTED)
187*4882a593Smuzhiyun TEST_END_NA(padding_name);
188*4882a593Smuzhiyun else if (res)
189*4882a593Smuzhiyun TEST_END_FAIL(padding_name);
190*4882a593Smuzhiyun else
191*4882a593Smuzhiyun TEST_END_PASS(padding_name);
192*4882a593Smuzhiyun
193*4882a593Smuzhiyun rk_crypto_deinit();
194*4882a593Smuzhiyun
195*4882a593Smuzhiyun return res == RK_CRYPTO_ERR_NOT_SUPPORTED ? RK_CRYPTO_SUCCESS : res;
196*4882a593Smuzhiyun }
197*4882a593Smuzhiyun
198*4882a593Smuzhiyun
demo_rsa_priv_enc(void)199*4882a593Smuzhiyun RK_RES demo_rsa_priv_enc(void)
200*4882a593Smuzhiyun {
201*4882a593Smuzhiyun RK_RES res = RK_CRYPTO_SUCCESS;
202*4882a593Smuzhiyun uint8_t *enc_buf = NULL;
203*4882a593Smuzhiyun uint8_t *dec_buf = NULL;
204*4882a593Smuzhiyun uint8_t plain[SHA256_HASH_SIZE];
205*4882a593Smuzhiyun uint32_t out_len, padding = RK_RSA_CRYPT_PADDING_PKCS1_V1_5;
206*4882a593Smuzhiyun const char *padding_name = "PKCS1_V1_5";
207*4882a593Smuzhiyun uint32_t nbytes = RSA_BITS_2048 / 8;
208*4882a593Smuzhiyun rk_rsa_pub_key_pack pub_key;
209*4882a593Smuzhiyun rk_rsa_priv_key_pack priv_key;
210*4882a593Smuzhiyun
211*4882a593Smuzhiyun res = rk_crypto_init();
212*4882a593Smuzhiyun if (res) {
213*4882a593Smuzhiyun printf("rk_crypto_init error %08x\n", res);
214*4882a593Smuzhiyun return res;
215*4882a593Smuzhiyun }
216*4882a593Smuzhiyun
217*4882a593Smuzhiyun enc_buf = (uint8_t *)malloc(nbytes);
218*4882a593Smuzhiyun if (!enc_buf) {
219*4882a593Smuzhiyun printf("malloc for enc_buf failed\n");
220*4882a593Smuzhiyun res = RK_CRYPTO_ERR_OUT_OF_MEMORY;
221*4882a593Smuzhiyun goto exit;
222*4882a593Smuzhiyun }
223*4882a593Smuzhiyun
224*4882a593Smuzhiyun dec_buf = (uint8_t *)malloc(nbytes);
225*4882a593Smuzhiyun if (!dec_buf) {
226*4882a593Smuzhiyun printf("malloc for dec_buf failed\n");
227*4882a593Smuzhiyun res = RK_CRYPTO_ERR_OUT_OF_MEMORY;
228*4882a593Smuzhiyun goto exit;
229*4882a593Smuzhiyun }
230*4882a593Smuzhiyun
231*4882a593Smuzhiyun memset(plain, 0xab, sizeof(plain));
232*4882a593Smuzhiyun
233*4882a593Smuzhiyun test_init_pubkey(&pub_key);
234*4882a593Smuzhiyun test_init_privkey(&priv_key);
235*4882a593Smuzhiyun
236*4882a593Smuzhiyun res = rk_rsa_priv_encrypt(&priv_key, padding, plain, sizeof(plain), enc_buf, &out_len);
237*4882a593Smuzhiyun if (res) {
238*4882a593Smuzhiyun if (res != RK_CRYPTO_ERR_NOT_SUPPORTED)
239*4882a593Smuzhiyun printf("rk_rsa_priv_encrypt failed %x\n", res);
240*4882a593Smuzhiyun goto exit;
241*4882a593Smuzhiyun }
242*4882a593Smuzhiyun
243*4882a593Smuzhiyun res = rk_rsa_pub_decrypt(&pub_key, padding, enc_buf, out_len, dec_buf, &out_len);
244*4882a593Smuzhiyun if (res) {
245*4882a593Smuzhiyun printf("rk_rsa_pub_decrypt failed %x\n", res);
246*4882a593Smuzhiyun goto exit;
247*4882a593Smuzhiyun }
248*4882a593Smuzhiyun
249*4882a593Smuzhiyun if (sizeof(plain) != out_len || memcmp(dec_buf, plain, out_len)) {
250*4882a593Smuzhiyun printf("rk_rsa_pub_decrypt compare failed\n");
251*4882a593Smuzhiyun res = RK_CRYPTO_ERR_GENERIC;
252*4882a593Smuzhiyun goto exit;
253*4882a593Smuzhiyun }
254*4882a593Smuzhiyun
255*4882a593Smuzhiyun exit:
256*4882a593Smuzhiyun if (enc_buf)
257*4882a593Smuzhiyun free(enc_buf);
258*4882a593Smuzhiyun
259*4882a593Smuzhiyun if (dec_buf)
260*4882a593Smuzhiyun free(dec_buf);
261*4882a593Smuzhiyun
262*4882a593Smuzhiyun if (res == RK_CRYPTO_ERR_NOT_SUPPORTED)
263*4882a593Smuzhiyun TEST_END_NA(padding_name);
264*4882a593Smuzhiyun else if (res)
265*4882a593Smuzhiyun TEST_END_FAIL(padding_name);
266*4882a593Smuzhiyun else
267*4882a593Smuzhiyun TEST_END_PASS(padding_name);
268*4882a593Smuzhiyun
269*4882a593Smuzhiyun rk_crypto_deinit();
270*4882a593Smuzhiyun
271*4882a593Smuzhiyun return res == RK_CRYPTO_ERR_NOT_SUPPORTED ? RK_CRYPTO_SUCCESS : res;
272*4882a593Smuzhiyun }
273*4882a593Smuzhiyun
demo_rsa_sign_data(void)274*4882a593Smuzhiyun static RK_RES demo_rsa_sign_data(void)
275*4882a593Smuzhiyun {
276*4882a593Smuzhiyun RK_RES res = RK_CRYPTO_SUCCESS;
277*4882a593Smuzhiyun uint8_t *sign = NULL;
278*4882a593Smuzhiyun uint8_t data[512];
279*4882a593Smuzhiyun uint32_t sign_len;
280*4882a593Smuzhiyun uint32_t nbytes = RSA_BITS_2048 / 8;
281*4882a593Smuzhiyun uint32_t padding = RK_RSA_SIGN_PADDING_PKCS1_V15_SHA256;
282*4882a593Smuzhiyun const char *padding_name = "PKCS1_V1_5_SHA256";
283*4882a593Smuzhiyun rk_rsa_pub_key_pack pub_key;
284*4882a593Smuzhiyun rk_rsa_priv_key_pack priv_key;
285*4882a593Smuzhiyun
286*4882a593Smuzhiyun res = rk_crypto_init();
287*4882a593Smuzhiyun if (res) {
288*4882a593Smuzhiyun printf("rk_crypto_init error %08x\n", res);
289*4882a593Smuzhiyun return res;
290*4882a593Smuzhiyun }
291*4882a593Smuzhiyun
292*4882a593Smuzhiyun sign = (uint8_t *)malloc(nbytes);
293*4882a593Smuzhiyun if (!sign) {
294*4882a593Smuzhiyun printf("malloc for sign failed\n");
295*4882a593Smuzhiyun res = RK_CRYPTO_ERR_OUT_OF_MEMORY;
296*4882a593Smuzhiyun goto exit;
297*4882a593Smuzhiyun }
298*4882a593Smuzhiyun
299*4882a593Smuzhiyun test_init_pubkey(&pub_key);
300*4882a593Smuzhiyun test_init_privkey(&priv_key);
301*4882a593Smuzhiyun
302*4882a593Smuzhiyun memset(data, 0xab, sizeof(data));
303*4882a593Smuzhiyun
304*4882a593Smuzhiyun res = rk_rsa_sign(&priv_key, padding, data, sizeof(data), NULL, sign, &sign_len);
305*4882a593Smuzhiyun if (res) {
306*4882a593Smuzhiyun printf("rk_rsa_sign data failed %x\n", res);
307*4882a593Smuzhiyun goto exit;
308*4882a593Smuzhiyun }
309*4882a593Smuzhiyun
310*4882a593Smuzhiyun res = rk_rsa_verify(&pub_key, padding, data, sizeof(data), NULL, sign, sign_len);
311*4882a593Smuzhiyun if (res) {
312*4882a593Smuzhiyun printf("rk_rsa_verify data failed %x\n", res);
313*4882a593Smuzhiyun goto exit;
314*4882a593Smuzhiyun }
315*4882a593Smuzhiyun
316*4882a593Smuzhiyun printf("****************** %-20s %-16s test PASS !!! ******************\n",
317*4882a593Smuzhiyun __func__, padding_name);
318*4882a593Smuzhiyun
319*4882a593Smuzhiyun exit:
320*4882a593Smuzhiyun if (sign)
321*4882a593Smuzhiyun free(sign);
322*4882a593Smuzhiyun
323*4882a593Smuzhiyun if (res)
324*4882a593Smuzhiyun printf("****************** %-20s %-16s test FAIL !!! ******************\n",
325*4882a593Smuzhiyun __func__, padding_name);
326*4882a593Smuzhiyun
327*4882a593Smuzhiyun rk_crypto_deinit();
328*4882a593Smuzhiyun
329*4882a593Smuzhiyun return res;
330*4882a593Smuzhiyun }
331*4882a593Smuzhiyun
demo_rsa_sign_digest(void)332*4882a593Smuzhiyun static RK_RES demo_rsa_sign_digest(void)
333*4882a593Smuzhiyun {
334*4882a593Smuzhiyun RK_RES res = RK_CRYPTO_SUCCESS;
335*4882a593Smuzhiyun uint8_t *sign = NULL;
336*4882a593Smuzhiyun uint8_t hash[SHA256_HASH_SIZE];
337*4882a593Smuzhiyun uint32_t sign_len;
338*4882a593Smuzhiyun uint32_t nbytes = RSA_BITS_2048 / 8;
339*4882a593Smuzhiyun uint32_t padding = RK_RSA_SIGN_PADDING_PKCS1_V15_SHA256;
340*4882a593Smuzhiyun const char *padding_name = "PKCS1_V1_5_SHA256";
341*4882a593Smuzhiyun rk_rsa_pub_key_pack pub_key;
342*4882a593Smuzhiyun rk_rsa_priv_key_pack priv_key;
343*4882a593Smuzhiyun
344*4882a593Smuzhiyun res = rk_crypto_init();
345*4882a593Smuzhiyun if (res) {
346*4882a593Smuzhiyun printf("rk_crypto_init error %08x\n", res);
347*4882a593Smuzhiyun return res;
348*4882a593Smuzhiyun }
349*4882a593Smuzhiyun
350*4882a593Smuzhiyun sign = (uint8_t *)malloc(nbytes);
351*4882a593Smuzhiyun if (!sign) {
352*4882a593Smuzhiyun printf("malloc for sign failed\n");
353*4882a593Smuzhiyun res = RK_CRYPTO_ERR_OUT_OF_MEMORY;
354*4882a593Smuzhiyun goto exit;
355*4882a593Smuzhiyun }
356*4882a593Smuzhiyun
357*4882a593Smuzhiyun test_init_pubkey(&pub_key);
358*4882a593Smuzhiyun test_init_privkey(&priv_key);
359*4882a593Smuzhiyun
360*4882a593Smuzhiyun memset(hash, 0xab, sizeof(hash));
361*4882a593Smuzhiyun
362*4882a593Smuzhiyun res = rk_rsa_sign(&priv_key, padding, NULL, 0, hash, sign, &sign_len);
363*4882a593Smuzhiyun if (res) {
364*4882a593Smuzhiyun printf("rk_rsa_sign digest failed %x\n", res);
365*4882a593Smuzhiyun goto exit;
366*4882a593Smuzhiyun }
367*4882a593Smuzhiyun
368*4882a593Smuzhiyun res = rk_rsa_verify(&pub_key, padding, NULL, 0, hash, sign, sign_len);
369*4882a593Smuzhiyun if (res) {
370*4882a593Smuzhiyun printf("rk_rsa_verify digest failed %x\n", res);
371*4882a593Smuzhiyun goto exit;
372*4882a593Smuzhiyun }
373*4882a593Smuzhiyun
374*4882a593Smuzhiyun printf("****************** %-20s %-16s test PASS !!! ******************\n",
375*4882a593Smuzhiyun __func__, padding_name);
376*4882a593Smuzhiyun
377*4882a593Smuzhiyun exit:
378*4882a593Smuzhiyun if (sign)
379*4882a593Smuzhiyun free(sign);
380*4882a593Smuzhiyun
381*4882a593Smuzhiyun if (res)
382*4882a593Smuzhiyun printf("****************** %-20s %-16s test FAIL !!! ******************\n",
383*4882a593Smuzhiyun __func__, padding_name);
384*4882a593Smuzhiyun
385*4882a593Smuzhiyun rk_crypto_deinit();
386*4882a593Smuzhiyun
387*4882a593Smuzhiyun return res;
388*4882a593Smuzhiyun }
389*4882a593Smuzhiyun
demo_rsa(void)390*4882a593Smuzhiyun RK_RES demo_rsa(void)
391*4882a593Smuzhiyun {
392*4882a593Smuzhiyun RK_RES res;
393*4882a593Smuzhiyun
394*4882a593Smuzhiyun res = demo_rsa_pub_enc();
395*4882a593Smuzhiyun if (res) {
396*4882a593Smuzhiyun printf("demo_rsa_pub_enc demo failed.\n");
397*4882a593Smuzhiyun return res;
398*4882a593Smuzhiyun }
399*4882a593Smuzhiyun
400*4882a593Smuzhiyun res = demo_rsa_priv_enc();
401*4882a593Smuzhiyun if (res) {
402*4882a593Smuzhiyun printf("demo_rsa_priv_enc demo failed.\n");
403*4882a593Smuzhiyun return res;
404*4882a593Smuzhiyun }
405*4882a593Smuzhiyun
406*4882a593Smuzhiyun res = demo_rsa_sign_data();
407*4882a593Smuzhiyun if (res) {
408*4882a593Smuzhiyun printf("demo_rsa_sign_data demo failed.\n");
409*4882a593Smuzhiyun return res;
410*4882a593Smuzhiyun }
411*4882a593Smuzhiyun
412*4882a593Smuzhiyun res = demo_rsa_sign_digest();
413*4882a593Smuzhiyun if (res) {
414*4882a593Smuzhiyun printf("demo_rsa_sign_data demo failed.\n");
415*4882a593Smuzhiyun return res;
416*4882a593Smuzhiyun }
417*4882a593Smuzhiyun
418*4882a593Smuzhiyun return RK_CRYPTO_SUCCESS;
419*4882a593Smuzhiyun }
420*4882a593Smuzhiyun
421