• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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_ECC_C
23 #include "hks_openssl_ecc.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_engine.h"
36 #include "hks_template.h"
37 #include "securec.h"
38 
HksOpensslEccCheckKeyLen(uint32_t keyLen)39 static int32_t HksOpensslEccCheckKeyLen(uint32_t keyLen)
40 {
41     if ((keyLen != HKS_ECC_KEY_SIZE_224) && (keyLen != HKS_ECC_KEY_SIZE_256) && (keyLen != HKS_ECC_KEY_SIZE_384) &&
42         (keyLen != HKS_ECC_KEY_SIZE_521)) {
43         HKS_LOG_E("invalid param keyLen(0x%" LOG_PUBLIC "x)!", keyLen);
44         return HKS_ERROR_INVALID_ARGUMENT;
45     }
46     return HKS_SUCCESS;
47 }
48 
HksOpensslGetCurveId(uint32_t keyLen,int * nid)49 static int32_t HksOpensslGetCurveId(uint32_t keyLen, int *nid)
50 {
51     switch (keyLen) {
52         case HKS_ECC_KEY_SIZE_224:
53             *nid = NID_secp224r1;
54             break;
55         case HKS_ECC_KEY_SIZE_256:
56             *nid = NID_X9_62_prime256v1;
57             break;
58         case HKS_ECC_KEY_SIZE_384:
59             *nid = NID_secp384r1;
60             break;
61         case HKS_ECC_KEY_SIZE_521:
62             *nid = NID_secp521r1;
63             break;
64         default:
65             HKS_LOG_E("invalid key size.");
66             return HKS_ERROR_INVALID_AE_TAG;
67     }
68 
69     return HKS_SUCCESS;
70 }
71 
72 #ifdef HKS_SUPPORT_ECC_GENERATE_KEY
TransEccKeyToKeyBlob(const EC_KEY * eccKey,const struct KeyMaterialEcc * keyMaterial,BIGNUM * pubX,BIGNUM * pubY,uint8_t * rawMaterial)73 static int32_t TransEccKeyToKeyBlob(
74     const EC_KEY *eccKey, const struct KeyMaterialEcc *keyMaterial, BIGNUM *pubX, BIGNUM *pubY, uint8_t *rawMaterial)
75 {
76     const EC_GROUP *ecGroup = EC_KEY_get0_group(eccKey);
77     int retCode = EC_POINT_get_affine_coordinates_GFp(ecGroup, EC_KEY_get0_public_key(eccKey), pubX, pubY, NULL);
78     if (retCode <= 0) {
79         HksLogOpensslError();
80         return HKS_ERROR_CRYPTO_ENGINE_ERROR;
81     }
82 
83     const BIGNUM *priv = EC_KEY_get0_private_key(eccKey);
84     uint32_t offset = sizeof(struct KeyMaterialEcc);
85 
86     retCode = BN_bn2binpad(pubX, rawMaterial + offset, keyMaterial->xSize);
87     if (retCode <= 0) {
88         HksLogOpensslError();
89         return HKS_ERROR_CRYPTO_ENGINE_ERROR;
90     }
91     offset += keyMaterial->xSize;
92 
93     retCode = BN_bn2binpad(pubY, rawMaterial + offset, keyMaterial->ySize);
94     if (retCode <= 0) {
95         HksLogOpensslError();
96         return HKS_ERROR_CRYPTO_ENGINE_ERROR;
97     }
98     offset += keyMaterial->ySize;
99 
100     retCode = BN_bn2binpad(priv, rawMaterial + offset, keyMaterial->zSize);
101     if (retCode <= 0) {
102         HksLogOpensslError();
103         return HKS_ERROR_CRYPTO_ENGINE_ERROR;
104     }
105 
106     return HKS_SUCCESS;
107 }
108 
EccSaveKeyMaterial(const EC_KEY * eccKey,const struct HksKeySpec * spec,uint8_t ** output,uint32_t * outputSize)109 static int32_t EccSaveKeyMaterial(const EC_KEY *eccKey, const struct HksKeySpec *spec,
110     uint8_t **output, uint32_t *outputSize)
111 {
112     uint32_t keySize = spec->keyLen;
113     /* public exponent x and y, and private exponent, so need size is: keySize / 8 * 3 */
114     uint32_t rawMaterialLen = sizeof(struct KeyMaterialEcc) + HKS_KEY_BYTES(keySize) * ECC_KEYPAIR_CNT;
115     uint8_t *rawMaterial = (uint8_t *)HksMalloc(rawMaterialLen);
116     HKS_IF_NULL_LOGE_RETURN(rawMaterial, HKS_ERROR_MALLOC_FAIL, "malloc buffer failed!")
117 
118     (void)memset_s(rawMaterial, rawMaterialLen, 0, rawMaterialLen);
119 
120     /*
121      * ECC key data internal struct:
122      * struct KeyMaterialEcc + pubX_data + pubY_data + pri_data
123      */
124     struct KeyMaterialEcc *keyMaterial = (struct KeyMaterialEcc *)rawMaterial;
125     keyMaterial->keyAlg = (enum HksKeyAlg)spec->algType;
126     keyMaterial->keySize = keySize;
127     keyMaterial->xSize = HKS_KEY_BYTES(keySize);
128     keyMaterial->ySize = HKS_KEY_BYTES(keySize);
129     keyMaterial->zSize = HKS_KEY_BYTES(keySize);
130 
131     BIGNUM *pubX = BN_new();
132     BIGNUM *pubY = BN_new();
133 
134     int32_t ret;
135     do {
136         if ((pubX == NULL) || (pubY == NULL)) {
137             HKS_LOG_E("BN_new x or y failed");
138             ret = HKS_ERROR_NULL_POINTER;
139             HKS_FREE(rawMaterial);
140             break;
141         }
142         ret = TransEccKeyToKeyBlob(eccKey, keyMaterial, pubX, pubY, rawMaterial);
143         if (ret != HKS_SUCCESS) {
144             HKS_LOG_E("transfer ecc key to key blob failed");
145             HKS_FREE(rawMaterial);
146             break;
147         }
148         *output = rawMaterial;
149         *outputSize = rawMaterialLen;
150     } while (0);
151 
152     if (pubX != NULL) {
153         BN_free(pubX);
154         pubX = NULL;
155     }
156     if (pubY != NULL) {
157         BN_free(pubY);
158         pubY = NULL;
159     }
160     return ret;
161 }
162 
HksOpensslEccGenerateKey(const struct HksKeySpec * spec,struct HksBlob * key)163 int32_t HksOpensslEccGenerateKey(const struct HksKeySpec *spec, struct HksBlob *key)
164 {
165     if (spec->algType != HKS_ALG_ECC) {
166         HKS_LOG_E("invalid alg type %" LOG_PUBLIC "u", spec->algType);
167         return HKS_ERROR_INVALID_ARGUMENT;
168     }
169     HKS_IF_NOT_SUCC_LOGE_RETURN(HksOpensslEccCheckKeyLen(spec->keyLen),
170         HKS_ERROR_INVALID_ARGUMENT, "Ecc Invalid Param!")
171 
172     int curveId = 0;
173     HKS_IF_NOT_SUCC_LOGE_RETURN(HksOpensslGetCurveId(spec->keyLen, &curveId),
174         HKS_ERROR_INVALID_ARGUMENT, "Ecc get curveId failed!")
175 
176     EC_KEY *eccKey = NULL;
177     int32_t ret = HKS_ERROR_CRYPTO_ENGINE_ERROR;
178     do {
179         eccKey = EC_KEY_new_by_curve_name(curveId);
180         if (eccKey == NULL) {
181             HKS_LOG_E("new ec key failed");
182             HksLogOpensslError();
183             break;
184         }
185 
186         if (EC_KEY_generate_key(eccKey) <= 0) {
187             HKS_LOG_E("generate ec key failed");
188             HksLogOpensslError();
189             break;
190         }
191 
192         ret = EccSaveKeyMaterial(eccKey, spec, &key->data, &key->size);
193         HKS_IF_NOT_SUCC_LOGE(ret, "save ec key material failed! ret=0x%" LOG_PUBLIC "x", ret)
194     } while (0);
195 
196     if (eccKey != NULL) {
197         EC_KEY_free(eccKey);
198         eccKey = NULL;
199     }
200 
201     return ret;
202 }
203 #endif
204 
205 #if defined(HKS_SUPPORT_ECC_GET_PUBLIC_KEY) || defined(HKS_SUPPORT_SM2_GET_PUBLIC_KEY)
HksOpensslGetEccPubKey(const struct HksBlob * input,struct HksBlob * output)206 int32_t HksOpensslGetEccPubKey(const struct HksBlob *input, struct HksBlob *output)
207 {
208     struct KeyMaterialEcc *keyMaterial = (struct KeyMaterialEcc *)input->data;
209 
210     if ((keyMaterial->xSize == 0) || (keyMaterial->ySize == 0)) {
211         HKS_LOG_E("not support get pubkey");
212         return HKS_ERROR_NOT_SUPPORTED;
213     }
214 
215     output->size = sizeof(struct KeyMaterialEcc) + keyMaterial->xSize + keyMaterial->ySize;
216 
217     struct KeyMaterialEcc *publickeyMaterial = (struct KeyMaterialEcc *)output->data;
218     publickeyMaterial->keyAlg = keyMaterial->keyAlg;
219     publickeyMaterial->keySize = keyMaterial->keySize;
220     publickeyMaterial->xSize = keyMaterial->xSize;
221     publickeyMaterial->ySize = keyMaterial->ySize;
222     publickeyMaterial->zSize = 0;
223 
224     (void)memcpy_s(output->data + sizeof(struct KeyMaterialEcc), output->size - sizeof(struct KeyMaterialEcc),
225         input->data + sizeof(struct KeyMaterialEcc), keyMaterial->xSize + keyMaterial->ySize);
226 
227     return HKS_SUCCESS;
228 }
229 #endif
230 
GetEccModules(const uint8_t * key,uint32_t * keySize,uint32_t * publicXSize,uint32_t * publicYSize,uint32_t * privateXSize)231 static int GetEccModules(
232     const uint8_t *key, uint32_t *keySize, uint32_t *publicXSize, uint32_t *publicYSize, uint32_t *privateXSize)
233 {
234     struct KeyMaterialEcc *keyMaterial = (struct KeyMaterialEcc *)key;
235     *keySize = keyMaterial->keySize;
236     *publicXSize = keyMaterial->xSize;
237     *publicYSize = keyMaterial->ySize;
238     *privateXSize = keyMaterial->zSize;
239 
240     return 0;
241 }
242 
EccInitPublicKey(EC_KEY * eccKey,const uint8_t * keyPair,uint32_t xSize,uint32_t ySize)243 static int32_t EccInitPublicKey(EC_KEY *eccKey, const uint8_t *keyPair, uint32_t xSize, uint32_t ySize)
244 {
245     const EC_GROUP *ecGroup = EC_KEY_get0_group(eccKey);
246     if (ecGroup == NULL) {
247         HksLogOpensslError();
248         return HKS_ERROR_INVALID_ARGUMENT;
249     }
250 
251     int32_t ret = HKS_ERROR_CRYPTO_ENGINE_ERROR;
252     uint32_t offset = sizeof(struct KeyMaterialEcc);
253     EC_POINT *pub = EC_POINT_new(ecGroup);
254     BIGNUM *pubX = BN_bin2bn(keyPair + offset, xSize, NULL);
255     offset += xSize;
256     BIGNUM *pubY = BN_bin2bn(keyPair + offset, ySize, NULL);
257     do {
258         if ((pubX == NULL) || (pubY == NULL) || (pub == NULL)) {
259             HKS_LOG_E("new big num x or y or pub failed");
260             break;
261         }
262 
263         if (EC_POINT_set_affine_coordinates_GFp(ecGroup, pub, pubX, pubY, NULL) <= 0) {
264             HksLogOpensslError();
265             break;
266         }
267 
268         if (EC_KEY_set_public_key(eccKey, pub) <= 0) {
269             HksLogOpensslError();
270             break;
271         }
272         ret = HKS_SUCCESS;
273     } while (0);
274 
275     if (pubX != NULL) {
276         BN_free(pubX);
277         pubX = NULL;
278     }
279 
280     if (pubY != NULL) {
281         BN_free(pubY);
282         pubY = NULL;
283     }
284 
285     if (pub != NULL) {
286         EC_POINT_free(pub);
287         pub = NULL;
288     }
289     return ret;
290 }
291 
EccInitKey(const struct HksBlob * keyBlob,bool private)292 static EC_KEY *EccInitKey(const struct HksBlob *keyBlob, bool private)
293 {
294     /* get ecc pubX,pubY,pri */
295     uint8_t *keyPair = keyBlob->data;
296     uint32_t publicXSize;
297     uint32_t publicYSize;
298     uint32_t privateSize;
299     uint32_t keySize;
300 
301     if (GetEccModules(keyPair, &keySize, &publicXSize, &publicYSize, &privateSize) != 0) {
302         HKS_LOG_E("get ecc key modules is failed");
303         return NULL;
304     }
305 
306     int nid;
307     HKS_IF_NOT_SUCC_LOGE_RETURN(HksOpensslGetCurveId(keySize, &nid), NULL, "get curve id failed")
308 
309     EC_KEY *eccKey = EC_KEY_new_by_curve_name(nid);
310     if (eccKey == NULL) {
311         HksLogOpensslError();
312         return NULL;
313     }
314 
315     if (!private) {
316         if (EccInitPublicKey(eccKey, keyPair, publicXSize, publicYSize) != HKS_SUCCESS) {
317             HKS_LOG_E("initialize ecc public key failed");
318             EC_KEY_free(eccKey);
319             return NULL;
320         }
321     }
322 
323     if (private) {
324         BIGNUM *pri = BN_bin2bn(keyPair + sizeof(struct KeyMaterialEcc) + publicXSize + publicYSize, privateSize, NULL);
325         if (pri == NULL || EC_KEY_set_private_key(eccKey, pri) <= 0) {
326             HKS_LOG_E("build ecc key failed");
327             BN_free(pri);
328             EC_KEY_free(eccKey);
329             return NULL;
330         }
331         BN_clear_free(pri);
332     }
333 
334     return eccKey;
335 }
336 
GetEvpKey(const struct HksBlob * keyBlob,EVP_PKEY * key,bool private)337 static int32_t GetEvpKey(const struct HksBlob *keyBlob, EVP_PKEY *key, bool private)
338 {
339     EC_KEY *eccKey = EccInitKey(keyBlob, private);
340     HKS_IF_NULL_LOGE_RETURN(eccKey, HKS_ERROR_CRYPTO_ENGINE_ERROR, "initialize ecc key failed\n")
341 
342     if (EVP_PKEY_assign_EC_KEY(key, eccKey) <= 0) {
343         HksLogOpensslError();
344         EC_KEY_free(eccKey);
345         return HKS_ERROR_CRYPTO_ENGINE_ERROR;
346     }
347     return HKS_SUCCESS;
348 }
349 
GetNativePKey(const struct HksBlob * nativeKey,EVP_PKEY * key)350 static int32_t GetNativePKey(const struct HksBlob *nativeKey, EVP_PKEY *key)
351 {
352     int32_t ret = GetEvpKey(nativeKey, key, true);
353     HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "get native evp key failed")
354     return ret;
355 }
356 
GetPeerKey(const struct HksBlob * pubKey,EVP_PKEY * key)357 static int32_t GetPeerKey(const struct HksBlob *pubKey, EVP_PKEY *key)
358 {
359     int32_t ret = GetEvpKey(pubKey, key, false);
360     HKS_IF_NOT_SUCC_LOGE_RETURN(ret, HKS_ERROR_CRYPTO_ENGINE_ERROR, "get peer evp key failed")
361     return ret;
362 }
363 
EcdhDerive(EVP_PKEY_CTX * ctx,EVP_PKEY * peerKey,struct HksBlob * sharedKey)364 static int32_t EcdhDerive(EVP_PKEY_CTX *ctx, EVP_PKEY *peerKey, struct HksBlob *sharedKey)
365 {
366     size_t tmpSharedKeySize = (size_t)sharedKey->size;
367     if (EVP_PKEY_derive_init(ctx) != 1) {
368         HksLogOpensslError();
369         return HKS_ERROR_CRYPTO_ENGINE_ERROR;
370     }
371     if (EVP_PKEY_derive_set_peer(ctx, peerKey) != 1) {
372         HksLogOpensslError();
373         return HKS_ERROR_CRYPTO_ENGINE_ERROR;
374     }
375     if (EVP_PKEY_derive(ctx, NULL, &tmpSharedKeySize) != 1) {
376         HksLogOpensslError();
377         return HKS_ERROR_CRYPTO_ENGINE_ERROR;
378     }
379 
380     if (tmpSharedKeySize > sharedKey->size) {
381         return HKS_ERROR_BUFFER_TOO_SMALL;
382     }
383 
384     if (EVP_PKEY_derive(ctx, sharedKey->data, &tmpSharedKeySize) != 1) {
385         HksLogOpensslError();
386         return HKS_ERROR_CRYPTO_ENGINE_ERROR;
387     }
388     sharedKey->size = tmpSharedKeySize;
389 
390     return HKS_SUCCESS;
391 }
392 
AgreeKeyEcdh(const struct HksBlob * nativeKey,const struct HksBlob * pubKey,struct HksBlob * sharedKey)393 static int32_t AgreeKeyEcdh(const struct HksBlob *nativeKey, const struct HksBlob *pubKey, struct HksBlob *sharedKey)
394 {
395     int32_t res = HKS_ERROR_CRYPTO_ENGINE_ERROR;
396     EVP_PKEY *pKey = EVP_PKEY_new();
397     EVP_PKEY *peerKey = EVP_PKEY_new();
398     EVP_PKEY_CTX *ctx = NULL;
399 
400     do {
401         if ((peerKey == NULL) || (pKey == NULL)) {
402             HKS_LOG_E("new pkey failed\n");
403             break;
404         }
405         int32_t ret = GetNativePKey(nativeKey, pKey);
406         HKS_IF_NOT_SUCC_LOGE_BREAK(ret, "get native pkey failed\n")
407 
408         ret = GetPeerKey(pubKey, peerKey);
409         HKS_IF_NOT_SUCC_LOGE_BREAK(ret, "get peer pkey failed\n")
410 
411         ctx = EVP_PKEY_CTX_new(pKey, NULL);
412         if (ctx == NULL) {
413             HksLogOpensslError();
414             break;
415         }
416 
417         ret = EcdhDerive(ctx, peerKey, sharedKey);
418         HKS_IF_NOT_SUCC_LOGE_BREAK(ret, "derive ecdh key failed\n")
419 
420         res = HKS_SUCCESS;
421     } while (0);
422 
423     EVP_PKEY_CTX_free(ctx);
424     if (peerKey != NULL) {
425         EVP_PKEY_free(peerKey);
426     }
427     if (pKey != NULL) {
428         EVP_PKEY_free(pKey);
429     }
430 
431     return res;
432 }
433 
434 #ifdef HKS_SUPPORT_ECDH_AGREE_KEY
HksOpensslEcdhAgreeKey(const struct HksBlob * nativeKey,const struct HksBlob * pubKey,const struct HksKeySpec * spec,struct HksBlob * sharedKey)435 int32_t HksOpensslEcdhAgreeKey(const struct HksBlob *nativeKey, const struct HksBlob *pubKey,
436     const struct HksKeySpec *spec, struct HksBlob *sharedKey)
437 {
438     HKS_IF_NOT_SUCC_LOGE_RETURN(HksOpensslEccCheckKeyLen(spec->keyLen),
439         HKS_ERROR_INVALID_ARGUMENT, "invalid param!")
440     int32_t ret = AgreeKeyEcdh(nativeKey, pubKey, sharedKey);
441     HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "ecdh key agreement failed!")
442 
443     return ret;
444 }
445 #endif
446 
447 #ifdef HKS_SUPPORT_ECDSA_SIGN_VERIFY
InitEcdsaCtx(const struct HksBlob * mainKey,uint32_t digest,bool sign,uint32_t len)448 static EVP_PKEY_CTX *InitEcdsaCtx(const struct HksBlob *mainKey, uint32_t digest, bool sign, uint32_t len)
449 {
450     const EVP_MD *opensslAlg = GetOpensslAlg(digest);
451     if (digest == HKS_DIGEST_NONE) {
452         opensslAlg = GetOpensslAlgFromLen(len);
453     }
454     if (opensslAlg == NULL) {
455         HKS_LOG_E("get openssl algorithm fail");
456         return NULL;
457     }
458 
459     EC_KEY *eccKey = EccInitKey(mainKey, sign);
460     HKS_IF_NULL_LOGE_RETURN(eccKey, NULL, "initialize ecc key failed")
461 
462     EVP_PKEY *key = EVP_PKEY_new();
463     if (key == NULL) {
464         HksLogOpensslError();
465         EC_KEY_free(eccKey);
466         return NULL;
467     }
468 
469     if (EVP_PKEY_assign_EC_KEY(key, eccKey) <= 0) {
470         EC_KEY_free(eccKey);
471         EVP_PKEY_free(key);
472         return NULL;
473     }
474 
475     EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new(key, NULL);
476     if (ctx == NULL) {
477         HksLogOpensslError();
478         EVP_PKEY_free(key);
479         return NULL;
480     }
481     int32_t ret;
482     if (sign) {
483         ret = EVP_PKEY_sign_init(ctx);
484     } else {
485         ret = EVP_PKEY_verify_init(ctx);
486     }
487     EVP_PKEY_free(key);
488     if (ret != HKS_OPENSSL_SUCCESS) {
489         HksLogOpensslError();
490         EVP_PKEY_CTX_free(ctx);
491         return NULL;
492     }
493     if (EVP_PKEY_CTX_set_signature_md(ctx, opensslAlg) != HKS_OPENSSL_SUCCESS) {
494         HksLogOpensslError();
495         EVP_PKEY_CTX_free(ctx);
496         return NULL;
497     }
498     return ctx;
499 }
500 
HksOpensslEcdsaVerify(const struct HksBlob * key,const struct HksUsageSpec * usageSpec,const struct HksBlob * message,const struct HksBlob * signature)501 int32_t HksOpensslEcdsaVerify(const struct HksBlob *key, const struct HksUsageSpec *usageSpec,
502     const struct HksBlob *message, const struct HksBlob *signature)
503 {
504     EVP_PKEY_CTX *ctx = InitEcdsaCtx(key, usageSpec->digest, false, message->size);
505     HKS_IF_NULL_LOGE_RETURN(ctx, HKS_ERROR_INVALID_KEY_INFO, "initialize ecc context failed")
506 
507     if (EVP_PKEY_verify(ctx, signature->data, signature->size, message->data, message->size) != HKS_OPENSSL_SUCCESS) {
508         HksLogOpensslError();
509         EVP_PKEY_CTX_free(ctx);
510         return HKS_ERROR_CRYPTO_ENGINE_ERROR;
511     }
512 
513     EVP_PKEY_CTX_free(ctx);
514     return HKS_SUCCESS;
515 }
516 
HksOpensslEcdsaSign(const struct HksBlob * key,const struct HksUsageSpec * usageSpec,const struct HksBlob * message,struct HksBlob * signature)517 int32_t HksOpensslEcdsaSign(const struct HksBlob *key, const struct HksUsageSpec *usageSpec,
518     const struct HksBlob *message, struct HksBlob *signature)
519 {
520     EVP_PKEY_CTX *ctx = InitEcdsaCtx(key, usageSpec->digest, true, message->size);
521     HKS_IF_NULL_LOGE_RETURN(ctx, HKS_ERROR_INVALID_KEY_INFO, "initialize ecc context failed")
522 
523     size_t sigSize = (size_t)signature->size;
524     if (EVP_PKEY_sign(ctx, signature->data, &sigSize, message->data, message->size) != HKS_OPENSSL_SUCCESS) {
525         HksLogOpensslError();
526         EVP_PKEY_CTX_free(ctx);
527         return HKS_ERROR_CRYPTO_ENGINE_ERROR;
528     }
529     signature->size = (uint32_t)sigSize;
530     EVP_PKEY_CTX_free(ctx);
531     return HKS_SUCCESS;
532 }
533 #endif
534 #endif