Skip to content

Instantly share code, notes, and snippets.

@majek

majek/aeshash.c Secret

Last active March 3, 2020 12:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save majek/96dd615ed6c8aa64f60aac14e3f6ab5a to your computer and use it in GitHub Desktop.
Save majek/96dd615ed6c8aa64f60aac14e3f6ab5a to your computer and use it in GitHub Desktop.
#include <x86intrin.h>
static uint64_t aesnihash(uint8_t *in, unsigned long src_sz)
{
uint8_t tmp_buf[16] = {0};
__m128i rk0 = {0x736f6d6570736575ULL, 0x646f72616e646f6dULL};
__m128i rk1 = {0x1231236570743245ULL, 0x126f12321321456dULL};
__m128i hash = rk0;
while (src_sz >= 16) {
onemoretry:;
__m128i piece = _mm_loadu_si128((__m128i *)in);
in += 16;
src_sz -= 16;
hash = _mm_aesenc_si128(_mm_xor_si128(hash, piece), rk0);
hash = _mm_aesenc_si128(hash, rk1);
}
if (src_sz > 0) {
unsigned long i;
for (i = 0; i < src_sz && i < 16; i++) {
tmp_buf[i] = in[i];
}
src_sz = 16;
in = &tmp_buf[0];
goto onemoretry;
}
hash = _mm_aesenc_si128(hash, _mm_set_epi64x(src_sz, src_sz));
return hash[0] ^ hash[1];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment