hash.c 1 KB
Newer Older
Martin Schläffer committed
1
#include "api.h"
Martin Schläffer committed
2 3 4
#include "ascon.h"
#include "permutations.h"
#include "printstate.h"
lwc-tester committed
5

Martin Schläffer committed
6 7
int crypto_hash(uint8_t* out, const uint8_t* in, uint64_t len) {
  state_t s;
lwc-tester committed
8

Martin Schläffer committed
9
  /* initialization */
Martin Schläffer committed
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
#ifdef ASCON_HASH
  s.x0 = ASCON_HASH_IV0;
  s.x1 = ASCON_HASH_IV1;
  s.x2 = ASCON_HASH_IV2;
  s.x3 = ASCON_HASH_IV3;
  s.x4 = ASCON_HASH_IV4;
#endif
#ifdef ASCON_XOF
  s.x0 = ASCON_XOF_IV0;
  s.x1 = ASCON_XOF_IV1;
  s.x2 = ASCON_XOF_IV2;
  s.x3 = ASCON_XOF_IV3;
  s.x4 = ASCON_XOF_IV4;
#endif
  printstate("initialization", &s);

  /* absorb plaintext */
  while (len >= ASCON_RATE) {
    s.x0 = XOR(s.x0, LOAD64(in));
    P12(&s);
Martin Schläffer committed
30
    in += ASCON_RATE;
Martin Schläffer committed
31
    len -= ASCON_RATE;
lwc-tester committed
32
  }
Martin Schläffer committed
33 34 35 36 37 38 39 40 41 42
  if (len) s.x0 = XOR(s.x0, LOAD(in, len));
  s.x0 = XOR(s.x0, PAD(len));
  P12(&s);
  printstate("absorb plaintext", &s);

  /* squeeze output */
  len = CRYPTO_BYTES;
  while (len > ASCON_RATE) {
    STORE64(out, s.x0);
    P12(&s);
Martin Schläffer committed
43
    out += ASCON_RATE;
Martin Schläffer committed
44
    len -= ASCON_RATE;
lwc-tester committed
45
  }
Martin Schläffer committed
46 47
  STORE64(out, s.x0);
  printstate("squeeze output", &s);
lwc-tester committed
48 49 50

  return 0;
}