mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
runtime: faster & safer hash function
Uses AES hardware instructions on 386/amd64 to implement a fast hash function. Incorporates a random key to thwart hash collision DOS attacks. Depends on CL#7548043 for new assembly instructions. Update #3885 Helps some by making hashing faster. Go time drops from 0.65s to 0.51s. R=rsc, r, bradfitz, remyoudompheng, khr, dsymonds, minux.ma, elias.naur CC=golang-dev https://golang.org/cl/7543043
This commit is contained in:
parent
4e032ce301
commit
a5d4024139
23 changed files with 891 additions and 8 deletions
|
|
@ -467,6 +467,41 @@ runtime·algarray[] =
|
|||
|
||||
// Runtime helpers.
|
||||
|
||||
// used in asm_{386,amd64}.s
|
||||
byte runtime·aeskeysched[HashRandomBytes];
|
||||
|
||||
void
|
||||
runtime·hashinit(void)
|
||||
{
|
||||
// Install aes hash algorithm if we have the instructions we need
|
||||
if((runtime·cpuid_ecx & (1 << 25)) != 0 && // aes (aesenc)
|
||||
(runtime·cpuid_ecx & (1 << 9)) != 0 && // sse3 (pshufb)
|
||||
(runtime·cpuid_ecx & (1 << 19)) != 0) { // sse4.1 (pinsr{d,q})
|
||||
byte *rnd;
|
||||
int32 n;
|
||||
runtime·algarray[AMEM].hash = runtime·aeshash;
|
||||
runtime·algarray[AMEM8].hash = runtime·aeshash;
|
||||
runtime·algarray[AMEM16].hash = runtime·aeshash;
|
||||
runtime·algarray[AMEM32].hash = runtime·aeshash32;
|
||||
runtime·algarray[AMEM64].hash = runtime·aeshash64;
|
||||
runtime·algarray[AMEM128].hash = runtime·aeshash;
|
||||
runtime·algarray[ASTRING].hash = runtime·aeshashstr;
|
||||
|
||||
// Initialize with random data so hash collisions will be hard to engineer.
|
||||
runtime·get_random_data(&rnd, &n);
|
||||
if(n > HashRandomBytes)
|
||||
n = HashRandomBytes;
|
||||
runtime·memmove(runtime·aeskeysched, rnd, n);
|
||||
if(n < HashRandomBytes) {
|
||||
// Not very random, but better than nothing.
|
||||
int64 t = runtime·nanotime();
|
||||
while (n < HashRandomBytes) {
|
||||
runtime·aeskeysched[n++] = (int8)(t >> (8 * (n % 8)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// func equal(t *Type, x T, y T) (ret bool)
|
||||
#pragma textflag 7
|
||||
void
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue