internal-photon256.c 16.2 KB
Newer Older
Rhys Weatherley committed
1
/*
2
 * Copyright (C) 2021 Southern Storm Software, Pty Ltd.
Rhys Weatherley committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 * 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-photon256.h"
24
#include "internal-photon256-mix.h"
Rhys Weatherley committed
25 26
#include "internal-util.h"

27 28 29 30 31 32 33 34 35 36
/* Determine if PHOTON-256 should be accelerated with assembly code */
#if defined(__AVR__)
#define PHOTON128_ASM 1
#elif defined(__ARM_ARCH_ISA_THUMB) && __ARM_ARCH == 7
#define PHOTON128_ASM 1
#else
#define PHOTON128_ASM 0
#endif

#if !PHOTON128_ASM
37

Rhys Weatherley committed
38 39 40 41 42
/**
 * \brief Number of rounds in the PHOTON-256 permutation in bit-sliced form.
 */
#define PHOTON256_ROUNDS 12

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
/* Round constants for PHOTON-256, split out into separate bit-slices */
static uint32_t const photon256_rc[PHOTON256_ROUNDS * 8] = {
    0x00000001, 0x01010000, 0x01000000, 0x00000000, /* Round  1 */
    0x01010100, 0x00000101, 0x00010101, 0x01010101,
    0x00000001, 0x00000101, 0x01000000, 0x00000000, /* Round  2 */
    0x01010100, 0x01010000, 0x00010101, 0x01010101,
    0x00000001, 0x00000101, 0x00010101, 0x00000000, /* Round  3 */
    0x01010100, 0x01010000, 0x01000000, 0x01010101,
    0x01010100, 0x00000101, 0x00010101, 0x01010101, /* Round  4 */
    0x00000001, 0x01010000, 0x01000000, 0x00000000,
    0x00000001, 0x01010000, 0x00010101, 0x01010101, /* Round  5 */
    0x01010100, 0x00000101, 0x01000000, 0x00000000,
    0x00000001, 0x00000101, 0x01000000, 0x01010101, /* Round  6 */
    0x01010100, 0x01010000, 0x00010101, 0x00000000,
    0x01010100, 0x00000101, 0x00010101, 0x00000000, /* Round  7 */
    0x00000001, 0x01010000, 0x01000000, 0x01010101,
    0x01010100, 0x01010000, 0x00010101, 0x01010101, /* Round  8 */
    0x00000001, 0x00000101, 0x01000000, 0x00000000,
    0x00000001, 0x01010000, 0x01000000, 0x01010101, /* Round  9 */
    0x01010100, 0x00000101, 0x00010101, 0x00000000,
    0x01010100, 0x00000101, 0x01000000, 0x00000000, /* Round 10 */
    0x00000001, 0x01010000, 0x00010101, 0x01010101,
    0x00000001, 0x01010000, 0x00010101, 0x00000000, /* Round 11 */
    0x01010100, 0x00000101, 0x01000000, 0x01010101,
    0x01010100, 0x00000101, 0x01000000, 0x01010101, /* Round 12 */
    0x00000001, 0x01010000, 0x00010101, 0x00000000
Rhys Weatherley committed
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
};

/**
 * \brief Evaluates the PHOTON-256 S-box in bit-sliced form.
 *
 * \param x0 Slice with bit 0 of all nibbles.
 * \param x1 Slice with bit 1 of all nibbles.
 * \param x2 Slice with bit 2 of all nibbles.
 * \param x3 Slice with bit 3 of all nibbles.
 *
 * This bit-sliced S-box implementation is based on the AVR version
 * "add_avr8_bitslice_asm" from the PHOTON-Beetle reference code.
 */
#define photon256_sbox(x0, x1, x2, x3) \
    do { \
        x1 ^= x2; \
        x3 ^= (x2 & x1); \
        t1 = x3; \
        x3 = (x3 & x1) ^ x2; \
        t2 = x3; \
        x3 ^= x0; \
        x3 = ~(x3); \
        x2 = x3; \
        t2 |= x0; \
        x0 ^= t1; \
        x1 ^= x0; \
        x2 |= x1; \
        x2 ^= t1; \
        x1 ^= t2; \
        x3 ^= x1; \
    } while (0)

/* http://programming.sirrida.de/perm_fn.html#bit_permute_step */
#define bit_permute_step(_y, mask, shift) \
    do { \
        uint32_t y = (_y); \
        uint32_t t = ((y >> (shift)) ^ y) & (mask); \
        (_y) = (y ^ t) ^ (t << (shift)); \
    } while (0)

109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
/* To convert to bit-sliced form, we first scatter bits 0..3 of the nibbles
 * to bytes 0..3 of the words.  Then we rearrange the bytes to group all
 * bits N into word N.
 *
 * Permutation generated with "http://programming.sirrida.de/calcperm.php".
 *
 * P = [0 8 16 24 1 9 17 25 2 10 18 26 3 11 19 27
 *      4 12 20 28 5 13 21 29 6 14 22 30 7 15 23 31]
 */
#define TO_BITSLICED_PERM(x) \
    do { \
        bit_permute_step(x, 0x0a0a0a0a, 3); \
        bit_permute_step(x, 0x00cc00cc, 6); \
        bit_permute_step(x, 0x0000f0f0, 12); \
        bit_permute_step(x, 0x0000ff00, 8); \
    } while (0)
#define FROM_BITSLICED_PERM(x) \
    do { \
        bit_permute_step(x, 0x00aa00aa, 7); \
        bit_permute_step(x, 0x0000cccc, 14); \
        bit_permute_step(x, 0x00f000f0, 4); \
        bit_permute_step(x, 0x0000ff00, 8); \
    } while (0)

Rhys Weatherley committed
133
/**
134
 * \brief Converts half of the PHOTON-256 state into bit-sliced form.
Rhys Weatherley committed
135
 *
136 137 138 139 140 141 142
 * \param s0 First word of the state half on output.
 * \param s1 Second word of the state half on output.
 * \param s2 Third word of the state half on output.
 * \param s3 Fourth word of the state half on output.
 * \param in Points to the input bytes to convert.
 *
 * Assumes temporary variables t0, t1, t2, and t3 are in the calling scope.
Rhys Weatherley committed
143
 */
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
#define photon256_to_sliced_half(s0, s1, s2, s3, in) \
    do { \
        t0 = le_load_word32((in)); \
        t1 = le_load_word32((in) + 4); \
        t2 = le_load_word32((in) + 8); \
        t3 = le_load_word32((in) + 12); \
        TO_BITSLICED_PERM(t0); \
        TO_BITSLICED_PERM(t1); \
        TO_BITSLICED_PERM(t2); \
        TO_BITSLICED_PERM(t3); \
        (s0) = (t0 & 0x000000FFU) | ((t1 << 8) & 0x0000FF00U) | \
               ((t2 << 16) & 0x00FF0000U) | ((t3 << 24) & 0xFF000000U); \
        (s1) = ((t0 >> 8) & 0x000000FFU) | (t1 & 0x0000FF00U) | \
               ((t2 << 8) & 0x00FF0000U) | ((t3 << 16) & 0xFF000000U); \
        (s2) = ((t0 >> 16) & 0x000000FFU) | ((t1 >> 8) & 0x0000FF00U) | \
               (t2 & 0x00FF0000U) | ((t3 << 8) & 0xFF000000U); \
        (s3) = ((t0 >> 24) & 0x000000FFU) | ((t1 >> 16) & 0x0000FF00U) | \
               ((t2 >> 8) & 0x00FF0000U) | (t3 & 0xFF000000U); \
    } while (0)
Rhys Weatherley committed
163 164

/**
165 166 167 168 169 170 171
 * \brief Converts half of the PHOTON-256 state into bit-sliced form.
 *
 * \param out Points to the output buffer.
 * \param s0 First word of the state half on input.
 * \param s1 Second word of the state half on input.
 * \param s2 Third word of the state half on input.
 * \param s3 Fourth word of the state half on input.
Rhys Weatherley committed
172
 *
173
 * Assumes temporary variables t0, t1, t2, and t3 are in the calling scope.
Rhys Weatherley committed
174
 */
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
#define photon256_from_sliced_half(out, s0, s1, s2, s3) \
    do { \
        t0 = ((s0) & 0x000000FFU) | (((s1) & 0x000000FFU) << 8) | \
             (((s2) & 0x000000FFU) << 16) | (((s3) & 0x000000FFU) << 24); \
        t1 = (((s0) & 0x0000FF00U) >> 8) | ((s1) & 0x0000FF00U) | \
             (((s2) & 0x0000FF00U) << 8) | (((s3) & 0x0000FF00U) << 16); \
        t2 = (((s0) & 0x00FF0000U) >> 16) | (((s1) & 0x00FF0000U) >> 8) | \
             ((s2) & 0x00FF0000U) | (((s3) & 0x00FF0000U) << 8); \
        t3 = (((s0) & 0xFF000000U) >> 24) | (((s1) & 0xFF000000U) >> 16) | \
             (((s2) & 0xFF000000U) >> 8) | ((s3) & 0xFF000000U); \
        FROM_BITSLICED_PERM(t0); \
        FROM_BITSLICED_PERM(t1); \
        FROM_BITSLICED_PERM(t2); \
        FROM_BITSLICED_PERM(t3); \
        le_store_word32((out),      t0); \
        le_store_word32((out) + 4,  t1); \
        le_store_word32((out) + 8,  t2); \
        le_store_word32((out) + 12, t3); \
    } while (0)
Rhys Weatherley committed
194 195 196 197

#if defined(LW_UTIL_LITTLE_ENDIAN)
/* Index the bit-sliced state bytes in little-endian byte order */
#define READ_ROW0() \
198 199 200 201
     (((uint32_t)(S.B[0])) | \
     (((uint32_t)(S.B[4]))  << 8)  | \
     (((uint32_t)(S.B[8]))  << 16) | \
     (((uint32_t)(S.B[12])) << 24))
Rhys Weatherley committed
202
#define READ_ROW1() \
203 204 205 206
     (((uint32_t)(S.B[1])) | \
     (((uint32_t)(S.B[5]))  << 8)  | \
     (((uint32_t)(S.B[9]))  << 16) | \
     (((uint32_t)(S.B[13])) << 24))
Rhys Weatherley committed
207
#define READ_ROW2() \
208 209 210 211
     (((uint32_t)(S.B[2])) | \
     (((uint32_t)(S.B[6]))  << 8)  | \
     (((uint32_t)(S.B[10])) << 16) | \
     (((uint32_t)(S.B[14])) << 24))
Rhys Weatherley committed
212
#define READ_ROW3() \
213 214 215 216
     (((uint32_t)(S.B[3])) | \
     (((uint32_t)(S.B[7]))  << 8)  | \
     (((uint32_t)(S.B[11])) << 16) | \
     (((uint32_t)(S.B[15])) << 24))
Rhys Weatherley committed
217
#define READ_ROW4() \
218 219 220 221
     (((uint32_t)(S.B[16])) | \
     (((uint32_t)(S.B[20])) << 8)  | \
     (((uint32_t)(S.B[24])) << 16) | \
     (((uint32_t)(S.B[28])) << 24))
Rhys Weatherley committed
222
#define READ_ROW5() \
223 224 225 226
     (((uint32_t)(S.B[17])) | \
     (((uint32_t)(S.B[21])) << 8)  | \
     (((uint32_t)(S.B[25])) << 16) | \
     (((uint32_t)(S.B[29])) << 24))
