• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <stddef.h>
17 #include <string.h>
18 #include "crypt_algid.h"
19 #include "crypt_errno.h"
20 #include "crypt_eal_pkey.h"
21 #include "benchmark.h"
22 
Sm2NewCtx(void ** ctx)23 static int32_t Sm2NewCtx(void **ctx)
24 {
25     CRYPT_EAL_PkeyCtx *pkeyCtx = CRYPT_EAL_PkeyNewCtx(CRYPT_PKEY_SM2);
26     if (pkeyCtx == NULL) {
27         printf("Failed to create pkey context\n");
28         return CRYPT_MEM_ALLOC_FAIL;
29     }
30     *ctx = pkeyCtx;
31     return CRYPT_SUCCESS;
32 }
33 
Sm2FreeCtx(void * ctx)34 static void Sm2FreeCtx(void *ctx)
35 {
36     CRYPT_EAL_PkeyFreeCtx(ctx);
37 }
38 
Sm2KeyGen(void * ctx,BenchCtx * bench)39 static int32_t Sm2KeyGen(void *ctx, BenchCtx *bench)
40 {
41     int rc = CRYPT_SUCCESS;
42     BENCH_TIMES(rc = CRYPT_EAL_PkeyGen(ctx), rc, CRYPT_SUCCESS, bench->times, "sm2 keyGen");
43     return rc;
44 }
45 
Sm2KeyDeriveInner(void * ctx,void * peerCtx)46 static int32_t Sm2KeyDeriveInner(void *ctx, void *peerCtx)
47 {
48     int rc = CRYPT_SUCCESS;
49     uint8_t localR[128] = {0};
50     rc = CRYPT_EAL_PkeyCtrl(ctx, CRYPT_CTRL_GENE_SM2_R, localR, sizeof(localR));
51     if (rc != CRYPT_SUCCESS) {
52         printf("Failed to generate R\n");
53         return rc;
54     }
55     uint8_t shareKey[64] = {0};
56     uint32_t shareKeyLen = sizeof(shareKey);
57     rc = CRYPT_EAL_PkeyComputeShareKey(ctx, peerCtx, shareKey, &shareKeyLen);
58     if (rc != CRYPT_SUCCESS) {
59         printf("Failed to compute share key\n");
60         return rc;
61     }
62     return CRYPT_SUCCESS;
63 }
64 
Sm2KeyDerive(void * ctx,BenchCtx * bench)65 static int32_t Sm2KeyDerive(void *ctx, BenchCtx *bench)
66 {
67     int rc = CRYPT_SUCCESS;
68     CRYPT_EAL_PkeyCtx *peerCtx = CRYPT_EAL_PkeyNewCtx(CRYPT_PKEY_SM2);
69     if (peerCtx == NULL || CRYPT_EAL_PkeyGen(peerCtx) != CRYPT_SUCCESS) {
70         printf("Failed to create pkey context\n");
71         CRYPT_EAL_PkeyFreeCtx(peerCtx);
72         rc = CRYPT_MEM_ALLOC_FAIL;
73         goto ERR_OUT;
74     }
75 
76     char *peerRHex =
77         "04acc27688a6f7b706098bc91ff3ad1bff7dc2802cdb14ccccdb0a90471f9bd7072fedac0494b2ffc4d6853876c79b8f301c6573ad0aa50f39fc87181e1a1b46fe";
78     uint8_t peerR[128] = {0};
79     uint32_t peerRLen = sizeof(peerR);
80     Hex2Bin(peerRHex, peerR, &peerRLen);
81     rc = CRYPT_EAL_PkeyCtrl(peerCtx, CRYPT_CTRL_SET_SM2_R, peerR, peerRLen);
82     if (rc != CRYPT_SUCCESS) {
83         printf("Failed to set R\n");
84         goto ERR_OUT;
85     }
86     BENCH_TIMES(Sm2KeyDeriveInner(ctx, peerCtx), rc, CRYPT_SUCCESS, bench->times, "sm2 keyDerive");
87 ERR_OUT:
88     CRYPT_EAL_PkeyFreeCtx(peerCtx);
89     return rc;
90 }
91 
Sm2EncInner(void * ctx)92 static int32_t Sm2EncInner(void *ctx)
93 {
94     uint8_t plainText[32];
95     uint8_t cipherText[256]; // > 32 + 97 + 12
96     uint32_t outLen = sizeof(cipherText);
97     return CRYPT_EAL_PkeyEncrypt(ctx, plainText, sizeof(plainText), cipherText, &outLen);
98 }
99 
Sm2Enc(void * ctx,BenchCtx * bench)100 static int32_t Sm2Enc(void *ctx, BenchCtx *bench)
101 {
102     int rc = CRYPT_SUCCESS;
103     BENCH_TIMES(Sm2EncInner(ctx), rc, CRYPT_SUCCESS, bench->times, "sm2 enc");
104     return rc;
105 }
106 
Sm2Dec(void * ctx,BenchCtx * bench)107 static int32_t Sm2Dec(void *ctx, BenchCtx *bench)
108 {
109     int rc;
110     uint8_t plainText[32];
111     uint32_t plainTextLen = sizeof(plainText);
112     uint8_t cipherText[256]; // > 32 + 97 + 12
113     uint32_t outLen = sizeof(cipherText);
114     rc = CRYPT_EAL_PkeyEncrypt(ctx, plainText, sizeof(plainText), cipherText, &outLen);
115     if (rc != CRYPT_SUCCESS) {
116         printf("Failed to encrypt\n");
117         return rc;
118     }
119     BENCH_TIMES(CRYPT_EAL_PkeyDecrypt(ctx, cipherText, outLen, plainText, &plainTextLen), rc, CRYPT_SUCCESS, bench->times,
120                 "sm2 dec");
121     return rc;
122 }
123 
Sm2SignInner(void * ctx)124 static int32_t Sm2SignInner(void *ctx)
125 {
126     uint8_t plainText[32];
127     uint8_t signature[256];
128     uint32_t signatureLen = sizeof(signature);
129     return CRYPT_EAL_PkeySign(ctx, CRYPT_MD_SM3, plainText, sizeof(plainText), signature, &signatureLen);
130 }
131 
Sm2Sign(void * ctx,BenchCtx * bench)132 static int32_t Sm2Sign(void *ctx, BenchCtx *bench)
133 {
134     int rc;
135     BENCH_TIMES(Sm2SignInner(ctx), rc, CRYPT_SUCCESS, bench->times, "sm2 sign");
136     return rc;
137 }
138 
Sm2Verify(void * ctx,BenchCtx * bench)139 static int32_t Sm2Verify(void *ctx, BenchCtx *bench)
140 {
141     int rc;
142     uint8_t plainText[32];
143     uint8_t signature[256];
144     uint32_t signatureLen = sizeof(signature);
145     rc = CRYPT_EAL_PkeySign(ctx, CRYPT_MD_SM3, plainText, sizeof(plainText), signature, &signatureLen);
146     if (rc != CRYPT_SUCCESS) {
147         printf("Failed to sign\n");
148         return rc;
149     }
150     BENCH_TIMES(CRYPT_EAL_PkeyVerify(ctx, CRYPT_MD_SM3, plainText, sizeof(plainText), signature, signatureLen), rc,
151                 CRYPT_SUCCESS, bench->times, "sm2 verify");
152     return rc;
153 }
154 
155 DEFINE_OPS(Sm2);
156 DEFINE_BENCH_CTX(Sm2);
157