| 1 |
// Package mldsa_eddsa implements hybrid ML-DSA + EdDSA encryption for OpenPGP, |
| 2 |
// according to https://www.rfc-editor.org/rfc/rfc9980.html#name-composite-signature-schemes. |
| 3 |
package mldsa_eddsa |
| 4 |
|
| 5 |
import ( |
| 6 |
goerrors "errors" |
| 7 |
"io" |
| 8 |
|
| 9 |
"github.com/ProtonMail/go-crypto/openpgp/errors" |
| 10 |
"github.com/ProtonMail/go-crypto/openpgp/internal/ecc" |
| 11 |
"github.com/cloudflare/circl/sign" |
| 12 |
"github.com/cloudflare/circl/sign/mldsa/mldsa65" |
| 13 |
"github.com/cloudflare/circl/sign/mldsa/mldsa87" |
| 14 |
) |
| 15 |
|
| 16 |
const ( |
| 17 |
MlDsaSeedLen = 32 |
| 18 |
) |
| 19 |
|
| 20 |
type PublicKey struct { |
| 21 |
AlgId uint8 |
| 22 |
Curve ecc.EdDSACurve |
| 23 |
Mldsa sign.Scheme |
| 24 |
PublicPoint []byte |
| 25 |
PublicMldsa sign.PublicKey |
| 26 |
} |
| 27 |
|
| 28 |
type PrivateKey struct { |
| 29 |
PublicKey |
| 30 |
SecretEc []byte |
| 31 |
SecretMldsa sign.PrivateKey |
| 32 |
SecretMldsaSeed []byte |
| 33 |
} |
| 34 |
|
| 35 |
// GenerateKey generates a ML-DSA + EdDSA composite key as specified in |
| 36 |
// https://www.rfc-editor.org/rfc/rfc9980.html#name-key-generation-procedure-2 |
| 37 |
func GenerateKey(rand io.Reader, algId uint8, c ecc.EdDSACurve, d sign.Scheme) (priv *PrivateKey, err error) { |
| 38 |
priv = new(PrivateKey) |
| 39 |
|
| 40 |
priv.PublicKey.AlgId = algId |
| 41 |
priv.PublicKey.Curve = c |
| 42 |
priv.PublicKey.Mldsa = d |
| 43 |
|
| 44 |
priv.PublicKey.PublicPoint, priv.SecretEc, err = c.GenerateEdDSA(rand) |
| 45 |
if err != nil { |
| 46 |
return nil, err |
| 47 |
} |
| 48 |
|
| 49 |
keySeed := make([]byte, d.SeedSize()) |
| 50 |
if _, err = rand.Read(keySeed); err != nil { |
| 51 |
return nil, err |
| 52 |
} |
| 53 |
|
| 54 |
if err := priv.DeriveMlDsaKeys(keySeed, true); err != nil { |
| 55 |
return nil, err |
| 56 |
} |
| 57 |
return priv, nil |
| 58 |
} |
| 59 |
|
| 60 |
// DeriveMlDsaKeys derives the ML-DSA keys from the provided seed and stores them inside priv. |
| 61 |
func (priv *PrivateKey) DeriveMlDsaKeys(seed []byte, overridePublicKey bool) (err error) { |
| 62 |
if len(seed) != MlDsaSeedLen { |
| 63 |
return goerrors.New("mldsa_eddsa: ml-dsa secret seed has the wrong length") |
| 64 |
} |
| 65 |
priv.SecretMldsaSeed = seed |
| 66 |
publicKey, privateKey := priv.PublicKey.Mldsa.DeriveKey(priv.SecretMldsaSeed) |
| 67 |
if overridePublicKey { |
| 68 |
priv.PublicKey.PublicMldsa = publicKey |
| 69 |
} |
| 70 |
priv.SecretMldsa = privateKey |
| 71 |
return nil |
| 72 |
} |
| 73 |
|
| 74 |
// Sign generates a ML-DSA + EdDSA composite signature as specified in |
| 75 |
// https://www.rfc-editor.org/rfc/rfc9980.html#name-signature-generation |
| 76 |
func Sign(priv *PrivateKey, message []byte) (mldsaSig, ecSig []byte, err error) { |
| 77 |
ecSig, err = priv.PublicKey.Curve.Sign(priv.PublicKey.PublicPoint, priv.SecretEc, message) |
| 78 |
if err != nil { |
| 79 |
return nil, nil, err |
| 80 |
} |
| 81 |
|
| 82 |
// The default signer interface does not use the hedged variant. |
| 83 |
// Thus, we need to use the low level api |
| 84 |
switch key := priv.SecretMldsa.(type) { |
| 85 |
case *mldsa65.PrivateKey: |
| 86 |
mldsaSig = make([]byte, mldsa65.SignatureSize) |
| 87 |
err = mldsa65.SignTo(key, message, nil, true, mldsaSig) |
| 88 |
case *mldsa87.PrivateKey: |
| 89 |
mldsaSig = make([]byte, mldsa87.SignatureSize) |
| 90 |
err = mldsa87.SignTo(key, message, nil, true, mldsaSig) |
| 91 |
default: |
| 92 |
err = goerrors.New("mldsa_eddsa: unsupported ML-DSA private key type") |
| 93 |
} |
| 94 |
|
| 95 |
if err != nil { |
| 96 |
return nil, nil, err |
| 97 |
} |
| 98 |
|
| 99 |
return mldsaSig, ecSig, nil |
| 100 |
} |
| 101 |
|
| 102 |
// Verify verifies a ML-DSA + EdDSA composite signature as specified in |
| 103 |
// https://www.rfc-editor.org/rfc/rfc9980.html#name-signature-verification |
| 104 |
func Verify(pub *PublicKey, message, dSig, ecSig []byte) bool { |
| 105 |
return pub.Curve.Verify(pub.PublicPoint, message, ecSig) && pub.Mldsa.Verify(pub.PublicMldsa, message, dSig, nil) |
| 106 |
} |
| 107 |
|
| 108 |
// Validate checks that the public key corresponds to the private key |
| 109 |
func Validate(priv *PrivateKey) (err error) { |
| 110 |
if err = priv.PublicKey.Curve.ValidateEdDSA(priv.PublicKey.PublicPoint, priv.SecretEc); err != nil { |
| 111 |
return err |
| 112 |
} |
| 113 |
|
| 114 |
if !priv.PublicMldsa.Equal(priv.SecretMldsa.Public()) { |
| 115 |
return errors.KeyInvalidError("mldsa_eddsa: invalid public key") |
| 116 |
} |
| 117 |
|
| 118 |
return nil |
| 119 |
} |