internal-saturnin.c 17.8 KB
Newer Older
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 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
/*
 * Copyright (C) 2020 Southern Storm Software, Pty Ltd.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

#include "internal-saturnin.h"

#if !defined(__AVR__)

/* Round constants for various combinations of rounds and domain_sep */
static uint32_t const saturnin_rc[] = {
    /* RC_10_1 */
    0x4eb026c2, 0x90595303, 0xaa8fe632, 0xfe928a92, 0x4115a419,
    0x93539532, 0x5db1cc4e, 0x541515ca, 0xbd1f55a8, 0x5a6e1a0d,
    /* RC_10_2 */
    0x4e4526b5, 0xa3565ff0, 0x0f8f20d8, 0x0b54bee1, 0x7d1a6c9d,
    0x17a6280a, 0xaa46c986, 0xc1199062, 0x182c5cde, 0xa00d53fe,
    /* RC_10_3 */
    0x4e162698, 0xb2535ba1, 0x6c8f9d65, 0x5816ad30, 0x691fd4fa,
    0x6bf5bcf9, 0xf8eb3525, 0xb21decfa, 0x7b3da417, 0xf62c94b4,
    /* RC_10_4 */
    0x4faf265b, 0xc5484616, 0x45dcad21, 0xe08bd607, 0x0504fdb8,
    0x1e1f5257, 0x45fbc216, 0xeb529b1f, 0x52194e32, 0x5498c018,
    /* RC_10_5 */
    0x4ffc2676, 0xd44d4247, 0x26dc109c, 0xb3c9c5d6, 0x110145df,
    0x624cc6a4, 0x17563eb5, 0x9856e787, 0x3108b6fb, 0x02b90752,
    /* RC_10_6 */
    0x4f092601, 0xe7424eb4, 0x83dcd676, 0x460ff1a5, 0x2d0e8d5b,
    0xe6b97b9c, 0xe0a13b7d, 0x0d5a622f, 0x943bbf8d, 0xf8da4ea1,
    /* RC_16_7 */
    0x3fba180c, 0x563ab9ab, 0x125ea5ef, 0x859da26c, 0xb8cf779b,
    0x7d4de793, 0x07efb49f, 0x8d525306, 0x1e08e6ab, 0x41729f87,
    0x8c4aef0a, 0x4aa0c9a7, 0xd93a95ef, 0xbb00d2af, 0xb62c5bf0,
    0x386d94d8,
    /* RC_16_8 */
    0x3c9b19a7, 0xa9098694, 0x23f878da, 0xa7b647d3, 0x74fc9d78,
    0xeacaae11, 0x2f31a677, 0x4cc8c054, 0x2f51ca05, 0x5268f195,
    0x4f5b8a2b, 0xf614b4ac, 0xf1d95401, 0x764d2568, 0x6a493611,
    0x8eef9c3e
};

/* Loads a 32-bit word from the two halves of a 256-bit Saturnin input block */
#define saturnin_load_word32(ptr) \
    ((((uint32_t)((ptr)[17])) << 24) | \
     (((uint32_t)((ptr)[16])) << 16) | \
     (((uint32_t)((ptr)[1]))  << 8) | \
      ((uint32_t)((ptr)[0])))

/* Stores a 32-bit word to the two halves of a 256-bit Saturnin output block */
#define saturnin_store_word32(ptr, x) \
    do { \
        (ptr)[0]  = (uint8_t)(x); \
        (ptr)[1]  = (uint8_t)((x) >> 8); \
        (ptr)[16] = (uint8_t)((x) >> 16); \
        (ptr)[17] = (uint8_t)((x) >> 24); \
    } while (0)

/* Rotate the 4-bit nibbles within a 16-bit word left */
#define leftRotate4_N(a, mask1, bits1, mask2, bits2) \
    do { \
        (a) = (((a) & (mask1)) << (bits1)) | \
              (((a) & ((mask1) ^ (uint32_t)0xFFFFU)) >> (4 - (bits1))) | \
              (((a) & (((uint32_t)(mask2)) << 16)) << (bits2)) | \
              (((a) & (((uint32_t)((mask2)) << 16) ^ 0xFFFF0000U)) >> (4 - (bits2))); \
    } while (0)

