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 <stdint.h>
17 #include <stddef.h>
18 #include "hitls_build.h"
19 #include "securec.h"
20 #include "bsl_sal.h"
21 #include "hitls_crypt_reg.h"
22 #include "hitls_error.h"
23 #include "hs_common.h"
24 #include "config_type.h"
25 #include "stub_replace.h"
26 #include "crypt_default.h"
27 #ifdef HITLS_TLS_FEATURE_PROVIDER
28 #include "hitls_crypt.h"
29 #include "crypt_eal_rand.h"
30 #endif
31
32 #define MD5_DIGEST_LENGTH 16
33 #define SHA1_DIGEST_LENGTH 20
34 #define SHA256_DIGEST_LENGTH 32
35 #define SHA384_DIGEST_LENGTH 48
36 #define SHA512_DIGEST_LENGTH 64
37 #define SM3_DIGEST_LENGTH 32
38 #define AEAD_TAG_LENGTH 16
39
40 typedef struct {
41 HITLS_HashAlgo algo;
42 uint8_t *key;
43 uint32_t keyLen;
44 } FRAME_HmacCtx;
45
46 typedef struct {
47 HITLS_HashAlgo algo;
48 } FRAME_HashCtx;
49
50 typedef struct {
51 uint8_t *pubKey;
52 uint32_t pubKeyLen;
53 uint8_t *privateKey;
54 uint32_t privateKeyLen;
55 } FRAME_EcdhKey;
56
57 typedef struct {
58 uint8_t *p;
59 uint8_t *g;
60 uint16_t plen;
61 uint16_t glen;
62 uint8_t *pubKey;
63 uint32_t pubKeyLen;
64 uint8_t *privateKey;
65 uint32_t privateKeyLen;
66 } FRAME_DhKey;
67
68 /**
69 * @ingroup hitls_crypt_reg
70 * @brief Obtain the random number
71 *
72 * @param buf [OUT] random number
73 * @param len [IN] random number length
74 *
75 * @return 0 indicates success. Other values indicate failure
76 */
STUB_CRYPT_RandBytesCallback(uint8_t * buf,uint32_t len)77 int32_t STUB_CRYPT_RandBytesCallback(uint8_t *buf, uint32_t len)
78 {
79 if (memset_s(buf, len, 1, len) != EOK) {
80 return HITLS_MEMCPY_FAIL;
81 }
82 return HITLS_SUCCESS;
83 }
84
STUB_CRYPT_RandBytesCallbackLibCtx(void * libCtx,uint8_t * buf,uint32_t len)85 int32_t STUB_CRYPT_RandBytesCallbackLibCtx(void *libCtx, uint8_t *buf, uint32_t len)
86 {
87 (void)libCtx;
88 if (memset_s(buf, len, 1, len) != EOK) {
89 return HITLS_MEMCPY_FAIL;
90 }
91 return HITLS_SUCCESS;
92 }
93
94 /**
95 * @ingroup hitls_crypt_reg
96 * @brief Generate a key pair based on the elliptic curve parameters
97 *
98 * @param curveParams [IN] Elliptic curve parameters
99 *
100 * @return Key handle
101 */
STUB_CRYPT_GenerateEcdhKeyPairCallback(const HITLS_ECParameters * curveParams)102 HITLS_CRYPT_Key *STUB_CRYPT_GenerateEcdhKeyPairCallback(const HITLS_ECParameters *curveParams)
103 {
104 uint32_t keyLen = 0u;
105 if (curveParams == NULL) {
106 return NULL;
107 }
108
109 FRAME_EcdhKey *ecdhKey = (FRAME_EcdhKey *)BSL_SAL_Calloc(1u, sizeof(FRAME_EcdhKey));
110 if (ecdhKey == NULL) {
111 return NULL;
112 }
113 const TLS_GroupInfo *groupInfo = NULL;
114 switch (curveParams->type) {
115 case HITLS_EC_CURVE_TYPE_NAMED_CURVE:
116 groupInfo = ConfigGetGroupInfo(NULL, curveParams->param.namedcurve);
117 if (groupInfo == NULL) {
118 BSL_SAL_FREE(ecdhKey);
119 return NULL;
120 }
121 keyLen = groupInfo->pubkeyLen;
122 break;
123 default:
124 break;
125 }
126
127 uint8_t *pubKey = (uint8_t *)BSL_SAL_Malloc(keyLen);
128 if (pubKey == NULL) {
129 BSL_SAL_FREE(ecdhKey);
130 return NULL;
131 }
132 memset_s(pubKey, keyLen, 1u, keyLen);
133
134 uint8_t *privateKey = (uint8_t *)BSL_SAL_Malloc(keyLen);
135 if (privateKey == NULL) {
136 BSL_SAL_FREE(pubKey);
137 BSL_SAL_FREE(ecdhKey);
138 return NULL;
139 }
140 memset_s(privateKey, keyLen, 2u, keyLen);
141
142 ecdhKey->pubKey = pubKey;
143 ecdhKey->pubKeyLen = keyLen;
144 ecdhKey->privateKey = privateKey;
145 ecdhKey->privateKeyLen = keyLen;
146 return ecdhKey;
147 }
148
STUB_CRYPT_GenerateEcdhKeyPairCallbackLibCtx(void * libCtx,const char * attrName,const HITLS_Config * config,const HITLS_ECParameters * curveParams)149 HITLS_CRYPT_Key *STUB_CRYPT_GenerateEcdhKeyPairCallbackLibCtx(void *libCtx,
150 const char *attrName, const HITLS_Config *config, const HITLS_ECParameters *curveParams)
151 {
152 (void)libCtx;
153 (void)attrName;
154 (void)config;
155 return STUB_CRYPT_GenerateEcdhKeyPairCallback(curveParams);
156 }
157
158 /**
159 * @ingroup hitls_crypt_reg
160 * @brief Release the key
161 *
162 * @param key [IN] Key handle
163 */
STUB_CRYPT_FreeEcdhKeyCallback(HITLS_CRYPT_Key * key)164 void STUB_CRYPT_FreeEcdhKeyCallback(HITLS_CRYPT_Key *key)
165 {
166 FRAME_EcdhKey *ecdhKey = (FRAME_EcdhKey *)key;
167 if (ecdhKey != NULL) {
168 BSL_SAL_FREE(ecdhKey->pubKey);
169 BSL_SAL_FREE(ecdhKey->privateKey);
170 BSL_SAL_FREE(ecdhKey);
171 }
172 return;
173 }
174
175 /**
176 * @ingroup hitls_crypt_reg
177 * @brief Extract the public key data
178 *
179 * @param key [IN] Key handle
180 * @param pubKeyBuf [OUT] Public key data
181 * @param bufLen [IN] buffer length
182 * @param pubKeyLen [OUT] Public key data length
183 *
184 * @return 0 indicates success. Other values indicate failure
185 */
STUB_CRYPT_GetEcdhEncodedPubKeyCallback(HITLS_CRYPT_Key * key,uint8_t * pubKeyBuf,uint32_t bufLen,uint32_t * pubKeyLen)186 int32_t STUB_CRYPT_GetEcdhEncodedPubKeyCallback(HITLS_CRYPT_Key *key, uint8_t *pubKeyBuf, uint32_t bufLen,
187 uint32_t *pubKeyLen)
188 {
189 FRAME_EcdhKey *ecdhKey = (FRAME_EcdhKey *)key;
190 if ((ecdhKey == NULL) ||
191 (pubKeyBuf == NULL) ||
192 (pubKeyLen == NULL) ||
193 (bufLen < ecdhKey->pubKeyLen)) {
194 return HITLS_INTERNAL_EXCEPTION;
195 }
196
197 if (memcpy_s(pubKeyBuf, bufLen, ecdhKey->pubKey, ecdhKey->pubKeyLen) != EOK) {
198 return HITLS_MEMCPY_FAIL;
199 }
200 *pubKeyLen = ecdhKey->pubKeyLen;
201 return HITLS_SUCCESS;
202 }
203
204 /**
205 * @ingroup hitls_crypt_reg
206 * @brief Calculate the shared key based on the local key and peer public key
207 *
208 * @param key [IN] Key handle
209 * @param pubKeyBuf [IN] Public key data
210 * @param pubKeyLen [IN] Public key data length
211 * @param sharedSecret [OUT] Shared key
212 * @param sharedSecretLen [IN/OUT] IN: Maximum length of the key padding OUT: Key length
213 *
214 * @return 0 indicates success. Other values indicate failure
215 */
STUB_CRYPT_CalcEcdhSharedSecretCallback(HITLS_CRYPT_Key * key,uint8_t * peerPubkey,uint32_t pubKeyLen,uint8_t * sharedSecret,uint32_t * sharedSecretLen)216 int32_t STUB_CRYPT_CalcEcdhSharedSecretCallback(HITLS_CRYPT_Key *key, uint8_t *peerPubkey, uint32_t pubKeyLen,
217 uint8_t *sharedSecret, uint32_t *sharedSecretLen)
218 {
219 FRAME_EcdhKey *ecdhKey = (FRAME_EcdhKey *)key;
220
221 if ((ecdhKey == NULL) ||
222 (peerPubkey == NULL) ||
223 (sharedSecret == NULL) ||
224 (sharedSecretLen == NULL) ||
225 (ecdhKey->privateKeyLen > pubKeyLen) ||
226 (*sharedSecretLen < pubKeyLen)) {
227 return HITLS_INTERNAL_EXCEPTION;
228 }
229
230 if (memset_s(sharedSecret, *sharedSecretLen, 3u, pubKeyLen) != EOK) {
231 return HITLS_MEMCPY_FAIL;
232 }
233 *sharedSecretLen = pubKeyLen;
234 return HITLS_SUCCESS;
235 }
236
STUB_CRYPT_CalcEcdhSharedSecretCallbackLibCtx(void * libCtx,const char * attrName,HITLS_CRYPT_Key * key,uint8_t * peerPubkey,uint32_t pubKeyLen,uint8_t * sharedSecret,uint32_t * sharedSecretLen)237 int32_t STUB_CRYPT_CalcEcdhSharedSecretCallbackLibCtx(void *libCtx, const char *attrName,
238 HITLS_CRYPT_Key *key, uint8_t *peerPubkey,
239 uint32_t pubKeyLen, uint8_t *sharedSecret, uint32_t *sharedSecretLen)
240 {
241 (void)libCtx;
242 (void)attrName;
243 return STUB_CRYPT_CalcEcdhSharedSecretCallback(key, peerPubkey, pubKeyLen, sharedSecret, sharedSecretLen);
244 }
245
STUB_CRYPT_FreeDhKeyCallback(HITLS_CRYPT_Key * key)246 void STUB_CRYPT_FreeDhKeyCallback(HITLS_CRYPT_Key *key)
247 {
248 FRAME_DhKey *dhKey = (FRAME_DhKey *)key;
249 if (dhKey != NULL) {
250 BSL_SAL_FREE(dhKey->p);
251 BSL_SAL_FREE(dhKey->g);
252 BSL_SAL_FREE(dhKey->privateKey);
253 BSL_SAL_FREE(dhKey->pubKey);
254 BSL_SAL_FREE(dhKey);
255 }
256 return;
257 }
258
STUB_CRYPT_GenerateDhKeyBySecbitsCallback(int32_t secbits)259 HITLS_CRYPT_Key *STUB_CRYPT_GenerateDhKeyBySecbitsCallback(int32_t secbits)
260 {
261 uint16_t plen;
262 if (secbits >= 192) {
263 plen = 1024;
264 } else if (secbits >= 152) {
265 plen = 512;
266 } else if (secbits >= 128) {
267 plen = 384;
268 } else if (secbits >= 112) {
269 plen = 256;
270 } else {
271 plen = 128;
272 }
273
274 FRAME_DhKey *dhKey = (FRAME_DhKey *)BSL_SAL_Calloc(1u, sizeof(FRAME_DhKey));
275 if (dhKey == NULL) {
276 return NULL;
277 }
278
279 dhKey->p = BSL_SAL_Calloc(1u, plen);
280 if (dhKey->p == NULL) {
281 BSL_SAL_FREE(dhKey);
282 return NULL;
283 }
284 memset_s(dhKey->p, plen, 1u, plen);
285 dhKey->plen = plen;
286
287 dhKey->g = BSL_SAL_Calloc(1u, plen);
288 if (dhKey->g == NULL) {
289 STUB_CRYPT_FreeDhKeyCallback(dhKey);
290 return NULL;
291 }
292 memset_s(dhKey->g, plen, 2u, plen);
293 dhKey->glen = plen;
294
295 dhKey->pubKey = BSL_SAL_Calloc(1u, plen);
296 if (dhKey->pubKey == NULL) {
297 STUB_CRYPT_FreeDhKeyCallback(dhKey);
298 return NULL;
299 }
300 memset_s(dhKey->pubKey, plen, 3u, plen);
301 dhKey->pubKeyLen = plen;
302
303 dhKey->privateKey = BSL_SAL_Calloc(1u, plen);
304 if (dhKey->privateKey == NULL) {
305 STUB_CRYPT_FreeDhKeyCallback(dhKey);
306 return NULL;
307 }
308 memset_s(dhKey->privateKey, plen, 4u, plen);
309 dhKey->privateKeyLen = plen;
310
311 return dhKey;
312 }
313
STUB_CRYPT_GenerateDhKeyBySecbitsCallbackLibCtx(void * libCtx,const char * attrName,int32_t secbits)314 HITLS_CRYPT_Key *STUB_CRYPT_GenerateDhKeyBySecbitsCallbackLibCtx(void *libCtx, const char *attrName, int32_t secbits)
315 {
316 (void)libCtx;
317 (void)attrName;
318 return STUB_CRYPT_GenerateDhKeyBySecbitsCallback(secbits);
319 }
320
STUB_CRYPT_GenerateDhKeyByParamsCallback(uint8_t * p,uint16_t plen,uint8_t * g,uint16_t glen)321 HITLS_CRYPT_Key *STUB_CRYPT_GenerateDhKeyByParamsCallback(uint8_t *p, uint16_t plen, uint8_t *g, uint16_t glen)
322 {
323 if ((p == NULL) || (plen == 0) || (g == NULL) || (glen == 0)) {
324 return NULL;
325 }
326
327 FRAME_DhKey *dhKey = (FRAME_DhKey *)BSL_SAL_Calloc(1u, sizeof(FRAME_DhKey));
328 if (dhKey == NULL) {
329 return NULL;
330 }
331
332 dhKey->p = BSL_SAL_Dump(p, plen);
333 if (dhKey->p == NULL) {
334 BSL_SAL_FREE(dhKey);
335 return NULL;
336 }
337 dhKey->plen = plen;
338
339 dhKey->g = BSL_SAL_Dump(g, glen);
340 if (dhKey->g == NULL) {
341 STUB_CRYPT_FreeDhKeyCallback(dhKey);
342 return NULL;
343 }
344 dhKey->glen = glen;
345
346 dhKey->pubKey = BSL_SAL_Calloc(1u, plen);
347 if (dhKey->pubKey == NULL) {
348 STUB_CRYPT_FreeDhKeyCallback(dhKey);
349 return NULL;
350 }
351 if (memset_s(dhKey->pubKey, plen, 3u, plen) != EOK) {
352 STUB_CRYPT_FreeDhKeyCallback(dhKey);
353 return NULL;
354 }
355 dhKey->pubKeyLen = plen;
356
357 dhKey->privateKey = BSL_SAL_Calloc(1u, plen);
358 if (dhKey->privateKey == NULL) {
359 STUB_CRYPT_FreeDhKeyCallback(dhKey);
360 return NULL;
361 }
362 if (memset_s(dhKey->privateKey, plen, 4u, plen) != EOK) {
363 STUB_CRYPT_FreeDhKeyCallback(dhKey);
364 return NULL;
365 }
366 dhKey->privateKeyLen = plen;
367
368 return dhKey;
369 }
370
STUB_CRYPT_GenerateDhKeyByParamsCallbackLibCtx(void * libCtx,const char * attrName,uint8_t * p,uint16_t plen,uint8_t * g,uint16_t glen)371 HITLS_CRYPT_Key *STUB_CRYPT_GenerateDhKeyByParamsCallbackLibCtx(void *libCtx, const char *attrName,
372 uint8_t *p, uint16_t plen, uint8_t *g, uint16_t glen)
373 {
374 (void)libCtx;
375 (void)attrName;
376 return STUB_CRYPT_GenerateDhKeyByParamsCallback(p, plen, g, glen);
377 }
378
STUB_CRYPT_DHGetParametersCallback(HITLS_CRYPT_Key * key,uint8_t * p,uint16_t * plen,uint8_t * g,uint16_t * glen)379 int32_t STUB_CRYPT_DHGetParametersCallback(HITLS_CRYPT_Key *key, uint8_t *p, uint16_t *plen, uint8_t *g, uint16_t *glen)
380 {
381 if ((key == NULL) || (plen == NULL) || (glen == NULL)) {
382 return HITLS_INTERNAL_EXCEPTION;
383 }
384
385 FRAME_DhKey *dhKey = (FRAME_DhKey *)key;
386
387 if (p != NULL) {
388 if (memcpy_s(p, *plen, dhKey->p, dhKey->plen) != EOK) {
389 return HITLS_MEMCPY_FAIL;
390 }
391 }
392
393 if (g != NULL) {
394 if (memcpy_s(g, *glen, dhKey->g, dhKey->glen) != EOK) {
395 return HITLS_MEMCPY_FAIL;
396 }
397 }
398
399 *plen = dhKey->plen;
400 *glen = dhKey->glen;
401 return HITLS_SUCCESS;
402 }
403
STUB_CRYPT_GetDhEncodedPubKeyCallback(HITLS_CRYPT_Key * key,uint8_t * pubKeyBuf,uint32_t bufLen,uint32_t * pubKeyLen)404 int32_t STUB_CRYPT_GetDhEncodedPubKeyCallback(HITLS_CRYPT_Key *key, uint8_t *pubKeyBuf, uint32_t bufLen,
405 uint32_t *pubKeyLen)
406 {
407 if ((key == NULL) || (pubKeyBuf == NULL) || (pubKeyLen == NULL)) {
408 return HITLS_INTERNAL_EXCEPTION;
409 }
410
411 FRAME_DhKey *dhKey = (FRAME_DhKey *)key;
412
413 if (memcpy_s(pubKeyBuf, bufLen, dhKey->pubKey, dhKey->pubKeyLen) != EOK) {
414 return HITLS_MEMCPY_FAIL;
415 }
416
417 *pubKeyLen = dhKey->pubKeyLen;
418 return HITLS_SUCCESS;
419 }
420
STUB_CRYPT_CalcDhSharedSecretCallback(HITLS_CRYPT_Key * key,uint8_t * peerPubkey,uint32_t pubKeyLen,uint8_t * sharedSecret,uint32_t * sharedSecretLen)421 int32_t STUB_CRYPT_CalcDhSharedSecretCallback(HITLS_CRYPT_Key *key, uint8_t *peerPubkey, uint32_t pubKeyLen,
422 uint8_t *sharedSecret, uint32_t *sharedSecretLen)
423 {
424 FRAME_DhKey *dhKey = (FRAME_DhKey *)key;
425
426 if ((dhKey == NULL) ||
427 (peerPubkey == NULL) ||
428 (sharedSecret == NULL) ||
429 (sharedSecretLen == NULL) ||
430 (dhKey->plen < pubKeyLen)) {
431 return HITLS_INTERNAL_EXCEPTION;
432 }
433
434 if (memset_s(sharedSecret, *sharedSecretLen, 1u, dhKey->plen) != EOK) {
435 return HITLS_MEMCPY_FAIL;
436 }
437 *sharedSecretLen = dhKey->plen;
438 return HITLS_SUCCESS;
439 }
440
441 /**
442 * @ingroup hitls_crypt_reg
443 * @brief Obtain the HMAC length
444 *
445 * @param hashAlgo [IN] Hash algorithm
446 *
447 * @return HMAC length
448 */
STUB_CRYPT_HmacSizeCallback(HITLS_HashAlgo hashAlgo)449 uint32_t STUB_CRYPT_HmacSizeCallback(HITLS_HashAlgo hashAlgo)
450 {
451 switch (hashAlgo) {
452 case HITLS_HASH_MD5:
453 return MD5_DIGEST_LENGTH;
454 case HITLS_HASH_SHA1:
455 return SHA1_DIGEST_LENGTH;
456 case HITLS_HASH_SHA_256:
457 return SHA256_DIGEST_LENGTH;
458 case HITLS_HASH_SHA_384:
459 return SHA384_DIGEST_LENGTH;
460 case HITLS_HASH_SHA_512:
461 return SHA512_DIGEST_LENGTH;
462 default:
463 break;
464 }
465 return 0u;
466 }
467
468 /**
469 * @ingroup hitls_crypt_reg
470 * @brief Initialize the HMAC context
471 *
472 * @param hashAlgo [IN] Hash algorithm
473 * @param key [IN] Key
474 * @param len [IN] Key length
475 *
476 * @return HMAC context
477 */
STUB_CRYPT_HmacInitCallback(HITLS_HashAlgo hashAlgo,const uint8_t * key,uint32_t len)478 HITLS_HMAC_Ctx *STUB_CRYPT_HmacInitCallback(HITLS_HashAlgo hashAlgo, const uint8_t *key, uint32_t len)
479 {
480 FRAME_HmacCtx *ctx = BSL_SAL_Calloc(1u, sizeof(FRAME_HmacCtx));
481 if (ctx == NULL) {
482 return NULL;
483 }
484 ctx->algo = hashAlgo;
485 ctx->key = BSL_SAL_Dump(key, len);
486 if (ctx->key == NULL) {
487 BSL_SAL_FREE(ctx);
488 return NULL;
489 }
490 ctx->keyLen = len;
491 return ctx;
492 }
493
STUB_CRYPT_HmacInitCallbackLibCtx(void * libCtx,const char * attrName,HITLS_HashAlgo hashAlgo,const uint8_t * key,uint32_t len)494 HITLS_HMAC_Ctx *STUB_CRYPT_HmacInitCallbackLibCtx(void *libCtx, const char *attrName,
495 HITLS_HashAlgo hashAlgo, const uint8_t *key, uint32_t len)
496 {
497 (void)libCtx;
498 (void)attrName;
499 return STUB_CRYPT_HmacInitCallback(hashAlgo, key, len);
500 }
501
502 /**
503 * @ingroup hitls_crypt_reg
504 * @brief Release the HMAC context
505 *
506 * @param ctx [IN] HMAC context
507 */
STUB_CRYPT_HmacFreeCallback(HITLS_HMAC_Ctx * ctx)508 void STUB_CRYPT_HmacFreeCallback(HITLS_HMAC_Ctx *ctx)
509 {
510 FRAME_HmacCtx *hmacCtx = (FRAME_HmacCtx *)ctx;
511 if (hmacCtx != NULL) {
512 BSL_SAL_FREE(hmacCtx->key);
513 BSL_SAL_FREE(hmacCtx);
514 }
515 return;
516 }
517
518 /**
519 * @ingroup hitls_crypt_reg
520 * @brief Add the input data
521 *
522 * @param ctx [IN] HMAC context
523 * @param data [IN] Input data
524 * @param len [IN] Data length
525 *
526 * @return 0 indicates success. Other values indicate failure
527 */
STUB_CRYPT_HmacUpdateCallback(HITLS_HMAC_Ctx * ctx,const uint8_t * data,uint32_t len)528 int32_t STUB_CRYPT_HmacUpdateCallback(HITLS_HMAC_Ctx *ctx, const uint8_t *data, uint32_t len)
529 {
530 if ((ctx == NULL) || (data == NULL) || len == 0) {
531 return HITLS_INTERNAL_EXCEPTION;
532 }
533 return HITLS_SUCCESS;
534 }
535
536 /**
537 * @ingroup hitls_crypt_reg
538 * @brief Output the HMAC result
539 *
540 * @param ctx [IN] HMAC context
541 * @param out [OUT] Output data
542 * @param len [IN/OUT] IN: Maximum buffer length OUT: Output data length
543 *
544 * @return 0 indicates success. Other values indicate failure
545 */
STUB_CRYPT_HmacFinalCallback(HITLS_HMAC_Ctx * ctx,uint8_t * out,uint32_t * len)546 int32_t STUB_CRYPT_HmacFinalCallback(HITLS_HMAC_Ctx *ctx, uint8_t *out, uint32_t *len)
547 {
548 FRAME_HmacCtx *hmacCtx = (FRAME_HmacCtx *)ctx;
549 if ((hmacCtx == NULL) || (out == NULL) || (len == NULL)) {
550 return HITLS_INTERNAL_EXCEPTION;
551 }
552
553 uint32_t hmacSize = STUB_CRYPT_HmacSizeCallback(hmacCtx->algo);
554 if ((hmacSize == 0u) || (hmacSize > *len)) {
555 return HITLS_INTERNAL_EXCEPTION;
556 }
557
558 if (memset_s(out, *len, 4u, hmacSize) != EOK) {
559 return HITLS_MEMCPY_FAIL;
560 }
561 *len = hmacSize;
562 return HITLS_SUCCESS;
563 }
564
565 /**
566 * @ingroup hitls_crypt_reg
567 * @brief HMAC function
568 *
569 * @param hashAlgo [IN] Hash algorithm
570 * @param key [IN] Key
571 * @param keyLen [IN] Key length
572 * @param in [IN] Input data
573 * @param inLen [IN] Input data length
574 * @param out [OUT] Output data
575 * @param outLen [IN/OUT] IN: Maximum buffer length OUT: Output data length
576 *
577 * @return 0 indicates success. Other values indicate failure
578 */
STUB_CRYPT_HmacCallback(HITLS_HashAlgo hashAlgo,const uint8_t * key,uint32_t keyLen,const uint8_t * in,uint32_t inLen,uint8_t * out,uint32_t * outLen)579 int32_t STUB_CRYPT_HmacCallback(HITLS_HashAlgo hashAlgo, const uint8_t *key, uint32_t keyLen,
580 const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen)
581 {
582 if ((key == NULL) || (keyLen == 0) || (in == NULL) || (inLen == 0) ||
583 (out == NULL) || (outLen == NULL)) {
584 return HITLS_INTERNAL_EXCEPTION;
585 }
586
587 uint32_t hmacSize = STUB_CRYPT_HmacSizeCallback(hashAlgo);
588 if ((hmacSize == 0u) || (hmacSize > *outLen)) {
589 return HITLS_INTERNAL_EXCEPTION;
590 }
591
592 if (memset_s(out, *outLen, 4u, hmacSize) != EOK) {
593 return HITLS_MEMCPY_FAIL;
594 }
595 *outLen = hmacSize;
596 return HITLS_SUCCESS;
597 }
598
STUB_CRYPT_HmacCallbackLibCtx(void * libCtx,const char * attrName,HITLS_HashAlgo hashAlgo,const uint8_t * key,uint32_t keyLen,const uint8_t * in,uint32_t inLen,uint8_t * out,uint32_t * outLen)599 int32_t STUB_CRYPT_HmacCallbackLibCtx(void *libCtx, const char *attrName,
600 HITLS_HashAlgo hashAlgo, const uint8_t *key, uint32_t keyLen,
601 const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen)
602 {
603 (void)libCtx;
604 (void)attrName;
605 return STUB_CRYPT_HmacCallback(hashAlgo, key, keyLen, in, inLen, out, outLen);
606 }
607
608 /**
609 * @ingroup hitls_crypt_reg
610 * @brief Obtain the hash length
611 *
612 * @param hashAlgo [IN] Hash algorithm
613 *
614 * @return Hash length
615 */
STUB_CRYPT_DigestSizeCallback(HITLS_HashAlgo hashAlgo)616 uint32_t STUB_CRYPT_DigestSizeCallback(HITLS_HashAlgo hashAlgo)
617 {
618 return STUB_CRYPT_HmacSizeCallback(hashAlgo);
619 }
620
621 /**
622 * @ingroup hitls_crypt_reg
623 * @brief Initialize the hash context
624 *
625 * @param hashAlgo [IN] Hash algorithm
626 *
627 * @return hash context
628 */
STUB_CRYPT_DigestInitCallback(HITLS_HashAlgo hashAlgo)629 HITLS_HASH_Ctx *STUB_CRYPT_DigestInitCallback(HITLS_HashAlgo hashAlgo)
630 {
631 FRAME_HashCtx *ctx = BSL_SAL_Calloc(1u, sizeof(FRAME_HashCtx));
632 if (ctx == NULL) {
633 return NULL;
634 }
635 ctx->algo = hashAlgo;
636 return ctx;
637 }
638
STUB_CRYPT_DigestInitCallbackLibCtx(void * libCtx,const char * attrName,HITLS_HashAlgo hashAlgo)639 HITLS_HASH_Ctx *STUB_CRYPT_DigestInitCallbackLibCtx(void *libCtx, const char *attrName, HITLS_HashAlgo hashAlgo)
640 {
641 (void)libCtx;
642 (void)attrName;
643 return STUB_CRYPT_DigestInitCallback(hashAlgo);
644 }
645
646 /**
647 * @ingroup hitls_crypt_reg
648 * @brief Copy hash Context
649 *
650 * @param ctx [IN] hash Context
651 *
652 * @return hash Context
653 */
STUB_CRYPT_DigestCopyCallback(HITLS_HASH_Ctx * ctx)654 HITLS_HASH_Ctx *STUB_CRYPT_DigestCopyCallback(HITLS_HASH_Ctx *ctx)
655 {
656 FRAME_HashCtx *srcCtx = (FRAME_HashCtx *)ctx;
657 if (srcCtx == NULL) {
658 return NULL;
659 }
660
661 FRAME_HashCtx *newCtx = BSL_SAL_Calloc(1u, sizeof(FRAME_HashCtx));
662 if (newCtx == NULL) {
663 return NULL;
664 }
665 newCtx->algo = srcCtx->algo;
666 return newCtx;
667 }
668
669 /**
670 * @ingroup hitls_crypt_reg
671 * @brief Release the hash context
672 *
673 * @param ctx [IN] hash Context
674 */
STUB_CRYPT_DigestFreeCallback(HITLS_HASH_Ctx * ctx)675 void STUB_CRYPT_DigestFreeCallback(HITLS_HASH_Ctx *ctx)
676 {
677 BSL_SAL_FREE(ctx);
678 return;
679 }
680
681 /**
682 * @ingroup hitls_crypt_reg
683 * @brief Add the input data
684 *
685 * @param ctx [IN] hash Context
686 * @param data [IN] Input data
687 * @param len [IN] Input data length
688 *
689 * @return 0 indicates success. Other values indicate failure
690 */
STUB_CRYPT_DigestUpdateCallback(HITLS_HASH_Ctx * ctx,const uint8_t * data,uint32_t len)691 int32_t STUB_CRYPT_DigestUpdateCallback(HITLS_HASH_Ctx *ctx, const uint8_t *data, uint32_t len)
692 {
693 if ((ctx == NULL) || (data == NULL) || (len == 0u)) {
694 return HITLS_INTERNAL_EXCEPTION;
695 }
696 return HITLS_SUCCESS;
697 }
698
699 /**
700 * @ingroup hitls_crypt_reg
701 * @brief Output the hash result
702 *
703 * @param ctx [IN] hash Context
704 * @param out [IN] Output data
705 * @param len [IN/OUT] IN: Maximum buffer length OUT: Output data length
706 *
707 * @return 0 indicates success. Other values indicate failure
708 */
STUB_CRYPT_DigestFinalCallback(HITLS_HASH_Ctx * ctx,uint8_t * out,uint32_t * len)709 int32_t STUB_CRYPT_DigestFinalCallback(HITLS_HASH_Ctx *ctx, uint8_t *out, uint32_t *len)
710 {
711 FRAME_HashCtx *hashCtx = (FRAME_HashCtx *)ctx;
712 if ((hashCtx == NULL) || (out == NULL) || (len == NULL)) {
713 return HITLS_INTERNAL_EXCEPTION;
714 }
715
716 uint32_t digestSize = STUB_CRYPT_DigestSizeCallback(hashCtx->algo);
717 if ((digestSize == 0) || (digestSize > *len)) {
718 return HITLS_INTERNAL_EXCEPTION;
719 }
720
721 if (memset_s(out, *len, 5u, digestSize) != EOK) {
722 return HITLS_MEMCPY_FAIL;
723 }
724 *len = digestSize;
725 return HITLS_SUCCESS;
726 }
727
728 /**
729 * @ingroup hitls_crypt_reg
730 * @brief hash function
731 *
732 * @param hashAlgo [IN] Hash algorithm
733 * @param in [IN] Input data
734 * @param inLen [IN] Input data length
735 * @param out [OUT] Output data
736 * @param outLen [IN/OUT] IN: Maximum buffer length OUT: Output data length
737 *
738 * @return 0 indicates success. Other values indicate failure
739 */
STUB_CRYPT_DigestCallback(HITLS_HashAlgo hashAlgo,const uint8_t * in,uint32_t inLen,uint8_t * out,uint32_t * outLen)740 int32_t STUB_CRYPT_DigestCallback(HITLS_HashAlgo hashAlgo, const uint8_t *in, uint32_t inLen,
741 uint8_t *out, uint32_t *outLen)
742 {
743 if ((in == NULL) ||
744 (out == NULL) ||
745 (outLen == NULL) ||
746 (inLen == 0)) {
747 return HITLS_INTERNAL_EXCEPTION;
748 }
749
750 uint32_t digestSize = STUB_CRYPT_DigestSizeCallback(hashAlgo);
751 if ((digestSize == 0) || (digestSize > *outLen)) {
752 return HITLS_INTERNAL_EXCEPTION;
753 }
754
755 if (memset_s(out, *outLen, 5u, digestSize) != EOK) {
756 return HITLS_MEMCPY_FAIL;
757 }
758 *outLen = digestSize;
759 return HITLS_SUCCESS;
760 }
761
STUB_CRYPT_DigestCallbackLibCtx(void * libCtx,const char * attrName,HITLS_HashAlgo hashAlgo,const uint8_t * in,uint32_t inLen,uint8_t * out,uint32_t * outLen)762 int32_t STUB_CRYPT_DigestCallbackLibCtx(void *libCtx, const char *attrName,
763 HITLS_HashAlgo hashAlgo, const uint8_t *in, uint32_t inLen,
764 uint8_t *out, uint32_t *outLen)
765 {
766 (void)libCtx;
767 (void)attrName;
768 return STUB_CRYPT_DigestCallback(hashAlgo, in, inLen, out, outLen);
769 }
770
771 /**
772 * @ingroup hitls_crypt_reg
773 * @brief Encryption
774 *
775 * @param cipher [IN] Key parameters
776 * @param in [IN] Plaintext data
777 * @param inLen [IN] Plaintext data length
778 * @param out [OUT] Ciphertext data
779 * @param outLen [IN/OUT] IN: maximum buffer length OUT: ciphertext data length
780 *
781 * @return 0 indicates success. Other values indicate failure
782 */
STUB_CRYPT_EncryptCallback(const HITLS_CipherParameters * cipher,const uint8_t * in,uint32_t inLen,uint8_t * out,uint32_t * outLen)783 int32_t STUB_CRYPT_EncryptCallback(const HITLS_CipherParameters *cipher, const uint8_t *in, uint32_t inLen,
784 uint8_t *out, uint32_t *outLen)
785 {
786 if (cipher->type == HITLS_AEAD_CIPHER) {
787 if (*outLen < inLen + AEAD_TAG_LENGTH) {
788 return HITLS_INTERNAL_EXCEPTION;
789 }
790 (void)memset_s(out, *outLen, 0, *outLen);
791 if (inLen != 0 && memcpy_s(out, *outLen, in, inLen) != EOK) {
792 return HITLS_MEMCPY_FAIL;
793 }
794 *outLen = inLen + AEAD_TAG_LENGTH;
795 } else {
796 *outLen = 0;
797 return HITLS_INTERNAL_EXCEPTION;
798 }
799
800 return HITLS_SUCCESS;
801 }
802
STUB_CRYPT_EncryptCallbackLibCtx(void * libCtx,const char * attrName,const HITLS_CipherParameters * cipher,const uint8_t * in,uint32_t inLen,uint8_t * out,uint32_t * outLen)803 int32_t STUB_CRYPT_EncryptCallbackLibCtx(void *libCtx, const char *attrName,
804 const HITLS_CipherParameters *cipher, const uint8_t *in,
805 uint32_t inLen, uint8_t *out, uint32_t *outLen)
806 {
807 (void)libCtx;
808 (void)attrName;
809 return STUB_CRYPT_EncryptCallback(cipher, in, inLen, out, outLen);
810 }
811
812 /**
813 * @ingroup hitls_crypt_reg
814 * @brief Decrypt
815 *
816 * @param cipher [IN] Key parameters
817 * @param in [IN] Ciphertext data
818 * @param inLen [IN] Ciphertext data length
819 * @param out [OUT] Plaintext data
820 * @param outLen [IN/OUT] IN: Maximum buffer length OUT: Plaintext data length
821 *
822 * @return 0 indicates success. Other values indicate failure
823 */
STUB_CRYPT_DecryptCallback(const HITLS_CipherParameters * cipher,const uint8_t * in,uint32_t inLen,uint8_t * out,uint32_t * outLen)824 int32_t STUB_CRYPT_DecryptCallback(const HITLS_CipherParameters *cipher, const uint8_t *in, uint32_t inLen,
825 uint8_t *out, uint32_t *outLen)
826 {
827 if (cipher->type == HITLS_AEAD_CIPHER) {
828 if (inLen < AEAD_TAG_LENGTH) {
829 return HITLS_INTERNAL_EXCEPTION;
830 }
831 (void)memset_s(out, *outLen, 0, *outLen);
832 if (memcpy_s(out, *outLen, in, inLen - AEAD_TAG_LENGTH) != EOK) {
833 return HITLS_MEMCPY_FAIL;
834 }
835 *outLen = inLen - AEAD_TAG_LENGTH;
836 } else {
837 *outLen = 0;
838 return HITLS_INTERNAL_EXCEPTION;
839 }
840
841 return HITLS_SUCCESS;
842 }
843
STUB_CRYPT_DecryptCallbackLibCtx(void * libCtx,const char * attrName,const HITLS_CipherParameters * cipher,const uint8_t * in,uint32_t inLen,uint8_t * out,uint32_t * outLen)844 int32_t STUB_CRYPT_DecryptCallbackLibCtx(void *libCtx, const char *attrName,
845 const HITLS_CipherParameters *cipher, const uint8_t *in,
846 uint32_t inLen, uint8_t *out, uint32_t *outLen)
847 {
848 (void)libCtx;
849 (void)attrName;
850 return STUB_CRYPT_DecryptCallback(cipher, in, inLen, out, outLen);
851 }
852
853 FuncStubInfo g_tmpRpInfo[16] = {0};
FRAME_RegCryptMethod(void)854 void FRAME_RegCryptMethod(void)
855 {
856 #ifndef HITLS_TLS_FEATURE_PROVIDER
857 HITLS_CRYPT_BaseMethod cryptMethod = { 0 };
858 cryptMethod.randBytes = STUB_CRYPT_RandBytesCallback;
859 cryptMethod.hmacSize = STUB_CRYPT_HmacSizeCallback;
860 cryptMethod.hmacInit = STUB_CRYPT_HmacInitCallback;
861 cryptMethod.hmacFree = STUB_CRYPT_HmacFreeCallback;
862 cryptMethod.hmacUpdate = STUB_CRYPT_HmacUpdateCallback;
863 cryptMethod.hmacFinal = STUB_CRYPT_HmacFinalCallback;
864 cryptMethod.hmac = STUB_CRYPT_HmacCallback;
865 cryptMethod.digestSize = STUB_CRYPT_DigestSizeCallback;
866 cryptMethod.digestInit = STUB_CRYPT_DigestInitCallback;
867 cryptMethod.digestCopy = STUB_CRYPT_DigestCopyCallback;
868 cryptMethod.digestFree = CRYPT_DEFAULT_DigestFree;
869 cryptMethod.digestUpdate = STUB_CRYPT_DigestUpdateCallback;
870 cryptMethod.digestFinal = STUB_CRYPT_DigestFinalCallback;
871 cryptMethod.digest = STUB_CRYPT_DigestCallback;
872 cryptMethod.encrypt = STUB_CRYPT_EncryptCallback;
873 cryptMethod.decrypt = STUB_CRYPT_DecryptCallback;
874 cryptMethod.cipherFree = CRYPT_DEFAULT_CipherFree;
875 HITLS_CRYPT_RegisterBaseMethod(&cryptMethod);
876
877 HITLS_CRYPT_EcdhMethod ecdhMethod = { 0 };
878 ecdhMethod.generateEcdhKeyPair = STUB_CRYPT_GenerateEcdhKeyPairCallback;
879 ecdhMethod.freeEcdhKey = CRYPT_DEFAULT_FreeKey;
880 ecdhMethod.getEcdhPubKey = STUB_CRYPT_GetEcdhEncodedPubKeyCallback;
881 ecdhMethod.calcEcdhSharedSecret = STUB_CRYPT_CalcEcdhSharedSecretCallback;
882 HITLS_CRYPT_RegisterEcdhMethod(&ecdhMethod);
883
884 HITLS_CRYPT_DhMethod dhMethod = { 0 };
885 dhMethod.generateDhKeyBySecbits = STUB_CRYPT_GenerateDhKeyBySecbitsCallback;
886 dhMethod.generateDhKeyByParams = STUB_CRYPT_GenerateDhKeyByParamsCallback;
887 dhMethod.freeDhKey = CRYPT_DEFAULT_FreeKey;
888 dhMethod.getDhParameters = STUB_CRYPT_DHGetParametersCallback;
889 dhMethod.getDhPubKey = STUB_CRYPT_GetDhEncodedPubKeyCallback;
890 dhMethod.calcDhSharedSecret = STUB_CRYPT_CalcDhSharedSecretCallback;
891 HITLS_CRYPT_RegisterDhMethod(&dhMethod);
892 #else
893 STUB_Init();
894 STUB_Replace(&g_tmpRpInfo[0], CRYPT_EAL_RandbytesEx, STUB_CRYPT_RandBytesCallbackLibCtx);
895 STUB_Replace(&g_tmpRpInfo[1], HITLS_CRYPT_HMAC_Init, STUB_CRYPT_HmacInitCallbackLibCtx);
896 STUB_Replace(&g_tmpRpInfo[2], HITLS_CRYPT_HMAC, STUB_CRYPT_HmacCallbackLibCtx);
897 STUB_Replace(&g_tmpRpInfo[3], HITLS_CRYPT_DigestInit, STUB_CRYPT_DigestInitCallbackLibCtx);
898 STUB_Replace(&g_tmpRpInfo[4], HITLS_CRYPT_Digest, STUB_CRYPT_DigestCallbackLibCtx);
899 STUB_Replace(&g_tmpRpInfo[5], HITLS_CRYPT_Encrypt, STUB_CRYPT_EncryptCallbackLibCtx);
900 STUB_Replace(&g_tmpRpInfo[6], HITLS_CRYPT_Decrypt, STUB_CRYPT_DecryptCallbackLibCtx);
901 STUB_Replace(&g_tmpRpInfo[7], HITLS_CRYPT_GenerateEcdhKey, STUB_CRYPT_GenerateEcdhKeyPairCallbackLibCtx);
902 STUB_Replace(&g_tmpRpInfo[8], HITLS_CRYPT_EcdhCalcSharedSecret, STUB_CRYPT_CalcEcdhSharedSecretCallbackLibCtx);
903 STUB_Replace(&g_tmpRpInfo[9], HITLS_CRYPT_GenerateDhKeyByParameters, STUB_CRYPT_GenerateDhKeyByParamsCallbackLibCtx);
904 STUB_Replace(&g_tmpRpInfo[10], HITLS_CRYPT_GenerateDhKeyBySecbits, STUB_CRYPT_GenerateDhKeyBySecbitsCallbackLibCtx);
905 #endif
906 return;
907 }
908
FRAME_DeRegCryptMethod(void)909 void FRAME_DeRegCryptMethod(void)
910 {
911 #ifndef HITLS_TLS_FEATURE_PROVIDER
912 HITLS_CRYPT_BaseMethod cryptMethod = { 0 };
913 HITLS_CRYPT_RegisterBaseMethod(&cryptMethod);
914
915 HITLS_CRYPT_EcdhMethod ecdhMethod = { 0 };
916 HITLS_CRYPT_RegisterEcdhMethod(&ecdhMethod);
917
918 HITLS_CRYPT_DhMethod dhMethod = { 0 };
919 HITLS_CRYPT_RegisterDhMethod(&dhMethod);
920 #else
921 STUB_Reset(&g_tmpRpInfo[0]);
922 STUB_Reset(&g_tmpRpInfo[1]);
923 STUB_Reset(&g_tmpRpInfo[2]);
924 STUB_Reset(&g_tmpRpInfo[3]);
925 STUB_Reset(&g_tmpRpInfo[4]);
926 STUB_Reset(&g_tmpRpInfo[5]);
927 STUB_Reset(&g_tmpRpInfo[6]);
928 STUB_Reset(&g_tmpRpInfo[7]);
929 STUB_Reset(&g_tmpRpInfo[8]);
930 STUB_Reset(&g_tmpRpInfo[9]);
931 STUB_Reset(&g_tmpRpInfo[10]);
932 #endif
933 return;
934 }