encrypt.c 1.87 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
  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
23

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

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

Enrico Pozzobon committed
53
  /* full plaintext blocks */
Martin Schläffer committed
54
  while (mlen >= ASCON_128_RATE) {
Martin Schläffer committed
55 56
    s.x0 ^= LOADBYTES(m, 8);
    STOREBYTES(c, s.x0, 8);
lwc-tester committed
57
    P6(&s);
Martin Schläffer committed
58 59 60
    m += ASCON_128_RATE;
    c += ASCON_128_RATE;
    mlen -= ASCON_128_RATE;
lwc-tester committed
61
  }
Martin Schläffer committed
62
  /* final plaintext block */
Martin Schläffer committed
63 64
  s.x0 ^= LOADBYTES(m, mlen);
  STOREBYTES(c, s.x0, mlen);
Martin Schläffer committed
65
  s.x0 ^= PAD(mlen);
lwc-tester committed
66
  c += mlen;
Martin Schläffer committed
67
  printstate("process plaintext", &s);
lwc-tester committed
68

Enrico Pozzobon committed
69
  /* finalize */
lwc-tester committed
70 71 72 73 74
  s.x1 ^= K0;
  s.x2 ^= K1;
  P12(&s);
  s.x3 ^= K0;
  s.x4 ^= K1;
Martin Schläffer committed
75
  printstate("finalization", &s);
lwc-tester committed
76

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

  return 0;
}