hash.c 1005 Bytes
Newer Older
lwc-tester committed
1
#include "api.h"
Martin Schläffer committed
2
#include "ascon.h"
Enrico Pozzobon committed
3
#include "crypto_hash.h"
lwc-tester committed
4
#include "permutations.h"
Martin Schläffer committed
5
#include "printstate.h"
Martin Schläffer committed
6
#include "word.h"
lwc-tester committed
7

Enrico Pozzobon committed
8 9 10
int crypto_hash(unsigned char* out, const unsigned char* in,
                unsigned long long len) {
  /* initialize */
Martin Schläffer committed
11 12
  state_t s;
  s.x0 = ASCON_XOF_IV;
lwc-tester committed
13 14 15 16 17
  s.x1 = 0;
  s.x2 = 0;
  s.x3 = 0;
  s.x4 = 0;
  P12(&s);
Martin Schläffer committed
18
  printstate("initialization", &s);
lwc-tester committed
19

Enrico Pozzobon committed
20 21
  /* absorb full plaintext blocks */
  while (len >= ASCON_HASH_RATE) {
Martin Schläffer committed
22
    s.x0 ^= LOADBYTES(in, 8);
lwc-tester committed
23
    P12(&s);
Enrico Pozzobon committed
24 25
    in += ASCON_HASH_RATE;
    len -= ASCON_HASH_RATE;
lwc-tester committed
26
  }
Enrico Pozzobon committed
27
  /* absorb final plaintext block */
Martin Schläffer committed
28
  s.x0 ^= LOADBYTES(in, len);
Martin Schläffer committed
29
  s.x0 ^= PAD(len);
lwc-tester committed
30
  P12(&s);
Martin Schläffer committed
31
  printstate("absorb plaintext", &s);
lwc-tester committed
32

Enrico Pozzobon committed
33
  /* squeeze full output blocks */
Martin Schläffer committed
34
  len = CRYPTO_BYTES;
Enrico Pozzobon committed
35
  while (len > ASCON_HASH_RATE) {
Martin Schläffer committed
36
    STOREBYTES(out, s.x0, 8);
lwc-tester committed
37
    P12(&s);
Enrico Pozzobon committed
38 39
    out += ASCON_HASH_RATE;
    len -= ASCON_HASH_RATE;
lwc-tester committed
40
  }
Enrico Pozzobon committed
41 42
  /* squeeze final output block */
  STOREBYTES(out, s.x0, len);
Martin Schläffer committed
43
  printstate("squeeze output", &s);
lwc-tester committed
44 45 46

  return 0;
}