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 for signature verification.
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
StdMalloc(uint32_t len)28 void *StdMalloc(uint32_t len) {
29 return malloc((size_t)len);
30 }
31
PrintLastError(void)32 void PrintLastError(void) {
33 const char *file = NULL;
34 uint32_t line = 0;
35 BSL_ERR_GetLastErrorFileLine(&file, &line);// Obtain the name and number of lines of the error file.
36 printf("failed at file %s at line %d\n", file, line);
37 }
38
main(void)39 int main(void)
40 {
41 int ret;
42 uint8_t userId[32] = {0};
43 uint8_t key[32] = {0};
44 uint8_t msg[32] = {0};
45 uint8_t signBuf[100] = {0};
46 uint32_t signLen = sizeof(signBuf);
47 CRYPT_EAL_PkeyPrv prv = {0};
48 CRYPT_EAL_PkeyPub pub = {0};
49 CRYPT_EAL_PkeyCtx *ctx = NULL;
50
51 BSL_ERR_Init(); // Initialize the error code module.
52 /**
53 * Before calling the algorithm APIs,
54 * call the BSL_SAL_CallBack_Ctrl function to register the malloc and free functions.
55 * Execute this step only once. If the memory allocation ability of Linux is available,
56 * the two functions can be registered using Linux by default.
57 */
58 BSL_SAL_CallBack_Ctrl(BSL_SAL_MEM_MALLOC, StdMalloc);
59 BSL_SAL_CallBack_Ctrl(BSL_SAL_MEM_FREE, free);
60 ret = CRYPT_EAL_Init(CRYPT_EAL_INIT_CPU | CRYPT_EAL_INIT_PROVIDER);
61 if (ret != CRYPT_SUCCESS) {
62 printf("error code is %x\n", ret);
63 goto EXIT;
64 }
65
66 ctx = CRYPT_EAL_PkeyNewCtx(CRYPT_PKEY_SM2);
67 if (ctx == NULL) {
68 goto EXIT;
69 }
70
71 // Set a user ID.
72 ret = CRYPT_EAL_PkeyCtrl(ctx, CRYPT_CTRL_SET_SM2_USER_ID, userId, sizeof(userId));
73 if (ret != CRYPT_SUCCESS) {
74 printf("error code is %x\n", ret);
75 PrintLastError();
76 goto EXIT;
77 }
78
79 // Initialize the random number.
80 ret = CRYPT_EAL_ProviderRandInitCtx(NULL, CRYPT_RAND_SHA256, "provider=default", NULL, 0, NULL);
81 if (ret != CRYPT_SUCCESS) {
82 printf("error code is %x\n", ret);
83 PrintLastError();
84 goto EXIT;
85 }
86
87 // Generate a key pair.
88 ret = CRYPT_EAL_PkeyGen(ctx);
89 if (ret != CRYPT_SUCCESS) {
90 printf("error code is %x\n", ret);
91 PrintLastError();
92 goto EXIT;
93 }
94
95 // Sign.
96 ret = CRYPT_EAL_PkeySign(ctx, CRYPT_MD_SM3, msg, sizeof(msg), signBuf, &signLen);
97 if (ret != CRYPT_SUCCESS) {
98 printf("error code is %x\n", ret);
99 PrintLastError();
100 goto EXIT;
101 }
102
103 // Verify the signature.
104 ret = CRYPT_EAL_PkeyVerify(ctx, CRYPT_MD_SM3, msg, sizeof(msg), signBuf, signLen);
105 if (ret != CRYPT_SUCCESS) {
106 printf("error code is %x\n", ret);
107 PrintLastError();
108 goto EXIT;
109 }
110
111 printf("pass \n");
112
113 EXIT:
114 // Release the context memory.
115 CRYPT_EAL_PkeyFreeCtx(ctx);
116 CRYPT_EAL_RandDeinit();
117 BSL_ERR_DeInit();
118 return ret;
119 }