internal-forkskinny.c 13.4 KB
Newer Older
Arne Deprez 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
/*
 * 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-forkskinny.h"
#include "internal-skinnyutil.h"

/**
 * \brief 7-bit round constants for all ForkSkinny block ciphers.
 */
static unsigned char const RC[87] = {
    0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7e, 0x7d,
    0x7b, 0x77, 0x6f, 0x5f, 0x3e, 0x7c, 0x79, 0x73,
    0x67, 0x4f, 0x1e, 0x3d, 0x7a, 0x75, 0x6b, 0x57,
    0x2e, 0x5c, 0x38, 0x70, 0x61, 0x43, 0x06, 0x0d,
    0x1b, 0x37, 0x6e, 0x5d, 0x3a, 0x74, 0x69, 0x53,
    0x26, 0x4c, 0x18, 0x31, 0x62, 0x45, 0x0a, 0x15,
    0x2b, 0x56, 0x2c, 0x58, 0x30, 0x60, 0x41, 0x02,
    0x05, 0x0b, 0x17, 0x2f, 0x5e, 0x3c, 0x78, 0x71,
    0x63, 0x47, 0x0e, 0x1d, 0x3b, 0x76, 0x6d, 0x5b,
    0x36, 0x6c, 0x59, 0x32, 0x64, 0x49, 0x12, 0x25,
    0x4a, 0x14, 0x29, 0x52, 0x24, 0x48, 0x10
};

Arne Deprez committed
43
#if !defined(__AVR__)
Arne Deprez committed
44

Arne Deprez committed
45
void forkskinny_128_256_init_tks(forkskinny_128_256_tweakey_schedule_t *tks, const unsigned char key[32], uint8_t nb_rounds)
Arne Deprez committed
46 47 48 49 50 51 52 53 54 55 56
{
	uint32_t TK[4];
	unsigned round;

	/* Load first Tweakey */
	TK[0] = le_load_word32(key);
	TK[1] = le_load_word32(key + 4);
	TK[2] = le_load_word32(key + 8);
	TK[3] = le_load_word32(key + 12);
	/* Initiate key schedule with permutations of TK1 */
	for(round = 0; round<nb_rounds; round++){
Arne Deprez committed
57 58
		tks->row0[round] = TK[0];
		tks->row1[round] = TK[1];
Arne Deprez committed
59 60 61 62 63 64 65 66 67 68 69

		skinny128_permute_tk(TK);
	}

	/* Load second Tweakey */
	TK[0] = le_load_word32(key + 16);
	TK[1] = le_load_word32(key + 20);
	TK[2] = le_load_word32(key + 24);
	TK[3] = le_load_word32(key + 28);
	/* Process second Tweakey and add it to the key schedule */
	for(round = 0; round<nb_rounds; round++){
Arne Deprez committed
70 71
		tks->row0[round] ^= TK[0];
		tks->row1[round] ^= TK[1];
Arne Deprez committed
72 73 74 75 76 77 78

		skinny128_permute_tk(TK);
		skinny128_LFSR2(TK[0]);
		skinny128_LFSR2(TK[1]);
	}
}

Arne Deprez committed
79 80
void forkskinny_128_256_rounds
    (forkskinny_128_256_state_t *state, forkskinny_128_256_tweakey_schedule_t *tks, unsigned first, unsigned last)
Arne Deprez committed
81 82 83 84 85
{
    uint32_t s0, s1, s2, s3, temp;
    uint8_t rc;

    /* Load the state into local variables */
Arne Deprez committed
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
    s0 = state->S[0];
    s1 = state->S[1];
    s2 = state->S[2];
    s3 = state->S[3];

    /* Perform all requested rounds */
    for (; first < last; ++first) {
        /* Apply the S-box to all cells in the state */
        skinny128_sbox(s0);
        skinny128_sbox(s1);
        skinny128_sbox(s2);
        skinny128_sbox(s3);

        /* XOR the round constant and the subkey for this round */
        rc = RC[first];
        s0 ^= tks->row0[first] ^ (rc & 0x0F) ^ 0x00020000;
        s1 ^= tks->row1[first] ^ (rc >> 4);
        s2 ^= 0x02;

        /* Shift the cells in the rows right, which moves the cell
         * values up closer to the MSB.  That is, we do a left rotate
         * on the word to rotate the cells in the word right */
        s1 = leftRotate8(s1);
        s2 = leftRotate16(s2);
        s3 = leftRotate24(s3);

        /* Mix the columns */
        s1 ^= s2;
        s2 ^= s0;
        temp = s3 ^ s2;
        s3 = s2;
        s2 = s1;
        s1 = s0;
        s0 = temp;
    }
Arne Deprez committed
121 122 123 124 125 126 127 128

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

Arne Deprez committed
129 130
void forkskinny_128_256_inv_rounds
    (forkskinny_128_256_state_t *state, forkskinny_128_256_tweakey_schedule_t *tks, unsigned first, unsigned last)
Arne Deprez committed
131 132 133 134 135 136 137 138 139 140
{
    uint32_t s0, s1, s2, s3, temp;
    uint8_t rc;

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

Arne Deprez committed
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
    /* Perform all requested rounds */
    while (first > last) {

        /* Inverse mix of the columns */
        temp = s0;
        s0 = s1;
        s1 = s2;
        s2 = s3;
        s3 = temp ^ s2;
        s2 ^= s0;
        s1 ^= s2;

        /* Shift the cells in the rows left, which moves the cell
         * values down closer to the LSB.  That is, we do a right
         * rotate on the word to rotate the cells in the word left */
        s1 = rightRotate8(s1);
        s2 = rightRotate16(s2);
        s3 = rightRotate24(s3);

        /* XOR the round constant and the subkey for this round */
        rc = RC[--first];
        s0 ^= tks->row0[first] ^ (rc & 0x0F) ^ 0x00020000;
        s1 ^= tks->row1[first] ^ (rc >> 4);
        s2 ^= 0x02;

        /* Apply the inverse of the S-box to all cells in the state */
        skinny128_inv_sbox(s0);
        skinny128_inv_sbox(s1);
        skinny128_inv_sbox(s2);
        skinny128_inv_sbox(s3);
    }
Arne Deprez committed
172 173 174 175 176 177 178 179

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

Arne Deprez committed
180
void forkskinny_128_384_init_tks(forkskinny_128_384_tweakey_schedule_t *tks, const unsigned char key[48], uint8_t nb_rounds)
Arne Deprez committed
181 182 183 184 185 186 187 188 189 190 191
{
	uint32_t TK[4];
	unsigned round;

	/* Load first Tweakey */
	TK[0] = le_load_word32(key);
	TK[1] = le_load_word32(key + 4);
	TK[2] = le_load_word32(key + 8);
	TK[3] = le_load_word32(key + 12);
	/* Initiate key schedule with permutations of TK1 */
	for(round = 0; round<nb_rounds; round++){
Arne Deprez committed
192 193
		tks->row0[round] = TK[0];
		tks->row1[round] = TK[1];
Arne Deprez committed
194 195 196 197 198 199 200 201 202

		skinny128_permute_tk(TK);
	}

	/* Load second Tweakey */
	TK[0] = le_load_word32(key + 16);
	TK[1] = le_load_word32(key + 20);
	TK[2] = le_load_word32(key + 24);
	TK[3] = le_load_word32(key + 28);
Arne Deprez committed
203

Arne Deprez committed
204 205
	/* Process second Tweakey and add it to the key schedule */
	for(round = 0; round<nb_rounds; round++){
Arne Deprez committed
206 207
		tks->row0[round] ^= TK[0];
		tks->row1[round] ^= TK[1];
Arne Deprez committed
208 209 210 211 212 213 214 215 216 217 218

		skinny128_permute_tk(TK);
		skinny128_LFSR2(TK[0]);
		skinny128_LFSR2(TK[1]);
	}

	/* Load third Tweakey */
	TK[0] = le_load_word32(key + 32);
	TK[1] = le_load_word32(key + 36);
	TK[2] = le_load_word32(key + 40);
	TK[3] = le_load_word32(key + 44);
Arne Deprez committed
219

Arne Deprez committed
220 221
	/* Process third Tweakey and add it to the key schedule */
	for(round = 0; round<nb_rounds; round++){
Arne Deprez committed
222 223
		tks->row0[round] ^= TK[0];
		tks->row1[round] ^= TK[1];
Arne Deprez committed
224 225 226 227 228 229 230

		skinny128_permute_tk(TK);
		skinny128_LFSR3(TK[0]);
		skinny128_LFSR3(TK[1]);
	}
}

Arne Deprez committed
231 232
void forkskinny_128_384_rounds
    (forkskinny_128_384_state_t *state, forkskinny_128_384_tweakey_schedule_t *tks, unsigned first, unsigned last)
Arne Deprez committed
233 234 235 236 237
{
    uint32_t s0, s1, s2, s3, temp;
    uint8_t rc;

    /* Load the state into local variables */
Arne Deprez committed
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
    s0 = state->S[0];
    s1 = state->S[1];
    s2 = state->S[2];
    s3 = state->S[3];

    /* Perform all requested rounds */
    for (; first < last; ++first) {
        /* Apply the S-box to all cells in the state */
        skinny128_sbox(s0);
        skinny128_sbox(s1);
        skinny128_sbox(s2);
        skinny128_sbox(s3);

        /* XOR the round constant and the subkey for this round */
        rc = RC[first];
        s0 ^= tks->row0[first] ^ (rc & 0x0F) ^ 0x00020000;
        s1 ^= tks->row1[first] ^ (rc >> 4);
        s2 ^= 0x02;

        /* Shift the cells in the rows right, which moves the cell
         * values up closer to the MSB.  That is, we do a left rotate
         * on the word to rotate the cells in the word right */
        s1 = leftRotate8(s1);
        s2 = leftRotate16(s2);
        s3 = leftRotate24(s3);

        /* Mix the columns */
        s1 ^= s2;
        s2 ^= s0;
        temp = s3 ^ s2;
        s3 = s2;
        s2 = s1;
        s1 = s0;
        s0 = temp;
    }
Arne Deprez committed
273 274 275 276 277 278 279 280

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

Arne Deprez committed
281 282
void forkskinny_128_384_inv_rounds
    (forkskinny_128_384_state_t *state, forkskinny_128_384_tweakey_schedule_t *tks, unsigned first, unsigned last)
Arne Deprez committed
283 284 285 286 287 288 289 290 291 292
{
    uint32_t s0, s1, s2, s3, temp;
    uint8_t rc;

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

Arne Deprez committed
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
    /* Perform all requested rounds */
    while (first > last) {

        /* Inverse mix of the columns */
        temp = s0;
        s0 = s1;
        s1 = s2;
        s2 = s3;
        s3 = temp ^ s2;
        s2 ^= s0;
        s1 ^= s2;

        /* Shift the cells in the rows left, which moves the cell
         * values down closer to the LSB.  That is, we do a right
         * rotate on the word to rotate the cells in the word left */
        s1 = rightRotate8(s1);
        s2 = rightRotate16(s2);
        s3 = rightRotate24(s3);

        /* XOR the round constant and the subkey for this round */
        rc = RC[--first];
        s0 ^= tks->row0[first] ^ (rc & 0x0F) ^ 0x00020000;
        s1 ^= tks->row1[first] ^ (rc >> 4);
        s2 ^= 0x02;

        /* Apply the inverse of the S-box to all cells in the state */
        skinny128_inv_sbox(s0);
        skinny128_inv_sbox(s1);
        skinny128_inv_sbox(s2);
        skinny128_inv_sbox(s3);
    }
Arne Deprez committed
324 325 326 327 328 329 330 331 332

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


Arne Deprez committed
333
void forkskinny_64_192_init_tks(forkskinny_64_192_tweakey_schedule_t *tks, const unsigned char key[24], uint8_t nb_rounds)
Arne Deprez committed
334 335 336 337 338 339 340 341 342 343 344
{
	uint16_t TK[4];
	unsigned round;

	/* Load first Tweakey */
	TK[0] = be_load_word16(key);
	TK[1] = be_load_word16(key + 2);
	TK[2] = be_load_word16(key + 4);
	TK[3] = be_load_word16(key + 6);
	/* Initiate key schedule with permutations of TK1 */
	for(round = 0; round<nb_rounds; round++){
Arne Deprez committed
345 346
		tks->row0[round] = TK[0];
		tks->row1[round] = TK[1];
Arne Deprez committed
347 348 349 350 351 352 353 354 355

		skinny64_permute_tk(TK);
	}

	/* Load second Tweakey */
	TK[0] = be_load_word16(key + 8);
	TK[1] = be_load_word16(key + 10);
	TK[2] = be_load_word16(key + 12);
	TK[3] = be_load_word16(key + 14);
Arne Deprez committed
356

Arne Deprez committed
357 358
	/* Process second Tweakey and add it to the key schedule */
	for(round = 0; round<nb_rounds; round++){
Arne Deprez committed
359 360
		tks->row0[round] ^= TK[0];
		tks->row1[round] ^= TK[1];
Arne Deprez committed
361 362 363 364 365 366 367 368 369 370 371 372 373

		skinny64_permute_tk(TK);
		skinny64_LFSR2(TK[0]);
		skinny64_LFSR2(TK[1]);
	}

	/* Load third Tweakey */
	TK[0] = be_load_word16(key + 16);
	TK[1] = be_load_word16(key + 18);
	TK[2] = be_load_word16(key + 20);
	TK[3] = be_load_word16(key + 22);
	/* Process third Tweakey and add it to the key schedule */
	for(round = 0; round<nb_rounds; round++){
Arne Deprez committed
374 375
		tks->row0[round] ^= TK[0];
		tks->row1[round] ^= TK[1];
Arne Deprez committed
376 377 378 379 380 381 382

		skinny64_permute_tk(TK);
		skinny64_LFSR3(TK[0]);
		skinny64_LFSR3(TK[1]);
	}
}

Arne Deprez committed
383 384
void forkskinny_64_192_rounds
    (forkskinny_64_192_state_t *state, forkskinny_64_192_tweakey_schedule_t *tks, unsigned first, unsigned last)
Arne Deprez committed
385 386 387 388 389 390 391 392 393 394
{
    uint16_t s0, s1, s2, s3, temp;
    uint8_t rc;

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

Arne Deprez committed
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
    /* Perform all requested rounds */
    for (; first < last; ++first) {
        /* Apply the S-box to all cells in the state */
        skinny64_sbox(s0);
        skinny64_sbox(s1);
        skinny64_sbox(s2);
        skinny64_sbox(s3);

        /* XOR the round constant and the subkey for this round */
        rc = RC[first];
        s0 ^= tks->row0[first] ^ ((rc & 0x0F) << 12) ^ 0x0020;
        s1 ^= tks->row1[first] ^ ((rc & 0x70) << 8);
        s2 ^= 0x2000;

        /* Shift the cells in the rows right */
        s1 = rightRotate4_16(s1);
        s2 = rightRotate8_16(s2);
        s3 = rightRotate12_16(s3);

        /* Mix the columns */
        s1 ^= s2;
        s2 ^= s0;
        temp = s3 ^ s2;
        s3 = s2;
        s2 = s1;
        s1 = s0;
        s0 = temp;
    }
Arne Deprez committed
423 424 425 426 427 428 429 430

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

Arne Deprez committed
431 432
void forkskinny_64_192_inv_rounds
    (forkskinny_64_192_state_t *state, forkskinny_64_192_tweakey_schedule_t *tks, unsigned first, unsigned last)
Arne Deprez committed
433 434 435 436 437 438 439 440 441 442
{
    uint16_t s0, s1, s2, s3, temp;
    uint8_t rc;

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

Arne Deprez committed
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
    /* Perform all requested rounds */
    while (first > last) {

        /* Inverse mix of the columns */
        temp = s0;
        s0 = s1;
        s1 = s2;
        s2 = s3;
        s3 = temp ^ s2;
        s2 ^= s0;
        s1 ^= s2;

        /* Shift the cells in the rows left */
        s1 = leftRotate4_16(s1);
        s2 = leftRotate8_16(s2);
        s3 = leftRotate12_16(s3);

        /* XOR the round constant and the subkey for this round */
        rc = RC[--first];
        s0 ^= tks->row0[first] ^ ((rc & 0x0F) << 12) ^ 0x0020;
        s1 ^= tks->row1[first] ^ ((rc & 0x70) << 8);
        s2 ^= 0x2000;

        /* Apply the inverse of the S-box to all cells in the state */
        skinny64_inv_sbox(s0);
        skinny64_inv_sbox(s1);
        skinny64_inv_sbox(s2);
        skinny64_inv_sbox(s3);
    }
Arne Deprez committed
472 473 474 475 476 477 478 479

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

Arne Deprez committed
480
#endif /* !__AVR__ */