decrypt.c 2.05 KB
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 9 10 11 12
int crypto_aead_decrypt(uint8_t* m, uint64_t* mlen, uint8_t* nsec,
                        const uint8_t* c, uint64_t clen, const uint8_t* ad,
                        uint64_t adlen, const uint8_t* npub, const uint8_t* k) {
  uint64_t K0, K1, K2, N0, N1;
  state_t s;
  (void)nsec;
lwc-tester committed
13 14 15 16 17 18

  if (clen < CRYPTO_ABYTES) {
    *mlen = 0;
    return -1;
  }

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

Martin Schläffer committed
22
  /* load key and nonce */
Martin Schläffer committed
23 24 25 26 27
  K0 = LOADBYTES(k + 0, 4) >> 32;
  K1 = LOADBYTES(k + 4, 8);
  K2 = LOADBYTES(k + 12, 8);
  N0 = LOADBYTES(npub, 8);
  N1 = LOADBYTES(npub + 8, 8);
Martin Schläffer committed
28 29 30

  /* initialization */
  s.x0 = ASCON_80PQ_IV | K0;
lwc-tester committed
31 32 33 34 35 36 37 38
  s.x1 = K1;
  s.x2 = K2;
  s.x3 = N0;
  s.x4 = N1;
  P12(&s);
  s.x2 ^= K0;
  s.x3 ^= K1;
  s.x4 ^= K2;
Martin Schläffer committed
39
  printstate("initialization", &s);
lwc-tester committed
40

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

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

Martin Schläffer committed
77
  /* finalization */
lwc-tester committed
78 79 80 81 82 83
  s.x1 ^= K0 << 32 | K1 >> 32;
  s.x2 ^= K1 << 32 | K2 >> 32;
  s.x3 ^= K2 << 32;
  P12(&s);
  s.x3 ^= K1;
  s.x4 ^= K2;
Martin Schläffer committed
84
  printstate("finalization", &s);
lwc-tester committed
85

Martin Schläffer committed
86
  /* verify tag (should be constant time, check compiler output) */
Martin Schläffer committed
87
  if ((s.x3 ^ LOADBYTES(c, 8)) | (s.x4 ^ LOADBYTES(c + 8, 8))) {
lwc-tester committed
88 89 90 91 92 93
    *mlen = 0;
    return -1;
  }

  return 0;
}