Skip to content

Commit 5f124a9

Browse files
committed
Peer review fixes
1 parent 72c57dc commit 5f124a9

3 files changed

Lines changed: 87 additions & 33 deletions

File tree

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -703,12 +703,12 @@ private static void mlkem_test(wolfcrypt.MlKemTypes type)
703703
}
704704
if (ret == 0)
705705
{
706-
Console.WriteLine("Generate Key Pair B...");
707-
keyB = wolfcrypt.MlKemMakeKey(type, heap, devId);
706+
Console.WriteLine("Initialize Key B for decode...");
707+
keyB = wolfcrypt.MlKemNew(type, heap, devId);
708708
if (keyB == IntPtr.Zero)
709709
{
710710
ret = -1;
711-
Console.Error.WriteLine("Failed to generate key pair B.");
711+
Console.Error.WriteLine("Failed to initialize key B for decode.");
712712
}
713713
}
714714
if (ret == 0)
@@ -769,7 +769,7 @@ private static void mlkem_test(wolfcrypt.MlKemTypes type)
769769
ret = wolfcrypt.MlKemDecodePublicKey(keyB, pubA);
770770
if (ret != 0)
771771
{
772-
Console.Error.WriteLine($"Failed to decode public key of B. Error code: {ret}");
772+
Console.Error.WriteLine($"Failed to decode public key of A. Error code: {ret}");
773773
}
774774
}
775775
if (ret == 0)

wrapper/CSharp/wolfSSL_CSharp/wolfCrypt.cs

