Skip to content

Commit cce75da

Browse files
alecbakholdingopherbot
authored andcommitted
crypto/mlkem: swap order of return values of Encapsulate
Per FIPS 203 (https://csrc.nist.gov/pubs/fips/203/final), the order of return values should be sharedKey, ciphertext. This commit simply swaps those return values and updates any consumers of the Encapsulate() method to respect the new order. Fixes #70950 Change-Id: I2a0d605e3baf7fe69510d60d3d35bbac18f883c9 Reviewed-on: https://go-review.googlesource.com/c/go/+/638376 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Austin Clements <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]> Reviewed-by: Filippo Valsorda <[email protected]> Reviewed-by: Cherry Mui <[email protected]>
1 parent 772f024 commit cce75da

File tree

7 files changed

+25
-25
lines changed

7 files changed

+25
-25
lines changed

src/crypto/internal/fips140/mlkem/cast.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func init() {
4040
dk := &DecapsulationKey768{}
4141
kemKeyGen(dk, d, z)
4242
ek := dk.EncapsulationKey()
43-
c, Ke := ek.EncapsulateInternal(m)
43+
Ke, c := ek.EncapsulateInternal(m)
4444
Kd, err := dk.Decapsulate(c)
4545
if err != nil {
4646
return err

src/crypto/internal/fips140/mlkem/mlkem1024.go

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/crypto/internal/fips140/mlkem/mlkem768.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ func kemKeyGen(dk *DecapsulationKey768, d, z *[32]byte) {
246246
// the first operational use (if not exported before the first use)."
247247
func kemPCT(dk *DecapsulationKey768) error {
248248
ek := dk.EncapsulationKey()
249-
c, K := ek.Encapsulate()
249+
K, c := ek.Encapsulate()
250250
K1, err := dk.Decapsulate(c)
251251
if err != nil {
252252
return err
@@ -261,13 +261,13 @@ func kemPCT(dk *DecapsulationKey768) error {
261261
// encapsulation key, drawing random bytes from a DRBG.
262262
//
263263
// The shared key must be kept secret.
264-
func (ek *EncapsulationKey768) Encapsulate() (ciphertext, sharedKey []byte) {
264+
func (ek *EncapsulationKey768) Encapsulate() (sharedKey, ciphertext []byte) {
265265
// The actual logic is in a separate function to outline this allocation.
266266
var cc [CiphertextSize768]byte
267267
return ek.encapsulate(&cc)
268268
}
269269

270-
func (ek *EncapsulationKey768) encapsulate(cc *[CiphertextSize768]byte) (ciphertext, sharedKey []byte) {
270+
func (ek *EncapsulationKey768) encapsulate(cc *[CiphertextSize768]byte) (sharedKey, ciphertext []byte) {
271271
var m [messageSize]byte
272272
drbg.Read(m[:])
273273
// Note that the modulus check (step 2 of the encapsulation key check from
@@ -278,22 +278,22 @@ func (ek *EncapsulationKey768) encapsulate(cc *[CiphertextSize768]byte) (ciphert
278278

279279
// EncapsulateInternal is a derandomized version of Encapsulate, exclusively for
280280
// use in tests.
281-
func (ek *EncapsulationKey768) EncapsulateInternal(m *[32]byte) (ciphertext, sharedKey []byte) {
281+
func (ek *EncapsulationKey768) EncapsulateInternal(m *[32]byte) (sharedKey, ciphertext []byte) {
282282
cc := &[CiphertextSize768]byte{}
283283
return kemEncaps(cc, ek, m)
284284
}
285285

286286
// kemEncaps generates a shared key and an associated ciphertext.
287287
//
288288
// It implements ML-KEM.Encaps_internal according to FIPS 203, Algorithm 17.
289-
func kemEncaps(cc *[CiphertextSize768]byte, ek *EncapsulationKey768, m *[messageSize]byte) (c, K []byte) {
289+
func kemEncaps(cc *[CiphertextSize768]byte, ek *EncapsulationKey768, m *[messageSize]byte) (K, c []byte) {
290290
g := sha3.New512()
291291
g.Write(m[:])
292292
g.Write(ek.h[:])
293293
G := g.Sum(nil)
294294
K, r := G[:SharedKeySize], G[SharedKeySize:]
295295
c = pkeEncrypt(cc, &ek.encryptionKey, m, r)
296-
return c, K
296+
return K, c
297297
}
298298

299299
// NewEncapsulationKey768 parses an encapsulation key from its encoded form.

src/crypto/mlkem/mlkem1024.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,6 @@ func (ek *EncapsulationKey1024) Bytes() []byte {
9191
// encapsulation key, drawing random bytes from crypto/rand.
9292
//
9393
// The shared key must be kept secret.
94-
func (ek *EncapsulationKey1024) Encapsulate() (ciphertext, sharedKey []byte) {
94+
func (ek *EncapsulationKey1024) Encapsulate() (sharedKey, ciphertext []byte) {
9595
return ek.key.Encapsulate()
9696
}

src/crypto/mlkem/mlkem768.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,6 @@ func (ek *EncapsulationKey768) Bytes() []byte {
101101
// encapsulation key, drawing random bytes from crypto/rand.
102102
//
103103
// The shared key must be kept secret.
104-
func (ek *EncapsulationKey768) Encapsulate() (ciphertext, sharedKey []byte) {
104+
func (ek *EncapsulationKey768) Encapsulate() (sharedKey, ciphertext []byte) {
105105
return ek.key.Encapsulate()
106106
}

src/crypto/mlkem/mlkem_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func testRoundTrip[E encapsulationKey, D decapsulationKey[E]](
4343
t.Fatal(err)
4444
}
4545
ek := dk.EncapsulationKey()
46-
c, Ke := ek.Encapsulate()
46+
Ke, c := ek.Encapsulate()
4747
Kd, err := dk.Decapsulate(c)
4848
if err != nil {
4949
t.Fatal(err)
@@ -66,7 +66,7 @@ func testRoundTrip[E encapsulationKey, D decapsulationKey[E]](
6666
if !bytes.Equal(dk.Bytes(), dk1.Bytes()) {
6767
t.Fail()
6868
}
69-
c1, Ke1 := ek1.Encapsulate()
69+
Ke1, c1 := ek1.Encapsulate()
7070
Kd1, err := dk1.Decapsulate(c1)
7171
if err != nil {
7272
t.Fatal(err)
@@ -86,7 +86,7 @@ func testRoundTrip[E encapsulationKey, D decapsulationKey[E]](
8686
t.Fail()
8787
}
8888

89-
c2, Ke2 := dk.EncapsulationKey().Encapsulate()
89+
Ke2, c2 := dk.EncapsulationKey().Encapsulate()
9090
if bytes.Equal(c, c2) {
9191
t.Fail()
9292
}
@@ -115,7 +115,7 @@ func testBadLengths[E encapsulationKey, D decapsulationKey[E]](
115115
}
116116
ek := dk.EncapsulationKey()
117117
ekBytes := dk.EncapsulationKey().Bytes()
118-
c, _ := ek.Encapsulate()
118+
_, c := ek.Encapsulate()
119119

120120
for i := 0; i < len(dkBytes)-1; i++ {
121121
if _, err := newDecapsulationKey(dkBytes[:i]); err == nil {
@@ -189,7 +189,7 @@ func TestAccumulated(t *testing.T) {
189189
o.Write(ek.Bytes())
190190

191191
s.Read(msg[:])
192-
ct, k := ek.key.EncapsulateInternal(&msg)
192+
k, ct := ek.key.EncapsulateInternal(&msg)
193193
o.Write(ct)
194194
o.Write(k)
195195

@@ -244,7 +244,7 @@ func BenchmarkEncaps(b *testing.B) {
244244
if err != nil {
245245
b.Fatal(err)
246246
}
247-
c, K := ek.key.EncapsulateInternal(&m)
247+
K, c := ek.key.EncapsulateInternal(&m)
248248
sink ^= c[0] ^ K[0]
249249
}
250250
}
@@ -255,7 +255,7 @@ func BenchmarkDecaps(b *testing.B) {
255255
b.Fatal(err)
256256
}
257257
ek := dk.EncapsulationKey()
258-
c, _ := ek.Encapsulate()
258+
_, c := ek.Encapsulate()
259259
b.ResetTimer()
260260
for i := 0; i < b.N; i++ {
261261
K, _ := dk.Decapsulate(c)
@@ -270,7 +270,7 @@ func BenchmarkRoundTrip(b *testing.B) {
270270
}
271271
ek := dk.EncapsulationKey()
272272
ekBytes := ek.Bytes()
273-
c, _ := ek.Encapsulate()
273+
_, c := ek.Encapsulate()
274274
if err != nil {
275275
b.Fatal(err)
276276
}
@@ -296,7 +296,7 @@ func BenchmarkRoundTrip(b *testing.B) {
296296
if err != nil {
297297
b.Fatal(err)
298298
}
299-
cS, Ks := ek.Encapsulate()
299+
Ks, cS := ek.Encapsulate()
300300
if err != nil {
301301
b.Fatal(err)
302302
}

src/crypto/tls/handshake_server_tls13.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ func (hs *serverHandshakeStateTLS13) processClientHello() error {
280280
c.sendAlert(alertIllegalParameter)
281281
return errors.New("tls: invalid X25519MLKEM768 client key share")
282282
}
283-
ciphertext, mlkemSharedSecret := k.Encapsulate()
283+
mlkemSharedSecret, ciphertext := k.Encapsulate()
284284
// draft-kwiatkowski-tls-ecdhe-mlkem-02, Section 3.1.3: "For
285285
// X25519MLKEM768, the shared secret is the concatenation of the ML-KEM
286286
// shared secret and the X25519 shared secret. The shared secret is 64

0 commit comments

Comments
 (0)