aead.c 6.14 KB
Newer Older
Martin Schläffer committed
1 2
#include "api.h"
#include "ascon.h"
Enrico Pozzobon committed
3
#include "crypto_aead.h"
Martin Schläffer committed
4 5 6
#include "permutations.h"
#include "printstate.h"

Enrico Pozzobon committed
7 8 9 10 11 12 13
#if !ASCON_INLINE_MODE
#undef forceinline
#define forceinline
#endif

forceinline void ascon_loadkey(word_t* K0, word_t* K1, word_t* K2,
                               const uint8_t* k) {
Martin Schläffer committed
14
  KINIT(K0, K1, K2);
Enrico Pozzobon committed
15 16 17 18
  if (CRYPTO_KEYBYTES == 16) {
    *K1 = XOR(*K1, LOAD(k, 8));
    *K2 = XOR(*K2, LOAD(k + 8, 8));
  }
Martin Schläffer committed
19
  if (CRYPTO_KEYBYTES == 20) {
Enrico Pozzobon committed
20 21 22
    *K0 = XOR(*K0, KEYROT(WORD_T(0), LOADBYTES(k, 4)));
    *K1 = XOR(*K1, LOADBYTES(k + 4, 8));
    *K2 = XOR(*K2, LOADBYTES(k + 12, 8));
Martin Schläffer committed
23 24 25
  }
}

Enrico Pozzobon committed
26 27
forceinline void ascon_aeadinit(state_t* s, const uint8_t* npub,
                                const uint8_t* k) {
Martin Schläffer committed
28
  /* load nonce */
Enrico Pozzobon committed
29 30 31 32 33 34
  word_t N0 = LOAD(npub, 8);
  word_t N1 = LOAD(npub + 8, 8);
  /* load key */
  word_t K0, K1, K2;
  ascon_loadkey(&K0, &K1, &K2, k);
  /* initialize */
Martin Schläffer committed
35
  PINIT(s);
Enrico Pozzobon committed
36 37 38 39 40
  if (CRYPTO_KEYBYTES == 16 && ASCON_AEAD_RATE == 8)
    s->x0 = XOR(s->x0, ASCON_128_IV);
  if (CRYPTO_KEYBYTES == 16 && ASCON_AEAD_RATE == 16)
    s->x0 = XOR(s->x0, ASCON_128A_IV);
  if (CRYPTO_KEYBYTES == 20) s->x0 = XOR(s->x0, ASCON_80PQ_IV);
Martin Schläffer committed
41 42 43 44 45
  if (CRYPTO_KEYBYTES == 20) s->x0 = XOR(s->x0, K0);
  s->x1 = XOR(s->x1, K1);
  s->x2 = XOR(s->x2, K2);
  s->x3 = XOR(s->x3, N0);
  s->x4 = XOR(s->x4, N1);
Enrico Pozzobon committed
46
  P(s, 12);
Martin Schläffer committed
47 48 49 50 51 52
  if (CRYPTO_KEYBYTES == 20) s->x2 = XOR(s->x2, K0);
  s->x3 = XOR(s->x3, K1);
  s->x4 = XOR(s->x4, K2);
  printstate("initialization", s);
}

Enrico Pozzobon committed
53 54
forceinline void ascon_adata(state_t* s, const uint8_t* ad, uint64_t adlen) {
  const int nr = (ASCON_AEAD_RATE == 8) ? 6 : 8;
Martin Schläffer committed
55
  if (adlen) {
Enrico Pozzobon committed
56 57 58 59 60 61 62
    /* full associated data blocks */
    while (adlen >= ASCON_AEAD_RATE) {
      s->x0 = XOR(s->x0, LOAD(ad, 8));
      if (ASCON_AEAD_RATE == 16) s->x1 = XOR(s->x1, LOAD(ad + 8, 8));
      P(s, nr);
      ad += ASCON_AEAD_RATE;
      adlen -= ASCON_AEAD_RATE;
Martin Schläffer committed
63 64
    }
    /* final associated data block */
Enrico Pozzobon committed
65 66 67
    word_t* px = &s->x0;
    if (ASCON_AEAD_RATE == 16 && adlen >= 8) {
      s->x0 = XOR(s->x0, LOAD(ad, 8));
Martin Schläffer committed
68 69 70 71 72
      px = &s->x1;
      ad += 8;
      adlen -= 8;
    }
    *px = XOR(*px, PAD(adlen));
Enrico Pozzobon committed
73 74
    if (adlen) *px = XOR(*px, LOAD(ad, adlen));
    P(s, nr);
Martin Schläffer committed
75
  }
Enrico Pozzobon committed
76
  /* domain separation */
Martin Schläffer committed
77 78 79 80
  s->x4 = XOR(s->x4, WORD_T(1));
  printstate("process associated data", s);
}

