Skip to content

Commit a2946f2

Browse files
committed
crypto: add Encapsulator and Decapsulator interfaces
Updates #75300 Change-Id: I6a6a6964a0ab36ee3132d8481515c34c86011c13 Reviewed-on: https://go-review.googlesource.com/c/go/+/705796 Reviewed-by: Mark Freeman <[email protected]> Reviewed-by: Daniel McCarney <[email protected]> Reviewed-by: Junyang Shao <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 6b83bd7 commit a2946f2

File tree

5 files changed

+55
-1
lines changed

5 files changed

+55
-1
lines changed

api/next/75300.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1+
pkg crypto, type Decapsulator interface { Decapsulate, Encapsulator } #75300
2+
pkg crypto, type Decapsulator interface, Decapsulate([]uint8) ([]uint8, error) #75300
3+
pkg crypto, type Decapsulator interface, Encapsulator() Encapsulator #75300
4+
pkg crypto, type Encapsulator interface { Bytes, Encapsulate } #75300
5+
pkg crypto, type Encapsulator interface, Bytes() []uint8 #75300
6+
pkg crypto, type Encapsulator interface, Encapsulate() ([]uint8, []uint8) #75300
17
pkg crypto/ecdh, type KeyExchanger interface { Curve, ECDH, PublicKey } #75300
28
pkg crypto/ecdh, type KeyExchanger interface, Curve() Curve #75300
39
pkg crypto/ecdh, type KeyExchanger interface, ECDH(*PublicKey) ([]uint8, error) #75300
410
pkg crypto/ecdh, type KeyExchanger interface, PublicKey() *PublicKey #75300
11+
pkg crypto/mlkem, method (*DecapsulationKey1024) Encapsulator() crypto.Encapsulator #75300
12+
pkg crypto/mlkem, method (*DecapsulationKey768) Encapsulator() crypto.Encapsulator #75300
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
The new [Encapsulator] and [Decapsulator] interfaces allow accepting abstract
2+
KEM encapsulation or decapsulation keys.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
The new [DecapsulationKey768.Encapsulator] and
2+
[DecapsulationKey1024.Encapsulator] methods implement the new
3+
[crypto.Decapsulator] interface.

src/crypto/crypto.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,3 +253,21 @@ func SignMessage(signer Signer, rand io.Reader, msg []byte, opts SignerOpts) (si
253253
}
254254
return signer.Sign(rand, msg, opts)
255255
}
256+
257+
// Decapsulator is an interface for an opaque private KEM key that can be used for
258+
// decapsulation operations. For example, an ML-KEM key kept in a hardware module.
259+
//
260+
// It is implemented, for example, by [crypto/mlkem.DecapsulationKey768].
261+
type Decapsulator interface {
262+
Encapsulator() Encapsulator
263+
Decapsulate(ciphertext []byte) (sharedKey []byte, err error)
264+
}
265+
266+
// Encapsulator is an interface for a public KEM key that can be used for
267+
// encapsulation operations.
268+
//
269+
// It is implemented, for example, by [crypto/mlkem.EncapsulationKey768].
270+
type Encapsulator interface {
271+
Bytes() []byte
272+
Encapsulate() (sharedKey, ciphertext []byte)
273+
}

src/crypto/mlkem/mlkem.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
// [NIST FIPS 203]: https://doi.org/10.6028/NIST.FIPS.203
1212
package mlkem
1313

14-
import "crypto/internal/fips140/mlkem"
14+
import (
15+
"crypto"
16+
"crypto/internal/fips140/mlkem"
17+
)
1518

1619
const (
1720
// SharedKeySize is the size of a shared key produced by ML-KEM.
@@ -82,6 +85,16 @@ func (dk *DecapsulationKey768) EncapsulationKey() *EncapsulationKey768 {
8285
return &EncapsulationKey768{dk.key.EncapsulationKey()}
8386
}
8487

88+
// Encapsulator returns the encapsulation key, like
89+
// [DecapsulationKey768.EncapsulationKey].
90+
//
91+
// It implements [crypto.Decapsulator].
92+
func (dk *DecapsulationKey768) Encapsulator() crypto.Encapsulator {
93+
return dk.EncapsulationKey()
94+
}
95+
96+
var _ crypto.Decapsulator = (*DecapsulationKey768)(nil)
97+
8598
// An EncapsulationKey768 is the public key used to produce ciphertexts to be
8699
// decapsulated by the corresponding DecapsulationKey768.
87100
type EncapsulationKey768 struct {
@@ -164,6 +177,16 @@ func (dk *DecapsulationKey1024) EncapsulationKey() *EncapsulationKey1024 {
164177
return &EncapsulationKey1024{dk.key.EncapsulationKey()}
165178
}
166179

180+
// Encapsulator returns the encapsulation key, like
181+
// [DecapsulationKey1024.EncapsulationKey].
182+
//
183+
// It implements [crypto.Decapsulator].
184+
func (dk *DecapsulationKey1024) Encapsulator() crypto.Encapsulator {
185+
return dk.EncapsulationKey()
186+
}
187+
188+
var _ crypto.Decapsulator = (*DecapsulationKey1024)(nil)
189+
167190
// An EncapsulationKey1024 is the public key used to produce ciphertexts to be
168191
// decapsulated by the corresponding DecapsulationKey1024.
169192
type EncapsulationKey1024 struct {

0 commit comments

Comments
 (0)