Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 476 lines (424 sloc) 15.416 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
/* Groestl-opt.c January 2011
* ANSI C code optimised for 64-bit machines
* Authors: Soeren S. Thomsen
* Krystian Matusiewicz
*
* This code is placed in the public domain
*/

#include "Groestl-opt.h"
#include "tables.h"
#include <string.h>


void PrintState(u64 y[COLS512]) {
  int i;
  for (i = 0; i < COLS512; i++) printf("%016llx\n", U64BIG(y[i]));
  printf("\n");
}

/* compute one new state column */
#define COLUMN(x,y,i,c0,c1,c2,c3,c4,c5,c6,c7) \
y[i] = T[0*256+EXT_BYTE(x[c0],0)] \
^ T[1*256+EXT_BYTE(x[c1],1)] \
^ T[2*256+EXT_BYTE(x[c2],2)] \
^ T[3*256+EXT_BYTE(x[c3],3)] \
^ T[4*256+EXT_BYTE(x[c4],4)] \
^ T[5*256+EXT_BYTE(x[c5],5)] \
^ T[6*256+EXT_BYTE(x[c6],6)] \
^ T[7*256+EXT_BYTE(x[c7],7)]

/* compute a round in P (short variants) */
#define RND512P(x,y,r) do { \
x[0] ^= U64BIG(0x0000000000000000ull)^r; \
x[1] ^= U64BIG(0x1000000000000000ull)^r; \
x[2] ^= U64BIG(0x2000000000000000ull)^r; \
x[3] ^= U64BIG(0x3000000000000000ull)^r; \
x[4] ^= U64BIG(0x4000000000000000ull)^r; \
x[5] ^= U64BIG(0x5000000000000000ull)^r; \
x[6] ^= U64BIG(0x6000000000000000ull)^r; \
x[7] ^= U64BIG(0x7000000000000000ull)^r; \
COLUMN(x,y,0,0,1,2,3,4,5,6,7); \
COLUMN(x,y,1,1,2,3,4,5,6,7,0); \
COLUMN(x,y,2,2,3,4,5,6,7,0,1); \
COLUMN(x,y,3,3,4,5,6,7,0,1,2); \
COLUMN(x,y,4,4,5,6,7,0,1,2,3); \
COLUMN(x,y,5,5,6,7,0,1,2,3,4); \
COLUMN(x,y,6,6,7,0,1,2,3,4,5); \
COLUMN(x,y,7,7,0,1,2,3,4,5,6); \
} while (0)

/* compute a round in Q (short variants) */
#define RND512Q(x,y,r) do { \
x[0] ^= U64BIG(0xffffffffffffffffull)^r; \
x[1] ^= U64BIG(0xffffffffffffffefull)^r; \
x[2] ^= U64BIG(0xffffffffffffffdfull)^r; \
x[3] ^= U64BIG(0xffffffffffffffcfull)^r; \
x[4] ^= U64BIG(0xffffffffffffffbfull)^r; \
x[5] ^= U64BIG(0xffffffffffffffafull)^r; \
x[6] ^= U64BIG(0xffffffffffffff9full)^r; \
x[7] ^= U64BIG(0xffffffffffffff8full)^r; \
COLUMN(x,y,0,1,3,5,7,0,2,4,6); \
COLUMN(x,y,1,2,4,6,0,1,3,5,7); \
COLUMN(x,y,2,3,5,7,1,2,4,6,0); \
COLUMN(x,y,3,4,6,0,2,3,5,7,1); \
COLUMN(x,y,4,5,7,1,3,4,6,0,2); \
COLUMN(x,y,5,6,0,2,4,5,7,1,3); \
COLUMN(x,y,6,7,1,3,5,6,0,2,4); \
COLUMN(x,y,7,0,2,4,6,7,1,3,5); \
} while (0)