/* Rotate 16-bit subwords left */
#define leftRotate16_N(a, mask1, bits1, mask2, bits2) \
    do { \
        (a) = (((a) & (mask1)) << (bits1)) | \
              (((a) & ((mask1) ^ (uint32_t)0xFFFFU)) >> (16 - (bits1))) | \
              (((a) & (((uint32_t)(mask2)) << 16)) << (bits2)) | \
              (((a) & (((uint32_t)((mask2)) << 16) ^ 0xFFFF0000U)) >> (16 - (bits2))); \
    } while (0)

/**
 * \brief XOR the key into the Saturnin state.
 *
 * \param x0 First word of the bit-sliced state.
 * \param x1 Second word of the bit-sliced state.
 * \param x2 Third word of the bit-sliced state.
 * \param x3 Fourth word of the bit-sliced state.
 * \param x4 Fifth word of the bit-sliced state.
 * \param x5 Sixth word of the bit-sliced state.
 * \param x6 Seventh word of the bit-sliced state.
 * \param x7 Eighth word of the bit-sliced state.
 */
#define saturnin_xor_key(x0, x1, x2, x3, x4, x5, x6, x7) \
    do { \
        x0 ^= ks->k[0]; \
        x1 ^= ks->k[1]; \
        x2 ^= ks->k[2]; \
        x3 ^= ks->k[3]; \
        x4 ^= ks->k[4]; \
        x5 ^= ks->k[5]; \
        x6 ^= ks->k[6]; \
        x7 ^= ks->k[7]; \
    } while (0)

/**
 * \brief XOR a rotated version of the key into the Saturnin state.
 *
 * \param x0 First word of the bit-sliced state.
 * \param x1 Second word of the bit-sliced state.
 * \param x2 Third word of the bit-sliced state.
 * \param x3 Fourth word of the bit-sliced state.
 * \param x4 Fifth word of the bit-sliced state.
 * \param x5 Sixth word of the bit-sliced state.
 * \param x6 Seventh word of the bit-sliced state.
 * \param x7 Eighth word of the bit-sliced state.
 */
#define saturnin_xor_key_rotated(x0, x1, x2, x3, x4, x5, x6, x7) \
    do { \
        x0 ^= ks->k[8]; \
        x1 ^= ks->k[9]; \
        x2 ^= ks->k[10]; \
        x3 ^= ks->k[11]; \
        x4 ^= ks->k[12]; \
        x5 ^= ks->k[13]; \
        x6 ^= ks->k[14]; \
        x7 ^= ks->k[15]; \
    } while (0)

/**
 * \brief Applies the Saturnin S-box to a bit-sliced set of nibbles.
 *
 * \param a First bit-slice.
 * \param b Second bit-slice.
 * \param c Third bit-slice.
 * \param d Fourth bit-slice.
 *
 * The S-box also involves a rotation on the output words.  We perform the
 * rotation implicitly in the higher layers.
 */
#define saturnin_sbox(a, b, c, d) \
    do { \
        (a) ^= (b) & (c); \
        (b) ^= (a) | (d); \
        (d) ^= (b) | (c); \
        (c) ^= (b) & (d); \
        (b) ^= (a) | (c); \
        (a) ^= (b) | (d); \
    } while (0)

/**
 * \brief Applies the inverse of the Saturnin S-box to a set of nibbles.
 *
 * \param a First bit-slice.
 * \param b Second bit-slice.
 * \param c Third bit-slice.
 * \param d Fourth bit-slice.
 *
 * The inverse of the S-box also involves a rotation on the input words.
 * We perform the rotation implicitly in the higher layers.
 */
#define saturnin_sbox_inverse(a, b, c, d) \
    do { \
        (a) ^= (b) | (d); \
        (b) ^= (a) | (c); \
        (c) ^= (b) & (d); \
        (d) ^= (b) | (c); \
        (b) ^= (a) | (d); \
        (a) ^= (b) & (c); \
    } while (0)

/* Helpers for MDS matrix operations */
#define SWAP(a) (((a) << 16) | ((a) >> 16))
#define MUL(x0, x1, x2, x3) \
    do { \
        temp = x0; x0 = x1; x1 = x2; x2 = x3; x3 = temp ^ x0; \
    } while (0)
#define MULINV(x0, x1, x2, x3) \
    do { \
        temp = x3; x3 = x2; x2 = x1; x1 = x0; x0 = x1 ^ temp; \
    } while (0)

/**
 * \brief Applies the MDS matrix to the Saturnin state.
 *
 * \param x0 First word of the bit-sliced state.
 * \param x1 Second word of the bit-sliced state.
 * \param x2 Third word of the bit-sliced state.
 * \param x3 Fourth word of the bit-sliced state.
 * \param x4 Fifth word of the bit-sliced state.
 * \param x5 Sixth word of the bit-sliced state.
 * \param x6 Seventh word of the bit-sliced state.
 * \param x7 Eighth word of the bit-sliced state.
 */
#define saturnin_mds(x0, x1, x2, x3, x4, x5, x6, x7) \
    do { \
        x0 ^= x4; x1 ^= x5; x2 ^= x6; x3 ^= x7; \
        MUL(x4, x5, x6, x7); \
        x4 ^= SWAP(x0); x5 ^= SWAP(x1); \
        x6 ^= SWAP(x2); x7 ^= SWAP(x3); \
        MUL(x0, x1, x2, x3); \
        MUL(x0, x1, x2, x3); \
        x0 ^= x4; x1 ^= x5; x2 ^= x6; x3 ^= x7; \
        x4 ^= SWAP(x0); x5 ^= SWAP(x1); \
        x6 ^= SWAP(x2); x7 ^= SWAP(x3); \
    } while (0)

/**
 * \brief Applies the inverse of the MDS matrix to the Saturnin state.
 *
 * \param x0 First word of the bit-sliced state.
 * \param x1 Second word of the bit-sliced state.
 * \param x2 Third word of the bit-sliced state.
 * \param x3 Fourth word of the bit-sliced state.
 * \param x4 Fifth word of the bit-sliced state.
 * \param x5 Sixth word of the bit-sliced state.
 * \param x6 Seventh word of the bit-sliced state.
 * \param x7 Eighth word of the bit-sliced state.
 */
#define saturnin_mds_inverse(x0, x1, x2, x3, x4, x5, x6, x7) \
    do { \
        x6 ^= SWAP(x2); x7 ^= SWAP(x3); \
        x4 ^= SWAP(x0); x5 ^= SWAP(x1); \
        x0 ^= x4; x1 ^= x5; x2 ^= x6; x3 ^= x7; \
        MULINV(x0, x1, x2, x3); \
        MULINV(x0, x1, x2, x3); \
        x6 ^= SWAP(x2); x7 ^= SWAP(x3); \
        x4 ^= SWAP(x0); x5 ^= SWAP(x1); \
        MULINV(x4, x5, x6, x7); \
        x0 ^= x4; x1 ^= x5; x2 ^= x6; x3 ^= x7; \
    } while (0)

/**
 * \brief Applies the slice permutation to the Saturnin state.
 *
 * \param x0 First word of the bit-sliced state.
 * \param x1 Second word of the bit-sliced state.
 * \param x2 Third word of the bit-sliced state.
 * \param x3 Fourth word of the bit-sliced state.
 * \param x4 Fifth word of the bit-sliced state.
 * \param x5 Sixth word of the bit-sliced state.
 * \param x6 Seventh word of the bit-sliced state.
 * \param x7 Eighth word of the bit-sliced state.
 */