Rhys Weatherley committed
227
#define READ_ROW6() \
228 229 230 231
     (((uint32_t)(S.B[18])) | \
     (((uint32_t)(S.B[22])) << 8)  | \
     (((uint32_t)(S.B[26])) << 16) | \
     (((uint32_t)(S.B[30])) << 24))
Rhys Weatherley committed
232
#define READ_ROW7() \
233 234 235 236
     (((uint32_t)(S.B[19])) | \
     (((uint32_t)(S.B[23])) << 8)  | \
     (((uint32_t)(S.B[27])) << 16) | \
     (((uint32_t)(S.B[31])) << 24))
Rhys Weatherley committed
237 238 239
#define WRITE_ROW(row, value) \
    do { \
        if ((row) < 4) { \
240 241 242 243
            state->B[(row)]      = (uint8_t)(value); \
            state->B[(row) + 4]  = (uint8_t)((value) >> 8); \
            state->B[(row) + 8]  = (uint8_t)((value) >> 16); \
            state->B[(row) + 12] = (uint8_t)((value) >> 24); \
Rhys Weatherley committed
244
        } else { \
245 246 247 248
            state->B[(row) + 12] = (uint8_t)(value); \
            state->B[(row) + 16] = (uint8_t)((value) >> 8); \
            state->B[(row) + 20] = (uint8_t)((value) >> 16); \
            state->B[(row) + 24] = (uint8_t)((value) >> 24); \
Rhys Weatherley committed
249 250 251
        } \
    } while (0)
#else
252
/* Index the bit-sliced state B in big-endian byte order */
Rhys Weatherley committed
253
#define READ_ROW0() \
254 255 256 257
     (((uint32_t)(S.B[3])) | \
     (((uint32_t)(S.B[7]))  << 8)  | \
     (((uint32_t)(S.B[11])) << 16) | \
     (((uint32_t)(S.B[15])) << 24))
Rhys Weatherley committed
258
#define READ_ROW1() \
259 260 261 262
     (((uint32_t)(S.B[2])) | \
     (((uint32_t)(S.B[6]))  << 8)  | \
     (((uint32_t)(S.B[10])) << 16) | \
     (((uint32_t)(S.B[14])) << 24))
Rhys Weatherley committed
263
#define READ_ROW2() \
264 265 266 267
     (((uint32_t)(S.B[1])) | \
     (((uint32_t)(S.B[5]))  << 8)  | \
     (((uint32_t)(S.B[9]))  << 16) | \
     (((uint32_t)(S.B[13])) << 24))
Rhys Weatherley committed
268
#define READ_ROW3() \
269 270 271 272
     (((uint32_t)(S.B[0])) | \
     (((uint32_t)(S.B[4]))  << 8)  | \
     (((uint32_t)(S.B[8]))  << 16) | \
     (((uint32_t)(S.B[12])) << 24))
Rhys Weatherley committed
273
#define READ_ROW4() \
274 275 276 277
     (((uint32_t)(S.B[19])) | \
     (((uint32_t)(S.B[23])) << 8)  | \
     (((uint32_t)(S.B[27])) << 16) | \
     (((uint32_t)(S.B[31])) << 24))
Rhys Weatherley committed
278
#define READ_ROW5() \
279 280 281 282
     (((uint32_t)(S.B[18])) | \
     (((uint32_t)(S.B[22])) << 8)  | \
     (((uint32_t)(S.B[26])) << 16) | \
     (((uint32_t)(S.B[30])) << 24))
Rhys Weatherley committed
283
#define READ_ROW6() \
284 285 286 287
     (((uint32_t)(S.B[17])) | \
     (((uint32_t)(S.B[21])) << 8)  | \
     (((uint32_t)(S.B[25])) << 16) | \
     (((uint32_t)(S.B[29])) << 24))
