Skip to content

Commit 72c57dc

Browse files
committed
Improvements to C# PQC
1 parent 21be377 commit 72c57dc

4 files changed

Lines changed: 39 additions & 44 deletions

File tree

wrapper/CSharp/user_settings.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
#define HAVE_MLKEM
9292
#define WOLFSSL_WC_MLKEM
9393
#define WOLFSSL_HAVE_MLKEM
94+
/* Required for PQC with DTLS 1.3 (auto-enabled in settings.h, explicit for clarity) */
9495
#define WOLFSSL_DTLS_CH_FRAG
9596
#define HAVE_DILITHIUM
9697
#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
@@ -540,9 +540,9 @@ public class wolfcrypt
540540
[DllImport(wolfssl_dll)]
541541
private static extern int wc_dilithium_import_public(byte[] input, uint inputLen, IntPtr key);
542542
[DllImport(wolfssl_dll)]
543-
private static extern int wc_dilithium_sign_msg(byte[] msg, uint msgLen, byte[] sig, ref uint sigLen, IntPtr key, IntPtr rng);
543+
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);
544544
[DllImport(wolfssl_dll)]
545-
private static extern int wc_dilithium_verify_msg(byte[] sig, uint sigLen, byte[] msg, uint msgLen, ref int res, IntPtr key);
545+
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);
546546
[DllImport(wolfssl_dll)]
547547
private static extern int wc_MlDsaKey_GetPrivLen(IntPtr key, ref int len);
548548
[DllImport(wolfssl_dll)]
@@ -571,9 +571,9 @@ public class wolfcrypt
571571
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
572572
private static extern int wc_dilithium_import_public(byte[] input, uint inputLen, IntPtr key);
573573
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
574-
private static extern int wc_dilithium_sign_msg(byte[] msg, uint msgLen, byte[] sig, ref uint sigLen, IntPtr key, IntPtr rng);
574+
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);
575575
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
576-
private static extern int wc_dilithium_verify_msg(byte[] sig, uint sigLen, byte[] msg, uint msgLen, ref int res, IntPtr key);
576+
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);
577577
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
578578
private static extern int wc_MlDsaKey_GetPrivLen(IntPtr key, ref int len);
579579
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
@@ -2982,18 +2982,15 @@ public static IntPtr MlKemMakeKey(MlKemTypes type, IntPtr heap, int devId)
29822982
/// <returns>0 on success, negative value on error.</returns>
29832983
public static int MlKemFreeKey(ref IntPtr key)
29842984
{
2985-
int ret = 0;
2985+
int ret;
29862986

29872987
if (key == IntPtr.Zero)
29882988
{
29892989
return BAD_FUNC_ARG;
29902990
}
29912991

2992-
if (key != IntPtr.Zero)
2993-
{
2994-
ret = wc_MlKemKey_Delete(key, IntPtr.Zero);
2995-
key = IntPtr.Zero;
2996-
}
2992+
ret = wc_MlKemKey_Delete(key, IntPtr.Zero);
2993+
key = IntPtr.Zero;
29972994
return ret;
29982995
}
29992996

