Skip to content

Commit 6f789c3

Browse files
committed
addressed code review comments
1 parent 1b761c0 commit 6f789c3

File tree

7 files changed

+132
-343
lines changed

7 files changed

+132
-343
lines changed

src/wh_client_crypto.c

Lines changed: 39 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2383,7 +2383,7 @@ static int _xferSha256BlockAndUpdateDigest(whClientContext* ctx,
23832383
int ret = 0;
23842384
uint16_t dataSz = 0;
23852385
whMessageCrypto_Sha256Request* req = NULL;
2386-
whMessageCrypto_Sha256Response* res = NULL;
2386+
whMessageCrypto_Sha2Response* res = NULL;
23872387
uint8_t* dataPtr = NULL;
23882388

23892389
/* Get data buffer */
@@ -2517,13 +2517,7 @@ int wh_Client_Sha256(whClientContext* ctx, wc_Sha256* sha256, const uint8_t* in,
25172517
}
25182518

25192519
/* reset the state of the sha context (without blowing away devId) */
2520-
sha256->buffLen = 0;
2521-
sha256->hiLen = 0;
2522-
sha256->loLen = 0;
2523-
#ifdef WOLFSSL_HASH_FLAGS
2524-
sha256->flags = 0;
2525-
#endif
2526-
memset(sha256->digest, 0, sizeof(sha256->digest));
2520+
wc_InitSha256_ex(sha256, NULL, sha256->devId);
25272521
}
25282522

