Skip to content

Commit b6686a3

Browse files
committed
add ML-KEM/ML-DSA support for C# wrapper
1 parent a1e2ba2 commit b6686a3

File tree

4 files changed

+1063
-0
lines changed

4 files changed

+1063
-0
lines changed

wrapper/CSharp/user_settings.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,16 @@
8585
#define ECC_TIMING_RESISTANT
8686
#define HAVE_COMP_KEY
8787

88+
/* Enable ML-KEM, ML-DSA */
89+
#define HAVE_MLKEM
90+
#define WOLFSSL_WC_MLKEM
91+
#define WOLFSSL_HAVE_MLKEM
92+
#define WOLFSSL_DTLS_CH_FRAG
93+
#define HAVE_DILITHIUM
94+
#define WOLFSSL_WC_DILITHIUM
95+
#define WOLFSSL_SHAKE128
96+
#define WOLFSSL_SHAKE256
97+
8898
/* Disable features */
8999
#define NO_PSK
90100

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

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,170 @@ private static void curve25519_test()
648648
if (publicKeyB != IntPtr.Zero) wolfcrypt.Curve25519FreeKey(publicKeyB);
649649
} /* END curve25519_test */
650650

651+
private static void mlkem_test(wolfcrypt.MlKemTypes type)
652+
{
653+
int ret;
654+
IntPtr keyA = IntPtr.Zero;
655+
IntPtr keyB = IntPtr.Zero;
656+
IntPtr heap = IntPtr.Zero;
657+
int devId = wolfcrypt.INVALID_DEVID;
658+
byte[] pubA, privA, cipherText, sharedSecretA, sharedSecretB;
659+
660+
Console.WriteLine("\nStarting " + type + " shared secret test ...");
661+
662+
/* Generate Key Pair */
663+
Console.WriteLine("Testing ML-KEM Key Generation...");
664+
665+
Console.WriteLine("Generate Key Pair A...");
666+
keyA = wolfcrypt.MlKemMakeKey(type, heap, devId);
667+
if (keyA == IntPtr.Zero)
668+
{
669+
throw new Exception("Failed to generate key pair A.");
670+
}
671+
672+
Console.WriteLine("Generate Key Pair B...");
673+
keyB = wolfcrypt.MlKemMakeKey(type, heap, devId);
674+
if (keyB == IntPtr.Zero)
675+
{
676+
throw new Exception("Failed to generate key pair B.");
677+
}
678+
679+
Console.WriteLine("ML-KEM Key Generation test passed.");
680+
681+
/* Encode */
682+
Console.WriteLine("Testing ML-KEM Key Encode...");
683+
ret = wolfcrypt.MlKemEncodePublicKey(keyA, out pubA);
684+
if (ret != 0)
685+
{
686+
throw new Exception("Failed to encode public key of A.");
687+
}
688+
ret = wolfcrypt.MlKemEncodePrivateKey(keyA, out privA);
689+
if (ret != 0)
690+
{
691+
throw new Exception("Failed to encode private key of A.");
692+
}
693+
Console.WriteLine("ML-KEM Key Encode test passed.");
694+
695+
/* Encapsulate */
696+
Console.WriteLine("Testing ML-KEM Encapsulation...");
697+
ret = wolfcrypt.MlKemEncapsulate(keyA, out cipherText, out sharedSecretA);
698+
if (ret != 0)
699+
{
700+
throw new Exception("Failed to encapsulate.");
701+
}
702+
Console.WriteLine("ML-KEM Encapsulation test passed.");
703+
704+
/* Decode */
705+
Console.WriteLine("Testing ML-KEM Decode...");
706+
ret = wolfcrypt.MlKemDecodePrivateKey(keyB, privA);
707+
if (ret != 0)
708+
{
709+
throw new Exception("Failed to decode private key of A.");
710+
}
711+
ret = wolfcrypt.MlKemDecodePublicKey(keyB, pubA);
712+
if (ret != 0)
713+
{
714+
throw new Exception("Failed to decode public key of B.");
715+
}
716+
Console.WriteLine("ML-KEM Decode test passed.");
717+
718+
/* Decapsulate */
719+
Console.WriteLine("Testing ML-KEM Decapsulation...");
720+
ret = wolfcrypt.MlKemDecapsulate(keyB, cipherText, out sharedSecretB);
721+
if (ret != 0)
722+
{
723+
throw new Exception("Failed to decapsulate.");
724+
}
725+
Console.WriteLine("ML-KEM Decapsulation test passed.");
726+
727+
/* Check */
728+
Console.WriteLine("Comparing Shared Secrets...");
729+
if (!wolfcrypt.ByteArrayVerify(sharedSecretA, sharedSecretB))
730+
{
731+
throw new Exception("Shared secrets do not match.");
732+
}
733+
else
734+
{
735+
Console.WriteLine("ML-KEM shared secret match.");
736+
}
737+
738+
/* Cleanup */
739+
if (keyA != IntPtr.Zero) wolfcrypt.MlKemFreeKey(keyA);
740+
if (keyB != IntPtr.Zero) wolfcrypt.MlKemFreeKey(keyB);
741+
} /* END mlkem_test */
742+
743+
private static void mldsa_test(wolfcrypt.MlDsaTypes type)
744+
{
745+
int ret;
746+
IntPtr key = IntPtr.Zero;
747+
IntPtr heap = IntPtr.Zero;
748+
int devId = wolfcrypt.INVALID_DEVID;
749+
byte[] privateKey;
750+
byte[] publicKey;
751+
byte[] message = Encoding.UTF8.GetBytes("This is some data to sign with ML-DSA");
752+
byte[] signature;
753+
754+
Console.WriteLine("\nStarting " + type + " shared secret test ...");
755+
756+
/* Generate Key Pair */
757+
Console.WriteLine("Testing ML-DSA Key Generation...");
758+
key = wolfcrypt.DilithiumMakeKey(heap, devId, type);
759+
if (key == IntPtr.Zero)
760+
{
761+
throw new Exception("DilithiumMakeKey failed");
762+
}
763+
Console.WriteLine("ML-DSA Key Generation test passed.");
764+
765+
/* Export */
766+
Console.WriteLine("Testing ML-DSA Key Export...");
767+
ret = DilithiumExportPrivateKey(key, out privateKey);
768+
if (ret != 0)
769+
{
770+
throw new Exception("DilithiumExportPrivateKey failed");
771+
}
772+
ret = DilithiumExportPublicKey(key, out publicKey);
773+
if (ret != 0)
774+
{
775+
throw new Exception("DilithiumExportPublicKey failed");
776+
}
777+
Console.WriteLine("ML-DSA Key Export test passed.");
778+
779+
/* Import */
780+
Console.WriteLine("Testing ML-DSA Key Import...");
781+
ret = DilithiumImportPrivateKey(privateKey, key);
782+
if (ret != 0)
783+
{
784+
throw new Exception("DilithiumImportPrivateKey failed");
785+
}
786+
ret = DilithiumImportPublicKey(publicKey, key);
787+
if (ret != 0)
788+
{
789+
throw new Exception("DilithiumImportPrivateKey failed");
790+
}
791+
Console.WriteLine("ML-DSA Key Import test passed.");
792+
793+
/* Sign */
794+
Console.WriteLine("Testing ML-DSA Signature Creation...");
795+
ret = wolfcrypt.DilithiumSignMsg(key, message, out signature);
796+
if (ret != 0)
797+
{
798+
throw new Exception("DilithiumSign failed");
799+
}
800+
Console.WriteLine($"ML-DSA Signature Creation test passed. Signature Length: {signature.Length}");
801+
802+
/* Verify */
803+
Console.WriteLine("Testing ML-DSA Signature Verification...");
804+
ret = wolfcrypt.DilithiumVerifyMsg(key, message, signature);
805+
if (ret != 0)
806+
{
807+
throw new Exception("DilithiumVerify failed");
808+
}
809+
Console.WriteLine("ML-DSA Signature Verification test passed.");
810+
811+
if (key != IntPtr.Zero) wolfcrypt.DilithiumFreeKey(key);
812+
813+
} /* END mldsa_test */
814+
651815
private static void aes_gcm_test()
652816
{
653817
IntPtr aes = IntPtr.Zero;
@@ -904,6 +1068,18 @@ public static void Main(string[] args)
9041068

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

1071+
Console.WriteLine("\nStarting ML-KEM test");
1072+
1073+
mlkem_test(wolfcrypt.MlKemTypes.ML_KEM_512); /* ML-KEM test */
1074+
mlkem_test(wolfcrypt.MlKemTypes.ML_KEM_768); /* ML-KEM test */
1075+
mlkem_test(wolfcrypt.MlKemTypes.ML_KEM_1024); /* ML-KEM test */
1076+
1077+
Console.WriteLine("\nStarting ML-DSA test");
1078+
1079+
mldsa_test(wolfcrypt.MlDsaTypes.ML_DSA_44); /* ML-DSA test */
1080+
mldsa_test(wolfcrypt.MlDsaTypes.ML_DSA_65); /* ML-DSA test */
1081+
mldsa_test(wolfcrypt.MlDsaTypes.ML_DSA_87); /* ML-DSA test */
1082+
9071083
Console.WriteLine("\nStarting AES-GCM test");
9081084

9091085
aes_gcm_test(); /* AES_GCM test */

0 commit comments

Comments
 (0)