Skip to content

Commit

Permalink
CSharp wolfCrypt wrapper for ECC, RSA, ED25519, and Curve25519
Browse files Browse the repository at this point in the history
  • Loading branch information
aidangarske authored and dgarske committed Aug 22, 2024
1 parent 2600b74 commit a3641a5
Show file tree
Hide file tree
Showing 10 changed files with 2,305 additions and 324 deletions.
5 changes: 5 additions & 0 deletions IDE/WIN/user_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
#define WOLFSSL_DTLS13
#define WOLFSSL_SEND_HRR_COOKIE
#define WOLFSSL_DTLS_CID
#define WOLFSSL_KEY_GEN
#define DEBUG_WOLFSSL
#define HAVE_ED25519
#define WOLFSSL_ASN_TEMPLATE
#define HAVE_CURVE25519

/* Configurations */
#if defined(HAVE_FIPS)
Expand Down
22 changes: 22 additions & 0 deletions wolfcrypt/src/curve25519.c
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,22 @@ int wc_curve25519_import_private_ex(const byte* priv, word32 privSz,

#endif /* HAVE_CURVE25519_KEY_IMPORT */

curve25519_key* wc_curve25519_new(void* heap, int devId)
{
curve25519_key* key = (curve25519_key*)XMALLOC(sizeof(curve25519_key), heap,
DYNAMIC_TYPE_CURVE25519);
if (key != NULL) {
if (wc_curve25519_init_ex(key, heap, devId) != 0) {
XFREE(key, heap, DYNAMIC_TYPE_CURVE25519);
key = NULL;
}
else {
key->isAllocated = 1;
}
}
return key;
}

int wc_curve25519_init_ex(curve25519_key* key, void* heap, int devId)
{
if (key == NULL)
Expand Down Expand Up @@ -706,6 +722,12 @@ void wc_curve25519_free(curve25519_key* key)
#ifdef WOLFSSL_CHECK_MEM_ZERO
wc_MemZero_Check(key, sizeof(curve25519_key));
#endif

if (key->isAllocated) {
void* heap = key->heap;
XFREE(key, heap, DYNAMIC_TYPE_CURVE25519);
(void)heap;
}
}

/* get key size */
Expand Down
21 changes: 21 additions & 0 deletions wolfcrypt/src/ed25519.c
Original file line number Diff line number Diff line change
Expand Up @@ -968,6 +968,21 @@ int wc_ed25519ph_verify_msg(const byte* sig, word32 sigLen, const byte* msg,
}
#endif /* HAVE_ED25519_VERIFY */

ed25519_key* wc_ed25519_new(void* heap, int devId)
{
ed25519_key* key = (ed25519_key*)XMALLOC(sizeof(ed25519_key), heap,
DYNAMIC_TYPE_ED25519);
if (key != NULL) {
if (wc_ed25519_init_ex(key, heap, devId) != 0) {
XFREE(key, heap, DYNAMIC_TYPE_ED25519);
key = NULL;
}
else {
key->isAllocated = 1;
}
}
return key;
}

/* initialize information and memory for key */
int wc_ed25519_init_ex(ed25519_key* key, void* heap, int devId)
Expand Down Expand Up @@ -1023,6 +1038,12 @@ void wc_ed25519_free(ed25519_key* key)
#ifdef WOLFSSL_CHECK_MEM_ZERO
wc_MemZero_Check(key, sizeof(ed25519_key));
#endif

if (key->isAllocated) {
void* heap = key->heap;
XFREE(key, heap, DYNAMIC_TYPE_ED25519);
(void)heap;
}
}


Expand Down
21 changes: 21 additions & 0 deletions wolfcrypt/src/rsa.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,21 @@ static void wc_RsaCleanup(RsaKey* key)
#endif
}

RsaKey* wc_NewRsaKey(void* heap, int devId)
{
RsaKey* key = (RsaKey*)XMALLOC(sizeof(RsaKey), heap, DYNAMIC_TYPE_RSA);
if (key != NULL) {
if (wc_InitRsaKey_ex(key, heap, devId) != 0) {
XFREE(key, heap, DYNAMIC_TYPE_RSA);
key = NULL;
}
else {
key->isAllocated = 1;
}
}
return key;
}

