Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ckem: pass xof to elliptic.GenerateKey directly #403

Merged
merged 1 commit into from
Feb 16, 2023
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