Skip to content

Commit

Permalink
Added shared secret test for ecc ecdhe
Browse files Browse the repository at this point in the history
  • Loading branch information
aidangarske committed Sep 12, 2024
1 parent 14c66f9 commit c83bd8e
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 16 deletions.
112 changes: 107 additions & 5 deletions wrapper/CSharp/wolfCrypt-Test/wolfCrypt-Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
using System.Security.Cryptography;
using wolfSSL.CSharp;
using System.Runtime.InteropServices;
using static wolfSSL.CSharp.wolfcrypt;

public class wolfCrypt_Test_CSharp
{
Expand Down Expand Up @@ -85,12 +86,12 @@ private static void ecc_test(string hashAlgorithm, int keySize)
/* Export and Import Key */
Console.WriteLine("Testing ECC Key Export and Import...");
byte[] privateKeyDer;
ret = wolfcrypt.ExportPrivateKeyToDer(key, out privateKeyDer);
ret = wolfcrypt.EccExportPrivateKeyToDer(key, out privateKeyDer);
if (ret < 0) {
throw new Exception("ExportPrivateKeyToDer failed");
}
byte[] publicKeyDer;
ret = wolfcrypt.ExportPublicKeyToDer(key, out publicKeyDer, true);
ret = wolfcrypt.EccExportPublicKeyToDer(key, out publicKeyDer, true);
if (ret < 0) {
throw new Exception("ExportPublicKeyToDer failed");
}
Expand All @@ -100,7 +101,7 @@ private static void ecc_test(string hashAlgorithm, int keySize)
throw new Exception("EccImportKey Private failed");
}

PubKey = wolfcrypt.ImportPublicKeyFromDer(publicKeyDer);
PubKey = wolfcrypt.EccImportPublicKeyFromDer(publicKeyDer);
if (PubKey == IntPtr.Zero)
{
throw new Exception("ImportPublicKeyFromDer Public failed");
Expand Down Expand Up @@ -430,7 +431,7 @@ private static void curve25519_test()
IntPtr publicKeyB = IntPtr.Zero;
byte[] derKey;

Console.WriteLine("\nStarting Curve25519 test...");
Console.WriteLine("\nStarting Curve25519 shared secret test...");

/* Generate Key Pair A */
Console.WriteLine("Generating Key Pair A...");
Expand Down Expand Up @@ -520,6 +521,105 @@ private static void curve25519_test()
if (publicKeyB != IntPtr.Zero) wolfcrypt.Curve25519FreeKey(publicKeyB);
} /* END curve25519_test */

private static void ecdhe_test(int keySize)
{
int ret;
IntPtr keyA = IntPtr.Zero;
IntPtr keyB = IntPtr.Zero;
IntPtr publicKeyA = IntPtr.Zero;
IntPtr publicKeyB = IntPtr.Zero;
byte[] derKey;

Console.WriteLine("\nStarting ECDHE shared secret test...");

/* Generate Key Pair A */
Console.WriteLine("Generating Key Pair A...");
keyA = wolfcrypt.EccMakeKey(keySize);
if (keyA == IntPtr.Zero)
{
throw new Exception("Failed to generate key pair A.");
}

/* Generate Key Pair B */
Console.WriteLine("Generating Key Pair B...");
keyB = wolfcrypt.EccMakeKey(keySize);
if (keyB == IntPtr.Zero)
{
throw new Exception("Failed to generate key pair B.");
}
Console.WriteLine("ECC Key generation test passed.");

/* Export Public Key B to DER format */
Console.WriteLine("Exporting Public Key B to DER format...");
ret = wolfcrypt.EccExportPublicKeyToDer(keyB, out derKey, true);
if (ret < 0 || derKey == null)
{
throw new Exception("EccExportPublicKeyToDer failed");
}

/* Decode Public Key B from DER format */
Console.WriteLine("Decoding Public Key B from DER format...");
publicKeyB = wolfcrypt.EccImportPublicKeyFromDer(derKey);
if (publicKeyB == IntPtr.Zero)
{
throw new Exception("Failed to decode public key B from DER format.");
}
Console.WriteLine("ECC Export and Import test passed.");

/* Compute Shared Secret using Private Key A and Public Key B */
Console.WriteLine("Computing Shared Secret using Private Key A and Public Key B...");
byte[] sharedSecretA = new byte[wolfcrypt.ECC_KEY_SIZE];
int retA = wolfcrypt.EcdheSharedSecret(keyA, publicKeyB, sharedSecretA);
if (retA != 0)
{
throw new Exception("Failed to compute shared secret A. Error code: " + retA);
}
Console.WriteLine("ECC shared secret created using private Key A.");

/* Export Public Key A to DER format */
Console.WriteLine("Exporting Public Key A to DER format...");
ret = wolfcrypt.EccExportPublicKeyToDer(keyA, out derKey, true);
if (ret < 0 || derKey == null)
{
throw new Exception("EccExportPublicKeyToDer failed");
}

/* Decode Public Key A from DER format */
Console.WriteLine("Decoding Public Key A from DER format...");
publicKeyA = wolfcrypt.EccImportPublicKeyFromDer(derKey);
if (publicKeyA == IntPtr.Zero)
{
throw new Exception("Failed to decode public key A from DER format.");
}

/* Compute Shared Secret using Private Key B and Public Key A */
Console.WriteLine("Computing Shared Secret using Private Key B and Public Key A...");
byte[] sharedSecretB = new byte[wolfcrypt.ECC_KEY_SIZE];
int retB = wolfcrypt.EcdheSharedSecret(keyB, publicKeyA, sharedSecretB);
if (retB != 0)
{
throw new Exception("Failed to compute shared secret B. Error code: " + retB);
}
Console.WriteLine("ECC shared secret created using private Key B.");

/* Compare Shared Secrets */
Console.WriteLine("Comparing Shared Secrets...");
if (!wolfcrypt.ByteArrayVerify(sharedSecretA, sharedSecretB))
{
throw new Exception("Shared secrets do not match.");
}
else
{
Console.WriteLine("ECC shared secret match.");
}

/* Cleanup */
if (keyA != IntPtr.Zero) wolfcrypt.EccFreeKey(keyA);
if (keyB != IntPtr.Zero) wolfcrypt.EccFreeKey(keyB);
if (publicKeyA != IntPtr.Zero) wolfcrypt.EccFreeKey(publicKeyA);
if (publicKeyB != IntPtr.Zero) wolfcrypt.EccFreeKey(publicKeyB);
} /* END ecdhe_test */

