]> Cypherpunks repositories - gostls13.git/commitdiff
crypto: use bytes.Clone
authorcuiweixie <cuiweixie@gmail.com>
Tue, 27 Sep 2022 16:28:49 +0000 (00:28 +0800)
committerGopher Robot <gobot@golang.org>
Wed, 28 Sep 2022 03:55:33 +0000 (03:55 +0000)
Change-Id: I92e110023739c6f8f7815c7e47ad7639c4e8812d
Reviewed-on: https://go-review.googlesource.com/c/go/+/435279
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Auto-Submit: Dmitri Shuralyov <dmitshur@golang.org>
Reviewed-by: Filippo Valsorda <filippo@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: xie cui <523516579@qq.com>

src/crypto/cipher/cbc.go
src/crypto/cipher/cipher.go
src/crypto/cipher/ctr.go
src/crypto/ed25519/ed25519.go
src/crypto/internal/boring/aes.go
src/crypto/internal/boring/hmac.go
src/crypto/x509/internal/macos/corefoundation.go
src/crypto/x509/root_windows.go

index fe774c116e1f97e64c94878e8dff631b6091b211..51a142071f8c54b64048be9607117890fe00eafc 100644 (file)
@@ -12,6 +12,7 @@
 package cipher
 
 import (
+       "bytes"
        "crypto/internal/alias"
        "crypto/subtle"
 )
@@ -27,7 +28,7 @@ func newCBC(b Block, iv []byte) *cbc {
        return &cbc{
                b:         b,
                blockSize: b.BlockSize(),
-               iv:        dup(iv),
+               iv:        bytes.Clone(iv),
                tmp:       make([]byte, b.BlockSize()),
        }
 }
index 7e1a4de9a3778f201b08caea72424d8a64f13d51..df6f596b4d033024d96c971c14268568f606e504 100644 (file)
@@ -59,11 +59,3 @@ type BlockMode interface {
        // maintains state and does not reset at each CryptBlocks call.
        CryptBlocks(dst, src []byte)
 }
-
-// Utility routines
-
-func dup(p []byte) []byte {
-       q := make([]byte, len(p))
-       copy(q, p)
-       return q
-}
index 2b434ef83230c22748d26bd419e903357907fbea..3ac0ff74d069c14b7e397a7f7dfbcc589559e47d 100644 (file)
@@ -13,6 +13,7 @@
 package cipher
 
 import (
+       "bytes"
        "crypto/internal/alias"
        "crypto/subtle"
 )
@@ -48,7 +49,7 @@ func NewCTR(block Block, iv []byte) Stream {
        }
        return &ctr{
                b:       block,
-               ctr:     dup(iv),
+               ctr:     bytes.Clone(iv),
                out:     make([]byte, 0, bufSize),
                outUsed: 0,
        }
index d43dd12d083dfba6869840085d6641de0faa17ae..601da50a1afc03165cff3e7c5eeed6c3cf0b1e75 100644 (file)
@@ -72,9 +72,7 @@ func (priv PrivateKey) Equal(x crypto.PrivateKey) bool {
 // interoperability with RFC 8032. RFC 8032's private keys correspond to seeds
 // in this package.
 func (priv PrivateKey) Seed() []byte {
-       seed := make([]byte, SeedSize)
-       copy(seed, priv[:32])
-       return seed
+       return bytes.Clone(priv[:SeedSize])
 }
 
 // Sign signs the given message with priv.
index f52dc68b465b23add96cb9d80436f1209cbb95d1..6fae1d54f816afb0b48645fd5f595bc3e1de27c6 100644 (file)
@@ -44,6 +44,7 @@ int EVP_AEAD_CTX_open_wrapper(const GO_EVP_AEAD_CTX *ctx, uint8_t *out,
 */
 import "C"
 import (
+       "bytes"
        "crypto/cipher"
        "errors"
        "runtime"
@@ -76,8 +77,7 @@ type extraModes interface {
 var _ extraModes = (*aesCipher)(nil)
 
 func NewAESCipher(key []byte) (cipher.Block, error) {
-       c := &aesCipher{key: make([]byte, len(key))}
-       copy(c.key, key)
+       c := &aesCipher{key: bytes.Clone(key)}
        // Note: 0 is success, contradicting the usual BoringCrypto convention.
        if C._goboringcrypto_AES_set_decrypt_key((*C.uint8_t)(unsafe.Pointer(&c.key[0])), C.uint(8*len(c.key)), &c.dec) != 0 ||
                C._goboringcrypto_AES_set_encrypt_key((*C.uint8_t)(unsafe.Pointer(&c.key[0])), C.uint(8*len(c.key)), &c.enc) != 0 {
index 7833bc1938df69abdcacb3e075023f732678f4e5..6241a65f5fafdaa6f1a9389d777f3387c342bbff 100644 (file)
@@ -9,6 +9,7 @@ package boring
 // #include "goboringcrypto.h"
 import "C"
 import (
+       "bytes"
        "crypto"
        "hash"
        "runtime"
@@ -67,8 +68,7 @@ func NewHMAC(h func() hash.Hash, key []byte) hash.Hash {
        }
 
        // Note: Could hash down long keys here using EVP_Digest.
-       hkey := make([]byte, len(key))
-       copy(hkey, key)
+       hkey := bytes.Clone(key)
        hmac := &boringHMAC{
                md:        md,
                size:      ch.Size(),
index b27a9172e1a91516212a05ee232133d84a528145..352eb8eecc9161f891e03d2eb61994112ed4e17d 100644 (file)
@@ -10,6 +10,7 @@
 package macOS
 
 import (
+       "bytes"
        "errors"
        "internal/abi"
        "runtime"
@@ -31,9 +32,7 @@ func CFDataToSlice(data CFRef) []byte {
        length := CFDataGetLength(data)
        ptr := CFDataGetBytePtr(data)
        src := unsafe.Slice((*byte)(unsafe.Pointer(ptr)), length)
-       out := make([]byte, length)
-       copy(out, src)
-       return out
+       return bytes.Clone(src)
 }
 
 // CFStringToString returns a Go string representation of the passed
index 5515c439c78deb02679a122966a6ee88d4d71fef..76d6e6ac7094dec21f883a114c501c48337a4978 100644 (file)
@@ -5,6 +5,7 @@
 package x509
 
 import (
+       "bytes"
        "errors"
        "syscall"
        "unsafe"
@@ -76,8 +77,7 @@ func extractSimpleChain(simpleChain **syscall.CertSimpleChain, count int) (chain
                // Copy the buf, since ParseCertificate does not create its own copy.
                cert := elements[i].CertContext
                encodedCert := unsafe.Slice(cert.EncodedCert, cert.Length)
-               buf := make([]byte, cert.Length)
-               copy(buf, encodedCert)
+               buf := bytes.Clone(encodedCert)
                parsedCert, err := ParseCertificate(buf)
                if err != nil {
                        return nil, err