Path: blob/linux/scryptjane/scrypt-jane-hash_keccak.h
1201 views
#if defined(SCRYPT_KECCAK256)1#define SCRYPT_HASH "Keccak-256"2#define SCRYPT_HASH_DIGEST_SIZE 323#else4#define SCRYPT_HASH "Keccak-512"5#define SCRYPT_HASH_DIGEST_SIZE 646#endif7#define SCRYPT_KECCAK_F 16008#define SCRYPT_KECCAK_C (SCRYPT_HASH_DIGEST_SIZE * 8 * 2) /* 256=512, 512=1024 */9#define SCRYPT_KECCAK_R (SCRYPT_KECCAK_F - SCRYPT_KECCAK_C) /* 256=1088, 512=576 */10#define SCRYPT_HASH_BLOCK_SIZE (SCRYPT_KECCAK_R / 8)1112typedef uint8_t scrypt_hash_digest[SCRYPT_HASH_DIGEST_SIZE];1314typedef struct scrypt_hash_state_t {15uint64_t state[SCRYPT_KECCAK_F / 64];16uint32_t leftover;17uint8_t buffer[SCRYPT_HASH_BLOCK_SIZE];18} scrypt_hash_state;1920static const uint64_t keccak_round_constants[24] = {210x0000000000000001ull, 0x0000000000008082ull,220x800000000000808aull, 0x8000000080008000ull,230x000000000000808bull, 0x0000000080000001ull,240x8000000080008081ull, 0x8000000000008009ull,250x000000000000008aull, 0x0000000000000088ull,260x0000000080008009ull, 0x000000008000000aull,270x000000008000808bull, 0x800000000000008bull,280x8000000000008089ull, 0x8000000000008003ull,290x8000000000008002ull, 0x8000000000000080ull,300x000000000000800aull, 0x800000008000000aull,310x8000000080008081ull, 0x8000000000008080ull,320x0000000080000001ull, 0x8000000080008008ull33};3435static void36keccak_block(scrypt_hash_state *S, const uint8_t *in) {37size_t i;38uint64_t *s = S->state, t[5], u[5], v, w;3940/* absorb input */41for (i = 0; i < SCRYPT_HASH_BLOCK_SIZE / 8; i++, in += 8)42s[i] ^= U8TO64_LE(in);4344for (i = 0; i < 24; i++) {45/* theta: c = a[0,i] ^ a[1,i] ^ .. a[4,i] */46t[0] = s[0] ^ s[5] ^ s[10] ^ s[15] ^ s[20];47t[1] = s[1] ^ s[6] ^ s[11] ^ s[16] ^ s[21];48t[2] = s[2] ^ s[7] ^ s[12] ^ s[17] ^ s[22];49t[3] = s[3] ^ s[8] ^ s[13] ^ s[18] ^ s[23];50t[4] = s[4] ^ s[9] ^ s[14] ^ s[19] ^ s[24];5152/* theta: d[i] = c[i+4] ^ rotl(c[i+1],1) */53u[0] = t[4] ^ ROTL64(t[1], 1);54u[1] = t[0] ^ ROTL64(t[2], 1);55u[2] = t[1] ^ ROTL64(t[3], 1);56u[3] = t[2] ^ ROTL64(t[4], 1);57u[4] = t[3] ^ ROTL64(t[0], 1);5859/* theta: a[0,i], a[1,i], .. a[4,i] ^= d[i] */60s[0] ^= u[0]; s[5] ^= u[0]; s[10] ^= u[0]; s[15] ^= u[0]; s[20] ^= u[0];61s[1] ^= u[1]; s[6] ^= u[1]; s[11] ^= u[1]; s[16] ^= u[1]; s[21] ^= u[1];62s[2] ^= u[2]; s[7] ^= u[2]; s[12] ^= u[2]; s[17] ^= u[2]; s[22] ^= u[2];63s[3] ^= u[3]; s[8] ^= u[3]; s[13] ^= u[3]; s[18] ^= u[3]; s[23] ^= u[3];64s[4] ^= u[4]; s[9] ^= u[4]; s[14] ^= u[4]; s[19] ^= u[4]; s[24] ^= u[4];6566/* rho pi: b[..] = rotl(a[..], ..) */67v = s[ 1];68s[ 1] = ROTL64(s[ 6], 44);69s[ 6] = ROTL64(s[ 9], 20);70s[ 9] = ROTL64(s[22], 61);71s[22] = ROTL64(s[14], 39);72s[14] = ROTL64(s[20], 18);73s[20] = ROTL64(s[ 2], 62);74s[ 2] = ROTL64(s[12], 43);75s[12] = ROTL64(s[13], 25);76s[13] = ROTL64(s[19], 8);77s[19] = ROTL64(s[23], 56);78s[23] = ROTL64(s[15], 41);79s[15] = ROTL64(s[ 4], 27);80s[ 4] = ROTL64(s[24], 14);81s[24] = ROTL64(s[21], 2);82s[21] = ROTL64(s[ 8], 55);83s[ 8] = ROTL64(s[16], 45);84s[16] = ROTL64(s[ 5], 36);85s[ 5] = ROTL64(s[ 3], 28);86s[ 3] = ROTL64(s[18], 21);87s[18] = ROTL64(s[17], 15);88s[17] = ROTL64(s[11], 10);89s[11] = ROTL64(s[ 7], 6);90s[ 7] = ROTL64(s[10], 3);91s[10] = ROTL64( v, 1);9293/* chi: a[i,j] ^= ~b[i,j+1] & b[i,j+2] */94v = s[ 0]; w = s[ 1]; s[ 0] ^= (~w) & s[ 2]; s[ 1] ^= (~s[ 2]) & s[ 3]; s[ 2] ^= (~s[ 3]) & s[ 4]; s[ 3] ^= (~s[ 4]) & v; s[ 4] ^= (~v) & w;95v = s[ 5]; w = s[ 6]; s[ 5] ^= (~w) & s[ 7]; s[ 6] ^= (~s[ 7]) & s[ 8]; s[ 7] ^= (~s[ 8]) & s[ 9]; s[ 8] ^= (~s[ 9]) & v; s[ 9] ^= (~v) & w;96v = s[10]; w = s[11]; s[10] ^= (~w) & s[12]; s[11] ^= (~s[12]) & s[13]; s[12] ^= (~s[13]) & s[14]; s[13] ^= (~s[14]) & v; s[14] ^= (~v) & w;97v = s[15]; w = s[16]; s[15] ^= (~w) & s[17]; s[16] ^= (~s[17]) & s[18]; s[17] ^= (~s[18]) & s[19]; s[18] ^= (~s[19]) & v; s[19] ^= (~v) & w;98v = s[20]; w = s[21]; s[20] ^= (~w) & s[22]; s[21] ^= (~s[22]) & s[23]; s[22] ^= (~s[23]) & s[24]; s[23] ^= (~s[24]) & v; s[24] ^= (~v) & w;99100/* iota: a[0,0] ^= round constant */101s[0] ^= keccak_round_constants[i];102}103}104105static void106scrypt_hash_init(scrypt_hash_state *S) {107memset(S, 0, sizeof(*S));108}109110static void111scrypt_hash_update(scrypt_hash_state *S, const uint8_t *in, size_t inlen) {112size_t want;113114/* handle the previous data */115if (S->leftover) {116want = (SCRYPT_HASH_BLOCK_SIZE - S->leftover);117want = (want < inlen) ? want : inlen;118memcpy(S->buffer + S->leftover, in, want);119S->leftover += (uint32_t)want;120if (S->leftover < SCRYPT_HASH_BLOCK_SIZE)121return;122in += want;123inlen -= want;124keccak_block(S, S->buffer);125}126127/* handle the current data */128while (inlen >= SCRYPT_HASH_BLOCK_SIZE) {129keccak_block(S, in);130in += SCRYPT_HASH_BLOCK_SIZE;131inlen -= SCRYPT_HASH_BLOCK_SIZE;132}133134/* handle leftover data */135S->leftover = (uint32_t)inlen;136if (S->leftover)137memcpy(S->buffer, in, S->leftover);138}139140static void141scrypt_hash_finish(scrypt_hash_state *S, uint8_t *hash) {142size_t i;143144S->buffer[S->leftover] = 0x01;145memset(S->buffer + (S->leftover + 1), 0, SCRYPT_HASH_BLOCK_SIZE - (S->leftover + 1));146S->buffer[SCRYPT_HASH_BLOCK_SIZE - 1] |= 0x80;147keccak_block(S, S->buffer);148149for (i = 0; i < SCRYPT_HASH_DIGEST_SIZE; i += 8) {150U64TO8_LE(&hash[i], S->state[i / 8]);151}152}153154#if defined(SCRYPT_KECCAK256)155static const uint8_t scrypt_test_hash_expected[SCRYPT_HASH_DIGEST_SIZE] = {1560x26,0xb7,0x10,0xb3,0x66,0xb1,0xd1,0xb1,0x25,0xfc,0x3e,0xe3,0x1e,0x33,0x1d,0x19,1570x94,0xaa,0x63,0x7a,0xd5,0x77,0x29,0xb4,0x27,0xe9,0xe0,0xf4,0x19,0xba,0x68,0xea,158};159#else160static const uint8_t scrypt_test_hash_expected[SCRYPT_HASH_DIGEST_SIZE] = {1610x17,0xc7,0x8c,0xa0,0xd9,0x08,0x1d,0xba,0x8a,0xc8,0x3e,0x07,0x90,0xda,0x91,0x88,1620x25,0xbd,0xd3,0xf8,0x78,0x4a,0x8d,0x5e,0xe4,0x96,0x9c,0x01,0xf3,0xeb,0xdc,0x12,1630xea,0x35,0x57,0xba,0x94,0xb8,0xe9,0xb9,0x27,0x45,0x0a,0x48,0x5c,0x3d,0x69,0xf0,1640xdb,0x22,0x38,0xb5,0x52,0x22,0x29,0xea,0x7a,0xb2,0xe6,0x07,0xaa,0x37,0x4d,0xe6,165};166#endif167168169170