@@ -45,6 +45,55 @@ typedef struct {
4545 int exampleVar ; /* example, not used */
4646} myCryptoCbCtx ;
4747
48+ typedef struct {
49+ word32 bufSz ;
50+ } hash_ctx_t ;
51+
52+ /* type: WC_HASH_TYPE_SHA, WC_HASH_TYPE_SHA256, WC_HASH_TYPE_SHA384, etc */
53+ /* in: Update (when not NULL) / Final (when NULL) */
54+ static int cb_hash (int type , const byte * in , word32 inSz , byte * digest ,
55+ void * shactx , void * * devCtx )
56+ {
57+ int ret = 0 ;
58+ enum wc_HashType hash_type = (enum wc_HashType )type ;
59+ hash_ctx_t * ctx = (hash_ctx_t * )* devCtx ;
60+ byte * hashBuf = NULL ;
61+ word32 hashBufSz = 0 ;
62+
63+ /* for updates alloc/realloc and copy */
64+ if (in != NULL ) {
65+ if (ctx == NULL ) {
66+ ctx = (hash_ctx_t * )malloc (sizeof (hash_ctx_t ) + hashBufSz + inSz );
67+ }
68+ else {
69+ hashBufSz = ctx -> bufSz ;
70+ ctx = (hash_ctx_t * )realloc (ctx , sizeof (hash_ctx_t ) + hashBufSz + inSz );
71+ }
72+ if (ctx == NULL ) {
73+ return MEMORY_E ;
74+ }
75+ hashBuf = (byte * )ctx + sizeof (hash_ctx_t );
76+ memcpy (& hashBuf [hashBufSz ], in , inSz );
77+ ctx -> bufSz = hashBufSz + inSz ;
78+ * devCtx = ctx ;
79+ }
80+ /* final */
81+ else if (digest != NULL ) {
82+ if (ctx == NULL ) {
83+ /* valid case of empty hash (0 len hash) */
84+ }
85+ else {
86+ hashBuf = (byte * )ctx + sizeof (hash_ctx_t );
87+ hashBufSz = ctx -> bufSz ;
88+ }
89+ ret = wc_Hash_ex (hash_type ,
90+ hashBuf , hashBufSz ,
91+ digest , wc_HashGetDigestSize (hash_type ),
92+ NULL , INVALID_DEVID );
93+ }
94+ return ret ;
95+ }
96+
4897/* Example crypto dev callback function that calls software version */
4998/* This is where you would plug-in calls to your own hardware crypto */
5099static int myCryptoCb (int devIdArg , wc_CryptoInfo * info , void * ctx )
@@ -286,7 +335,8 @@ static int myCryptoCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
286335#endif /* !NO_AES || !NO_DES3 */
287336 }
288337 else if (info -> algo_type == WC_ALGO_TYPE_HASH ) {
289- #if !defined(NO_SHA ) || !defined(NO_SHA256 )
338+ #if !defined(NO_SHA ) || !defined(NO_SHA256 ) || defined(WOLFSSL_SHA384 ) || \
339+ defined(WOLFSSL_SHA512 )
290340 #if !defined(NO_SHA )
291341 if (info -> hash .type == WC_HASH_TYPE_SHA ) {
292342 if (info -> hash .sha1 == NULL )
@@ -295,17 +345,8 @@ static int myCryptoCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
295345 /* set devId to invalid, so software is used */
296346 info -> hash .sha1 -> devId = INVALID_DEVID ;
297347
298- if (info -> hash .in != NULL ) {
299- ret = wc_ShaUpdate (
300- info -> hash .sha1 ,
301- info -> hash .in ,
302- info -> hash .inSz );
303- }
304- if (info -> hash .digest != NULL ) {
305- ret = wc_ShaFinal (
306- info -> hash .sha1 ,
307- info -> hash .digest );
308- }
348+ ret = cb_hash (info -> hash .type , info -> hash .in , info -> hash .inSz ,
349+ info -> hash .digest , info -> hash .sha1 , & info -> hash .sha1 -> devCtx );
309350
310351 /* reset devId */
311352 info -> hash .sha1 -> devId = devIdArg ;
@@ -320,26 +361,49 @@ static int myCryptoCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
320361 /* set devId to invalid, so software is used */
321362 info -> hash .sha256 -> devId = INVALID_DEVID ;
322363
323- if (info -> hash .in != NULL ) {
324- ret = wc_Sha256Update (
325- info -> hash .sha256 ,
326- info -> hash .in ,
327- info -> hash .inSz );
328- }
329- if (info -> hash .digest != NULL ) {
330- ret = wc_Sha256Final (
331- info -> hash .sha256 ,
332- info -> hash .digest );
333- }
364+ ret = cb_hash (info -> hash .type , info -> hash .in , info -> hash .inSz ,
365+ info -> hash .digest , info -> hash .sha256 , & info -> hash .sha256 -> devCtx );
334366
335367 /* reset devId */
336368 info -> hash .sha256 -> devId = devIdArg ;
337369 }
338370 else
371+ #endif
372+ #ifdef WOLFSSL_SHA384
373+ if (info -> hash .type == WC_HASH_TYPE_SHA384 ) {
374+ if (info -> hash .sha384 == NULL )
375+ return CRYPTOCB_UNAVAILABLE ;
376+
377+ /* set devId to invalid, so software is used */
378+ info -> hash .sha384 -> devId = INVALID_DEVID ;
379+
380+ ret = cb_hash (info -> hash .type , info -> hash .in , info -> hash .inSz ,
381+ info -> hash .digest , info -> hash .sha384 , & info -> hash .sha384 -> devCtx );
382+
383+ /* reset devId */
384+ info -> hash .sha384 -> devId = devIdArg ;
385+ }
386+ else
387+ #endif
388+ #ifdef WOLFSSL_SHA512
389+ if (info -> hash .type == WC_HASH_TYPE_SHA512 ) {
390+ if (info -> hash .sha512 == NULL )
391+ return CRYPTOCB_UNAVAILABLE ;
392+
393+ /* set devId to invalid, so software is used */
394+ info -> hash .sha512 -> devId = INVALID_DEVID ;
395+
396+ ret = cb_hash (info -> hash .type , info -> hash .in , info -> hash .inSz ,
397+ info -> hash .digest , info -> hash .sha512 , & info -> hash .sha512 -> devCtx );
398+
399+ /* reset devId */
400+ info -> hash .sha512 -> devId = devIdArg ;
401+ }
402+ else
339403 #endif
340404 {
341405 }
342- #endif /* !NO_SHA || !NO_SHA256 */
406+ #endif /* !NO_SHA || !NO_SHA256 || WOLFSSL_SHA384 || WOLFSSL_SHA512 */
343407 }
344408 else if (info -> algo_type == WC_ALGO_TYPE_HMAC ) {
345409#ifndef NO_HMAC
@@ -387,6 +451,7 @@ int main(int argc, char** argv)
387451 size_t len ;
388452 int shutdown = 0 ;
389453 const char * reply = "I hear ya fa shizzle!\n" ;
454+ int on ;
390455
391456 /* declare wolfSSL objects */
392457 WOLFSSL_CTX * ctx = NULL ;
@@ -455,6 +520,10 @@ int main(int argc, char** argv)
455520 goto exit ;
456521 }
457522
523+ on = 1 ;
524+ setsockopt (sockfd , SOL_SOCKET , SO_REUSEADDR ,
525+ (char * )& on , (socklen_t )sizeof (on ));
526+
458527
459528 /* Initialize the server address struct with zeros */
460529 memset (& servAddr , 0 , sizeof (servAddr ));
0 commit comments