-
Notifications
You must be signed in to change notification settings - Fork 948
add ML-KEM/ML-DSA support for C# wrapper #9994
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -674,6 +674,299 @@ private static void curve25519_test() | |
| if (publicKeyB != IntPtr.Zero) wolfcrypt.Curve25519FreeKey(publicKeyB); | ||
| } /* END curve25519_test */ | ||
|
|
||
| private static void mlkem_test(wolfcrypt.MlKemTypes type) | ||
| { | ||
| int ret = 0; | ||
| IntPtr keyA = IntPtr.Zero; | ||
| IntPtr keyB = IntPtr.Zero; | ||
| IntPtr heap = IntPtr.Zero; | ||
| int devId = wolfcrypt.INVALID_DEVID; | ||
| byte[] pubA = null; | ||
| byte[] privA = null; | ||
| byte[] cipherText = null; | ||
| byte[] sharedSecretA = null; | ||
| byte[] sharedSecretB = null; | ||
|
|
||
| try | ||
| { | ||
| Console.WriteLine("\nStarting " + type + " shared secret test ..."); | ||
|
|
||
| /* Generate Key Pair */ | ||
| Console.WriteLine("Testing ML-KEM Key Generation..."); | ||
|
|
||
| Console.WriteLine("Generate Key Pair A..."); | ||
| keyA = wolfcrypt.MlKemMakeKey(type, heap, devId); | ||
| if (keyA == IntPtr.Zero) | ||
| { | ||
| ret = -1; | ||
| Console.Error.WriteLine("Failed to generate key pair A."); | ||
| } | ||
| if (ret == 0) | ||
| { | ||
| Console.WriteLine("Generate Key Pair B..."); | ||
| keyB = wolfcrypt.MlKemMakeKey(type, heap, devId); | ||
| if (keyB == IntPtr.Zero) | ||
| { | ||
| ret = -1; | ||
| Console.Error.WriteLine("Failed to generate key pair B."); | ||
| } | ||
| } | ||
| if (ret == 0) | ||
| { | ||
| Console.WriteLine("ML-KEM Key Generation test passed."); | ||
| } | ||
|
|
||
| /* Encode */ | ||
| if (ret == 0) | ||
| { | ||
| Console.WriteLine("Testing ML-KEM Key Encode..."); | ||
| ret = wolfcrypt.MlKemEncodePublicKey(keyA, out pubA); | ||
| if (ret != 0) | ||
| { | ||
| Console.Error.WriteLine($"Failed to encode public key of A. Error code: {ret}"); | ||
| } | ||
| } | ||
| if (ret == 0) | ||
| { | ||
| ret = wolfcrypt.MlKemEncodePrivateKey(keyA, out privA); | ||
| if (ret != 0) | ||
| { | ||
| Console.Error.WriteLine($"Failed to encode private key of A. Error code: {ret}"); | ||
| } | ||
| } | ||
| if (ret == 0) | ||
| { | ||
| Console.WriteLine("ML-KEM Key Encode test passed."); | ||
| } | ||
|
|
||
| /* Encapsulate */ | ||
| if (ret == 0) | ||
| { | ||
| Console.WriteLine("Testing ML-KEM Encapsulation..."); | ||
| ret = wolfcrypt.MlKemEncapsulate(keyA, out cipherText, out sharedSecretA); | ||
| if (ret != 0) | ||
| { | ||
| Console.Error.WriteLine($"Failed to encapsulate. Error code: {ret}"); | ||
| } | ||
| } | ||
| if (ret == 0) | ||
| { | ||
| Console.WriteLine("ML-KEM Encapsulation test passed."); | ||
| } | ||
|
|
||
| /* Decode */ | ||
| if (ret == 0) | ||
| { | ||
| Console.WriteLine("Testing ML-KEM Decode..."); | ||
| ret = wolfcrypt.MlKemDecodePrivateKey(keyB, privA); | ||
| if (ret != 0) | ||
| { | ||
| Console.Error.WriteLine($"Failed to decode private key of A. Error code: {ret}"); | ||
| } | ||
| } | ||
| if (ret == 0) | ||
| { | ||
| ret = wolfcrypt.MlKemDecodePublicKey(keyB, pubA); | ||
| if (ret != 0) | ||
| { | ||
| Console.Error.WriteLine($"Failed to decode public key of B. Error code: {ret}"); | ||
| } | ||
| } | ||
| if (ret == 0) | ||
| { | ||
| Console.WriteLine("ML-KEM Decode test passed."); | ||
| } | ||
|
|
||
| /* Decapsulate */ | ||
| if (ret == 0) | ||
| { | ||
| Console.WriteLine("Testing ML-KEM Decapsulation..."); | ||
| ret = wolfcrypt.MlKemDecapsulate(keyB, cipherText, out sharedSecretB); | ||
| if (ret != 0) | ||
| { | ||
| Console.Error.WriteLine($"Failed to decapsulate. Error code: {ret}"); | ||
| } | ||
| } | ||
| if (ret == 0) | ||
| { | ||
| Console.WriteLine("ML-KEM Decapsulation test passed."); | ||
| } | ||
|
|
||
| /* Check */ | ||
| if (ret == 0) | ||
| { | ||
| Console.WriteLine("Comparing Shared Secrets..."); | ||
| if (!wolfcrypt.ByteArrayVerify(sharedSecretA, sharedSecretB)) | ||
| { | ||
| ret = -1; | ||
| Console.Error.WriteLine($"Shared secrets do not match. Error code: {ret}"); | ||
| } | ||
| } | ||
| if (ret == 0) | ||
| { | ||
| Console.WriteLine("ML-KEM shared secret match."); | ||
| } | ||
|
|
||
| if (ret != 0) | ||
| { | ||
| throw new Exception("ML-KEM test failed."); | ||
| } | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| Console.WriteLine($"ML-KEM test failed: {ex.Message}"); | ||
tamasan238 marked this conversation as resolved.
Show resolved
Hide resolved
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is strange. This API has already been added in #9039 .
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 1️⃣ cp wrapper/CSharp/user_settings.h .
./configure --enable-usersettingsI confirmed that ML-KEM/ML-DSA is not enabled in the above steps. 2️⃣ cp wrapper/CSharp/user_settings.h .
./configure --enable-usersettings
make
make checkI confirmed that this fails.
I would like to investigate these as well, but my knowledge in these areas is not extensive. |
||
| throw; | ||
| } | ||
| finally | ||
| { | ||
| /* Cleanup */ | ||
| if (keyA != IntPtr.Zero) | ||
| { | ||
| ret = wolfcrypt.MlKemFreeKey(ref keyA); | ||
| if (ret != 0) | ||
tamasan238 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| Console.Error.WriteLine($"Failed to free MlKem key A. Error code: {ret}"); | ||
| } | ||
| } | ||
| if (keyB != IntPtr.Zero) | ||
| { | ||
| ret = wolfcrypt.MlKemFreeKey(ref keyB); | ||
| if (ret != 0) | ||
tamasan238 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| Console.Error.WriteLine($"Failed to free MlKem key B. Error code: {ret}"); | ||
| } | ||
| } | ||
| } | ||
| } /* END mlkem_test */ | ||
|
|
||
| private static void mldsa_test(wolfcrypt.MlDsaLevels level) | ||
| { | ||
| int ret = 0; | ||
| IntPtr key = IntPtr.Zero; | ||
| IntPtr heap = IntPtr.Zero; | ||
| int devId = wolfcrypt.INVALID_DEVID; | ||
| byte[] privateKey = null; | ||
| byte[] publicKey = null; | ||
| byte[] message = Encoding.UTF8.GetBytes("This is some data to sign with ML-DSA"); | ||
| byte[] signature = null; | ||
|
|
||
| try | ||
| { | ||
| Console.WriteLine("\nStarting " + level + " key generation and signature test ..."); | ||
|
|
||
| /* Generate Key */ | ||
| Console.WriteLine("Testing ML-DSA Key Generation..."); | ||
| key = wolfcrypt.DilithiumMakeKey(heap, devId, level); | ||
| if (key == IntPtr.Zero) | ||
| { | ||
| ret = -1; | ||
| Console.Error.WriteLine("Failed to generate keypair."); | ||
| } | ||
| if (ret == 0) | ||
| { | ||
| Console.WriteLine("ML-DSA Key Generation test passed."); | ||
| } | ||
|
|
||
| /* Export */ | ||
| if (ret == 0) | ||
| { | ||
| Console.WriteLine("Testing ML-DSA Key Export..."); | ||
| ret = DilithiumExportPrivateKey(key, out privateKey); | ||
| if (ret != 0) | ||
| { | ||
| Console.Error.WriteLine($"Failed to export private key. Error code: {ret}"); | ||
| } | ||
| } | ||
| if (ret == 0) | ||
| { | ||
| ret = DilithiumExportPublicKey(key, out publicKey); | ||
| if (ret != 0) | ||
| { | ||
| Console.Error.WriteLine($"Failed to export public key. Error code: {ret}"); | ||
| } | ||
| } | ||
| if (ret == 0) | ||
| { | ||
| Console.WriteLine("ML-DSA Key Export test passed."); | ||
| } | ||
|
|
||
| /* Import */ | ||
| if (ret == 0) | ||
| { | ||
| Console.WriteLine("Testing ML-DSA Key Import..."); | ||
| ret = DilithiumImportPrivateKey(privateKey, key); | ||
| if (ret != 0) | ||
| { | ||
| Console.Error.WriteLine($"Failed to import private key. Error code: {ret}"); | ||
| } | ||
| } | ||
| if (ret == 0) | ||
| { | ||
| ret = DilithiumImportPublicKey(publicKey, key); | ||
| if (ret != 0) | ||
| { | ||
| Console.Error.WriteLine($"Failed to import public key. Error code: {ret}"); | ||
| } | ||
| } | ||
| if (ret == 0) | ||
| { | ||
| Console.WriteLine("ML-DSA Key Import test passed."); | ||
| } | ||
|
|
||
| /* Sign */ | ||
| if (ret == 0) | ||
| { | ||
| Console.WriteLine("Testing ML-DSA Signature Creation..."); | ||
| ret = wolfcrypt.DilithiumSignMsg(key, message, out signature); | ||
| if (ret != 0) | ||
| { | ||
| Console.Error.WriteLine($"Failed to sign. Error code: {ret}"); | ||
| } | ||
| } | ||
| if (ret == 0) | ||
| { | ||
| Console.WriteLine($"ML-DSA Signature Creation test passed. Signature Length: {signature.Length}"); | ||
| } | ||
|
|
||
| /* Verify */ | ||
| if (ret == 0) | ||
| { | ||
| Console.WriteLine("Testing ML-DSA Signature Verification..."); | ||
| ret = wolfcrypt.DilithiumVerifyMsg(key, message, signature); | ||
| if (ret != 0) | ||
| { | ||
| Console.Error.WriteLine($"Failed to verify message. Error code: {ret}"); | ||
| } | ||
| } | ||
| if (ret == 0) | ||
| { | ||
| Console.WriteLine("ML-DSA Signature Verification test passed."); | ||
| } | ||
|
|
||
| if (ret != 0) | ||
| { | ||
| throw new Exception("ML-DSA test failed."); | ||
| } | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| Console.WriteLine($"ML-DSA test failed: {ex.Message}"); | ||
tamasan238 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| throw; | ||
| } | ||
| finally | ||
| { | ||
| if (key != IntPtr.Zero) | ||
| { | ||
| ret = wolfcrypt.DilithiumFreeKey(ref key); | ||
| if (ret != 0) | ||
tamasan238 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| Console.Error.WriteLine($"Failed to free ML-DSA key. Error code: {ret}"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } /* END mldsa_test */ | ||
|
|
||
| private static void aes_gcm_test() | ||
| { | ||
| IntPtr aes = IntPtr.Zero; | ||
|
|
@@ -930,6 +1223,18 @@ public static void Main(string[] args) | |
|
|
||
| curve25519_test(); /* curve25519 shared secret test */ | ||
|
|
||
| Console.WriteLine("\nStarting ML-KEM test"); | ||
|
|
||
| mlkem_test(wolfcrypt.MlKemTypes.ML_KEM_512); /* ML-KEM test */ | ||
| mlkem_test(wolfcrypt.MlKemTypes.ML_KEM_768); /* ML-KEM test */ | ||
| mlkem_test(wolfcrypt.MlKemTypes.ML_KEM_1024); /* ML-KEM test */ | ||
|
|
||
| Console.WriteLine("\nStarting ML-DSA test"); | ||
|
|
||
| mldsa_test(wolfcrypt.MlDsaLevels.ML_DSA_44); /* ML-DSA test */ | ||
| mldsa_test(wolfcrypt.MlDsaLevels.ML_DSA_65); /* ML-DSA test */ | ||
| mldsa_test(wolfcrypt.MlDsaLevels.ML_DSA_87); /* ML-DSA test */ | ||
|
|
||
| Console.WriteLine("\nStarting AES-GCM test"); | ||
|
|
||
| aes_gcm_test(); /* AES_GCM test */ | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.