ascon.c 20.9 KB
Newer Older
Martin Schläffer committed
1 2
#include <stdint.h>

lwc-tester committed
3 4 5 6 7 8
#include "api.h"
#include "endian.h"

#define PA_ROUNDS 12
#define PB_ROUNDS 8

Martin Schläffer committed
9 10
#define ROR16(x, n) (((x) >> (n)) | ((x) << (16 - (n))))
#define ROL16(x, n) (((x) << (n)) | ((x) >> (16 - (n))))
lwc-tester committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

#define COMPRESS_LONG_16(x)      \
  do {                           \
    x &= 0x1111;                 \
    x = (x | (x >> 3)) & 0x0303; \
    x = (x | (x >> 6)) & 0x000f; \
  } while (0)

#define COMPRESS_U16(var, var_3, var_2, var_1, var_0) \
  do {                                                \
    /* var 16-bit, and var_0/1/2/3 4-bit */           \
    var_0 = var;                                      \
    var_1 = var_0 >> 1;                               \
    var_2 = var_1 >> 1;                               \
    var_3 = var_2 >> 1;                               \
    COMPRESS_LONG_16(var_0);                          \
    COMPRESS_LONG_16(var_1);                          \
    COMPRESS_LONG_16(var_2);                          \
    COMPRESS_LONG_16(var_3);                          \
  } while (0)

Martin Schläffer committed
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
#define COMPRESS_BYTE_ARRAY_16(a, var_3, var_2, var_1, var_0)              \
  do {                                                                     \
    COMPRESS_U16(U16BIG(((uint16_t*)(a))[3]), var_3, var_2, var_1, var_0); \
    COMPRESS_U16(U16BIG(((uint16_t*)(a))[2]), t1_3, t1_2, t1_1, t1_0);     \
    var_0 |= t1_0 << 4;                                                    \
    var_1 |= t1_1 << 4;                                                    \
    var_2 |= t1_2 << 4;                                                    \
    var_3 |= t1_3 << 4;                                                    \
    COMPRESS_U16(U16BIG(((uint16_t*)(a))[1]), t1_3, t1_2, t1_1, t1_0);     \
    var_0 |= t1_0 << 8;                                                    \
    var_1 |= t1_1 << 8;                                                    \
    var_2 |= t1_2 << 8;                                                    \
    var_3 |= t1_3 << 8;                                                    \
    COMPRESS_U16(U16BIG(((uint16_t*)(a))[0]), t1_3, t1_2, t1_1, t1_0);     \
    var_0 |= t1_0 << 12;                                                   \
    var_1 |= t1_1 << 12;                                                   \
    var_2 |= t1_2 << 12;                                                   \
    var_3 |= t1_3 << 12;                                                   \
lwc-tester committed
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
  } while (0)

#define EXPAND_SHORT_16(x)       \
  do {                           \
    x &= 0x000f;                 \
    x = (x | (x << 6)) & 0x0303; \
    x = (x | (x << 3)) & 0x1111; \
  } while (0)

#define EXPAND_U16(var, var_3, var_2, var_1, var_0)                \
  do {                                                             \
    /* var 16-bit, and var_0/1/2/3 4-bit */                        \
    t0_0 = var_0;                                                  \
    t0_1 = var_1;                                                  \
    t0_2 = var_2;                                                  \
    t0_3 = var_3;                                                  \
    EXPAND_SHORT_16(t0_0);                                         \
    EXPAND_SHORT_16(t0_1);                                         \
    EXPAND_SHORT_16(t0_2);                                         \
    EXPAND_SHORT_16(t0_3);                                         \
    *var = U16BIG(t0_0 | (t0_1 << 1) | (t0_2 << 2) | (t0_3 << 3)); \
  } while (0)

Martin Schläffer committed
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
#define EXPAND_BYTE_ARRAY_16(a, var_3, var_2, var_1, var_0)         \
  do {                                                              \
    EXPAND_U16((((uint16_t*)(a)) + 3), var_3, var_2, var_1, var_0); \
    t1_3 = var_3 >> 4;                                              \
    t1_2 = var_2 >> 4;                                              \
    t1_1 = var_1 >> 4;                                              \
    t1_0 = var_0 >> 4;                                              \
    EXPAND_U16((((uint16_t*)(a)) + 2), t1_3, t1_2, t1_1, t1_0);     \
    t1_3 >>= 4;                                                     \
    t1_2 >>= 4;                                                     \
    t1_1 >>= 4;                                                     \
    t1_0 >>= 4;                                                     \
    EXPAND_U16((((uint16_t*)(a)) + 1), t1_3, t1_2, t1_1, t1_0);     \
    t1_3 >>= 4;                                                     \
    t1_2 >>= 4;                                                     \
    t1_1 >>= 4;                                                     \
    t1_0 >>= 4;                                                     \
    EXPAND_U16((((uint16_t*)(a)) + 0), t1_3, t1_2, t1_1, t1_0);     \
lwc-tester committed
91 92
  } while (0)

Martin Schläffer committed
93 94
/* This way of implementing Ascon's S-box was inpired by personal communication
   with Joan Daemen about implementing the 3-bit chi layer. */
lwc-tester committed
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
#define ROUND_16(C_3, C_2, C_1, C_0) \
  do {                               \
    /* round constant */             \
    x2_0 ^= C_0;                     \
    x2_1 ^= C_1;                     \
    x2_2 ^= C_2;                     \
    x2_3 ^= C_3;                     \
    /* s-box layer */                \
    x0_0 ^= x4_0;                    \
    x4_0 ^= x3_0;                    \
    x2_0 ^= x1_0;                    \
    t0_0 = x0_0 & (~x4_0);           \
    t1_0 = x2_0 & (~x1_0);           \
    x0_0 ^= t1_0;                    \
    t1_0 = x4_0 & (~x3_0);           \
    x2_0 ^= t1_0;                    \
    t1_0 = x1_0 & (~x0_0);           \
    x4_0 ^= t1_0;                    \
    t1_0 = x3_0 & (~x2_0);           \
    x1_0 ^= t1_0;                    \
    x3_0 ^= t0_0;                    \
    x1_0 ^= x0_0;                    \
    x3_0 ^= x2_0;                    \
    x0_0 ^= x4_0;                    \
    x2_0 = ~x2_0;                    \
    x0_1 ^= x4_1;                    \
    x4_1 ^= x3_1;                    \
    x2_1 ^= x1_1;                    \
    t0_0 = x0_1 & (~x4_1);           \
    t1_0 = x2_1 & (~x1_1);           \
    x0_1 ^= t1_0;                    \
    t1_0 = x4_1 & (~x3_1);           \
    x2_1 ^= t1_0;                    \
    t1_0 = x1_1 & (~x0_1);           \
    x4_1 ^= t1_0;                    \
    t1_0 = x3_1 & (~x2_1);           \
    x1_1 ^= t1_0;                    \
    x3_1 ^= t0_0;                    \
    x1_1 ^= x0_1;                    \
    x3_1 ^= x2_1;                    \
    x0_1 ^= x4_1;                    \
    x2_1 = ~x2_1;                    \
    x0_2 ^= x4_2;                    \
    x4_2 ^= x3_2;                    \
    x2_2 ^= x1_2;                    \
    t0_0 = x0_2 & (~x4_2);           \
    t1_0 = x2_2 & (~x1_2);           \
    x0_2 ^= t1_0;                    \
    t1_0 = x4_2 & (~x3_2);           \
    x2_2 ^= t1_0;                    \
    t1_0 = x1_2 & (~x0_2);           \
    x4_2 ^= t1_0;                    \
    t1_0 = x3_2 & (~x2_2);           \
    x1_2 ^= t1_0;                    \
    x3_2 ^= t0_0;                    \
    x1_2 ^= x0_2;                    \
    x3_2 ^= x2_2;                    \
    x0_2 ^= x4_2;                    \
    x2_2 = ~x2_2;                    \
    x0_3 ^= x4_3;                    \
    x4_3 ^= x3_3;                    \
    x2_3 ^= x1_3;                    \
    t0_0 = x0_3 & (~x4_3);           \
    t1_0 = x2_3 & (~x1_3);           \
    x0_3 ^= t1_0;                    \
    t1_0 = x4_3 & (~x3_3);           \
    x2_3 ^= t1_0;                    \
    t1_0 = x1_3 & (~x0_3);           \
    x4_3 ^= t1_0;                    \
    t1_0 = x3_3 & (~x2_3);           \
    x1_3 ^= t1_0;                    \
    x3_3 ^= t0_0;                    \
    x1_3 ^= x0_3;                    \
    x3_3 ^= x2_3;                    \
    x0_3 ^= x4_3;                    \
    x2_3 = ~x2_3;                    \
    /* linear layer */               \
    t0_0 = x0_0;                     \
    t0_1 = x0_1;                     \
    t0_2 = x0_2;                     \
    t0_3 = x0_3;                     \
Martin Schläffer committed
176 177 178 179 180 181 182 183
    x0_1 ^= ROR16(t0_0, 5);          \
    x0_2 ^= ROR16(t0_1, 5);          \
    x0_3 ^= ROR16(t0_2, 5);          \
    x0_0 ^= ROR16(t0_3, 4);          \
    x0_0 ^= ROR16(t0_0, 7);          \
    x0_1 ^= ROR16(t0_1, 7);          \
    x0_2 ^= ROR16(t0_2, 7);          \
    x0_3 ^= ROR16(t0_3, 7);          \
lwc-tester committed
184 185 186 187 188
    t0_0 = x1_0;                     \
    t0_1 = x1_1;                     \
    t0_2 = x1_2;                     \
    t0_3 = x1_3;                     \
    x1_3 ^= t0_0;                    \
Martin Schläffer committed
189 190 191 192 193 194 195
    x1_0 ^= ROL16(t0_1, 1);          \
    x1_1 ^= ROL16(t0_2, 1);          \
    x1_2 ^= ROL16(t0_3, 1);          \
    x1_1 ^= ROL16(t0_0, 6);          \
    x1_2 ^= ROL16(t0_1, 6);          \
    x1_3 ^= ROL16(t0_2, 6);          \
    x1_0 ^= ROL16(t0_3, 7);          \
lwc-tester committed
196 197 198 199
    t0_0 = x2_0;                     \
    t0_1 = x2_1;                     \
    t0_2 = x2_2;                     \
    t0_3 = x2_3;                     \
Martin Schläffer committed
200
    x2_3 ^= ROR16(t0_0, 1);          \
lwc-tester committed
201 202 203
    x2_0 ^= t0_1;                    \
    x2_1 ^= t0_2;                    \
    x2_2 ^= t0_3;                    \
Martin Schläffer committed
204 205 206 207
    x2_2 ^= ROR16(t0_0, 2);          \
    x2_3 ^= ROR16(t0_1, 2);          \
    x2_0 ^= ROR16(t0_2, 1);          \
    x2_1 ^= ROR16(t0_3, 1);          \
lwc-tester committed
208 209 210 211
    t0_0 = x3_0;                     \
    t0_1 = x3_1;                     \
    t0_2 = x3_2;                     \
    t0_3 = x3_3;                     \
Martin Schläffer committed
212 213 214 215 216 217 218 219
    x3_2 ^= ROR16(t0_0, 3);          \
    x3_3 ^= ROR16(t0_1, 3);          \
    x3_0 ^= ROR16(t0_2, 2);          \
    x3_1 ^= ROR16(t0_3, 2);          \
    x3_3 ^= ROR16(t0_0, 5);          \
    x3_0 ^= ROR16(t0_1, 4);          \
    x3_1 ^= ROR16(t0_2, 4);          \
    x3_2 ^= ROR16(t0_3, 4);          \
lwc-tester committed
220 221 222 223
    t0_0 = x4_0;                     \
    t0_1 = x4_1;                     \
    t0_2 = x4_2;                     \
    t0_3 = x4_3;                     \
Martin Schläffer committed
224 225 226 227 228 229 230 231
    x4_1 ^= ROR16(t0_0, 2);          \
    x4_2 ^= ROR16(t0_1, 2);          \
    x4_3 ^= ROR16(t0_2, 2);          \
    x4_0 ^= ROR16(t0_3, 1);          \
    x4_3 ^= ROL16(t0_0, 5);          \
    x4_0 ^= ROL16(t0_1, 6);          \
    x4_1 ^= ROL16(t0_2, 6);          \
    x4_2 ^= ROL16(t0_3, 6);          \
lwc-tester committed
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
  } while (0)

#define P12_16            \
  do {                    \
    ROUND_16(2, 2, 2, 2); \
    ROUND_16(2, 2, 2, 1); \
    ROUND_16(2, 2, 1, 2); \
    ROUND_16(2, 2, 1, 1); \
    ROUND_16(2, 1, 2, 2); \
    ROUND_16(2, 1, 2, 1); \
    ROUND_16(2, 1, 1, 2); \
    ROUND_16(2, 1, 1, 1); \
    ROUND_16(1, 2, 2, 2); \
    ROUND_16(1, 2, 2, 1); \
    ROUND_16(1, 2, 1, 2); \
    ROUND_16(1, 2, 1, 1); \
  } while (0)

#define P8_16             \
  do {                    \
    ROUND_16(2, 1, 2, 2); \
    ROUND_16(2, 1, 2, 1); \
    ROUND_16(2, 1, 1, 2); \
    ROUND_16(2, 1, 1, 1); \
    ROUND_16(1, 2, 2, 2); \
    ROUND_16(1, 2, 2, 1); \
    ROUND_16(1, 2, 1, 2); \
    ROUND_16(1, 2, 1, 1); \
  } while (0)

Martin Schläffer committed
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
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 rlen;
  uint64_t i;

  uint8_t buffer[16];

  uint16_t K0_0;
  uint16_t K1_0;
  uint16_t N0_0;
  uint16_t N1_0;
  uint16_t x0_0, x1_0, x2_0, x3_0, x4_0;
  uint16_t t0_0, t1_0;

  uint16_t K0_1;
  uint16_t K1_1;
  uint16_t N0_1;
  uint16_t N1_1;
  uint16_t x0_1, x1_1, x2_1, x3_1, x4_1;
  uint16_t t0_1, t1_1;

  uint16_t K0_2;
  uint16_t K1_2;
  uint16_t N0_2;
  uint16_t N1_2;
  uint16_t x0_2, x1_2, x2_2, x3_2, x4_2;
  uint16_t t0_2, t1_2;

  uint16_t K0_3;
  uint16_t K1_3;
  uint16_t N0_3;
  uint16_t N1_3;
  uint16_t x0_3, x1_3, x2_3, x3_3, x4_3;
  uint16_t t0_3, t1_3;

  uint16_t in_0, in_1, in_2, in_3;