Enrico Pozzobon committed
81 82 83 84 85 86 87 88 89 90
forceinline void ascon_encrypt(state_t* s, uint8_t* c, const uint8_t* m,
                               uint64_t mlen) {
  const int nr = (ASCON_AEAD_RATE == 8) ? 6 : 8;
  /* full plaintext blocks */
  while (mlen >= ASCON_AEAD_RATE) {
    s->x0 = XOR(s->x0, LOAD(m, 8));
    STORE(c, s->x0, 8);
    if (ASCON_AEAD_RATE == 16) {
      s->x1 = XOR(s->x1, LOAD(m + 8, 8));
      STORE(c + 8, s->x1, 8);
Martin Schläffer committed
91
    }
Enrico Pozzobon committed
92 93 94 95
    P(s, nr);
    m += ASCON_AEAD_RATE;
    c += ASCON_AEAD_RATE;
    mlen -= ASCON_AEAD_RATE;
Martin Schläffer committed
96 97
  }
  /* final plaintext block */
Enrico Pozzobon committed
98 99 100 101
  word_t* px = &s->x0;
  if (ASCON_AEAD_RATE == 16 && mlen >= 8) {
    s->x0 = XOR(s->x0, LOAD(m, 8));
    STORE(c, s->x0, 8);
Martin Schläffer committed
102 103 104 105 106
    px = &s->x1;
    m += 8;
    c += 8;
    mlen -= 8;
  }
Enrico Pozzobon committed
107
  *px = XOR(*px, PAD(mlen));
Martin Schläffer committed
108 109 110 111 112 113 114
  if (mlen) {
    *px = XOR(*px, LOAD(m, mlen));
    STORE(c, *px, mlen);
  }
  printstate("process plaintext", s);
}

Enrico Pozzobon committed
115 116 117 118 119 120
forceinline void ascon_decrypt(state_t* s, uint8_t* m, const uint8_t* c,
                               uint64_t clen) {
  const int nr = (ASCON_AEAD_RATE == 8) ? 6 : 8;
  /* full ciphertext blocks */
  while (clen >= ASCON_AEAD_RATE) {
    word_t cx = LOAD(c, 8);
Martin Schläffer committed
121
    s->x0 = XOR(s->x0, cx);
Enrico Pozzobon committed
122
    STORE(m, s->x0, 8);
Martin Schläffer committed
123
    s->x0 = cx;
Enrico Pozzobon committed
124 125
    if (ASCON_AEAD_RATE == 16) {
      cx = LOAD(c + 8, 8);
Martin Schläffer committed
126
      s->x1 = XOR(s->x1, cx);
Enrico Pozzobon committed
127
      STORE(m + 8, s->x1, 8);
Martin Schläffer committed
128 129
      s->x1 = cx;
    }
Enrico Pozzobon committed
130 131 132 133
    P(s, nr);
    m += ASCON_AEAD_RATE;
    c += ASCON_AEAD_RATE;
    clen -= ASCON_AEAD_RATE;
Martin Schläffer committed
134 135
  }
  /* final ciphertext block */
Enrico Pozzobon committed
136 137 138
  word_t* px = &s->x0;
  if (ASCON_AEAD_RATE == 16 && clen >= 8) {
    word_t cx = LOAD(c, 8);
Martin Schläffer committed
139
    s->x0 = XOR(s->x0, cx);
Enrico Pozzobon committed
140
    STORE(m, s->x0, 8);
Martin Schläffer committed
141 142 143 144 145 146
    s->x0 = cx;
    px = &s->x1;
    m += 8;
    c += 8;
    clen -= 8;
  }
Enrico Pozzobon committed
147
  *px = XOR(*px, PAD(clen));
Martin Schläffer committed
148
  if (clen) {
Enrico Pozzobon committed
149
    word_t cx = LOAD(c, clen);
Martin Schläffer committed
150 151 152 153 154 155 156 157
    *px = XOR(*px, cx);
    STORE(m, *px, clen);
    *px = CLEAR(*px, clen);
    *px = XOR(*px, cx);
  }
  printstate("process ciphertext", s);
}

