Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions openssl/mldsa.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,22 @@ func SupportsMLDSA(params MLDSAParameters) bool {
}
}

// SupportsMLDSAExternalMu reports whether the provider supports signing and
// verifying externally computed message representatives for the parameter set.
// Providers may support ML-DSA without supporting external-mu operations.
func SupportsMLDSAExternalMu(params MLDSAParameters) bool {
switch params.keyType {
case _KeyTypeMLDSA44:
return supportsMLDSA44ExternalMu()
case _KeyTypeMLDSA65:
return supportsMLDSA65ExternalMu()
case _KeyTypeMLDSA87:
return supportsMLDSA87ExternalMu()
default:
return false
}
}

// probeMLDSA reports whether the OpenSSL provider exposes the given ML-DSA
// algorithm via the keymgmt fetch interface. ML-DSA was added in OpenSSL 3.5;
// older 3.x releases return nil for unknown algorithm names, and 1.x lacks
Expand All @@ -78,8 +94,31 @@ var (
supportsMLDSA44 = sync.OnceValue(func() bool { return probeMLDSA(_KeyTypeMLDSA44) })
supportsMLDSA65 = sync.OnceValue(func() bool { return probeMLDSA(_KeyTypeMLDSA65) })
supportsMLDSA87 = sync.OnceValue(func() bool { return probeMLDSA(_KeyTypeMLDSA87) })

supportsMLDSA44ExternalMu = sync.OnceValue(func() bool { return probeMLDSAExternalMu(MLDSA44()) })
supportsMLDSA65ExternalMu = sync.OnceValue(func() bool { return probeMLDSAExternalMu(MLDSA65()) })
supportsMLDSA87ExternalMu = sync.OnceValue(func() bool { return probeMLDSAExternalMu(MLDSA87()) })
)

func probeMLDSAExternalMu(params MLDSAParameters) bool {
if !SupportsMLDSA(params) {
return false
}
var seed [privateKeySizeMLDSA]byte
pkey, err := newMLDSAPrivatePkey(params.keyType, seed[:])
if err != nil {
return false
}
defer ossl.EVP_PKEY_free(pkey)

var mu [muSizeMLDSA]byte
signature, err := mldsaSignWithKey(pkey, params, mu[:], "", true)
if err != nil {
return false
}
return mldsaVerifyWithKey(pkey, mu[:], signature, "", true) == nil
}

// MLDSAParameters represents one of the fixed ML-DSA parameter sets.
type MLDSAParameters struct {
name string
Expand Down
84 changes: 62 additions & 22 deletions openssl/mldsa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,41 @@ var mldsaExternalMuTestCases = []mldsaExternalMuTestCase{
{"CAST/ML-DSA-65", openssl.MLDSA65(), "F215BA2280D86F142012FC05FFC04F2C7D22FF5DD7D69AA0EFB081E3A53E9318", "35cdb7dddbed44af4641bac659f46598ed769ea9693fd4ed2152b84c45811d2e66eded1eb20cde1c1f4b82642a330d8e86ac432a2aefaa56cd9b2b5f4affd450"},
}

func TestSupportsMLDSAExternalMu(t *testing.T) {
t.Parallel()
if openssl.SupportsMLDSAExternalMu(openssl.MLDSAParameters{}) {
t.Fatal("invalid parameter set supports external-mu")
}
for _, test := range mldsaParameterTests {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
supported := openssl.SupportsMLDSAExternalMu(test.params)
if !openssl.SupportsMLDSA(test.params) {
if supported {
t.Fatal("external-mu supported without ML-DSA support")
}
return
}
key, err := openssl.GenerateKeyMLDSA(test.params)
if err != nil {
t.Fatal(err)
}
mu := make([]byte, 64)
signature, err := key.SignExternalMu(mu)
if err != nil {
if supported {
t.Fatalf("external-mu reported supported but signing failed: %v", err)
}
return
}
err = key.PublicKey().VerifyExternalMu(mu, signature)
if supported != (err == nil) {
t.Fatalf("external-mu support = %v, verification error = %v", supported, err)
}
})
}
}