lwc-tester committed
300 301 302 303 304 305 306 307

  (void)nsec;

  COMPRESS_BYTE_ARRAY_16(k, K0_3, K0_2, K0_1, K0_0);
  COMPRESS_BYTE_ARRAY_16(k + 8, K1_3, K1_2, K1_1, K1_0);
  COMPRESS_BYTE_ARRAY_16(npub, N0_3, N0_2, N0_1, N0_0);
  COMPRESS_BYTE_ARRAY_16(npub + 8, N1_3, N1_2, N1_1, N1_0);

Martin Schläffer committed
308 309
  /* initialization */
  t1_0 = (uint16_t)((CRYPTO_KEYBYTES * 8) << 8 | (ASCON_RATE * 8) << 0);
lwc-tester committed
310 311 312 313 314 315 316 317 318 319 320
  t1_1 = t1_0 >> 1;
  t1_2 = t1_1 >> 1;
  t1_3 = t1_2 >> 1;
  COMPRESS_LONG_16(t1_0);
  COMPRESS_LONG_16(t1_1);
  COMPRESS_LONG_16(t1_2);
  COMPRESS_LONG_16(t1_3);
  x0_0 = t1_0 << 12;
  x0_1 = t1_1 << 12;
  x0_2 = t1_2 << 12;
  x0_3 = t1_3 << 12;
Martin Schläffer committed
321
  t1_0 = (uint16_t)(PA_ROUNDS << 8 | PB_ROUNDS << 0);
lwc-tester committed
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
  t1_1 = t1_0 >> 1;
  t1_2 = t1_1 >> 1;
  t1_3 = t1_2 >> 1;
  COMPRESS_LONG_16(t1_0);
  COMPRESS_LONG_16(t1_1);
  COMPRESS_LONG_16(t1_2);
  COMPRESS_LONG_16(t1_3);
  x0_0 |= t1_0 << 8;
  x0_1 |= t1_1 << 8;
  x0_2 |= t1_2 << 8;
  x0_3 |= t1_3 << 8;
  x1_0 = K0_0;
  x1_1 = K0_1;
  x1_2 = K0_2;
  x1_3 = K0_3;
  x2_0 = K1_0;
  x2_1 = K1_1;
  x2_2 = K1_2;
  x2_3 = K1_3;
  x3_0 = N0_0;
  x3_1 = N0_1;
  x3_2 = N0_2;
  x3_3 = N0_3;
  x4_0 = N1_0;
  x4_1 = N1_1;
  x4_2 = N1_2;
  x4_3 = N1_3;
  P12_16;
  x3_0 ^= K0_0;
  x3_1 ^= K0_1;
  x3_2 ^= K0_2;
  x3_3 ^= K0_3;
  x4_0 ^= K1_0;
  x4_1 ^= K1_1;
  x4_2 ^= K1_2;
  x4_3 ^= K1_3;
