Path: blob/linux/scryptjane/scrypt-jane-hash_sha256.h
1201 views
#define SCRYPT_HASH "SHA-2-256"1#define SCRYPT_HASH_BLOCK_SIZE 642#define SCRYPT_HASH_DIGEST_SIZE 3234typedef uint8_t scrypt_hash_digest[SCRYPT_HASH_DIGEST_SIZE];56typedef struct scrypt_hash_state_t {7uint32_t H[8];8uint64_t T;9uint32_t leftover;10uint8_t buffer[SCRYPT_HASH_BLOCK_SIZE];11} scrypt_hash_state;1213static const uint32_t sha256_constants[64] = {140x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,150xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,160xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,170x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,180x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,190xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,200x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,210x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f222};2324#define Ch(x,y,z) (z ^ (x & (y ^ z)))25#define Maj(x,y,z) (((x | y) & z) | (x & y))26#define S0(x) (ROTR32(x, 2) ^ ROTR32(x, 13) ^ ROTR32(x, 22))27#define S1(x) (ROTR32(x, 6) ^ ROTR32(x, 11) ^ ROTR32(x, 25))28#define G0(x) (ROTR32(x, 7) ^ ROTR32(x, 18) ^ (x >> 3))29#define G1(x) (ROTR32(x, 17) ^ ROTR32(x, 19) ^ (x >> 10))30#define W0(in,i) (U8TO32_BE(&in[i * 4]))31#define W1(i) (G1(w[i - 2]) + w[i - 7] + G0(w[i - 15]) + w[i - 16])32#define STEP(i) \33t1 = S0(r[0]) + Maj(r[0], r[1], r[2]); \34t0 = r[7] + S1(r[4]) + Ch(r[4], r[5], r[6]) + sha256_constants[i] + w[i]; \35r[7] = r[6]; \36r[6] = r[5]; \37r[5] = r[4]; \38r[4] = r[3] + t0; \39r[3] = r[2]; \40r[2] = r[1]; \41r[1] = r[0]; \42r[0] = t0 + t1;4344static void45sha256_blocks(scrypt_hash_state *S, const uint8_t *in, size_t blocks) {46uint32_t r[8], w[64], t0, t1;47size_t i;4849for (i = 0; i < 8; i++) r[i] = S->H[i];5051while (blocks--) {52for (i = 0; i < 16; i++) { w[i] = W0(in, i); }53for (i = 16; i < 64; i++) { w[i] = W1(i); }54for (i = 0; i < 64; i++) { STEP(i); }55for (i = 0; i < 8; i++) { r[i] += S->H[i]; S->H[i] = r[i]; }56S->T += SCRYPT_HASH_BLOCK_SIZE * 8;57in += SCRYPT_HASH_BLOCK_SIZE;58}59}6061static void62scrypt_hash_init(scrypt_hash_state *S) {63S->H[0] = 0x6a09e667;64S->H[1] = 0xbb67ae85;65S->H[2] = 0x3c6ef372;66S->H[3] = 0xa54ff53a;67S->H[4] = 0x510e527f;68S->H[5] = 0x9b05688c;69S->H[6] = 0x1f83d9ab;70S->H[7] = 0x5be0cd19;71S->T = 0;72S->leftover = 0;73}7475static void76scrypt_hash_update(scrypt_hash_state *S, const uint8_t *in, size_t inlen) {77size_t blocks, want;7879/* handle the previous data */80if (S->leftover) {81want = (SCRYPT_HASH_BLOCK_SIZE - S->leftover);82want = (want < inlen) ? want : inlen;83memcpy(S->buffer + S->leftover, in, want);84S->leftover += (uint32_t)want;85if (S->leftover < SCRYPT_HASH_BLOCK_SIZE)86return;87in += want;88inlen -= want;89sha256_blocks(S, S->buffer, 1);90}9192/* handle the current data */93blocks = (inlen & ~(SCRYPT_HASH_BLOCK_SIZE - 1));94S->leftover = (uint32_t)(inlen - blocks);95if (blocks) {96sha256_blocks(S, in, blocks / SCRYPT_HASH_BLOCK_SIZE);97in += blocks;98}99100/* handle leftover data */101if (S->leftover)102memcpy(S->buffer, in, S->leftover);103}104105static void106scrypt_hash_finish(scrypt_hash_state *S, uint8_t *hash) {107uint64_t t = S->T + (S->leftover * 8);108109S->buffer[S->leftover] = 0x80;110if (S->leftover <= 55) {111memset(S->buffer + S->leftover + 1, 0, 55 - S->leftover);112} else {113memset(S->buffer + S->leftover + 1, 0, 63 - S->leftover);114sha256_blocks(S, S->buffer, 1);115memset(S->buffer, 0, 56);116}117118U64TO8_BE(S->buffer + 56, t);119sha256_blocks(S, S->buffer, 1);120121U32TO8_BE(&hash[ 0], S->H[0]);122U32TO8_BE(&hash[ 4], S->H[1]);123U32TO8_BE(&hash[ 8], S->H[2]);124U32TO8_BE(&hash[12], S->H[3]);125U32TO8_BE(&hash[16], S->H[4]);126U32TO8_BE(&hash[20], S->H[5]);127U32TO8_BE(&hash[24], S->H[6]);128U32TO8_BE(&hash[28], S->H[7]);129}130131static const uint8_t scrypt_test_hash_expected[SCRYPT_HASH_DIGEST_SIZE] = {1320xee,0x36,0xae,0xa6,0x65,0xf0,0x28,0x7d,0xc9,0xde,0xd8,0xad,0x48,0x33,0x7d,0xbf,1330xcb,0xc0,0x48,0xfa,0x5f,0x92,0xfd,0x0a,0x95,0x6f,0x34,0x8e,0x8c,0x1e,0x73,0xad,134};135136137