#define saturnin_slice(x0, x1, x2, x3, x4, x5, x6, x7) \
    do { \
        leftRotate4_N(x0, 0xFFFFU, 0, 0x3333, 2); \
        leftRotate4_N(x1, 0xFFFFU, 0, 0x3333, 2); \
        leftRotate4_N(x2, 0xFFFFU, 0, 0x3333, 2); \
        leftRotate4_N(x3, 0xFFFFU, 0, 0x3333, 2); \
        leftRotate4_N(x4, 0x7777U, 1, 0x1111, 3); \
        leftRotate4_N(x5, 0x7777U, 1, 0x1111, 3); \
        leftRotate4_N(x6, 0x7777U, 1, 0x1111, 3); \
        leftRotate4_N(x7, 0x7777U, 1, 0x1111, 3); \
    } while (0)

/**
 * \brief Applies the inverse of the slice permutation to the Saturnin state.
 *
 * \param x0 First word of the bit-sliced state.
 * \param x1 Second word of the bit-sliced state.
 * \param x2 Third word of the bit-sliced state.
 * \param x3 Fourth word of the bit-sliced state.
 * \param x4 Fifth word of the bit-sliced state.
 * \param x5 Sixth word of the bit-sliced state.
 * \param x6 Seventh word of the bit-sliced state.
 * \param x7 Eighth word of the bit-sliced state.
 */
#define saturnin_slice_inverse(x0, x1, x2, x3, x4, x5, x6, x7) \
    do { \
        leftRotate4_N(x0, 0xFFFFU, 0, 0x3333, 2); \
        leftRotate4_N(x1, 0xFFFFU, 0, 0x3333, 2); \
        leftRotate4_N(x2, 0xFFFFU, 0, 0x3333, 2); \
        leftRotate4_N(x3, 0xFFFFU, 0, 0x3333, 2); \
        leftRotate4_N(x4, 0x1111U, 3, 0x7777, 1); \
        leftRotate4_N(x5, 0x1111U, 3, 0x7777, 1); \
        leftRotate4_N(x6, 0x1111U, 3, 0x7777, 1); \
        leftRotate4_N(x7, 0x1111U, 3, 0x7777, 1); \
    } while (0)

/**
 * \brief Applies the sheet permutation to the Saturnin state.
 *
 * \param x0 First word of the bit-sliced state.
 * \param x1 Second word of the bit-sliced state.
 * \param x2 Third word of the bit-sliced state.
 * \param x3 Fourth word of the bit-sliced state.
 * \param x4 Fifth word of the bit-sliced state.
 * \param x5 Sixth word of the bit-sliced state.
 * \param x6 Seventh word of the bit-sliced state.
 * \param x7 Eighth word of the bit-sliced state.
 */
#define saturnin_sheet(x0, x1, x2, x3, x4, x5, x6, x7) \
    do { \
        leftRotate16_N(x0, 0xFFFFU, 0, 0x00FF, 8); \
        leftRotate16_N(x1, 0xFFFFU, 0, 0x00FF, 8); \
        leftRotate16_N(x2, 0xFFFFU, 0, 0x00FF, 8); \
        leftRotate16_N(x3, 0xFFFFU, 0, 0x00FF, 8); \
        leftRotate16_N(x4, 0x0FFFU, 4, 0x000F, 12); \
        leftRotate16_N(x5, 0x0FFFU, 4, 0x000F, 12); \
        leftRotate16_N(x6, 0x0FFFU, 4, 0x000F, 12); \
        leftRotate16_N(x7, 0x0FFFU, 4, 0x000F, 12); \
    } while (0)

/**
 * \brief Applies the inverse of the sheet permutation to the Saturnin state.
 *
 * \param x0 First word of the bit-sliced state.
 * \param x1 Second word of the bit-sliced state.
 * \param x2 Third word of the bit-sliced state.
 * \param x3 Fourth word of the bit-sliced state.
 * \param x4 Fifth word of the bit-sliced state.
 * \param x5 Sixth word of the bit-sliced state.
 * \param x6 Seventh word of the bit-sliced state.
 * \param x7 Eighth word of the bit-sliced state.
 */
#define saturnin_sheet_inverse(x0, x1, x2, x3, x4, x5, x6, x7) \
    do { \
        leftRotate16_N(x0, 0xFFFFU, 0, 0x00FF, 8); \
        leftRotate16_N(x1, 0xFFFFU, 0, 0x00FF, 8); \
        leftRotate16_N(x2, 0xFFFFU, 0, 0x00FF, 8); \
        leftRotate16_N(x3, 0xFFFFU, 0, 0x00FF, 8); \
        leftRotate16_N(x4, 0x000FU, 12, 0x0FFF, 4); \
        leftRotate16_N(x5, 0x000FU, 12, 0x0FFF, 4); \
        leftRotate16_N(x6, 0x000FU, 12, 0x0FFF, 4); \
        leftRotate16_N(x7, 0x000FU, 12, 0x0FFF, 4); \
    } while (0)

void saturnin_setup_key
    (saturnin_key_schedule_t *ks, const unsigned char *key)
{
    int index;
    uint32_t temp;
    for (index = 0; index < 16; index += 2) {
        temp = saturnin_load_word32(key + index);
        ks->k[index / 2] = temp;
        ks->k[8 + (index / 2)] = ((temp & 0x001F001FU) << 11) |
                                 ((temp >> 5) & 0x07FF07FFU);
    }
}

void saturnin_encrypt_block
    (const saturnin_key_schedule_t *ks, unsigned char *output,
     const unsigned char *input, unsigned domain)
{
    unsigned rounds = (domain >= SATURNIN_DOMAIN_16_7) ? 8 : 5;
    const uint32_t *rc = saturnin_rc + domain;
    uint32_t x0, x1, x2, x3, x4, x5, x6, x7, temp;

    /* Load the input into local variables */
    x0 = saturnin_load_word32(input);
    x1 = saturnin_load_word32(input + 2);
    x2 = saturnin_load_word32(input + 4);
    x3 = saturnin_load_word32(input + 6);
    x4 = saturnin_load_word32(input + 8);
    x5 = saturnin_load_word32(input + 10);
    x6 = saturnin_load_word32(input + 12);
    x7 = saturnin_load_word32(input + 14);

    /* XOR the key into the state */
    saturnin_xor_key(x0, x1, x2, x3, x4, x5, x6, x7);

    /* Perform all encryption rounds, two at a time */
    for (; rounds > 0; --rounds, rc += 2) {
        /* Even rounds */
        saturnin_sbox(x0, x1, x2, x3);
        saturnin_sbox(x4, x5, x6, x7);
        saturnin_mds(x1, x2, x3, x0, x7, x5, x4, x6);
        saturnin_sbox(x1, x2, x3, x0);
        saturnin_sbox(x7, x5, x4, x6);
        saturnin_slice(x2, x3, x0, x1, x6, x5, x7, x4);
        saturnin_mds(x2, x3, x0, x1, x6, x5, x7, x4);
        saturnin_slice_inverse(x2, x3, x0, x1, x6, x5, x7, x4);
        x2 ^= rc[0];
        saturnin_xor_key_rotated(x2, x3, x0, x1, x6, x5, x7, x4);

        /* Odd rounds */
        saturnin_sbox(x2, x3, x0, x1);
        saturnin_sbox(x6, x5, x7, x4);
        saturnin_mds(x3, x0, x1, x2, x4, x5, x6, x7);
        saturnin_sbox(x3, x0, x1, x2);
        saturnin_sbox(x4, x5, x6, x7);
        saturnin_sheet(x0, x1, x2, x3, x7, x5, x4, x6);
        saturnin_mds(x0, x1, x2, x3, x7, x5, x4, x6);
        saturnin_sheet_inverse(x0, x1, x2, x3, x7, x5, x4, x6);
        x0 ^= rc[1];
        saturnin_xor_key(x0, x1, x2, x3, x7, x5, x4, x6);

        /* Correct the rotation of the second half before the next round */
        temp = x4;
        x4 = x7;
        x7 = x6;
        x6 = temp;
    }

    /* Store the local variables to the output buffer */
    saturnin_store_word32(output,      x0);
    saturnin_store_word32(output +  2, x1);
    saturnin_store_word32(output +  4, x2);
    saturnin_store_word32(output +  6, x3);
    saturnin_store_word32(output +  8, x4);
    saturnin_store_word32(output + 10, x5);
    saturnin_store_word32(output + 12, x6);
    saturnin_store_word32(output + 14, x7);
}

void saturnin_decrypt_block
    (const saturnin_key_schedule_t *ks, unsigned char *output,
     const unsigned char *input, unsigned domain)
{
    unsigned rounds = (domain >= SATURNIN_DOMAIN_16_7) ? 8 : 5;
    const uint32_t *rc = saturnin_rc + domain + (rounds - 1) * 2;
    uint32_t x0, x1, x2, x3, x4, x5, x6, x7, temp;

    /* Load the input into local variables */
    x0 = saturnin_load_word32(input);
    x1 = saturnin_load_word32(input + 2);
    x2 = saturnin_load_word32(input + 4);
    x3 = saturnin_load_word32(input + 6);
    x4 = saturnin_load_word32(input + 8);
    x5 = saturnin_load_word32(input + 10);
    x6 = saturnin_load_word32(input + 12);
    x7 = saturnin_load_word32(input + 14);

    /* Perform all decryption rounds, two at a time */
    for (; rounds > 0; --rounds, rc -= 2) {
        /* Correct the rotation of the second half before the next round */
        temp = x6;
        x6 = x7;
        x7 = x4;
        x4 = temp;

        /* Odd rounds */
        saturnin_xor_key(x0, x1, x2, x3, x7, x5, x4, x6);
        x0 ^= rc[1];
        saturnin_sheet(x0, x1, x2, x3, x7, x5, x4, x6);
        saturnin_mds_inverse(x0, x1, x2, x3, x7, x5, x4, x6);
        saturnin_sheet_inverse(x0, x1, x2, x3, x7, x5, x4, x6);
        saturnin_sbox_inverse(x3, x0, x1, x2);
        saturnin_sbox_inverse(x4, x5, x6, x7);
        saturnin_mds_inverse(x3, x0, x1, x2, x4, x5, x6, x7);
        saturnin_sbox_inverse(x2, x3, x0, x1);
        saturnin_sbox_inverse(x6, x5, x7, x4);

        /* Even rounds */
        saturnin_xor_key_rotated(x2, x3, x0, x1, x6, x5, x7, x4);
        x2 ^= rc[0];
        saturnin_slice(x2, x3, x0, x1, x6, x5, x7, x4);
        saturnin_mds_inverse(x2, x3, x0, x1, x6, x5, x7, x4);
        saturnin_slice_inverse(x2, x3, x0, x1, x6, x5, x7, x4);
        saturnin_sbox_inverse(x1, x2, x3, x0);
        saturnin_sbox_inverse(x7, x5, x4, x6);
        saturnin_mds_inverse(x1, x2, x3, x0, x7, x5, x4, x6);
        saturnin_sbox_inverse(x0, x1, x2, x3);
        saturnin_sbox_inverse(x4, x5, x6, x7);
    }

    /* XOR the key into the state */
    saturnin_xor_key(x0, x1, x2, x3, x4, x5, x6, x7);

    /* Store the local variables to the output buffer */
    saturnin_store_word32(output,      x0);
    saturnin_store_word32(output +  2, x1);
    saturnin_store_word32(output +  4, x2);
    saturnin_store_word32(output +  6, x3);
    saturnin_store_word32(output +  8, x4);
    saturnin_store_word32(output + 10, x5);
    saturnin_store_word32(output + 12, x6);
    saturnin_store_word32(output + 14, x7);
}

#endif /* !__AVR__ */