25292523
return ret;
@@ -2639,8 +2633,8 @@ static int _xferSha224BlockAndUpdateDigest(whClientContext* ctx,
26392633
uint16_t action = WH_MESSAGE_ACTION_NONE;
26402634
int ret = 0;
26412635
uint16_t dataSz = 0;
2642-
whMessageCrypto_Sha224Request* req = NULL;
2643-
whMessageCrypto_Sha224Response* res = NULL;
2636+
whMessageCrypto_Sha256Request* req = NULL;
2637+
whMessageCrypto_Sha2Response* res = NULL;
26442638
uint8_t* dataPtr = NULL;
26452639

26462640
/* Get data buffer */
@@ -2650,7 +2644,7 @@ static int _xferSha224BlockAndUpdateDigest(whClientContext* ctx,
26502644
}
26512645

26522646
/* Setup generic header and get pointer to request data */
2653-
req = (whMessageCrypto_Sha224Request*)_createCryptoRequest(
2647+
req = (whMessageCrypto_Sha256Request*)_createCryptoRequest(
26542648
dataPtr, WC_HASH_TYPE_SHA224);
26552649

26562650

@@ -2670,7 +2664,7 @@ static int _xferSha224BlockAndUpdateDigest(whClientContext* ctx,
26702664

26712665
/* Send the hash state - this will be 0 on the first block on a properly
26722666
* initialized sha224 struct */
2673-
memcpy(req->resumeState.hash, sha224->digest, WC_SHA224_DIGEST_SIZE);
2667+
memcpy(req->resumeState.hash, sha224->digest, WC_SHA256_DIGEST_SIZE);
26742668
req->resumeState.hiLen = sha224->hiLen;
26752669
req->resumeState.loLen = sha224->loLen;
26762670

@@ -2709,8 +2703,12 @@ static int _xferSha224BlockAndUpdateDigest(whClientContext* ctx,
27092703
#endif /* DEBUG_CRYPTOCB_VERBOSE */
27102704
/* Store the received intermediate hash in the sha224
27112705
* context and indicate the field is now valid and
2712-
* should be passed back and forth to the server */
2713-
memcpy(sha224->digest, res->hash, WC_SHA224_DIGEST_SIZE);
2706+
* should be passed back and forth to the server.
2707+
* The digest length is the same as sha256
2708+
* for intermediate operation. Final output will be
2709+
* truncated to WC_SHA224_DIGEST_SIZE.
2710+
*/
2711+
memcpy(sha224->digest, res->hash, WC_SHA256_DIGEST_SIZE);
27142712
sha224->hiLen = res->hiLen;
27152713
sha224->loLen = res->loLen;
27162714
#ifdef DEBUG_CRYPTOCB_VERBOSE
@@ -2774,13 +2772,7 @@ int wh_Client_Sha224(whClientContext* ctx, wc_Sha224* sha224, const uint8_t* in,
27742772
}
27752773

27762774
/* reset the state of the sha context (without blowing away devId) */
2777-
sha224->buffLen = 0;
2778-
sha224->hiLen = 0;
2779-
sha224->loLen = 0;
2780-
#ifdef WOLFSSL_HASH_FLAGS
2781-
sha224->flags = 0;
2782-
#endif
2783-
memset(sha224->digest, 0, sizeof(sha224->digest));
2775+
wc_InitSha224_ex(sha224, NULL, sha224->devId);
27842776
}
27852777

27862778
return ret;
@@ -2897,8 +2889,8 @@ static int _xferSha384BlockAndUpdateDigest(whClientContext* ctx,
28972889
uint16_t action = WH_MESSAGE_ACTION_NONE;
28982890
int ret = 0;
28992891
uint16_t dataSz = 0;
2900-
whMessageCrypto_Sha384Request* req = NULL;
2901-
whMessageCrypto_Sha384Response* res = NULL;
2892+
whMessageCrypto_Sha512Request* req = NULL;
2893+
whMessageCrypto_Sha2Response* res = NULL;
29022894
uint8_t* dataPtr = NULL;
29032895

29042896
/* Get data buffer */
@@ -2908,7 +2900,7 @@ static int _xferSha384BlockAndUpdateDigest(whClientContext* ctx,
29082900
}
29092901

29102902
/* Setup generic header and get pointer to request data */
2911-
req = (whMessageCrypto_Sha384Request*)_createCryptoRequest(
2903+
req = (whMessageCrypto_Sha512Request*)_createCryptoRequest(
29122904
dataPtr, WC_HASH_TYPE_SHA384);
29132905

29142906

@@ -2928,7 +2920,7 @@ static int _xferSha384BlockAndUpdateDigest(whClientContext* ctx,
29282920

29292921
/* Send the hash state - this will be 0 on the first block on a properly
29302922
* initialized sha384 struct */
2931-
memcpy(req->resumeState.hash, sha384->digest, WC_SHA384_DIGEST_SIZE);
2923+
memcpy(req->resumeState.hash, sha384->digest, WC_SHA512_DIGEST_SIZE);
29322924
req->resumeState.hiLen = sha384->hiLen;
29332925
req->resumeState.loLen = sha384->loLen;
29342926

@@ -2967,8 +2959,12 @@ static int _xferSha384BlockAndUpdateDigest(whClientContext* ctx,
29672959
#endif /* DEBUG_CRYPTOCB_VERBOSE */
29682960
/* Store the received intermediate hash in the sha384
29692961
* context and indicate the field is now valid and
2970-
* should be passed back and forth to the server */
2971-
memcpy(sha384->digest, res->hash, WC_SHA384_DIGEST_SIZE);
2962+
* should be passed back and forth to the server
2963+
* The digest length is the same as sha512
2964+
* for intermediate operation. Final output will be
2965+
* truncated to WC_SHA384_DIGEST_SIZE.
2966+
*/
2967+
memcpy(sha384->digest, res->hash, WC_SHA512_DIGEST_SIZE);
29722968
sha384->hiLen = res->hiLen;
29732969
sha384->loLen = res->loLen;
29742970
#ifdef DEBUG_CRYPTOCB_VERBOSE
@@ -3032,13 +3028,7 @@ int wh_Client_Sha384(whClientContext* ctx, wc_Sha384* sha384, const uint8_t* in,
30323028
}
30333029

30343030
/* reset the state of the sha context (without blowing away devId) */
3035-
sha384->buffLen = 0;
3036-
sha384->hiLen = 0;
3037-
sha384->loLen = 0;
3038-
#ifdef WOLFSSL_HASH_FLAGS
3039-
sha384->flags = 0;
3040-
#endif
3041-
memset(sha384->digest, 0, sizeof(sha384->digest));
3031+
wc_InitSha384_ex(sha384, NULL, sha384->devId);
30423032
}
30433033

30443034
return ret;
@@ -3155,7 +3145,7 @@ static int _xferSha512BlockAndUpdateDigest(whClientContext* ctx,
31553145
int ret = 0;
31563146
uint16_t dataSz = 0;
31573147
whMessageCrypto_Sha512Request* req = NULL;
3158-
whMessageCrypto_Sha512Response* res = NULL;
3148+
whMessageCrypto_Sha2Response* res = NULL;
31593149
uint8_t* dataPtr = NULL;
31603150

31613151
/* Get data buffer */
@@ -3245,6 +3235,7 @@ int wh_Client_Sha512(whClientContext* ctx, wc_Sha512* sha512, const uint8_t* in,
32453235
{
32463236
int ret = 0;
32473237
uint8_t* sha512BufferBytes = (uint8_t*)sha512->buffer;
3238+
int hashType = WC_HASH_TYPE_SHA512;
32483239

32493240
/* Caller invoked SHA Update:
32503241
* wc_CryptoCb_Sha512Hash(sha512, data, len, NULL) */
@@ -3288,17 +3279,22 @@ int wh_Client_Sha512(whClientContext* ctx, wc_Sha512* sha512, const uint8_t* in,
32883279
if (ret == 0) {
32893280
memcpy(out, sha512->digest, WC_SHA512_DIGEST_SIZE);
32903281
}
3291-
3282+
/* keep hashtype before initialization */
3283+
hashType = sha512->hashType;
32923284
/* reset the state of the sha context (without blowing away devId and
32933285
* hashType)
32943286
*/
3295-
sha512->buffLen = 0;
3296-
sha512->hiLen = 0;
3297-
sha512->loLen = 0;
3298-
#ifdef WOLFSSL_HASH_FLAGS
3299-
sha512->flags = 0;
3300-
#endif
3301-
memset(sha512->digest, 0, sizeof(sha512->digest));
3287+
switch(hashType) {
3288+
case WC_HASH_TYPE_SHA512_224:
3289+
ret = wc_InitSha512_224_ex(sha512, NULL, sha512->devId);
3290+
break;
3291+
case WC_HASH_TYPE_SHA512_256:
3292+
ret = wc_InitSha512_256_ex(sha512, NULL, sha512->devId);
3293+
break;
3294+
default:
3295+
ret = wc_InitSha512_ex(sha512, NULL, sha512->devId);
3296+
break;
3297+
}
33023298
}
33033299

33043300
return ret;

src/wh_message_crypto.c

Lines changed: 7 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -440,109 +440,7 @@ int wh_MessageCrypto_TranslateSha256Request(
440440
return 0;
441441
}
442442

443-
/* SHA256 Response translation */
444-
int wh_MessageCrypto_TranslateSha256Response(
445-
uint16_t magic, const whMessageCrypto_Sha256Response* src,
446-
whMessageCrypto_Sha256Response* dest)
447-
{
448-
if ((src == NULL) || (dest == NULL)) {
449-
return WH_ERROR_BADARGS;
450-
}
451-
WH_T32(magic, dest, src, hiLen);
452-
WH_T32(magic, dest, src, loLen);
453-
/* Hash value is just a byte array, no translation needed */
454-
if (src != dest) {
455-
memcpy(dest->hash, src->hash, sizeof(src->hash));
456-
}
457-
return 0;
458-
}
459-
460-
#if defined(WOLFSSL_SHA224)
461-
/* SHA224 Request translation */
462-
int wh_MessageCrypto_TranslateSha224Request(
463-
uint16_t magic, const whMessageCrypto_Sha224Request* src,
464-
whMessageCrypto_Sha224Request* dest)
465-
{
466-
if ((src == NULL) || (dest == NULL)) {
467-
return WH_ERROR_BADARGS;
468-
}
469-
WH_T32(magic, dest, src, resumeState.hiLen);
470-
WH_T32(magic, dest, src, resumeState.loLen);
471-
/* Hash value is just a byte array, no translation needed */
472-
if (src != dest) {
473-
memcpy(dest->resumeState.hash, src->resumeState.hash,
474-
sizeof(src->resumeState.hash));
475-
}
476-
WH_T32(magic, dest, src, isLastBlock);
477-
WH_T32(magic, dest, src, lastBlockLen);
478-
/* Input block is just a byte array, no translation needed */
479-
if (src != dest) {
480-
memcpy(dest->inBlock, src->inBlock, sizeof(src->inBlock));
481-
}
482-
return 0;
483-
}
484-
485-
/* SHA224 Response translation */
486-
int wh_MessageCrypto_TranslateSha224Response(
487-
uint16_t magic, const whMessageCrypto_Sha224Response* src,
488-
whMessageCrypto_Sha224Response* dest)
489-
{
490-
if ((src == NULL) || (dest == NULL)) {
491-
return WH_ERROR_BADARGS;
492-
}
493-
WH_T32(magic, dest, src, hiLen);
494-
WH_T32(magic, dest, src, loLen);
495-
/* Hash value is just a byte array, no translation needed */
496-
if (src != dest) {
497-
memcpy(dest->hash, src->hash, sizeof(src->hash));
498-
}
499-
return 0;
500-
}
501-
#endif /* WOLFSSL_SHA224 */
502-
503-
#if defined(WOLFSSL_SHA384)
504-
/* SHA384 Request translation */
505-
int wh_MessageCrypto_TranslateSha384Request(
506-
uint16_t magic, const whMessageCrypto_Sha384Request* src,
507-
whMessageCrypto_Sha384Request* dest)
508-
{
509-
if ((src == NULL) || (dest == NULL)) {
510-
return WH_ERROR_BADARGS;
511-
}
512-
WH_T32(magic, dest, src, resumeState.hiLen);
513-
WH_T32(magic, dest, src, resumeState.loLen);
514-
/* Hash value is just a byte array, no translation needed */
515-
if (src != dest) {
516-
memcpy(dest->resumeState.hash, src->resumeState.hash,
517-
sizeof(src->resumeState.hash));
518-
}
519-
WH_T32(magic, dest, src, isLastBlock);
520-
WH_T32(magic, dest, src, lastBlockLen);
521-
/* Input block is just a byte array, no translation needed */
522-
if (src != dest) {
523-
memcpy(dest->inBlock, src->inBlock, sizeof(src->inBlock));
524-
}
525-
return 0;
526-
}
527-
528-
/* SHA384 Response translation */
529-
int wh_MessageCrypto_TranslateSha384Response(
530-
uint16_t magic, const whMessageCrypto_Sha384Response* src,
531-
whMessageCrypto_Sha384Response* dest)
532-
{
533-
if ((src == NULL) || (dest == NULL)) {
534-
return WH_ERROR_BADARGS;
535-
}
536-
WH_T32(magic, dest, src, hiLen);
537-
WH_T32(magic, dest, src, loLen);
538-
/* Hash value is just a byte array, no translation needed */
539-
if (src != dest) {
540-
memcpy(dest->hash, src->hash, sizeof(src->hash));
541-
}
542-
return 0;
543-
}
544-
#endif /* WOLFSSL_SHA384 */
545-
#if defined(WOLFSSL_SHA512)
443+
#if defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA384)
546444
/* SHA512 Request translation */
547445
int wh_MessageCrypto_TranslateSha512Request(
548446
uint16_t magic, const whMessageCrypto_Sha512Request* src,
@@ -567,11 +465,12 @@ int wh_MessageCrypto_TranslateSha512Request(
567465
}
568466
return 0;
569467
}
468+
#endif /* WOLFSSL_SHA512 || WOLFSSL_SHA384 */
570469

571-
/* SHA512 Response translation */
572-
int wh_MessageCrypto_TranslateSha512Response(
573-
uint16_t magic, const whMessageCrypto_Sha512Response* src,
574-
whMessageCrypto_Sha512Response* dest)
470+
/* SHA2 Response translation */
471+
int wh_MessageCrypto_TranslateSha2Response(
472+
uint16_t magic, const whMessageCrypto_Sha2Response* src,
473+
whMessageCrypto_Sha2Response* dest)
575474
{
576475
if ((src == NULL) || (dest == NULL)) {
577476
return WH_ERROR_BADARGS;
@@ -585,7 +484,7 @@ int wh_MessageCrypto_TranslateSha512Response(
585484
}
586485
return 0;
587486
}
588-
#endif /* WOLFSSL_SHA512 */
487+
589488

590489
/* CMAC Request translation */
591490
int wh_MessageCrypto_TranslateCmacRequest(

0 commit comments

Comments
 (0)