Skip to content

Commit 1e5f87b

Browse files
committed
Better example for crypto callback hashing.
1 parent 124998f commit 1e5f87b

2 files changed

Lines changed: 180 additions & 48 deletions

File tree

tls/client-tls-cryptocb.c

Lines changed: 87 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,54 @@ typedef struct {
4747
int exampleVar; /* example, not used */
4848
} myCryptoCbCtx;
4949

50+
typedef struct {
51+
word32 bufSz;
52+
} hash_ctx_t;
53+
54+
/* type: WC_HASH_TYPE_SHA, WC_HASH_TYPE_SHA256, WC_HASH_TYPE_SHA384, etc */
55+
/* in: Update (when not NULL) / Final (when NULL) */
56+
static int cb_hash(int type, const byte* in, word32 inSz, byte* digest,
57+
void* shactx, void** devCtx)
58+
{
59+
int ret = 0;
60+
enum wc_HashType hash_type = (enum wc_HashType)type;
61+
hash_ctx_t* ctx = (hash_ctx_t*)*devCtx;
62+
byte* hashBuf = NULL;
63+
word32 hashBufSz = 0;
64+
65+
/* for updates alloc/realloc and copy */
66+
if (in != NULL) {
67+
if (ctx == NULL) {
68+
ctx = (hash_ctx_t*)malloc(sizeof(hash_ctx_t) + hashBufSz + inSz);
69+
}
70+
else {
71+
hashBufSz = ctx->bufSz;
72+
ctx = (hash_ctx_t*)realloc(ctx, sizeof(hash_ctx_t) + hashBufSz + inSz);
73+
}
74+
if (ctx == NULL) {
75+
return MEMORY_E;
76+
}
77+
hashBuf = (byte*)ctx + sizeof(hash_ctx_t);
78+
memcpy(&hashBuf[hashBufSz], in, inSz);
79+
ctx->bufSz = hashBufSz + inSz;
80+
*devCtx = ctx;
81+
}
82+
/* final */
83+
else if (digest != NULL) {
84+
if (ctx == NULL) {
85+
/* valid case of empty hash (0 len hash) */
86+
}
87+
else {
88+
hashBuf = (byte*)ctx + sizeof(hash_ctx_t);
89+
hashBufSz = ctx->bufSz;
90+
}
91+
ret = wc_Hash_ex(hash_type,
92+
hashBuf, hashBufSz,
93+
digest, wc_HashGetDigestSize(hash_type),
94+
NULL, INVALID_DEVID);
95+
}
96+
return ret;
97+
}
5098

5199
/* Example crypto dev callback function that calls software version */
52100
/* This is where you would plug-in calls to your own hardware crypto */
@@ -289,7 +337,8 @@ static int myCryptoCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
289337
#endif /* !NO_AES || !NO_DES3 */
290338
}
291339
else if (info->algo_type == WC_ALGO_TYPE_HASH) {
292-
#if !defined(NO_SHA) || !defined(NO_SHA256)
340+
#if !defined(NO_SHA) || !defined(NO_SHA256) || defined(WOLFSSL_SHA384) || \
341+
defined(WOLFSSL_SHA512)
293342
#if !defined(NO_SHA)
294343
if (info->hash.type == WC_HASH_TYPE_SHA) {
295344
if (info->hash.sha1 == NULL)
@@ -298,17 +347,8 @@ static int myCryptoCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
298347
/* set devId to invalid, so software is used */
299348
info->hash.sha1->devId = INVALID_DEVID;
300349

301-
if (info->hash.in != NULL) {
302-
ret = wc_ShaUpdate(
303-
info->hash.sha1,
304-
info->hash.in,
305-
info->hash.inSz);
306-
}
307-
if (info->hash.digest != NULL) {
308-
ret = wc_ShaFinal(
309-
info->hash.sha1,
310-
info->hash.digest);
311-
}
350+
ret = cb_hash(info->hash.type, info->hash.in, info->hash.inSz,
351+
info->hash.digest, info->hash.sha1, &info->hash.sha1->devCtx);
312352

313353
/* reset devId */
314354
info->hash.sha1->devId = devIdArg;
@@ -323,26 +363,49 @@ static int myCryptoCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
323363
/* set devId to invalid, so software is used */
324364
info->hash.sha256->devId = INVALID_DEVID;
325365

326-
if (info->hash.in != NULL) {
327-
ret = wc_Sha256Update(
328-
info->hash.sha256,
329-
info->hash.in,
330-
info->hash.inSz);
331-
}
332-
if (info->hash.digest != NULL) {
333-
ret = wc_Sha256Final(
334-
info->hash.sha256,
335-
info->hash.digest);
336-
}
366+
ret = cb_hash(info->hash.type, info->hash.in, info->hash.inSz,
367+
info->hash.digest, info->hash.sha256, &info->hash.sha256->devCtx);
337368

338369
/* reset devId */
339370
info->hash.sha256->devId = devIdArg;
340371
}
341372
else
373+
#endif
374+
#ifdef WOLFSSL_SHA384
375+
if (info->hash.type == WC_HASH_TYPE_SHA384) {
376+
if (info->hash.sha384 == NULL)
377+
return CRYPTOCB_UNAVAILABLE;
378+
379+
/* set devId to invalid, so software is used */
380+
info->hash.sha384->devId = INVALID_DEVID;
381+
382+
ret = cb_hash(info->hash.type, info->hash.in, info->hash.inSz,
383+
info->hash.digest, info->hash.sha384, &info->hash.sha384->devCtx);
384+
385+
/* reset devId */
386+
info->hash.sha384->devId = devIdArg;
387+
}
388+
else
389+
#endif
390+
#ifdef WOLFSSL_SHA512
391+
if (info->hash.type == WC_HASH_TYPE_SHA512) {
392+
if (info->hash.sha512 == NULL)
393+
return CRYPTOCB_UNAVAILABLE;
394+
395+
/* set devId to invalid, so software is used */
396+
info->hash.sha512->devId = INVALID_DEVID;
397+
398+
ret = cb_hash(info->hash.type, info->hash.in, info->hash.inSz,
399+
info->hash.digest, info->hash.sha512, &info->hash.sha512->devCtx);
400+
401+
/* reset devId */
402+
info->hash.sha512->devId = devIdArg;
403+
}
404+
else
342405
#endif
343406
{
344407
}
345-
#endif /* !NO_SHA || !NO_SHA256 */
408+
#endif /* !NO_SHA || !NO_SHA256 || WOLFSSL_SHA384 || WOLFSSL_SHA512 */
346409
}
347410
else if (info->algo_type == WC_ALGO_TYPE_HMAC) {
348411
#ifndef NO_HMAC

tls/server-tls-cryptocb.c

Lines changed: 93 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -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 */
5099
static 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

Comments
 (0)