From 3af9aa88a310f7c2b86bae1ebb3ed3843424ce9e Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Sun, 3 Feb 2019 22:00:36 +0100 Subject: [PATCH] noise: store clamped key instead of raw key --- noise-helpers.go | 12 +++++++----- noise-types.go | 6 ++++-- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/noise-helpers.go b/noise-helpers.go index 1b2746c..af11f09 100644 --- a/noise-helpers.go +++ b/noise-helpers.go @@ -78,12 +78,14 @@ func setZero(arr []byte) { } } -func newPrivateKey() (sk NoisePrivateKey, err error) { - // clamping: https://cr.yp.to/ecdh.html - _, err = rand.Read(sk[:]) +func (sk *NoisePrivateKey) clamp() { sk[0] &= 248 - sk[31] &= 127 - sk[31] |= 64 + sk[31] = (sk[31] & 127) | 64 +} + +func newPrivateKey() (sk NoisePrivateKey, err error) { + _, err = rand.Read(sk[:]) + sk.clamp() return } diff --git a/noise-types.go b/noise-types.go index df6ee77..902905e 100644 --- a/noise-types.go +++ b/noise-types.go @@ -45,8 +45,10 @@ func (key NoisePrivateKey) Equals(tar NoisePrivateKey) bool { return subtle.ConstantTimeCompare(key[:], tar[:]) == 1 } -func (key *NoisePrivateKey) FromHex(src string) error { - return loadExactHex(key[:], src) +func (key *NoisePrivateKey) FromHex(src string) (err error) { + err = loadExactHex(key[:], src) + key.clamp() + return } func (key NoisePrivateKey) ToHex() string {