From bf5a3141448765cb3cb13673f970c4f489b763a9 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Sat, 7 Mar 2015 01:48:16 -0800 Subject: [PATCH] clang lacks proper gas support, implement stubs for sha256,sha512 for darwin. REF: http://llvm.org/bugs/show_bug.cgi?id=18918 --- ...ha256-avx-asm.S => sha256-avx-asm_linux.S} | 0 ...256-avx2-asm.S => sha256-avx2-asm_linux.S} | 0 ...6-ssse3-asm.S => sha256-ssse3-asm_linux.S} | 0 pkg/utils/crypto/sha256/sha256_darwin.go | 31 +++++++++++++ .../sha256/{sha256.go => sha256_linux.go} | 4 +- .../{sha256_test.go => sha256_linux_test.go} | 0 .../{sha256block.go => sha256block_linux.go} | 0 ...ha512-avx-asm.S => sha512-avx-asm_linux.S} | 0 ...512-avx2-asm.S => sha512-avx2-asm_linux.S} | 0 ...2-ssse3-asm.S => sha512-ssse3-asm_linux.S} | 0 pkg/utils/crypto/sha512/sha512_darwin.go | 46 +++++++++++++++++++ .../sha512/{sha512.go => sha512_linux.go} | 4 +- .../{sha512_test.go => sha512_linux_test.go} | 0 .../{sha512block.go => sha512block_linux.go} | 0 14 files changed, 81 insertions(+), 4 deletions(-) rename pkg/utils/crypto/sha256/{sha256-avx-asm.S => sha256-avx-asm_linux.S} (100%) rename pkg/utils/crypto/sha256/{sha256-avx2-asm.S => sha256-avx2-asm_linux.S} (100%) rename pkg/utils/crypto/sha256/{sha256-ssse3-asm.S => sha256-ssse3-asm_linux.S} (100%) create mode 100644 pkg/utils/crypto/sha256/sha256_darwin.go rename pkg/utils/crypto/sha256/{sha256.go => sha256_linux.go} (98%) rename pkg/utils/crypto/sha256/{sha256_test.go => sha256_linux_test.go} (100%) rename pkg/utils/crypto/sha256/{sha256block.go => sha256block_linux.go} (100%) rename pkg/utils/crypto/sha512/{sha512-avx-asm.S => sha512-avx-asm_linux.S} (100%) rename pkg/utils/crypto/sha512/{sha512-avx2-asm.S => sha512-avx2-asm_linux.S} (100%) rename pkg/utils/crypto/sha512/{sha512-ssse3-asm.S => sha512-ssse3-asm_linux.S} (100%) create mode 100644 pkg/utils/crypto/sha512/sha512_darwin.go rename pkg/utils/crypto/sha512/{sha512.go => sha512_linux.go} (98%) rename pkg/utils/crypto/sha512/{sha512_test.go => sha512_linux_test.go} (100%) rename pkg/utils/crypto/sha512/{sha512block.go => sha512block_linux.go} (100%) diff --git a/pkg/utils/crypto/sha256/sha256-avx-asm.S b/pkg/utils/crypto/sha256/sha256-avx-asm_linux.S similarity index 100% rename from pkg/utils/crypto/sha256/sha256-avx-asm.S rename to pkg/utils/crypto/sha256/sha256-avx-asm_linux.S diff --git a/pkg/utils/crypto/sha256/sha256-avx2-asm.S b/pkg/utils/crypto/sha256/sha256-avx2-asm_linux.S similarity index 100% rename from pkg/utils/crypto/sha256/sha256-avx2-asm.S rename to pkg/utils/crypto/sha256/sha256-avx2-asm_linux.S diff --git a/pkg/utils/crypto/sha256/sha256-ssse3-asm.S b/pkg/utils/crypto/sha256/sha256-ssse3-asm_linux.S similarity index 100% rename from pkg/utils/crypto/sha256/sha256-ssse3-asm.S rename to pkg/utils/crypto/sha256/sha256-ssse3-asm_linux.S diff --git a/pkg/utils/crypto/sha256/sha256_darwin.go b/pkg/utils/crypto/sha256/sha256_darwin.go new file mode 100644 index 000000000..e688332bb --- /dev/null +++ b/pkg/utils/crypto/sha256/sha256_darwin.go @@ -0,0 +1,31 @@ +package sha256 + +import ( + "io" + + "crypto/sha256" +) + +// Sum256 - single caller sha256 helper +func Sum256(data []byte) []byte { + d := sha256.New() + d.Write(data) + return d.Sum(nil) +} + +// Sum - io.Reader based streaming sha256 helper +func Sum(reader io.Reader) ([]byte, error) { + d := sha256.New() + var err error + for err == nil { + length := 0 + byteBuffer := make([]byte, 1024*1024) + length, err = reader.Read(byteBuffer) + byteBuffer = byteBuffer[0:length] + d.Write(byteBuffer) + } + if err != io.EOF { + return nil, err + } + return d.Sum(nil), nil +} diff --git a/pkg/utils/crypto/sha256/sha256.go b/pkg/utils/crypto/sha256/sha256_linux.go similarity index 98% rename from pkg/utils/crypto/sha256/sha256.go rename to pkg/utils/crypto/sha256/sha256_linux.go index 95489ddb6..c5b81b7e1 100644 --- a/pkg/utils/crypto/sha256/sha256.go +++ b/pkg/utils/crypto/sha256/sha256_linux.go @@ -152,11 +152,11 @@ func (d *digest) checkSum() [Size]byte { /// Convenience functions // Sum256 - single caller sha256 helper -func Sum256(data []byte) [Size]byte { +func Sum256(data []byte) []byte { var d digest d.Reset() d.Write(data) - return d.checkSum() + return d.Sum(nil) } // Sum - io.Reader based streaming sha256 helper diff --git a/pkg/utils/crypto/sha256/sha256_test.go b/pkg/utils/crypto/sha256/sha256_linux_test.go similarity index 100% rename from pkg/utils/crypto/sha256/sha256_test.go rename to pkg/utils/crypto/sha256/sha256_linux_test.go diff --git a/pkg/utils/crypto/sha256/sha256block.go b/pkg/utils/crypto/sha256/sha256block_linux.go similarity index 100% rename from pkg/utils/crypto/sha256/sha256block.go rename to pkg/utils/crypto/sha256/sha256block_linux.go diff --git a/pkg/utils/crypto/sha512/sha512-avx-asm.S b/pkg/utils/crypto/sha512/sha512-avx-asm_linux.S similarity index 100% rename from pkg/utils/crypto/sha512/sha512-avx-asm.S rename to pkg/utils/crypto/sha512/sha512-avx-asm_linux.S diff --git a/pkg/utils/crypto/sha512/sha512-avx2-asm.S b/pkg/utils/crypto/sha512/sha512-avx2-asm_linux.S similarity index 100% rename from pkg/utils/crypto/sha512/sha512-avx2-asm.S rename to pkg/utils/crypto/sha512/sha512-avx2-asm_linux.S diff --git a/pkg/utils/crypto/sha512/sha512-ssse3-asm.S b/pkg/utils/crypto/sha512/sha512-ssse3-asm_linux.S similarity index 100% rename from pkg/utils/crypto/sha512/sha512-ssse3-asm.S rename to pkg/utils/crypto/sha512/sha512-ssse3-asm_linux.S diff --git a/pkg/utils/crypto/sha512/sha512_darwin.go b/pkg/utils/crypto/sha512/sha512_darwin.go new file mode 100644 index 000000000..52c66638b --- /dev/null +++ b/pkg/utils/crypto/sha512/sha512_darwin.go @@ -0,0 +1,46 @@ +package sha512 + +import ( + "io" + + "crypto/sha512" +) + +const ( + Size = sha512.Size +) + +// Sum512 - single caller sha512 helper +func Sum512(data []byte) []byte { + d := sha512.New() + d.Write(data) + return d.Sum(nil) +} + +// Sum - io.Reader based streaming sha512 helper +func Sum(reader io.Reader) ([]byte, error) { + d := sha512.New() + var err error + for err == nil { + length := 0 + byteBuffer := make([]byte, 1024*1024) + length, err = reader.Read(byteBuffer) + byteBuffer = byteBuffer[0:length] + d.Write(byteBuffer) + } + if err != io.EOF { + return nil, err + } + return d.Sum(nil), nil +} + +// SumStream - similar to 'Sum()' but returns a [sha512.Size]byte +func SumStream(reader io.Reader) ([sha512.Size]byte, error) { + var returnValue [sha512.Size]byte + sumSlice, err := Sum(reader) + if err != nil { + return returnValue, err + } + copy(returnValue[:], sumSlice) + return returnValue, err +} diff --git a/pkg/utils/crypto/sha512/sha512.go b/pkg/utils/crypto/sha512/sha512_linux.go similarity index 98% rename from pkg/utils/crypto/sha512/sha512.go rename to pkg/utils/crypto/sha512/sha512_linux.go index b2fd58bad..8fd2667b7 100644 --- a/pkg/utils/crypto/sha512/sha512.go +++ b/pkg/utils/crypto/sha512/sha512_linux.go @@ -158,11 +158,11 @@ func (d *digest) checkSum() [Size]byte { /// Convenience functions // Sum512 - single caller sha512 helper -func Sum512(data []byte) [Size]byte { +func Sum512(data []byte) []byte { var d digest d.Reset() d.Write(data) - return d.checkSum() + return d.Sum(nil) } // Sum - io.Reader based streaming sha512 helper diff --git a/pkg/utils/crypto/sha512/sha512_test.go b/pkg/utils/crypto/sha512/sha512_linux_test.go similarity index 100% rename from pkg/utils/crypto/sha512/sha512_test.go rename to pkg/utils/crypto/sha512/sha512_linux_test.go diff --git a/pkg/utils/crypto/sha512/sha512block.go b/pkg/utils/crypto/sha512/sha512block_linux.go similarity index 100% rename from pkg/utils/crypto/sha512/sha512block.go rename to pkg/utils/crypto/sha512/sha512block_linux.go