Rhys Weatherley committed
288
#define READ_ROW7() \
289 290 291 292
     (((uint32_t)(S.B[16])) | \
     (((uint32_t)(S.B[20])) << 8)  | \
     (((uint32_t)(S.B[24])) << 16) | \
     (((uint32_t)(S.B[28])) << 24))
Rhys Weatherley committed
293 294 295
#define WRITE_ROW(row, value) \
    do { \
        if ((row) < 4) { \
296 297 298 299
            state->B[3  - (row)] = (uint8_t)(value); \
            state->B[7  - (row)] = (uint8_t)((value) >> 8); \
            state->B[11 - (row)] = (uint8_t)((value) >> 16); \
            state->B[15 - (row)] = (uint8_t)((value) >> 24); \
Rhys Weatherley committed
300
        } else { \
301 302 303 304
            state->B[20 - (row)] = (uint8_t)(value); \
            state->B[24 - (row)] = (uint8_t)((value) >> 8); \
            state->B[28 - (row)] = (uint8_t)((value) >> 16); \
            state->B[32 - (row)] = (uint8_t)((value) >> 24); \
Rhys Weatherley committed
305 306 307 308
        } \
    } while (0)
#endif

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
/* Rotate all rows left by the row number.
 *
 * We do this by applying permutations to the top and bottom words
 * to rearrange the bits into the rotated form.  Permutations
 * generated with "http://programming.sirrida.de/calcperm.php".
 *
 * P_top = [0 1 2 3 4 5 6 7 15 8 9 10 11 12 13 14 22 23
 *          16 17 18 19 20 21 29 30 31 24 25 26 27 28]
 * P_bot = [4 5 6 7 0 1 2 3 11 12 13 14 15 8 9 10 18 19
 *          20 21 22 23 16 17 25 26 27 28 29 30 31 24
 */
#define TOP_ROTATE_PERM(x) \
    do { \
        t1 = (x); \
        bit_permute_step(t1, 0x07030100, 4); \
        bit_permute_step(t1, 0x22331100, 2); \
        bit_permute_step(t1, 0x55005500, 1); \
        (x) = t1; \
    } while (0)
#define BOTTOM_ROTATE_PERM(x) \
    do { \
        t1 = (x); \
        bit_permute_step(t1, 0x080c0e0f, 4); \
        bit_permute_step(t1, 0x22331100, 2); \
        bit_permute_step(t1, 0x55005500, 1); \
        (x) = t1; \
    } while (0)