func TestMLDSARoundTrip(t *testing.T) {
t.Parallel()
for _, test := range mldsaParameterTests {
Expand Down Expand Up @@ -153,22 +188,27 @@ func testMLDSARoundTrip(t *testing.T, params openssl.MLDSAParameters) {
t.Error("Verify passed with wrong context")
}

mu := append(fromHexBytes(testCase.msg), fromHexBytes(testCase.msg)...)
externalSignature, err := privateKey.SignExternalMu(mu)
if err != nil {
t.Fatalf("SignExternalMu: %v", err)
}
if len(externalSignature) != params.SignatureSize() {
t.Fatalf("external signature length = %d, want %d", len(externalSignature), params.SignatureSize())
}
if err := reparsedPublicKey.VerifyExternalMu(mu, externalSignature); err != nil {
t.Fatalf("VerifyExternalMu: %v", err)
}
wrongMu := append([]byte(nil), mu...)
wrongMu[0] ^= 0x80
if err := reparsedPublicKey.VerifyExternalMu(wrongMu, externalSignature); err == nil {
t.Error("VerifyExternalMu passed on wrong message")
}
t.Run("external-mu", func(t *testing.T) {
if !openssl.SupportsMLDSAExternalMu(params) {
t.Skipf("%s external-mu not supported on this platform", params)
}
mu := append(fromHexBytes(testCase.msg), fromHexBytes(testCase.msg)...)
externalSignature, err := privateKey.SignExternalMu(mu)
if err != nil {
t.Fatalf("SignExternalMu: %v", err)
}
if len(externalSignature) != params.SignatureSize() {
t.Fatalf("external signature length = %d, want %d", len(externalSignature), params.SignatureSize())
}
if err := reparsedPublicKey.VerifyExternalMu(mu, externalSignature); err != nil {
t.Fatalf("VerifyExternalMu: %v", err)
}
wrongMu := append([]byte(nil), mu...)
wrongMu[0] ^= 0x80
if err := reparsedPublicKey.VerifyExternalMu(wrongMu, externalSignature); err == nil {
t.Error("VerifyExternalMu passed on wrong message")
}
})
})
}
}
Expand Down Expand Up @@ -306,8 +346,8 @@ func TestMLDSAExternalMuCASTVectors(t *testing.T) {
t.Parallel()
for _, test := range mldsaExternalMuTestCases {
t.Run(test.name, func(t *testing.T) {
if !openssl.SupportsMLDSA(test.params) {
t.Skipf("%s not supported on this platform", test.params)
if !openssl.SupportsMLDSAExternalMu(test.params) {
t.Skipf("%s external-mu not supported on this platform", test.params)
}
t.Parallel()
privateKey, err := openssl.NewPrivateKeyMLDSA(test.params, fromHexBytes(test.seed))
Expand Down Expand Up @@ -486,8 +526,8 @@ func BenchmarkMLDSASign(b *testing.B) {
func BenchmarkMLDSASignExternalMu(b *testing.B) {
for _, test := range mldsaParameterTests {
b.Run(test.name, func(b *testing.B) {
if !openssl.SupportsMLDSA(test.params) {
b.Skipf("%s not supported on this platform", test.params)
if !openssl.SupportsMLDSAExternalMu(test.params) {
b.Skipf("%s external-mu not supported on this platform", test.params)
}
privateKey := newBenchmarkMLDSAPrivateKey(b, test.params)
mu := make([]byte, 64)
Expand Down Expand Up @@ -531,8 +571,8 @@ func BenchmarkMLDSAVerify(b *testing.B) {
func BenchmarkMLDSAVerifyExternalMu(b *testing.B) {
for _, test := range mldsaParameterTests {
b.Run(test.name, func(b *testing.B) {
if !openssl.SupportsMLDSA(test.params) {
b.Skipf("%s not supported on this platform", test.params)
if !openssl.SupportsMLDSAExternalMu(test.params) {
b.Skipf("%s external-mu not supported on this platform", test.params)
}
privateKey := newBenchmarkMLDSAPrivateKey(b, test.params)
publicKey := privateKey.PublicKey()
Expand Down
Loading