]> Cypherpunks repositories - gostls13.git/commitdiff
crypto/tls: reject low-order Curve25519 points
authorFilippo Valsorda <filippo@golang.org>
Wed, 19 Jun 2019 22:31:43 +0000 (18:31 -0400)
committerFilippo Valsorda <filippo@golang.org>
Thu, 20 Jun 2019 19:30:01 +0000 (19:30 +0000)
The RFC recommends checking the X25519 output to ensure it's not the
zero value, to guard against peers trying to remove contributory
behavior.

In TLS there should be enough transcript involvement to mitigate any
attack, and the RSA key exchange would suffer from the same issues by
design, so not proposing a backport.

See #31846

Change-Id: I8e657f8ee8aa72c3f8ca3b124555202638c53f5e
Reviewed-on: https://go-review.googlesource.com/c/go/+/183039
Run-TryBot: Filippo Valsorda <filippo@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Adam Langley <agl@golang.org>
src/crypto/tls/key_schedule.go

index 3cd6e8297a42a96a9cffc886d875f429d552f726..83e5480fc5ed9eb6e2988f29dd4ab98c463e7240 100644 (file)
@@ -7,6 +7,7 @@ package tls
 import (
        "crypto/elliptic"
        "crypto/hmac"
+       "crypto/subtle"
        "errors"
        "golang.org/x/crypto/cryptobyte"
        "golang.org/x/crypto/curve25519"
@@ -193,8 +194,16 @@ func (p *x25519Parameters) SharedKey(peerPublicKey []byte) []byte {
        if len(peerPublicKey) != 32 {
                return nil
        }
+
        var theirPublicKey, sharedKey [32]byte
        copy(theirPublicKey[:], peerPublicKey)
        curve25519.ScalarMult(&sharedKey, &p.privateKey, &theirPublicKey)
+
+       // Check for low-order inputs. See RFC 8422, Section 5.11.
+       var allZeroes [32]byte
+       if subtle.ConstantTimeCompare(allZeroes[:], sharedKey[:]) == 1 {
+               return nil
+       }
+
        return sharedKey[:]
 }