decrypt.c 2.26 KB
Newer Older
lwc-tester committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
#include "api.h"
#include "permutations.h"

#define RATE (64 / 8)
#define PA_ROUNDS 12
#define PB_ROUNDS 6
#define IV                                                        \
  ((u64)(8 * (CRYPTO_KEYBYTES)) << 56 | (u64)(8 * (RATE)) << 48 | \
   (u64)(PA_ROUNDS) << 40 | (u64)(PB_ROUNDS) << 32)

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) {
  if (clen < CRYPTO_ABYTES) {
    *mlen = 0;
    return -1;
  }

  const u64 K0 = BYTES_TO_U64(k, 8) >> 32;
  const u64 K1 = BYTES_TO_U64(k + 4, 8);
  const u64 K2 = BYTES_TO_U64(k + 12, 8);
  const u64 N0 = BYTES_TO_U64(npub, 8);
  const u64 N1 = BYTES_TO_U64(npub + 8, 8);
  state s;
  u64 c0;
  (void)nsec;

  // set plaintext size
  *mlen = clen - CRYPTO_ABYTES;

  // initialization
  s.x0 = IV | K0;
  s.x1 = K1;
  s.x2 = K2;
  s.x3 = N0;
  s.x4 = N1;
  printstate("initial value:", s);
  P12(&s);
  s.x2 ^= K0;
  s.x3 ^= K1;
  s.x4 ^= K2;
  printstate("initialization:", s);

  // process associated data
  if (adlen) {
    while (adlen >= RATE) {
      s.x0 ^= BYTES_TO_U64(ad, 8);
      P6(&s);
      adlen -= RATE;
      ad += RATE;
    }
    s.x0 ^= BYTES_TO_U64(ad, adlen);
    s.x0 ^= 0x80ull << (56 - 8 * adlen);
    P6(&s);
  }
  s.x4 ^= 1;
  printstate("process associated data:", s);

  // process plaintext
  clen -= CRYPTO_ABYTES;
  while (clen >= RATE) {
    c0 = BYTES_TO_U64(c, 8);
    U64_TO_BYTES(m, s.x0 ^ c0, 8);
    s.x0 = c0;
    P6(&s);
    clen -= RATE;
    m += RATE;
    c += RATE;
  }
  c0 = BYTES_TO_U64(c, clen);
  U64_TO_BYTES(m, s.x0 ^ c0, clen);
  s.x0 &= ~BYTE_MASK(clen);
  s.x0 |= c0;
  s.x0 ^= 0x80ull << (56 - 8 * clen);
  c += clen;
  printstate("process plaintext:", s);

  // finalization
  s.x1 ^= K0 << 32 | K1 >> 32;
  s.x2 ^= K1 << 32 | K2 >> 32;
  s.x3 ^= K2 << 32;
  P12(&s);
  s.x3 ^= K1;
  s.x4 ^= K2;
  printstate("finalization:", s);

lwc-tester committed
89 90
  // verify tag (should be constant time, check compiler output)
  if (((s.x3 ^ BYTES_TO_U64(c, 8)) | (s.x4 ^ BYTES_TO_U64(c + 8, 8))) != 0) {
lwc-tester committed
91 92 93 94 95 96 97
    *mlen = 0;
    return -1;
  }

  return 0;
}