예제 #1
0
void SHA512::transform (const unsigned char *message, unsigned int block_nb) {
	uint64 w[80];
	uint64 wv[8];
	uint64 t1, t2;
	const unsigned char *sub_block;
	int i, j;
	for (i = 0; i < (int)block_nb; i++) {
		sub_block = message + (i << 7);
		for (j = 0; j < 16; j++) {
			SHA2_PACK64(&sub_block[j << 3], &w[j]);
		}
		for (j = 16; j < 80; j++) {
			w[j] = SHA512_F4(w[j - 2]) + w[j - 7] + SHA512_F3(w[j - 15]) + w[j - 16];
		}
		for (j = 0; j < 8; j++) {
			wv[j] = m_h[j];
		}
		for (j = 0; j < 80; j++) {
			t1 = wv[7] + SHA512_F2(wv[4]) + SHA2_CH(wv[4], wv[5], wv[6]) + sha512_k[j] + w[j];
			t2 = SHA512_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];
		}

	}
}
예제 #2
0
파일: sha2.c 프로젝트: xharbour/core
static void sha512_transf( sha512_ctx * ctx, const unsigned char * message,
                           unsigned int block_nb )
{
   uint64                  w[ 80 ];
   uint64                  wv[ 8 ];
   uint64                  t1, t2;
   const unsigned char *   sub_block;
   int                     i, j;

   for( i = 0; i < ( int ) block_nb; i++ )
   {
      sub_block = message + ( i << 7 );

#ifndef UNROLL_LOOPS
      for( j = 0; j < 16; j++ )
      {
         PACK64( &sub_block[ j << 3 ], &w[ j ] );
      }

      for( j = 16; j < 80; j++ )
      {
         SHA512_SCR( j );
      }

      for( j = 0; j < 8; j++ )
      {
         wv[ j ] = ctx->h[ j ];
      }

      for( j = 0; j < 80; j++ )
      {
         t1       = wv[ 7 ] + SHA512_F2( wv[ 4 ] ) + CH( wv[ 4 ], wv[ 5 ], wv[ 6 ] )
                    + sha512_k[ j ] + w[ j ];
         t2       = SHA512_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
      PACK64( &sub_block[ 0 ], &w[ 0 ] ); PACK64( &sub_block[ 8 ], &w[ 1 ] );
      PACK64( &sub_block[ 16 ], &w[ 2 ] ); PACK64( &sub_block[ 24 ], &w[ 3 ] );
      PACK64( &sub_block[ 32 ], &w[ 4 ] ); PACK64( &sub_block[ 40 ], &w[ 5 ] );
      PACK64( &sub_block[ 48 ], &w[ 6 ] ); PACK64( &sub_block[ 56 ], &w[ 7 ] );
      PACK64( &sub_block[ 64 ], &w[ 8 ] ); PACK64( &sub_block[ 72 ], &w[ 9 ] );
      PACK64( &sub_block[ 80 ], &w[ 10 ] ); PACK64( &sub_block[ 88 ], &w[ 11 ] );
      PACK64( &sub_block[ 96 ], &w[ 12 ] ); PACK64( &sub_block[ 104 ], &w[ 13 ] );
      PACK64( &sub_block[ 112 ], &w[ 14 ] ); PACK64( &sub_block[ 120 ], &w[ 15 ] );

      SHA512_SCR( 16 ); SHA512_SCR( 17 ); SHA512_SCR( 18 ); SHA512_SCR( 19 );
      SHA512_SCR( 20 ); SHA512_SCR( 21 ); SHA512_SCR( 22 ); SHA512_SCR( 23 );
      SHA512_SCR( 24 ); SHA512_SCR( 25 ); SHA512_SCR( 26 ); SHA512_SCR( 27 );
      SHA512_SCR( 28 ); SHA512_SCR( 29 ); SHA512_SCR( 30 ); SHA512_SCR( 31 );
      SHA512_SCR( 32 ); SHA512_SCR( 33 ); SHA512_SCR( 34 ); SHA512_SCR( 35 );
      SHA512_SCR( 36 ); SHA512_SCR( 37 ); SHA512_SCR( 38 ); SHA512_SCR( 39 );
      SHA512_SCR( 40 ); SHA512_SCR( 41 ); SHA512_SCR( 42 ); SHA512_SCR( 43 );
      SHA512_SCR( 44 ); SHA512_SCR( 45 ); SHA512_SCR( 46 ); SHA512_SCR( 47 );
      SHA512_SCR( 48 ); SHA512_SCR( 49 ); SHA512_SCR( 50 ); SHA512_SCR( 51 );
      SHA512_SCR( 52 ); SHA512_SCR( 53 ); SHA512_SCR( 54 ); SHA512_SCR( 55 );
      SHA512_SCR( 56 ); SHA512_SCR( 57 ); SHA512_SCR( 58 ); SHA512_SCR( 59 );
      SHA512_SCR( 60 ); SHA512_SCR( 61 ); SHA512_SCR( 62 ); SHA512_SCR( 63 );
      SHA512_SCR( 64 ); SHA512_SCR( 65 ); SHA512_SCR( 66 ); SHA512_SCR( 67 );
      SHA512_SCR( 68 ); SHA512_SCR( 69 ); SHA512_SCR( 70 ); SHA512_SCR( 71 );
      SHA512_SCR( 72 ); SHA512_SCR( 73 ); SHA512_SCR( 74 ); SHA512_SCR( 75 );
      SHA512_SCR( 76 ); SHA512_SCR( 77 ); SHA512_SCR( 78 ); SHA512_SCR( 79 );

      wv[ 0 ]  = ctx->h[ 0 ]; wv[ 1 ] = ctx->h[ 1 ];
      wv[ 2 ]  = ctx->h[ 2 ]; wv[ 3 ] = ctx->h[ 3 ];
      wv[ 4 ]  = ctx->h[ 4 ]; wv[ 5 ] = ctx->h[ 5 ];
      wv[ 6 ]  = ctx->h[ 6 ]; wv[ 7 ] = ctx->h[ 7 ];

      j        = 0;

      do
      {
         SHA512_EXP( 0, 1, 2, 3, 4, 5, 6, 7, j ); j++;
         SHA512_EXP( 7, 0, 1, 2, 3, 4, 5, 6, j ); j++;
         SHA512_EXP( 6, 7, 0, 1, 2, 3, 4, 5, j ); j++;
         SHA512_EXP( 5, 6, 7, 0, 1, 2, 3, 4, j ); j++;
         SHA512_EXP( 4, 5, 6, 7, 0, 1, 2, 3, j ); j++;
         SHA512_EXP( 3, 4, 5, 6, 7, 0, 1, 2, j ); j++;
         SHA512_EXP( 2, 3, 4, 5, 6, 7, 0, 1, j ); j++;
         SHA512_EXP( 1, 2, 3, 4, 5, 6, 7, 0, j ); j++;
      }
      while( j < 80 );

      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 */
   }
}