Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions kem/hybrid/ckem.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package hybrid
// TODO move over to crypto/ecdh once we can assume Go 1.20.

import (
"bytes"
"crypto/elliptic"
cryptoRand "crypto/rand"
"crypto/subtle"
Expand Down Expand Up @@ -123,10 +122,7 @@ func (sch *cScheme) DeriveKeyPair(seed []byte) (kem.PublicKey, kem.PrivateKey) {
}
h := xof.SHAKE256.New()
_, _ = h.Write(seed)
buf := make([]byte, sch.PrivateKeySize())
_, _ = h.Read(buf)
rnd := bytes.NewReader(buf)
key, x, y, err := elliptic.GenerateKey(sch.curve, rnd)
key, x, y, err := elliptic.GenerateKey(sch.curve, h)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a bugfix? Previously, only enough randomness was provided for exactly one "private key". After this change, it could sample another key if the first secret was invalid:

func GenerateKey(curve Curve, rand io.Reader) (priv []byte, x, y *big.Int, err error) {
	N := curve.Params().N
	bitSize := N.BitLen()
	byteLen := (bitSize + 7) / 8
	priv = make([]byte, byteLen)

	for x == nil {
		_, err = io.ReadFull(rand, priv)
// ...
		// If the scalar is out of range, sample another random number.
		if new(big.Int).SetBytes(priv).Cmp(N) >= 0 {
			continue
		}

		x, y = curve.ScalarBaseMult(priv)
	}

For reference, sch.PrivateKeySize() ends up with the same size:

func (sch *cScheme) scSize() int {
	return (sch.curve.Params().N.BitLen() + 7) / 8
}
// ...
func (sch *cScheme) PrivateKeySize() int {
	return sch.scSize()
}

if err != nil {
panic(err)
}
Expand Down