Closed
Description
The native shim for OpenSSL asserts that the msg
parameter is not null:
However it can be null when signing an empty message, as ReadOnlySpan<byte>.Empty
will marshal as NULL
. This unit test reproduces it:
[Fact]
public static void MlDsaNullMessage()
{
using MLDsa mldsa = MLDsa.GenerateKey(MLDsaAlgorithm.MLDsa44);
byte[] signature = new byte[mldsa.Algorithm.SignatureSizeInBytes];
_ = mldsa.SignData(ReadOnlySpan<byte>.Empty, signature);
}
The active test run was aborted. Reason: Test host process crashed : dotnet: /home/vcsjones/Projects/runtime/src/native/libs/System.Security.Cryptography.Native/pal_evp_pkey_ml_dsa.c:81: int32_t CryptoNative_MLDsaSignPure(EVP_PKEY *, void *, uint8_t *, int32_t, uint8_t *, int32_t, uint8_t *, int32_t): Assertion `msg' failed.
The two ways to fix this are:
- If
EVP_PKEY_sign
permitstbs
to beNULL
(as long astbslen
is 0) then we can relax the assert. - If
EVP_PKEY_sign
requires a non-NULL tbs, we can either handle that in the native shim (put an empty buffer on the stack), or useGetNonNullPinnableReference
from the managed side.
Additionally, we should make sure context
works correctly as well for "null-span" contexts, and that everything has proper unit test coverage. Verify
should be tested as well.