internal-tinyjambu.c 9.07 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
/*
 * Copyright (C) 2021 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-tinyjambu.h"
#include "internal-util.h"

/* Determine if the permutations should be accelerated with assembly code */
#if defined(__AVR__)
#define TINYJAMBU_ASM 1
#elif defined(__ARM_ARCH_ISA_THUMB) && __ARM_ARCH == 7
#define TINYJAMBU_ASM 1
#else
#define TINYJAMBU_ASM 0
#endif

#if !TINYJAMBU_ASM

#if !TINY_JAMBU_64BIT

/* Note: The last line should contain ~(t2 & t3) according to the
 * specification but we can avoid the NOT by inverting the words
 * of the key ahead of time. */
#define tiny_jambu_steps_32(s0, s1, s2, s3, kword) \
    do { \
        t1 = (s1 >> 15) | (s2 << 17); \
        t2 = (s2 >> 6)  | (s3 << 26); \
        t3 = (s2 >> 21) | (s3 << 11); \
        t4 = (s2 >> 27) | (s3 << 5); \
        s0 ^= t1 ^ (t2 & t3) ^ t4 ^ kword; \
    } while (0)

void tiny_jambu_permutation_128
    (tiny_jambu_state_t *state, const tiny_jambu_key_word_t *key,
     unsigned rounds)
{
    uint32_t t1, t2, t3, t4;

    /* Load the state into local variables */
    uint32_t s0 = state->s[0];
    uint32_t s1 = state->s[1];
    uint32_t s2 = state->s[2];
    uint32_t s3 = state->s[3];

    /* Perform all permutation rounds 128 at a time */
    for (; rounds > 0; --rounds) {
        /* Perform the first set of 128 steps */
        tiny_jambu_steps_32(s0, s1, s2, s3, key[0]);
        tiny_jambu_steps_32(s1, s2, s3, s0, key[1]);
        tiny_jambu_steps_32(s2, s3, s0, s1, key[2]);
        tiny_jambu_steps_32(s3, s0, s1, s2, key[3]);

        /* Bail out if this is the last round */
        if ((--rounds) == 0)
            break;

        /* Perform the second set of 128 steps */
        tiny_jambu_steps_32(s0, s1, s2, s3, key[0]);
        tiny_jambu_steps_32(s1, s2, s3, s0, key[1]);
        tiny_jambu_steps_32(s2, s3, s0, s1, key[2]);
        tiny_jambu_steps_32(s3, s0, s1, s2, key[3]);
    }

    /* Store the local variables back to the state */
    state->s[0] = s0;
    state->s[1] = s1;
    state->s[2] = s2;
    state->s[3] = s3;
}

void tiny_jambu_permutation_192
    (tiny_jambu_state_t *state, const tiny_jambu_key_word_t *key,
     unsigned rounds)
{
    uint32_t t1, t2, t3, t4;

    /* Load the state into local variables */
    uint32_t s0 = state->s[0];
    uint32_t s1 = state->s[1];
    uint32_t s2 = state->s[2];
    uint32_t s3 = state->s[3];

    /* Perform all permutation rounds 128 at a time */
    for (; rounds > 0; --rounds) {
        /* Perform the first set of 128 steps */
        tiny_jambu_steps_32(s0, s1, s2, s3, key[0]);
        tiny_jambu_steps_32(s1, s2, s3, s0, key[1]);
        tiny_jambu_steps_32(s2, s3, s0, s1, key[2]);
        tiny_jambu_steps_32(s3, s0, s1, s2, key[3]);

        /* Bail out if this is the last round */
        if ((--rounds) == 0)
            break;

        /* Perform the second set of 128 steps */
        tiny_jambu_steps_32(s0, s1, s2, s3, key[4]);
        tiny_jambu_steps_32(s1, s2, s3, s0, key[5]);
        tiny_jambu_steps_32(s2, s3, s0, s1, key[0]);
        tiny_jambu_steps_32(s3, s0, s1, s2, key[1]);

        /* Bail out if this is the last round */
        if ((--rounds) == 0)
            break;

        /* Perform the third set of 128 steps */
        tiny_jambu_steps_32(s0, s1, s2, s3, key[2]);
        tiny_jambu_steps_32(s1, s2, s3, s0, key[3]);
        tiny_jambu_steps_32(s2, s3, s0, s1, key[4]);
        tiny_jambu_steps_32(s3, s0, s1, s2, key[5]);
    }

    /* Store the local variables back to the state */
    state->s[0] = s0;
    state->s[1] = s1;
    state->s[2] = s2;
    state->s[3] = s3;
}