/* compute a round in P (long variants) */
#define RND1024P(x,y,r) do { \
x[ 0] ^= U64BIG(0x0000000000000000ull)^r; \
x[ 1] ^= U64BIG(0x1000000000000000ull)^r; \
x[ 2] ^= U64BIG(0x2000000000000000ull)^r; \
x[ 3] ^= U64BIG(0x3000000000000000ull)^r; \
x[ 4] ^= U64BIG(0x4000000000000000ull)^r; \
x[ 5] ^= U64BIG(0x5000000000000000ull)^r; \
x[ 6] ^= U64BIG(0x6000000000000000ull)^r; \
x[ 7] ^= U64BIG(0x7000000000000000ull)^r; \
x[ 8] ^= U64BIG(0x8000000000000000ull)^r; \
x[ 9] ^= U64BIG(0x9000000000000000ull)^r; \
x[10] ^= U64BIG(0xa000000000000000ull)^r; \
x[11] ^= U64BIG(0xb000000000000000ull)^r; \
x[12] ^= U64BIG(0xc000000000000000ull)^r; \
x[13] ^= U64BIG(0xd000000000000000ull)^r; \
x[14] ^= U64BIG(0xe000000000000000ull)^r; \
x[15] ^= U64BIG(0xf000000000000000ull)^r; \
COLUMN(x,y,15,15, 0, 1, 2, 3, 4, 5,10); \
COLUMN(x,y,14,14,15, 0, 1, 2, 3, 4, 9); \
COLUMN(x,y,13,13,14,15, 0, 1, 2, 3, 8); \
COLUMN(x,y,12,12,13,14,15, 0, 1, 2, 7); \
COLUMN(x,y,11,11,12,13,14,15, 0, 1, 6); \
COLUMN(x,y,10,10,11,12,13,14,15, 0, 5); \
COLUMN(x,y, 9, 9,10,11,12,13,14,15, 4); \
COLUMN(x,y, 8, 8, 9,10,11,12,13,14, 3); \
COLUMN(x,y, 7, 7, 8, 9,10,11,12,13, 2); \
COLUMN(x,y, 6, 6, 7, 8, 9,10,11,12, 1); \
COLUMN(x,y, 5, 5, 6, 7, 8, 9,10,11, 0); \
COLUMN(x,y, 4, 4, 5, 6, 7, 8, 9,10,15); \
COLUMN(x,y, 3, 3, 4, 5, 6, 7, 8, 9,14); \
COLUMN(x,y, 2, 2, 3, 4, 5, 6, 7, 8,13); \
COLUMN(x,y, 1, 1, 2, 3, 4, 5, 6, 7,12); \
COLUMN(x,y, 0, 0, 1, 2, 3, 4, 5, 6,11); \
} while (0)

/* compute a round in Q (long variants) */
#define RND1024Q(x,y,r) do { \
x[ 0] ^= U64BIG(0xffffffffffffffffull)^r; \
x[ 1] ^= U64BIG(0xffffffffffffffefull)^r; \
x[ 2] ^= U64BIG(0xffffffffffffffdfull)^r; \
x[ 3] ^= U64BIG(0xffffffffffffffcfull)^r; \
x[ 4] ^= U64BIG(0xffffffffffffffbfull)^r; \
x[ 5] ^= U64BIG(0xffffffffffffffafull)^r; \
x[ 6] ^= U64BIG(0xffffffffffffff9full)^r; \
x[ 7] ^= U64BIG(0xffffffffffffff8full)^r; \
x[ 8] ^= U64BIG(0xffffffffffffff7full)^r; \
x[ 9] ^= U64BIG(0xffffffffffffff6full)^r; \
x[10] ^= U64BIG(0xffffffffffffff5full)^r; \
x[11] ^= U64BIG(0xffffffffffffff4full)^r; \
x[12] ^= U64BIG(0xffffffffffffff3full)^r; \
x[13] ^= U64BIG(0xffffffffffffff2full)^r; \
x[14] ^= U64BIG(0xffffffffffffff1full)^r; \
x[15] ^= U64BIG(0xffffffffffffff0full)^r; \
COLUMN(x,y,15, 0, 2, 4,10,15, 1, 3, 5); \
COLUMN(x,y,14,15, 1, 3, 9,14, 0, 2, 4); \
COLUMN(x,y,13,14, 0, 2, 8,13,15, 1, 3); \
COLUMN(x,y,12,13,15, 1, 7,12,14, 0, 2); \
COLUMN(x,y,11,12,14, 0, 6,11,13,15, 1); \
COLUMN(x,y,10,11,13,15, 5,10,12,14, 0); \
COLUMN(x,y, 9,10,12,14, 4, 9,11,13,15); \
COLUMN(x,y, 8, 9,11,13, 3, 8,10,12,14); \
COLUMN(x,y, 7, 8,10,12, 2, 7, 9,11,13); \
COLUMN(x,y, 6, 7, 9,11, 1, 6, 8,10,12); \
COLUMN(x,y, 5, 6, 8,10, 0, 5, 7, 9,11); \
COLUMN(x,y, 4, 5, 7, 9,15, 4, 6, 8,10); \
COLUMN(x,y, 3, 4, 6, 8,14, 3, 5, 7, 9); \
COLUMN(x,y, 2, 3, 5, 7,13, 2, 4, 6, 8); \
COLUMN(x,y, 1, 2, 4, 6,12, 1, 3, 5, 7); \
COLUMN(x,y, 0, 1, 3, 5,11, 0, 2, 4, 6); \
} while (0)

/* the compression function (short variants) */
inline void F512(u64 *h, const u64 *m) {
  u64 y[COLS512] __attribute__((aligned(16)));
  u64 z[COLS512] __attribute__((aligned(16)));
  u64 outQ[COLS512] __attribute__((aligned(16)));
  u64 inP[COLS512] __attribute__((aligned(16)));
  int i;

  for (i = 0; i < COLS512; i++) {
    z[i] = m[i];
    inP[i] = h[i] ^ m[i];
  }

  /* compute Q(m) */
  RND512Q(z,y,U64BIG(0x0000000000000000ull));
  RND512Q(y,z,U64BIG(0x0000000000000001ull));
  RND512Q(z,y,U64BIG(0x0000000000000002ull));
  RND512Q(y,z,U64BIG(0x0000000000000003ull));
  RND512Q(z,y,U64BIG(0x0000000000000004ull));
  RND512Q(y,z,U64BIG(0x0000000000000005ull));
  RND512Q(z,y,U64BIG(0x0000000000000006ull));
  RND512Q(y,z,U64BIG(0x0000000000000007ull));
  RND512Q(z,y,U64BIG(0x0000000000000008ull));
  RND512Q(y,outQ,U64BIG(0x0000000000000009ull));

  /* compute P(h+m) */
  RND512P(inP,z,U64BIG(0x0000000000000000ull));
  RND512P(z,y, U64BIG(0x0100000000000000ull));
  RND512P(y,z, U64BIG(0x0200000000000000ull));
  RND512P(z,y, U64BIG(0x0300000000000000ull));
  RND512P(y,z, U64BIG(0x0400000000000000ull));
  RND512P(z,y, U64BIG(0x0500000000000000ull));
  RND512P(y,z, U64BIG(0x0600000000000000ull));
  RND512P(z,y, U64BIG(0x0700000000000000ull));
  RND512P(y,z, U64BIG(0x0800000000000000ull));
  RND512P(z,y, U64BIG(0x0900000000000000ull));

  /* h' == h + Q(m) + P(h+m) */
#pragma vector aligned
  for (i = 0; i < COLS512; i++) {
    h[i] ^= outQ[i] ^ y[i];
  }
}


