encrypt.c 15.1 KB
Newer Older
Johann Großschädl 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 251 252 253 254 255 256 257 258 259 260 261 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 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 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 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
///////////////////////////////////////////////////////////////////////////////
// encrypt.c: Reference C99 implementation of the AEAD algorithm SCHWAEMM.   //
// This file is part of the SPARKLE submission to NIST's LW Crypto Project.  //
// Version 1.1.2 (2020-10-30), see <http://www.cryptolux.org/> for updates.  //
// Authors: The SPARKLE Group (C. Beierle, A. Biryukov, L. Cardoso dos       //
// Santos, J. Groszschaedl, L. Perrin, A. Udovenko, V. Velichkov, Q. Wang).  //
// License: GPLv3 (see LICENSE file), other licenses available upon request. //
// Copyright (C) 2019-2020 University of Luxembourg <http://www.uni.lu/>.    //
// ------------------------------------------------------------------------- //
// This program is free software: you can redistribute it and/or modify it   //
// under the terms of the GNU General Public License as published by the     //
// Free Software Foundation, either version 3 of the License, or (at your    //
// option) any later version. This program is distributed in the hope that   //
// it will be useful, but WITHOUT ANY WARRANTY; without even the implied     //
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the  //
// GNU General Public License for more details. You should have received a   //
// copy of the GNU General Public License along with this program. If not,   //
// see <http://www.gnu.org/licenses/>.                                       //
///////////////////////////////////////////////////////////////////////////////


// This source code file should be compiled with the following set of flags:
// -std=c99 -Wall -Wextra -Wshadow -fsanitize=address,undefined -O2

// gencat_aead.c shall be used to generate the test vector output file. The
// test vector output file shall be provided in the corresponding
// crypto_aead/[algorithm]/ directory


#include <stddef.h>  // for size_t
#include <string.h>  // for memcpy, memset
#include "schwaemm_cfg.h"
#include "sparkle_ref.h"


typedef unsigned char UChar;
typedef unsigned long long int ULLInt;


#define KEY_WORDS   (SCHWAEMM_KEY_LEN/32)
#define KEY_BYTES   (SCHWAEMM_KEY_LEN/8)
#define NONCE_WORDS (SCHWAEMM_NONCE_LEN/32)
#define NONCE_BYTES (SCHWAEMM_NONCE_LEN/8)
#define TAG_WORDS   (SCHWAEMM_TAG_LEN/32)
#define TAG_BYTES   (SCHWAEMM_TAG_LEN/8)

#define STATE_BRANS (SPARKLE_STATE/64)
#define STATE_WORDS (SPARKLE_STATE/32)
#define STATE_BYTES (SPARKLE_STATE/8)
#define RATE_BRANS  (SPARKLE_RATE/64)
#define RATE_WORDS  (SPARKLE_RATE/32)
#define RATE_BYTES  (SPARKLE_RATE/8)
#define CAP_BRANS   (SPARKLE_CAPACITY/64)
#define CAP_WORDS   (SPARKLE_CAPACITY/32)
#define CAP_BYTES   (SPARKLE_CAPACITY/8)

#define CONST_A0 (((uint32_t) (0 ^ (1 << CAP_BRANS))) << 24)
#define CONST_A1 (((uint32_t) (1 ^ (1 << CAP_BRANS))) << 24)
#define CONST_M2 (((uint32_t) (2 ^ (1 << CAP_BRANS))) << 24)
#define CONST_M3 (((uint32_t) (3 ^ (1 << CAP_BRANS))) << 24)


///////////////////////////////////////////////////////////////////////////////
/////// HELPER FUNCTIONS AND MACROS (RHO1, RHO2, RATE-WHITENING, ETC.) ////////
///////////////////////////////////////////////////////////////////////////////


// The macro STATE_WORD expands to the address of the i-th word of the state,
// which is always an x-word if i is even and a y-word otherwise.

#define STATE_WORD(s, i) (((i) & 1) ? (&((s)->y[(i)/2])) : (&((s)->x[(i)/2])))


// Rho and rate-whitening for the authentication of associated data.

