hash.c 843 Bytes
Newer Older
lwc-tester committed
1
#include "api.h"
Martin Schläffer committed
2
#include "ascon.h"
lwc-tester committed
3
#include "permutations.h"
Martin Schläffer committed
4
#include "printstate.h"
Martin Schläffer committed
5
#include "word.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_XOF_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
  /* absorb plaintext */
  while (len >= ASCON_128_RATE) {
Martin Schläffer committed
21
    s.x0 ^= LOADBYTES(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
  s.x0 ^= LOADBYTES(in, len);
Martin Schläffer committed
27
  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
  /* squeeze output */
  len = CRYPTO_BYTES;
  while (len > ASCON_128_RATE) {
Martin Schläffer committed
34
    STOREBYTES(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
  STOREBYTES(out, s.x0, 8);
Martin Schläffer committed
40
  printstate("squeeze output", &s);
lwc-tester committed
41 42 43

  return 0;
}