Path: blob/linux/scryptjane/scrypt-jane-hash_blake256.h
1201 views
#define SCRYPT_HASH "BLAKE-256"1#define SCRYPT_HASH_BLOCK_SIZE 642#define SCRYPT_HASH_DIGEST_SIZE 3234typedef uint8_t scrypt_hash_digest[SCRYPT_HASH_DIGEST_SIZE];56const uint8_t blake256_sigma[] = {70, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,814,10, 4, 8, 9,15,13, 6, 1,12, 0, 2,11, 7, 5, 3,911, 8,12, 0, 5, 2,15,13,10,14, 3, 6, 7, 1, 9, 4,107, 9, 3, 1,13,12,11,14, 2, 6, 5,10, 4, 0,15, 8,119, 0, 5, 7, 2, 4,10,15,14, 1,11,12, 6, 8, 3,13,122,12, 6,10, 0,11, 8, 3, 4,13, 7, 5,15,14, 1, 9,1312, 5, 1,15,14,13, 4,10, 0, 7, 6, 3, 9, 2, 8,11,1413,11, 7,14,12, 1, 3, 9, 5, 0,15, 4, 8, 6, 2,10,156,15,14, 9,11, 3, 0, 8,12, 2,13, 7, 1, 4,10, 5,1610, 2, 8, 4, 7, 6, 1, 5,15,11, 9,14, 3,12,13 ,0,17};1819const uint32_t blake256_constants[16] = {200x243f6a88, 0x85a308d3, 0x13198a2e, 0x03707344,0xa4093822, 0x299f31d0, 0x082efa98, 0xec4e6c89,210x452821e6, 0x38d01377, 0xbe5466cf, 0x34e90c6c,0xc0ac29b7, 0xc97c50dd, 0x3f84d5b5, 0xb547091722};2324typedef struct scrypt_hash_state_t {25uint32_t H[8], T[2];26uint32_t leftover;27uint8_t buffer[SCRYPT_HASH_BLOCK_SIZE];28} scrypt_hash_state;2930static void31blake256_blocks(scrypt_hash_state *S, const uint8_t *in, size_t blocks) {32const uint8_t *sigma, *sigma_end = blake256_sigma + (10 * 16);33uint32_t m[16], v[16], h[8], t[2];34uint32_t i;3536for (i = 0; i < 8; i++) h[i] = S->H[i];37for (i = 0; i < 2; i++) t[i] = S->T[i];3839while (blocks--) {40t[0] += 512;41t[1] += (t[0] < 512) ? 1 : 0;4243for (i = 0; i < 8; i++) v[i ] = h[i];44for (i = 0; i < 4; i++) v[i + 8] = blake256_constants[i];45for (i = 0; i < 2; i++) v[i + 12] = blake256_constants[i+4] ^ t[0];46for (i = 0; i < 2; i++) v[i + 14] = blake256_constants[i+6] ^ t[1];4748for (i = 0; i < 16; i++) m[i] = U8TO32_BE(&in[i * 4]);49in += 64;5051#define G(a,b,c,d,e) \52v[a] += (m[sigma[e+0]] ^ blake256_constants[sigma[e+1]]) + v[b]; \53v[d] = ROTR32(v[d] ^ v[a],16); \54v[c] += v[d]; \55v[b] = ROTR32(v[b] ^ v[c],12); \56v[a] += (m[sigma[e+1]] ^ blake256_constants[sigma[e+0]]) + v[b]; \57v[d] = ROTR32(v[d] ^ v[a], 8); \58v[c] += v[d]; \59v[b] = ROTR32(v[b] ^ v[c], 7);6061for (i = 0, sigma = blake256_sigma; i < 14; i++) {62G(0, 4, 8,12, 0);63G(1, 5, 9,13, 2);64G(2, 6,10,14, 4);65G(3, 7,11,15, 6);6667G(0, 5,10,15, 8);68G(1, 6,11,12,10);69G(2, 7, 8,13,12);70G(3, 4, 9,14,14);7172sigma += 16;73if (sigma == sigma_end)74sigma = blake256_sigma;75}7677#undef G7879for (i = 0; i < 8; i++) h[i] ^= (v[i] ^ v[i + 8]);80}8182for (i = 0; i < 8; i++) S->H[i] = h[i];83for (i = 0; i < 2; i++) S->T[i] = t[i];84}8586static void87scrypt_hash_init(scrypt_hash_state *S) {88S->H[0] = 0x6a09e667ULL;89S->H[1] = 0xbb67ae85ULL;90S->H[2] = 0x3c6ef372ULL;91S->H[3] = 0xa54ff53aULL;92S->H[4] = 0x510e527fULL;93S->H[5] = 0x9b05688cULL;94S->H[6] = 0x1f83d9abULL;95S->H[7] = 0x5be0cd19ULL;96S->T[0] = 0;97S->T[1] = 0;98S->leftover = 0;99}100101static void102scrypt_hash_update(scrypt_hash_state *S, const uint8_t *in, size_t inlen) {103size_t blocks, want;104105/* handle the previous data */106if (S->leftover) {107want = (SCRYPT_HASH_BLOCK_SIZE - S->leftover);108want = (want < inlen) ? want : inlen;109memcpy(S->buffer + S->leftover, in, want);110S->leftover += (uint32_t)want;111if (S->leftover < SCRYPT_HASH_BLOCK_SIZE)112return;113in += want;114inlen -= want;115blake256_blocks(S, S->buffer, 1);116}117118/* handle the current data */119blocks = (inlen & ~(SCRYPT_HASH_BLOCK_SIZE - 1));120S->leftover = (uint32_t)(inlen - blocks);121if (blocks) {122blake256_blocks(S, in, blocks / SCRYPT_HASH_BLOCK_SIZE);123in += blocks;124}125126/* handle leftover data */127if (S->leftover)128memcpy(S->buffer, in, S->leftover);129}130131static void132scrypt_hash_finish(scrypt_hash_state *S, uint8_t *hash) {133uint32_t th, tl, bits;134135bits = (S->leftover << 3);136tl = S->T[0] + bits;137th = S->T[1];138if (S->leftover == 0) {139S->T[0] = (uint32_t)0 - (uint32_t)512;140S->T[1] = (uint32_t)0 - (uint32_t)1;141} else if (S->T[0] == 0) {142S->T[0] = ((uint32_t)0 - (uint32_t)512) + bits;143S->T[1] = S->T[1] - 1;144} else {145S->T[0] -= (512 - bits);146}147148S->buffer[S->leftover] = 0x80;149if (S->leftover <= 55) {150memset(S->buffer + S->leftover + 1, 0, 55 - S->leftover);151} else {152memset(S->buffer + S->leftover + 1, 0, 63 - S->leftover);153blake256_blocks(S, S->buffer, 1);154S->T[0] = (uint32_t)0 - (uint32_t)512;155S->T[1] = (uint32_t)0 - (uint32_t)1;156memset(S->buffer, 0, 56);157}158S->buffer[55] |= 1;159U32TO8_BE(S->buffer + 56, th);160U32TO8_BE(S->buffer + 60, tl);161blake256_blocks(S, S->buffer, 1);162163U32TO8_BE(&hash[ 0], S->H[0]);164U32TO8_BE(&hash[ 4], S->H[1]);165U32TO8_BE(&hash[ 8], S->H[2]);166U32TO8_BE(&hash[12], S->H[3]);167U32TO8_BE(&hash[16], S->H[4]);168U32TO8_BE(&hash[20], S->H[5]);169U32TO8_BE(&hash[24], S->H[6]);170U32TO8_BE(&hash[28], S->H[7]);171}172173static const uint8_t scrypt_test_hash_expected[SCRYPT_HASH_DIGEST_SIZE] = {1740xcc,0xa9,0x1e,0xa9,0x20,0x97,0x37,0x40,0x17,0xc0,0xa0,0x52,0x87,0xfc,0x08,0x20,1750x40,0xf5,0x81,0x86,0x62,0x75,0x78,0xb2,0x79,0xce,0xde,0x27,0x3c,0x7f,0x85,0xd8,176};177178179