round.h 1.15 KB
Newer Older
Martin Schläffer committed
1 2 3 4 5 6
#ifndef ROUND_H_
#define ROUND_H_

#include "ascon.h"
#include "printstate.h"

Enrico Pozzobon committed
7
static inline uint64_t ROR(uint64_t x, int n) {
Martin Schläffer committed
8
  return x >> n | x << (-n & 63);
Martin Schläffer committed
9 10 11 12 13
}

static inline void ROUND(state_t* s, uint8_t C) {
  state_t t;
  /* addition of round constant */
Martin Schläffer committed
14
  s->x[2] ^= C;
Enrico Pozzobon committed
15
  /* printstate(" round constant", s); */
Martin Schläffer committed
16
  /* substitution layer */
Martin Schläffer committed
17 18 19
  s->x[0] ^= s->x[4];
  s->x[4] ^= s->x[3];
  s->x[2] ^= s->x[1];
Martin Schläffer committed
20
  /* start of keccak s-box */
Martin Schläffer committed
21 22 23 24 25
  t.x[0] = s->x[0] ^ (~s->x[1] & s->x[2]);
  t.x[1] = s->x[1] ^ (~s->x[2] & s->x[3]);
  t.x[2] = s->x[2] ^ (~s->x[3] & s->x[4]);
  t.x[3] = s->x[3] ^ (~s->x[4] & s->x[0]);
  t.x[4] = s->x[4] ^ (~s->x[0] & s->x[1]);
Martin Schläffer committed
26
  /* end of keccak s-box */
Martin Schläffer committed
27 28 29 30
  t.x[1] ^= t.x[0];
  t.x[0] ^= t.x[4];
  t.x[3] ^= t.x[2];
  t.x[2] = ~t.x[2];
Martin Schläffer committed
31 32
  /* printstate(" substitution layer", &t); */
  /* linear diffusion layer */
Martin Schläffer committed
33 34 35 36 37
  s->x[0] = t.x[0] ^ ROR(t.x[0], 19) ^ ROR(t.x[0], 28);
  s->x[1] = t.x[1] ^ ROR(t.x[1], 61) ^ ROR(t.x[1], 39);
  s->x[2] = t.x[2] ^ ROR(t.x[2], 1) ^ ROR(t.x[2], 6);
  s->x[3] = t.x[3] ^ ROR(t.x[3], 10) ^ ROR(t.x[3], 17);
  s->x[4] = t.x[4] ^ ROR(t.x[4], 7) ^ ROR(t.x[4], 41);
Martin Schläffer committed
38 39 40 41
  printstate(" round output", s);
}

#endif /* ROUND_H_ */