internal-sparkle.c 10.6 KB
Newer Older
Rhys Weatherley 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
/*
 * 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-sparkle.h"

25 26
#if !defined(__AVR__)

Rhys Weatherley committed
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
/* The 8 basic round constants from the specification */
#define RC_0 0xB7E15162
#define RC_1 0xBF715880
#define RC_2 0x38B4DA56
#define RC_3 0x324E7738
#define RC_4 0xBB1185EB
#define RC_5 0x4F7C7B57
#define RC_6 0xCFBFA1C8
#define RC_7 0xC2B3293D

/* Round constants for all SPARKLE steps; maximum of 12 for SPARKLE-512 */
static uint32_t const sparkle_rc[12] = {
    RC_0, RC_1, RC_2, RC_3, RC_4, RC_5, RC_6, RC_7,
    RC_0, RC_1, RC_2, RC_3
};

/**
 * \brief Alzette block cipher that implements the ARXbox layer of the
 * SPARKLE permutation.
 *
 * \param x Left half of the 64-bit block.
 * \param y Right half of the 64-bit block.
 * \param k 32-bit round key.
 */
#define alzette(x, y, k) \
    do { \
        (x) += leftRotate1((y)); \
        (y) ^= leftRotate8((x)); \
        (x) ^= (k); \
        (x) += leftRotate15((y)); \
        (y) ^= leftRotate15((x)); \
        (x) ^= (k); \
        (x) += (y); \
        (y) ^= leftRotate1((x)); \
        (x) ^= (k); \
        (x) += leftRotate8((y)); \
        (y) ^= leftRotate16((x)); \
        (x) ^= (k); \
    } while (0)

void sparkle_256(uint32_t s[SPARKLE_256_STATE_SIZE], unsigned steps)
{
    uint32_t x0, x1, x2, x3;
    uint32_t y0, y1, y2, y3;
71
    uint32_t tx, ty;
Rhys Weatherley committed
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
    unsigned step;

    /* Load the SPARKLE-256 state up into local variables */
#if defined(LW_UTIL_LITTLE_ENDIAN)
    x0 = s[0];
    y0 = s[1];
    x1 = s[2];
    y1 = s[3];
    x2 = s[4];
    y2 = s[5];
    x3 = s[6];
    y3 = s[7];
#else
    x0 = le_load_word32((const uint8_t *)&(s[0]));
    y0 = le_load_word32((const uint8_t *)&(s[1]));
    x1 = le_load_word32((const uint8_t *)&(s[2]));
    y1 = le_load_word32((const uint8_t *)&(s[3]));
    x2 = le_load_word32((const uint8_t *)&(s[4]));
    y2 = le_load_word32((const uint8_t *)&(s[5]));
    x3 = le_load_word32((const uint8_t *)&(s[6]));
    y3 = le_load_word32((const uint8_t *)&(s[7]));
#endif

    /* Perform all requested steps */
    for (step = 0; step < steps; ++step) {
        /* Add round constants */
        y0 ^= sparkle_rc[step];
        y1 ^= step;

        /* ARXbox layer */
        alzette(x0, y0, RC_0);
        alzette(x1, y1, RC_1);
        alzette(x2, y2, RC_2);
        alzette(x3, y3, RC_3);

        /* Linear layer */
        tx = x0 ^ x1;
        ty = y0 ^ y1;
        tx = leftRotate16(tx ^ (tx << 16));
        ty = leftRotate16(ty ^ (ty << 16));
112 113
        y2 ^= tx;
        tx ^= y3;
Rhys Weatherley committed
114
        y3 = y1;
115 116 117 118 119 120 121 122 123
        y1 = y2 ^ y0;
        y2 = y0;
        y0 = tx ^ y3;
        x2 ^= ty;
        ty ^= x3;
        x3 = x1;
        x1 = x2 ^ x0;
        x2 = x0;
        x0 = ty ^ x3;
Rhys Weatherley committed
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
    }

    /* Write the local variables back to the SPARKLE-256 state */
#if defined(LW_UTIL_LITTLE_ENDIAN)
    s[0] = x0;
    s[1] = y0;
    s[2] = x1;
    s[3] = y1;
    s[4] = x2;
    s[5] = y2;
    s[6] = x3;
    s[7] = y3;
#else
    le_store_word32((uint8_t *)&(s[0]), x0);
    le_store_word32((uint8_t *)&(s[1]), y0);
    le_store_word32((uint8_t *)&(s[2]), x1);
    le_store_word32((uint8_t *)&(s[3]), y1);
    le_store_word32((uint8_t *)&(s[4]), x2);
    le_store_word32((uint8_t *)&(s[5]), y2);
    le_store_word32((uint8_t *)&(s[6]), x3);
    le_store_word32((uint8_t *)&(s[7]), y3);
#endif
}

