Skip to content

Conversation

@bwesterb
Copy link
Member

No description provided.

@bwesterb bwesterb requested a review from armfazh February 16, 2023 11:18
Copy link
Contributor

@Lekensteyn Lekensteyn left a comment

Choose a reason for hiding this comment

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

Change looks reasonable, perhaps the commit message could include a further motivation.

_, _ = 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()
}

@bwesterb bwesterb merged commit d5f5e29 into main Feb 16, 2023
@bwesterb bwesterb deleted the bas/fix-ckem branch February 16, 2023 13:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants