Exemple #1
0
void blake256_hash( uint8_t *out, const uint8_t *in, uint64_t inlen )
{
  state256 S;
  blake256_init( &S );
  blake256_update( &S, in, inlen );
  blake256_final( &S, out );
}
Exemple #2
0
unsigned char *blake256_file(unsigned char *out, FILE *fp) {
	state256 context;
	size_t ret = 0;
	int errsv = 0;
	unsigned char buf[8192], *digest = NULL;

	blake256_init(&context);

	for (;;) {
		ret = fread(buf, 1, 8192, fp);
		errsv = errno;

		if ((ret != 8192) && ferror(fp)) {
			errno = errsv;
			return NULL;
		}

		blake256_update(&context, (const uint8_t *) buf, ret);

		if (feof(fp))
			break;
	}

	if (!out) {
		if (!(digest = malloc(32)))
			return NULL;
	} else {
		digest = out;
	}

	blake256_final(&context, (uint8_t *) digest);

	return digest;
}
Exemple #3
0
int main( int argc, char **argv )
{
#define BLOCK256 64
  FILE *fp;
  int i, j, bytesread;
  uint8_t in[BLOCK256], out[32];
  state256 S;
  blake256_test();

  for( i = 1; i < argc; ++i )
  {
    fp = fopen( *( argv + i ), "r" );

    if ( fp == NULL )
    {
      printf( "Error: unable to open %s\n", *( argv + i ) );
      return 1;
    }

    blake256_init( &S );

    while( 1 )
    {
      bytesread = fread( in, 1, BLOCK256, fp );

      if ( bytesread )
        blake256_update( &S, in, bytesread );
      else
        break;
    }

    blake256_final( &S, out );

    for( j = 0; j < 32; ++j )
      printf( "%02x", out[j] );

    printf( " %s\n", *( argv + i ) );
    fclose( fp );
  }

  return 0;
}
Exemple #4
0
void blake256_final( state256 *S, uint8_t *out )
{
  uint8_t msglen[8], zo = 0x01, oo = 0x81;
  uint32_t lo = S->t[0] + ( S->buflen << 3 ), hi = S->t[1];

  /* support for hashing more than 2^32 bits */
  if ( lo < ( S->buflen << 3 ) ) hi++;

  U32TO8_BIG(  msglen + 0, hi );
  U32TO8_BIG(  msglen + 4, lo );

  if ( S->buflen == 55 )   /* one padding byte */
  {
    S->t[0] -= 8;
    blake256_update( S, &oo, 1 );
  }
  else
  {
    if ( S->buflen < 55 )   /* enough space to fill the block  */
    {
      if ( !S->buflen ) S->nullt = 1;

      S->t[0] -= 440 - ( S->buflen << 3 );
      blake256_update( S, padding, 55 - S->buflen );
    }
    else   /* need 2 compressions */
    {
      S->t[0] -= 512 - ( S->buflen << 3 );
      blake256_update( S, padding, 64 - S->buflen );
      S->t[0] -= 440;
      blake256_update( S, padding + 1, 55 );
      S->nullt = 1;
    }

    blake256_update( S, &zo, 1 );
    S->t[0] -= 8;
  }

  S->t[0] -= 64;
  blake256_update( S, msglen, 8 );
  U32TO8_BIG( out + 0, S->h[0] );
  U32TO8_BIG( out + 4, S->h[1] );
  U32TO8_BIG( out + 8, S->h[2] );
  U32TO8_BIG( out + 12, S->h[3] );
  U32TO8_BIG( out + 16, S->h[4] );
  U32TO8_BIG( out + 20, S->h[5] );
  U32TO8_BIG( out + 24, S->h[6] );
  U32TO8_BIG( out + 28, S->h[7] );
}
Exemple #5
0
void precalc_hash_blake256(dev_blk_ctx *blk, uint32_t *data)
{
blake_state256 S;
blake256_init(&S);
blake256_update(&S, data);

blk->ctx_a = S.h[0];
blk->ctx_b = S.h[1];
blk->ctx_c = S.h[2];
blk->ctx_d = S.h[3];
blk->ctx_e = S.h[4];
blk->ctx_f = S.h[5];
blk->ctx_g = S.h[6];
blk->ctx_h = S.h[7];

blk->cty_a = data[16];
blk->cty_b = data[17];
blk->cty_c = data[18];

}
Exemple #6
0
/* BLAKE-256 Generic Interface */
unsigned char *blake256_buffer(
	unsigned char *out,
	const unsigned char *in,
	size_t in_len)
{
	state256 context;
	unsigned char *digest = NULL;

	blake256_init(&context);

	if (!out) {
		if (!(digest = malloc(32)))
			return NULL;
	} else {
		digest = out;
	}

	blake256_update(&context, (const uint8_t *) in, in_len);
	blake256_final(&context, (uint8_t *) digest);

	return digest;
}