static void rho_whi_aut(SparkleState *state, const uint8_t *in, size_t inlen)
{
  uint32_t inbuf[RATE_WORDS] = { 0 };
  uint32_t *left_word, *right_word, tmp;  // Feistel-swap
  int i;
  
  memcpy(inbuf, in, inlen);
  if (inlen < RATE_BYTES)  // padding (only for last block)
    *(((uint8_t *) inbuf) + inlen) = 0x80;
  
  // Rho1 part1: Feistel swap of the rate-part of the state
  for (i = 0; i < RATE_BRANS; i++) {
    left_word = STATE_WORD(state, i);
    right_word = STATE_WORD(state, (RATE_BRANS + i));
    tmp = *left_word;
    *left_word = *right_word;
    *right_word ^= tmp;
  }
  // Rho1 part2: rate-part of state is XORed with assoc data
  for (i = 0; i < RATE_BRANS; i++) {
    state->x[i] ^= inbuf[2*i];
    state->y[i] ^= inbuf[2*i+1];
  }
  // Rate-whitening: capacity-part is XORed to the rate-part
  for (i = 0; i < RATE_BRANS; i++) {
    state->x[i] ^= state->x[RATE_BRANS+(i%CAP_BRANS)];
    state->y[i] ^= state->y[RATE_BRANS+(i%CAP_BRANS)];
  }
}


// Rho and rate-whitening for the encryption of plaintext.

static void rho_whi_enc(SparkleState *state, uint8_t *out, const uint8_t *in, \
                        size_t inlen)
{
  uint32_t inbuf[RATE_WORDS] = { 0 }, outbuf[RATE_WORDS];
  uint32_t *left_word, *right_word, tmp;  // Feistel-swap
  int i;
  
  memcpy(inbuf, in, inlen);
  if (inlen < RATE_BYTES)  // padding (only for last block)
    *(((uint8_t *) inbuf) + inlen) = 0x80;
  
  // Rho2: ciphertext = plaintext XOR rate-part of the state
  for (i = 0; i < RATE_BRANS; i++) {
    outbuf[2*i] = inbuf[2*i] ^ state->x[i];
    outbuf[2*i+1] = inbuf[2*i+1] ^ state->y[i];
  }
  // Rho1 part1: Feistel swap of the rate-part of the state
  for (i = 0; i < RATE_BRANS; i++) {
    left_word = STATE_WORD(state, i);
    right_word = STATE_WORD(state, (RATE_BRANS + i));
    tmp = *left_word;
    *left_word = *right_word;
    *right_word ^= tmp;
  }
  // Rho1 part2: rate-part of state is XORed with ciphertext
  for (i = 0; i < RATE_BRANS; i++) {
    state->x[i] ^= inbuf[2*i];
    state->y[i] ^= inbuf[2*i+1];
  }
  // Rate-whitening: capacity-part is XORed to the rate-part
  for (i = 0; i < RATE_BRANS; i++) {
    state->x[i] ^= state->x[RATE_BRANS+(i%CAP_BRANS)];
    state->y[i] ^= state->y[RATE_BRANS+(i%CAP_BRANS)];
  }
  memcpy(out, outbuf, inlen);
}


// Rho and rate-whitening for the decryption of ciphertext.

static void rho_whi_dec(SparkleState *state, uint8_t *out, const uint8_t *in, \
                        size_t inlen)
{
  uint32_t inbuf[RATE_WORDS] = { 0 }, outbuf[RATE_WORDS];
  SparkleState statebuf;
  uint32_t *left_word, *right_word, tmp;  // Feistel-swap
  int i;
  
  memcpy(inbuf, in, inlen);
  memcpy(&statebuf, state, sizeof(SparkleState));
  if (inlen < RATE_BYTES)  // padding (only for last block!)
    *(((uint8_t *) inbuf) + inlen) = 0x80;
  
  // Rho2': plaintext = ciphertext XOR rate-part of the state
  for (i = 0; i < RATE_BRANS; i++) {
    outbuf[2*i] = inbuf[2*i] ^ state->x[i];
    outbuf[2*i+1] = inbuf[2*i+1] ^ state->y[i];
  }
  // Rho1' part1: Feistel swap of the rate-part of the state
  for (i = 0; i < RATE_BRANS; i++) {
    left_word = STATE_WORD(state, i);
    right_word = STATE_WORD(state, (RATE_BRANS + i));
    tmp = *left_word;
    *left_word = *right_word;
    *right_word ^= tmp;
  }
  if (inlen < RATE_BYTES) {
    // padding of last block of plaintext (computed by Rho2')
    memset((((uint8_t *) outbuf) + inlen), 0, (RATE_BYTES - inlen));
    *(((uint8_t *) outbuf) + inlen) = 0x80;
    // Rho1 part2: rate-part of state is XORed with plaintext
    for (i = 0; i < RATE_BRANS; i++) {
      state->x[i] ^= outbuf[2*i];
      state->y[i] ^= outbuf[2*i+1];
    }
  } else {
    // Rho1' part2: rate-part XORed with orig rate and ciphertext
    for (i = 0; i < RATE_BRANS; i++) {
      state->x[i] ^= statebuf.x[i] ^ inbuf[2*i];
      state->y[i] ^= statebuf.y[i] ^ inbuf[2*i+1];
    }
  }
  // Rate-whitening: capacity-part is XORed to the rate-part
  for (i = 0; i < RATE_BRANS; i++) {
    state->x[i] ^= state->x[RATE_BRANS+(i%CAP_BRANS)];
    state->y[i] ^= state->y[RATE_BRANS+(i%CAP_BRANS)];
  }
  memcpy(out, outbuf, inlen);
}