void sparkle_384(uint32_t s[SPARKLE_384_STATE_SIZE], unsigned steps)
{
    uint32_t x0, x1, x2, x3, x4, x5;
    uint32_t y0, y1, y2, y3, y4, y5;
152
    uint32_t tx, ty;
Rhys Weatherley committed
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
    unsigned step;

    /* Load the SPARKLE-384 state up into local variables */
#if defined(LW_UTIL_LITTLE_ENDIAN)
    x0 = s[0];
    y0 = s[1];
    x1 = s[2];
    y1 = s[3];
    x2 = s[4];
    y2 = s[5];
    x3 = s[6];
    y3 = s[7];
    x4 = s[8];
    y4 = s[9];
    x5 = s[10];
    y5 = s[11];
#else
    x0 = le_load_word32((const uint8_t *)&(s[0]));
    y0 = le_load_word32((const uint8_t *)&(s[1]));
    x1 = le_load_word32((const uint8_t *)&(s[2]));
    y1 = le_load_word32((const uint8_t *)&(s[3]));
    x2 = le_load_word32((const uint8_t *)&(s[4]));
    y2 = le_load_word32((const uint8_t *)&(s[5]));
    x3 = le_load_word32((const uint8_t *)&(s[6]));
    y3 = le_load_word32((const uint8_t *)&(s[7]));
    x4 = le_load_word32((const uint8_t *)&(s[8]));
    y4 = le_load_word32((const uint8_t *)&(s[9]));
    x5 = le_load_word32((const uint8_t *)&(s[10]));
    y5 = le_load_word32((const uint8_t *)&(s[11]));
#endif

    /* Perform all requested steps */
    for (step = 0; step < steps; ++step) {
        /* Add round constants */
        y0 ^= sparkle_rc[step];
        y1 ^= step;

        /* ARXbox layer */
        alzette(x0, y0, RC_0);
        alzette(x1, y1, RC_1);
        alzette(x2, y2, RC_2);
        alzette(x3, y3, RC_3);
        alzette(x4, y4, RC_4);
        alzette(x5, y5, RC_5);

        /* Linear layer */
        tx = x0 ^ x1 ^ x2;
        ty = y0 ^ y1 ^ y2;
        tx = leftRotate16(tx ^ (tx << 16));
        ty = leftRotate16(ty ^ (ty << 16));
203 204 205 206 207 208 209
        y3 ^= tx;
        y4 ^= tx;
        tx ^= y5;
        y5 = y2;
        y2 = y3 ^ y0;
        y3 = y0;
        y0 = y4 ^ y1;
Rhys Weatherley committed
210
        y4 = y1;
211 212 213 214
        y1 = tx ^ y5;
        x3 ^= ty;
        x4 ^= ty;
        ty ^= x5;
Rhys Weatherley committed
215
        x5 = x2;
216 217 218 219 220
        x2 = x3 ^ x0;
        x3 = x0;
        x0 = x4 ^ x1;
        x4 = x1;
        x1 = ty ^ x5;
Rhys Weatherley committed
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
    }

    /* Write the local variables back to the SPARKLE-384 state */
#if defined(LW_UTIL_LITTLE_ENDIAN)
    s[0]  = x0;
    s[1]  = y0;
    s[2]  = x1;
    s[3]  = y1;
    s[4]  = x2;
    s[5]  = y2;
    s[6]  = x3;
    s[7]  = y3;
    s[8]  = x4;
    s[9]  = y4;
    s[10] = x5;
    s[11] = y5;
#else
    le_store_word32((uint8_t *)&(s[0]),  x0);
    le_store_word32((uint8_t *)&(s[1]),  y0);
    le_store_word32((uint8_t *)&(s[2]),  x1);
    le_store_word32((uint8_t *)&(s[3]),  y1);
    le_store_word32((uint8_t *)&(s[4]),  x2);
    le_store_word32((uint8_t *)&(s[5]),  y2);
    le_store_word32((uint8_t *)&(s[6]),  x3);
    le_store_word32((uint8_t *)&(s[7]),  y3);
    le_store_word32((uint8_t *)&(s[8]),  x4);
    le_store_word32((uint8_t *)&(s[9]),  y4);
    le_store_word32((uint8_t *)&(s[10]), x5);
    le_store_word32((uint8_t *)&(s[11]), y5);
#endif
}

