/* * Process a single block of input using the hash algorithm. */ void SHA256::ProcessBlock(const unsigned char *block) { uint32_t W[64]; uint32_t a, b, c, d, e, f, g, h; uint32_t temp, temp2; int t; /* Unpack the block into 64 32-bit words */ for(t = 0; t < 16; ++t) { W[t] = (((uint32_t)(block[t * 4 + 0])) << 24) | (((uint32_t)(block[t * 4 + 1])) << 16) | (((uint32_t)(block[t * 4 + 2])) << 8) | ((uint32_t)(block[t * 4 + 3])); } for(t = 16; t < 64; ++t) { W[t] = TRUNCLONG(RHO1(W[t - 2]) + W[t - 7] + RHO0(W[t - 15]) + W[t - 16]); } /* Load the SHA-256 state into local variables */ a = this->A; b = this->B; c = this->C; d = this->D; e = this->E; f = this->F; g = this->G; h = this->H; /* Perform 64 rounds of hash computations */ for(t = 0; t < 64; ++t) { temp = TRUNCLONG(h + SUM1(e) + CH(e, f, g) + K[t] + W[t]); temp2 = TRUNCLONG(SUM0(a) + MAJ(a, b, c)); h = g; g = f; f = e; e = TRUNCLONG(d + temp); d = c; c = b; b = a; a = TRUNCLONG(temp + temp2); } /* Combine the previous SHA-256 state with the new state */ this->A = TRUNCLONG(this->A + a); this->B = TRUNCLONG(this->B + b); this->C = TRUNCLONG(this->C + c); this->D = TRUNCLONG(this->D + d); this->E = TRUNCLONG(this->E + e); this->F = TRUNCLONG(this->F + f); this->G = TRUNCLONG(this->G + g); this->H = TRUNCLONG(this->H + h); /* Clear the temporary state */ memset(W, 0 , sizeof(uint32_t) * 64); a = b = c = d = e = f = g = h = temp = temp2 = 0; }
XMoshSum digestBlock( hp_byte* initialByteBlock, XMoshSum sum ) { sha2_word block[ ROUNDS ]; sha2_word* words = ( sha2_word* ) &sum.data; for( unsigned int i = 0; i < 16; i++ ) { BLOCK_COPY( block, initialByteBlock, i ); } for( unsigned i = 16; i < ROUNDS; i++ ) { block[ i ] = block[ i - 16 ] + SIGMA0( block, i ) + block[ i - 7 ] + SIGMA1( block, i ); } sha2_word a = words[ 0 ]; sha2_word b = words[ 1 ]; sha2_word c = words[ 2 ]; sha2_word d = words[ 3 ]; sha2_word e = words[ 4 ]; sha2_word f = words[ 5 ]; sha2_word g = words[ 6 ]; sha2_word h = words[ 7 ]; sha2_word temp1, temp2; for( unsigned i = 0; i < ROUNDS; i++ ) { temp1 = ( h + ( SUM1( e ) ) + CH( e, f, g ) + K[ i ] + block[ i ] ); temp2 = ( SUM0( a ) ) + MAJ( a, b, c ); h = g; g = f; f = e; e = d + temp1; d = c; c = b; b = a; a = temp1 + temp2; } words[ 0 ] += a; words[ 1 ] += b; words[ 2 ] += c; words[ 3 ] += d; words[ 4 ] += e; words[ 5 ] += f; words[ 6 ] += g; words[ 7 ] += h; return sum; }