Enrico Pozzobon committed
158 159 160 161 162 163
forceinline void ascon_final(state_t* s, const uint8_t* k) {
  /* load key */
  word_t K0, K1, K2;
  ascon_loadkey(&K0, &K1, &K2, k);
  /* finalize */
  if (CRYPTO_KEYBYTES == 16 && ASCON_AEAD_RATE == 8) {
Martin Schläffer committed
164 165 166
    s->x1 = XOR(s->x1, K1);
    s->x2 = XOR(s->x2, K2);
  }
Enrico Pozzobon committed
167
  if (CRYPTO_KEYBYTES == 16 && ASCON_AEAD_RATE == 16) {
Martin Schläffer committed
168 169 170 171 172 173 174 175
    s->x2 = XOR(s->x2, K1);
    s->x3 = XOR(s->x3, K2);
  }
  if (CRYPTO_KEYBYTES == 20) {
    s->x1 = XOR(s->x1, KEYROT(K0, K1));
    s->x2 = XOR(s->x2, KEYROT(K1, K2));
    s->x3 = XOR(s->x3, KEYROT(K2, WORD_T(0)));
  }
Enrico Pozzobon committed
176
  P(s, 12);
Martin Schläffer committed
177 178 179 180 181
  s->x3 = XOR(s->x3, K1);
  s->x4 = XOR(s->x4, K2);
  printstate("finalization", s);
}

Enrico Pozzobon committed
182 183 184 185 186
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) {
Martin Schläffer committed
187 188 189 190
  state_t s;
  (void)nsec;
  *clen = mlen + CRYPTO_ABYTES;
  /* perform ascon computation */
Enrico Pozzobon committed
191 192 193 194
  ascon_aeadinit(&s, npub, k);
  ascon_adata(&s, ad, adlen);
  ascon_encrypt(&s, c, m, mlen);
  ascon_final(&s, k);
Martin Schläffer committed
195
  /* set tag */
Enrico Pozzobon committed
196 197
  STOREBYTES(c + mlen, s.x3, 8);
  STOREBYTES(c + mlen + 8, s.x4, 8);
Martin Schläffer committed
198 199 200
  return 0;
}

Enrico Pozzobon committed
201 202 203 204 205
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) {
Martin Schläffer committed
206 207
  state_t s;
  (void)nsec;
Enrico Pozzobon committed
208
  if (clen < CRYPTO_ABYTES) return -1;
Martin Schläffer committed
209 210
  *mlen = clen = clen - CRYPTO_ABYTES;
  /* perform ascon computation */
Enrico Pozzobon committed
211 212 213 214
  ascon_aeadinit(&s, npub, k);
  ascon_adata(&s, ad, adlen);
  ascon_decrypt(&s, m, c, clen);
  ascon_final(&s, k);
Martin Schläffer committed
215
  /* verify tag (should be constant time, check compiler output) */
Enrico Pozzobon committed
216 217 218
  s.x3 = XOR(s.x3, LOADBYTES(c + clen, 8));
  s.x4 = XOR(s.x4, LOADBYTES(c + clen + 8, 8));
  return NOTZERO(s.x3, s.x4);
Martin Schläffer committed
219
}