1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #ifdef HKS_CONFIG_FILE
17 #include HKS_CONFIG_FILE
18 #else
19 #include "hks_config.h"
20 #endif
21
22 #ifdef HKS_SUPPORT_SM2_C
23 #include "hks_openssl_sm2.h"
24
25 #include <openssl/bn.h>
26 #include <openssl/ec.h>
27 #include <openssl/evp.h>
28 #include <openssl/obj_mac.h>
29 #include <openssl/ossl_typ.h>
30 #include <stdbool.h>
31 #include <stddef.h>
32
33 #include "hks_log.h"
34 #include "hks_mem.h"
35 #include "hks_openssl_ecc.h"
36 #include "hks_openssl_engine.h"
37 #include "hks_template.h"
38
39 #ifdef HKS_SUPPORT_SM2_GENERATE_KEY
HksOpensslSm2GenerateKey(const struct HksKeySpec * spec,struct HksBlob * key)40 int32_t HksOpensslSm2GenerateKey(const struct HksKeySpec *spec, struct HksBlob *key)
41 {
42 return HksOpensslEccGenerateKey(spec, key);
43 }
44 #endif
45
46 #ifdef HKS_SUPPORT_SM2_GET_PUBLIC_KEY
HksOpensslGetSm2PubKey(const struct HksBlob * input,struct HksBlob * output)47 int32_t HksOpensslGetSm2PubKey(const struct HksBlob *input, struct HksBlob *output)
48 {
49 return HksOpensslGetEccPubKey(input, output);
50 }
51 #endif
52
53 #ifdef HKS_SUPPORT_SM2_SIGN_VERIFY
GetSm2Modules(const uint8_t * key,uint32_t * keySize,uint32_t * publicXSize,uint32_t * publicYSize,uint32_t * privateXSize)54 static int GetSm2Modules(
55 const uint8_t *key, uint32_t *keySize, uint32_t *publicXSize, uint32_t *publicYSize, uint32_t *privateXSize)
56 {
57 struct KeyMaterialEcc *keyMaterial = (struct KeyMaterialEcc *)key;
58 *keySize = keyMaterial->keySize;
59 *publicXSize = keyMaterial->xSize;
60 *publicYSize = keyMaterial->ySize;
61 *privateXSize = keyMaterial->zSize;
62 return HKS_SUCCESS;
63 }
64
Sm2InitPublicKey(EC_KEY * sm2Key,const uint8_t * keyPair,uint32_t xSize,uint32_t ySize)65 static int32_t Sm2InitPublicKey(EC_KEY *sm2Key, const uint8_t *keyPair, uint32_t xSize, uint32_t ySize)
66 {
67 const EC_GROUP *ecGroup = EC_KEY_get0_group(sm2Key);
68 if (ecGroup == NULL) {
69 HksLogOpensslError();
70 return HKS_ERROR_INVALID_ARGUMENT;
71 }
72
73 int32_t ret = HKS_ERROR_CRYPTO_ENGINE_ERROR;
74 uint32_t offset = sizeof(struct KeyMaterialEcc);
75 EC_POINT *pub = EC_POINT_new(ecGroup);
76 BIGNUM *pubX = BN_bin2bn(keyPair + offset, xSize, NULL);
77 offset += xSize;
78 BIGNUM *pubY = BN_bin2bn(keyPair + offset, ySize, NULL);
79 do {
80 if ((pubX == NULL) || (pubY == NULL) || (pub == NULL)) {
81 HKS_LOG_E("new big num x or y or pub failed");
82 break;
83 }
84
85 if (EC_POINT_set_affine_coordinates_GFp(ecGroup, pub, pubX, pubY, NULL) <= 0) {
86 HKS_LOG_E("ec point set failed");
87 HksLogOpensslError();
88 break;
89 }
90
91 if (EC_KEY_set_public_key(sm2Key, pub) <= 0) {
92 HKS_LOG_E("set public key failed");
93 HksLogOpensslError();
94 break;
95 }
96 ret = HKS_SUCCESS;
97 } while (0);
98
99 SELF_FREE_PTR(pubX, BN_free);
100 SELF_FREE_PTR(pubY, BN_free);
101 SELF_FREE_PTR(pub, EC_POINT_free);
102 return ret;
103 }
104
Sm2InitKey(const struct HksBlob * keyBlob,bool private)105 static EC_KEY *Sm2InitKey(const struct HksBlob *keyBlob, bool private)
106 {
107 /* get ecc pubX,pubY,pri */
108 uint8_t *keyPair = keyBlob->data;
109 uint32_t publicXSize;
110 uint32_t publicYSize;
111 uint32_t privateSize;
112 uint32_t keySize;
113
114 HKS_IF_NOT_SUCC_LOGE_RETURN(GetSm2Modules(keyPair, &keySize, &publicXSize, &publicYSize, &privateSize),
115 NULL, "get sm2 key modules is failed")
116
117 EC_KEY *sm2Key = EC_KEY_new_by_curve_name(NID_sm2);
118 if (sm2Key == NULL) {
119 HKS_LOG_E("new sm2 key failed");
120 HksLogOpensslError();
121 return NULL;
122 }
123
124 if (!private) {
125 if (Sm2InitPublicKey(sm2Key, keyPair, publicXSize, publicYSize) != HKS_SUCCESS) {
126 HKS_LOG_E("initialize sm2 public key failed");
127 SELF_FREE_PTR(sm2Key, EC_KEY_free);
128 return NULL;
129 }
130 }
131
132 if (private) {
133 BIGNUM *pri = BN_bin2bn(keyPair + sizeof(struct KeyMaterialEcc) + publicXSize + publicYSize, privateSize, NULL);
134 if (pri == NULL || EC_KEY_set_private_key(sm2Key, pri) <= 0) {
135 HKS_LOG_E("build sm2 key failed");
136 SELF_FREE_PTR(pri, BN_free);
137 SELF_FREE_PTR(sm2Key, EC_KEY_free);
138 return NULL;
139 }
140 SELF_FREE_PTR(pri, BN_clear_free);
141 }
142
143 return sm2Key;
144 }
145
InitSm2Ctx(const struct HksBlob * mainKey,uint32_t digest,bool sign)146 static EVP_PKEY_CTX *InitSm2Ctx(const struct HksBlob *mainKey, uint32_t digest, bool sign)
147 {
148 EC_KEY *sm2Key = Sm2InitKey(mainKey, sign);
149 HKS_IF_NULL_LOGE_RETURN(sm2Key, NULL, "initialize sm2 key failed")
150
151 EVP_PKEY *key = EVP_PKEY_new();
152 if (key == NULL) {
153 HKS_LOG_E("new evp key failed");
154 HksLogOpensslError();
155 SELF_FREE_PTR(sm2Key, EC_KEY_free);
156 return NULL;
157 }
158
159 int32_t ret = HKS_ERROR_CRYPTO_ENGINE_ERROR;
160 EVP_PKEY_CTX *ctx = NULL;
161 do {
162 if (EVP_PKEY_assign_EC_KEY(key, sm2Key) <= 0) {
163 HKS_LOG_E("assign ec key failed");
164 break;
165 }
166
167 if (EVP_PKEY_set_alias_type(key, EVP_PKEY_SM2) != HKS_OPENSSL_SUCCESS) {
168 HKS_LOG_E("set alias type failed");
169 break;
170 }
171
172 ctx = EVP_PKEY_CTX_new(key, NULL);
173 HKS_IF_NULL_LOGE_BREAK(ctx, "new ctx failed")
174
175 if (sign) {
176 if (EVP_PKEY_sign_init(ctx) != HKS_OPENSSL_SUCCESS) {
177 HKS_LOG_E("sign init failed");
178 break;
179 }
180 } else {
181 if (EVP_PKEY_verify_init(ctx) != HKS_OPENSSL_SUCCESS) {
182 HKS_LOG_E("verify init failed");
183 break;
184 }
185 }
186 ret = HKS_SUCCESS;
187 } while (0);
188
189 if (ret != HKS_SUCCESS) {
190 HksLogOpensslError();
191 SELF_FREE_PTR(sm2Key, EC_KEY_free);
192 SELF_FREE_PTR(key, EVP_PKEY_free);
193 SELF_FREE_PTR(ctx, EVP_PKEY_CTX_free);
194 return NULL;
195 }
196
197 return ctx;
198 }
199
HksOpensslSm2Verify(const struct HksBlob * key,const struct HksUsageSpec * usageSpec,const struct HksBlob * message,const struct HksBlob * signature)200 int32_t HksOpensslSm2Verify(const struct HksBlob *key, const struct HksUsageSpec *usageSpec,
201 const struct HksBlob *message, const struct HksBlob *signature)
202 {
203 EVP_PKEY_CTX *ctx = InitSm2Ctx(key, usageSpec->digest, false);
204 HKS_IF_NULL_LOGE_RETURN(ctx, HKS_ERROR_INVALID_KEY_INFO, "initialize sm2 context failed")
205
206 if (EVP_PKEY_verify(ctx, signature->data, signature->size, message->data, message->size) != HKS_OPENSSL_SUCCESS) {
207 HKS_LOG_D("verify data failed");
208 HksLogOpensslError();
209 SELF_FREE_PTR(ctx, EVP_PKEY_CTX_free);
210 return HKS_ERROR_CRYPTO_ENGINE_ERROR;
211 }
212
213 SELF_FREE_PTR(ctx, EVP_PKEY_CTX_free);
214 return HKS_SUCCESS;
215 }
216
HksOpensslSm2Sign(const struct HksBlob * key,const struct HksUsageSpec * usageSpec,const struct HksBlob * message,struct HksBlob * signature)217 int32_t HksOpensslSm2Sign(const struct HksBlob *key, const struct HksUsageSpec *usageSpec,
218 const struct HksBlob *message, struct HksBlob *signature)
219 {
220 EVP_PKEY_CTX *ctx = InitSm2Ctx(key, usageSpec->digest, true);
221 HKS_IF_NULL_LOGE_RETURN(ctx, HKS_ERROR_INVALID_KEY_INFO, "initialize sm2 context failed")
222
223 size_t sigSize;
224 if (EVP_PKEY_sign(ctx, NULL, &sigSize, message->data, message->size) != HKS_OPENSSL_SUCCESS) {
225 HKS_LOG_D("get sigSize failed");
226 HksLogOpensslError();
227 SELF_FREE_PTR(ctx, EVP_PKEY_CTX_free);
228 return HKS_ERROR_CRYPTO_ENGINE_ERROR;
229 }
230
231 if (EVP_PKEY_sign(ctx, signature->data, &sigSize, message->data, message->size) != HKS_OPENSSL_SUCCESS) {
232 HKS_LOG_D("sign data failed");
233 HksLogOpensslError();
234 SELF_FREE_PTR(ctx, EVP_PKEY_CTX_free);
235 return HKS_ERROR_CRYPTO_ENGINE_ERROR;
236 }
237 signature->size = (uint32_t)sigSize;
238 SELF_FREE_PTR(ctx, EVP_PKEY_CTX_free);
239
240 return HKS_SUCCESS;
241 }
242 #endif
243
244 #endif
245