]> Cypherpunks.ru repositories - gostls13.git/blobdiff - src/crypto/sha512/sha512.go
[dev.boringcrypto] all: merge master into dev.boringcrypto
[gostls13.git] / src / crypto / sha512 / sha512.go
index 24fde7dce708b85094df8b5869bc9a8808109919..355d7bbe0731ff5f1a088a01a511652dec4e8876 100644 (file)
@@ -12,6 +12,7 @@ package sha512
 
 import (
        "crypto"
+       "crypto/internal/boring"
        "errors"
        "hash"
 )
@@ -222,6 +223,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
@@ -243,6 +247,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
@@ -264,6 +271,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 {
@@ -287,6 +297,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
@@ -341,6 +354,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)
@@ -349,6 +369,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)