@@ -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 ) {
0 commit comments