/* the compression function (long variants) */
inline void F1024(u64 *h, const u64 *m) {
  static u64 y[COLS1024] __attribute__((aligned(16)));
  static u64 z[COLS1024] __attribute__((aligned(16)));
  static u64 outQ[COLS1024] __attribute__((aligned(16)));
  static u64 inP[COLS1024] __attribute__((aligned(16)));
  int i;

  for (i = 0; i < COLS1024; i++) {
    z[i] = m[i];
    inP[i] = h[i] ^ m[i];
  }

  /* compute Q(m) */
  RND1024Q(z,y,0);
  for (i = 1; i < ROUNDS1024-1; i += 2) {
    RND1024Q(y,z,U64BIG((u64)i));
    RND1024Q(z,y,U64BIG((u64)(i+1)));
  }
  RND1024Q(y,outQ,U64BIG((u64)(ROUNDS1024-1)));

  /* compute P(h+m) */
  RND1024P(inP,z,0);
  for (i = 1; i < ROUNDS1024-1; i += 2) {
    RND1024P(z,y,U64BIG(((u64)i)<<56));
    RND1024P(y,z,U64BIG(((u64)(i+1))<<56));
  }
  RND1024P(z,y,U64BIG(((u64)(ROUNDS1024-1))<<56));

  /* h' == h + Q(m) + P(h+m) */
#pragma vector aligned
  for (i = 0; i < COLS1024; i++) {
    h[i] ^= outQ[i] ^ y[i];
  }
}


/* digest up to msglen bytes of input (full blocks only) */
void Transform(hashState *ctx,
const u8 *input,
int msglen) {

  /* determine variant, SHORT or LONG, and select underlying
compression function based on the variant */
  if (ctx->size == SHORT) {
    /* increment block counter */
    ctx->block_counter += msglen/SIZE512;
    while (msglen >= SIZE512) {
      F512(ctx->chaining,(u64*)input);
      msglen -= SIZE512;
      input += SIZE512;
    }
  }
  else {
    /* increment block counter */
    ctx->block_counter += msglen/SIZE1024;
    while (msglen >= SIZE1024) {
      F1024(ctx->chaining,(u64*)input);
      msglen -= SIZE1024;
      input += SIZE1024;
    }
  }

}

/* given state h, do h <- P(h)+h */
void OutputTransformation(hashState *ctx) {
  int j;
  u64 temp[COLS1024];
  u64 y[COLS1024];
  u64 z[COLS1024];

  /* determine variant */
  switch (ctx->size) {
  case SHORT :
    for (j = 0; j < COLS512; j++) {
      temp[j] = ctx->chaining[j];
    }
    RND512P(temp,z,U64BIG(0x0000000000000000ull));
    RND512P(z,y,U64BIG(0x0100000000000000ull));
    RND512P(y,z,U64BIG(0x0200000000000000ull));
    RND512P(z,y,U64BIG(0x0300000000000000ull));
    RND512P(y,z,U64BIG(0x0400000000000000ull));
    RND512P(z,y,U64BIG(0x0500000000000000ull));
    RND512P(y,z,U64BIG(0x0600000000000000ull));
    RND512P(z,y,U64BIG(0x0700000000000000ull));
    RND512P(y,z,U64BIG(0x0800000000000000ull));
    RND512P(z,temp,U64BIG(0x0900000000000000ull));
    for (j = 0; j < COLS512; j++) {
      ctx->chaining[j] ^= temp[j];
    }
    break;
  case LONG :
    for (j = 0; j < COLS1024; j++) {
      temp[j] = ctx->chaining[j];
    }
    RND1024P(temp,y,0);
    for (j = 1; j < ROUNDS1024-1; j += 2) {
      RND1024P(y,z,U64BIG(((u64)j)<<56));
      RND1024P(z,y,U64BIG(((u64)j+1)<<56));
    }
    RND1024P(y,temp,U64BIG(((u64)(ROUNDS1024-1))<<56));
    for (j = 0; j < COLS1024; j++) {
      ctx->chaining[j] ^= temp[j];
    }
    break;
  }
}