Martin Schläffer committed
358
  /* process associated data */
lwc-tester committed
359 360
  if (adlen) {
    rlen = adlen;
Martin Schläffer committed
361
    while (rlen >= ASCON_RATE) {
lwc-tester committed
362 363 364 365 366 367 368 369 370 371 372
      COMPRESS_BYTE_ARRAY_16(ad, in_3, in_2, in_1, in_0);
      x0_0 ^= in_0;
      x0_1 ^= in_1;
      x0_2 ^= in_2;
      x0_3 ^= in_3;
      COMPRESS_BYTE_ARRAY_16(ad + 8, in_3, in_2, in_1, in_0);
      x1_0 ^= in_0;
      x1_1 ^= in_1;
      x1_2 ^= in_2;
      x1_3 ^= in_3;
      P8_16;
Martin Schläffer committed
373 374
      rlen -= ASCON_RATE;
      ad += ASCON_RATE;
lwc-tester committed
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
    }
    for (i = 0; i < rlen; ++i, ++ad) buffer[i] = *ad;
    buffer[rlen] = 0x80;
    for (i = rlen + 1; i < 16; ++i) buffer[i] = 0;
    COMPRESS_BYTE_ARRAY_16(buffer, in_3, in_2, in_1, in_0);
    x0_0 ^= in_0;
    x0_1 ^= in_1;
    x0_2 ^= in_2;
    x0_3 ^= in_3;
    COMPRESS_BYTE_ARRAY_16(buffer + 8, in_3, in_2, in_1, in_0);
    x1_0 ^= in_0;
    x1_1 ^= in_1;
    x1_2 ^= in_2;
    x1_3 ^= in_3;
    P8_16;
  }
  x4_0 ^= 1;

Martin Schläffer committed
393
  /* process plaintext */
lwc-tester committed
394
  rlen = mlen;
Martin Schläffer committed
395
  while (rlen >= ASCON_RATE) {
lwc-tester committed
396 397 398 399 400 401 402 403 404 405 406 407 408
    COMPRESS_BYTE_ARRAY_16(m, in_3, in_2, in_1, in_0);
    x0_0 ^= in_0;
    x0_1 ^= in_1;
    x0_2 ^= in_2;
    x0_3 ^= in_3;
    COMPRESS_BYTE_ARRAY_16(m + 8, in_3, in_2, in_1, in_0);
    x1_0 ^= in_0;
    x1_1 ^= in_1;
    x1_2 ^= in_2;
    x1_3 ^= in_3;
    EXPAND_BYTE_ARRAY_16(c, x0_3, x0_2, x0_1, x0_0);
    EXPAND_BYTE_ARRAY_16(c + 8, x1_3, x1_2, x1_1, x1_0);
    P8_16;
Martin Schläffer committed
409 410 411
    rlen -= ASCON_RATE;
    m += ASCON_RATE;
    c += ASCON_RATE;
lwc-tester committed
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
  }
  for (i = 0; i < rlen; ++i, ++m) buffer[i] = *m;
  buffer[rlen] = 0x80;
  for (i = rlen + 1; i < 16; ++i) buffer[i] = 0;
  COMPRESS_BYTE_ARRAY_16(buffer, in_3, in_2, in_1, in_0);
  x0_0 ^= in_0;
  x0_1 ^= in_1;
  x0_2 ^= in_2;
  x0_3 ^= in_3;
  COMPRESS_BYTE_ARRAY_16(buffer + 8, in_3, in_2, in_1, in_0);
  x1_0 ^= in_0;
  x1_1 ^= in_1;
  x1_2 ^= in_2;
  x1_3 ^= in_3;
  EXPAND_BYTE_ARRAY_16(buffer, x0_3, x0_2, x0_1, x0_0);
  EXPAND_BYTE_ARRAY_16(buffer + 8, x1_3, x1_2, x1_1, x1_0);
  for (i = 0; i < rlen; ++i, ++c) *c = buffer[i];

Martin Schläffer committed
430
  /* finalization */
