示例#1
0
/* finalise: process remaining data (including padding), perform
   output transformation, and write hash result to 'output' */
HashReturn Final(hashState* ctx,
		 BitSequence* output) {
  int i, j = 0, hashbytelen = LENGTH/8;
  u8 *s = (BitSequence*)ctx->chaining;

  /* pad with '1'-bit and first few '0'-bits */
  if (BILB) {
    ctx->buffer[(int)ctx->buf_ptr-1] &= ((1<<BILB)-1)<<(8-BILB);
    ctx->buffer[(int)ctx->buf_ptr-1] ^= 0x1<<(7-BILB);
    BILB = 0;
  }
  else ctx->buffer[(int)ctx->buf_ptr++] = 0x80;

  /* pad with '0'-bits */
  if (ctx->buf_ptr > ctx->statesize-LENGTHFIELDLEN) {
    /* padding requires two blocks */
    while (ctx->buf_ptr < ctx->statesize) {
      ctx->buffer[(int)ctx->buf_ptr++] = 0;
    }
    /* digest first padding block */
    Transform(ctx, ctx->buffer, ctx->statesize);
    ctx->buf_ptr = 0;
  }
  while (ctx->buf_ptr < ctx->statesize-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->buffer[(int)--ctx->buf_ptr] = (u8)ctx->block_counter;
    ctx->block_counter >>= 8;
  }

  /* digest final padding block */
  Transform(ctx, ctx->buffer, ctx->statesize);
  /* perform output transformation */
  OutputTransformation(ctx);

  /* store hash result in output */
  for (i = ctx->statesize-hashbytelen; i < ctx->statesize; i++,j++) {
    output[j] = s[i];
  }

  /* zeroise relevant variables and deallocate memory */
  
  for (i = 0; i < ctx->columns; i++) {
    ctx->chaining[i] = 0;
  }
  
  for (i = 0; i < ctx->statesize; i++) {
    ctx->buffer[i] = 0;
  }
//  free(ctx->chaining);
//  free(ctx->buffer);

  return SUCCESS;
}
示例#2
0
/* finalise: process remaining data (including padding), perform
   output transformation, and write hash result to 'output' */
static void Final(hashState* ctx,
		 BitSequence* output) {
  int i, j = 0, hashbytelen = HASH_BIT_LEN/8;
  uint8_t *s = (BitSequence*)ctx->chaining;

  /* pad with '1'-bit and first few '0'-bits */
  if (BILB) {
    ctx->buffer[(int)ctx->buf_ptr-1] &= ((1<<BILB)-1)<<(8-BILB);
    ctx->buffer[(int)ctx->buf_ptr-1] ^= 0x1<<(7-BILB);
    BILB = 0;
  }
  else ctx->buffer[(int)ctx->buf_ptr++] = 0x80;

  /* pad with '0'-bits */
  if (ctx->buf_ptr > SIZE512-LENGTHFIELDLEN) {
    /* padding requires two blocks */
    while (ctx->buf_ptr < SIZE512) {
      ctx->buffer[(int)ctx->buf_ptr++] = 0;
    }
    /* digest first padding block */
    Transform(ctx, ctx->buffer, SIZE512);
    ctx->buf_ptr = 0;
  }
  while (ctx->buf_ptr < SIZE512-LENGTHFIELDLEN) {
    ctx->buffer[(int)ctx->buf_ptr++] = 0;
  }

  /* length padding */
  ctx->block_counter1++;
  if (ctx->block_counter1 == 0) ctx->block_counter2++;
  ctx->buf_ptr = SIZE512;

  while (ctx->buf_ptr > SIZE512-(int)sizeof(uint32_t)) {
    ctx->buffer[(int)--ctx->buf_ptr] = (uint8_t)ctx->block_counter1;
    ctx->block_counter1 >>= 8;
  }
  while (ctx->buf_ptr > SIZE512-LENGTHFIELDLEN) {
    ctx->buffer[(int)--ctx->buf_ptr] = (uint8_t)ctx->block_counter2;
    ctx->block_counter2 >>= 8;
  }
  /* digest final padding block */
  Transform(ctx, ctx->buffer, SIZE512); 
  /* perform output transformation */
  OutputTransformation(ctx);

  /* store hash result in output */
  for (i = SIZE512-hashbytelen; i < SIZE512; i++,j++) {
    output[j] = s[i];
  }

  /* zeroise relevant variables and deallocate memory */
  for (i = 0; i < COLS512; i++) {
    ctx->chaining[i] = 0;
  }
  for (i = 0; i < SIZE512; i++) {
    ctx->buffer[i] = 0;
  }
}
示例#3
0
HashReturn Final(hashState* ctx,
		 BitSequence* output) {
  int i, j, hashbytelen = ctx->hashbitlen/8;

  /* 100... padding */
  if (ctx->bits_in_last_byte) {
    ctx->buffer[ctx->buf_ptr-1] &= ((1<<BILB)-1)<<(8-BILB);
    ctx->buffer[ctx->buf_ptr-1] ^= 0x1<<(7-BILB);
  }
  else ctx->buffer[ctx->buf_ptr++] = 0x80;

  if (ctx->buf_ptr > ctx->statesize-LENGTHFIELDLEN) {
    /* padding requires two blocks */
    while (ctx->buf_ptr < ctx->statesize) {
      ctx->buffer[ctx->buf_ptr++] = 0;
    }
    Transform(ctx, ctx->buffer, ctx->statesize);
    ctx->buf_ptr = 0;
  }
  while (ctx->buf_ptr < ctx->statesize-LENGTHFIELDLEN) {
    ctx->buffer[ctx->buf_ptr++] = 0;
  }

  /* length padding */
  ctx->block_counter++;
  ctx->buf_ptr = ctx->statesize;
  while (ctx->buf_ptr > ctx->statesize-LENGTHFIELDLEN) {
    ctx->buffer[--ctx->buf_ptr] = (u8)ctx->block_counter;
    ctx->block_counter >>= 8;
  }

  /* digest (last) padding block */
  Transform(ctx, ctx->buffer, ctx->statesize);
  /* output transformation */
  OutputTransformation(ctx);

  /* store hash output */
  j = 0;
  for (i = ctx->statesize-hashbytelen; i < ctx->statesize; i++,j++) {
    output[j] = ctx->chaining[i%ROWS][i/ROWS];
  }

  /* zeroise */
  for (i = 0; i < ROWS; i++) {
    for (j = 0; j < ctx->columns; j++) {
      ctx->chaining[i][j] = 0;
    }
  }
  for (i = 0; i < ctx->statesize; i++) {
    ctx->buffer[i] = 0;
  }

  return SUCCESS;
}
示例#4
0
/* finalise: process remaining data (including padding), perform
   output transformation, and write hash result to 'output' */
int Final(context* ctx,
	  unsigned char* out) {
  int i, j = 0;
  unsigned char *s = (unsigned char*)ctx->state;

  ctx->buffer[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[ctx->buf_ptr++] = 0;
    }
    /* digest first padding block */
    Transform(ctx, ctx->buffer, SIZE);
    ctx->buf_ptr = 0;
  }
  while (ctx->buf_ptr < SIZE-LENGTHFIELDLEN) {
    ctx->buffer[ctx->buf_ptr++] = 0;
  }

  /* length padding */
  ctx->block_counter++;
  ctx->buf_ptr = SIZE;
  while (ctx->buf_ptr > SIZE-LENGTHFIELDLEN) {
    ctx->buffer[--ctx->buf_ptr] = (unsigned char)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-DIGESTSIZE; i < SIZE; i++,j++) {
    out[j] = s[i];
  }

  /* zeroise relevant variables and deallocate memory */
  for (i = 0; i < COLS; i++) {
    ctx->state[i] = 0;
  }
  for (i = 0; i < SIZE; i++) {
    ctx->buffer[i] = 0;
  }

  return 0;
}
示例#5
0
HashReturn Final(hashState* ctx,
		 BitSequence* output) {

  int i, j = 0, hashbytelen = ctx->hashbitlen/8;
  u8 *s = (BitSequence*)ctx->chaining;

u64 kbyts=0,kbits;	//ADDED

  /* pad with '1'-bit and first few '0'-bits */
  if (BILB) {
    ctx->buffer[(int)ctx->buf_ptr-1] &= ((1<<BILB)-1)<<(8-BILB);
    ctx->buffer[(int)ctx->buf_ptr-1] ^= 0x1<<(7-BILB);
    BILB = 0;
  }
  else ctx->buffer[(int)ctx->buf_ptr++] = 0x80;

  /* pad with '0'-bits */
//modified :1 byte for r value & 8 bytes for length

  if (ctx->buf_ptr > ctx->blocksize-LENGTHFIELDLEN-1) { ////Modified 1 byte for r value & 8 bytes for length

    /* padding requires two blocks */
    while (ctx->buf_ptr < ctx->blocksize) {
      ctx->buffer[(int)ctx->buf_ptr++] = 0;
	kbyts++; 		//ADDED: no of zeros appended
    }

    /* digest first padding block */
    Transform(ctx, ctx->buffer, ctx->blocksize);	//Modified
    ctx->buf_ptr = 0;
  }

  while (ctx->buf_ptr < ctx->blocksize-LENGTHFIELDLEN-1) {	//Modified
    ctx->buffer[(int)ctx->buf_ptr++] = 0;
	kbyts++; 		//no of zeros appended
  }

//ADDSED bY GURPREET-- Feb14,2012

//for k zeros
  while (ctx->buf_ptr < ctx->blocksize-LENGTHFIELDLEN-1) {
    ctx->buffer[(int)ctx->buf_ptr++] = 0;
	kbyts++; 		//no of zeros appended
  }

    //ADDED BY gurpreet FOR R-BYTES
	kbits=(kbyts*8)+ctx->bits_in_last_byte;

	kbits+=8;		//7bits as 10000000 & added 1 bit as space is 7bits for rbytes
	int r_bytes;
	// for 7 bit r_bytes padding

	r_bytes=(952-kbits)%1024;
	while (r_bytes<0)
	   {r_bytes+=1024;}		// convert -vr to +ve mod value
	r_bytes/=8;			// convert it into bytes

	//for 7bit r_bytes value
	 while (ctx->buf_ptr >= ctx->blocksize-LENGTHFIELDLEN-1) {
	    ctx->buffer[ctx->buf_ptr--] = (u8)r_bytes;
	    r_bytes >>= 8;
	  } 

    //-----

  /* length padding */
  ctx->block_counter++;
  ctx->buf_ptr = ctx->blocksize;		//modified
//ADDED
   ctx->cnt_block=ctx->block_counter;
//------

/*for padding block counter*/
  while (ctx->buf_ptr > ctx->blocksize-LENGTHFIELDLEN) {	//modified
    ctx->buffer[(int)--ctx->buf_ptr] = (u8)ctx->block_counter;
    ctx->block_counter >>= 8;
  }

  /* digest final padding block */
  Transform(ctx, ctx->buffer, ctx->blocksize);		//modified
  /* perform output transformation */
  OutputTransformation(ctx);

j=0;
  /* store hash result in output */
  for (i = ctx->size-hashbytelen; i < ctx->size; i++,j++) {
    output[j] = s[i];
  }

  /* zeroise relevant variables and deallocate memory */
  if (ctx->size == SHORT) {
    memset(ctx->chaining, 0, COLS512*sizeof(u64));
    memset(ctx->buffer, 0, SIZE512);
  }
  else {
    memset(ctx->chaining, 0, COLS1024*sizeof(u64));
    memset(ctx->buffer, 0, SIZE1024);
  }
  free(ctx->chaining);
  free(ctx->buffer);

  return SUCCESS;
}
示例#6
0
/* finalise: process remaining data (including padding), perform
   output transformation, and write hash result to 'output' */
