| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
#include <stddef.h> |
| 12 |
#include "groestl.h" |
| 13 |
#include "groestl_tables.h" |
| 14 |
|
| 15 |
#define P_TYPE 0 |
| 16 |
#define Q_TYPE 1 |
| 17 |
|
| 18 |
const uint8_t shift_Values[2][8] = {{0,1,2,3,4,5,6,7},{1,3,5,7,0,2,4,6}}; |
| 19 |
|
| 20 |
const uint8_t indices_cyclic[15] = {0,1,2,3,4,5,6,7,0,1,2,3,4,5,6}; |
| 21 |
|
| 22 |
|
| 23 |
#if BYTE_ORDER == LITTLE_ENDIAN |
| 24 |
#define ROTATE_COLUMN_DOWN(v1, v2, amount_bytes, temp_var) {temp_var = (v1<<(8*amount_bytes))|(v2>>(8*(4-amount_bytes))); \ |
| 25 |
v2 = (v2<<(8*amount_bytes))|(v1>>(8*(4-amount_bytes))); \ |
| 26 |
v1 = temp_var;} |
| 27 |
#else |
| 28 |
#define ROTATE_COLUMN_DOWN(v1, v2, amount_bytes, temp_var) {temp_var = (v1>>(8*amount_bytes))|(v2<<(8*(4-amount_bytes))); \ |
| 29 |
v2 = (v2>>(8*amount_bytes))|(v1<<(8*(4-amount_bytes))); \ |
| 30 |
v1 = temp_var;} |
| 31 |
#endif |
| 32 |
|
| 33 |
|
| 34 |
#define COLUMN(x,y,i,c0,c1,c2,c3,c4,c5,c6,c7,tv1,tv2,tu,tl,t) \ |
| 35 |
tu = T[2*(uint32_t)x[4*c0+0]]; \ |
| 36 |
tl = T[2*(uint32_t)x[4*c0+0]+1]; \ |
| 37 |
tv1 = T[2*(uint32_t)x[4*c1+1]]; \ |
| 38 |
tv2 = T[2*(uint32_t)x[4*c1+1]+1]; \ |
| 39 |
ROTATE_COLUMN_DOWN(tv1,tv2,1,t) \ |
| 40 |
tu ^= tv1; \ |
| 41 |
tl ^= tv2; \ |
| 42 |
tv1 = T[2*(uint32_t)x[4*c2+2]]; \ |
| 43 |
tv2 = T[2*(uint32_t)x[4*c2+2]+1]; \ |
| 44 |
ROTATE_COLUMN_DOWN(tv1,tv2,2,t) \ |
| 45 |
tu ^= tv1; \ |
| 46 |
tl ^= tv2; \ |
| 47 |
tv1 = T[2*(uint32_t)x[4*c3+3]]; \ |
| 48 |
tv2 = T[2*(uint32_t)x[4*c3+3]+1]; \ |
| 49 |
ROTATE_COLUMN_DOWN(tv1,tv2,3,t) \ |
| 50 |
tu ^= tv1; \ |
| 51 |
tl ^= tv2; \ |
| 52 |
tl ^= T[2*(uint32_t)x[4*c4+0]]; \ |
| 53 |
tu ^= T[2*(uint32_t)x[4*c4+0]+1]; \ |
| 54 |
tv1 = T[2*(uint32_t)x[4*c5+1]]; \ |
| 55 |
tv2 = T[2*(uint32_t)x[4*c5+1]+1]; \ |
| 56 |
ROTATE_COLUMN_DOWN(tv1,tv2,1,t) \ |
| 57 |
tl ^= tv1; \ |
| 58 |
tu ^= tv2; \ |
| 59 |
tv1 = T[2*(uint32_t)x[4*c6+2]]; \ |
| 60 |
tv2 = T[2*(uint32_t)x[4*c6+2]+1]; \ |
| 61 |
ROTATE_COLUMN_DOWN(tv1,tv2,2,t) \ |
| 62 |
tl ^= tv1; \ |
| 63 |
tu ^= tv2; \ |
| 64 |
tv1 = T[2*(uint32_t)x[4*c7+3]]; \ |
| 65 |
tv2 = T[2*(uint32_t)x[4*c7+3]+1]; \ |
| 66 |
ROTATE_COLUMN_DOWN(tv1,tv2,3,t) \ |
| 67 |
tl ^= tv1; \ |
| 68 |
tu ^= tv2; \ |
| 69 |
y[i] = tu; \ |
| 70 |
y[i+1] = tl; |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
static void RND512P(uint8_t *x, uint32_t *y, uint32_t r) { |
| 75 |
uint32_t temp_v1, temp_v2, temp_upper_value, temp_lower_value, temp; |
| 76 |
uint32_t* x32 = (uint32_t*)x; |
| 77 |
x32[ 0] ^= SWAP32LE(0x00000000)^r; |
| 78 |
x32[ 2] ^= SWAP32LE(0x00000010)^r; |
| 79 |
x32[ 4] ^= SWAP32LE(0x00000020)^r; |
| 80 |
x32[ 6] ^= SWAP32LE(0x00000030)^r; |
| 81 |
x32[ 8] ^= SWAP32LE(0x00000040)^r; |
| 82 |
x32[10] ^= SWAP32LE(0x00000050)^r; |
| 83 |
x32[12] ^= SWAP32LE(0x00000060)^r; |
| 84 |
x32[14] ^= SWAP32LE(0x00000070)^r; |
| 85 |
COLUMN(x,y, 0, 0, 2, 4, 6, 9, 11, 13, 15, temp_v1, temp_v2, temp_upper_value, temp_lower_value, temp); |
| 86 |
COLUMN(x,y, 2, 2, 4, 6, 8, 11, 13, 15, 1, temp_v1, temp_v2, temp_upper_value, temp_lower_value, temp); |
| 87 |
COLUMN(x,y, 4, 4, 6, 8, 10, 13, 15, 1, 3, temp_v1, temp_v2, temp_upper_value, temp_lower_value, temp); |
| 88 |
COLUMN(x,y, 6, 6, 8, 10, 12, 15, 1, 3, 5, temp_v1, temp_v2, temp_upper_value, temp_lower_value, temp); |
| 89 |
COLUMN(x,y, 8, 8, 10, 12, 14, 1, 3, 5, 7, temp_v1, temp_v2, temp_upper_value, temp_lower_value, temp); |
| 90 |
COLUMN(x,y,10, 10, 12, 14, 0, 3, 5, 7, 9, temp_v1, temp_v2, temp_upper_value, temp_lower_value, temp); |
| 91 |
COLUMN(x,y,12, 12, 14, 0, 2, 5, 7, 9, 11, temp_v1, temp_v2, temp_upper_value, temp_lower_value, temp); |
| 92 |
COLUMN(x,y,14, 14, 0, 2, 4, 7, 9, 11, 13, temp_v1, temp_v2, temp_upper_value, temp_lower_value, temp); |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
static void RND512Q(uint8_t *x, uint32_t *y, uint32_t r) { |
| 97 |
uint32_t temp_v1, temp_v2, temp_upper_value, temp_lower_value, temp; |
| 98 |
uint32_t* x32 = (uint32_t*)x; |
| 99 |
x32[ 0] = ~x32[ 0]; |
| 100 |
x32[ 1] ^= SWAP32LE(0xffffffff)^r; |
| 101 |
x32[ 2] = ~x32[ 2]; |
| 102 |
x32[ 3] ^= SWAP32LE(0xefffffff)^r; |
| 103 |
x32[ 4] = ~x32[ 4]; |
| 104 |
x32[ 5] ^= SWAP32LE(0xdfffffff)^r; |
| 105 |
x32[ 6] = ~x32[ 6]; |
| 106 |
x32[ 7] ^= SWAP32LE(0xcfffffff)^r; |
| 107 |
x32[ 8] = ~x32[ 8]; |
| 108 |
x32[ 9] ^= SWAP32LE(0xbfffffff)^r; |
| 109 |
x32[10] = ~x32[10]; |
| 110 |
x32[11] ^= SWAP32LE(0xafffffff)^r; |
| 111 |
x32[12] = ~x32[12]; |
| 112 |
x32[13] ^= SWAP32LE(0x9fffffff)^r; |
| 113 |
x32[14] = ~x32[14]; |
| 114 |
x32[15] ^= SWAP32LE(0x8fffffff)^r; |
| 115 |
|
| 116 |
COLUMN(x,y, 0, 2, 6, 10, 14, 1, 5, 9, 13, temp_v1, temp_v2, temp_upper_value, temp_lower_value, temp); |
| 117 |
COLUMN(x,y, 2, 4, 8, 12, 0, 3, 7, 11, 15, temp_v1, temp_v2, temp_upper_value, temp_lower_value, temp); |
| 118 |
COLUMN(x,y, 4, 6, 10, 14, 2, 5, 9, 13, 1, temp_v1, temp_v2, temp_upper_value, temp_lower_value, temp); |
| 119 |
COLUMN(x,y, 6, 8, 12, 0, 4, 7, 11, 15, 3, temp_v1, temp_v2, temp_upper_value, temp_lower_value, temp); |
| 120 |
COLUMN(x,y, 8, 10, 14, 2, 6, 9, 13, 1, 5, temp_v1, temp_v2, temp_upper_value, temp_lower_value, temp); |
| 121 |
COLUMN(x,y,10, 12, 0, 4, 8, 11, 15, 3, 7, temp_v1, temp_v2, temp_upper_value, temp_lower_value, temp); |
| 122 |
COLUMN(x,y,12, 14, 2, 6, 10, 13, 1, 5, 9, temp_v1, temp_v2, temp_upper_value, temp_lower_value, temp); |
| 123 |
COLUMN(x,y,14, 0, 4, 8, 12, 15, 3, 7, 11, temp_v1, temp_v2, temp_upper_value, temp_lower_value, temp); |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
static void F512(uint32_t *h, const uint32_t *m) { |
| 128 |
int i; |
| 129 |
uint32_t Ptmp[2*COLS512]; |
| 130 |
uint32_t Qtmp[2*COLS512]; |
| 131 |
uint32_t y[2*COLS512]; |
| 132 |
uint32_t z[2*COLS512]; |
| 133 |
|
| 134 |
for (i = 0; i < 2*COLS512; i++) { |
| 135 |
z[i] = m[i]; |
| 136 |
Ptmp[i] = h[i]^m[i]; |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
RND512Q((uint8_t*)z, y, SWAP32LE(0x00000000)); |
| 141 |
RND512Q((uint8_t*)y, z, SWAP32LE(0x01000000)); |
| 142 |
RND512Q((uint8_t*)z, y, SWAP32LE(0x02000000)); |
| 143 |
RND512Q((uint8_t*)y, z, SWAP32LE(0x03000000)); |
| 144 |
RND512Q((uint8_t*)z, y, SWAP32LE(0x04000000)); |
| 145 |
RND512Q((uint8_t*)y, z, SWAP32LE(0x05000000)); |
| 146 |
RND512Q((uint8_t*)z, y, SWAP32LE(0x06000000)); |
| 147 |
RND512Q((uint8_t*)y, z, SWAP32LE(0x07000000)); |
| 148 |
RND512Q((uint8_t*)z, y, SWAP32LE(0x08000000)); |
| 149 |
RND512Q((uint8_t*)y, Qtmp, SWAP32LE(0x09000000)); |
| 150 |
|
| 151 |
|
| 152 |
RND512P((uint8_t*)Ptmp, y, SWAP32LE(0x00000000)); |
| 153 |
RND512P((uint8_t*)y, z, SWAP32LE(0x00000001)); |
| 154 |
RND512P((uint8_t*)z, y, SWAP32LE(0x00000002)); |
| 155 |
RND512P((uint8_t*)y, z, SWAP32LE(0x00000003)); |
| 156 |
RND512P((uint8_t*)z, y, SWAP32LE(0x00000004)); |
| 157 |
RND512P((uint8_t*)y, z, SWAP32LE(0x00000005)); |
| 158 |
RND512P((uint8_t*)z, y, SWAP32LE(0x00000006)); |
| 159 |
RND512P((uint8_t*)y, z, SWAP32LE(0x00000007)); |
| 160 |
RND512P((uint8_t*)z, y, SWAP32LE(0x00000008)); |
| 161 |
RND512P((uint8_t*)y, Ptmp, SWAP32LE(0x00000009)); |
| 162 |
|
| 163 |
|
| 164 |
for (i = 0; i < 2*COLS512; i++) { |
| 165 |
h[i] ^= Ptmp[i]^Qtmp[i]; |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
static void Transform(hashState *ctx, |
| 172 |
const uint8_t *input, |
| 173 |
int msglen) { |
| 174 |
|
| 175 |
|
| 176 |
for (; msglen >= SIZE512; |
| 177 |
msglen -= SIZE512, input += SIZE512) { |
| 178 |
F512(ctx->chaining,(uint32_t*)input); |
| 179 |
|
| 180 |
|
| 181 |
ctx->block_counter1++; |
| 182 |
if (ctx->block_counter1 == 0) ctx->block_counter2++; |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
static void OutputTransformation(hashState *ctx) { |
| 188 |
int j; |
| 189 |
uint32_t temp[2*COLS512]; |
| 190 |
uint32_t y[2*COLS512]; |
| 191 |
uint32_t z[2*COLS512]; |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
for (j = 0; j < 2*COLS512; j++) { |
| 196 |
temp[j] = ctx->chaining[j]; |
| 197 |
} |
| 198 |
RND512P((uint8_t*)temp, y, SWAP32LE(0x00000000)); |
| 199 |
RND512P((uint8_t*)y, z, SWAP32LE(0x00000001)); |
| 200 |
RND512P((uint8_t*)z, y, SWAP32LE(0x00000002)); |
| 201 |
RND512P((uint8_t*)y, z, SWAP32LE(0x00000003)); |
| 202 |
RND512P((uint8_t*)z, y, SWAP32LE(0x00000004)); |
| 203 |
RND512P((uint8_t*)y, z, SWAP32LE(0x00000005)); |
| 204 |
RND512P((uint8_t*)z, y, SWAP32LE(0x00000006)); |
| 205 |
RND512P((uint8_t*)y, z, SWAP32LE(0x00000007)); |
| 206 |
RND512P((uint8_t*)z, y, SWAP32LE(0x00000008)); |
| 207 |
RND512P((uint8_t*)y, temp, SWAP32LE(0x00000009)); |
| 208 |
for (j = 0; j < 2*COLS512; j++) { |
| 209 |
ctx->chaining[j] ^= temp[j]; |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
static void Init(hashState* ctx) { |
| 215 |
|
| 216 |
|
| 217 |
for(size_t i = 0; i < (SIZE512/sizeof(uint32_t)); i++) |
| 218 |
{ |
| 219 |
ctx->chaining[i] = 0; |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
ctx->chaining[2*COLS512-1] = SWAP32LE(u32BIG((uint32_t)HASH_BIT_LEN)); |
| 224 |
|
| 225 |
|
| 226 |
ctx->buf_ptr = 0; |
| 227 |
ctx->block_counter1 = 0; |
| 228 |
ctx->block_counter2 = 0; |
| 229 |
ctx->bits_in_last_byte = 0; |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
static void Update(hashState* ctx, |
| 234 |
const BitSequence* input, |
| 235 |
DataLength databitlen) { |
| 236 |
int index = 0; |
| 237 |
int msglen = (int)(databitlen/8); |
| 238 |
int rem = (int)(databitlen%8); |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
if (ctx->buf_ptr) { |
| 243 |
while (ctx->buf_ptr < SIZE512 && index < msglen) { |
| 244 |
ctx->buffer[(int)ctx->buf_ptr++] = input[index++]; |
| 245 |
} |
| 246 |
if (ctx->buf_ptr < SIZE512) { |
| 247 |
|
| 248 |
if (rem) { |
| 249 |
ctx->bits_in_last_byte = rem; |
| 250 |
ctx->buffer[(int)ctx->buf_ptr++] = input[index]; |
| 251 |
} |
| 252 |
return; |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
ctx->buf_ptr = 0; |
| 257 |
Transform(ctx, ctx->buffer, SIZE512); |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
Transform(ctx, input+index, msglen-index); |
| 262 |
index += ((msglen-index)/SIZE512)*SIZE512; |
| 263 |
|
| 264 |
|
| 265 |
while (index < msglen) { |
| 266 |
ctx->buffer[(int)ctx->buf_ptr++] = input[index++]; |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
if (rem) { |
| 273 |
ctx->bits_in_last_byte = rem; |
| 274 |
ctx->buffer[(int)ctx->buf_ptr++] = input[index]; |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
#define BILB ctx->bits_in_last_byte |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
static void Final(hashState* ctx, |
| 283 |
BitSequence* output) { |
| 284 |
int i, j = 0, hashbytelen = HASH_BIT_LEN/8; |
| 285 |
uint8_t *s = (BitSequence*)ctx->chaining; |
| 286 |
|
| 287 |
|
| 288 |
if (BILB) { |
| 289 |
ctx->buffer[(int)ctx->buf_ptr-1] &= ((1<<BILB)-1)<<(8-BILB); |
| 290 |
ctx->buffer[(int)ctx->buf_ptr-1] ^= 0x1<<(7-BILB); |
| 291 |
BILB = 0; |
| 292 |
} |
| 293 |
else ctx->buffer[(int)ctx->buf_ptr++] = 0x80; |
| 294 |
|
| 295 |
|
| 296 |
if (ctx->buf_ptr > SIZE512-LENGTHFIELDLEN) { |
| 297 |
|
| 298 |
while (ctx->buf_ptr < SIZE512) { |
| 299 |
ctx->buffer[(int)ctx->buf_ptr++] = 0; |
| 300 |
} |
| 301 |
|
| 302 |
Transform(ctx, ctx->buffer, SIZE512); |
| 303 |
ctx->buf_ptr = 0; |
| 304 |
} |
| 305 |
while (ctx->buf_ptr < SIZE512-LENGTHFIELDLEN) { |
| 306 |
ctx->buffer[(int)ctx->buf_ptr++] = 0; |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
ctx->block_counter1++; |
| 311 |
if (ctx->block_counter1 == 0) ctx->block_counter2++; |
| 312 |
ctx->buf_ptr = SIZE512; |
| 313 |
|
| 314 |
while (ctx->buf_ptr > SIZE512-(int)sizeof(uint32_t)) { |
| 315 |
ctx->buffer[(int)--ctx->buf_ptr] = (uint8_t)ctx->block_counter1; |
| 316 |
ctx->block_counter1 >>= 8; |
| 317 |
} |
| 318 |
while (ctx->buf_ptr > SIZE512-LENGTHFIELDLEN) { |
| 319 |
ctx->buffer[(int)--ctx->buf_ptr] = (uint8_t)ctx->block_counter2; |
| 320 |
ctx->block_counter2 >>= 8; |
| 321 |
} |
| 322 |
|
| 323 |
Transform(ctx, ctx->buffer, SIZE512); |
| 324 |
|
| 325 |
OutputTransformation(ctx); |
| 326 |
|
| 327 |
|
| 328 |
for (i = SIZE512-hashbytelen; i < SIZE512; i++,j++) { |
| 329 |
output[j] = s[i]; |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
for (i = 0; i < COLS512; i++) { |
| 334 |
ctx->chaining[i] = 0; |
| 335 |
} |
| 336 |
for (i = 0; i < SIZE512; i++) { |
| 337 |
ctx->buffer[i] = 0; |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
|
| 342 |
void groestl(const BitSequence* data, |
| 343 |
DataLength databitlen, |
| 344 |
BitSequence* hashval) { |
| 345 |
|
| 346 |
hashState context; |
| 347 |
|
| 348 |
|
| 349 |
Init(&context); |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
Update(&context, data, databitlen); |
| 354 |
|
| 355 |
|
| 356 |
Final(&context, hashval); |
| 357 |
} |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|