1
0
mirror of https://github.com/golang/go synced 2024-11-26 11:28:21 -07:00

crypto/internal/mlkem768: make Decapsulate a method

This will make it easier to support multiple sizes if needed.

Change-Id: I47495559fdbbf678fd98421ad6cb28172e5c810d
Reviewed-on: https://go-review.googlesource.com/c/go/+/621977
Reviewed-by: Daniel McCarney <daniel@binaryparadox.net>
Reviewed-by: Russ Cox <rsc@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Filippo Valsorda <filippo@golang.org>
Reviewed-by: Roland Shoemaker <roland@golang.org>
This commit is contained in:
Filippo Valsorda 2024-10-16 14:31:44 +02:00 committed by Gopher Robot
parent 9656f9296e
commit ffb8b1c586
3 changed files with 8 additions and 8 deletions

View File

@ -320,7 +320,7 @@ func pkeEncrypt(cc *[CiphertextSize]byte, ex *encryptionKey, m *[messageSize]byt
// If the ciphertext is not valid, Decapsulate returns an error. // If the ciphertext is not valid, Decapsulate returns an error.
// //
// The shared key must be kept secret. // The shared key must be kept secret.
func Decapsulate(dk *DecapsulationKey, ciphertext []byte) (sharedKey []byte, err error) { func (dk *DecapsulationKey) Decapsulate(ciphertext []byte) (sharedKey []byte, err error) {
if len(ciphertext) != CiphertextSize { if len(ciphertext) != CiphertextSize {
return nil, errors.New("mlkem768: invalid ciphertext length") return nil, errors.New("mlkem768: invalid ciphertext length")
} }

View File

@ -206,7 +206,7 @@ func TestRoundTrip(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
Kd, err := Decapsulate(dk, c) Kd, err := dk.Decapsulate(c)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -263,14 +263,14 @@ func TestBadLengths(t *testing.T) {
} }
for i := 0; i < len(c)-1; i++ { for i := 0; i < len(c)-1; i++ {
if _, err := Decapsulate(dk, c[:i]); err == nil { if _, err := dk.Decapsulate(c[:i]); err == nil {
t.Errorf("expected error for c length %d", i) t.Errorf("expected error for c length %d", i)
} }
} }
cLong := c cLong := c
for i := 0; i < 100; i++ { for i := 0; i < 100; i++ {
cLong = append(cLong, 0) cLong = append(cLong, 0)
if _, err := Decapsulate(dk, cLong); err == nil { if _, err := dk.Decapsulate(cLong); err == nil {
t.Errorf("expected error for c length %d", len(cLong)) t.Errorf("expected error for c length %d", len(cLong))
} }
} }
@ -315,7 +315,7 @@ func TestAccumulated(t *testing.T) {
o.Write(ct) o.Write(ct)
o.Write(k) o.Write(k)
kk, err := Decapsulate(dk, ct) kk, err := dk.Decapsulate(ct)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -324,7 +324,7 @@ func TestAccumulated(t *testing.T) {
} }
s.Read(ct1) s.Read(ct1)
k1, err := Decapsulate(dk, ct1) k1, err := dk.Decapsulate(ct1)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -408,7 +408,7 @@ func BenchmarkRoundTrip(b *testing.B) {
ekS := dkS.EncapsulationKey() ekS := dkS.EncapsulationKey()
sink ^= ekS[0] sink ^= ekS[0]
Ks, err := Decapsulate(dk, c) Ks, err := dk.Decapsulate(c)
if err != nil { if err != nil {
b.Fatal(err) b.Fatal(err)
} }

View File

@ -59,7 +59,7 @@ type keySharePrivateKeys struct {
// kyberDecapsulate implements decapsulation according to Kyber Round 3. // kyberDecapsulate implements decapsulation according to Kyber Round 3.
func kyberDecapsulate(dk *mlkem768.DecapsulationKey, c []byte) ([]byte, error) { func kyberDecapsulate(dk *mlkem768.DecapsulationKey, c []byte) ([]byte, error) {
K, err := mlkem768.Decapsulate(dk, c) K, err := dk.Decapsulate(c)
if err != nil { if err != nil {
return nil, err return nil, err
} }