This commit is contained in:
Jay D Dee
2017-01-31 20:12:56 -05:00
parent c1d6c3a57f
commit 698286189b
43 changed files with 844 additions and 481 deletions

View File

@@ -6,8 +6,10 @@
* This code is placed in the public domain
*/
#include <memory.h>
#include "hash-groestl.h"
#include "miner.h"
#include "avxdefs.h"
#ifndef NO_AES_NI
@@ -53,15 +55,9 @@ void Transform( hashState_groestl *ctx, const u8 *in, unsigned long long len )
{
/* increment block counter */
ctx->block_counter += len/SIZE;
/* digest message, one block at a time */
for (; len >= SIZE; len -= SIZE, in += SIZE)
#if LENGTH<=256
TF512((u64*)ctx->chaining, (u64*)in);
#else
TF1024((u64*)ctx->chaining, (u64*)in);
#endif
for ( ; len >= SIZE; len -= SIZE, in += SIZE )
TF1024( (u64*)ctx->chaining, (u64*)in );
asm volatile ("emms");
}
@@ -69,73 +65,57 @@ void Transform( hashState_groestl *ctx, const u8 *in, unsigned long long len )
void OutputTransformation( hashState_groestl *ctx )
{
/* determine variant */
#if (LENGTH <= 256)
OF512((u64*)ctx->chaining);
#else
OF1024((u64*)ctx->chaining);
#endif
OF1024( (u64*)ctx->chaining );
asm volatile ("emms");
}
/* initialise context */
HashReturn_gr init_groestl( hashState_groestl* ctx )
HashReturn_gr init_groestl( hashState_groestl* ctx, int hashlen )
{
u8 i = 0;
/* output size (in bits) must be a positive integer less than or
equal to 512, and divisible by 8 */
if (LENGTH <= 0 || (LENGTH%8) || LENGTH > 512)
return BAD_HASHBITLEN_GR;
/* set number of state columns and state size depending on
variant */
ctx->columns = COLS;
ctx->statesize = SIZE;
#if (LENGTH <= 256)
ctx->v = SHoRT;
#else
ctx->v = LoNG;
#endif
ctx->hashlen = hashlen;
SET_CONSTANTS();
for (i=0; i<SIZE/8; i++)
for ( i = 0; i < SIZE / 8; i++ )
ctx->chaining[i] = 0;
for (i=0; i<SIZE; i++)
for ( i = 0; i < SIZE; i++ )
ctx->buffer[i] = 0;
if (ctx->chaining == NULL || ctx->buffer == NULL)
return FAIL_GR;
/* set initial value */
ctx->chaining[ctx->columns-1] = U64BIG((u64)LENGTH);
ctx->chaining[COLS-1] = U64BIG((u64)LENGTH);
INIT(ctx->chaining);
/* set other variables */
ctx->buf_ptr = 0;
ctx->block_counter = 0;
return SUCCESS_GR;
}
/*
HashReturn_gr init_groestl( hashState_groestl* ctx )
{
return Xinit_groestl( ctx, 64 );
}
*/
HashReturn_gr reinit_groestl( hashState_groestl* ctx )
{
int i;
for (i=0; i<SIZE/8; i++)
for ( i = 0; i < SIZE / 8; i++ )
ctx->chaining[i] = 0;
for (i=0; i<SIZE; i++)
for ( i = 0; i < SIZE; i++ )
ctx->buffer[i] = 0;
if (ctx->chaining == NULL || ctx->buffer == NULL)
return FAIL_GR;
/* set initial value */
ctx->chaining[ctx->columns-1] = U64BIG((u64)LENGTH);
INIT(ctx->chaining);
/* set other variables */
ctx->chaining[COLS-1] = U64BIG( (u64)LENGTH );
INIT( ctx->chaining );
ctx->buf_ptr = 0;
ctx->block_counter = 0;
@@ -147,21 +127,16 @@ HashReturn_gr update_groestl( hashState_groestl* ctx,
const BitSequence_gr* input,
DataLength_gr databitlen )
{
int index = 0;
int msglen = (int)(databitlen/8);
int rem = (int)(databitlen%8); // not used
int i;
const int msglen = (int)(databitlen/8);
/* digest bulk of message */
Transform( ctx, input+index, msglen-index );
// this line makes no sense, index = 0 before and after
// but removing this line breaks the hash.
index += ((msglen-index)/ctx->statesize)*ctx->statesize;
Transform( ctx, input, msglen );
/* store remaining data in buffer */
while (index < msglen)
ctx->buffer[(int)ctx->buf_ptr++] = input[index++];
i = ( msglen / SIZE ) * SIZE;
while ( i < msglen )
ctx->buffer[(int)ctx->buf_ptr++] = input[i++];
return SUCCESS_GR;
}
@@ -171,43 +146,97 @@ HashReturn_gr update_groestl( hashState_groestl* ctx,
HashReturn_gr final_groestl( hashState_groestl* ctx,
BitSequence_gr* output )
{
int i, j = 0, hashbytelen = LENGTH/8;
u8 *s = (BitSequence_gr*)ctx->chaining;
int i, j;
ctx->buffer[(int)ctx->buf_ptr++] = 0x80;
/* pad with '0'-bits */
if ( ctx->buf_ptr > ctx->statesize-LENGTHFIELDLEN )
if ( ctx->buf_ptr > SIZE - LENGTHFIELDLEN )
{
/* padding requires two blocks */
while ( ctx->buf_ptr < ctx->statesize )
while ( ctx->buf_ptr < SIZE )
ctx->buffer[(int)ctx->buf_ptr++] = 0;
/* digest first padding block */
Transform( ctx, ctx->buffer, ctx->statesize );
Transform( ctx, ctx->buffer, SIZE );
ctx->buf_ptr = 0;
}
// this will pad up to 120 bytes
while (ctx->buf_ptr < ctx->statesize-LENGTHFIELDLEN)
while ( ctx->buf_ptr < SIZE - LENGTHFIELDLEN )
ctx->buffer[(int)ctx->buf_ptr++] = 0;
/* length padding */
ctx->block_counter++;
ctx->buf_ptr = ctx->statesize;
while (ctx->buf_ptr > ctx->statesize-LENGTHFIELDLEN)
ctx->buf_ptr = SIZE;
while ( ctx->buf_ptr > SIZE - LENGTHFIELDLEN )
{
ctx->buffer[(int)--ctx->buf_ptr] = (u8)ctx->block_counter;
ctx->block_counter >>= 8;
}
/* digest final padding block */
Transform(ctx, ctx->buffer, ctx->statesize);
Transform( ctx, ctx->buffer, SIZE );
/* perform output transformation */
OutputTransformation(ctx);
OutputTransformation( ctx );
/* store hash result in output */
for (i = ctx->statesize-hashbytelen; i < ctx->statesize; i++,j++)
output[j] = s[i];
// store hash result in output
for ( i = ( SIZE - ctx->hashlen) / 16, j = 0; i < SIZE / 16; i++, j++ )
casti_m128i( output, j ) = casti_m128i( ctx->chaining , i );
return SUCCESS_GR;
}
HashReturn_gr update_and_final_groestl( hashState_groestl* ctx,
BitSequence_gr* output, const BitSequence_gr* input,
DataLength_gr databitlen )
{
const int inlen = (int)(databitlen/8); // need bytes
int i, j;
/* digest bulk of message */
Transform( ctx, input, inlen );
/* store remaining data in buffer */
i = ( inlen / SIZE ) * SIZE;
while ( i < inlen )
ctx->buffer[(int)ctx->buf_ptr++] = input[i++];
// start of final
ctx->buffer[(int)ctx->buf_ptr++] = 0x80;
/* pad with '0'-bits */
if ( ctx->buf_ptr > SIZE - LENGTHFIELDLEN )
{
/* padding requires two blocks */
while ( ctx->buf_ptr < SIZE )
ctx->buffer[(int)ctx->buf_ptr++] = 0;
memset( ctx->buffer + ctx->buf_ptr, 0, SIZE - ctx->buf_ptr );
/* digest first padding block */
Transform( ctx, ctx->buffer, SIZE );
ctx->buf_ptr = 0;
}
// this will pad up to 120 bytes
memset( ctx->buffer + ctx->buf_ptr, 0, SIZE - ctx->buf_ptr - LENGTHFIELDLEN );
/* length padding */
ctx->block_counter++;
ctx->buf_ptr = SIZE;
while (ctx->buf_ptr > SIZE - LENGTHFIELDLEN)
{
ctx->buffer[(int)--ctx->buf_ptr] = (u8)ctx->block_counter;
ctx->block_counter >>= 8;
}
/* digest final padding block */
Transform( ctx, ctx->buffer, SIZE );
/* perform output transformation */
OutputTransformation( ctx );
// store hash result in output
for ( i = ( SIZE - ctx->hashlen) / 16, j = 0; i < SIZE / 16; i++, j++ )
casti_m128i( output, j ) = casti_m128i( ctx->chaining , i );
return SUCCESS_GR;
}
@@ -221,7 +250,7 @@ HashReturn_gr hash_groestl(int hashbitlen,
hashState_groestl context;
/* initialise */
if ((ret = init_groestl(&context)) != SUCCESS_GR)
if ((ret = init_groestl( &context, hashbitlen/8 )) != SUCCESS_GR)
return ret;
/* process message */

View File

@@ -15,35 +15,12 @@
#endif
#include <stdlib.h>
/* eBash API begin */
/*
#include "crypto_hash.h"
#ifdef crypto_hash_BYTES
#include <crypto_uint8.h>
#include <crypto_uint32.h>
#include <crypto_uint64.h>
typedef crypto_uint8 u8;
typedef crypto_uint32 u32;
typedef crypto_uint64 u64;
#endif
* /
/* eBash API end */
#define LENGTH (512)
#include "brg_endian.h"
#define NEED_UINT_64T
#include "brg_types.h"
#ifdef IACA_TRACE
#include IACA_MARKS
#endif
#ifndef LENGTH
#define LENGTH (256)
#endif
/* some sizes (number of bytes) */
#define ROWS (8)
#define LENGTHFIELDLEN (ROWS)
@@ -80,10 +57,6 @@ typedef crypto_uint64 u64;
(ROTL64(a,56) & li_64(FF000000FF000000)))
#endif /* IS_LITTLE_ENDIAN */
typedef enum { LoNG, SHoRT } Var;
/* NIST API begin */
typedef unsigned char BitSequence_gr;
typedef unsigned long long DataLength_gr;
typedef enum { SUCCESS_GR = 0, FAIL_GR = 1, BAD_HASHBITLEN_GR = 2} HashReturn_gr;
@@ -91,22 +64,28 @@ typedef enum { SUCCESS_GR = 0, FAIL_GR = 1, BAD_HASHBITLEN_GR = 2} HashReturn_gr
// Use area128 overlay for buffer to facilitate fast copying
typedef struct {
__attribute__ ((aligned (32))) u64 chaining[SIZE/8]; /* actual state */
__attribute__ ((aligned (32))) BitSequence_gr buffer[SIZE]; /* data buffer */
__attribute__ ((aligned (32))) u64 chaining[SIZE/8]; // actual state
__attribute__ ((aligned (32))) BitSequence_gr buffer[SIZE]; // data buffer
u64 block_counter; /* message block counter */
int hashlen; // bytes
int buf_ptr; /* data buffer pointer */
int columns; /* no. of columns in state */
int statesize; /* total no. of bytes in state */
Var v; /* LONG or SHORT */
} hashState_groestl;
HashReturn_gr init_groestl( hashState_groestl* );
//HashReturn_gr init_groestl( hashState_groestl* );
HashReturn_gr init_groestl( hashState_groestl*, int );
HashReturn_gr reinit_groestl( hashState_groestl* );
HashReturn_gr update_groestl( hashState_groestl*, const BitSequence_gr*,
DataLength_gr );
HashReturn_gr final_groestl( hashState_groestl*, BitSequence_gr* );
HashReturn_gr hash_groestl( int, const BitSequence_gr*, DataLength_gr,
BitSequence_gr* );
/* NIST API end */
HashReturn_gr update_and_final_groestl( hashState_groestl*,
BitSequence_gr*, const BitSequence_gr*, DataLength_gr );
#endif /* __hash_h */

View File

@@ -6,8 +6,10 @@
* This code is placed in the public domain
*/
#include <memory.h>
#include "hash-groestl256.h"
#include "miner.h"
#include "avxdefs.h"
#ifndef NO_AES_NI
@@ -71,16 +73,11 @@ void OutputTransformation256(hashState_groestl256 *ctx) {
}
/* initialise context */
HashReturn_gr init_groestl256(hashState_groestl256* ctx) {
HashReturn_gr init_groestl256( hashState_groestl256* ctx, int hashlen )
{
u8 i = 0;
/* output size (in bits) must be a positive integer less than or
equal to 512, and divisible by 8 */
/* set number of state columns and state size depending on
variant */
ctx->columns = COLS;
ctx->statesize = SIZE;
ctx->v = SHoRT;
ctx->hashlen = hashlen;
SET_CONSTANTS();
@@ -93,7 +90,7 @@ HashReturn_gr init_groestl256(hashState_groestl256* ctx) {
return FAIL_GR;
/* set initial value */
ctx->chaining[ctx->columns-1] = U64BIG((u64)256);
ctx->chaining[COLS-1] = U64BIG((u64)256);
INIT256(ctx->chaining);
@@ -104,7 +101,6 @@ HashReturn_gr init_groestl256(hashState_groestl256* ctx) {
return SUCCESS_GR;
}
HashReturn_gr reinit_groestl256(hashState_groestl256* ctx)
{
int i;
@@ -117,7 +113,7 @@ HashReturn_gr reinit_groestl256(hashState_groestl256* ctx)
return FAIL_GR;
/* set initial value */
ctx->chaining[ctx->columns-1] = 256;
ctx->chaining[COLS-1] = 256;
INIT256(ctx->chaining);
@@ -128,83 +124,124 @@ HashReturn_gr reinit_groestl256(hashState_groestl256* ctx)
return SUCCESS_GR;
}
/* update state with databitlen bits of input */
HashReturn_gr update_groestl256( hashState_groestl256* ctx,
const BitSequence_gr* input,
DataLength_gr databitlen )
const BitSequence_gr* input, DataLength_gr databitlen )
{
int index = 0;
int msglen = (int)(databitlen/8);
int rem = (int)(databitlen%8);
const int msglen = (int)(databitlen/8); // bytes
int i;
/* digest bulk of message */
Transform256( ctx, input+index, msglen-index );
index += ((msglen-index)/ctx->statesize)*ctx->statesize;
Transform256( ctx, input, msglen );
/* store remaining data in buffer */
while (index < msglen)
ctx->buffer[(int)ctx->buf_ptr++] = input[index++];
i = ( msglen / SIZE ) * SIZE;
while ( i < msglen )
ctx->buffer[(int)ctx->buf_ptr++] = input[i++];
return SUCCESS_GR;
}
/* finalise: process remaining data (including padding), perform
output transformation, and write hash result to 'output' */
HashReturn_gr final_groestl256( hashState_groestl256* ctx,
BitSequence_gr* output )
BitSequence_gr* output )
{
int i, j = 0, hashbytelen = 256/8;
u8 *s = (BitSequence_gr*)ctx->chaining;
ctx->buffer[(int)ctx->buf_ptr++] = 0x80;
/* pad with '0'-bits */
if ( ctx->buf_ptr > ctx->statesize-LENGTHFIELDLEN )
if ( ctx->buf_ptr > SIZE - LENGTHFIELDLEN )
{
/* padding requires two blocks */
while ( ctx->buf_ptr < ctx->statesize )
while ( ctx->buf_ptr < SIZE )
ctx->buffer[(int)ctx->buf_ptr++] = 0;
/* digest first padding block */
Transform256( ctx, ctx->buffer, ctx->statesize );
Transform256( ctx, ctx->buffer, SIZE );
ctx->buf_ptr = 0;
}
while ( ctx->buf_ptr < ctx->statesize-LENGTHFIELDLEN )
while ( ctx->buf_ptr < SIZE - LENGTHFIELDLEN )
ctx->buffer[(int)ctx->buf_ptr++] = 0;
/* length padding */
ctx->block_counter++;
ctx->buf_ptr = ctx->statesize;
while ( ctx->buf_ptr > ctx->statesize-LENGTHFIELDLEN )
ctx->buf_ptr = SIZE;
while ( ctx->buf_ptr > SIZE - LENGTHFIELDLEN )
{
ctx->buffer[(int)--ctx->buf_ptr] = (u8)ctx->block_counter;
ctx->block_counter >>= 8;
}
/* digest final padding block */
Transform256(ctx, ctx->buffer, ctx->statesize);
Transform256( ctx, ctx->buffer, SIZE );
/* perform output transformation */
OutputTransformation256(ctx);
OutputTransformation256( ctx );
/* store hash result in output */
for ( i = ctx->statesize-hashbytelen; i < ctx->statesize; i++,j++ )
output[j] = s[i];
for ( int i = ( (SIZE - ctx->hashlen) / 16 ), j = 0; i < SIZE/16; i++, j++ )
casti_m128i( output, j ) = casti_m128i( ctx->chaining, i );
return SUCCESS_GR;
}
HashReturn_gr update_and_final_groestl256( hashState_groestl256* ctx,
BitSequence_gr* output, const BitSequence_gr* input,
DataLength_gr databitlen )
{
const int msglen = (int)(databitlen/8); // bytes
int i, j;
/* digest bulk of message */
Transform256( ctx, input, msglen );
/* store remaining data in buffer */
i = ( msglen / SIZE ) * SIZE;
while ( i < msglen )
ctx->buffer[(int)ctx->buf_ptr++] = input[i++];
// start of final
ctx->buffer[(int)ctx->buf_ptr++] = 0x80;
/* pad with '0'-bits */
if ( ctx->buf_ptr > SIZE - LENGTHFIELDLEN )
{
/* padding requires two blocks */
while ( ctx->buf_ptr < SIZE )
ctx->buffer[(int)ctx->buf_ptr++] = 0;
/* digest first padding block */
Transform256( ctx, ctx->buffer, SIZE );
ctx->buf_ptr = 0;
}
while ( ctx->buf_ptr < SIZE - LENGTHFIELDLEN )
ctx->buffer[(int)ctx->buf_ptr++] = 0;
/* length padding */
ctx->block_counter++;
ctx->buf_ptr = SIZE;
while ( ctx->buf_ptr > SIZE - LENGTHFIELDLEN )
{
ctx->buffer[(int)--ctx->buf_ptr] = (u8)ctx->block_counter;
ctx->block_counter >>= 8;
}
/* digest final padding block */
Transform256( ctx, ctx->buffer, SIZE );
/* perform output transformation */
OutputTransformation256( ctx );
/* store hash result in output */
for ( i = ( (SIZE - ctx->hashlen) / 16 ), j = 0; i < SIZE/16; i++, j++ )
casti_m128i( output, j ) = casti_m128i( ctx->chaining, i );
return SUCCESS_GR;
}
/* hash bit sequence */
HashReturn_gr hash_groestl256(int hashbitlen,
const BitSequence_gr* data,
DataLength_gr databitlen,
BitSequence_gr* hashval) {
const BitSequence_gr* data,
DataLength_gr databitlen,
BitSequence_gr* hashval) {
HashReturn_gr ret;
hashState_groestl256 context;
/* initialise */
if ((ret = init_groestl256(&context)) != SUCCESS_GR)
if ((ret = init_groestl256(&context, hashbitlen/8)) != SUCCESS_GR)
return ret;
/* process message */

View File

@@ -80,31 +80,37 @@ typedef crypto_uint64 u64;
(ROTL64(a,56) & li_64(FF000000FF000000)))
#endif /* IS_LITTLE_ENDIAN */
typedef enum { LoNG, SHoRT } Var;
/* NIST API begin */
typedef unsigned char BitSequence_gr;
typedef unsigned long long DataLength_gr;
typedef enum { SUCCESS_GR = 0, FAIL_GR = 1, BAD_HASHBITLEN_GR = 2} HashReturn_gr;
typedef enum
{
SUCCESS_GR = 0,
FAIL_GR = 1,
BAD_HASHBITLEN_GR = 2
} HashReturn_gr;
typedef struct {
__attribute__ ((aligned (32))) u64 chaining[SIZE/8]; /* actual state */
__attribute__ ((aligned (32))) BitSequence_gr buffer[SIZE]; /* data buffer */
u64 block_counter; /* message block counter */
int hashlen; // bytes
int buf_ptr; /* data buffer pointer */
int columns; /* no. of columns in state */
int statesize; /* total no. of bytes in state */
Var v; /* LONG or SHORT */
} hashState_groestl256;
HashReturn_gr init_groestl( hashState_groestl256* );
HashReturn_gr init_groestl256( hashState_groestl256*, int );
HashReturn_gr reinit_groestl( hashState_groestl256* );
HashReturn_gr update_groestl( hashState_groestl256*, const BitSequence_gr*,
DataLength_gr );
HashReturn_gr final_groestl( hashState_groestl256*, BitSequence_gr* );
HashReturn_gr hash_groestl( int, const BitSequence_gr*, DataLength_gr,
BitSequence_gr* );
/* NIST API end */
HashReturn_gr update_and_final_groestl256( hashState_groestl256*,
BitSequence_gr*, const BitSequence_gr*,
DataLength_gr );
#endif /* __hash_h */