Skip to content

Commit

Permalink
wolfCrypt CSharp wrapper ECIES support and test
Browse files Browse the repository at this point in the history
  • Loading branch information
aidangarske authored and dgarske committed Sep 11, 2024
1 parent 0c1a940 commit 4b282ff
Show file tree
Hide file tree
Showing 4 changed files with 598 additions and 28 deletions.
2 changes: 1 addition & 1 deletion wrapper/CSharp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ apt-get install mono-complete

```
./autogen.sh
./configure --enable-keygen --enable-ed25519 --enable-curve25519 --enable-aesgcm
./configure --enable-keygen --enable--eccencrypt --enable-ed25519 --enable-curve25519 --enable-aesgcm
make
make check
sudo make install
Expand Down
3 changes: 3 additions & 0 deletions wrapper/CSharp/user_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#define WOLFSSL_KEY_GEN /* RSA key gen */
#define WOLFSSL_ASN_TEMPLATE /* default */
#define WOLFSSL_SHA3

#if 0
#define OPENSSL_EXTRA
#endif
Expand All @@ -67,6 +68,8 @@
#define HAVE_HKDF

#undef NO_DH
#define HAVE_PUBLIC_FFDHE
#define HAVE_FFDHE_2048
#define HAVE_FFDHE_4096

#undef NO_RSA
Expand Down
102 changes: 102 additions & 0 deletions wrapper/CSharp/wolfCrypt-Test/wolfCrypt-Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,106 @@ private static void hash_test(uint hashType)
}
} /* END hash_test */

private static void ecies_test()
{
const int keySize = 32;
const int bufferSize = 128;
const string message = "Hello wolfSSL!";
byte[] salt = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };

IntPtr a = IntPtr.Zero;
IntPtr b = IntPtr.Zero;
IntPtr aCtx = IntPtr.Zero;
IntPtr bCtx = IntPtr.Zero;
IntPtr rng = IntPtr.Zero;
IntPtr heap = IntPtr.Zero;

byte[] plaintext = new byte[bufferSize];
byte[] encrypted = new byte[bufferSize];
byte[] decrypted = new byte[bufferSize];

try
{
Console.WriteLine($"\nStarting ecies test for {keySize} key size...");

/* Create a new RNG context */
rng = wolfcrypt.RandomNew();
if (rng == IntPtr.Zero)
{
throw new Exception("RNG initialization failed.");
}

/* Initialize keys */
a = wolfcrypt.EccMakeKey(keySize);
b = wolfcrypt.EccMakeKey(keySize);
if (a == IntPtr.Zero || b == IntPtr.Zero)
{
throw new Exception("Key generation failed.");
}
Console.WriteLine("ECC key generation passed.");

/* Create ECIES contexts for encryption and decryption */
aCtx = wolfcrypt.EciesNewCtx((int)wolfcrypt.ecFlags.REQ_RESP_CLIENT, rng, heap);
bCtx = wolfcrypt.EciesNewCtx((int)wolfcrypt.ecFlags.REQ_RESP_SERVER, rng, heap);
if (aCtx == IntPtr.Zero || bCtx == IntPtr.Zero)
{
throw new Exception("Context creation failed.");
}
Console.WriteLine("ECC context creation passed.");

/* Set KDF salt */
if (wolfcrypt.EciesSetKdfSalt(aCtx, salt) != 0 ||
wolfcrypt.EciesSetKdfSalt(bCtx, salt) != 0)
{
throw new Exception("Failed to set KDF salt.");
}
Console.WriteLine("KDF salt setup passed.");

/* Prepare plaintext */
Array.Clear(plaintext, 0, plaintext.Length);
Array.Copy(Encoding.ASCII.GetBytes(message), plaintext, message.Length);
/* Pad to block size */
int plaintextLen = ((message.Length + (wolfcrypt.AES_BLOCK_SIZE - 1)) /
wolfcrypt.AES_BLOCK_SIZE) * wolfcrypt.AES_BLOCK_SIZE;

/* Encrypt message */
int ret = wolfcrypt.EciesEncrypt(a, b, plaintext, (uint)plaintextLen, encrypted, aCtx);
if (ret < 0)
{
throw new Exception("Encryption failed.");
}

int encryptedLen = ret;
Console.WriteLine("ECC encryption passed.");

/* Decrypt message */
ret = wolfcrypt.EciesDecrypt(b, a, encrypted, (uint)encryptedLen, decrypted, bCtx);
if (ret < 0)
{
throw new Exception("Decryption failed.");
}

int decryptedLen = ret;
Console.WriteLine("ECC decryption passed.");

/* Compare decrypted text to original plaintext */
if (!wolfcrypt.ByteArrayVerify(plaintext, decrypted))
{
throw new Exception("Decrypted text does not match original plaintext.");
}
Console.WriteLine("Decrypted text matches original plaintext.");
}
finally
{
/* Cleanup key and context */
if (a != IntPtr.Zero) wolfcrypt.EccFreeKey(a);
if (b != IntPtr.Zero) wolfcrypt.EccFreeKey(b);
if (aCtx != IntPtr.Zero) wolfcrypt.EciesFreeCtx(aCtx);
if (bCtx != IntPtr.Zero) wolfcrypt.EciesFreeCtx(bCtx);
if (rng != IntPtr.Zero) wolfcrypt.RandomFree(rng);
}
} /* END ecies_test */

public static void standard_log(int lvl, StringBuilder msg)
{
Console.WriteLine(msg);
Expand Down Expand Up @@ -670,6 +770,8 @@ public static void Main(string[] args)
hash_test((uint)wolfcrypt.hashType.WC_HASH_TYPE_SHA512); /* SHA-512 HASH test */
hash_test((uint)wolfcrypt.hashType.WC_HASH_TYPE_SHA3_256); /* SHA3_256 HASH test */

ecies_test(); /* ECIES test */

wolfcrypt.Cleanup();

Console.WriteLine("\nAll tests completed successfully");
Expand Down
Loading

0 comments on commit 4b282ff

Please sign in to comment.