diff options
Diffstat (limited to 'src/crypto/sha1')
-rw-r--r-- | src/crypto/sha1/boring.go | 22 | ||||
-rw-r--r-- | src/crypto/sha1/notboring.go | 17 | ||||
-rw-r--r-- | src/crypto/sha1/sha1.go | 12 | ||||
-rw-r--r-- | src/crypto/sha1/sha1_test.go | 8 |
4 files changed, 59 insertions, 0 deletions
diff --git a/src/crypto/sha1/boring.go b/src/crypto/sha1/boring.go new file mode 100644 index 00000000000..44c26092ee0 --- /dev/null +++ b/src/crypto/sha1/boring.go @@ -0,0 +1,22 @@ +// Copyright 2009 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. + +// Extra indirection here so that when building go_bootstrap +// cmd/internal/boring is not even imported, so that we don't +// have to maintain changes to cmd/dist's deps graph. + +// +build !cmd_go_bootstrap + +package sha1 + +import ( + "crypto/internal/boring" + "hash" +) + +const boringEnabled = boring.Enabled + +func boringNewSHA1() hash.Hash { return boring.NewSHA1() } + +func boringUnreachable() { boring.Unreachable() } diff --git a/src/crypto/sha1/notboring.go b/src/crypto/sha1/notboring.go new file mode 100644 index 00000000000..9726fcd268e --- /dev/null +++ b/src/crypto/sha1/notboring.go @@ -0,0 +1,17 @@ +// Copyright 2009 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. + +// +build cmd_go_bootstrap + +package sha1 + +import ( + "hash" +) + +const boringEnabled = false + +func boringNewSHA1() hash.Hash { panic("boringcrypto: not available") } + +func boringUnreachable() {} diff --git a/src/crypto/sha1/sha1.go b/src/crypto/sha1/sha1.go index 286a59d33d6..329435f282f 100644 --- a/src/crypto/sha1/sha1.go +++ b/src/crypto/sha1/sha1.go @@ -119,6 +119,9 @@ func (d *digest) Reset() { // implements encoding.BinaryMarshaler and encoding.BinaryUnmarshaler to // marshal and unmarshal the internal state of the hash. func New() hash.Hash { + if boringEnabled { + return boringNewSHA1() + } d := new(digest) d.Reset() return d @@ -129,6 +132,7 @@ func (d *digest) Size() int { return Size } func (d *digest) BlockSize() int { return BlockSize } func (d *digest) Write(p []byte) (nn int, err error) { + boringUnreachable() nn = len(p) d.len += uint64(nn) if d.nx > 0 { @@ -152,6 +156,7 @@ func (d *digest) Write(p []byte) (nn int, err error) { } func (d *digest) Sum(in []byte) []byte { + boringUnreachable() // Make a copy of d so that caller can keep writing and summing. d0 := *d hash := d0.checkSum() @@ -259,6 +264,13 @@ func (d *digest) constSum() [Size]byte { // Sum returns the SHA-1 checksum of the data. func Sum(data []byte) [Size]byte { + if boringEnabled { + h := New() + h.Write(data) + var ret [Size]byte + h.Sum(ret[:0]) + return ret + } var d digest d.Reset() d.Write(data) diff --git a/src/crypto/sha1/sha1_test.go b/src/crypto/sha1/sha1_test.go index c3e6010af12..e369c3b7f49 100644 --- a/src/crypto/sha1/sha1_test.go +++ b/src/crypto/sha1/sha1_test.go @@ -16,6 +16,8 @@ import ( "testing" ) +import "crypto/internal/boring" + type sha1Test struct { out string in string @@ -77,6 +79,9 @@ func TestGolden(t *testing.T) { io.WriteString(c, g.in[len(g.in)/2:]) sum = c.Sum(nil) case 3: + if boring.Enabled { + continue + } io.WriteString(c, g.in[0:len(g.in)/2]) c.(*digest).ConstantTimeSum(nil) io.WriteString(c, g.in[len(g.in)/2:]) @@ -141,6 +146,9 @@ func TestBlockSize(t *testing.T) { // Tests that blockGeneric (pure Go) and block (in assembly for some architectures) match. func TestBlockGeneric(t *testing.T) { + if boring.Enabled { + t.Skip("BoringCrypto doesn't expose digest") + } for i := 1; i < 30; i++ { // arbitrary factor gen, asm := New().(*digest), New().(*digest) buf := make([]byte, BlockSize*i) |