Skip to content

Commit ece3d2e

Browse files
committed
Improvements to C# PQC
1 parent bd6b24d commit ece3d2e

File tree

5 files changed

+50
-47
lines changed

5 files changed

+50
-47
lines changed

wrapper/CSharp/README.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,15 @@ apt-get install mono-complete
4040

4141
### Build wolfSSL and install
4242

43+
Build and install into a local prefix (no sudo required):
44+
4345
```
4446
./autogen.sh
4547
cp wrapper/CSharp/user_settings.h .
46-
./configure --enable-usersettings
48+
./configure --enable-usersettings --prefix=$(pwd)/install
4749
make
4850
make check
49-
sudo make install
51+
make install
5052
```
5153

5254
### Build and run the wolfCrypt test wrapper
@@ -57,7 +59,13 @@ Compile wolfCrypt test:
5759

5860
```
5961
mcs wolfCrypt-Test/wolfCrypt-Test.cs wolfSSL_CSharp/wolfCrypt.cs wolfSSL_CSharp/wolfSSL.cs wolfSSL_CSharp/X509.cs -OUT:wolfcrypttest.exe
60-
mono wolfcrypttest.exe
62+
```
63+
64+
Run from the wolfSSL root directory using `LD_LIBRARY_PATH` so mono loads
65+
the freshly-built library from the local install prefix:
66+
67+
```
68+
LD_LIBRARY_PATH=./install/lib mono wrapper/CSharp/wolfcrypttest.exe
6169
```
6270

6371
### Build and run the wolfSSL client/server test

wrapper/CSharp/user_settings.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
#define HAVE_MLKEM
9191
#define WOLFSSL_WC_MLKEM
9292
#define WOLFSSL_HAVE_MLKEM
93+
/* Required for PQC with DTLS 1.3 (auto-enabled in settings.h, explicit for clarity) */
9394
#define WOLFSSL_DTLS_CH_FRAG
9495
#define HAVE_DILITHIUM
9596
#define WOLFSSL_WC_DILITHIUM