/* initialise context */
HashReturn Init(hashState* ctx,
int hashbitlen) {
  /* output size (in bits) must be a positive integer less than or
equal to 512, and divisible by 8 */
  if (hashbitlen <= 0 || (hashbitlen%8) || hashbitlen > 512)
    return BAD_HASHLEN;

  /* set number of state columns and state size depending on
variant */
  if (hashbitlen <= 256) {
    ctx->size = SHORT;
    ctx->chaining = calloc(COLS512,sizeof(u64));
    ctx->buffer = malloc(SIZE512);
    /* set initial value */
    ctx->chaining[COLS512-1] = U64BIG((u64)hashbitlen);
  }
  else {
    ctx->size = LONG;
    ctx->chaining = calloc(COLS1024,sizeof(u64));
    ctx->buffer = malloc(SIZE1024);
    /* set initial value */
    ctx->chaining[COLS1024-1] = U64BIG((u64)hashbitlen);
  }

  /* set other variables */
  ctx->hashbitlen = hashbitlen;
  ctx->buf_ptr = 0;
  ctx->block_counter = 0;
  ctx->bits_in_last_byte = 0;

  return HASH_SUCCESS;
}

/* update state with databitlen bits of input */
HashReturn Update(hashState* ctx,
const BitSequence* input,
DataLength databitlen) {
  int index = 0;
  int msglen = (int)(databitlen/8);
  int rem = (int)(databitlen%8);

  /* non-integral number of message bytes can only be supplied in the
last call to this function */
  if (ctx->bits_in_last_byte) return HASH_FAIL;

  /* if the buffer contains data that has not yet been digested, first
add data to buffer until full */
  if (ctx->buf_ptr) {
    while (ctx->buf_ptr < ctx->size && index < msglen) {
      ctx->buffer[(int)ctx->buf_ptr++] = input[index++];
    }
    if (ctx->buf_ptr < ctx->size) {
      /* buffer still not full, return */
      if (rem) {
ctx->bits_in_last_byte = rem;
ctx->buffer[(int)ctx->buf_ptr++] = input[index];
      }
      return HASH_SUCCESS;
    }

    /* digest buffer */
    ctx->buf_ptr = 0;
    Transform(ctx, ctx->buffer, ctx->size);
  }

  /* digest bulk of message */
  Transform(ctx, input+index, msglen-index);
  index += ((msglen-index)/ctx->size)*ctx->size;

  /* store remaining data in buffer */
  while (index < msglen) {
    ctx->buffer[(int)ctx->buf_ptr++] = input[index++];
  }

  /* if non-integral number of bytes have been supplied, store
remaining bits in last byte, together with information about
number of bits */
  if (rem) {
    ctx->bits_in_last_byte = rem;
    ctx->buffer[(int)ctx->buf_ptr++] = input[index];
  }
  return HASH_SUCCESS;
}

#define BILB ctx->bits_in_last_byte

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

  /* length padding */
  ctx->block_counter++;
  ctx->buf_ptr = ctx->size;
  while (ctx->buf_ptr > ctx->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->size);
  /* perform output transformation */
  OutputTransformation(ctx);

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

/* hash bit sequence */
HashReturn Hash(int hashbitlen,
const BitSequence* data,
DataLength databitlen,
BitSequence* hashval) {
  HashReturn ret;
  hashState context;

  /* initialise */
  if ((ret = Init(&context, hashbitlen)) != HASH_SUCCESS)
    return ret;

  /* process message */
  if ((ret = Update(&context, data, databitlen)) != HASH_SUCCESS)
    return ret;

  /* finalise */
  ret = Final(&context, hashval);

  return ret;
}

void PrintHash(const BitSequence* hash,
int hashbitlen) {
  int i;
  for (i = 0; i < hashbitlen/8; i++) {
    printf("%02x", hash[i]);
  }
  printf("\n");
}
Something went wrong with that request. Please try again.