void SHA256Transform(SHA256Context *ctx, unsigned char *message, unsigned int block_nb) { uint32_t w[64]; uint32_t wv[8]; unsigned char *sub_block; for (unsigned int i = 1; i <= block_nb; i++) { int j; sub_block = message + ((i - 1) << 6); for (j = 0; j < 16; j++) PACK32(&sub_block[j << 2], &w[j]); for (j = 16; j < 64; j++) SHA256_SCR(j); for (j = 0; j < 8; j++) wv[j] = ctx->h[j]; for (j = 0; j < 64; j++) { uint32_t t1 = wv[7] + SHA256_F2(wv[4]) + CH(wv[4], wv[5], wv[6]) + sha256_k[j] + w[j]; uint32_t t2 = SHA256_F1(wv[0]) + MAJ(wv[0], wv[1], wv[2]); wv[7] = wv[6]; wv[6] = wv[5]; wv[5] = wv[4]; wv[4] = wv[3] + t1; wv[3] = wv[2]; wv[2] = wv[1]; wv[1] = wv[0]; wv[0] = t1 + t2; } for (j = 0; j < 8; j++) ctx->h[j] += wv[j]; } }
void sha256_loc(const unsigned char *buf, unsigned int *per_a, unsigned int *per_b) { uint32_t w[64]; uint32_t wv[8]; uint32_t t1, t2; int j; uint32_t *buf_word_p = (uint32_t *)buf; for(j = 0; j < 3; j++){ w[j] = buf_word_p[13+j]; } for (j = 0; j < 8; j++) { wv[j] = buf_word_p[j]; } for (j = 0; j < 64; j++) { t1 = wv[7] + SHA256_F2(wv[4]) + CH(wv[4], wv[5], wv[6]) + sha256_k[j] + w[j]; t2 = SHA256_F1(wv[0]) + MAJ(wv[0], wv[1], wv[2]); wv[7] = wv[6]; wv[6] = wv[5]; wv[5] = wv[4]; wv[4] = wv[3] + t1; wv[3] = wv[2]; wv[2] = wv[1]; wv[1] = wv[0]; wv[0] = t1 + t2; per_a[j] = wv[0]; per_b[j] = wv[4]; if (j == 2) { break; } } }
void sha256_transf(sha256_ctx *ctx, const unsigned char *message, unsigned int block_nb) { uint32_t w[64]; uint32_t wv[8]; uint32_t t1, t2; const unsigned char *sub_block; int i; int j; for (i = 0; i < (int) block_nb; i++) { sub_block = message + (i << 6); for (j = 0; j < 16; j++) { PACK32(&sub_block[j << 2], &w[j]); } for (j = 16; j < 64; j++) { SHA256_SCR(j); } for (j = 0; j < 8; j++) { wv[j] = ctx->h[j]; } for (j = 0; j < 64; j++) { t1 = wv[7] + SHA256_F2(wv[4]) + CH(wv[4], wv[5], wv[6]) + sha256_k[j] + w[j]; t2 = SHA256_F1(wv[0]) + MAJ(wv[0], wv[1], wv[2]); wv[7] = wv[6]; wv[6] = wv[5]; wv[5] = wv[4]; wv[4] = wv[3] + t1; wv[3] = wv[2]; wv[2] = wv[1]; wv[1] = wv[0]; wv[0] = t1 + t2; if (j == 2) { break; } } for (j = 0; j < 8; j++) { ctx->h[j] = wv[j]; } } }
static void SHA256_transform(struct sha256_ctx *ctx, const uint8_t *message, unsigned int block_nb) { /* Note: this function requires a considerable amount of stack */ uint32_t w[64]; uint32_t wv[8]; uint32_t t1, t2; const unsigned char *sub_block; int i, j; for (i = 0; i < (int) block_nb; i++) { sub_block = message + (i << 6); for (j = 0; j < 16; j++) PACK32(&sub_block[j << 2], &w[j]); for (j = 16; j < 64; j++) SHA256_SCR(j); for (j = 0; j < 8; j++) wv[j] = ctx->h[j]; for (j = 0; j < 64; j++) { t1 = wv[7] + SHA256_F2(wv[4]) + CH(wv[4], wv[5], wv[6]) + sha256_k[j] + w[j]; t2 = SHA256_F1(wv[0]) + MAJ(wv[0], wv[1], wv[2]); wv[7] = wv[6]; wv[6] = wv[5]; wv[5] = wv[4]; wv[4] = wv[3] + t1; wv[3] = wv[2]; wv[2] = wv[1]; wv[1] = wv[0]; wv[0] = t1 + t2; } for (j = 0; j < 8; j++) ctx->h[j] += wv[j]; } }
void SHA256::transform(const SHA256::uint8 *message, unsigned int block_nb) { uint32 w[64]; uint32 wv[8]; uint32 t1, t2; const SHA256::uint8 *sub_block; int i; int j; for (i = 0; i < (int) block_nb; i++) { sub_block = message + (i << 6); for (j = 0; j < 16; j++) { SHA2_PACK32(&sub_block[j << 2], &w[j]); } for (j = 16; j < 64; j++) { w[j] = SHA256_F4(w[j - 2]) + w[j - 7] + SHA256_F3(w[j - 15]) + w[j - 16]; } for (j = 0; j < 8; j++) { wv[j] = m_h[j]; } for (j = 0; j < 64; j++) { t1 = wv[7] + SHA256_F2(wv[4]) + SHA2_CH(wv[4], wv[5], wv[6]) + sha256_k[j] + w[j]; t2 = SHA256_F1(wv[0]) + SHA2_MAJ(wv[0], wv[1], wv[2]); wv[7] = wv[6]; wv[6] = wv[5]; wv[5] = wv[4]; wv[4] = wv[3] + t1; wv[3] = wv[2]; wv[2] = wv[1]; wv[1] = wv[0]; wv[0] = t1 + t2; } for (j = 0; j < 8; j++) { m_h[j] += wv[j]; } } }
static void sha256_transf( sha256_ctx * ctx, const unsigned char * message, unsigned int block_nb ) { uint32 w[ 64 ]; uint32 wv[ 8 ]; uint32 t1, t2; const unsigned char * sub_block; int i; #ifndef UNROLL_LOOPS int j; #endif for( i = 0; i < ( int ) block_nb; i++ ) { sub_block = message + ( i << 6 ); #ifndef UNROLL_LOOPS for( j = 0; j < 16; j++ ) { PACK32( &sub_block[ j << 2 ], &w[ j ] ); } for( j = 16; j < 64; j++ ) { SHA256_SCR( j ); } for( j = 0; j < 8; j++ ) { wv[ j ] = ctx->h[ j ]; } for( j = 0; j < 64; j++ ) { t1 = wv[ 7 ] + SHA256_F2( wv[ 4 ] ) + CH( wv[ 4 ], wv[ 5 ], wv[ 6 ] ) + sha256_k[ j ] + w[ j ]; t2 = SHA256_F1( wv[ 0 ] ) + MAJ( wv[ 0 ], wv[ 1 ], wv[ 2 ] ); wv[ 7 ] = wv[ 6 ]; wv[ 6 ] = wv[ 5 ]; wv[ 5 ] = wv[ 4 ]; wv[ 4 ] = wv[ 3 ] + t1; wv[ 3 ] = wv[ 2 ]; wv[ 2 ] = wv[ 1 ]; wv[ 1 ] = wv[ 0 ]; wv[ 0 ] = t1 + t2; } for( j = 0; j < 8; j++ ) { ctx->h[ j ] += wv[ j ]; } #else PACK32( &sub_block[ 0 ], &w[ 0 ] ); PACK32( &sub_block[ 4 ], &w[ 1 ] ); PACK32( &sub_block[ 8 ], &w[ 2 ] ); PACK32( &sub_block[ 12 ], &w[ 3 ] ); PACK32( &sub_block[ 16 ], &w[ 4 ] ); PACK32( &sub_block[ 20 ], &w[ 5 ] ); PACK32( &sub_block[ 24 ], &w[ 6 ] ); PACK32( &sub_block[ 28 ], &w[ 7 ] ); PACK32( &sub_block[ 32 ], &w[ 8 ] ); PACK32( &sub_block[ 36 ], &w[ 9 ] ); PACK32( &sub_block[ 40 ], &w[ 10 ] ); PACK32( &sub_block[ 44 ], &w[ 11 ] ); PACK32( &sub_block[ 48 ], &w[ 12 ] ); PACK32( &sub_block[ 52 ], &w[ 13 ] ); PACK32( &sub_block[ 56 ], &w[ 14 ] ); PACK32( &sub_block[ 60 ], &w[ 15 ] ); SHA256_SCR( 16 ); SHA256_SCR( 17 ); SHA256_SCR( 18 ); SHA256_SCR( 19 ); SHA256_SCR( 20 ); SHA256_SCR( 21 ); SHA256_SCR( 22 ); SHA256_SCR( 23 ); SHA256_SCR( 24 ); SHA256_SCR( 25 ); SHA256_SCR( 26 ); SHA256_SCR( 27 ); SHA256_SCR( 28 ); SHA256_SCR( 29 ); SHA256_SCR( 30 ); SHA256_SCR( 31 ); SHA256_SCR( 32 ); SHA256_SCR( 33 ); SHA256_SCR( 34 ); SHA256_SCR( 35 ); SHA256_SCR( 36 ); SHA256_SCR( 37 ); SHA256_SCR( 38 ); SHA256_SCR( 39 ); SHA256_SCR( 40 ); SHA256_SCR( 41 ); SHA256_SCR( 42 ); SHA256_SCR( 43 ); SHA256_SCR( 44 ); SHA256_SCR( 45 ); SHA256_SCR( 46 ); SHA256_SCR( 47 ); SHA256_SCR( 48 ); SHA256_SCR( 49 ); SHA256_SCR( 50 ); SHA256_SCR( 51 ); SHA256_SCR( 52 ); SHA256_SCR( 53 ); SHA256_SCR( 54 ); SHA256_SCR( 55 ); SHA256_SCR( 56 ); SHA256_SCR( 57 ); SHA256_SCR( 58 ); SHA256_SCR( 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, 0, 1, 2, 3, 20 ); SHA256_EXP( 3, 4, 5, 6, 7, 0, 1, 2, 21 ); SHA256_EXP( 2, 3, 4, 5, 6, 7, 0, 1, 22 ); SHA256_EXP( 1, 2, 3, 4, 5, 6, 7, 0, 23 ); SHA256_EXP( 0, 1, 2, 3, 4, 5, 6, 7, 24 ); SHA256_EXP( 7, 0, 1, 2, 3, 4, 5, 6, 25 ); SHA256_EXP( 6, 7, 0, 1, 2, 3, 4, 5, 26 ); SHA256_EXP( 5, 6, 7, 0, 1, 2, 3, 4, 27 ); SHA256_EXP( 4, 5, 6, 7, 0, 1, 2, 3, 28 ); SHA256_EXP( 3, 4, 5, 6, 7, 0, 1, 2, 29 ); SHA256_EXP( 2, 3, 4, 5, 6, 7, 0, 1, 30 ); SHA256_EXP( 1, 2, 3, 4, 5, 6, 7, 0, 31 ); SHA256_EXP( 0, 1, 2, 3, 4, 5, 6, 7, 32 ); SHA256_EXP( 7, 0, 1, 2, 3, 4, 5, 6, 33 ); SHA256_EXP( 6, 7, 0, 1, 2, 3, 4, 5, 34 ); SHA256_EXP( 5, 6, 7, 0, 1, 2, 3, 4, 35 ); SHA256_EXP( 4, 5, 6, 7, 0, 1, 2, 3, 36 ); SHA256_EXP( 3, 4, 5, 6, 7, 0, 1, 2, 37 ); SHA256_EXP( 2, 3, 4, 5, 6, 7, 0, 1, 38 ); SHA256_EXP( 1, 2, 3, 4, 5, 6, 7, 0, 39 ); SHA256_EXP( 0, 1, 2, 3, 4, 5, 6, 7, 40 ); SHA256_EXP( 7, 0, 1, 2, 3, 4, 5, 6, 41 ); SHA256_EXP( 6, 7, 0, 1, 2, 3, 4, 5, 42 ); SHA256_EXP( 5, 6, 7, 0, 1, 2, 3, 4, 43 ); SHA256_EXP( 4, 5, 6, 7, 0, 1, 2, 3, 44 ); SHA256_EXP( 3, 4, 5, 6, 7, 0, 1, 2, 45 ); SHA256_EXP( 2, 3, 4, 5, 6, 7, 0, 1, 46 ); SHA256_EXP( 1, 2, 3, 4, 5, 6, 7, 0, 47 ); SHA256_EXP( 0, 1, 2, 3, 4, 5, 6, 7, 48 ); SHA256_EXP( 7, 0, 1, 2, 3, 4, 5, 6, 49 ); SHA256_EXP( 6, 7, 0, 1, 2, 3, 4, 5, 50 ); SHA256_EXP( 5, 6, 7, 0, 1, 2, 3, 4, 51 ); SHA256_EXP( 4, 5, 6, 7, 0, 1, 2, 3, 52 ); SHA256_EXP( 3, 4, 5, 6, 7, 0, 1, 2, 53 ); SHA256_EXP( 2, 3, 4, 5, 6, 7, 0, 1, 54 ); SHA256_EXP( 1, 2, 3, 4, 5, 6, 7, 0, 55 ); SHA256_EXP( 0, 1, 2, 3, 4, 5, 6, 7, 56 ); SHA256_EXP( 7, 0, 1, 2, 3, 4, 5, 6, 57 ); SHA256_EXP( 6, 7, 0, 1, 2, 3, 4, 5, 58 ); SHA256_EXP( 5, 6, 7, 0, 1, 2, 3, 4, 59 ); SHA256_EXP( 4, 5, 6, 7, 0, 1, 2, 3, 60 ); SHA256_EXP( 3, 4, 5, 6, 7, 0, 1, 2, 61 ); SHA256_EXP( 2, 3, 4, 5, 6, 7, 0, 1, 62 ); SHA256_EXP( 1, 2, 3, 4, 5, 6, 7, 0, 63 ); ctx->h[ 0 ] += wv[ 0 ]; ctx->h[ 1 ] += wv[ 1 ]; ctx->h[ 2 ] += wv[ 2 ]; ctx->h[ 3 ] += wv[ 3 ]; ctx->h[ 4 ] += wv[ 4 ]; ctx->h[ 5 ] += wv[ 5 ]; ctx->h[ 6 ] += wv[ 6 ]; ctx->h[ 7 ] += wv[ 7 ]; #endif /* !UNROLL_LOOPS */ } }