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 HKS_IF_TRUE_LOGE_RETURN(spec->algType != HKS_ALG_ECC, HKS_ERROR_INVALID_ARGUMENT,
166 "invalid alg type %" LOG_PUBLIC "u", spec->algType)
167 HKS_IF_NOT_SUCC_LOGE_RETURN(HksOpensslEccCheckKeyLen(spec->keyLen),
168 HKS_ERROR_INVALID_ARGUMENT, "Ecc Invalid Param!")
169
170 int curveId = 0;
171 HKS_IF_NOT_SUCC_LOGE_RETURN(HksOpensslGetCurveId(spec->keyLen, &curveId),
172 HKS_ERROR_INVALID_ARGUMENT, "Ecc get curveId failed!")
173
174 EC_KEY *eccKey = NULL;
175 int32_t ret = HKS_ERROR_CRYPTO_ENGINE_ERROR;
176 do {
177 eccKey = EC_KEY_new_by_curve_name(curveId);
178 if (eccKey == NULL) {
179 HKS_LOG_E("new ec key failed");
180 HksLogOpensslError();
181 break;
182 }
183
184 if (EC_KEY_generate_key(eccKey) <= 0) {
185 HKS_LOG_E("generate ec key failed");
186 HksLogOpensslError();
187 break;
188 }
189
190 ret = EccSaveKeyMaterial(eccKey, spec, &key->data, &key->size);
191 HKS_IF_NOT_SUCC_LOGE(ret, "save ec key material failed! ret=0x%" LOG_PUBLIC "x", ret)
192 } while (0);
193
194 if (eccKey != NULL) {
195 EC_KEY_free(eccKey);
196 eccKey = NULL;
197 }
198
199 return ret;
200 }
201 #endif
202
203 #if defined(HKS_SUPPORT_ECC_GET_PUBLIC_KEY) || defined(HKS_SUPPORT_SM2_GET_PUBLIC_KEY)
HksOpensslGetEccPubKey(const struct HksBlob * input,struct HksBlob * output)204 int32_t HksOpensslGetEccPubKey(const struct HksBlob *input, struct HksBlob *output)
205 {
206 struct KeyMaterialEcc *keyMaterial = (struct KeyMaterialEcc *)input->data;
207 HKS_IF_TRUE_LOGE_RETURN(keyMaterial->xSize == 0 || keyMaterial->ySize == 0, HKS_ERROR_NOT_SUPPORTED,
208 "not support get pubkey")
209
210 output->size = sizeof(struct KeyMaterialEcc) + keyMaterial->xSize + keyMaterial->ySize;
211
212 struct KeyMaterialEcc *publickeyMaterial = (struct KeyMaterialEcc *)output->data;
213 publickeyMaterial->keyAlg = keyMaterial->keyAlg;
214 publickeyMaterial->keySize = keyMaterial->keySize;
215 publickeyMaterial->xSize = keyMaterial->xSize;
216 publickeyMaterial->ySize = keyMaterial->ySize;
217 publickeyMaterial->zSize = 0;
218
219 if (memcpy_s(output->data + sizeof(struct KeyMaterialEcc), output->size - sizeof(struct KeyMaterialEcc),
220 input->data + sizeof(struct KeyMaterialEcc), keyMaterial->xSize + keyMaterial->ySize) != EOK) {
221 HKS_LOG_E("copy output data + sizeof(struct KeyMaterialEcc) failed!");
222 return HKS_ERROR_INSUFFICIENT_MEMORY;
223 }
224
225 return HKS_SUCCESS;
226 }
227 #endif
228
GetEccModules(const uint8_t * key,uint32_t * keySize,uint32_t * publicXSize,uint32_t * publicYSize,uint32_t * privateXSize)229 static int GetEccModules(
230 const uint8_t *key, uint32_t *keySize, uint32_t *publicXSize, uint32_t *publicYSize, uint32_t *privateXSize)
231 {
232 struct KeyMaterialEcc *keyMaterial = (struct KeyMaterialEcc *)key;
233 *keySize = keyMaterial->keySize;
234 *publicXSize = keyMaterial->xSize;
235 *publicYSize = keyMaterial->ySize;
236 *privateXSize = keyMaterial->zSize;
237
238 return 0;
239 }
240
EccInitPublicKey(EC_KEY * eccKey,const uint8_t * keyPair,uint32_t xSize,uint32_t ySize)241 static int32_t EccInitPublicKey(EC_KEY *eccKey, const uint8_t *keyPair, uint32_t xSize, uint32_t ySize)
242 {
243 const EC_GROUP *ecGroup = EC_KEY_get0_group(eccKey);
244 if (ecGroup == NULL) {
245 HksLogOpensslError();
246 return HKS_ERROR_INVALID_ARGUMENT;
247 }
248
249 int32_t ret = HKS_ERROR_CRYPTO_ENGINE_ERROR;
250 uint32_t offset = sizeof(struct KeyMaterialEcc);
251 EC_POINT *pub = EC_POINT_new(ecGroup);
252 BIGNUM *pubX = BN_bin2bn(keyPair + offset, xSize, NULL);
253 offset += xSize;
254 BIGNUM *pubY = BN_bin2bn(keyPair + offset, ySize, NULL);
255 do {
256 if ((pubX == NULL) || (pubY == NULL) || (pub == NULL)) {
257 HKS_LOG_E("new big num x or y or pub failed");
258 break;
259 }
260
261 if (EC_POINT_set_affine_coordinates_GFp(ecGroup, pub, pubX, pubY, NULL) <= 0) {
262 HksLogOpensslError();
263 break;
264 }
265
266 if (EC_KEY_set_public_key(eccKey, pub) <= 0) {
267 HksLogOpensslError();
268 break;
269 }
270 ret = HKS_SUCCESS;
271 } while (0);
272
273 if (pubX != NULL) {
274 BN_free(pubX);
275 pubX = NULL;
276 }
277
278 if (pubY != NULL) {
279 BN_free(pubY);
280 pubY = NULL;
281 }
282
283 if (pub != NULL) {
284 EC_POINT_free(pub);
285 pub = NULL;
286 }
287 return ret;
288 }
289
EccInitKey(const struct HksBlob * keyBlob,bool private)290 static EC_KEY *EccInitKey(const struct HksBlob *keyBlob, bool private)
291 {
292 /* get ecc pubX,pubY,pri */
293 uint8_t *keyPair = keyBlob->data;
294 uint32_t publicXSize;
295 uint32_t publicYSize;
296 uint32_t privateSize;
297 uint32_t keySize;
298
299 HKS_IF_TRUE_LOGE_RETURN(GetEccModules(keyPair, &keySize, &publicXSize, &publicYSize, &privateSize) != 0, NULL,
300 "get ecc key modules is failed")
301
302 int nid;
303 HKS_IF_NOT_SUCC_LOGE_RETURN(HksOpensslGetCurveId(keySize, &nid), NULL, "get curve id failed")
304
305 EC_KEY *eccKey = EC_KEY_new_by_curve_name(nid);
306 if (eccKey == NULL) {
307 HksLogOpensslError();
308 return NULL;
309 }
310
311 if (!private) {
312 if (EccInitPublicKey(eccKey, keyPair, publicXSize, publicYSize) != HKS_SUCCESS) {
313 HKS_LOG_E("initialize ecc public key failed");
314 EC_KEY_free(eccKey);
315 return NULL;
316 }
317 }
318
319 if (private) {
320 BIGNUM *pri = BN_bin2bn(keyPair + sizeof(struct KeyMaterialEcc) + publicXSize + publicYSize, privateSize, NULL);
321 if (pri == NULL || EC_KEY_set_private_key(eccKey, pri) <= 0) {
322 HKS_LOG_E("build ecc key failed");
323 BN_free(pri);
324 EC_KEY_free(eccKey);
325 return NULL;
326 }
327 BN_clear_free(pri);
328 }
329
330 return eccKey;
331 }
332
GetEvpKey(const struct HksBlob * keyBlob,EVP_PKEY * key,bool private)333 static int32_t GetEvpKey(const struct HksBlob *keyBlob, EVP_PKEY *key, bool private)
334 {
335 EC_KEY *eccKey = EccInitKey(keyBlob, private);
336 HKS_IF_NULL_LOGE_RETURN(eccKey, HKS_ERROR_CRYPTO_ENGINE_ERROR, "initialize ecc key failed\n")
337
338 if (EVP_PKEY_assign_EC_KEY(key, eccKey) <= 0) {
339 HksLogOpensslError();
340 EC_KEY_free(eccKey);
341 return HKS_ERROR_CRYPTO_ENGINE_ERROR;
342 }
343 return HKS_SUCCESS;
344 }
345
GetNativePKey(const struct HksBlob * nativeKey,EVP_PKEY * key)346 static int32_t GetNativePKey(const struct HksBlob *nativeKey, EVP_PKEY *key)
347 {
348 int32_t ret = GetEvpKey(nativeKey, key, true);
349 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "get native evp key failed")
350 return ret;
351 }
352
GetPeerKey(const struct HksBlob * pubKey,EVP_PKEY * key)353 static int32_t GetPeerKey(const struct HksBlob *pubKey, EVP_PKEY *key)
354 {
355 int32_t ret = GetEvpKey(pubKey, key, false);
356 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, HKS_ERROR_CRYPTO_ENGINE_ERROR, "get peer evp key failed")
357 return ret;
358 }
359
EcdhDerive(EVP_PKEY_CTX * ctx,EVP_PKEY * peerKey,struct HksBlob * sharedKey)360 static int32_t EcdhDerive(EVP_PKEY_CTX *ctx, EVP_PKEY *peerKey, struct HksBlob *sharedKey)
361 {
362 size_t tmpSharedKeySize = (size_t)sharedKey->size;
363 if (EVP_PKEY_derive_init(ctx) != 1) {
364 HksLogOpensslError();
365 return HKS_ERROR_CRYPTO_ENGINE_ERROR;
366 }
367 if (EVP_PKEY_derive_set_peer(ctx, peerKey) != 1) {
368 HksLogOpensslError();
369 return HKS_ERROR_CRYPTO_ENGINE_ERROR;
370 }
371 if (EVP_PKEY_derive(ctx, NULL, &tmpSharedKeySize) != 1) {
372 HksLogOpensslError();
373 return HKS_ERROR_CRYPTO_ENGINE_ERROR;
374 }
375
376 HKS_IF_TRUE_RETURN(tmpSharedKeySize > sharedKey->size, HKS_ERROR_BUFFER_TOO_SMALL)
377
378 if (EVP_PKEY_derive(ctx, sharedKey->data, &tmpSharedKeySize) != 1) {
379 HksLogOpensslError();
380 return HKS_ERROR_CRYPTO_ENGINE_ERROR;
381 }
382 sharedKey->size = tmpSharedKeySize;
383
384 return HKS_SUCCESS;
385 }
386
AgreeKeyEcdh(const struct HksBlob * nativeKey,const struct HksBlob * pubKey,struct HksBlob * sharedKey)387 static int32_t AgreeKeyEcdh(const struct HksBlob *nativeKey, const struct HksBlob *pubKey, struct HksBlob *sharedKey)
388 {
389 int32_t res = HKS_ERROR_CRYPTO_ENGINE_ERROR;
390 EVP_PKEY *pKey = EVP_PKEY_new();
391 EVP_PKEY *peerKey = EVP_PKEY_new();
392 EVP_PKEY_CTX *ctx = NULL;
393
394 do {
395 if ((peerKey == NULL) || (pKey == NULL)) {
396 HKS_LOG_E("new pkey failed\n");
397 break;
398 }
399 int32_t ret = GetNativePKey(nativeKey, pKey);
400 HKS_IF_NOT_SUCC_LOGE_BREAK(ret, "get native pkey failed\n")
401
402 ret = GetPeerKey(pubKey, peerKey);
403 HKS_IF_NOT_SUCC_LOGE_BREAK(ret, "get peer pkey failed\n")
404
405 ctx = EVP_PKEY_CTX_new(pKey, NULL);
406 if (ctx == NULL) {
407 HksLogOpensslError();
408 break;
409 }
410
411 ret = EcdhDerive(ctx, peerKey, sharedKey);
412 HKS_IF_NOT_SUCC_LOGE_BREAK(ret, "derive ecdh key failed\n")
413
414 res = HKS_SUCCESS;
415 } while (0);
416
417 EVP_PKEY_CTX_free(ctx);
418 if (peerKey != NULL) {
419 EVP_PKEY_free(peerKey);
420 }
421 if (pKey != NULL) {
422 EVP_PKEY_free(pKey);
423 }
424
425 return res;
426 }
427
428 #ifdef HKS_SUPPORT_ECDH_AGREE_KEY
HksOpensslEcdhAgreeKey(const struct HksBlob * nativeKey,const struct HksBlob * pubKey,const struct HksKeySpec * spec,struct HksBlob * sharedKey)429 int32_t HksOpensslEcdhAgreeKey(const struct HksBlob *nativeKey, const struct HksBlob *pubKey,
430 const struct HksKeySpec *spec, struct HksBlob *sharedKey)
431 {
432 HKS_IF_NOT_SUCC_LOGE_RETURN(HksOpensslEccCheckKeyLen(spec->keyLen),
433 HKS_ERROR_INVALID_ARGUMENT, "invalid param!")
434 int32_t ret = AgreeKeyEcdh(nativeKey, pubKey, sharedKey);
435 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "ecdh key agreement failed!")
436
437 return ret;
438 }
439 #endif
440
441 #ifdef HKS_SUPPORT_ECDSA_SIGN_VERIFY
InitEcdsaCtx(const struct HksBlob * mainKey,uint32_t digest,bool sign,uint32_t len)442 static EVP_PKEY_CTX *InitEcdsaCtx(const struct HksBlob *mainKey, uint32_t digest, bool sign, uint32_t len)
443 {
444 const EVP_MD *opensslAlg = GetOpensslAlg(digest);
445 if (digest == HKS_DIGEST_NONE) {
446 opensslAlg = GetOpensslAlgFromLen(len);
447 }
448 if (opensslAlg == NULL) {
449 HKS_LOG_E("get openssl algorithm fail");
450 return NULL;
451 }
452
453 EC_KEY *eccKey = EccInitKey(mainKey, sign);
454 HKS_IF_NULL_LOGE_RETURN(eccKey, NULL, "initialize ecc key failed")
455
456 EVP_PKEY *key = EVP_PKEY_new();
457 if (key == NULL) {
458 HksLogOpensslError();
459 EC_KEY_free(eccKey);
460 return NULL;
461 }
462
463 if (EVP_PKEY_assign_EC_KEY(key, eccKey) <= 0) {
464 EC_KEY_free(eccKey);
465 EVP_PKEY_free(key);
466 return NULL;
467 }
468
469 EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new(key, NULL);
470 if (ctx == NULL) {
471 HksLogOpensslError();
472 EVP_PKEY_free(key);
473 return NULL;
474 }
475 int32_t ret;
476 if (sign) {
477 ret = EVP_PKEY_sign_init(ctx);
478 } else {
479 ret = EVP_PKEY_verify_init(ctx);
480 }
481 EVP_PKEY_free(key);
482 if (ret != HKS_OPENSSL_SUCCESS) {
483 HksLogOpensslError();
484 EVP_PKEY_CTX_free(ctx);
485 return NULL;
486 }
487 if (EVP_PKEY_CTX_set_signature_md(ctx, opensslAlg) != HKS_OPENSSL_SUCCESS) {
488 HksLogOpensslError();
489 EVP_PKEY_CTX_free(ctx);
490 return NULL;
491 }
492 return ctx;
493 }
494
HksOpensslEcdsaVerify(const struct HksBlob * key,const struct HksUsageSpec * usageSpec,const struct HksBlob * message,const struct HksBlob * signature)495 int32_t HksOpensslEcdsaVerify(const struct HksBlob *key, const struct HksUsageSpec *usageSpec,
496 const struct HksBlob *message, const struct HksBlob *signature)
497 {
498 EVP_PKEY_CTX *ctx = InitEcdsaCtx(key, usageSpec->digest, false, message->size);
499 HKS_IF_NULL_LOGE_RETURN(ctx, HKS_ERROR_INVALID_KEY_INFO, "initialize ecc context failed")
500
501 if (EVP_PKEY_verify(ctx, signature->data, signature->size, message->data, message->size) != HKS_OPENSSL_SUCCESS) {
502 HksLogOpensslError();
503 EVP_PKEY_CTX_free(ctx);
504 return HKS_ERROR_CRYPTO_ENGINE_ERROR;
505 }
506
507 EVP_PKEY_CTX_free(ctx);
508 return HKS_SUCCESS;
509 }
510
HksOpensslEcdsaSign(const struct HksBlob * key,const struct HksUsageSpec * usageSpec,const struct HksBlob * message,struct HksBlob * signature)511 int32_t HksOpensslEcdsaSign(const struct HksBlob *key, const struct HksUsageSpec *usageSpec,
512 const struct HksBlob *message, struct HksBlob *signature)
513 {
514 EVP_PKEY_CTX *ctx = InitEcdsaCtx(key, usageSpec->digest, true, message->size);
515 HKS_IF_NULL_LOGE_RETURN(ctx, HKS_ERROR_INVALID_KEY_INFO, "initialize ecc context failed")
516
517 size_t sigSize = (size_t)signature->size;
518 if (EVP_PKEY_sign(ctx, signature->data, &sigSize, message->data, message->size) != HKS_OPENSSL_SUCCESS) {
519 HksLogOpensslError();
520 EVP_PKEY_CTX_free(ctx);
521 return HKS_ERROR_CRYPTO_ENGINE_ERROR;
522 }
523 signature->size = (uint32_t)sigSize;
524 EVP_PKEY_CTX_free(ctx);
525 return HKS_SUCCESS;
526 }
527 #endif
528 #endif