Lines changed: 79 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -457,22 +457,14 @@ public class wolfcrypt
457457
[DllImport(wolfssl_dll)]
458458
private static extern int wc_MlKemKey_Delete(IntPtr key, IntPtr key_p);
459459
[DllImport(wolfssl_dll)]
460-
private static extern int wc_MlKemKey_Init(IntPtr key, int type, IntPtr heap, int devId);
461-
[DllImport(wolfssl_dll)]
462-
private static extern int wc_MlKemKey_Free(IntPtr key);
463-
[DllImport(wolfssl_dll)]
464460
private static extern int wc_MlKemKey_MakeKey(IntPtr key, IntPtr rng);
465461
[DllImport(wolfssl_dll)]
466-
private static extern int wc_MlKemKey_MakeKeyWithRandom(IntPtr key, byte[] rand, int len);
467-
[DllImport(wolfssl_dll)]
468462
private static extern int wc_MlKemKey_EncodePublicKey(IntPtr key, byte[] output, uint len);
469463
[DllImport(wolfssl_dll)]
470464
private static extern int wc_MlKemKey_DecodePublicKey(IntPtr key, byte[] input, uint len);
471465
[DllImport(wolfssl_dll)]
472466
private static extern int wc_MlKemKey_Encapsulate(IntPtr key, byte[] ct, byte[] ss, IntPtr rng);
473467
[DllImport(wolfssl_dll)]
474-
private static extern int wc_MlKemKey_EncapsulateWithRandom(IntPtr key, byte[] ct, byte[] ss, byte[] rand, int len);
475-
[DllImport(wolfssl_dll)]
476468
private static extern int wc_MlKemKey_Decapsulate(IntPtr key, byte[] ss, byte[] ct, uint len);
477469
[DllImport(wolfssl_dll)]
478470
private static extern int wc_MlKemKey_EncodePrivateKey(IntPtr key, byte[] output, uint len);
@@ -492,22 +484,14 @@ public class wolfcrypt
492484
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
493485
private static extern int wc_MlKemKey_Delete(IntPtr key, IntPtr key_p);
494486
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
495-
private static extern int wc_MlKemKey_Init(IntPtr key, int type, IntPtr heap, int devId);
496-
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
497-
private static extern int wc_MlKemKey_Free(IntPtr key);
498-
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
499487
private static extern int wc_MlKemKey_MakeKey(IntPtr key, IntPtr rng);
500488
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
501-
private static extern int wc_MlKemKey_MakeKeyWithRandom(IntPtr key, byte[] rand, int len);
502-
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
503489
private static extern int wc_MlKemKey_EncodePublicKey(IntPtr key, byte[] output, uint len);
504490
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
505491
private static extern int wc_MlKemKey_DecodePublicKey(IntPtr key, byte[] input, uint len);
506492
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
507493
private static extern int wc_MlKemKey_Encapsulate(IntPtr key, byte[] ct, byte[] ss, IntPtr rng);
508494
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
509-
private static extern int wc_MlKemKey_EncapsulateWithRandom(IntPtr key, byte[] ct, byte[] ss, byte[] rand, int len);
510-
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
511495
private static extern int wc_MlKemKey_Decapsulate(IntPtr key, byte[] ss, byte[] ct, uint len);
512496
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
513497
private static extern int wc_MlKemKey_EncodePrivateKey(IntPtr key, byte[] output, uint len);
@@ -524,12 +508,8 @@ public class wolfcrypt
524508
[DllImport(wolfssl_dll)]
525509
private static extern int wc_dilithium_delete(IntPtr key, IntPtr key_p);
526510
[DllImport(wolfssl_dll)]
527-
private static extern int wc_dilithium_init_ex(IntPtr key, IntPtr heap, int devId);
528-
[DllImport(wolfssl_dll)]
529511
private static extern int wc_dilithium_set_level(IntPtr key, byte level);
530512
[DllImport(wolfssl_dll)]
531-
private static extern void wc_dilithium_free(IntPtr key);
532-
[DllImport(wolfssl_dll)]
533513
private static extern int wc_dilithium_make_key(IntPtr key, IntPtr rng);
534514
[DllImport(wolfssl_dll)]
535515
private static extern int wc_dilithium_export_private(IntPtr key, byte[] output, ref uint outLen);
@@ -555,12 +535,8 @@ public class wolfcrypt
555535
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
556536
private static extern int wc_dilithium_delete(IntPtr key, IntPtr key_p);
557537
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
558-
private static extern int wc_dilithium_init_ex(IntPtr key, IntPtr heap, int devId);
559-
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
560538
private static extern int wc_dilithium_set_level(IntPtr key, byte level);
561539
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
562-
private static extern void wc_dilithium_free(IntPtr key);
563-
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
564540
private static extern int wc_dilithium_make_key(IntPtr key, IntPtr rng);
565541
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
566542
private static extern int wc_dilithium_export_private(IntPtr key, byte[] output, ref uint outLen);
@@ -2913,6 +2889,33 @@ public static void Curve25519ExportKeyRaw(IntPtr key, out byte[] privateKey, out
29132889
// These APIs work by adding several options to wolfCrypt.
29142890
// Please refer to `../user_settings.h`.
29152891

2892+
/// <summary>
2893+
/// Allocate and initialize a new ML-KEM key without generating key
2894+
/// material. Use this when you intend to import or decode an existing
2895+
/// key (e.g., before calling MlKemDecodePublicKey/MlKemDecodePrivateKey).
2896+
/// </summary>
2897+
/// <param name="type">ML-KEM parameter set type</param>
2898+
/// <param name="heap">Heap pointer for memory allocation</param>
2899+
/// <param name="devId">Device ID (if applicable)</param>
2900+
/// <returns>Pointer to the MlKem key structure, or IntPtr.Zero on failure</returns>
2901+
public static IntPtr MlKemNew(MlKemTypes type, IntPtr heap, int devId)
2902+
{
2903+
try
2904+
{
2905+
IntPtr key = wc_MlKemKey_New((int)type, heap, devId);
2906+
if (key == IntPtr.Zero)
2907+
{
2908+
log(ERROR_LOG, "Failed to allocate or initialize MlKem key.");
2909+
}
2910+
return key;
2911+
}
2912+
catch (Exception ex)
2913+
{
2914+
log(ERROR_LOG, "MlKem key allocation exception: " + ex.ToString());
2915+
return IntPtr.Zero;
2916+
}
2917+
}
2918+
29162919
/// <summary>
29172920
/// Create a new ML-KEM key pair and initialize it with random values
29182921
/// </summary>
@@ -3337,6 +3340,58 @@ public enum MlKemTypes
33373340
// These APIs work by adding several options to wolfCrypt.
33383341
// Please refer to `../user_settings.h`.
33393342

3343+
/// <summary>
3344+
/// Allocate and initialize a new Dilithium key (with level set) without
3345+
/// generating key material. Use this when you intend to import an
3346+
/// existing key (e.g., before calling DilithiumImportPublicKey or
3347+
/// DilithiumImportPrivateKey).
3348+
/// </summary>
3349+
/// <param name="heap">Heap pointer for memory allocation</param>
3350+
/// <param name="devId">Device ID (if applicable)</param>
3351+
/// <param name="level">Dilithium security level</param>
3352+
/// <returns>Pointer to the Dilithium key structure, or IntPtr.Zero on failure</returns>
3353+
public static IntPtr DilithiumNew(IntPtr heap, int devId, MlDsaLevels level)
3354+
{
3355+
IntPtr key = IntPtr.Zero;
3356+
bool success = false;
3357+
3358+
try
3359+
{
3360+
key = wc_dilithium_new(heap, devId);
3361+
if (key == IntPtr.Zero)
3362+
{
3363+
log(ERROR_LOG, "Failed to allocate and initialize Dilithium key.");
3364+
return IntPtr.Zero;
3365+
}
3366+
3367+
int ret = wc_dilithium_set_level(key, (byte)level);
3368+
if (ret != 0)
3369+
{
3370+
log(ERROR_LOG, "Failed to set Dilithium level. Error code: " + ret);
3371+
return IntPtr.Zero;
3372+
}
3373+
3374+
success = true;
3375+
return key;
3376+
}
3377+
catch (Exception ex)
3378+
{
3379+
log(ERROR_LOG, "Dilithium key allocation exception: " + ex.ToString());
3380+
return IntPtr.Zero;
3381+
}
3382+
finally
3383+
{
3384+
if (!success && key != IntPtr.Zero)
3385+
{
3386+
int ret = DilithiumFreeKey(ref key);
3387+
if (ret != 0)
3388+
{
3389+
log(ERROR_LOG, "Failed to free Dilithium key. Error code: " + ret);
3390+
}
3391+
}
3392+
}
3393+
}
3394+
33403395
/// <summary>
33413396
/// Create a new Dilithium key pair and initialize it with random values
33423397
/// </summary>

wrapper/CSharp/wolfSSL_CSharp/wolfSSL.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -767,16 +767,15 @@ public enum NamedGroup
767767
WOLFSSL_X25519_KYBER_LEVEL3 = 25497,
768768
WOLFSSL_P256_KYBER_LEVEL3 = 25498,
769769

770-
/* Taken from draft-connolly-tls-mlkem-key-agreement, see:
771-
* https://github.com/dconnolly/draft-connolly-tls-mlkem-key-agreement/
770+
/* Taken from draft-ietf-tls-mlkem, see:
771+
* https://datatracker.ietf.org/doc/draft-ietf-tls-mlkem/
772772
*/
773773
WOLFSSL_ML_KEM_512 = 512,
774774
WOLFSSL_ML_KEM_768 = 513,
775775
WOLFSSL_ML_KEM_1024 = 514,
776776

777-
/* Taken from draft-kwiatkowski-tls-ecdhe-mlkem. see:
778-
* https://github.com/post-quantum-cryptography/
779-
* draft-kwiatkowski-tls-ecdhe-mlkem/
777+
/* Taken from draft-ietf-tls-ecdhe-mlkem, see:
778+
* https://datatracker.ietf.org/doc/draft-ietf-tls-ecdhe-mlkem/
780779
*/
781780
WOLFSSL_SECP256R1MLKEM768 = 4587,
782781
WOLFSSL_X25519MLKEM768 = 4588,

0 commit comments

Comments
 (0)