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