diff options
Diffstat (limited to 'src/crypto/sha256')
-rw-r--r-- | src/crypto/sha256/sha256.go | 24 | ||||
-rw-r--r-- | src/crypto/sha256/sha256_test.go | 5 |
2 files changed, 29 insertions, 0 deletions
diff --git a/src/crypto/sha256/sha256.go b/src/crypto/sha256/sha256.go index e1cccf65a6..8b54a427d7 100644 --- a/src/crypto/sha256/sha256.go +++ b/src/crypto/sha256/sha256.go @@ -13,6 +13,8 @@ import ( "hash" ) +import "crypto/internal/boring" + func init() { crypto.RegisterHash(crypto.SHA224, New224) crypto.RegisterHash(crypto.SHA256, New) @@ -159,6 +161,9 @@ func (d *digest) Reset() { // encoding.BinaryUnmarshaler to marshal and unmarshal the internal // state of the hash. func New() hash.Hash { + if boring.Enabled { + return boring.NewSHA256() + } d := new(digest) d.Reset() return d @@ -166,6 +171,9 @@ func New() hash.Hash { // New224 returns a new hash.Hash computing the SHA224 checksum. func New224() hash.Hash { + if boring.Enabled { + return boring.NewSHA224() + } d := new(digest) d.is224 = true d.Reset() @@ -182,6 +190,7 @@ func (d *digest) Size() int { func (d *digest) BlockSize() int { return BlockSize } func (d *digest) Write(p []byte) (nn int, err error) { + boring.Unreachable() nn = len(p) d.len += uint64(nn) if d.nx > 0 { @@ -205,6 +214,7 @@ func (d *digest) Write(p []byte) (nn int, err error) { } func (d *digest) Sum(in []byte) []byte { + boring.Unreachable() // Make a copy of d so that caller can keep writing and summing. d0 := *d hash := d0.checkSum() @@ -252,6 +262,13 @@ func (d *digest) checkSum() [Size]byte { // Sum256 returns the SHA256 checksum of the data. func Sum256(data []byte) [Size]byte { + if boring.Enabled { + h := New() + h.Write(data) + var ret [Size]byte + h.Sum(ret[:0]) + return ret + } var d digest d.Reset() d.Write(data) @@ -260,6 +277,13 @@ func Sum256(data []byte) [Size]byte { // Sum224 returns the SHA224 checksum of the data. func Sum224(data []byte) (sum224 [Size224]byte) { + if boring.Enabled { + h := New224() + h.Write(data) + var ret [Size224]byte + h.Sum(ret[:0]) + return ret + } var d digest d.is224 = true d.Reset() diff --git a/src/crypto/sha256/sha256_test.go b/src/crypto/sha256/sha256_test.go index 433c5a4c5e..13178fb21f 100644 --- a/src/crypto/sha256/sha256_test.go +++ b/src/crypto/sha256/sha256_test.go @@ -16,6 +16,8 @@ import ( "testing" ) +import "crypto/internal/boring" + type sha256Test struct { out string in string @@ -216,6 +218,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") + } gen, asm := New().(*digest), New().(*digest) buf := make([]byte, BlockSize*20) // arbitrary factor rand.Read(buf) |