void tiny_jambu_permutation_256
    (tiny_jambu_state_t *state, const tiny_jambu_key_word_t *key,
     unsigned rounds)
{
    uint32_t t1, t2, t3, t4;

    /* Load the state into local variables */
    uint32_t s0 = state->s[0];
    uint32_t s1 = state->s[1];
    uint32_t s2 = state->s[2];
    uint32_t s3 = state->s[3];

    /* Perform all permutation rounds 128 at a time */
    for (; rounds > 0; --rounds) {
        /* Perform the first set of 128 steps */
        tiny_jambu_steps_32(s0, s1, s2, s3, key[0]);
        tiny_jambu_steps_32(s1, s2, s3, s0, key[1]);
        tiny_jambu_steps_32(s2, s3, s0, s1, key[2]);
        tiny_jambu_steps_32(s3, s0, s1, s2, key[3]);

        /* Bail out if this is the last round */
        if ((--rounds) == 0)
            break;

        /* Perform the second set of 128 steps */
        tiny_jambu_steps_32(s0, s1, s2, s3, key[4]);
        tiny_jambu_steps_32(s1, s2, s3, s0, key[5]);
        tiny_jambu_steps_32(s2, s3, s0, s1, key[6]);
        tiny_jambu_steps_32(s3, s0, s1, s2, key[7]);
    }

    /* Store the local variables back to the state */
    state->s[0] = s0;
    state->s[1] = s1;
    state->s[2] = s2;
    state->s[3] = s3;
}

#else /* TINY_JAMBU_64BIT */

/* Perform 64 steps of the TinyJAMBU permutation on 64-bit platforms */
#define tiny_jambu_steps_64(s0, s2, kword0, kword1) \
    do { \
        t1 = (s0 >> 47) | (s2 << 17); \
        t2 = (s2 >> 6); \
        t3 = (s2 >> 21); \
        t4 = (s2 >> 27); \
        s0 ^= t1 ^ (uint32_t)((t2 & t3) ^ t4 ^ kword0); \
        t2 |= (s0 << 58); \
        t3 |= (s0 << 43); \
        t4 |= (s0 << 37); \
        s0 ^= ((t2 & t3) ^ t4 ^ kword1) & 0xFFFFFFFF00000000ULL; \
    } while (0)

void tiny_jambu_permutation_128
    (tiny_jambu_state_t *state, const tiny_jambu_key_word_t *key,
     unsigned rounds)
{
    uint64_t t1, t2, t3, t4;

    /* Load the state into local variables */
    uint64_t s0 = state->t[0];
    uint64_t s2 = state->t[1];

    /* Perform all permutation rounds 128 at a time */
    for (; rounds > 0; --rounds) {
        /* Perform the first set of 128 steps */
        tiny_jambu_steps_64(s0, s2, key[0], key[1]);
        tiny_jambu_steps_64(s2, s0, key[2], key[3]);

        /* Bail out if this is the last round */
        if ((--rounds) == 0)
            break;

        /* Perform the second set of 128 steps */
        tiny_jambu_steps_64(s0, s2, key[0], key[1]);
        tiny_jambu_steps_64(s2, s0, key[2], key[3]);
    }

    /* Store the local variables back to the state */
    state->t[0] = s0;
    state->t[1] = s2;
}

void tiny_jambu_permutation_192
    (tiny_jambu_state_t *state, const tiny_jambu_key_word_t *key,
     unsigned rounds)
{
    uint64_t t1, t2, t3, t4;

    /* Load the state into local variables */
    uint64_t s0 = state->t[0];
    uint64_t s2 = state->t[1];

    /* Perform all permutation rounds 128 at a time */
    for (; rounds > 0; --rounds) {
        /* Perform the first set of 128 steps */
        tiny_jambu_steps_64(s0, s2, key[0], key[1]);
        tiny_jambu_steps_64(s2, s0, key[2], key[3]);

        /* Bail out if this is the last round */
        if ((--rounds) == 0)
            break;

        /* Perform the second set of 128 steps */
        tiny_jambu_steps_64(s0, s2, key[4], key[5]);
        tiny_jambu_steps_64(s2, s0, key[0], key[1]);

        /* Bail out if this is the last round */
        if ((--rounds) == 0)
            break;

        /* Perform the third set of 128 steps */
        tiny_jambu_steps_64(s0, s2, key[2], key[3]);
        tiny_jambu_steps_64(s2, s0, key[4], key[5]);
    }

    /* Store the local variables back to the state */
    state->t[0] = s0;
    state->t[1] = s2;
}

void tiny_jambu_permutation_256
    (tiny_jambu_state_t *state, const tiny_jambu_key_word_t *key,
     unsigned rounds)
{
    uint64_t t1, t2, t3, t4;

    /* Load the state into local variables */
    uint64_t s0 = state->t[0];
    uint64_t s2 = state->t[1];

    /* Perform all permutation rounds 128 at a time */
    for (; rounds > 0; --rounds) {
        /* Perform the first set of 128 steps */
        tiny_jambu_steps_64(s0, s2, key[0], key[1]);
        tiny_jambu_steps_64(s2, s0, key[2], key[3]);

        /* Bail out if this is the last round */
        if ((--rounds) == 0)
            break;

        /* Perform the second set of 128 steps */
        tiny_jambu_steps_64(s0, s2, key[4], key[5]);
        tiny_jambu_steps_64(s2, s0, key[6], key[7]);
    }

    /* Store the local variables back to the state */
    state->t[0] = s0;
    state->t[1] = s2;
}

#endif /* TINY_JAMBU_64BIT */

#endif /* !TINYJAMBU_ASM */