int wc_InitRsaKey_ex(RsaKey* key, void* heap, int devId)
{
int ret = 0;
Expand Down Expand Up @@ -595,6 +610,12 @@ int wc_FreeRsaKey(RsaKey* key)
wc_fspsm_RsaKeyFree(key);
#endif

if (key->isAllocated) {
void* heap = key->heap;
XFREE(key, heap, DYNAMIC_TYPE_RSA);
(void)heap;
}

return ret;
}

Expand Down
6 changes: 5 additions & 1 deletion wolfssl/wolfcrypt/curve25519.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ struct curve25519_key {
void* devCtx;
int devId;
#endif

void *heap;
#ifdef WOLFSSL_SE050
word32 keyId;
byte keyIdSet;
Expand All @@ -99,6 +99,8 @@ struct curve25519_key {
/* bit fields */
byte pubSet:1;
byte privSet:1;

unsigned int isAllocated:1; /* flag indicates if structure was allocated */
};

enum {
Expand Down Expand Up @@ -131,6 +133,8 @@ int wc_curve25519_shared_secret_ex(curve25519_key* private_key,
curve25519_key* public_key,
byte* out, word32* outlen, int endian);

WOLFSSL_API
curve25519_key* wc_curve25519_new(void* heap);
WOLFSSL_API
int wc_curve25519_init(curve25519_key* key);
WOLFSSL_API
Expand Down
4 changes: 3 additions & 1 deletion wolfssl/wolfcrypt/ed25519.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ struct ed25519_key {
wc_Sha512 sha;
int sha_clean_flag;
#endif
unsigned int isAllocated:1; /* flag indicates if structure was allocated */
};

#ifndef WC_ED25519KEY_TYPE_DEFINED
Expand Down Expand Up @@ -175,7 +176,8 @@ int wc_ed25519_verify_msg_final(const byte* sig, word32 sigLen, int* res,
#endif /* WOLFSSL_ED25519_STREAMING_VERIFY */
#endif /* HAVE_ED25519_VERIFY */


WOLFSSL_API
ed25519_key* wc_ed25519_new(void* heap);
WOLFSSL_API
int wc_ed25519_init(ed25519_key* key);
WOLFSSL_API
Expand Down
2 changes: 2 additions & 0 deletions wolfssl/wolfcrypt/rsa.h
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ struct RsaKey {
#if defined(WOLFSSL_RENESAS_FSPSM)
FSPSM_RSA_CTX ctx;
#endif
unsigned int isAllocated:1; /* flag indicates if structure was allocated */
};

#ifndef WC_RSAKEY_TYPE_DEFINED
Expand All @@ -274,6 +275,7 @@ struct RsaKey {

#endif /* HAVE_FIPS */

WOLFSSL_API RsaKey* wc_NewRsaKey(void* heap, int devId);
WOLFSSL_API int wc_InitRsaKey(RsaKey* key, void* heap);
WOLFSSL_API int wc_InitRsaKey_ex(RsaKey* key, void* heap, int devId);
WOLFSSL_API int wc_FreeRsaKey(RsaKey* key);
Expand Down
14 changes: 13 additions & 1 deletion wrapper/CSharp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
This directory contains the CSharp wrapper for the wolfSSL TLS layer with examples.

* `wolfSSL_CSharp`: wolfSSL TLS layer wrappers (library)
* `wolfCrypt-Test`: wolfSSL TLS layer wrapper testing.

Examples:
* `wolfSSL-DTLS-PSK-Server`
Expand All @@ -20,6 +21,17 @@ A Visual Studio solution `wolfSSL_CSharp.sln` is provided. This will allow you
to build the wrapper library and examples. It includes the wolfSSL Visual Studio
project directly.

To successfully run and build the solution on Windows Visual Studio you will
need to open a new solution `wolfSSL_CSharp.sln` located in `wrapper\CSharp\wolfSSL_CSharp.sln`.

Select the CPU type, configuration, and target file.
select `Build` and either `Rebuild Solution` or `Build Solution`.

If you have problems with the .dll you can change the output path by going into
the Solution Explorer, left clicking the solution you wish to configure, select
Properties and then Output Directory.
The default is, `bin\$(Configuration)\$(Platform)\`

## Linux (Ubuntu) using mono

Prerequisites for linux:
Expand Down Expand Up @@ -81,7 +93,7 @@ mono client.exe
To enable SNI, just pass the `-S` argument with the specified hostname to the client:

```
mono client.exe -S hostname
mono client.exe -S hostname
```

And run the server with the `-S` flag:
Expand Down
Loading

0 comments on commit a3641a5

Please sign in to comment.