1 /*
2 * This file is part of the openHiTLS project.
3 *
4 * openHiTLS is licensed under the Mulan PSL v2.
5 * You can use this software according to the terms and conditions of the Mulan PSL v2.
6 * You may obtain a copy of Mulan PSL v2 at:
7 *
8 * http://license.coscl.org.cn/MulanPSL2
9 *
10 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13 * See the Mulan PSL v2 for more details.
14 */
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <stdint.h>
19 #include <string.h>
20 #include "crypt_eal_pkey.h" // Header file of the interfaces for asymmetric encryption and decryption.
21 #include "bsl_sal.h"
22 #include "bsl_err.h"
23 #include "crypt_algid.h"
24 #include "crypt_errno.h"
25 #include "crypt_eal_rand.h"
26 #include "crypt_eal_init.h"
27 #include "crypt_types.h"
28
StdMalloc(uint32_t len)29 void *StdMalloc(uint32_t len) {
30 return malloc((uint32_t)len);
31 }
PrintLastError(void)32 void PrintLastError(void) {
33 const char *file = NULL;
34 uint32_t line = 0;
35 BSL_ERR_GetLastErrorFileLine(&file, &line);
36 printf("failed at file %s at line %d\n", file, line);
37 }
38
main(void)39 int main(void) {
40 int32_t ret;
41 BSL_ERR_Init(); // Initialize the error code module.
42 /**
43 * Before calling the algorithm APIs,
44 * call the BSL_SAL_CallBack_Ctrl function to register the malloc and free functions.
45 * Execute this step only once. If the memory allocation ability of Linux is available,
46 * the two functions can be registered using Linux by default.
47 */
48 BSL_SAL_CallBack_Ctrl(BSL_SAL_MEM_MALLOC, StdMalloc);
49 BSL_SAL_CallBack_Ctrl(BSL_SAL_MEM_FREE, free);
50 ret = CRYPT_EAL_Init(CRYPT_EAL_INIT_CPU | CRYPT_EAL_INIT_PROVIDER);
51 if (ret != CRYPT_SUCCESS) {
52 printf("error code is %x\n", ret);
53 PrintLastError();
54 goto EXIT;
55 }
56 CRYPT_EAL_PkeyCtx *pkey = NULL;
57 pkey = CRYPT_EAL_PkeyNewCtx(CRYPT_PKEY_SM2);
58 if (pkey == NULL) {
59 PrintLastError();
60 goto EXIT;
61 }
62
63 // Initialize the random number.
64 ret = CRYPT_EAL_ProviderRandInitCtx(NULL, CRYPT_RAND_SHA256, "provider=default", NULL, 0, NULL);
65 if (ret != CRYPT_SUCCESS) {
66 printf("RandInit: error code is %x\n", ret);
67 PrintLastError();
68 goto EXIT;
69 }
70
71 // Generate a key pair.
72 ret = CRYPT_EAL_PkeyGen(pkey);
73 if (ret != CRYPT_SUCCESS) {
74 printf("CRYPT_EAL_PkeyGen: error code is %x\n", ret);
75 PrintLastError();
76 goto EXIT;
77 }
78
79 // Data to be encrypted.
80 char *data = "test enc data";
81 uint32_t dataLen = 12;
82 uint8_t ecrypt[125] = {0};
83 uint32_t ecryptLen = 125;
84 uint8_t dcrypt[125] = {0};
85 uint32_t dcryptLen = 125;
86 // Encrypt data.
87 ret = CRYPT_EAL_PkeyEncrypt(pkey, data, dataLen, ecrypt, &ecryptLen);
88 if (ret != CRYPT_SUCCESS) {
89 printf("CRYPT_EAL_PkeyEncrypt: error code is %x\n", ret);
90 PrintLastError();
91 goto EXIT;
92 }
93
94 // Decrypt data.
95 ret = CRYPT_EAL_PkeyDecrypt(pkey, ecrypt, ecryptLen, dcrypt, &dcryptLen);
96 if (ret != CRYPT_SUCCESS) {
97 printf("CRYPT_EAL_PkeyDecrypt: error code is %x\n", ret);
98 PrintLastError();
99 goto EXIT;
100 }
101
102 if (memcmp(dcrypt, data, dataLen) == 0) {
103 printf("encrypt and decrypt success\n");
104 } else {
105 ret = -1;
106 }
107 EXIT:
108 // Release the context memory.
109 CRYPT_EAL_PkeyFreeCtx(pkey);
110 CRYPT_EAL_RandDeinit();
111 BSL_ERR_DeInit();
112 return ret;
113 }