wrapper/CSharp/wolfCrypt-Test/wolfCrypt-Test.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -871,15 +871,15 @@ private static void mldsa_test(wolfcrypt.MlDsaLevels level)
871871
if (ret == 0)
872872
{
873873
Console.WriteLine("Testing ML-DSA Key Export...");
874-
ret = DilithiumExportPrivateKey(key, out privateKey);
874+
ret = wolfcrypt.DilithiumExportPrivateKey(key, out privateKey);
875875
if (ret != 0)
876876
{
877877
Console.Error.WriteLine($"Failed to export private key. Error code: {ret}");
878878
}
879879
}
880880
if (ret == 0)
881881
{
882-
ret = DilithiumExportPublicKey(key, out publicKey);
882+
ret = wolfcrypt.DilithiumExportPublicKey(key, out publicKey);
883883
if (ret != 0)
884884
{
885885
Console.Error.WriteLine($"Failed to export public key. Error code: {ret}");
@@ -894,15 +894,15 @@ private static void mldsa_test(wolfcrypt.MlDsaLevels level)
894894
if (ret == 0)
895895
{
896896
Console.WriteLine("Testing ML-DSA Key Import...");
897-
ret = DilithiumImportPrivateKey(privateKey, key);
897+
ret = wolfcrypt.DilithiumImportPrivateKey(privateKey, key);
898898
if (ret != 0)
899899
{
900900
Console.Error.WriteLine($"Failed to import private key. Error code: {ret}");
901901
}
902902
}
903903
if (ret == 0)
904904
{
905-
ret = DilithiumImportPublicKey(publicKey, key);
905+
ret = wolfcrypt.DilithiumImportPublicKey(publicKey, key);
906906
if (ret != 0)
907907
{
908908
Console.Error.WriteLine($"Failed to import public key. Error code: {ret}");

wrapper/CSharp/wolfSSL_CSharp/wolfCrypt.cs

Lines changed: 34 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -528,9 +528,9 @@ public class wolfcrypt
528528
[DllImport(wolfssl_dll)]
529529
private static extern int wc_dilithium_import_public(byte[] input, uint inputLen, IntPtr key);
530530
[DllImport(wolfssl_dll)]
531-
private static extern int wc_dilithium_sign_msg(byte[] msg, uint msgLen, byte[] sig, ref uint sigLen, IntPtr key, IntPtr rng);
531+
private static extern int wc_dilithium_sign_ctx_msg(byte[] ctx, byte ctxLen, byte[] msg, uint msgLen, byte[] sig, ref uint sigLen, IntPtr key, IntPtr rng);
532532
[DllImport(wolfssl_dll)]
533-
private static extern int wc_dilithium_verify_msg(byte[] sig, uint sigLen, byte[] msg, uint msgLen, ref int res, IntPtr key);
533+
private static extern int wc_dilithium_verify_ctx_msg(byte[] sig, uint sigLen, byte[] ctx, byte ctxLen, byte[] msg, uint msgLen, ref int res, IntPtr key);
534534
[DllImport(wolfssl_dll)]
535535
private static extern int wc_MlDsaKey_GetPrivLen(IntPtr key, ref int len);
536536
[DllImport(wolfssl_dll)]
@@ -559,9 +559,9 @@ public class wolfcrypt
559559
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
560560
private static extern int wc_dilithium_import_public(byte[] input, uint inputLen, IntPtr key);
561561
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
562-
private static extern int wc_dilithium_sign_msg(byte[] msg, uint msgLen, byte[] sig, ref uint sigLen, IntPtr key, IntPtr rng);
562+
private static extern int wc_dilithium_sign_ctx_msg(byte[] ctx, byte ctxLen, byte[] msg, uint msgLen, byte[] sig, ref uint sigLen, IntPtr key, IntPtr rng);
563563
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
564-
private static extern int wc_dilithium_verify_msg(byte[] sig, uint sigLen, byte[] msg, uint msgLen, ref int res, IntPtr key);
564+
private static extern int wc_dilithium_verify_ctx_msg(byte[] sig, uint sigLen, byte[] ctx, byte ctxLen, byte[] msg, uint msgLen, ref int res, IntPtr key);
565565
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
566566
private static extern int wc_MlDsaKey_GetPrivLen(IntPtr key, ref int len);
567567
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
@@ -2933,18 +2933,15 @@ public static IntPtr MlKemMakeKey(MlKemTypes type, IntPtr heap, int devId)
29332933
/// <returns>0 on success, negative value on error.</returns>
29342934
public static int MlKemFreeKey(ref IntPtr key)
29352935
{
2936-
int ret = 0;
2936+
int ret;
29372937

29382938
if (key == IntPtr.Zero)
29392939
{
29402940
return BAD_FUNC_ARG;
29412941
}
29422942

2943-
if (key != IntPtr.Zero)
2944-
{
2945-
ret = wc_MlKemKey_Delete(key, IntPtr.Zero);
2946-
key = IntPtr.Zero;
2947-
}
2943+
ret = wc_MlKemKey_Delete(key, IntPtr.Zero);
2944+
key = IntPtr.Zero;
29482945
return ret;
29492946
}
29502947

@@ -2968,10 +2965,10 @@ public static int MlKemEncodePublicKey(IntPtr key, out byte[] publicKey)
29682965
try
29692966
{
29702967
ret = wc_MlKemKey_PublicKeySize(key, ref pubLen);
2971-
if (ret !=0 || pubLen == 0)
2968+
if (ret != 0 || pubLen == 0)
29722969
{
29732970
log(ERROR_LOG, "Failed to get MlKem public key length. Error code: " + ret);
2974-
return ret;
2971+
return (ret != 0) ? ret : BAD_FUNC_ARG;
29752972
}
29762973
if (pubLen > int.MaxValue)
29772974
{
@@ -3017,10 +3014,10 @@ public static int MlKemEncodePrivateKey(IntPtr key, out byte[] privateKey)
30173014
try
30183015
{
30193016
ret = wc_MlKemKey_PrivateKeySize(key, ref privLen);
3020-
if (ret !=0 || privLen == 0)
3017+
if (ret != 0 || privLen == 0)
30213018
{
30223019
log(ERROR_LOG, "Failed to get MlKem private key length. Error code: " + ret);
3023-
return ret;
3020+
return (ret != 0) ? ret : BAD_FUNC_ARG;
30243021
}
30253022
if (privLen > int.MaxValue)
30263023
{
@@ -3074,14 +3071,14 @@ public static int MlKemDecodePublicKey(IntPtr key, byte[] publicKey)
30743071
if (ret != 0 || pubLen == 0)
30753072
{
30763073
log(ERROR_LOG, "Failed to get MlKem public key length. Error code: " + ret);
3077-
return ret;
3074+
return (ret != 0) ? ret : BAD_FUNC_ARG;
30783075
}
30793076
if ((uint)publicKey.Length != pubLen)
3080-
{
3081-
log(ERROR_LOG, "MlKem public key buffer length mismatch. Expected: " +
3082-
pubLen + ", actual: " + publicKey.Length);
3083-
return BUFFER_E;
3084-
}
3077+
{
3078+
log(ERROR_LOG, "MlKem public key buffer length mismatch. Expected: " +
3079+
pubLen + ", actual: " + publicKey.Length);
3080+
return BUFFER_E;
3081+
}
30853082

30863083
ret = wc_MlKemKey_DecodePublicKey(key, publicKey, pubLen);
30873084
if (ret != 0)
@@ -3123,12 +3120,12 @@ public static int MlKemDecodePrivateKey(IntPtr key, byte[] privateKey)
31233120
try
31243121
{
31253122
ret = wc_MlKemKey_PrivateKeySize(key, ref privLen);
3126-
if (privLen == 0)
3123+
if (ret != 0 || privLen == 0)
31273124
{
31283125
log(ERROR_LOG, "Failed to get MlKem private key length. Error code: " + ret);
3129-
return ret;
3126+
return (ret != 0) ? ret : BAD_FUNC_ARG;
31303127
}
3131-
3128+
31323129
if ((uint)privateKey.Length != privLen)
31333130
{
31343131
log(ERROR_LOG, "MlKem private key buffer length mismatch. Required: " + privLen +
@@ -3367,18 +3364,15 @@ public static IntPtr DilithiumMakeKey(IntPtr heap, int devId, MlDsaLevels level)
33673364
/// <returns>0 on success, negative value on error.</returns>
33683365
public static int DilithiumFreeKey(ref IntPtr key)
33693366
{
3370-
int ret = 0;
3367+
int ret;
33713368

33723369
if (key == IntPtr.Zero)
33733370
{
33743371
return BAD_FUNC_ARG;
33753372
}
33763373

3377-
if (key != IntPtr.Zero)
3378-
{
3379-
ret = wc_dilithium_delete(key, IntPtr.Zero);
3380-
key = IntPtr.Zero;
3381-
}
3374+
ret = wc_dilithium_delete(key, IntPtr.Zero);
3375+
key = IntPtr.Zero;
33823376
return ret;
33833377
}
33843378

@@ -3451,10 +3445,10 @@ public static int DilithiumExportPrivateKey(IntPtr key, out byte[] privateKey)
34513445
try
34523446
{
34533447
ret = wc_MlDsaKey_GetPrivLen(key, ref privLen);
3454-
if (privLen <= 0)
3448+
if (ret != 0 || privLen <= 0)
34553449
{
34563450
log(ERROR_LOG, "Failed to get Dilithium private key length. Error code: " + ret);
3457-
return ret;
3451+
return (ret != 0) ? ret : BAD_FUNC_ARG;
34583452
}
34593453

34603454
privateKey = new byte[privLen];
@@ -3501,10 +3495,10 @@ public static int DilithiumExportPublicKey(IntPtr key, out byte[] publicKey)
35013495
try
35023496
{
35033497
ret = wc_MlDsaKey_GetPubLen(key, ref pubLen);
3504-
if (pubLen <= 0)
3498+
if (ret != 0 || pubLen <= 0)
35053499
{
35063500
log(ERROR_LOG, "Failed to get Dilithium public key length. Error code: " + ret);
3507-
return ret;
3501+
return (ret != 0) ? ret : BAD_FUNC_ARG;
35083502
}
35093503

35103504
publicKey = new byte[pubLen];
@@ -3553,10 +3547,10 @@ public static int DilithiumSignMsg(IntPtr key, byte[] msg, out byte[] sig)
35533547
try
35543548
{
35553549
ret = wc_MlDsaKey_GetSigLen(key, ref sigLen);
3556-
if (sigLen <= 0)
3550+
if (ret != 0 || sigLen <= 0)
35573551
{
35583552
log(ERROR_LOG, "Failed to get Dilithium signature length. Error code: " + ret);
3559-
return ret;
3553+
return (ret != 0) ? ret : BAD_FUNC_ARG;
35603554
}
35613555

35623556
sig = new byte[sigLen];
@@ -3565,9 +3559,10 @@ public static int DilithiumSignMsg(IntPtr key, byte[] msg, out byte[] sig)
35653559
if (rng == IntPtr.Zero)
35663560
{
35673561
log(ERROR_LOG, "Failed to create RNG for Dilithium signing.");
3568-
return EXCEPTION_E;
3562+
return MEMORY_E;
35693563
}
3570-
ret = wc_dilithium_sign_msg(msg, (uint)msg.Length, sig, ref outLen, key, rng);
3564+
/* FIPS 204 sign with empty context (ctx=null, ctxLen=0). */
3565+
ret = wc_dilithium_sign_ctx_msg(null, 0, msg, (uint)msg.Length, sig, ref outLen, key, rng);
35713566
if (ret != 0)
35723567
{
35733568
log(ERROR_LOG, "Failed to sign message with Dilithium key. Error code: " + ret);
@@ -3611,7 +3606,8 @@ public static int DilithiumVerifyMsg(IntPtr key, byte[] msg, byte[] sig)
36113606

36123607
try
36133608
{
3614-
ret = wc_dilithium_verify_msg(sig, (uint)sig.Length, msg, (uint)msg.Length, ref res, key);
3609+
/* FIPS 204 verify with empty context (ctx=null, ctxLen=0). */
3610+
ret = wc_dilithium_verify_ctx_msg(sig, (uint)sig.Length, null, 0, msg, (uint)msg.Length, ref res, key);
36153611
if (ret != 0)
36163612
{
36173613
log(ERROR_LOG, "Failed to verify message with Dilithium key. Error code: " + ret);

wrapper/CSharp/wolfSSL_CSharp/wolfSSL.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -795,8 +795,6 @@ public enum NamedGroup
795795
WOLFSSL_SECP521R1MLKEM1024 = 12109,
796796
WOLFSSL_X25519MLKEM512 = 12214,
797797
WOLFSSL_X448MLKEM768 = 12215,
798-
799-
WOLF_ENUM_DUMMY_LAST_ELEMENT = 0
800798
}
801799

802800

0 commit comments

Comments
 (0)