update.c 1.11 KB
Newer Older
Martin Schlaeffer committed
1 2 3 4 5 6 7
#include "api.h"
#include "ascon.h"
#include "permutations.h"
#include "printstate.h"

void ascon_update(state_t* s, uint8_t* out, const uint8_t* in, uint64_t len,
                  uint8_t mode) {
Enrico Pozzobon committed
8 9
  const int rate = 16;
  const int nr = 8;
Martin Schlaeffer committed
10
  word_t tmp0, tmp1;
Enrico Pozzobon committed
11 12 13 14 15 16 17 18
  int n = 0, n0 = 0, n1 = 0;
  while (len) {
    /* determine block size */
    n0 = len < 8 ? len : 8;
    n1 = len < 8 ? 0 : (len < 16 ? len - 8 : 8);
    n = n0 + n1;
    /* absorb data */
    tmp0 = LOAD(in, n0);
Martin Schlaeffer committed
19
    s->x0 = XOR(s->x0, tmp0);
Enrico Pozzobon committed
20 21 22
    if (n1) tmp1 = LOAD(in + 8, n1);
    if (n1) s->x1 = XOR(s->x1, tmp1);
    /* extract data */
Martin Schlaeffer committed
23
    if (mode & ASCON_SQUEEZE) {
Enrico Pozzobon committed
24 25
      STORE(out, s->x0, n0);
      if (n1) STORE(out + 8, s->x1, n1);
Martin Schlaeffer committed
26
    }
Enrico Pozzobon committed
27
    /* insert data */
Martin Schlaeffer committed
28
    if (mode & ASCON_INSERT) {
Enrico Pozzobon committed
29 30 31 32
      s->x0 = CLEAR(s->x0, n0);
      s->x0 = XOR(s->x0, tmp0);
      if (n1) s->x1 = CLEAR(s->x1, n1);
      if (n1) s->x1 = XOR(s->x1, tmp1);
Martin Schlaeffer committed
33
    }
Enrico Pozzobon committed
34 35 36 37 38
    /* compute permutation for full blocks */
    if (n == rate) P(s, nr);
    in += n;
    out += n;
    len -= n;
Martin Schlaeffer committed
39
  }
Enrico Pozzobon committed
40 41
  if (n % rate < 8)
    s->x0 = XOR(s->x0, PAD(n0 % 8));
Martin Schlaeffer committed
42
  else
Enrico Pozzobon committed
43
    s->x1 = XOR(s->x1, PAD(n1 % 8));
Martin Schlaeffer committed
44
}