internal-gimli24.c 4.82 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 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
/*
 * 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-gimli24.h"

#if !defined(__AVR__)

/* Apply the SP-box to a specific column in the state array */
#define GIMLI24_SP(s0, s4, s8) \
    do { \
        x = leftRotate24(s0); \
        y = leftRotate9(s4); \
        s4 = y ^ x ^ ((x | s8) << 1); \
        s0 = s8 ^ y ^ ((x & y) << 3); \
        s8 = x ^ (s8 << 1) ^ ((y & s8) << 2); \
    } while (0)

void gimli24_permute(uint32_t state[12])
{
    uint32_t s0, s1, s2, s3, s4,  s5;
    uint32_t s6, s7, s8, s9, s10, s11;
    uint32_t x, y;
    unsigned round;

    /* Load the state into local variables and convert from little-endian */
#if defined(LW_UTIL_LITTLE_ENDIAN)
    s0  = state[0];
    s1  = state[1];
    s2  = state[2];
    s3  = state[3];
    s4  = state[4];
    s5  = state[5];
    s6  = state[6];
    s7  = state[7];
    s8  = state[8];
    s9  = state[9];
    s10 = state[10];
    s11 = state[11];
#else
    s0  = le_load_word32((const unsigned char *)(&(state[0])));
    s1  = le_load_word32((const unsigned char *)(&(state[1])));
    s2  = le_load_word32((const unsigned char *)(&(state[2])));
    s3  = le_load_word32((const unsigned char *)(&(state[3])));
    s4  = le_load_word32((const unsigned char *)(&(state[4])));
    s5  = le_load_word32((const unsigned char *)(&(state[5])));
    s6  = le_load_word32((const unsigned char *)(&(state[6])));
    s7  = le_load_word32((const unsigned char *)(&(state[7])));
    s8  = le_load_word32((const unsigned char *)(&(state[8])));
    s9  = le_load_word32((const unsigned char *)(&(state[9])));
    s10 = le_load_word32((const unsigned char *)(&(state[10])));
    s11 = le_load_word32((const unsigned char *)(&(state[11])));
#endif

    /* Unroll and perform the rounds 4 at a time */
    for (round = 24; round > 0; round -= 4) {
        /* Round 0: SP-box, small swap, add round constant */
        GIMLI24_SP(s0, s4, s8);
        GIMLI24_SP(s1, s5, s9);
        GIMLI24_SP(s2, s6, s10);
        GIMLI24_SP(s3, s7, s11);
        x = s0;
        y = s2;
        s0 = s1 ^ 0x9e377900U ^ round;
        s1 = x;
        s2 = s3;
        s3 = y;

        /* Round 1: SP-box only */
        GIMLI24_SP(s0, s4, s8);
        GIMLI24_SP(s1, s5, s9);
        GIMLI24_SP(s2, s6, s10);
        GIMLI24_SP(s3, s7, s11);

        /* Round 2: SP-box, big swap */
        GIMLI24_SP(s0, s4, s8);
        GIMLI24_SP(s1, s5, s9);
        GIMLI24_SP(s2, s6, s10);
        GIMLI24_SP(s3, s7, s11);
        x = s0;
        y = s1;
        s0 = s2;
        s1 = s3;
        s2 = x;
        s3 = y;

        /* Round 3: SP-box only */
        GIMLI24_SP(s0, s4, s8);
        GIMLI24_SP(s1, s5, s9);
        GIMLI24_SP(s2, s6, s10);
        GIMLI24_SP(s3, s7, s11);
    }

    /* Convert state to little-endian if the platform is not little-endian */
#if defined(LW_UTIL_LITTLE_ENDIAN)
    state[0]  = s0;
    state[1]  = s1;
    state[2]  = s2;
    state[3]  = s3;
    state[4]  = s4;
    state[5]  = s5;
    state[6]  = s6;
    state[7]  = s7;
    state[8]  = s8;
    state[9]  = s9;
    state[10] = s10;
    state[11] = s11;
#else
    le_store_word32(((unsigned char *)(&(state[0]))),  s0);
    le_store_word32(((unsigned char *)(&(state[1]))),  s1);
    le_store_word32(((unsigned char *)(&(state[2]))),  s2);
    le_store_word32(((unsigned char *)(&(state[3]))),  s3);
    le_store_word32(((unsigned char *)(&(state[4]))),  s4);
    le_store_word32(((unsigned char *)(&(state[5]))),  s5);
    le_store_word32(((unsigned char *)(&(state[6]))),  s6);
    le_store_word32(((unsigned char *)(&(state[7]))),  s7);
    le_store_word32(((unsigned char *)(&(state[8]))),  s8);
    le_store_word32(((unsigned char *)(&(state[9]))),  s9);
    le_store_word32(((unsigned char *)(&(state[10]))), s10);
    le_store_word32(((unsigned char *)(&(state[11]))), s11);
#endif
}

#endif /* !__AVR__ */