Skip to content

Commit

Permalink
mlkem768,xwing: add SeedSize
Browse files Browse the repository at this point in the history
  • Loading branch information
FiloSottile committed Feb 21, 2024
1 parent b43add9 commit 5ce9162
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
5 changes: 3 additions & 2 deletions mlkem768.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ const (
EncapsulationKeySize = encryptionKeySize
DecapsulationKeySize = decryptionKeySize + encryptionKeySize + 32 + 32
SharedKeySize = 32
SeedSize = 32 + 32
)

// GenerateKey generates an encapsulation key and a corresponding decapsulation
Expand All @@ -86,10 +87,10 @@ func GenerateKey() (encapsulationKey, decapsulationKey []byte, err error) {
// corresponding decapsulation key from a 64-byte seed. The seed must be
// uniformly random.
func NewKeyFromSeed(seed []byte) (encapsulationKey, decapsulationKey []byte, err error) {
if len(seed) != 64 {
if len(seed) != SeedSize {
return nil, nil, errors.New("mlkem768: invalid seed length")
}
ek, dk := kemKeyGen(seed[0:32], seed[32:64])
ek, dk := kemKeyGen(seed[:32], seed[32:])
return ek, dk, nil
}

Expand Down
7 changes: 4 additions & 3 deletions xwing/xwing.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const (
EncapsulationKeySize = mlkem768.EncapsulationKeySize + 32
DecapsulationKeySize = mlkem768.DecapsulationKeySize + 32 + 32
SharedKeySize = 32
SeedSize = mlkem768.SeedSize + 32
)

// GenerateKey generates an encapsulation key and a corresponding decapsulation
Expand All @@ -48,18 +49,18 @@ func GenerateKey() (encapsulationKey, decapsulationKey []byte, err error) {
// corresponding decapsulation key from a 96-byte seed. The seed must be
// uniformly random.
func NewKeyFromSeed(seed []byte) (encapsulationKey, decapsulationKey []byte, err error) {
if len(seed) != 96 {
if len(seed) != SeedSize {
return nil, nil, errors.New("xwing: invalid seed length")
}

skX := seed[64:96]
skX := seed[mlkem768.SeedSize:]
x, err := ecdh.X25519().NewPrivateKey(skX)
if err != nil {
return nil, nil, err
}
pkX := x.PublicKey().Bytes()

pkM, skM, err := mlkem768.NewKeyFromSeed(seed[0:64])
pkM, skM, err := mlkem768.NewKeyFromSeed(seed[:mlkem768.SeedSize])
if err != nil {
return nil, nil, err
}
Expand Down

0 comments on commit 5ce9162

Please sign in to comment.