round.h 1.32 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
forceinline void KINIT(word_t* K0, word_t* K1, word_t* K2) {
Martin Schläffer committed
8 9 10 11 12
  *K0 = WORD_T(0);
  *K1 = WORD_T(0);
  *K2 = WORD_T(0);
}

Enrico Pozzobon committed
13
forceinline void PINIT(state_t* s) {
Martin Schläffer committed
14 15 16 17 18 19 20
  s->x0 = WORD_T(0);
  s->x1 = WORD_T(0);
  s->x2 = WORD_T(0);
  s->x3 = WORD_T(0);
  s->x4 = WORD_T(0);
}

Enrico Pozzobon committed
21
forceinline void ROUND(state_t* s, word_t C) {
Martin Schläffer committed
22 23
  state_t t;
  /* round constant */
Enrico Pozzobon committed
24
  s->x2 = XOR(s->x2, C);
Martin Schläffer committed
25
  /* s-box layer */
Enrico Pozzobon committed
26 27 28 29 30 31 32 33 34 35 36
  s->x0 = XOR(s->x0, s->x4);
  s->x4 = XOR(s->x4, s->x3);
  s->x2 = XOR(s->x2, s->x1);
  t.x0 = XOR(s->x0, AND(NOT(s->x1), s->x2));
  t.x2 = XOR(s->x2, AND(NOT(s->x3), s->x4));
  t.x4 = XOR(s->x4, AND(NOT(s->x0), s->x1));
  t.x1 = XOR(s->x1, AND(NOT(s->x2), s->x3));
  t.x3 = XOR(s->x3, AND(NOT(s->x4), s->x0));
  t.x1 = XOR(t.x1, t.x0);
  t.x3 = XOR(t.x3, t.x2);
  t.x0 = XOR(t.x0, t.x4);
Martin Schläffer committed
37
  /* linear layer */
Enrico Pozzobon committed
38 39 40 41 42 43 44 45 46 47 48
  s->x2 = XOR(t.x2, ROR(t.x2, 6 - 1));
  s->x3 = XOR(t.x3, ROR(t.x3, 17 - 10));
  s->x4 = XOR(t.x4, ROR(t.x4, 41 - 7));
  s->x0 = XOR(t.x0, ROR(t.x0, 28 - 19));
  s->x1 = XOR(t.x1, ROR(t.x1, 61 - 39));
  s->x2 = XOR(t.x2, ROR(s->x2, 1));
  s->x3 = XOR(t.x3, ROR(s->x3, 10));
  s->x4 = XOR(t.x4, ROR(s->x4, 7));
  s->x0 = XOR(t.x0, ROR(s->x0, 19));
  s->x1 = XOR(t.x1, ROR(s->x1, 39));
  s->x2 = NOT(s->x2);
Martin Schläffer committed
49 50 51 52
  printstate(" round output", s);
}

#endif /* ROUND_H_ */