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

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

Martin Schläffer committed
10 11
  /* initialization */
  s.x0 = ASCON_HASH_IV;
lwc-tester committed
12 13 14 15 16
  s.x1 = 0;
  s.x2 = 0;
  s.x3 = 0;
  s.x4 = 0;
  P12(&s);
Martin Schläffer committed
17
  printstate("initialization", &s);
lwc-tester committed
18

Martin Schläffer committed
19 20 21
  /* absorb plaintext */
  while (len >= ASCON_128_RATE) {
    s.x0 ^= LOAD(in, 8);
lwc-tester committed
22
    P12(&s);
Martin Schläffer committed
23 24
    in += ASCON_128_RATE;
    len -= ASCON_128_RATE;
lwc-tester committed
25
  }
Martin Schläffer committed
26 27
  s.x0 ^= LOAD(in, len);
  s.x0 ^= PAD(len);
lwc-tester committed
28
  P12(&s);
Martin Schläffer committed
29
  printstate("absorb plaintext", &s);
lwc-tester committed
30

Martin Schläffer committed
31 32 33 34
  /* squeeze output */
  len = CRYPTO_BYTES;
  while (len > ASCON_128_RATE) {
    STORE(out, s.x0, 8);
lwc-tester committed
35
    P12(&s);
Martin Schläffer committed
36 37
    out += ASCON_128_RATE;
    len -= ASCON_128_RATE;
lwc-tester committed
38
  }
Martin Schläffer committed
39 40
  STORE(out, s.x0, 8);
  printstate("squeeze output", &s);
lwc-tester committed
41 42 43

  return 0;
}