@@ -528,9 +528,9 @@ public class wolfcrypt
528528 [ DllImport ( wolfssl_dll ) ]
529529 private static extern int wc_dilithium_import_public ( byte [ ] input , uint inputLen , IntPtr key ) ;
530530 [ DllImport ( wolfssl_dll ) ]
531- private static extern int wc_dilithium_sign_msg ( byte [ ] msg , uint msgLen , byte [ ] sig , ref uint sigLen , IntPtr key , IntPtr rng ) ;
531+ 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 ) ;
532532 [ DllImport ( wolfssl_dll ) ]
533- private static extern int wc_dilithium_verify_msg ( byte [ ] sig , uint sigLen , byte [ ] msg , uint msgLen , ref int res , IntPtr key ) ;
533+ 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 ) ;
534534 [ DllImport ( wolfssl_dll ) ]
535535 private static extern int wc_MlDsaKey_GetPrivLen ( IntPtr key , ref int len ) ;
536536 [ DllImport ( wolfssl_dll ) ]
@@ -559,9 +559,9 @@ public class wolfcrypt
559559 [ DllImport ( wolfssl_dll , CallingConvention = CallingConvention . Cdecl ) ]
560560 private static extern int wc_dilithium_import_public ( byte [ ] input , uint inputLen , IntPtr key ) ;
561561 [ DllImport ( wolfssl_dll , CallingConvention = CallingConvention . Cdecl ) ]
562- private static extern int wc_dilithium_sign_msg ( byte [ ] msg , uint msgLen , byte [ ] sig , ref uint sigLen , IntPtr key , IntPtr rng ) ;
562+ 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 ) ;
563563 [ DllImport ( wolfssl_dll , CallingConvention = CallingConvention . Cdecl ) ]
564- private static extern int wc_dilithium_verify_msg ( byte [ ] sig , uint sigLen , byte [ ] msg , uint msgLen , ref int res , IntPtr key ) ;
564+ 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 ) ;
565565 [ DllImport ( wolfssl_dll , CallingConvention = CallingConvention . Cdecl ) ]
566566 private static extern int wc_MlDsaKey_GetPrivLen ( IntPtr key , ref int len ) ;
567567 [ DllImport ( wolfssl_dll , CallingConvention = CallingConvention . Cdecl ) ]
@@ -2933,18 +2933,15 @@ public static IntPtr MlKemMakeKey(MlKemTypes type, IntPtr heap, int devId)
29332933 /// <returns>0 on success, negative value on error.</returns>
29342934 public static int MlKemFreeKey ( ref IntPtr key )
29352935 {
2936- int ret = 0 ;
2936+ int ret ;
29372937
29382938 if ( key == IntPtr . Zero )
29392939 {
29402940 return BAD_FUNC_ARG ;
29412941 }
29422942
2943- if ( key != IntPtr . Zero )
2944- {
2945- ret = wc_MlKemKey_Delete ( key , IntPtr . Zero ) ;
2946- key = IntPtr . Zero ;
2947- }
2943+ ret = wc_MlKemKey_Delete ( key , IntPtr . Zero ) ;
2944+ key = IntPtr . Zero ;
29482945 return ret ;
29492946 }
29502947
@@ -2968,10 +2965,10 @@ public static int MlKemEncodePublicKey(IntPtr key, out byte[] publicKey)
29682965 try
29692966 {
29702967 ret = wc_MlKemKey_PublicKeySize ( key , ref pubLen ) ;
2971- if ( ret != 0 || pubLen == 0 )
2968+ if ( ret != 0 || pubLen == 0 )
29722969 {
29732970 log ( ERROR_LOG , "Failed to get MlKem public key length. Error code: " + ret ) ;
2974- return ret ;
2971+ return ( ret != 0 ) ? ret : BAD_FUNC_ARG ;
29752972 }
29762973 if ( pubLen > int . MaxValue )
29772974 {
@@ -3017,10 +3014,10 @@ public static int MlKemEncodePrivateKey(IntPtr key, out byte[] privateKey)
30173014 try
30183015 {
30193016 ret = wc_MlKemKey_PrivateKeySize ( key , ref privLen ) ;
3020- if ( ret != 0 || privLen == 0 )
3017+ if ( ret != 0 || privLen == 0 )
30213018 {
30223019 log ( ERROR_LOG , "Failed to get MlKem private key length. Error code: " + ret ) ;
3023- return ret ;
3020+ return ( ret != 0 ) ? ret : BAD_FUNC_ARG ;
30243021 }
30253022 if ( privLen > int . MaxValue )
30263023 {
@@ -3074,14 +3071,14 @@ public static int MlKemDecodePublicKey(IntPtr key, byte[] publicKey)
30743071 if ( ret != 0 || pubLen == 0 )
30753072 {
30763073 log ( ERROR_LOG , "Failed to get MlKem public key length. Error code: " + ret ) ;
3077- return ret ;
3074+ return ( ret != 0 ) ? ret : BAD_FUNC_ARG ;
30783075 }
30793076 if ( ( uint ) publicKey . Length != pubLen )
3080- {
3081- log ( ERROR_LOG , "MlKem public key buffer length mismatch. Expected: " +
3082- pubLen + ", actual: " + publicKey . Length ) ;
3083- return BUFFER_E ;
3084- }
3077+ {
3078+ log ( ERROR_LOG , "MlKem public key buffer length mismatch. Expected: " +
3079+ pubLen + ", actual: " + publicKey . Length ) ;
3080+ return BUFFER_E ;
3081+ }
30853082
30863083 ret = wc_MlKemKey_DecodePublicKey ( key , publicKey , pubLen ) ;
30873084 if ( ret != 0 )
@@ -3123,12 +3120,12 @@ public static int MlKemDecodePrivateKey(IntPtr key, byte[] privateKey)
31233120 try
31243121 {
31253122 ret = wc_MlKemKey_PrivateKeySize ( key , ref privLen ) ;
3126- if ( privLen == 0 )
3123+ if ( ret != 0 || privLen == 0 )
31273124 {
31283125 log ( ERROR_LOG , "Failed to get MlKem private key length. Error code: " + ret ) ;
3129- return ret ;
3126+ return ( ret != 0 ) ? ret : BAD_FUNC_ARG ;
31303127 }
3131-
3128+
31323129 if ( ( uint ) privateKey . Length != privLen )
31333130 {
31343131 log ( ERROR_LOG , "MlKem private key buffer length mismatch. Required: " + privLen +
@@ -3367,18 +3364,15 @@ public static IntPtr DilithiumMakeKey(IntPtr heap, int devId, MlDsaLevels level)
33673364 /// <returns>0 on success, negative value on error.</returns>
33683365 public static int DilithiumFreeKey ( ref IntPtr key )
33693366 {
3370- int ret = 0 ;
3367+ int ret ;
33713368
33723369 if ( key == IntPtr . Zero )
33733370 {
33743371 return BAD_FUNC_ARG ;
33753372 }
33763373
3377- if ( key != IntPtr . Zero )
3378- {
3379- ret = wc_dilithium_delete ( key , IntPtr . Zero ) ;
3380- key = IntPtr . Zero ;
3381- }
3374+ ret = wc_dilithium_delete ( key , IntPtr . Zero ) ;
3375+ key = IntPtr . Zero ;
33823376 return ret ;
33833377 }
33843378
@@ -3451,10 +3445,10 @@ public static int DilithiumExportPrivateKey(IntPtr key, out byte[] privateKey)
34513445 try
34523446 {
34533447 ret = wc_MlDsaKey_GetPrivLen ( key , ref privLen ) ;
3454- if ( privLen <= 0 )
3448+ if ( ret != 0 || privLen <= 0 )
34553449 {
34563450 log ( ERROR_LOG , "Failed to get Dilithium private key length. Error code: " + ret ) ;
3457- return ret ;
3451+ return ( ret != 0 ) ? ret : BAD_FUNC_ARG ;
34583452 }
34593453
34603454 privateKey = new byte [ privLen ] ;
@@ -3501,10 +3495,10 @@ public static int DilithiumExportPublicKey(IntPtr key, out byte[] publicKey)
35013495 try
35023496 {
35033497 ret = wc_MlDsaKey_GetPubLen ( key , ref pubLen ) ;
3504- if ( pubLen <= 0 )
3498+ if ( ret != 0 || pubLen <= 0 )
35053499 {
35063500 log ( ERROR_LOG , "Failed to get Dilithium public key length. Error code: " + ret ) ;
3507- return ret ;
3501+ return ( ret != 0 ) ? ret : BAD_FUNC_ARG ;
35083502 }
35093503
35103504 publicKey = new byte [ pubLen ] ;
@@ -3553,10 +3547,10 @@ public static int DilithiumSignMsg(IntPtr key, byte[] msg, out byte[] sig)
35533547 try
35543548 {
35553549 ret = wc_MlDsaKey_GetSigLen ( key , ref sigLen ) ;
3556- if ( sigLen <= 0 )
3550+ if ( ret != 0 || sigLen <= 0 )
35573551 {
35583552 log ( ERROR_LOG , "Failed to get Dilithium signature length. Error code: " + ret ) ;
3559- return ret ;
3553+ return ( ret != 0 ) ? ret : BAD_FUNC_ARG ;
35603554 }
35613555
35623556 sig = new byte [ sigLen ] ;
@@ -3565,9 +3559,10 @@ public static int DilithiumSignMsg(IntPtr key, byte[] msg, out byte[] sig)
35653559 if ( rng == IntPtr . Zero )
35663560 {
35673561 log ( ERROR_LOG , "Failed to create RNG for Dilithium signing." ) ;
3568- return EXCEPTION_E ;
3562+ return MEMORY_E ;
35693563 }
3570- ret = wc_dilithium_sign_msg ( msg , ( uint ) msg . Length , sig , ref outLen , key , rng ) ;
3564+ /* FIPS 204 sign with empty context (ctx=null, ctxLen=0). */
3565+ ret = wc_dilithium_sign_ctx_msg ( null , 0 , msg , ( uint ) msg . Length , sig , ref outLen , key , rng ) ;
35713566 if ( ret != 0 )
35723567 {
35733568 log ( ERROR_LOG , "Failed to sign message with Dilithium key. Error code: " + ret ) ;
@@ -3611,7 +3606,8 @@ public static int DilithiumVerifyMsg(IntPtr key, byte[] msg, byte[] sig)
36113606
36123607 try
36133608 {
3614- ret = wc_dilithium_verify_msg ( sig , ( uint ) sig . Length , msg , ( uint ) msg . Length , ref res , key ) ;
3609+ /* FIPS 204 verify with empty context (ctx=null, ctxLen=0). */
3610+ ret = wc_dilithium_verify_ctx_msg ( sig , ( uint ) sig . Length , null , 0 , msg , ( uint ) msg . Length , ref res , key ) ;
36153611 if ( ret != 0 )
36163612 {
36173613 log ( ERROR_LOG , "Failed to verify message with Dilithium key. Error code: " + ret ) ;
0 commit comments