void photon256_permute(photon256_state_t *state)
Rhys Weatherley committed
338
{
339 340 341 342
    uint32_t s0, s1, s2, s3;
    uint32_t t0, t1, t2, t3;
    uint32_t t4, t5, t6, t7;
    const uint32_t *rc = photon256_rc;
Rhys Weatherley committed
343 344
    uint8_t round;

345 346 347 348 349 350 351 352 353 354 355
    /* Temporary state to convert from column order to row order */
    photon256_state_t S;

    /* Convert the state into bit-sliced form.  The bottom half of the
     * state is left in memory with the top half in local variables */
    photon256_to_sliced_half(s0, s1, s2, s3, state->B + 16);
    state->W[4] = s0;
    state->W[5] = s1;
    state->W[6] = s2;
    state->W[7] = s3;
    photon256_to_sliced_half(s0, s1, s2, s3, state->B);
Rhys Weatherley committed
356

357 358 359
    /* Perform all 12 permutation rounds.  To reduce the register pressure
     * on the CPU, we operate on half of the state at a time: top, bottom,
     * left, or right depending upon the step */
Rhys Weatherley committed
360
    for (round = 0; round < PHOTON256_ROUNDS; ++round) {
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
        /* Apply the round constants to the top half of the state */
        s0 ^= rc[0];
        s1 ^= rc[1];
        s2 ^= rc[2];
        s3 ^= rc[3];

        /* Apply the sbox to the top half of the state */
        photon256_sbox(s0, s1, s2, s3);

        /* Rotate the rows of the top half by 0..3 bit positions and store */
        TOP_ROTATE_PERM(s0);
        TOP_ROTATE_PERM(s1);
        TOP_ROTATE_PERM(s2);
        TOP_ROTATE_PERM(s3);
        S.W[0] = s0;
        S.W[1] = s1;
        S.W[2] = s2;
        S.W[3] = s3;

        /* Load the bottom half of the state */
        s0 = state->W[4];
        s1 = state->W[5];
        s2 = state->W[6];
        s3 = state->W[7];

        /* Apply the round constants to the bottom half of the state */
        s0 ^= rc[4];
        s1 ^= rc[5];
        s2 ^= rc[6];
        s3 ^= rc[7];
        rc += 8;

        /* Apply the sbox to the bottom half of the state */
        photon256_sbox(s0, s1, s2, s3);

        /* Rotate the rows of the bottom half by 4..7 bit positions and store */
        BOTTOM_ROTATE_PERM(s0);
        BOTTOM_ROTATE_PERM(s1);
        BOTTOM_ROTATE_PERM(s2);
        BOTTOM_ROTATE_PERM(s3);
        S.W[4] = s0;
        S.W[5] = s1;
        S.W[6] = s2;
        S.W[7] = s3;
Rhys Weatherley committed
405

406 407 408 409 410 411 412 413 414 415 416 417 418
        /* Mixing the columns; process the left half of the state */
        s0 = READ_ROW0();
        s1 = READ_ROW1();
        s2 = READ_ROW2();
        s3 = READ_ROW3();
        MIXL0(t0, s0, s1, s2, s3);
        MIXL1(t1, s0, s1, s2, s3);
        MIXL2(t2, s0, s1, s2, s3);
        MIXL3(t3, s0, s1, s2, s3);
        MIXL4(t4, s0, s1, s2, s3);
        MIXL5(t5, s0, s1, s2, s3);
        MIXL6(t6, s0, s1, s2, s3);
        MIXL7(t7, s0, s1, s2, s3);
Rhys Weatherley committed
419

420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
        /* Mixing the columns; process the right half of the state */
        s0 = READ_ROW4();
        s1 = READ_ROW5();
        s2 = READ_ROW6();
        s3 = READ_ROW7();
        MIXR4(t4, s0, s1, s2, s3);
        MIXR5(t5, s0, s1, s2, s3);
        MIXR6(t6, s0, s1, s2, s3);
        MIXR7(t7, s0, s1, s2, s3);
        WRITE_ROW(4, t4);
        WRITE_ROW(5, t5);
        WRITE_ROW(6, t6);
        WRITE_ROW(7, t7);
        MIXR0(t0, s0, s1, s2, s3);
        MIXR1(t1, s0, s1, s2, s3);
        MIXR2(t2, s0, s1, s2, s3);
        MIXR3(t3, s0, s1, s2, s3);
        WRITE_ROW(0, t0);
        WRITE_ROW(1, t1);
        WRITE_ROW(2, t2);
        WRITE_ROW(3, t3);
Rhys Weatherley committed
441

442 443 444 445 446
        /* Reload the top half of the state for the next round */
        s0 = state->W[0];
        s1 = state->W[1];
        s2 = state->W[2];
        s3 = state->W[3];
Rhys Weatherley committed
447 448 449
    }

    /* Convert back from bit-sliced form to regular form */
450 451 452 453 454 455
    photon256_from_sliced_half(state->B, s0, s1, s2, s3);
    s0 = state->W[4];
    s1 = state->W[5];
    s2 = state->W[6];
    s3 = state->W[7];
    photon256_from_sliced_half(state->B + 16, s0, s1, s2, s3);
Rhys Weatherley committed
456
}
457

458
#endif /* !PHOTON128_ASM */