///////////////////////////////////////////////////////////////////////////////
///////////// LOW-LEVEL AEAD FUNCTIONS (FOR USE WITH FELICS-AEAD) /////////////
///////////////////////////////////////////////////////////////////////////////


// The Initialize function loads nonce and key into the state and executes the
// SPARKLE permutation with the big number of steps.

void Initialize(SparkleState *state, const uint8_t *key, const uint8_t *nonce)
{
  uint32_t keybuf[KEY_WORDS], noncebuf[NONCE_WORDS];
  int i;
  
  // to prevent (potentially) unaligned memory accesses
  memcpy(keybuf, key, KEY_BYTES);
  memcpy(noncebuf, nonce, NONCE_BYTES);
  // load nonce into the rate-part of the state
  for (i = 0; i < NONCE_WORDS/2; i++) {
    state->x[i] = noncebuf[2*i];
    state->y[i] = noncebuf[2*i+1];
  }
  // load key into the capacity-part of the sate
  for (i = 0; i < KEY_WORDS/2; i++) {
    state->x[RATE_BRANS+i] = keybuf[2*i];
    state->y[RATE_BRANS+i] = keybuf[2*i+1];
  }
  // execute SPARKLE with big number of steps
  sparkle_ref(state, STATE_BRANS, SPARKLE_STEPS_BIG);
}


// The ProcessAssocData function absorbs the associated data, which becomes
// only authenticated but not encrypted, into the state (in blocks of size
// RATE_BYTES). Note that this function MUST NOT be called when the length of
// the associated data is 0.

void ProcessAssocData(SparkleState *state, const uint8_t *in, size_t inlen)
{
  // Main Authentication Loop
  
  while (inlen > RATE_BYTES) {
    // combined Rho and rate-whitening operation
    rho_whi_aut(state, in, RATE_BYTES);
    // execute SPARKLE with slim number of steps
    sparkle_ref(state, STATE_BRANS, SPARKLE_STEPS_SLIM);
    inlen -= RATE_BYTES;
    in += RATE_BYTES;
  }
  
  // Authentication of Last Block
  
  // addition of constant A0 or A1 to the state
  state->y[STATE_BRANS-1] ^= ((inlen < RATE_BYTES) ? CONST_A0 : CONST_A1);
  // combined Rho and rate-whitening operation
  rho_whi_aut(state, in, inlen);
  // execute SPARKLE with big number of steps
  sparkle_ref(state, STATE_BRANS, SPARKLE_STEPS_BIG);
}


// The ProcessPlainText function encrypts the plaintext (in blocks of size
// RATE_BYTES) and generates the respective ciphertext. The uint8_t-array 'in'
// contains the plaintext and the ciphertext is written to uint8_t-array 'out'
// ('in' and 'out' can be the same array, i.e. they can have the same start
// address). Note that this function MUST NOT be called when the length of the
// plaintext is 0.

void ProcessPlainText(SparkleState *state, uint8_t *out, const uint8_t *in, \
                      size_t inlen)
{
  // Main Encryption Loop
  
  while (inlen > RATE_BYTES) {
    // combined Rho and rate-whitening operation
    rho_whi_enc(state, out, in, RATE_BYTES);
    // execute SPARKLE with slim number of steps
    sparkle_ref(state, STATE_BRANS, SPARKLE_STEPS_SLIM);
    inlen -= RATE_BYTES;
    out += RATE_BYTES;
    in += RATE_BYTES;
  }
  
  // Encryption of Last Block
  
  // addition of constant M2 or M3 to the state
  state->y[STATE_BRANS-1] ^= ((inlen < RATE_BYTES) ? CONST_M2 : CONST_M3);
  // combined Rho and rate-whitening (incl. padding)
  rho_whi_enc(state, out, in, inlen);
  // execute SPARKLE with big number of steps
  sparkle_ref(state, STATE_BRANS, SPARKLE_STEPS_BIG);
}


// The Finalize function adds the key to the capacity part of the state.

void Finalize(SparkleState *state, const uint8_t *key)
{
  uint32_t keybuf[KEY_WORDS];
  int i;
  
  // to prevent (potentially) unaligned memory accesses
  memcpy(keybuf, key, KEY_BYTES);
  // add key to the capacity-part of the state
  for (i = 0; i < KEY_WORDS/2; i++) {
    state->x[RATE_BRANS+i] ^= keybuf[2*i];
    state->y[RATE_BRANS+i] ^= keybuf[2*i+1];
  }
}


