| 1 |
// Package mlkem_ecdh implements hybrid ML-KEM + ECDH encryption, suitable for OpenPGP, experimental. |
| 2 |
// It follows the spec https://www.rfc-editor.org/rfc/rfc9980.html#name-composite-kem-schemes |
| 3 |
package mlkem_ecdh |
| 4 |
|
| 5 |
import ( |
| 6 |
goerrors "errors" |
| 7 |
"fmt" |
| 8 |
"io" |
| 9 |
|
| 10 |
"golang.org/x/crypto/sha3" |
| 11 |
|
| 12 |
"github.com/ProtonMail/go-crypto/openpgp/aes/keywrap" |
| 13 |
"github.com/ProtonMail/go-crypto/openpgp/errors" |
| 14 |
"github.com/ProtonMail/go-crypto/openpgp/internal/ecc" |
| 15 |
"github.com/cloudflare/circl/kem" |
| 16 |
) |
| 17 |
|
| 18 |
const ( |
| 19 |
maxSessionKeyLength = 64 |
| 20 |
MlKemSeedLen = 64 |
| 21 |
domSep = "OpenPGPCompositeKDFv1" |
| 22 |
) |
| 23 |
|
| 24 |
type PublicKey struct { |
| 25 |
AlgId uint8 |
| 26 |
Curve ecc.ECDHCurve |
| 27 |
Mlkem kem.Scheme |
| 28 |
PublicMlkem kem.PublicKey |
| 29 |
PublicPoint []byte |
| 30 |
} |
| 31 |
|
| 32 |
type PrivateKey struct { |
| 33 |
PublicKey |
| 34 |
SecretEc []byte |
| 35 |
SecretMlkem kem.PrivateKey |
| 36 |
SecretMlkemSeed []byte |
| 37 |
} |
| 38 |
|
| 39 |
// GenerateKey implements ML-KEM + ECC key generation as specified in |
| 40 |
// https://www.rfc-editor.org/rfc/rfc9980.html#name-key-generation-procedure |
| 41 |
func GenerateKey(rand io.Reader, algId uint8, c ecc.ECDHCurve, k kem.Scheme) (priv *PrivateKey, err error) { |
| 42 |
priv = new(PrivateKey) |
| 43 |
|
| 44 |
priv.PublicKey.AlgId = algId |
| 45 |
priv.PublicKey.Curve = c |
| 46 |
priv.PublicKey.Mlkem = k |
| 47 |
|
| 48 |
priv.PublicKey.PublicPoint, priv.SecretEc, err = c.GenerateECDH(rand) |
| 49 |
if err != nil { |
| 50 |
return nil, err |
| 51 |
} |
| 52 |
|
| 53 |
seed, err := generateRandomSeed(rand, MlKemSeedLen) |
| 54 |
if err != nil { |
| 55 |
return nil, err |
| 56 |
} |
| 57 |
|
| 58 |
if err := priv.DeriveMlKemKeys(seed, true); err != nil { |
| 59 |
return nil, err |
| 60 |
} |
| 61 |
return priv, nil |
| 62 |
} |
| 63 |
|
| 64 |
// DeriveMlKemKeys derives the ML-KEM keys from the provided seed and stores them inside priv. |
| 65 |
func (priv *PrivateKey) DeriveMlKemKeys(seed []byte, overridePublicKey bool) (err error) { |
| 66 |
if len(seed) != MlKemSeedLen { |
| 67 |
return goerrors.New("mlkem_ecdh: ml-kem secret seed has the wrong length") |
| 68 |
} |
| 69 |
priv.SecretMlkemSeed = seed |
| 70 |
publicKey, privateKey := priv.PublicKey.Mlkem.DeriveKeyPair(priv.SecretMlkemSeed) |
| 71 |
if overridePublicKey { |
| 72 |
priv.PublicKey.PublicMlkem = publicKey |
| 73 |
} |
| 74 |
priv.SecretMlkem = privateKey |
| 75 |
return nil |
| 76 |
} |
| 77 |
|
| 78 |
// Encrypt implements ML-KEM + ECC encryption as specified in |
| 79 |
// https://www.rfc-editor.org/rfc/rfc9980.html#name-encryption-procedure |
| 80 |
func Encrypt(rand io.Reader, pub *PublicKey, msg []byte) (kEphemeral, ecEphemeral, ciphertext []byte, err error) { |
| 81 |
if len(msg) > maxSessionKeyLength { |
| 82 |
return nil, nil, nil, goerrors.New("mlkem_ecdh: session key too long") |
| 83 |
} |
| 84 |
|
| 85 |
if len(msg)%8 != 0 { |
| 86 |
return nil, nil, nil, goerrors.New("mlkem_ecdh: session key not a multiple of 8") |
| 87 |
} |
| 88 |
|
| 89 |
// EC shared secret derivation |
| 90 |
ecEphemeral, ecSS, err := pub.Curve.Encaps(rand, pub.PublicPoint) |
| 91 |
if err != nil { |
| 92 |
return nil, nil, nil, err |
| 93 |
} |
| 94 |
|
| 95 |
// ML-KEM shared secret derivation |
| 96 |
kyberSeed, err := generateRandomSeed(rand, pub.Mlkem.EncapsulationSeedSize()) |
| 97 |
if err != nil { |
| 98 |
return nil, nil, nil, err |
| 99 |
} |
| 100 |
|
| 101 |
kEphemeral, kSS, err := pub.Mlkem.EncapsulateDeterministically(pub.PublicMlkem, kyberSeed) |
| 102 |
if err != nil { |
| 103 |
return nil, nil, nil, err |
| 104 |
} |
| 105 |
|
| 106 |
keyEncryptionKey, err := buildKey(pub, ecSS, ecEphemeral, pub.PublicPoint, kSS) |
| 107 |
if err != nil { |
| 108 |
return nil, nil, nil, err |
| 109 |
} |
| 110 |
|
| 111 |
if ciphertext, err = keywrap.Wrap(keyEncryptionKey, msg); err != nil { |
| 112 |
return nil, nil, nil, err |
| 113 |
} |
| 114 |
|
| 115 |
return kEphemeral, ecEphemeral, ciphertext, nil |
| 116 |
} |
| 117 |
|
| 118 |
// Decrypt implements ML-KEM + ECC decryption as specified in |
| 119 |
// https://www.rfc-editor.org/rfc/rfc9980.html#name-decryption-procedure |
| 120 |
func Decrypt(priv *PrivateKey, kEphemeral, ecEphemeral, ciphertext []byte) (msg []byte, err error) { |
| 121 |
// EC shared secret derivation |
| 122 |
ecSS, err := priv.PublicKey.Curve.Decaps(ecEphemeral, priv.SecretEc) |
| 123 |
if err != nil { |
| 124 |
return nil, err |
| 125 |
} |
| 126 |
|
| 127 |
// ML-KEM shared secret derivation |
| 128 |
kSS, err := priv.PublicKey.Mlkem.Decapsulate(priv.SecretMlkem, kEphemeral) |
| 129 |
if err != nil { |
| 130 |
return nil, err |
| 131 |
} |
| 132 |
|
| 133 |
kek, err := buildKey(&priv.PublicKey, ecSS, ecEphemeral, priv.PublicPoint, kSS) |
| 134 |
if err != nil { |
| 135 |
return nil, err |
| 136 |
} |
| 137 |
|
| 138 |
return keywrap.Unwrap(kek, ciphertext) |
| 139 |
} |
| 140 |
|
| 141 |
// buildKey implements the composite KDF from |
| 142 |
// https://www.rfc-editor.org/rfc/rfc9980.html#name-key-combiner |
| 143 |
func buildKey(pub *PublicKey, eccSecretPoint, eccCipherText, eccPublicKey, mlkemKeyShare []byte) ([]byte, error) { |
| 144 |
/// Set the output `ecdhKeyShare` to `eccSecretPoint` |
| 145 |
eccKeyShare := eccSecretPoint |
| 146 |
|
| 147 |
// mlkemKeyShare - the ML-KEM key share encoded as an octet string |
| 148 |
// algId - the OpenPGP algorithm ID of the public-key encryption algorithm |
| 149 |
// eccKeyShare - the ECDH key share encoded as an octet string |
| 150 |
// eccCipherText - the ECDH ciphertext encoded as an octet string |
| 151 |
// eccPublicKey - The ECDH public key of the recipient as an octet string |
| 152 |
|
| 153 |
// SHA3-256(mlkemKeyShare || eccKeyShare || eccCipherText || eccPublicKey || |
| 154 |
// algId || "OpenPGPCompositeKDFv1" || len("OpenPGPCompositeKDFv1")) |
| 155 |
h := sha3.New256() |
| 156 |
_, _ = h.Write(mlkemKeyShare) |
| 157 |
_, _ = h.Write(eccKeyShare) |
| 158 |
_, _ = h.Write(eccCipherText) |
| 159 |
_, _ = h.Write(eccPublicKey) |
| 160 |
_, _ = h.Write([]byte{pub.AlgId}) |
| 161 |
_, _ = h.Write([]byte(domSep)) |
| 162 |
_, _ = h.Write([]byte{byte(len(domSep))}) |
| 163 |
return h.Sum(nil), nil |
| 164 |
} |
| 165 |
|
| 166 |
// Validate checks that the public key corresponds to the private key |
| 167 |
func Validate(priv *PrivateKey) (err error) { |
| 168 |
if err = priv.PublicKey.Curve.ValidateECDH(priv.PublicKey.PublicPoint, priv.SecretEc); err != nil { |
| 169 |
return err |
| 170 |
} |
| 171 |
|
| 172 |
if !priv.PublicKey.PublicMlkem.Equal(priv.SecretMlkem.Public()) { |
| 173 |
return errors.KeyInvalidError("mlkem_ecdh: invalid public key") |
| 174 |
} |
| 175 |
|
| 176 |
return |
| 177 |
} |
| 178 |
|
| 179 |
// EncodeFields encodes an ML-KEM + ECDH session key encryption fields as |
| 180 |
// ephemeral ECDH public key | ML-KEM ciphertext | follow byte length | cipherFunction (v3 only) | encryptedSessionKey |
| 181 |
// and writes it to writer. |
| 182 |
func EncodeFields(w io.Writer, ec, ml, encryptedSessionKey []byte, cipherFunction byte, v6 bool) (err error) { |
| 183 |
if _, err = w.Write(ec); err != nil { |
| 184 |
return err |
| 185 |
} |
| 186 |
|
| 187 |
if _, err = w.Write(ml); err != nil { |
| 188 |
return err |
| 189 |
} |
| 190 |
|
| 191 |
lenAlgorithm := 0 |
| 192 |
if !v6 { |
| 193 |
lenAlgorithm = 1 |
| 194 |
} |
| 195 |
|
| 196 |
if _, err = w.Write([]byte{byte(len(encryptedSessionKey) + lenAlgorithm)}); err != nil { |
| 197 |
return err |
| 198 |
} |
| 199 |
|
| 200 |
if !v6 { |
| 201 |
if _, err = w.Write([]byte{cipherFunction}); err != nil { |
| 202 |
return err |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
if _, err = w.Write(encryptedSessionKey); err != nil { |
| 207 |
return err |
| 208 |
} |
| 209 |
|
| 210 |
return nil |
| 211 |
} |
| 212 |
|
| 213 |
// DecodeFields decodes an ML-KEM + ECDH session key encryption fields as |
| 214 |
// ephemeral ECDH public key | ML-KEM ciphertext | follow byte length | cipherFunction (v3 only) | encryptedSessionKey. |
| 215 |
func DecodeFields(r io.Reader, lenEcc, lenMlkem int, v6 bool) (ephemeralPublicEcc, ephemeralPublicMlKem, encryptedSessionKey []byte, cipherFunction byte, err error) { |
| 216 |
var buf [1]byte |
| 217 |
|
| 218 |
ephemeralPublicEcc = make([]byte, lenEcc) |
| 219 |
if _, err = io.ReadFull(r, ephemeralPublicEcc); err != nil { |
| 220 |
return |
| 221 |
} |
| 222 |
|
| 223 |
ephemeralPublicMlKem = make([]byte, lenMlkem) |
| 224 |
if _, err = io.ReadFull(r, ephemeralPublicMlKem); err != nil { |
| 225 |
return |
| 226 |
} |
| 227 |
|
| 228 |
// A one-octet size of the following fields. |
| 229 |
if _, err = io.ReadFull(r, buf[:]); err != nil { |
| 230 |
return |
| 231 |
} |
| 232 |
|
| 233 |
followingLen := buf[0] |
| 234 |
// The one-octet algorithm identifier, if it was passed (in the case of a v3 PKESK packet). |
| 235 |
if !v6 { |
| 236 |
if _, err = io.ReadFull(r, buf[:]); err != nil { |
| 237 |
return |
| 238 |
} |
| 239 |
cipherFunction = buf[0] |
| 240 |
followingLen -= 1 |
| 241 |
} |
| 242 |
|
| 243 |
// The encrypted session key. |
| 244 |
encryptedSessionKey = make([]byte, followingLen) |
| 245 |
if _, err = io.ReadFull(r, encryptedSessionKey); err != nil { |
| 246 |
return |
| 247 |
} |
| 248 |
|
| 249 |
return |
| 250 |
} |
| 251 |
|
| 252 |
func generateRandomSeed(rand io.Reader, size int) ([]byte, error) { |
| 253 |
randomBytes := make([]byte, size) |
| 254 |
if _, err := rand.Read(randomBytes); err != nil { |
| 255 |
return nil, fmt.Errorf("failed to generate random bytes: %w", err) |
| 256 |
} |
| 257 |
return randomBytes, nil |
| 258 |
} |