]> Cypherpunks.ru repositories - gostls13.git/commitdiff
all: boringcrypto post-merge cleanup
authorRuss Cox <rsc@golang.org>
Wed, 4 May 2022 12:58:02 +0000 (08:58 -0400)
committerRuss Cox <rsc@golang.org>
Mon, 6 Jun 2022 19:10:07 +0000 (19:10 +0000)
This CL addresses the comments on CL 403154.

For #51940.

Change-Id: I99bb3530916d469077bfbd53095bfcd1d2aa82ef
Reviewed-on: https://go-review.googlesource.com/c/go/+/403976
Reviewed-by: Roland Shoemaker <roland@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
13 files changed:
src/cmd/go/internal/modindex/build.go
src/cmd/internal/notsha256/example_test.go [deleted file]
src/crypto/aes/cipher.go
src/crypto/aes/cipher_asm.go
src/crypto/ecdsa/ecdsa.go
src/crypto/hmac/hmac.go
src/crypto/internal/boring/aes.go
src/crypto/internal/boring/notboring.go
src/crypto/rand/rand_unix.go
src/crypto/rsa/pkcs1v15.go
src/crypto/rsa/pss.go
src/crypto/tls/cipher_suites.go
src/go/build/build.go

index 78bd12636d96c33af49a56b55f7c63d2164246d0..9d52be851b21df7cc051bd93edf1c56e8da634b4 100644 (file)
@@ -887,8 +887,8 @@ func (ctxt *Context) eval(x constraint.Expr, allTags map[string]bool) bool {
 //     $GOARCH
 //     boringcrypto
 //     ctxt.Compiler
-//     linux (if GOOS = android)
-//     solaris (if GOOS = illumos)
+//     linux (if GOOS == android)
+//     solaris (if GOOS == illumos)
 //     tag (if tag is listed in ctxt.BuildTags or ctxt.ReleaseTags)
 //
 // It records all consulted tags in allTags.
diff --git a/src/cmd/internal/notsha256/example_test.go b/src/cmd/internal/notsha256/example_test.go
deleted file mode 100644 (file)
index 06e9c37..0000000
+++ /dev/null
@@ -1,41 +0,0 @@
-// Copyright 2016 The Go Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE file.
-
-package notsha256_test
-
-import (
-       "crypto/sha256"
-       "fmt"
-       "io"
-       "log"
-       "os"
-)
-
-func ExampleSum256() {
-       sum := sha256.Sum256([]byte("hello world\n"))
-       fmt.Printf("%x", sum)
-       // Output: a948904f2f0f479b8f8197694b30184b0d2ed1c1cd2a1ec0fb85d299a192a447
-}
-
-func ExampleNew() {
-       h := sha256.New()
-       h.Write([]byte("hello world\n"))
-       fmt.Printf("%x", h.Sum(nil))
-       // Output: a948904f2f0f479b8f8197694b30184b0d2ed1c1cd2a1ec0fb85d299a192a447
-}
-
-func ExampleNew_file() {
-       f, err := os.Open("file.txt")
-       if err != nil {
-               log.Fatal(err)
-       }
-       defer f.Close()
-
-       h := sha256.New()
-       if _, err := io.Copy(h, f); err != nil {
-               log.Fatal(err)
-       }
-
-       fmt.Printf("%x", h.Sum(nil))
-}
index 29d01796eb398fe5842e5696f933b0f91b8a35c9..db0ee38b78cea57e44cff31868025a58cfcff7e3 100644 (file)
@@ -6,12 +6,11 @@ package aes
 
 import (
        "crypto/cipher"
+       "crypto/internal/boring"
        "crypto/internal/subtle"
        "strconv"
 )
 
-import "crypto/internal/boring"
-
 // The AES block size in bytes.
 const BlockSize = 16
 
index b7e59d7edb6bd4c4425ddaf8fb1ad9362330bd9a..1482b22d08b5f4a392e2401402607e7629af8033 100644 (file)
@@ -8,13 +8,12 @@ package aes
 
 import (
        "crypto/cipher"
+       "crypto/internal/boring"
        "crypto/internal/subtle"
        "internal/cpu"
        "internal/goarch"
 )
 
-import "crypto/internal/boring"
-
 // defined in asm_*.s
 
 //go:noescape
index 7ce7542872c48c528d9164620358aad880dc153b..d0e52ad864d8115da746223e21ed7a52a321eb94 100644 (file)
@@ -24,6 +24,7 @@ import (
        "crypto/aes"
        "crypto/cipher"
        "crypto/elliptic"
+       "crypto/internal/boring"
        "crypto/internal/boring/bbig"
        "crypto/internal/randutil"
        "crypto/sha512"
@@ -31,8 +32,6 @@ import (
        "io"
        "math/big"
 
-       "crypto/internal/boring"
-
        "golang.org/x/crypto/cryptobyte"
        "golang.org/x/crypto/cryptobyte/asn1"
 )
index 34805765d550c17234bafc0fc7e8215bbb70b81b..ed3ebc06027092bb8aea36ab1aa04a11f77f8e2e 100644 (file)
@@ -22,12 +22,11 @@ timing side-channels:
 package hmac
 
 import (
+       "crypto/internal/boring"
        "crypto/subtle"
        "hash"
 )
 
-import "crypto/internal/boring"
-
 // FIPS 198-1:
 // https://csrc.nist.gov/publications/fips/fips198-1/FIPS-198-1_final.pdf
 
index 515b60bb8a3fe3fb99f3735e29a8761a614f671c..eaa1adc892796b36249dcf099ae07ab8a913e4f2 100644 (file)
@@ -72,9 +72,6 @@ type extraModes interface {
        NewCBCDecrypter(iv []byte) cipher.BlockMode
        NewCTR(iv []byte) cipher.Stream
        NewGCM(nonceSize, tagSize int) (cipher.AEAD, error)
-
-       // Invented for BoringCrypto.
-       NewGCMTLS() (cipher.AEAD, error)
 }
 
 var _ extraModes = (*aesCipher)(nil)
@@ -235,8 +232,8 @@ func (c *aesCipher) NewGCM(nonceSize, tagSize int) (cipher.AEAD, error) {
        return c.newGCM(false)
 }
 
-func (c *aesCipher) NewGCMTLS() (cipher.AEAD, error) {
-       return c.newGCM(true)
+func NewGCMTLS(c cipher.Block) (cipher.AEAD, error) {
+       return c.(*aesCipher).newGCM(true)
 }
 
 func (c *aesCipher) newGCM(tls bool) (cipher.AEAD, error) {
index bb88fb00048cddc40f507567c1d98ffb64432635..53096a68d194a97ad7efa8f060d5d86f378f9a36 100644 (file)
@@ -50,6 +50,7 @@ func SHA512([]byte) [64]byte { panic("boringcrypto: not available") }
 func NewHMAC(h func() hash.Hash, key []byte) hash.Hash { panic("boringcrypto: not available") }
 
 func NewAESCipher(key []byte) (cipher.Block, error) { panic("boringcrypto: not available") }
+func NewGCMTLS(cipher.Block) (cipher.AEAD, error)   { panic("boringcrypto: not available") }
 
 type PublicKeyECDSA struct{ _ int }
 type PrivateKeyECDSA struct{ _ int }
index 830983c74ac65c42c5467722fad38b5fa25d3839..746e90cc919a4e1fa1041bf2fa09ee98ebf1580b 100644 (file)
@@ -10,6 +10,7 @@
 package rand
 
 import (
+       "crypto/internal/boring"
        "errors"
        "io"
        "os"
@@ -19,8 +20,6 @@ import (
        "time"
 )
 
-import "crypto/internal/boring"
-
 const urandomDevice = "/dev/urandom"
 
 func init() {
index 8cf3b6e255b0c32e66a660e8c15941260eebd1eb..ab19229a6c282279ca2bf68fea4dc8b9b2dcf332 100644 (file)
@@ -6,16 +6,14 @@ package rsa
 
 import (
        "crypto"
+       "crypto/internal/boring"
+       "crypto/internal/randutil"
        "crypto/subtle"
        "errors"
        "io"
        "math/big"
-
-       "crypto/internal/randutil"
 )
 
-import "crypto/internal/boring"
-
 // This file implements encryption and decryption using PKCS #1 v1.5 padding.
 
 // PKCS1v15DecrypterOpts is for passing options to PKCS #1 v1.5 decryption using
@@ -32,7 +30,7 @@ type PKCS1v15DecryptOptions struct {
 // scheme from PKCS #1 v1.5.  The message must be no longer than the
 // length of the public modulus minus 11 bytes.
 //
-// The rand parameter is used as a source of entropy to ensure that
+// The random parameter is used as a source of entropy to ensure that
 // encrypting the same message twice doesn't result in the same
 // ciphertext.
 //
@@ -84,14 +82,14 @@ func EncryptPKCS1v15(random io.Reader, pub *PublicKey, msg []byte) ([]byte, erro
 }
 
 // DecryptPKCS1v15 decrypts a plaintext using RSA and the padding scheme from PKCS #1 v1.5.
-// If rand != nil, it uses RSA blinding to avoid timing side-channel attacks.
+// If random != nil, it uses RSA blinding to avoid timing side-channel attacks.
 //
 // Note that whether this function returns an error or not discloses secret
 // information. If an attacker can cause this function to run repeatedly and
 // learn whether each instance returned an error then they can decrypt and
 // forge signatures as if they had the private key. See
 // DecryptPKCS1v15SessionKey for a way of solving this problem.
-func DecryptPKCS1v15(rand io.Reader, priv *PrivateKey, ciphertext []byte) ([]byte, error) {
+func DecryptPKCS1v15(random io.Reader, priv *PrivateKey, ciphertext []byte) ([]byte, error) {
        if err := checkPub(&priv.PublicKey); err != nil {
                return nil, err
        }
@@ -108,7 +106,7 @@ func DecryptPKCS1v15(rand io.Reader, priv *PrivateKey, ciphertext []byte) ([]byt
                return out, nil
        }
 
-       valid, out, index, err := decryptPKCS1v15(rand, priv, ciphertext)
+       valid, out, index, err := decryptPKCS1v15(random, priv, ciphertext)
        if err != nil {
                return nil, err
        }
@@ -119,7 +117,7 @@ func DecryptPKCS1v15(rand io.Reader, priv *PrivateKey, ciphertext []byte) ([]byt
 }
 
 // DecryptPKCS1v15SessionKey decrypts a session key using RSA and the padding scheme from PKCS #1 v1.5.
-// If rand != nil, it uses RSA blinding to avoid timing side-channel attacks.
+// If random != nil, it uses RSA blinding to avoid timing side-channel attacks.
 // It returns an error if the ciphertext is the wrong length or if the
 // ciphertext is greater than the public modulus. Otherwise, no error is
 // returned. If the padding is valid, the resulting plaintext message is copied
@@ -137,7 +135,7 @@ func DecryptPKCS1v15(rand io.Reader, priv *PrivateKey, ciphertext []byte) ([]byt
 // a random value was used (because it'll be different for the same ciphertext)
 // and thus whether the padding was correct. This defeats the point of this
 // function. Using at least a 16-byte key will protect against this attack.
-func DecryptPKCS1v15SessionKey(rand io.Reader, priv *PrivateKey, ciphertext []byte, key []byte) error {
+func DecryptPKCS1v15SessionKey(random io.Reader, priv *PrivateKey, ciphertext []byte, key []byte) error {
        if err := checkPub(&priv.PublicKey); err != nil {
                return err
        }
@@ -146,7 +144,7 @@ func DecryptPKCS1v15SessionKey(rand io.Reader, priv *PrivateKey, ciphertext []by
                return ErrDecryption
        }
 
-       valid, em, index, err := decryptPKCS1v15(rand, priv, ciphertext)
+       valid, em, index, err := decryptPKCS1v15(random, priv, ciphertext)
        if err != nil {
                return err
        }
@@ -163,12 +161,12 @@ func DecryptPKCS1v15SessionKey(rand io.Reader, priv *PrivateKey, ciphertext []by
 }
 
 // decryptPKCS1v15 decrypts ciphertext using priv and blinds the operation if
-// rand is not nil. It returns one or zero in valid that indicates whether the
+// random is not nil. It returns one or zero in valid that indicates whether the
 // plaintext was correctly structured. In either case, the plaintext is
 // returned in em so that it may be read independently of whether it was valid
 // in order to maintain constant memory access patterns. If the plaintext was
 // valid then index contains the index of the original message in em.
-func decryptPKCS1v15(rand io.Reader, priv *PrivateKey, ciphertext []byte) (valid int, em []byte, index int, err error) {
+func decryptPKCS1v15(random io.Reader, priv *PrivateKey, ciphertext []byte) (valid int, em []byte, index int, err error) {
        k := priv.Size()
        if k < 11 {
                err = ErrDecryption
@@ -188,7 +186,7 @@ func decryptPKCS1v15(rand io.Reader, priv *PrivateKey, ciphertext []byte) (valid
        } else {
                c := new(big.Int).SetBytes(ciphertext)
                var m *big.Int
-               m, err = decrypt(rand, priv, c)
+               m, err = decrypt(random, priv, c)
                if err != nil {
                        return
                }
@@ -220,15 +218,15 @@ func decryptPKCS1v15(rand io.Reader, priv *PrivateKey, ciphertext []byte) (valid
 }
 
 // nonZeroRandomBytes fills the given slice with non-zero random octets.
-func nonZeroRandomBytes(s []byte, rand io.Reader) (err error) {
-       _, err = io.ReadFull(rand, s)
+func nonZeroRandomBytes(s []byte, random io.Reader) (err error) {
+       _, err = io.ReadFull(random, s)
        if err != nil {
                return
        }
 
        for i := 0; i < len(s); i++ {
                for s[i] == 0 {
-                       _, err = io.ReadFull(rand, s[i:i+1])
+                       _, err = io.ReadFull(random, s[i:i+1])
                        if err != nil {
                                return
                        }
@@ -268,7 +266,7 @@ var hashPrefixes = map[crypto.Hash][]byte{
 // function. If hash is zero, hashed is signed directly. This isn't
 // advisable except for interoperability.
 //
-// If rand is not nil then RSA blinding will be used to avoid timing
+// If random is not nil then RSA blinding will be used to avoid timing
 // side-channel attacks.
 //
 // This function is deterministic. Thus, if the set of possible
index 16ebc0e6a7fb251dd9227b486de9dd206a626cf3..29e79bd34279939e726a478f5231bcb163650820 100644 (file)
@@ -9,14 +9,13 @@ package rsa
 import (
        "bytes"
        "crypto"
+       "crypto/internal/boring"
        "errors"
        "hash"
        "io"
        "math/big"
 )
 
-import "crypto/internal/boring"
-
 // Per RFC 8017, Section 9.1
 //
 //     EM = MGF1 xor DB || H( 8*0x00 || mHash || salt ) || 0xbc
@@ -298,6 +297,7 @@ func SignPSS(rand io.Reader, priv *PrivateKey, hash crypto.Hash, digest []byte,
                }
                return boring.SignRSAPSS(bkey, hash, digest, saltLength)
        }
+       boring.UnreachableExceptTests()
 
        salt := make([]byte, saltLength)
        if _, err := io.ReadFull(rand, salt); err != nil {
index 3004b31698d0d2b4639991b61cf6fd547cc5a543..9a1fa3104b7acc512e9f8df86a2fa2adbb414dc5 100644 (file)
@@ -4,14 +4,13 @@
 
 package tls
 
-import "crypto/internal/boring"
-
 import (
        "crypto"
        "crypto/aes"
        "crypto/cipher"
        "crypto/des"
        "crypto/hmac"
+       "crypto/internal/boring"
        "crypto/rc4"
        "crypto/sha1"
        "crypto/sha256"
@@ -517,12 +516,9 @@ func aeadAESGCM(key, noncePrefix []byte) aead {
        if err != nil {
                panic(err)
        }
-       type gcmtls interface {
-               NewGCMTLS() (cipher.AEAD, error)
-       }
        var aead cipher.AEAD
-       if aesTLS, ok := aes.(gcmtls); ok {
-               aead, err = aesTLS.NewGCMTLS()
+       if boring.Enabled {
+               aead, err = boring.NewGCMTLS(aes)
        } else {
                boring.Unreachable()
                aead, err = cipher.NewGCM(aes)
index 039b422dabb21d00ecd64d39cb62855d602f15f5..bfe3f444ca714bfbd49b946a79ae11ac89124bf8 100644 (file)
@@ -1883,11 +1883,13 @@ func (ctxt *Context) eval(x constraint.Expr, allTags map[string]bool) bool {
 //     cgo (if cgo is enabled)
 //     $GOOS
 //     $GOARCH
-//     boringcrypto
 //     ctxt.Compiler
 //     linux (if GOOS = android)
 //     solaris (if GOOS = illumos)
-//     tag (if tag is listed in ctxt.BuildTags or ctxt.ReleaseTags)
+//     darwin (if GOOS = ios)
+//     unix (if this is a Unix GOOS)
+//     boringcrypto (if GOEXPERIMENT=boringcrypto is enabled)
+//     tag (if tag is listed in ctxt.BuildTags, ctxt.ToolTags, or ctxt.ReleaseTags)
 //
 // It records all consulted tags in allTags.
 func (ctxt *Context) matchTag(name string, allTags map[string]bool) bool {