Skip to content

Commit d505779

Browse files
committed
Improved TLS cryptocb test cases.
1 parent ed2549c commit d505779

3 files changed

Lines changed: 85 additions & 23 deletions

File tree

tls/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ CFLAGS+=$(OPTIMIZE)
1919
#LIBS+=$(STATIC_LIB)
2020
LIBS+=$(DYN_LIB)
2121

22+
# Openssl Option
23+
#CFLAGS+=-DUSE_OPENSSL
24+
#LIBS+=-lcrypto
25+
2226
# build targets
2327
SRC=$(wildcard *.c)
2428
TARGETS=$(patsubst %.c, %, $(SRC))

tls/client-tls-cryptocb.c

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ typedef struct {
5151
word32 bufSz;
5252
} hash_ctx_t;
5353

54+
#ifdef USE_OPENSSL
55+
#include <openssl/sha.h>
56+
#endif
57+
5458
/* type: WC_HASH_TYPE_SHA, WC_HASH_TYPE_SHA256, WC_HASH_TYPE_SHA384, etc */
5559
/* in: Update (when not NULL) / Final (when NULL) */
5660
static int cb_hash(int type, const byte* in, word32 inSz, byte* digest,
@@ -88,11 +92,34 @@ static int cb_hash(int type, const byte* in, word32 inSz, byte* digest,
8892
hashBuf = (byte*)ctx + sizeof(hash_ctx_t);
8993
hashBufSz = ctx->bufSz;
9094
}
95+
96+
#ifdef USE_OPENSSL
97+
switch (hash_type) {
98+
case WC_HASH_TYPE_SHA:
99+
SHA1(hashBuf, hashBufSz, digest);
100+
break;
101+
case WC_HASH_TYPE_SHA224:
102+
SHA224(hashBuf, hashBufSz, digest);
103+
break;
104+
case WC_HASH_TYPE_SHA256:
105+
SHA256(hashBuf, hashBufSz, digest);
106+
break;
107+
case WC_HASH_TYPE_SHA384:
108+
SHA384(hashBuf, hashBufSz, digest);
109+
break;
110+
case WC_HASH_TYPE_SHA512:
111+
SHA512(hashBuf, hashBufSz, digest);
112+
break;
113+
default:
114+
ret = NOT_COMPILED_IN;
115+
break;
116+
}
117+
#else
91118
ret = wc_Hash_ex(hash_type,
92119
hashBuf, hashBufSz,
93120
digest, wc_HashGetDigestSize(hash_type),
94121
NULL, INVALID_DEVID);
95-
122+
#endif
96123
if (!(flags & WC_HASH_FLAG_ISCOPY)) {
97124
free(ctx);
98125
*devCtx = NULL;
@@ -518,27 +545,30 @@ int main(int argc, char** argv)
518545
goto exit;
519546
}
520547

548+
#if 1
549+
/* register a devID for crypto callbacks */
550+
ret = wc_CryptoCb_RegisterDevice(devId, myCryptoCb, &myCtx);
551+
if (ret != 0) {
552+
fprintf(stderr, "ERROR: wc_CryptoCb_RegisterDevice failed %d\n", ret);
553+
goto exit;
554+
}
555+
#endif
556+
521557
/* Create and initialize WOLFSSL_CTX */
522558
if ((ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method())) == NULL) {
523559
fprintf(stderr, "ERROR: failed to create WOLFSSL_CTX\n");
524560
ret = -1;
525561
goto exit;
526562
}
527563

564+
/* register a devID for crypto callbacks */
565+
wolfSSL_CTX_SetDevId(ctx, devId);
566+
528567
#if 0
568+
/* Example: "TLS13-AES256-GCM-SHA384", "TLS13-AES128-GCM-SHA256" or "TLS13-CHACHA20-POLY1305-SHA256" */
529569
wolfSSL_CTX_set_cipher_list(ctx, "TLS13-AES256-GCM-SHA384");
530570
#endif
531571

