Skip to content

Commit e9ff5ef

Browse files
committed
feat(pqc): draft-ietf-openpgp-pqc-06 with updated kem combiner
Updated kem-combiner: openpgp-pqc/draft-openpgp-pqc#161
1 parent 5154bfa commit e9ff5ef

File tree

5 files changed

+704
-488
lines changed

5 files changed

+704
-488
lines changed

internal/kmac/kmac.go

Lines changed: 0 additions & 147 deletions
This file was deleted.

internal/kmac/kmac_test.go

Lines changed: 0 additions & 142 deletions
This file was deleted.

openpgp/mlkem_ecdh/mlkem_ecdh.go

Lines changed: 17 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"fmt"
88
"io"
99

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

@@ -19,8 +18,8 @@ import (
1918

2019
const (
2120
maxSessionKeyLength = 64
22-
domainSeparator = "OpenPGPCompositeKDFv1"
2321
MlKemSeedLen = 64
22+
kdfContext = "OpenPGPCompositeKDFv1"
2423
)
2524

2625
type PublicKey struct {
@@ -140,16 +139,11 @@ func Decrypt(priv *PrivateKey, kEphemeral, ecEphemeral, ciphertext []byte) (msg
140139
return keywrap.Unwrap(kek, ciphertext)
141140
}
142141

143-
// buildKey implements the composite KDF as specified in
144-
// https://www.ietf.org/archive/id/draft-ietf-openpgp-pqc-05.html#name-key-combiner
142+
// buildKey implements the composite KDF from
143+
// https://github.com/openpgp-pqc/draft-openpgp-pqc/pull/161
145144
func buildKey(pub *PublicKey, eccSecretPoint, eccEphemeral, eccPublicKey, mlkemKeyShare, mlkemEphemeral []byte, mlkemPublicKey kem.PublicKey) ([]byte, error) {
146-
h := sha3.New256()
147-
148-
// SHA3 never returns error
149-
_, _ = h.Write(eccSecretPoint)
150-
_, _ = h.Write(eccEphemeral)
151-
_, _ = h.Write(eccPublicKey)
152-
eccKeyShare := h.Sum(nil)
145+
/// Set the output `ecdhKeyShare` to `eccSecretPoint`
146+
eccKeyShare := eccSecretPoint
153147

154148
serializedMlkemPublicKey, err := mlkemPublicKey.MarshalBinary()
155149
if err != nil {
@@ -160,35 +154,22 @@ func buildKey(pub *PublicKey, eccSecretPoint, eccEphemeral, eccPublicKey, mlkemK
160154
// mlkemEphemeral - the ML-KEM ciphertext encoded as an octet string
161155
// mlkemPublicKey - The ML-KEM public key of the recipient as an octet string
162156
// algId - the OpenPGP algorithm ID of the public-key encryption algorithm
163-
// domainSeparator – the UTF-8 encoding of the string "OpenPGPCompositeKDFv1"
164157
// eccKeyShare - the ECDH key share encoded as an octet string
165158
// eccEphemeral - the ECDH ciphertext encoded as an octet string
166159
// eccPublicKey - The ECDH public key of the recipient as an octet string
167160

168-
// KEK = KMAC256(
169-
// mlkemKeyShare || eccKeyShare,
170-
// mlkemEphemeral || eccEphemeral || mlkemPublicKey || ecdhPublicKey || algId,
171-
// 256 (32 bytes),
172-
// domainSeparator
173-
// )
174-
175-
kMacKeyBuffer := make([]byte, len(mlkemKeyShare)+len(eccKeyShare))
176-
copy(kMacKeyBuffer[:len(mlkemKeyShare)], mlkemKeyShare)
177-
copy(kMacKeyBuffer[len(mlkemKeyShare):], eccKeyShare)
178-
179-
k, err := kmac.NewKMAC256(kMacKeyBuffer, 32, []byte(domainSeparator))
180-
if err != nil {
181-
return nil, err
182-
}
183-
184-
// kmac hash never returns an error
185-
_, _ = k.Write(mlkemEphemeral)
186-
_, _ = k.Write(eccEphemeral)
187-
_, _ = k.Write(serializedMlkemPublicKey)
188-
_, _ = k.Write(eccPublicKey)
189-
_, _ = k.Write([]byte{pub.AlgId})
190-
191-
return k.Sum(nil), nil
161+
// SHA3-256(mlkemKeyShare || eccKeyShare || eccEphemeral || eccPublicKey ||
162+
// mlkemEphemeral || mlkemPublicKey || algId || "OpenPGPCompositeKDFv1")
163+
h := sha3.New256()
164+
_, _ = h.Write(mlkemKeyShare)
165+
_, _ = h.Write(eccKeyShare)
166+
_, _ = h.Write(eccEphemeral)
167+
_, _ = h.Write(eccPublicKey)
168+
_, _ = h.Write(mlkemEphemeral)
169+
_, _ = h.Write(serializedMlkemPublicKey)
170+
_, _ = h.Write([]byte{pub.AlgId})
171+
_, _ = h.Write([]byte(kdfContext))
172+
return h.Sum(nil), nil
192173
}
193174

194175
// Validate checks that the public key corresponds to the private key

0 commit comments

Comments
 (0)