lwc-tester committed
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
  x2_0 ^= K0_0;
  x2_1 ^= K0_1;
  x2_2 ^= K0_2;
  x2_3 ^= K0_3;
  x3_0 ^= K1_0;
  x3_1 ^= K1_1;
  x3_2 ^= K1_2;
  x3_3 ^= K1_3;
  P12_16;
  x3_0 ^= K0_0;
  x3_1 ^= K0_1;
  x3_2 ^= K0_2;
  x3_3 ^= K0_3;
  x4_0 ^= K1_0;
  x4_1 ^= K1_1;
  x4_2 ^= K1_2;
  x4_3 ^= K1_3;

Martin Schläffer committed
449
  /* return tag */
lwc-tester committed
450 451 452 453 454 455 456 457
  EXPAND_BYTE_ARRAY_16(c, x3_3, x3_2, x3_1, x3_0);
  c += 8;
  EXPAND_BYTE_ARRAY_16(c, x4_3, x4_2, x4_1, x4_0);
  *clen = mlen + CRYPTO_ABYTES;

  return 0;
}

Martin Schläffer committed
458 459 460
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) {
lwc-tester committed
461 462 463
  *mlen = 0;
  if (clen < CRYPTO_ABYTES) return -1;

Martin Schläffer committed
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
  uint16_t ret_val;
  uint64_t rlen;
  uint64_t i;

  uint8_t buffer[16];

  uint16_t K0_0;
  uint16_t K1_0;
  uint16_t N0_0;
  uint16_t N1_0;
  uint16_t x0_0, x1_0, x2_0, x3_0, x4_0;
  uint16_t t0_0, t1_0;

  uint16_t K0_1;
  uint16_t K1_1;
  uint16_t N0_1;
  uint16_t N1_1;
  uint16_t x0_1, x1_1, x2_1, x3_1, x4_1;
  uint16_t t0_1, t1_1;

  uint16_t K0_2;
  uint16_t K1_2;
  uint16_t N0_2;
  uint16_t N1_2;
  uint16_t x0_2, x1_2, x2_2, x3_2, x4_2;
  uint16_t t0_2, t1_2;

  uint16_t K0_3;
  uint16_t K1_3;
  uint16_t N0_3;
  uint16_t N1_3;
  uint16_t x0_3, x1_3, x2_3, x3_3, x4_3;
  uint16_t t0_3, t1_3;

  uint16_t in_0, in_1, in_2, in_3;
lwc-tester committed
499 500 501 502 503 504 505 506

  (void)nsec;

  COMPRESS_BYTE_ARRAY_16(k, K0_3, K0_2, K0_1, K0_0);
  COMPRESS_BYTE_ARRAY_16(k + 8, K1_3, K1_2, K1_1, K1_0);
  COMPRESS_BYTE_ARRAY_16(npub, N0_3, N0_2, N0_1, N0_0);
  COMPRESS_BYTE_ARRAY_16(npub + 8, N1_3, N1_2, N1_1, N1_0);

Martin Schläffer committed
507 508
  /* initialization */
  t1_0 = (uint16_t)((CRYPTO_KEYBYTES * 8) << 8 | (ASCON_RATE * 8) << 0);
lwc-tester committed
509 510 511 512 513 514 515 516 517 518 519
  t1_1 = t1_0 >> 1;
  t1_2 = t1_1 >> 1;
  t1_3 = t1_2 >> 1;
  COMPRESS_LONG_16(t1_0);
  COMPRESS_LONG_16(t1_1);
  COMPRESS_LONG_16(t1_2);
  COMPRESS_LONG_16(t1_3);
  x0_0 = t1_0 << 12;
  x0_1 = t1_1 << 12;
  x0_2 = t1_2 << 12;
  x0_3 = t1_3 << 12;
Martin Schläffer committed
520
  t1_0 = (uint16_t)(PA_ROUNDS << 8 | PB_ROUNDS << 0);
lwc-tester committed
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
  t1_1 = t1_0 >> 1;
  t1_2 = t1_1 >> 1;
  t1_3 = t1_2 >> 1;
  COMPRESS_LONG_16(t1_0);
  COMPRESS_LONG_16(t1_1);
  COMPRESS_LONG_16(t1_2);
  COMPRESS_LONG_16(t1_3);
  x0_0 |= t1_0 << 8;
  x0_1 |= t1_1 << 8;
  x0_2 |= t1_2 << 8;
  x0_3 |= t1_3 << 8;
  x1_0 = K0_0;
  x1_1 = K0_1;
  x1_2 = K0_2;
  x1_3 = K0_3;
  x2_0 = K1_0;
  x2_1 = K1_1;
  x2_2 = K1_2;
  x2_3 = K1_3;
  x3_0 = N0_0;
  x3_1 = N0_1;
  x3_2 = N0_2;
  x3_3 = N0_3;
  x4_0 = N1_0;
  x4_1 = N1_1;
  x4_2 = N1_2;
  x4_3 = N1_3;
  P12_16;
  x3_0 ^= K0_0;
  x3_1 ^= K0_1;
  x3_2 ^= K0_2;
  x3_3 ^= K0_3;
  x4_0 ^= K1_0;
  x4_1 ^= K1_1;
  x4_2 ^= K1_2;
  x4_3 ^= K1_3;
Martin Schläffer committed
557
  /* process associated data */
lwc-tester committed
558 559
  if (adlen) {
    rlen = adlen;
Martin Schläffer committed
560
    while (rlen >= ASCON_RATE) {
lwc-tester committed
561 562 563 564 565 566 567 568 569 570 571
      COMPRESS_BYTE_ARRAY_16(ad, in_3, in_2, in_1, in_0);
      x0_0 ^= in_0;
      x0_1 ^= in_1;
      x0_2 ^= in_2;
      x0_3 ^= in_3;
      COMPRESS_BYTE_ARRAY_16(ad + 8, in_3, in_2, in_1, in_0);
      x1_0 ^= in_0;
      x1_1 ^= in_1;
      x1_2 ^= in_2;
      x1_3 ^= in_3;
      P8_16;
Martin Schläffer committed
572 573
      rlen -= ASCON_RATE;
      ad += ASCON_RATE;
lwc-tester committed
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
    }
    for (i = 0; i < rlen; ++i, ++ad) buffer[i] = *ad;
    buffer[rlen] = 0x80;
    for (i = rlen + 1; i < 16; ++i) buffer[i] = 0;
    COMPRESS_BYTE_ARRAY_16(buffer, in_3, in_2, in_1, in_0);
    x0_0 ^= in_0;
    x0_1 ^= in_1;
    x0_2 ^= in_2;
    x0_3 ^= in_3;
    COMPRESS_BYTE_ARRAY_16(buffer + 8, in_3, in_2, in_1, in_0);
    x1_0 ^= in_0;
    x1_1 ^= in_1;
    x1_2 ^= in_2;
    x1_3 ^= in_3;
    P8_16;
  }
  x4_0 ^= 1;

Martin Schläffer committed
592
  /* process plaintext */
lwc-tester committed
593
  rlen = clen - CRYPTO_KEYBYTES;
Martin Schläffer committed
594
  while (rlen >= ASCON_RATE) {
lwc-tester committed
595 596 597 598
    EXPAND_U16(&t1_0, x0_3, x0_2, x0_1, x0_0);
    EXPAND_U16(&t1_1, x0_3 >> 4, x0_2 >> 4, x0_1 >> 4, x0_0 >> 4);
    EXPAND_U16(&t1_2, x0_3 >> 8, x0_2 >> 8, x0_1 >> 8, x0_0 >> 8);
    EXPAND_U16(&t1_3, x0_3 >> 12, x0_2 >> 12, x0_1 >> 12, x0_0 >> 12);
Martin Schläffer committed
599 600 601 602
    ((uint16_t*)m)[0] = (t1_3) ^ ((uint16_t*)c)[0];
    ((uint16_t*)m)[1] = (t1_2) ^ ((uint16_t*)c)[1];
    ((uint16_t*)m)[2] = (t1_1) ^ ((uint16_t*)c)[2];
    ((uint16_t*)m)[3] = (t1_0) ^ ((uint16_t*)c)[3];
lwc-tester committed
603 604 605 606
    EXPAND_U16(&t1_0, x1_3, x1_2, x1_1, x1_0);
    EXPAND_U16(&t1_1, x1_3 >> 4, x1_2 >> 4, x1_1 >> 4, x1_0 >> 4);
    EXPAND_U16(&t1_2, x1_3 >> 8, x1_2 >> 8, x1_1 >> 8, x1_0 >> 8);
    EXPAND_U16(&t1_3, x1_3 >> 12, x1_2 >> 12, x1_1 >> 12, x1_0 >> 12);
Martin Schläffer committed
607 608 609 610
    ((uint16_t*)m)[4] = (t1_3) ^ ((uint16_t*)c)[4];
    ((uint16_t*)m)[5] = (t1_2) ^ ((uint16_t*)c)[5];
    ((uint16_t*)m)[6] = (t1_1) ^ ((uint16_t*)c)[6];
    ((uint16_t*)m)[7] = (t1_0) ^ ((uint16_t*)c)[7];
lwc-tester committed
611 612 613
    COMPRESS_BYTE_ARRAY_16(c, x0_3, x0_2, x0_1, x0_0);
    COMPRESS_BYTE_ARRAY_16(c + 8, x1_3, x1_2, x1_1, x1_0);
    P8_16;
Martin Schläffer committed
614 615 616
    rlen -= ASCON_RATE;
    m += ASCON_RATE;
    c += ASCON_RATE;
lwc-tester committed
617 618 619 620 621 622 623 624 625 626 627 628
  }
  EXPAND_BYTE_ARRAY_16(buffer, x0_3, x0_2, x0_1, x0_0);
  EXPAND_BYTE_ARRAY_16(buffer + 8, x1_3, x1_2, x1_1, x1_0);
  for (i = 0; i < rlen; ++i, ++m, ++c) {
    *m = buffer[i] ^ *c;
    buffer[i] = *c;
  }
  buffer[rlen] ^= 0x80;

  COMPRESS_BYTE_ARRAY_16(buffer, x0_3, x0_2, x0_1, x0_0);
  COMPRESS_BYTE_ARRAY_16(buffer + 8, x1_3, x1_2, x1_1, x1_0);

Martin Schläffer committed
629
  /* finalization */
lwc-tester committed
630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
  x2_0 ^= K0_0;
  x2_1 ^= K0_1;
  x2_2 ^= K0_2;
  x2_3 ^= K0_3;
  x3_0 ^= K1_0;
  x3_1 ^= K1_1;
  x3_2 ^= K1_2;
  x3_3 ^= K1_3;
  P12_16;
  x3_0 ^= K0_0;
  x3_1 ^= K0_1;
  x3_2 ^= K0_2;
  x3_3 ^= K0_3;
  x4_0 ^= K1_0;
  x4_1 ^= K1_1;
  x4_2 ^= K1_2;
  x4_3 ^= K1_3;

Martin Schläffer committed
648
  /* return -1 if verification fails */
lwc-tester committed
649 650 651 652 653 654
  ret_val = 0;
  EXPAND_U16(&t1_0, x3_3, x3_2, x3_1, x3_0);
  EXPAND_U16(&t1_1, x3_3 >> 4, x3_2 >> 4, x3_1 >> 4, x3_0 >> 4);
  EXPAND_U16(&t1_2, x3_3 >> 8, x3_2 >> 8, x3_1 >> 8, x3_0 >> 8);
  EXPAND_U16(&t1_3, x3_3 >> 12, x3_2 >> 12, x3_1 >> 12, x3_0 >> 12);

Martin Schläffer committed
655 656 657 658
  ret_val |= ((uint16_t*)c)[0] ^ (t1_3);
  ret_val |= ((uint16_t*)c)[1] ^ (t1_2);
  ret_val |= ((uint16_t*)c)[2] ^ (t1_1);
  ret_val |= ((uint16_t*)c)[3] ^ (t1_0);
lwc-tester committed
659 660 661 662 663 664

  EXPAND_U16(&t1_0, x4_3, x4_2, x4_1, x4_0);
  EXPAND_U16(&t1_1, x4_3 >> 4, x4_2 >> 4, x4_1 >> 4, x4_0 >> 4);
  EXPAND_U16(&t1_2, x4_3 >> 8, x4_2 >> 8, x4_1 >> 8, x4_0 >> 8);
  EXPAND_U16(&t1_3, x4_3 >> 12, x4_2 >> 12, x4_1 >> 12, x4_0 >> 12);

Martin Schläffer committed
665 666 667 668
  ret_val |= ((uint16_t*)c)[4] ^ (t1_3);
  ret_val |= ((uint16_t*)c)[5] ^ (t1_2);
  ret_val |= ((uint16_t*)c)[6] ^ (t1_1);
  ret_val |= ((uint16_t*)c)[7] ^ (t1_0);
lwc-tester committed
669 670 671

  if (ret_val != 0) return -1;

Martin Schläffer committed
672
  /* return plaintext */
lwc-tester committed
673 674 675
  *mlen = clen - CRYPTO_ABYTES;
  return 0;
}