Skip to content

Commit 005e08d

Browse files
committed
For crypto cb TLS examples do free if not a copy.
1 parent 1e5f87b commit 005e08d

2 files changed

Lines changed: 28 additions & 10 deletions

File tree

tls/client-tls-cryptocb.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ typedef struct {
5454
/* type: WC_HASH_TYPE_SHA, WC_HASH_TYPE_SHA256, WC_HASH_TYPE_SHA384, etc */
5555
/* in: Update (when not NULL) / Final (when NULL) */
5656
static int cb_hash(int type, const byte* in, word32 inSz, byte* digest,
57-
void* shactx, void** devCtx)
57+
void* shactx, void** devCtx, word32 flags)
5858
{
5959
int ret = 0;
6060
enum wc_HashType hash_type = (enum wc_HashType)type;
@@ -92,6 +92,11 @@ static int cb_hash(int type, const byte* in, word32 inSz, byte* digest,
9292
hashBuf, hashBufSz,
9393
digest, wc_HashGetDigestSize(hash_type),
9494
NULL, INVALID_DEVID);
95+
96+
if (!(flags & WC_HASH_FLAG_ISCOPY)) {
97+
free(ctx);
98+
*devCtx = NULL;
99+
}
95100
}
96101
return ret;
97102
}
@@ -348,7 +353,8 @@ static int myCryptoCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
348353
info->hash.sha1->devId = INVALID_DEVID;
349354

350355
ret = cb_hash(info->hash.type, info->hash.in, info->hash.inSz,
351-
info->hash.digest, info->hash.sha1, &info->hash.sha1->devCtx);
356+
info->hash.digest, info->hash.sha1, &info->hash.sha1->devCtx,
357+
info->hash.sha1->flags);
352358

353359
/* reset devId */
354360
info->hash.sha1->devId = devIdArg;
@@ -364,7 +370,8 @@ static int myCryptoCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
364370
info->hash.sha256->devId = INVALID_DEVID;
365371

366372
ret = cb_hash(info->hash.type, info->hash.in, info->hash.inSz,
367-
info->hash.digest, info->hash.sha256, &info->hash.sha256->devCtx);
373+
info->hash.digest, info->hash.sha256, &info->hash.sha256->devCtx,
374+
info->hash.sha256->flags);
368375

369376
/* reset devId */
370377
info->hash.sha256->devId = devIdArg;
@@ -380,7 +387,8 @@ static int myCryptoCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
380387
info->hash.sha384->devId = INVALID_DEVID;
381388

382389
ret = cb_hash(info->hash.type, info->hash.in, info->hash.inSz,
383-
info->hash.digest, info->hash.sha384, &info->hash.sha384->devCtx);
390+
info->hash.digest, info->hash.sha384, &info->hash.sha384->devCtx,
391+
info->hash.sha384->flags);
384392

385393
/* reset devId */
386394
info->hash.sha384->devId = devIdArg;
@@ -396,7 +404,8 @@ static int myCryptoCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
396404
info->hash.sha512->devId = INVALID_DEVID;
397405

398406
ret = cb_hash(info->hash.type, info->hash.in, info->hash.inSz,
399-
info->hash.digest, info->hash.sha512, &info->hash.sha512->devCtx);
407+
info->hash.digest, info->hash.sha512, &info->hash.sha512->devCtx,
408+
info->hash.sha512->flags);
400409

401410
/* reset devId */
402411
info->hash.sha512->devId = devIdArg;

tls/server-tls-cryptocb.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ typedef struct {
5252
/* type: WC_HASH_TYPE_SHA, WC_HASH_TYPE_SHA256, WC_HASH_TYPE_SHA384, etc */
5353
/* in: Update (when not NULL) / Final (when NULL) */
5454
static int cb_hash(int type, const byte* in, word32 inSz, byte* digest,
55-
void* shactx, void** devCtx)
55+
void* shactx, void** devCtx, word32 flags)
5656
{
5757
int ret = 0;
5858
enum wc_HashType hash_type = (enum wc_HashType)type;
@@ -90,6 +90,11 @@ static int cb_hash(int type, const byte* in, word32 inSz, byte* digest,
9090
hashBuf, hashBufSz,
9191
digest, wc_HashGetDigestSize(hash_type),
9292
NULL, INVALID_DEVID);
93+
94+
if (!(flags & WC_HASH_FLAG_ISCOPY)) {
95+
free(ctx);
96+
*devCtx = NULL;
97+
}
9398
}
9499
return ret;
95100
}
@@ -346,7 +351,8 @@ static int myCryptoCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
346351
info->hash.sha1->devId = INVALID_DEVID;
347352

348353
ret = cb_hash(info->hash.type, info->hash.in, info->hash.inSz,
349-
info->hash.digest, info->hash.sha1, &info->hash.sha1->devCtx);
354+
info->hash.digest, info->hash.sha1, &info->hash.sha1->devCtx,
355+
info->hash.sha1->flags);
350356

351357
/* reset devId */
352358
info->hash.sha1->devId = devIdArg;
@@ -362,7 +368,8 @@ static int myCryptoCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
362368
info->hash.sha256->devId = INVALID_DEVID;
363369

364370
ret = cb_hash(info->hash.type, info->hash.in, info->hash.inSz,
365-
info->hash.digest, info->hash.sha256, &info->hash.sha256->devCtx);
371+
info->hash.digest, info->hash.sha256, &info->hash.sha256->devCtx,
372+
info->hash.sha256->flags);
366373

367374
/* reset devId */
368375
info->hash.sha256->devId = devIdArg;
@@ -378,7 +385,8 @@ static int myCryptoCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
378385
info->hash.sha384->devId = INVALID_DEVID;
379386

380387
ret = cb_hash(info->hash.type, info->hash.in, info->hash.inSz,
381-
info->hash.digest, info->hash.sha384, &info->hash.sha384->devCtx);
388+
info->hash.digest, info->hash.sha384, &info->hash.sha384->devCtx,
389+
info->hash.sha384->flags);
382390

383391
/* reset devId */
384392
info->hash.sha384->devId = devIdArg;
@@ -394,7 +402,8 @@ static int myCryptoCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
394402
info->hash.sha512->devId = INVALID_DEVID;
395403

396404
ret = cb_hash(info->hash.type, info->hash.in, info->hash.inSz,
397-
info->hash.digest, info->hash.sha512, &info->hash.sha512->devCtx);
405+
info->hash.digest, info->hash.sha512, &info->hash.sha512->devCtx,
406+
info->hash.sha512->flags);
398407

399408
/* reset devId */
400409
info->hash.sha512->devId = devIdArg;

0 commit comments

Comments
 (0)