diff mbox

libgo patch committed: Fix md5 on big-endian systems

Message ID mcrd2wb951e.fsf@google.com
State New
Headers show

Commit Message

Ian Lance Taylor Feb. 7, 2013, 9:40 p.m. UTC
PR 56173 points out a number of checksum failures that turn out to be
related to a bug in the crypto/md5 package: some optimizations had been
added that make it only work correctly on little-endian systems.  This
patch corrects the problem.  Bootstrapped and ran Go testsuite on
x86_64-unknown-linux-gnu.  Committed to mainline.

Ian
diff mbox

Patch

diff -r 317c1f1eb8c1 libgo/go/crypto/md5/md5block.go
--- a/libgo/go/crypto/md5/md5block.go	Thu Feb 07 10:01:36 2013 -0800
+++ b/libgo/go/crypto/md5/md5block.go	Thu Feb 07 13:32:23 2013 -0800
@@ -5,6 +5,16 @@ 
 	"unsafe"
 )
 
+const x86 = runtime.GOARCH == "amd64" || runtime.GOARCH == "386"
+
+var littleEndian bool
+
+func init() {
+	x := uint32(0x04030201)
+	y := [4]byte{0x1, 0x2, 0x3, 0x4}
+	littleEndian = *(*[4]byte)(unsafe.Pointer(&x)) == y
+}
+
 func block(dig *digest, p []byte) {
 	a := dig.s[0]
 	b := dig.s[1]
@@ -16,13 +26,13 @@ 
 		aa, bb, cc, dd := a, b, c, d
 
 		// This is a constant condition - it is not evaluated on each iteration.
-		if runtime.GOARCH == "amd64" || runtime.GOARCH == "386" {
+		if x86 {
 			// MD5 was designed so that x86 processors can just iterate
 			// over the block data directly as uint32s, and we generate
 			// less code and run 1.3x faster if we take advantage of that.
 			// My apologies.
 			X = (*[16]uint32)(unsafe.Pointer(&p[0]))
-		} else if uintptr(unsafe.Pointer(&p[0]))&(unsafe.Alignof(uint32(0))-1) == 0 {
+		} else if littleEndian && uintptr(unsafe.Pointer(&p[0]))&(unsafe.Alignof(uint32(0))-1) == 0 {
 			X = (*[16]uint32)(unsafe.Pointer(&p[0]))
 		} else {
 			X = &xbuf