diff options
author | Keith Randall <khr@golang.org> | 2015-01-06 09:06:44 -0800 |
---|---|---|
committer | Keith Randall <khr@golang.org> | 2015-01-07 16:02:05 +0000 |
commit | ce5cb037d171273f1a5294723234be5495c9d336 (patch) | |
tree | 76005ac2875ec6f39ed9aad62ab069162eb65853 /src/runtime/alg.go | |
parent | 31775c5a958e00411954724408d1a069df4b9061 (diff) | |
download | go-ce5cb037d171273f1a5294723234be5495c9d336.tar.gz go-ce5cb037d171273f1a5294723234be5495c9d336.zip |
runtime: use some startup randomness in the fallback hashes
Fold in some startup randomness to make the hash vary across
different runs. This helps prevent attackers from choosing
keys that all map to the same bucket.
Also, reorganize the hash a bit. Move the *m1 multiply to after
the xor of the current hash and the message. For hash quality
it doesn't really matter, but for DDOS resistance it helps a lot
(any processing done to the message before it is merged with the
random seed is useless, as it is easily inverted by an attacker).
Update #9365
Change-Id: Ib19968168e1bbc541d1d28be2701bb83e53f1e24
Reviewed-on: https://go-review.googlesource.com/2344
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Diffstat (limited to 'src/runtime/alg.go')
-rw-r--r-- | src/runtime/alg.go | 20 |
1 files changed, 12 insertions, 8 deletions
diff --git a/src/runtime/alg.go b/src/runtime/alg.go index 15e3abe368..6713d298da 100644 --- a/src/runtime/alg.go +++ b/src/runtime/alg.go @@ -285,20 +285,21 @@ func memclrBytes(b []byte) { memclr(s.array, uintptr(s.len)) } -// used in asm_{386,amd64}.s const hashRandomBytes = ptrSize / 4 * 64 +// used in asm_{386,amd64}.s to seed the hash function var aeskeysched [hashRandomBytes]byte -func init() { - if GOOS == "nacl" { - return - } +// used in hash{32,64}.go to seed the hash function +var hashkey [4]uintptr +func init() { // Install aes hash algorithm if we have the instructions we need - if (cpuid_ecx&(1<<25)) != 0 && // aes (aesenc) - (cpuid_ecx&(1<<9)) != 0 && // sse3 (pshufb) - (cpuid_ecx&(1<<19)) != 0 { // sse4.1 (pinsr{d,q}) + if (GOARCH == "386" || GOARCH == "amd64") && + GOOS != "nacl" && + cpuid_ecx&(1<<25) != 0 && // aes (aesenc) + cpuid_ecx&(1<<9) != 0 && // sse3 (pshufb) + cpuid_ecx&(1<<19) != 0 { // sse4.1 (pinsr{d,q}) useAeshash = true algarray[alg_MEM].hash = aeshash algarray[alg_MEM8].hash = aeshash @@ -309,5 +310,8 @@ func init() { algarray[alg_STRING].hash = aeshashstr // Initialize with random data so hash collisions will be hard to engineer. getRandomData(aeskeysched[:]) + return } + getRandomData((*[len(hashkey) * ptrSize]byte)(unsafe.Pointer(&hashkey))[:]) + hashkey[0] |= 1 // make sure this number is odd } |