Skip to content

Commit 1b761c0

Browse files
committed
fix Sha512 tests
enable sha224, sha384 and sh512 as default enable sha224, sha384 and sha512 at tcp server
1 parent 0a53cb3 commit 1b761c0

File tree

9 files changed

+63
-18
lines changed

9 files changed

+63
-18
lines changed

benchmark/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ SRC_C += \
104104
$(WOLFSSL_DIR)/wolfcrypt/src/hash.c \
105105
$(WOLFSSL_DIR)/wolfcrypt/src/hmac.c \
106106
$(WOLFSSL_DIR)/wolfcrypt/src/sha256.c \
107-
$(WOLFSSL_DIR)/wolfCrypt/src/sha512.c \
107+
$(WOLFSSL_DIR)/wolfcrypt/src/sha512.c \
108108
$(WOLFSSL_DIR)/wolfcrypt/src/aes.c \
109109
$(WOLFSSL_DIR)/wolfcrypt/src/ecc.c \
110110
$(WOLFSSL_DIR)/wolfcrypt/src/cmac.c \

benchmark/config/user_settings.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,10 @@ extern "C" {
127127
/** SHA Options */
128128
#define NO_SHA
129129
/* #define NO_SHA256 */
130-
/* #define WOLFSSL_SHA224 */
131-
/* #define WOLFSSL_SHA384 */
132-
/* #define WOLFSSL_SHA512 */
130+
#define WOLFSSL_SHA224
131+
#define WOLFSSL_SHA384
132+
#define WOLFSSL_SHA512
133+
#define WOLFSSL_SHA512_HASHTYPE
133134

134135
/* Dilithium Options */
135136
#define HAVE_DILITHIUM

benchmark/wh_bench_ops.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#include <stdint.h>
2727

2828
/* Maximum number of operations that can be registered */
29-
#define MAX_BENCH_OPS 73
29+
#define MAX_BENCH_OPS 79
3030
/* Maximum length of operation name */
3131
#define MAX_OP_NAME 64
3232

examples/posix/tcp/wh_server_tcp/user_settings.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,10 @@ extern "C" {
116116
/** SHA Options */
117117
#define NO_SHA
118118
/* #define NO_SHA256 */
119-
/* #define WOLFSSL_SHA224 */
120-
/* #define WOLFSSL_SHA384 */
121-
/* #define WOLFSSL_SHA512 */
119+
#define WOLFSSL_SHA224
120+
#define WOLFSSL_SHA384
121+
#define WOLFSSL_SHA512
122+
#define WOLFSSL_SHA512_HASHTYPE
122123

123124
/* Dilithium Options */
124125
#define HAVE_DILITHIUM

src/wh_client_crypto.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3188,7 +3188,7 @@ static int _xferSha512BlockAndUpdateDigest(whClientContext* ctx,
31883188
memcpy(req->resumeState.hash, sha512->digest, WC_SHA512_DIGEST_SIZE);
31893189
req->resumeState.hiLen = sha512->hiLen;
31903190
req->resumeState.loLen = sha512->loLen;
3191-
3191+
req->resumeState.hashType = sha512->hashType;
31923192
uint32_t req_len =
31933193
sizeof(whMessageCrypto_GenericRequestHeader) + sizeof(*req);
31943194

@@ -3221,6 +3221,7 @@ static int _xferSha512BlockAndUpdateDigest(whClientContext* ctx,
32213221
if (ret >= 0) {
32223222
#ifdef DEBUG_CRYPTOCB_VERBOSE
32233223
printf("[client] ERROR Client SHA512 Res recv: ret=%d", ret);
3224+
printf("[client] hashType: %d\n", sha512->hashType);
32243225
#endif /* DEBUG_CRYPTOCB_VERBOSE */
32253226
/* Store the received intermediate hash in the sha512
32263227
* context and indicate the field is now valid and
@@ -3288,7 +3289,9 @@ int wh_Client_Sha512(whClientContext* ctx, wc_Sha512* sha512, const uint8_t* in,
32883289
memcpy(out, sha512->digest, WC_SHA512_DIGEST_SIZE);
32893290
}
32903291

3291-
/* reset the state of the sha context (without blowing away devId) */
3292+
/* reset the state of the sha context (without blowing away devId and
3293+
* hashType)
3294+
*/
32923295
sha512->buffLen = 0;
32933296
sha512->hiLen = 0;
32943297
sha512->loLen = 0;

src/wh_message_crypto.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,7 @@ int wh_MessageCrypto_TranslateSha512Request(
553553
}
554554
WH_T32(magic, dest, src, resumeState.hiLen);
555555
WH_T32(magic, dest, src, resumeState.loLen);
556+
WH_T32(magic, dest, src, resumeState.hashType);
556557
/* Hash value is just a byte array, no translation needed */
557558
if (src != dest) {
558559
memcpy(dest->resumeState.hash, src->resumeState.hash,
@@ -577,6 +578,7 @@ int wh_MessageCrypto_TranslateSha512Response(
577578
}
578579
WH_T32(magic, dest, src, hiLen);
579580
WH_T32(magic, dest, src, loLen);
581+
WH_T32(magic, dest, src, hashType);
580582
/* Hash value is just a byte array, no translation needed */
581583
if (src != dest) {
582584
memcpy(dest->hash, src->hash, sizeof(src->hash));

src/wh_server_crypto.c

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1938,6 +1938,7 @@ static int _HandleSha512(whServerContext* ctx, uint16_t magic,
19381938
wc_Sha512* sha512 = ctx->crypto->algoCtx.sha512;
19391939
whMessageCrypto_Sha512Request req;
19401940
whMessageCrypto_Sha512Response res;
1941+
int hashType = WC_HASH_TYPE_SHA512;
19411942
(void)inSize;
19421943
/* The server SHA512 struct doesn't persist state (it is a union), meaning
19431944
* the devId may get blown away between calls. We must restore the server
@@ -1949,17 +1950,28 @@ static int _HandleSha512(whServerContext* ctx, uint16_t magic,
19491950
if (ret != 0) {
19501951
return ret;
19511952
}
1952-
1953+
hashType = req.resumeState.hashType;
19531954
/* Init the SHA512 context if this is the first time, otherwise restore the
19541955
* hash state from the client */
19551956
if (req.resumeState.hiLen == 0 && req.resumeState.loLen == 0) {
1956-
ret = wc_InitSha512_ex(sha512, NULL, ctx->crypto->devId);
1957+
switch(hashType) {
1958+
case WC_HASH_TYPE_SHA512_224:
1959+
ret = wc_InitSha512_224_ex(sha512, NULL, ctx->crypto->devId);
1960+
break;
1961+
case WC_HASH_TYPE_SHA512_256:
1962+
ret = wc_InitSha512_256_ex(sha512, NULL, ctx->crypto->devId);
1963+
break;
1964+
default:
1965+
ret = wc_InitSha512_ex(sha512, NULL, ctx->crypto->devId);
1966+
break;
1967+
}
19571968
}
19581969
else {
19591970
/* HAVE_DILITHIUM */
19601971
memcpy(sha512->digest, req.resumeState.hash, WC_SHA512_DIGEST_SIZE);
19611972
sha512->loLen = req.resumeState.loLen;
19621973
sha512->hiLen = req.resumeState.hiLen;
1974+
hashType = req.resumeState.hashType;
19631975
}
19641976

19651977
if (req.isLastBlock) {
@@ -1968,7 +1980,17 @@ static int _HandleSha512(whServerContext* ctx, uint16_t magic,
19681980
ret = wc_Sha512Update(sha512, req.inBlock, req.lastBlockLen);
19691981
}
19701982
if (ret == 0) {
1971-
ret = wc_Sha512Final(sha512, res.hash);
1983+
switch(hashType) {
1984+
case WC_HASH_TYPE_SHA512_224:
1985+
ret = wc_Sha512_224Final(sha512, res.hash);
1986+
break;
1987+
case WC_HASH_TYPE_SHA512_256:
1988+
ret = wc_Sha512_256Final(sha512, res.hash);
1989+
break;
1990+
default:
1991+
ret = wc_Sha512Final(sha512, res.hash);
1992+
break;
1993+
}
19721994
}
19731995
}
19741996
else {
@@ -3005,14 +3027,14 @@ static int _HandleSha512Dma(whServerContext* ctx, uint16_t magic, uint16_t seq,
30053027
whMessageCrypto_Sha512DmaResponse res;
30063028
wc_Sha512* sha512 = ctx->crypto->algoCtx.sha512;
30073029
int clientDevId;
3030+
int hashType = WC_HASH_TYPE_SHA512;
30083031

30093032
/* Translate the request */
30103033
ret = wh_MessageCrypto_TranslateSha512DmaRequest(
30113034
magic, (whMessageCrypto_Sha512DmaRequest*)cryptoDataIn, &req);
30123035
if (ret != WH_ERROR_OK) {
30133036
return ret;
30143037
}
3015-
30163038
/* Ensure state sizes are the same */
30173039
if (req.state.sz != sizeof(*sha512)) {
30183040
res.dmaAddrStatus.badAddr = req.state;
@@ -3032,6 +3054,8 @@ static int _HandleSha512Dma(whServerContext* ctx, uint16_t magic, uint16_t seq,
30323054
clientDevId = sha512->devId;
30333055
/* overwrite the devId to that of the server for local crypto */
30343056
sha512->devId = ctx->crypto->devId;
3057+
/* retrieve hash Type to handle 512, 512-224, or 512-256 */
3058+
hashType = sha512->hashType;
30353059
}
30363060
}
30373061

@@ -3051,8 +3075,19 @@ static int _HandleSha512Dma(whServerContext* ctx, uint16_t magic, uint16_t seq,
30513075
if (ret == WH_ERROR_OK) {
30523076
#ifdef DEBUG_CRYPTOCB_VERBOSE
30533077
printf("[server] wc_Sha512Final: outAddr=%p\n", outAddr);
3078+
printf("[server] hashTpe: %d\n", hashType);
30543079
#endif
3055-
ret = wc_Sha512Final(sha512, outAddr);
3080+
switch(hashType) {
3081+
case WC_HASH_TYPE_SHA512_224:
3082+
ret = wc_Sha512_224Final(sha512, outAddr);
3083+
break;
3084+
case WC_HASH_TYPE_SHA512_256:
3085+
ret = wc_Sha512_256Final(sha512, outAddr);
3086+
break;
3087+
default:
3088+
ret = wc_Sha512Final(sha512, outAddr);
3089+
break;
3090+
}
30563091
}
30573092

30583093
if (ret == WH_ERROR_OK) {

test/config/user_settings.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,10 @@ extern "C" {
128128
/** SHA Options */
129129
#define NO_SHA
130130
/* #define NO_SHA256 */
131-
/* #define WOLFSSL_SHA224 */
132-
/* #define WOLFSSL_SHA384 */
133-
/* #define WOLFSSL_SHA512 */
131+
#define WOLFSSL_SHA224
132+
#define WOLFSSL_SHA384
133+
#define WOLFSSL_SHA512
134+
#define WOLFSSL_SHA512_HASHTYPE
134135

135136
/* Dilithium Options */
136137
#define HAVE_DILITHIUM

wolfhsm/wh_message_crypto.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,7 @@ typedef struct {
605605
uint32_t loLen;
606606
/* intermediate hash value */
607607
uint8_t hash[64]; /* TODO (HM) WC_SHA512_DIGEST_SIZE */
608+
int hashType;
608609
} resumeState;
609610
/* Flag indicating to the server that this is the last block and it should
610611
* finalize the hash. If set, inBlock may be only partially full*/
@@ -622,6 +623,7 @@ typedef struct {
622623
uint32_t hiLen;
623624
uint32_t loLen;
624625
uint8_t hash[64]; /* TODO WC_SHA512_DIGEST_SIZE */
626+
int hashType;
625627
} whMessageCrypto_Sha512Response;
626628

627629
int wh_MessageCrypto_TranslateSha512Request(

0 commit comments

Comments
 (0)