532-
/* register a devID for crypto callbacks */
533-
ret = wc_CryptoCb_RegisterDevice(devId, myCryptoCb, &myCtx);
534-
if (ret != 0) {
535-
fprintf(stderr, "ERROR: wc_CryptoCb_RegisterDevice failed %d\n", ret);
536-
goto exit;
537-
}
538-
539-
/* register a devID for crypto callbacks */
540-
wolfSSL_CTX_SetDevId(ctx, devId);
541-
542572
/* Load client certificates into WOLFSSL_CTX */
543573
if ((ret = wolfSSL_CTX_load_verify_locations(ctx, CA_FILE, NULL))
544574
!= WOLFSSL_SUCCESS) {

tls/server-tls-cryptocb.c

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ typedef struct {
4949
word32 bufSz;
5050
} hash_ctx_t;
5151

52+
#ifdef USE_OPENSSL
53+
#include <openssl/sha.h>
54+
#endif
55+
5256
/* type: WC_HASH_TYPE_SHA, WC_HASH_TYPE_SHA256, WC_HASH_TYPE_SHA384, etc */
5357
/* in: Update (when not NULL) / Final (when NULL) */
5458
static int cb_hash(int type, const byte* in, word32 inSz, byte* digest,
@@ -86,11 +90,34 @@ static int cb_hash(int type, const byte* in, word32 inSz, byte* digest,
8690
hashBuf = (byte*)ctx + sizeof(hash_ctx_t);
8791
hashBufSz = ctx->bufSz;
8892
}
93+
94+
#ifdef USE_OPENSSL
95+
switch (hash_type) {
96+
case WC_HASH_TYPE_SHA:
97+
SHA1(hashBuf, hashBufSz, digest);
98+
break;
99+
case WC_HASH_TYPE_SHA224:
100+
SHA224(hashBuf, hashBufSz, digest);
101+
break;
102+
case WC_HASH_TYPE_SHA256:
103+
SHA256(hashBuf, hashBufSz, digest);
104+
break;
105+
case WC_HASH_TYPE_SHA384:
106+
SHA384(hashBuf, hashBufSz, digest);
107+
break;
108+
case WC_HASH_TYPE_SHA512:
109+
SHA512(hashBuf, hashBufSz, digest);
110+
break;
111+
default:
112+
ret = NOT_COMPILED_IN;
113+
break;
114+
}
115+
#else
89116
ret = wc_Hash_ex(hash_type,
90117
hashBuf, hashBufSz,
91118
digest, wc_HashGetDigestSize(hash_type),
92119
NULL, INVALID_DEVID);
93-
120+
#endif
94121
if (!(flags & WC_HASH_FLAG_ISCOPY)) {
95122
free(ctx);
96123
*devCtx = NULL;
@@ -481,6 +508,14 @@ int main(int argc, char** argv)
481508
memset(&myCtx, 0, sizeof(myCtx));
482509
myCtx.exampleVar = 1;
483510

511+
#if 1
512+
/* register a devID for crypto callbacks */
513+
ret = wc_CryptoCb_RegisterDevice(devId, myCryptoCb, &myCtx);
514+
if (ret != 0) {
515+
fprintf(stderr, "ERROR: wc_CryptoCb_RegisterDevice failed %d\n", ret);
516+
goto exit;
517+
}
518+
#endif
484519

485520
/* Create and initialize WOLFSSL_CTX */
486521
if ((ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method())) == NULL) {
@@ -489,6 +524,9 @@ int main(int argc, char** argv)
489524
goto exit;
490525
}
491526

527+
/* register a devID for crypto callbacks */
528+
wolfSSL_CTX_SetDevId(ctx, devId);
529+
492530
/* Load server certificates into WOLFSSL_CTX */
493531
if ((ret = wolfSSL_CTX_use_certificate_file(ctx, CERT_FILE, WOLFSSL_FILETYPE_PEM))
494532
!= WOLFSSL_SUCCESS) {
@@ -506,20 +544,10 @@ int main(int argc, char** argv)
506544
}
507545

508546
#if 0
547+
/* Example: "TLS13-AES256-GCM-SHA384", "TLS13-AES128-GCM-SHA256" or "TLS13-CHACHA20-POLY1305-SHA256" */
509548
wolfSSL_CTX_set_cipher_list(ctx, "TLS13-AES256-GCM-SHA384");
510549
#endif
511550

512-
/* register a devID for crypto callbacks */
513-
ret = wc_CryptoCb_RegisterDevice(devId, myCryptoCb, &myCtx);
514-
if (ret != 0) {
515-
fprintf(stderr, "ERROR: wc_CryptoCb_RegisterDevice failed %d\n", ret);
516-
goto exit;
517-
}
518-
519-
/* register a devID for crypto callbacks */
520-
wolfSSL_CTX_SetDevId(ctx, devId);
521-
522-
523551
/* Create a socket that uses an internet IPv4 address,
524552
* Sets the socket to be stream based (TCP),
525553
* 0 means choose the default protocol. */

0 commit comments

Comments
 (0)