@@ -445,22 +445,14 @@ public class wolfcrypt
445445 [ DllImport ( wolfssl_dll ) ]
446446 private static extern int wc_MlKemKey_Delete ( IntPtr key , IntPtr key_p ) ;
447447 [ DllImport ( wolfssl_dll ) ]
448- private static extern int wc_MlKemKey_Init ( IntPtr key , int type , IntPtr heap , int devId ) ;
449- [ DllImport ( wolfssl_dll ) ]
450- private static extern int wc_MlKemKey_Free ( IntPtr key ) ;
451- [ DllImport ( wolfssl_dll ) ]
452448 private static extern int wc_MlKemKey_MakeKey ( IntPtr key , IntPtr rng ) ;
453449 [ DllImport ( wolfssl_dll ) ]
454- private static extern int wc_MlKemKey_MakeKeyWithRandom ( IntPtr key , byte [ ] rand , int len ) ;
455- [ DllImport ( wolfssl_dll ) ]
456450 private static extern int wc_MlKemKey_EncodePublicKey ( IntPtr key , byte [ ] output , uint len ) ;
457451 [ DllImport ( wolfssl_dll ) ]
458452 private static extern int wc_MlKemKey_DecodePublicKey ( IntPtr key , byte [ ] input , uint len ) ;
459453 [ DllImport ( wolfssl_dll ) ]
460454 private static extern int wc_MlKemKey_Encapsulate ( IntPtr key , byte [ ] ct , byte [ ] ss , IntPtr rng ) ;
461455 [ DllImport ( wolfssl_dll ) ]
462- private static extern int wc_MlKemKey_EncapsulateWithRandom ( IntPtr key , byte [ ] ct , byte [ ] ss , byte [ ] rand , int len ) ;
463- [ DllImport ( wolfssl_dll ) ]
464456 private static extern int wc_MlKemKey_Decapsulate ( IntPtr key , byte [ ] ss , byte [ ] ct , uint len ) ;
465457 [ DllImport ( wolfssl_dll ) ]
466458 private static extern int wc_MlKemKey_EncodePrivateKey ( IntPtr key , byte [ ] output , uint len ) ;
@@ -480,22 +472,14 @@ public class wolfcrypt
480472 [ DllImport ( wolfssl_dll , CallingConvention = CallingConvention . Cdecl ) ]
481473 private static extern int wc_MlKemKey_Delete ( IntPtr key , IntPtr key_p ) ;
482474 [ DllImport ( wolfssl_dll , CallingConvention = CallingConvention . Cdecl ) ]
483- private static extern int wc_MlKemKey_Init ( IntPtr key , int type , IntPtr heap , int devId ) ;
484- [ DllImport ( wolfssl_dll , CallingConvention = CallingConvention . Cdecl ) ]
485- private static extern int wc_MlKemKey_Free ( IntPtr key ) ;
486- [ DllImport ( wolfssl_dll , CallingConvention = CallingConvention . Cdecl ) ]
487475 private static extern int wc_MlKemKey_MakeKey ( IntPtr key , IntPtr rng ) ;
488476 [ DllImport ( wolfssl_dll , CallingConvention = CallingConvention . Cdecl ) ]
489- private static extern int wc_MlKemKey_MakeKeyWithRandom ( IntPtr key , byte [ ] rand , int len ) ;
490- [ DllImport ( wolfssl_dll , CallingConvention = CallingConvention . Cdecl ) ]
491477 private static extern int wc_MlKemKey_EncodePublicKey ( IntPtr key , byte [ ] output , uint len ) ;
492478 [ DllImport ( wolfssl_dll , CallingConvention = CallingConvention . Cdecl ) ]
493479 private static extern int wc_MlKemKey_DecodePublicKey ( IntPtr key , byte [ ] input , uint len ) ;
494480 [ DllImport ( wolfssl_dll , CallingConvention = CallingConvention . Cdecl ) ]
495481 private static extern int wc_MlKemKey_Encapsulate ( IntPtr key , byte [ ] ct , byte [ ] ss , IntPtr rng ) ;
496482 [ DllImport ( wolfssl_dll , CallingConvention = CallingConvention . Cdecl ) ]
497- private static extern int wc_MlKemKey_EncapsulateWithRandom ( IntPtr key , byte [ ] ct , byte [ ] ss , byte [ ] rand , int len ) ;
498- [ DllImport ( wolfssl_dll , CallingConvention = CallingConvention . Cdecl ) ]
499483 private static extern int wc_MlKemKey_Decapsulate ( IntPtr key , byte [ ] ss , byte [ ] ct , uint len ) ;
500484 [ DllImport ( wolfssl_dll , CallingConvention = CallingConvention . Cdecl ) ]
501485 private static extern int wc_MlKemKey_EncodePrivateKey ( IntPtr key , byte [ ] output , uint len ) ;
@@ -512,12 +496,8 @@ public class wolfcrypt
512496 [ DllImport ( wolfssl_dll ) ]
513497 private static extern int wc_dilithium_delete ( IntPtr key , IntPtr key_p ) ;
514498 [ DllImport ( wolfssl_dll ) ]
515- private static extern int wc_dilithium_init_ex ( IntPtr key , IntPtr heap , int devId ) ;
516- [ DllImport ( wolfssl_dll ) ]
517499 private static extern int wc_dilithium_set_level ( IntPtr key , byte level ) ;
518500 [ DllImport ( wolfssl_dll ) ]
519- private static extern void wc_dilithium_free ( IntPtr key ) ;
520- [ DllImport ( wolfssl_dll ) ]
521501 private static extern int wc_dilithium_make_key ( IntPtr key , IntPtr rng ) ;
522502 [ DllImport ( wolfssl_dll ) ]
523503 private static extern int wc_dilithium_export_private ( IntPtr key , byte [ ] output , ref uint outLen ) ;
@@ -543,12 +523,8 @@ public class wolfcrypt
543523 [ DllImport ( wolfssl_dll , CallingConvention = CallingConvention . Cdecl ) ]
544524 private static extern int wc_dilithium_delete ( IntPtr key , IntPtr key_p ) ;
545525 [ DllImport ( wolfssl_dll , CallingConvention = CallingConvention . Cdecl ) ]
546- private static extern int wc_dilithium_init_ex ( IntPtr key , IntPtr heap , int devId ) ;
547- [ DllImport ( wolfssl_dll , CallingConvention = CallingConvention . Cdecl ) ]
548526 private static extern int wc_dilithium_set_level ( IntPtr key , byte level ) ;
549527 [ DllImport ( wolfssl_dll , CallingConvention = CallingConvention . Cdecl ) ]
550- private static extern void wc_dilithium_free ( IntPtr key ) ;
551- [ DllImport ( wolfssl_dll , CallingConvention = CallingConvention . Cdecl ) ]
552528 private static extern int wc_dilithium_make_key ( IntPtr key , IntPtr rng ) ;
553529 [ DllImport ( wolfssl_dll , CallingConvention = CallingConvention . Cdecl ) ]
554530 private static extern int wc_dilithium_export_private ( IntPtr key , byte [ ] output , ref uint outLen ) ;
@@ -2864,6 +2840,33 @@ public static void Curve25519ExportKeyRaw(IntPtr key, out byte[] privateKey, out
28642840 // These APIs work by adding several options to wolfCrypt.
28652841 // Please refer to `../user_settings.h`.
28662842
2843+ /// <summary>
2844+ /// Allocate and initialize a new ML-KEM key without generating key
2845+ /// material. Use this when you intend to import or decode an existing
2846+ /// key (e.g., before calling MlKemDecodePublicKey/MlKemDecodePrivateKey).
2847+ /// </summary>
2848+ /// <param name="type">ML-KEM parameter set type</param>
2849+ /// <param name="heap">Heap pointer for memory allocation</param>
2850+ /// <param name="devId">Device ID (if applicable)</param>
2851+ /// <returns>Pointer to the MlKem key structure, or IntPtr.Zero on failure</returns>
2852+ public static IntPtr MlKemNew ( MlKemTypes type , IntPtr heap , int devId )
2853+ {
2854+ try
2855+ {
2856+ IntPtr key = wc_MlKemKey_New ( ( int ) type , heap , devId ) ;
2857+ if ( key == IntPtr . Zero )
2858+ {
2859+ log ( ERROR_LOG , "Failed to allocate or initialize MlKem key." ) ;
2860+ }
2861+ return key ;
2862+ }
2863+ catch ( Exception ex )
2864+ {
2865+ log ( ERROR_LOG , "MlKem key allocation exception: " + ex . ToString ( ) ) ;
2866+ return IntPtr . Zero ;
2867+ }
2868+ }
2869+
28672870 /// <summary>
28682871 /// Create a new ML-KEM key pair and initialize it with random values
28692872 /// </summary>
@@ -3288,6 +3291,58 @@ public enum MlKemTypes
32883291 // These APIs work by adding several options to wolfCrypt.
32893292 // Please refer to `../user_settings.h`.
32903293
3294+ /// <summary>
3295+ /// Allocate and initialize a new Dilithium key (with level set) without
3296+ /// generating key material. Use this when you intend to import an
3297+ /// existing key (e.g., before calling DilithiumImportPublicKey or
3298+ /// DilithiumImportPrivateKey).
3299+ /// </summary>
3300+ /// <param name="heap">Heap pointer for memory allocation</param>
3301+ /// <param name="devId">Device ID (if applicable)</param>
3302+ /// <param name="level">Dilithium security level</param>
3303+ /// <returns>Pointer to the Dilithium key structure, or IntPtr.Zero on failure</returns>
3304+ public static IntPtr DilithiumNew ( IntPtr heap , int devId , MlDsaLevels level )
3305+ {
3306+ IntPtr key = IntPtr . Zero ;
3307+ bool success = false ;
3308+
3309+ try
3310+ {
3311+ key = wc_dilithium_new ( heap , devId ) ;
3312+ if ( key == IntPtr . Zero )
3313+ {
3314+ log ( ERROR_LOG , "Failed to allocate and initialize Dilithium key." ) ;
3315+ return IntPtr . Zero ;
3316+ }
3317+
3318+ int ret = wc_dilithium_set_level ( key , ( byte ) level ) ;
3319+ if ( ret != 0 )
3320+ {
3321+ log ( ERROR_LOG , "Failed to set Dilithium level. Error code: " + ret ) ;
3322+ return IntPtr . Zero ;
3323+ }
3324+
3325+ success = true ;
3326+ return key ;
3327+ }
3328+ catch ( Exception ex )
3329+ {
3330+ log ( ERROR_LOG , "Dilithium key allocation exception: " + ex . ToString ( ) ) ;
3331+ return IntPtr . Zero ;
3332+ }
3333+ finally
3334+ {
3335+ if ( ! success && key != IntPtr . Zero )
3336+ {
3337+ int ret = DilithiumFreeKey ( ref key ) ;
3338+ if ( ret != 0 )
3339+ {
3340+ log ( ERROR_LOG , "Failed to free Dilithium key. Error code: " + ret ) ;
3341+ }
3342+ }
3343+ }
3344+ }
3345+
32913346 /// <summary>
32923347 /// Create a new Dilithium key pair and initialize it with random values
32933348 /// </summary>
0 commit comments