decrypt.c 982 Bytes
Newer Older
Martin Schläffer committed
1 2 3 4 5
#include "api.h"
#include "ascon.h"
#include "loadstore.h"
#include "permutations.h"
#include "printstate.h"
Christoph Dobraunig committed
6

Martin Schläffer committed
7 8 9 10 11 12 13
void ascon_core(state_t* s, uint8_t* out, const uint8_t* in, uint64_t tlen,
                const uint8_t* ad, uint64_t adlen, const uint8_t* npub,
                const uint8_t* k, uint8_t mode);

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) {
Christoph Dobraunig committed
14 15 16 17
  if (clen < CRYPTO_ABYTES) {
    *mlen = 0;
    return -1;
  }
Martin Schläffer committed
18
  state_t s;
Christoph Dobraunig committed
19
  (void)nsec;
Martin Schläffer committed
20
  /* set plaintext size */
Christoph Dobraunig committed
21
  *mlen = clen - CRYPTO_ABYTES;
Martin Schläffer committed
22
  /* ascon decryption */
Christoph Dobraunig committed
23
  ascon_core(&s, m, c, *mlen, ad, adlen, npub, k, ASCON_DEC);
Martin Schläffer committed
24 25 26 27
  /* verify tag (should be constant time, check compiler output) */
  XOR(s.x3, LOAD64(c + *mlen));
  XOR(s.x4, LOAD64(c + *mlen + 8));
  if (NOTZERO(s.x3, s.x4)) {
Christoph Dobraunig committed
28 29 30 31 32
    *mlen = 0;
    return -1;
  }
  return 0;
}