diff options
Diffstat (limited to 'src/crypto/sha512')
-rw-r--r-- | src/crypto/sha512/sha512.go | 28 | ||||
-rw-r--r-- | src/crypto/sha512/sha512_test.go | 5 |
2 files changed, 33 insertions, 0 deletions
diff --git a/src/crypto/sha512/sha512.go b/src/crypto/sha512/sha512.go index 9c143a2a28..1a2cef317c 100644 --- a/src/crypto/sha512/sha512.go +++ b/src/crypto/sha512/sha512.go @@ -17,6 +17,8 @@ import ( "hash" ) +import "crypto/internal/boring" + func init() { crypto.RegisterHash(crypto.SHA384, New384) crypto.RegisterHash(crypto.SHA512, New) @@ -211,6 +213,9 @@ func consumeUint64(b []byte) ([]byte, uint64) { // New returns a new hash.Hash computing the SHA-512 checksum. func New() hash.Hash { + if boring.Enabled { + return boring.NewSHA512() + } d := &digest{function: crypto.SHA512} d.Reset() return d @@ -232,6 +237,9 @@ func New512_256() hash.Hash { // New384 returns a new hash.Hash computing the SHA-384 checksum. func New384() hash.Hash { + if boring.Enabled { + return boring.NewSHA384() + } d := &digest{function: crypto.SHA384} d.Reset() return d @@ -253,6 +261,9 @@ func (d *digest) Size() int { func (d *digest) BlockSize() int { return BlockSize } func (d *digest) Write(p []byte) (nn int, err error) { + if d.function != crypto.SHA512_224 && d.function != crypto.SHA512_256 { + boring.Unreachable() + } nn = len(p) d.len += uint64(nn) if d.nx > 0 { @@ -276,6 +287,9 @@ func (d *digest) Write(p []byte) (nn int, err error) { } func (d *digest) Sum(in []byte) []byte { + if d.function != crypto.SHA512_224 && d.function != crypto.SHA512_256 { + boring.Unreachable() + } // Make a copy of d so that caller can keep writing and summing. d0 := new(digest) *d0 = *d @@ -330,6 +344,13 @@ func (d *digest) checkSum() [Size]byte { // Sum512 returns the SHA512 checksum of the data. func Sum512(data []byte) [Size]byte { + if boring.Enabled { + h := New() + h.Write(data) + var ret [Size]byte + h.Sum(ret[:0]) + return ret + } d := digest{function: crypto.SHA512} d.Reset() d.Write(data) @@ -338,6 +359,13 @@ func Sum512(data []byte) [Size]byte { // Sum384 returns the SHA384 checksum of the data. func Sum384(data []byte) (sum384 [Size384]byte) { + if boring.Enabled { + h := New384() + h.Write(data) + var ret [Size384]byte + h.Sum(ret[:0]) + return ret + } d := digest{function: crypto.SHA384} d.Reset() d.Write(data) diff --git a/src/crypto/sha512/sha512_test.go b/src/crypto/sha512/sha512_test.go index 59998b1d38..6964bef8f7 100644 --- a/src/crypto/sha512/sha512_test.go +++ b/src/crypto/sha512/sha512_test.go @@ -17,6 +17,8 @@ import ( "testing" ) +import "crypto/internal/boring" + type sha512Test struct { out string in string @@ -822,6 +824,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) |