void sparkle_512(uint32_t s[SPARKLE_512_STATE_SIZE], unsigned steps)
{
    uint32_t x0, x1, x2, x3, x4, x5, x6, x7;
    uint32_t y0, y1, y2, y3, y4, y5, y6, y7;
257
    uint32_t tx, ty;
Rhys Weatherley committed
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
    unsigned step;

    /* Load the SPARKLE-512 state up into local variables */
#if defined(LW_UTIL_LITTLE_ENDIAN)
    x0 = s[0];
    y0 = s[1];
    x1 = s[2];
    y1 = s[3];
    x2 = s[4];
    y2 = s[5];
    x3 = s[6];
    y3 = s[7];
    x4 = s[8];
    y4 = s[9];
    x5 = s[10];
    y5 = s[11];
    x6 = s[12];
    y6 = s[13];
    x7 = s[14];
    y7 = s[15];
#else
    x0 = le_load_word32((const uint8_t *)&(s[0]));
    y0 = le_load_word32((const uint8_t *)&(s[1]));
    x1 = le_load_word32((const uint8_t *)&(s[2]));
    y1 = le_load_word32((const uint8_t *)&(s[3]));
    x2 = le_load_word32((const uint8_t *)&(s[4]));
    y2 = le_load_word32((const uint8_t *)&(s[5]));
    x3 = le_load_word32((const uint8_t *)&(s[6]));
    y3 = le_load_word32((const uint8_t *)&(s[7]));
    x4 = le_load_word32((const uint8_t *)&(s[8]));
    y4 = le_load_word32((const uint8_t *)&(s[9]));
    x5 = le_load_word32((const uint8_t *)&(s[10]));
    y5 = le_load_word32((const uint8_t *)&(s[11]));
    x6 = le_load_word32((const uint8_t *)&(s[12]));
    y6 = le_load_word32((const uint8_t *)&(s[13]));
    x7 = le_load_word32((const uint8_t *)&(s[14]));
    y7 = le_load_word32((const uint8_t *)&(s[15]));
#endif

    /* Perform all requested steps */
    for (step = 0; step < steps; ++step) {
        /* Add round constants */
        y0 ^= sparkle_rc[step];
        y1 ^= step;

        /* ARXbox layer */
        alzette(x0, y0, RC_0);
        alzette(x1, y1, RC_1);
        alzette(x2, y2, RC_2);
        alzette(x3, y3, RC_3);
        alzette(x4, y4, RC_4);
        alzette(x5, y5, RC_5);
        alzette(x6, y6, RC_6);
        alzette(x7, y7, RC_7);

        /* Linear layer */
        tx = x0 ^ x1 ^ x2 ^ x3;
        ty = y0 ^ y1 ^ y2 ^ y3;
        tx = leftRotate16(tx ^ (tx << 16));
        ty = leftRotate16(ty ^ (ty << 16));
318 319 320 321 322 323 324 325
        y4 ^= tx;
        y5 ^= tx;
        y6 ^= tx;
        tx ^= y7;
        y7 = y3;
        y3 = y4 ^ y0;
        y4 = y0;
        y0 = y5 ^ y1;
Rhys Weatherley committed
326
        y5 = y1;
327
        y1 = y6 ^ y2;
Rhys Weatherley committed
328
        y6 = y2;
329 330 331 332 333
        y2 = tx ^ y7;
        x4 ^= ty;
        x5 ^= ty;
        x6 ^= ty;
        ty ^= x7;
Rhys Weatherley committed
334
        x7 = x3;
335 336 337 338 339 340 341
        x3 = x4 ^ x0;
        x4 = x0;
        x0 = x5 ^ x1;
        x5 = x1;
        x1 = x6 ^ x2;
        x6 = x2;
        x2 = ty ^ x7;
Rhys Weatherley committed
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
    }

    /* Write the local variables back to the SPARKLE-512 state */
#if defined(LW_UTIL_LITTLE_ENDIAN)
    s[0]  = x0;
    s[1]  = y0;
    s[2]  = x1;
    s[3]  = y1;
    s[4]  = x2;
    s[5]  = y2;
    s[6]  = x3;
    s[7]  = y3;
    s[8]  = x4;
    s[9]  = y4;
    s[10] = x5;
    s[11] = y5;
    s[12] = x6;
    s[13] = y6;
    s[14] = x7;
    s[15] = y7;
#else
    le_store_word32((uint8_t *)&(s[0]),  x0);
    le_store_word32((uint8_t *)&(s[1]),  y0);
    le_store_word32((uint8_t *)&(s[2]),  x1);
    le_store_word32((uint8_t *)&(s[3]),  y1);
    le_store_word32((uint8_t *)&(s[4]),  x2);
    le_store_word32((uint8_t *)&(s[5]),  y2);
    le_store_word32((uint8_t *)&(s[6]),  x3);
    le_store_word32((uint8_t *)&(s[7]),  y3);
    le_store_word32((uint8_t *)&(s[8]),  x4);
    le_store_word32((uint8_t *)&(s[9]),  y4);
    le_store_word32((uint8_t *)&(s[10]), x5);
    le_store_word32((uint8_t *)&(s[11]), y5);
    le_store_word32((uint8_t *)&(s[12]), x6);
    le_store_word32((uint8_t *)&(s[13]), y6);
    le_store_word32((uint8_t *)&(s[14]), x7);
    le_store_word32((uint8_t *)&(s[15]), y7);
#endif
}
381 382

#endif