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

Martin Schläffer committed
6 7 8 9 10 11 12 13
#ifdef ASCON_AEAD_RATE

#if ASCON_AEAD_RATE == ASCON_128_RATE
#define ASCON_AEAD_ROUNDS ASCON_128_PB_ROUNDS
#else
#define ASCON_AEAD_ROUNDS ASCON_128A_PB_ROUNDS
#endif

Martin Schlaeffer committed
14 15
void ascon_update(state_t* s, uint8_t* out, const uint8_t* in, uint64_t len,
                  uint8_t mode) {
Martin Schläffer committed
16 17 18 19 20 21 22 23 24 25 26 27 28 29
#if defined(ASCON_HASH_BYTES)
  const int nr = (mode & ASCON_HASH) ? ASCON_HASH_ROUNDS : ASCON_AEAD_ROUNDS;
  const int rate = (mode & ASCON_HASH) ? ASCON_HASH_RATE : ASCON_AEAD_RATE;
#else
  const int nr = ASCON_AEAD_ROUNDS;
  const int rate = ASCON_AEAD_RATE;
#endif
#if ASCON_AEAD_RATE == 8
  const int i = 0;
#else
  int i = 0;
#endif
  uint64_t tmp;
  while (len >= 8) {
Enrico Pozzobon committed
30
    /* absorb data */
Martin Schläffer committed
31 32 33 34 35 36 37 38 39
#ifdef ASCON_HASH_BYTES
    if (mode & ASCON_ABSORB)
#endif
    {
      tmp = LOAD(in, 8);
      s->x[i] ^= tmp;
      if (mode == ASCON_ABSORB) printstate("absorb adata", s);
      if (mode == ASCON_ENCRYPT) printstate("absorb plaintext", s);
    }
Enrico Pozzobon committed
40
    /* extract data */
Martin Schlaeffer committed
41
    if (mode & ASCON_SQUEEZE) {
Martin Schläffer committed
42 43
      STORE(out, s->x[i], 8);
      if (mode & ASCON_HASH) printstate("squeeze output", s);
Martin Schlaeffer committed
44
    }
Enrico Pozzobon committed
45
    /* insert data */
Martin Schlaeffer committed
46
    if (mode & ASCON_INSERT) {
Martin Schläffer committed
47 48
      s->x[i] = tmp;
      printstate("insert ciphertext", s);
Martin Schlaeffer committed
49
    }
Enrico Pozzobon committed
50
    /* compute permutation for full blocks */
Martin Schläffer committed
51 52 53 54 55 56 57
#if ASCON_AEAD_RATE == 16
    if (++i == rate / 8) i = 0;
#endif
    if (i == 0) P(s, nr);
    in += 8;
    out += 8;
    len -= 8;
Martin Schlaeffer committed
58
  }
Martin Schläffer committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
  /* absorb data */
#ifdef ASCON_HASH_BYTES
  if (mode & ASCON_ABSORB)
#endif
  {
    tmp = LOADBYTES(in, len);
    s->x[i] ^= tmp;
  }
  /* extract data */
  if (mode & ASCON_SQUEEZE) STOREBYTES(out, s->x[i], len);
  /* insert data */
  if (mode & ASCON_INSERT) {
    s->x[i] = CLEAR(s->x[i], len);
    s->x[i] ^= tmp;
  }
  s->x[i] ^= PAD(len);
Martin Schlaeffer committed
75
}
Martin Schläffer committed
76 77

#endif