Skip to content

Commit

Permalink
feat: ML-KEM change key combiner to 2a mailinglist
Browse files Browse the repository at this point in the history
Implements the key combiner 2a from the OpenPGP mailinglist:
https://mailarchive.ietf.org/arch/msg/openpgp/NMTCy707LICtxIhP3Xt1U5C8MF0/
  • Loading branch information
lubux committed Nov 14, 2024
1 parent 8c808a9 commit ba0edb2
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 318 deletions.
147 changes: 0 additions & 147 deletions internal/kmac/kmac.go

This file was deleted.

142 changes: 0 additions & 142 deletions internal/kmac/kmac_test.go

This file was deleted.

42 changes: 13 additions & 29 deletions openpgp/mlkem_ecdh/mlkem_ecdh.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"io"

"github.com/ProtonMail/go-crypto/internal/kmac"
"github.com/ProtonMail/go-crypto/openpgp/internal/encoding"
"golang.org/x/crypto/sha3"

Expand All @@ -19,7 +18,6 @@ import (

const (
maxSessionKeyLength = 64
domainSeparator = "OpenPGPCompositeKDFv1"
MlKemSeedLen = 64
)

Expand Down Expand Up @@ -140,8 +138,8 @@ func Decrypt(priv *PrivateKey, kEphemeral, ecEphemeral, ciphertext []byte) (msg
return keywrap.Unwrap(kek, ciphertext)
}

// buildKey implements the composite KDF as specified in
// https://www.ietf.org/archive/id/draft-ietf-openpgp-pqc-05.html#name-key-combiner
// buildKey implements the composite KDF 2a from
// https://mailarchive.ietf.org/arch/msg/openpgp/NMTCy707LICtxIhP3Xt1U5C8MF0/
func buildKey(pub *PublicKey, eccSecretPoint, eccEphemeral, eccPublicKey, mlkemKeyShare, mlkemEphemeral []byte, mlkemPublicKey kem.PublicKey) ([]byte, error) {
h := sha3.New256()

Expand All @@ -160,35 +158,21 @@ func buildKey(pub *PublicKey, eccSecretPoint, eccEphemeral, eccPublicKey, mlkemK
// mlkemEphemeral - the ML-KEM ciphertext encoded as an octet string
// mlkemPublicKey - The ML-KEM public key of the recipient as an octet string
// algId - the OpenPGP algorithm ID of the public-key encryption algorithm
// domainSeparator – the UTF-8 encoding of the string "OpenPGPCompositeKDFv1"
// eccKeyShare - the ECDH key share encoded as an octet string
// eccEphemeral - the ECDH ciphertext encoded as an octet string
// eccPublicKey - The ECDH public key of the recipient as an octet string

// KEK = KMAC256(
// mlkemKeyShare || eccKeyShare,
// mlkemEphemeral || eccEphemeral || mlkemPublicKey || ecdhPublicKey || algId,
// 256 (32 bytes),
// domainSeparator
// )

kMacKeyBuffer := make([]byte, len(mlkemKeyShare)+len(eccKeyShare))
copy(kMacKeyBuffer[:len(mlkemKeyShare)], mlkemKeyShare)
copy(kMacKeyBuffer[len(mlkemKeyShare):], eccKeyShare)

k, err := kmac.NewKMAC256(kMacKeyBuffer, 32, []byte(domainSeparator))
if err != nil {
return nil, err
}

// kmac hash never returns an error
_, _ = k.Write(mlkemEphemeral)
_, _ = k.Write(eccEphemeral)
_, _ = k.Write(serializedMlkemPublicKey)
_, _ = k.Write(eccPublicKey)
_, _ = k.Write([]byte{pub.AlgId})

return k.Sum(nil), nil
// 2a. SHA3-256(mlkemKeyShare || eccKeyShare || eccEphemeral || eccPublicKey || Domain)
// where Domain is "Domain" for LAMPS, and "mlkemEphemeral || mlkemPublicKey || algId" for OpenPGP
h.Reset()
_, _ = h.Write(mlkemKeyShare)
_, _ = h.Write(eccKeyShare)
_, _ = h.Write(eccEphemeral)
_, _ = h.Write(eccPublicKey)
_, _ = h.Write(mlkemEphemeral)
_, _ = h.Write(serializedMlkemPublicKey)
_, _ = h.Write([]byte{pub.AlgId})
return h.Sum(nil), nil
}

// Validate checks that the public key corresponds to the private key
Expand Down

0 comments on commit ba0edb2

Please sign in to comment.