@@ -3017,10 +3014,10 @@ public static int MlKemEncodePublicKey(IntPtr key, out byte[] publicKey)
30173014
try
30183015
{
30193016
ret = wc_MlKemKey_PublicKeySize(key, ref pubLen);
3020-
if (ret !=0 || pubLen == 0)
3017+
if (ret != 0 || pubLen == 0)
30213018
{
30223019
log(ERROR_LOG, "Failed to get MlKem public key length. Error code: " + ret);
3023-
return ret;
3020+
return (ret != 0) ? ret : BAD_FUNC_ARG;
30243021
}
30253022
if (pubLen > int.MaxValue)
30263023
{
@@ -3066,10 +3063,10 @@ public static int MlKemEncodePrivateKey(IntPtr key, out byte[] privateKey)
30663063
try
30673064
{
30683065
ret = wc_MlKemKey_PrivateKeySize(key, ref privLen);
3069-
if (ret !=0 || privLen == 0)
3066+
if (ret != 0 || privLen == 0)
30703067
{
30713068
log(ERROR_LOG, "Failed to get MlKem private key length. Error code: " + ret);
3072-
return ret;
3069+
return (ret != 0) ? ret : BAD_FUNC_ARG;
30733070
}
30743071
if (privLen > int.MaxValue)
30753072
{
@@ -3123,14 +3120,14 @@ public static int MlKemDecodePublicKey(IntPtr key, byte[] publicKey)
31233120
if (ret != 0 || pubLen == 0)
31243121
{
31253122
log(ERROR_LOG, "Failed to get MlKem public key length. Error code: " + ret);
3126-
return ret;
3123+
return (ret != 0) ? ret : BAD_FUNC_ARG;
31273124
}
31283125
if ((uint)publicKey.Length != pubLen)
3129-
{
3130-
log(ERROR_LOG, "MlKem public key buffer length mismatch. Expected: " +
3131-
pubLen + ", actual: " + publicKey.Length);
3132-
return BUFFER_E;
3133-
}
3126+
{
3127+
log(ERROR_LOG, "MlKem public key buffer length mismatch. Expected: " +
3128+
pubLen + ", actual: " + publicKey.Length);
3129+
return BUFFER_E;
3130+
}
31343131

31353132
ret = wc_MlKemKey_DecodePublicKey(key, publicKey, pubLen);
31363133
if (ret != 0)
@@ -3172,12 +3169,12 @@ public static int MlKemDecodePrivateKey(IntPtr key, byte[] privateKey)
31723169
try
31733170
{
31743171
ret = wc_MlKemKey_PrivateKeySize(key, ref privLen);
3175-
if (privLen == 0)
3172+
if (ret != 0 || privLen == 0)
31763173
{
31773174
log(ERROR_LOG, "Failed to get MlKem private key length. Error code: " + ret);
3178-
return ret;
3175+
return (ret != 0) ? ret : BAD_FUNC_ARG;
31793176
}
3180-
3177+
31813178
if ((uint)privateKey.Length != privLen)
31823179
{
31833180
log(ERROR_LOG, "MlKem private key buffer length mismatch. Required: " + privLen +
@@ -3416,18 +3413,15 @@ public static IntPtr DilithiumMakeKey(IntPtr heap, int devId, MlDsaLevels level)
34163413
/// <returns>0 on success, negative value on error.</returns>
34173414
public static int DilithiumFreeKey(ref IntPtr key)
34183415
{
3419-
int ret = 0;
3416+
int ret;
34203417

34213418
if (key == IntPtr.Zero)
34223419
{
34233420
return BAD_FUNC_ARG;
34243421
}
34253422

3426-
if (key != IntPtr.Zero)
3427-
{
3428-
ret = wc_dilithium_delete(key, IntPtr.Zero);
3429-
key = IntPtr.Zero;
3430-
}
3423+
ret = wc_dilithium_delete(key, IntPtr.Zero);
3424+
key = IntPtr.Zero;
34313425
return ret;
34323426
}
34333427

@@ -3500,10 +3494,10 @@ public static int DilithiumExportPrivateKey(IntPtr key, out byte[] privateKey)
35003494
try
35013495
{
35023496
ret = wc_MlDsaKey_GetPrivLen(key, ref privLen);
3503-
if (privLen <= 0)
3497+
if (ret != 0 || privLen <= 0)
35043498
{
35053499
log(ERROR_LOG, "Failed to get Dilithium private key length. Error code: " + ret);
3506-
return ret;
3500+
return (ret != 0) ? ret : BAD_FUNC_ARG;
35073501
}
35083502

35093503
privateKey = new byte[privLen];
@@ -3550,10 +3544,10 @@ public static int DilithiumExportPublicKey(IntPtr key, out byte[] publicKey)
35503544
try
35513545
{
35523546
ret = wc_MlDsaKey_GetPubLen(key, ref pubLen);
3553-
if (pubLen <= 0)
3547+
if (ret != 0 || pubLen <= 0)
35543548
{
35553549
log(ERROR_LOG, "Failed to get Dilithium public key length. Error code: " + ret);
3556-
return ret;
3550+
return (ret != 0) ? ret : BAD_FUNC_ARG;
35573551
}
35583552

35593553
publicKey = new byte[pubLen];
@@ -3602,10 +3596,10 @@ public static int DilithiumSignMsg(IntPtr key, byte[] msg, out byte[] sig)
36023596
try
36033597
{
36043598
ret = wc_MlDsaKey_GetSigLen(key, ref sigLen);
3605-
if (sigLen <= 0)
3599+
if (ret != 0 || sigLen <= 0)
36063600
{
36073601
log(ERROR_LOG, "Failed to get Dilithium signature length. Error code: " + ret);
3608-
return ret;
3602+
return (ret != 0) ? ret : BAD_FUNC_ARG;
36093603
}
36103604

36113605
sig = new byte[sigLen];
@@ -3614,9 +3608,10 @@ public static int DilithiumSignMsg(IntPtr key, byte[] msg, out byte[] sig)
36143608
if (rng == IntPtr.Zero)
36153609
{
36163610
log(ERROR_LOG, "Failed to create RNG for Dilithium signing.");
3617-
return EXCEPTION_E;
3611+
return MEMORY_E;
36183612
}
3619-
ret = wc_dilithium_sign_msg(msg, (uint)msg.Length, sig, ref outLen, key, rng);
3613+
/* FIPS 204 sign with empty context (ctx=null, ctxLen=0). */
3614+
ret = wc_dilithium_sign_ctx_msg(null, 0, msg, (uint)msg.Length, sig, ref outLen, key, rng);
36203615
if (ret != 0)
36213616
{
36223617
log(ERROR_LOG, "Failed to sign message with Dilithium key. Error code: " + ret);
@@ -3660,7 +3655,8 @@ public static int DilithiumVerifyMsg(IntPtr key, byte[] msg, byte[] sig)
36603655

36613656
try
36623657
{
3663-
ret = wc_dilithium_verify_msg(sig, (uint)sig.Length, msg, (uint)msg.Length, ref res, key);
3658+
/* FIPS 204 verify with empty context (ctx=null, ctxLen=0). */
3659+
ret = wc_dilithium_verify_ctx_msg(sig, (uint)sig.Length, null, 0, msg, (uint)msg.Length, ref res, key);
36643660
if (ret != 0)
36653661
{
36663662
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)