decrypt.c 2.22 KB
Newer Older
lwc-tester committed
1
#include "api.h"
Martin Schläffer committed
2
#include "ascon.h"
Enrico Pozzobon committed
3
#include "crypto_aead.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 11 12
int crypto_aead_decrypt(unsigned char* m, unsigned long long* mlen,
                        unsigned char* nsec, const unsigned char* c,
                        unsigned long long clen, const unsigned char* ad,
                        unsigned long long adlen, const unsigned char* npub,
                        const unsigned char* k) {
Martin Schläffer committed
13
  (void)nsec;
lwc-tester committed
14

Enrico Pozzobon committed
15
  if (clen < CRYPTO_ABYTES) return -1;
lwc-tester committed
16

Martin Schläffer committed
17
  /* set plaintext size */
lwc-tester committed
18 19
  *mlen = clen - CRYPTO_ABYTES;

Martin Schläffer committed
20
  /* load key and nonce */
Enrico Pozzobon committed
21 22 23 24
  const uint64_t K0 = LOADBYTES(k, 8);
  const uint64_t K1 = LOADBYTES(k + 8, 8);
  const uint64_t N0 = LOADBYTES(npub, 8);
  const uint64_t N1 = LOADBYTES(npub + 8, 8);
Martin Schläffer committed
25

Enrico Pozzobon committed
26 27
  /* initialize */
  state_t s;
Martin Schläffer committed
28
  s.x0 = ASCON_128_IV;
lwc-tester committed
29 30 31 32 33 34 35
  s.x1 = K0;
  s.x2 = K1;
  s.x3 = N0;
  s.x4 = N1;
  P12(&s);
  s.x3 ^= K0;
  s.x4 ^= K1;
Martin Schläffer committed
36
  printstate("initialization", &s);
lwc-tester committed
37 38

  if (adlen) {
Enrico Pozzobon committed
39
    /* full associated data blocks */
Martin Schläffer committed
40
    while (adlen >= ASCON_128_RATE) {
Martin Schläffer committed
41
      s.x0 ^= LOADBYTES(ad, 8);
lwc-tester committed
42
      P6(&s);
Martin Schläffer committed
43 44
      ad += ASCON_128_RATE;
      adlen -= ASCON_128_RATE;
lwc-tester committed
45
    }
Martin Schläffer committed
46
    /* final associated data block */
Martin Schläffer committed
47
    s.x0 ^= LOADBYTES(ad, adlen);
Martin Schläffer committed
48
    s.x0 ^= PAD(adlen);
lwc-tester committed
49 50
    P6(&s);
  }
Enrico Pozzobon committed
51
  /* domain separation */
lwc-tester committed
52
  s.x4 ^= 1;
Martin Schläffer committed
53
  printstate("process associated data", &s);
lwc-tester committed
54

Enrico Pozzobon committed
55
  /* full ciphertext blocks */
lwc-tester committed
56
  clen -= CRYPTO_ABYTES;
Martin Schläffer committed
57
  while (clen >= ASCON_128_RATE) {
Martin Schläffer committed
58 59
    uint64_t c0 = LOADBYTES(c, 8);
    STOREBYTES(m, s.x0 ^ c0, 8);
lwc-tester committed
60 61
    s.x0 = c0;
    P6(&s);
Martin Schläffer committed
62 63 64
    m += ASCON_128_RATE;
    c += ASCON_128_RATE;
    clen -= ASCON_128_RATE;
lwc-tester committed
65
  }
Martin Schläffer committed
66
  /* final ciphertext block */
Martin Schläffer committed
67 68 69
  uint64_t c0 = LOADBYTES(c, clen);
  STOREBYTES(m, s.x0 ^ c0, clen);
  s.x0 = CLEARBYTES(s.x0, clen);
lwc-tester committed
70
  s.x0 |= c0;
Martin Schläffer committed
71
  s.x0 ^= PAD(clen);
lwc-tester committed
72
  c += clen;
Martin Schläffer committed
73
  printstate("process ciphertext", &s);
lwc-tester committed
74

Enrico Pozzobon committed
75
  /* finalize */
lwc-tester committed
76 77 78 79 80
  s.x1 ^= K0;
  s.x2 ^= K1;
  P12(&s);
  s.x3 ^= K0;
  s.x4 ^= K1;
Martin Schläffer committed
81
  printstate("finalization", &s);
lwc-tester committed
82

Enrico Pozzobon committed
83 84 85 86 87
  /* set tag */
  uint8_t t[16];
  STOREBYTES(t, s.x3, 8);
  STOREBYTES(t + 8, s.x4, 8);

Martin Schläffer committed
88
  /* verify tag (should be constant time, check compiler output) */
Enrico Pozzobon committed
89 90 91
  int result = 0;
  for (int i = 0; i < CRYPTO_ABYTES; ++i) result |= c[i] ^ t[i];
  result = (((result - 1) >> 8) & 1) - 1;
lwc-tester committed
92

Enrico Pozzobon committed
93
  return result;
lwc-tester committed
94
}