ascon.c 5.63 KB
Newer Older
Martin Schläffer committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
#include "ascon.h"

#include "api.h"
#include "loadstore.h"
#include "permutations.h"
#include "printstate.h"

__forceinline void loadkey(word_t* K0, word_t* K1, word_t* K2,
                           const uint8_t* k) {
  KINIT(K0, K1, K2);
  if (CRYPTO_KEYBYTES == 20) {
    XOR(*K0, KEYROT(WORD_T(0), LOAD(k, 4)));
    k += 4;
  }
  XOR(*K1, LOAD64(k));
  XOR(*K2, LOAD64(k + 8));
}

__forceinline void init(state_t* s, const uint8_t* npub, const uint8_t* k) {
  word_t N0, N1;
  word_t K0, K1, K2;
  /* load nonce */
  N0 = LOAD64(npub);
  N1 = LOAD64(npub + 8);
  /* load key */
  loadkey(&K0, &K1, &K2, k);
  /* initialization */
  PINIT(s);
  XOR(s->x0, IV);
  if (CRYPTO_KEYBYTES == 20) XOR(s->x0, K0);
  XOR(s->x1, K1);
  XOR(s->x2, K2);
  XOR(s->x3, N0);
  XOR(s->x4, N1);
  P12(s);
  if (CRYPTO_KEYBYTES == 20) XOR(s->x2, K0);
  XOR(s->x3, K1);
  XOR(s->x4, K2);
  printstate("initialization", s);
}

__forceinline void absorb(state_t* s, const uint8_t* ad, uint64_t adlen) {
  word_t* restrict px;
  /* process associated data */
  if (adlen) {
    while (adlen >= ASCON_RATE) {
      XOR(s->x0, LOAD64(ad));
      if (ASCON_RATE == 16) XOR(s->x1, LOAD64(ad + 8));
      PB(s);
      ad += ASCON_RATE;
      adlen -= ASCON_RATE;
    }
    /* final associated data block */
    px = &s->x0;
    if (ASCON_RATE == 16 && adlen >= 8) {
      XOR(s->x0, LOAD64(ad));
      px = &s->x1;
      ad += 8;
      adlen -= 8;
    }
    if (adlen) XOR(*px, LOAD(ad, adlen));
    XOR(*px, PAD(adlen));
    PB(s);
  }
  XOR(s->x4, WORD_T(1));
  printstate("process associated data", s);
}

__forceinline void encrypt(state_t* s, uint8_t* c, const uint8_t* m,
                           uint64_t mlen) {
  word_t* restrict px;
  /* process plaintext */
  while (mlen >= ASCON_RATE) {
    XOR(s->x0, LOAD64(m));
    STORE64(c, s->x0);
    if (ASCON_RATE == 16) {
      XOR(s->x1, LOAD64(m + 8));
      STORE64(c + 8, s->x1);
    }
    PB(s);
    m += ASCON_RATE;
    c += ASCON_RATE;
    mlen -= ASCON_RATE;
  }
  /* final plaintext block */
  px = &s->x0;
  if (ASCON_RATE == 16 && mlen >= 8) {
    XOR(s->x0, LOAD64(m));
    STORE64(c, s->x0);
    px = &s->x1;
    m += 8;
    c += 8;
    mlen -= 8;
  }
  if (mlen) {
    XOR(*px, LOAD(m, mlen));
    STORE(c, *px, mlen);
  }
  XOR(*px, PAD(mlen));
  printstate("process plaintext", s);
}