int Grostl::Final(BitSequence* output) {
  int i, j = 0, hashbytelen = grostlState.hashbitlen/8;
  grostl_u8 *s = (BitSequence*)grostlState.chaining;

  /* pad with '1'-bit and first few '0'-bits */
  if (BILB) {
    grostlState.buffer[(int)grostlState.buf_ptr-1] &= ((1<<BILB)-1)<<(8-BILB);
    grostlState.buffer[(int)grostlState.buf_ptr-1] ^= 0x1<<(7-BILB);
    BILB = 0;
  }
  else grostlState.buffer[(int)grostlState.buf_ptr++] = 0x80;

  /* pad with '0'-bits */
  if (grostlState.buf_ptr > grostlState.statesize-GROSTL_LENGTHFIELDLEN) {
    /* padding requires two blocks */
    while (grostlState.buf_ptr < grostlState.statesize) {
      grostlState.buffer[(int)grostlState.buf_ptr++] = 0;
    }
    /* digest first padding block */
    Transform(&grostlState, grostlState.buffer, grostlState.statesize, grostlNumRounds512, grostlNumRounds1024);
    grostlState.buf_ptr = 0;
  }
  while (grostlState.buf_ptr < grostlState.statesize-GROSTL_LENGTHFIELDLEN) {
    grostlState.buffer[(int)grostlState.buf_ptr++] = 0;
  }

  /* length padding */
  grostlState.block_counter1++;
  if (grostlState.block_counter1 == 0) grostlState.block_counter2++;
  grostlState.buf_ptr = grostlState.statesize;

  while (grostlState.buf_ptr > grostlState.statesize-(int)sizeof(grostl_u32)) {
    grostlState.buffer[(int)--grostlState.buf_ptr] = (grostl_u8)grostlState.block_counter1;
    grostlState.block_counter1 >>= 8;
  }
  while (grostlState.buf_ptr > grostlState.statesize-GROSTL_LENGTHFIELDLEN) {
    grostlState.buffer[(int)--grostlState.buf_ptr] = (grostl_u8)grostlState.block_counter2;
    grostlState.block_counter2 >>= 8;
  }

  /* digest final padding block */
  Transform(&grostlState, grostlState.buffer, grostlState.statesize, grostlNumRounds512, grostlNumRounds1024);
  /* perform output transformation */
  OutputTransformation(&grostlState, grostlNumRounds512, grostlNumRounds1024);

  /* store hash result in output */
  for (i = grostlState.statesize-hashbytelen; i < grostlState.statesize; i++,j++) {
    output[j] = s[i];
  }

  /* zeroise relevant variables and deallocate memory */
  for (i = 0; i < grostlState.columns; i++) {
    grostlState.chaining[i] = 0;
  }
  for (i = 0; i < grostlState.statesize; i++) {
    grostlState.buffer[i] = 0;
  }
  free(grostlState.chaining);
  free(grostlState.buffer);

  return SUCCESS;
}