encrypt.c 2 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_encrypt(unsigned char* c, unsigned long long* clen,
                        const unsigned char* m, unsigned long long mlen,
                        const unsigned char* ad, unsigned long long adlen,
                        const unsigned char* nsec, const unsigned char* npub,
                        const unsigned char* k) {
lwc-tester committed
13 14
  (void)nsec;

Martin Schläffer committed
15
  /* set ciphertext size */
lwc-tester committed
16 17
  *clen = mlen + CRYPTO_ABYTES;

Martin Schläffer committed
18
  /* load key and nonce */
Enrico Pozzobon committed
19 20 21 22 23
  const uint64_t K0 = LOADBYTES(k + 0, 4) >> 32;
  const uint64_t K1 = LOADBYTES(k + 4, 8);
  const uint64_t K2 = LOADBYTES(k + 12, 8);
  const uint64_t N0 = LOADBYTES(npub, 8);
  const uint64_t N1 = LOADBYTES(npub + 8, 8);
Martin Schläffer committed
24

Enrico Pozzobon committed
25 26
  /* initialize */
  state_t s;
Martin Schläffer committed
27
  s.x0 = ASCON_80PQ_IV | K0;
lwc-tester committed
28 29 30 31 32 33 34 35
  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
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 plaintext blocks */
Martin Schläffer committed
56
  while (mlen >= ASCON_128_RATE) {
Martin Schläffer committed
57 58
    s.x0 ^= LOADBYTES(m, 8);
    STOREBYTES(c, s.x0, 8);
lwc-tester committed
59
    P6(&s);
Martin Schläffer committed
60 61 62
    m += ASCON_128_RATE;
    c += ASCON_128_RATE;
    mlen -= ASCON_128_RATE;
lwc-tester committed
63
  }
Martin Schläffer committed
64
  /* final plaintext block */
Martin Schläffer committed
65 66
  s.x0 ^= LOADBYTES(m, mlen);
  STOREBYTES(c, s.x0, mlen);
Martin Schläffer committed
67
  s.x0 ^= PAD(mlen);
lwc-tester committed
68
  c += mlen;
Martin Schläffer committed
69
  printstate("process plaintext", &s);
lwc-tester committed
70

Enrico Pozzobon committed
71
  /* finalize */
lwc-tester committed
72 73 74 75 76 77
  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
78
  printstate("finalization", &s);
lwc-tester committed
79

Martin Schläffer committed
80
  /* set tag */
Martin Schläffer committed
81 82
  STOREBYTES(c, s.x3, 8);
  STOREBYTES(c + 8, s.x4, 8);
lwc-tester committed
83 84 85

  return 0;
}