__forceinline void decrypt(state_t* s, uint8_t* m, const uint8_t* c,
                           uint64_t clen) {
  word_t* restrict px;
  word_t cx;
  /* process ciphertext */
  while (clen >= ASCON_RATE) {
    cx = LOAD64(c);
    XOR(s->x0, cx);
    STORE64(m, s->x0);
    s->x0 = cx;
    if (ASCON_RATE == 16) {
      cx = LOAD64(c + 8);
      XOR(s->x1, cx);
      STORE64(m + 8, s->x1);
      s->x1 = cx;
    }
    PB(s);
    m += ASCON_RATE;
    c += ASCON_RATE;
    clen -= ASCON_RATE;
  }
  /* final ciphertext block */
  px = &s->x0;
  if (ASCON_RATE == 16 && clen >= 8) {
    cx = LOAD64(c);
    XOR(s->x0, cx);
    STORE64(m, s->x0);
    s->x0 = cx;
    px = &s->x1;
    m += 8;
    c += 8;
    clen -= 8;
  }
  if (clen) {
    cx = LOAD(c, clen);
    XOR(*px, cx);
    STORE(m, *px, clen);
    AND(*px, XMASK(clen));
    XOR(*px, cx);
  }
  XOR(*px, PAD(clen));
  printstate("process ciphertext", s);
}

__forceinline void final(state_t* s, const uint8_t* k) {
  word_t K0, K1, K2;
  /* load key */
  loadkey(&K0, &K1, &K2, k);
  /* finalization */
  if (CRYPTO_KEYBYTES == 16 && ASCON_RATE == 8) {
    XOR(s->x1, K1);
    XOR(s->x2, K2);
  }
  if (CRYPTO_KEYBYTES == 16 && ASCON_RATE == 16) {
    XOR(s->x2, K1);
    XOR(s->x3, K2);
  }
  if (CRYPTO_KEYBYTES == 20) {
    XOR(s->x1, KEYROT(K0, K1));
    XOR(s->x2, KEYROT(K1, K2));
    XOR(s->x3, KEYROT(K2, WORD_T(0)));
  }
  P12(s);
  XOR(s->x3, K1);
  XOR(s->x4, K2);
  printstate("finalization", s);
}

#if !ASCON_INLINE_MODE

#define INIT ascon_init
#define ABSORB ascon_absorb
#define ENCRYPT ascon_encrypt
#define DECRYPT ascon_decrypt
#define FINAL ascon_final

void ascon_init(state_t* s, const uint8_t* npub, const uint8_t* k) {
  init(s, npub, k);
}

void ascon_absorb(state_t* s, const uint8_t* ad, uint64_t adlen) {
  absorb(s, ad, adlen);
}

void ascon_encrypt(state_t* s, uint8_t* c, const uint8_t* m, uint64_t mlen) {
  encrypt(s, c, m, mlen);
}

void ascon_decrypt(state_t* s, uint8_t* m, const uint8_t* c, uint64_t clen) {
  decrypt(s, m, c, clen);
}

void ascon_final(state_t* s, const uint8_t* k) { final(s, k); }

#else

#define INIT init
#define ABSORB absorb
#define ENCRYPT encrypt
#define DECRYPT decrypt
#define FINAL final

#endif

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) {
  state_t s;
  (void)nsec;
  *clen = mlen + CRYPTO_ABYTES;
  /* perform ascon computation */
  INIT(&s, npub, k);
  ABSORB(&s, ad, adlen);
  ENCRYPT(&s, c, m, mlen);
  FINAL(&s, k);
  /* set tag */
  c += mlen;
  STORE64(c, s.x3);
  STORE64(c + 8, s.x4);
  return 0;
}

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) {
  state_t s;
  (void)nsec;
  if (clen < CRYPTO_ABYTES) {
    *mlen = 0;
    return -1;
  }
  *mlen = clen = clen - CRYPTO_ABYTES;
  /* perform ascon computation */
  INIT(&s, npub, k);
  ABSORB(&s, ad, adlen);
  DECRYPT(&s, m, c, clen);
  FINAL(&s, k);
  /* verify tag (should be constant time, check compiler output) */
  c += clen;
  XOR(s.x3, LOAD64(c));
  XOR(s.x4, LOAD64(c + 8));
  if (NOTZERO(s.x3, s.x4)) {
    *mlen = 0;
    return -1;
  }
  return 0;
}