// The GenerateTag function generates an authentication tag.

void GenerateTag(SparkleState *state, uint8_t *tag)
{
  uint32_t tagbuf[TAG_WORDS];
  int i;
  
  for (i = 0; i < TAG_WORDS/2; i++) {
    tagbuf[2*i] = state->x[RATE_BRANS+i];
    tagbuf[2*i+1] = state->y[RATE_BRANS+i];
  }
  memcpy(tag, tagbuf, TAG_BYTES);
}


// The VerifyTag function checks whether the given authentication tag is valid.
// It performs a simple constant-time comparison and returns 0 if the provided
// tag matches the computed tag and -1 otherwise.

int VerifyTag(SparkleState *state, const uint8_t *tag)
{
  uint32_t tagbuf[TAG_WORDS], diff = 0;
  int i;
  
  // to prevent (potentially) unaligned memory accesses
  memcpy(tagbuf, tag, TAG_BYTES);
  // constant-time comparison: 0 if equal, -1 otherwise
  for (i = 0; i < TAG_WORDS/2; i++) {
    diff |= (state->x[RATE_BRANS+i] ^ tagbuf[2*i]);
    diff |= (state->y[RATE_BRANS+i] ^ tagbuf[2*i+1]);
  }
  
  return (((int) (diff == 0)) - 1);
}


// The ProcessCipherText function decrypts the ciphertext (in blocks of size
// RATE_BYTES) and generates the respective plaintext. The uint8_t-array 'in'
// contains the ciphertext and the plaintext is written to uint8_t-array 'out'
// ('in' and 'out' can be the same array, i.e. they can have the same start
// address). Note that this function MUST NOT be called when the length of the
// ciphertext is 0.

void ProcessCipherText(SparkleState *state, uint8_t *out, const uint8_t *in, \
                       size_t inlen)
{
  // Main Decryption Loop
  
  while (inlen > RATE_BYTES) {
    // combined Rho and rate-whitening operation
    rho_whi_dec(state, out, in, RATE_BYTES);
    // execute SPARKLE with slim number of steps
    sparkle_ref(state, STATE_BRANS, SPARKLE_STEPS_SLIM);
    inlen -= RATE_BYTES;
    out += RATE_BYTES;
    in += RATE_BYTES;
  }
  
  // Decryption of Last Block
  
  // addition of constant M2 or M3 to the state
  state->y[STATE_BRANS-1] ^= ((inlen < RATE_BYTES) ? CONST_M2 : CONST_M3);
  // combined Rho and rate-whitening (incl. padding)
  rho_whi_dec(state, out, in, inlen);
  // execute SPARKLE with big number of steps
  sparkle_ref(state, STATE_BRANS, SPARKLE_STEPS_BIG);
}


///////////////////////////////////////////////////////////////////////////////
////////////// HIGH-LEVEL AEAD FUNCTIONS (FOR USE WITH SUPERCOP) //////////////
///////////////////////////////////////////////////////////////////////////////


// High-level encryption function from SUPERCOP.
// nsec is kept for compatibility with SUPERCOP, but is not used.

int crypto_aead_encrypt(UChar *c, ULLInt *clen, const UChar *m, ULLInt mlen, \
  const UChar *ad, ULLInt adlen, const UChar *nsec, const UChar *npub,       \
  const UChar *k)
{
  SparkleState state;
  size_t msize = (size_t) mlen;
  size_t adsize = (size_t) adlen;
  
  Initialize(&state, k, npub);
  if (adsize) ProcessAssocData(&state, ad, adsize);
  if (msize) ProcessPlainText(&state, c, m, msize);
  Finalize(&state, k);
  GenerateTag(&state, (c + msize));
  *clen = msize;
  *clen += TAG_BYTES;
  
  return 0;
}


// High-level decryption function from SUPERCOP.
// nsec is kept for compatibility with SUPERCOP, but is not used.

int crypto_aead_decrypt(UChar *m, ULLInt *mlen, UChar *nsec, const UChar *c, \
  ULLInt clen, const UChar *ad, ULLInt adlen, const UChar *npub,             \
  const UChar *k)
{
  SparkleState state;
  size_t csize = (size_t) (clen - TAG_BYTES);
  size_t adsize = (size_t) adlen;
  int retval;
  
  Initialize(&state, k, npub);
  if (adsize) ProcessAssocData(&state, ad, adsize);
  if (csize) ProcessCipherText(&state, m, c, csize);
  Finalize(&state, k);
  retval = VerifyTag(&state, (c + csize));
  *mlen = csize;
  
  return retval;
}