encrypt.c 2.15 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_encrypt(uint8_t* c, uint64_t* clen, const uint8_t* m,
                        uint64_t mlen, const uint8_t* ad, uint64_t adlen,
                        const uint8_t* nsec, const uint8_t* npub,
                        const uint8_t* k) {
  uint64_t K0, K1, N0, N1;
  state_t s;
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 */
Martin Schläffer committed
19 20 21 22
  K0 = LOADBYTES(k, 8);
  K1 = LOADBYTES(k + 8, 8);
  N0 = LOADBYTES(npub, 8);
  N1 = LOADBYTES(npub + 8, 8);
Martin Schläffer committed
23 24 25

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

Martin Schläffer committed
35
  /* process associated data */
lwc-tester committed
36
  if (adlen) {
Martin Schläffer committed
37
    while (adlen >= ASCON_128A_RATE) {
Martin Schläffer committed
38 39
      s.x0 ^= LOADBYTES(ad, 8);
      s.x1 ^= LOADBYTES(ad + 8, 8);
lwc-tester committed
40
      P8(&s);
Martin Schläffer committed
41 42
      ad += ASCON_128A_RATE;
      adlen -= ASCON_128A_RATE;
lwc-tester committed
43
    }
Martin Schläffer committed
44
    /* final associated data block */
lwc-tester committed
45
    if (adlen >= 8) {
Martin Schläffer committed
46 47
      s.x0 ^= LOADBYTES(ad, 8);
      s.x1 ^= LOADBYTES(ad + 8, adlen - 8);
Martin Schläffer committed
48
      s.x1 ^= PAD(adlen - 8);
lwc-tester committed
49
    } else {
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 55
    }
    P8(&s);
  }
  s.x4 ^= 1;
Martin Schläffer committed
56
  printstate("process associated data", &s);
lwc-tester committed
57

Martin Schläffer committed
58 59
  /* process plaintext */
  while (mlen >= ASCON_128A_RATE) {
Martin Schläffer committed
60 61 62 63
    s.x0 ^= LOADBYTES(m, 8);
    s.x1 ^= LOADBYTES(m + 8, 8);
    STOREBYTES(c, s.x0, 8);
    STOREBYTES(c + 8, s.x1, 8);
lwc-tester committed
64
    P8(&s);
Martin Schläffer committed
65 66 67
    m += ASCON_128A_RATE;
    c += ASCON_128A_RATE;
    mlen -= ASCON_128A_RATE;
lwc-tester committed
68
  }
Martin Schläffer committed
69
  /* final plaintext block */
lwc-tester committed
70
  if (mlen >= 8) {
Martin Schläffer committed
71 72 73 74
    s.x0 ^= LOADBYTES(m, 8);
    s.x1 ^= LOADBYTES(m + 8, mlen - 8);
    STOREBYTES(c, s.x0, 8);
    STOREBYTES(c + 8, s.x1, mlen - 8);
Martin Schläffer committed
75
    s.x1 ^= PAD(mlen - 8);
lwc-tester committed
76
  } else {
Martin Schläffer committed
77 78
    s.x0 ^= LOADBYTES(m, mlen);
    STOREBYTES(c, s.x0, mlen);
Martin Schläffer committed
79
    s.x0 ^= PAD(mlen);
lwc-tester committed
80 81
  }
  c += mlen;
Martin Schläffer committed
82
  printstate("process plaintext", &s);
lwc-tester committed
83

Martin Schläffer committed
84
  /* finalization */
lwc-tester committed
85 86 87 88 89
  s.x2 ^= K0;
  s.x3 ^= K1;
  P12(&s);
  s.x3 ^= K0;
  s.x4 ^= K1;
Martin Schläffer committed
90
  printstate("finalization", &s);
lwc-tester committed
91

Martin Schläffer committed
92
  /* set tag */
Martin Schläffer committed
93 94
  STOREBYTES(c, s.x3, 8);
  STOREBYTES(c + 8, s.x4, 8);
lwc-tester committed
95 96 97

  return 0;
}