private static void aes_gcm_test()
{
IntPtr aes = IntPtr.Zero;
Expand Down Expand Up @@ -758,7 +858,9 @@ public static void Main(string[] args)

ed25519_test(); /* ED25519 test */

curve25519_test(); /* curve25519 test */
curve25519_test(); /* curve25519 shared secret test */

ecdhe_test(32); /* ECDHE shared secret test */

aes_gcm_test(); /* AES_GCM test */

Expand Down
23 changes: 12 additions & 11 deletions wrapper/CSharp/wolfSSL_CSharp/wolfCrypt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ private static void log(int lvl, string msg)
public static readonly int OTHER_LOG = 4;
public static readonly int INVALID_DEVID = -2;
public static readonly int ECC_MAX_SIG_SIZE = 141; /* ECC max sig size */
public static readonly int ECC_KEY_SIZE = 32; /* ECC key size */
public static readonly int MAX_ECIES_TEST_SZ = 200; /* ECIES max sig size */
public static readonly int ED25519_SIG_SIZE = 64; /* ED25519 pub + priv */
public static readonly int ED25519_KEY_SIZE = 32; /* Private key only */
Expand Down Expand Up @@ -663,7 +664,7 @@ public static int EccVerify(IntPtr key, byte[] signature, byte[] hash)
/// </summary>
/// <param name="key">ECC key structure</param>
/// <returns>DER-encoded private key as byte array</returns>
public static int ExportPrivateKeyToDer(IntPtr key, out byte[] derKey)
public static int EccExportPrivateKeyToDer(IntPtr key, out byte[] derKey)
{
int ret;
derKey = null;
Expand Down Expand Up @@ -697,7 +698,7 @@ public static int ExportPrivateKeyToDer(IntPtr key, out byte[] derKey)
/// <param name="key">ECC key structure</param>
/// <param name="includeCurve">Include algorithm curve in the output</param>
/// <returns>DER-encoded public key as byte array</returns>
public static int ExportPublicKeyToDer(IntPtr key, out byte[] derKey, bool includeCurve)
public static int EccExportPublicKeyToDer(IntPtr key, out byte[] derKey, bool includeCurve)
{
int ret;
derKey = null;
Expand Down Expand Up @@ -730,7 +731,7 @@ public static int ExportPublicKeyToDer(IntPtr key, out byte[] derKey, bool inclu
/// </summary>
/// <param name="keyDer">DER-encoded public key</param>
/// <returns>Allocated ECC key structure or null</returns>
public static IntPtr ImportPublicKeyFromDer(byte[] keyDer)
public static IntPtr EccImportPublicKeyFromDer(byte[] keyDer)
{
int ret;
IntPtr key = IntPtr.Zero;
Expand Down Expand Up @@ -1176,8 +1177,8 @@ public static int EciesDecrypt(IntPtr privKey, IntPtr pubKey, byte[] msg, uint m
/// <returns>0 on success, otherwise an error code</returns>
public static int EcdheSharedSecret(IntPtr privateKey, IntPtr publicKey, byte[] secret)
{
int ret = -1;
int secretLength = secret.Length;
int ret;
int secretLength = secret.Length;

try
{
Expand Down Expand Up @@ -2247,8 +2248,8 @@ public static void Curve25519FreeKey(IntPtr key)
/// <returns>0 on success, otherwise an error code</returns>
public static int Curve25519SharedSecret(IntPtr privateKey, IntPtr publicKey, byte[] secret)
{
int ret = -1;
int secretLength = secret.Length;
int ret;
int secretLength = secret.Length;

try
{
Expand Down Expand Up @@ -2671,7 +2672,7 @@ public static IntPtr HashNew(uint hashType, IntPtr heap, int devId)
/// <returns>0 on success, otherwise an error code</returns>
public static int InitHash(IntPtr hash, uint hashType)
{
int ret = -1;
int ret = 0;

try
{
Expand Down Expand Up @@ -2704,7 +2705,7 @@ public static int InitHash(IntPtr hash, uint hashType)
/// <returns>0 on success, otherwise an error code</returns>
public static int HashUpdate(IntPtr hash, uint hashType, byte[] data)
{
int ret = -1;
int ret = 0;
IntPtr dataPtr = IntPtr.Zero;

try
Expand Down Expand Up @@ -2748,7 +2749,7 @@ public static int HashUpdate(IntPtr hash, uint hashType, byte[] data)
/// <returns>0 on success, otherwise an error code</returns>
public static int HashFinal(IntPtr hash, uint hashType, out byte[] output)
{
int ret = -1;
int ret = 0;
IntPtr outputPtr = IntPtr.Zero;

try
Expand Down Expand Up @@ -2796,7 +2797,7 @@ public static int HashFinal(IntPtr hash, uint hashType, out byte[] output)
/// <returns>0 on success, otherwise an error code</returns>
public static int HashFree(IntPtr hash, uint hashType)
{
int ret = -1;
int ret = 0;

try
{
Expand Down

0 comments on commit c83bd8e

Please sign in to comment.