masked_pyjamask.c 15.8 KB
Newer Older
lwc-tester 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 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 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 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 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 381 382 383 384 385 386 387 388 389 390 391 392 393 394 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 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 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 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
/*
  ===============================================================================

 Copyright (c) 2019, CryptoExperts and PQShield Ltd.
 
 All rights reserved. A copyright license for redistribution and use in
 source and binary forms, with or without modification, is hereby granted for
 non-commercial, experimental, research, public review and evaluation
 purposes, provided that the following conditions are met:
 
 * Redistributions of source code must retain the above copyright notice,
   this list of conditions and the following disclaimer.

 * Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 POSSIBILITY OF SUCH DAMAGE.

  Authors: Dahmun Goudarzi, Matthieu Rivain

  ===============================================================================
*/

#include <stdint.h>
#include "api.h"



//==============================================================================
//=== Definitions
//==============================================================================

#define STATE_SIZE_96        3
#define STATE_SIZE_128       4

#define NB_ROUNDS_96        14
#define NB_ROUNDS_128       14
#define NB_ROUNDS_KS        14

#define WITH_CONST_ADD       0
#define WOUT_CONST_ADD       1

//==============================================================================
//=== Macros
//==============================================================================

#define right_rotate(row)			\
  row = (row >> 1) | (row << 31);

#define left_rotate(row,n)			\
  row = (row >> n) | (row << (32-n));

//==============================================================================
//=== Constants
//==============================================================================

#define COL_M0        0xa3861085
#define COL_M1        0x63417021
#define COL_M2        0x692cf280
#define COL_M3        0x48a54813
#define COL_MK        0xb881b9ca

#define COL_INV_M0    0x2037a121
#define COL_INV_M1    0x108ff2a0 
#define COL_INV_M2    0x9054d8c0 
#define COL_INV_M3    0x3354b117

#define KS_CONSTANT_0   0x00000080
#define KS_CONSTANT_1   0x00006a00
#define KS_CONSTANT_2   0x003f0000
#define KS_CONSTANT_3   0x24000000

#define KS_ROT_GAP1            8
#define KS_ROT_GAP2           15
#define KS_ROT_GAP3           18

//==============================================================================
//=== Declarations (assembly functions)
//==============================================================================

uint32_t mat_mult(uint32_t mat_col, uint32_t vec);

void isw_macc_96 (uint32_t state[MASKING_ORDER][STATE_SIZE_96],  int acc, int op1, int op2);
void isw_macc_128(uint32_t state[MASKING_ORDER][STATE_SIZE_128], int acc, int op1, int op2);

//==============================================================================
//=== Common functions
//==============================================================================

void load_state(const uint8_t *plaintext, uint32_t *state, int state_size)
{
    int i;

    for (i=0; i<state_size; i++)
    {
        state[i] =                   plaintext[4*i+0];
        state[i] = (state[i] << 8) | plaintext[4*i+1];
        state[i] = (state[i] << 8) | plaintext[4*i+2];
        state[i] = (state[i] << 8) | plaintext[4*i+3];
    }
}

void unload_state(uint8_t *ciphertext, const uint32_t *state, int state_size)
{
    int i;

    for (i=0; i<state_size; i++)
    {
        ciphertext [4*i+0] = (uint8_t) (state[i] >> 24);
        ciphertext [4*i+1] = (uint8_t) (state[i] >> 16);
        ciphertext [4*i+2] = (uint8_t) (state[i] >>  8);
        ciphertext [4*i+3] = (uint8_t) (state[i] >>  0);
    }
}

//==============================================================================
//=== Masking functions
//==============================================================================

void mask_state_96(uint32_t state[MASKING_ORDER][STATE_SIZE_96])
{
    int i,j;

    for (i=1; i<MASKING_ORDER; i++)
    {
        for (j=0; j<STATE_SIZE_96; j++)
        {
            state[i][j] = get_random();
            state[0][j] ^= state[i][j];
        }
    }
}

void unmask_state_96(uint32_t state[MASKING_ORDER][STATE_SIZE_96])
{
    int i,j;

    for (i=1; i<MASKING_ORDER; i++)
    {
        for (j=0; j<STATE_SIZE_96; j++)
        {
            state[0][j] ^= state[i][j];
            state[i][j]  = 0;
        }
    }
}

void mask_state_128(uint32_t state[MASKING_ORDER][STATE_SIZE_128])
{
    int i,j;

    for (i=1; i<MASKING_ORDER; i++)
    {
        for (j=0; j<STATE_SIZE_128; j++)
        {
            state[i][j] = get_random();
            state[0][j] ^= state[i][j];
        }
    }
}

void unmask_state_128(uint32_t state[MASKING_ORDER][STATE_SIZE_128])
{
    int i,j;

    for (i=1; i<MASKING_ORDER; i++)
    {
        for (j=0; j<STATE_SIZE_128; j++)
        {
            state[0][j] ^= state[i][j];
            state[i][j]  = 0;
        }
    }
}

//==============================================================================
//=== Key schedule
//==============================================================================


void ks_mix_comlumns(const uint32_t *ks_prev, uint32_t *ks_next)
{
    uint32_t tmp;

    tmp = ks_prev[0] ^ ks_prev[1] ^ ks_prev[2] ^ ks_prev[3];

    ks_next[0] = ks_prev[0] ^ tmp;
    ks_next[1] = ks_prev[1] ^ tmp;
    ks_next[2] = ks_prev[2] ^ tmp;
    ks_next[3] = ks_prev[3] ^ tmp;
}

void ks_mix_rotate_rows(uint32_t *ks_state)
{
    ks_state[0] = mat_mult(COL_MK, ks_state[0]);
    left_rotate(ks_state[1],KS_ROT_GAP1)
    left_rotate(ks_state[2],KS_ROT_GAP2)
    left_rotate(ks_state[3],KS_ROT_GAP3)
}

void ks_add_constant(uint32_t *ks_state, const uint32_t ctr)
{
    ks_state[0] ^= KS_CONSTANT_0 ^ ctr;
    ks_state[1] ^= KS_CONSTANT_1;
    ks_state[2] ^= KS_CONSTANT_2;
    ks_state[3] ^= KS_CONSTANT_3;
}

void key_schedule(uint32_t* ks_state, uint8_t mode)
{
    int r;

    for (r=0; r<NB_ROUNDS_KS; r++)
    {
        ks_state += 4;

        ks_mix_comlumns(ks_state-4, ks_state);
        ks_mix_rotate_rows(ks_state);

        if (mode == WITH_CONST_ADD)
        {
            ks_add_constant(ks_state,r);    
        }
    }    
}

//==============================================================================
//=== Pyjamask-96 (encryption)
//==============================================================================

void mix_rows_96(uint32_t *state)
{
    state[0] = mat_mult(COL_M0, state[0]);
    state[1] = mat_mult(COL_M1, state[1]);
    state[2] = mat_mult(COL_M2, state[2]);
}

void add_round_key_96(uint32_t *state, const uint32_t *round_key, int r)
{
    state[0] ^= round_key[4*r+0];
    state[1] ^= round_key[4*r+1];
    state[2] ^= round_key[4*r+2];
}


void masked_sub_bytes_96(uint32_t state[MASKING_ORDER][STATE_SIZE_96])
{
    int i;

    for (i=0; i<MASKING_ORDER; i++)
    {
        state[i][0] ^= state[i][1];
        state[i][1] ^= state[i][2];
    }

    isw_macc_96(state, 2, 0, 1);
    isw_macc_96(state, 0, 1, 2);
    isw_macc_96(state, 1, 0, 2);
    
    for (i=0; i<MASKING_ORDER; i++)
    {
        state[i][2] ^= state[i][0];
        state[i][0] ^= state[i][1];

        // swap state[i][0] <-> state[i][1]
        state[i][0] ^= state[i][1];
        state[i][1] ^= state[i][0];
        state[i][0] ^= state[i][1];
    }

    state[0][2] = ~state[0][2];
}

void masked_pyjamask_96_enc(const uint8_t *plaintext, const uint8_t masked_key[MASKING_ORDER][16], uint8_t *ciphertext)
{
    int i, r;
    
    uint32_t state[MASKING_ORDER][STATE_SIZE_96];
    uint32_t round_keys[MASKING_ORDER][4*(NB_ROUNDS_KS+1)];

    // Load masked key

    for (i=0; i<MASKING_ORDER; i++)
    {
        load_state(masked_key[i], round_keys[i], 4); 
    }

    // Key schedule

    key_schedule(round_keys[0], WITH_CONST_ADD);

    for (i=1; i<MASKING_ORDER; i++)
    {
        key_schedule(round_keys[i], WOUT_CONST_ADD);
    }

    // Load and mask state

    load_state(plaintext, state[0], STATE_SIZE_96);
    mask_state_96(state);

    // Initial AddRoundKey

    for (i=0; i<MASKING_ORDER; i++)
    {
        add_round_key_96(state[i], round_keys[i], 0); 
    }

    // Main loop

    for (r=1; r<=NB_ROUNDS_96; r++)
    {
        masked_sub_bytes_96(state);

        for (i=0; i<MASKING_ORDER; i++)
        {
            mix_rows_96(state[i]);
            add_round_key_96(state[i], round_keys[i], r); 
        }
    }

    // Unmask and unload state 

    unmask_state_96(state);
    unload_state(ciphertext, state[0], STATE_SIZE_96);
}

//==============================================================================
//=== Pyjamask-96 (decryption)
//==============================================================================

void inv_mix_rows_96(uint32_t *state)
{
    state[0] = mat_mult(COL_INV_M0, state[0]);
    state[1] = mat_mult(COL_INV_M1, state[1]);
    state[2] = mat_mult(COL_INV_M2, state[2]);
}


void masked_inv_sub_bytes_96(uint32_t state[MASKING_ORDER][STATE_SIZE_96])
{
    int i;

    state[0][2] = ~state[0][2];

    for (i=0; i<MASKING_ORDER; i++)
    {
        // swap state[i][0] <-> state[i][1]
        state[i][0] ^= state[i][1];
        state[i][1] ^= state[i][0];
        state[i][0] ^= state[i][1];

        state[i][0] ^= state[i][1];
        state[i][2] ^= state[i][0];
    }
    
    isw_macc_96(state, 1, 0, 2);
    isw_macc_96(state, 0, 1, 2);
    isw_macc_96(state, 2, 0, 1);

    for (i=0; i<MASKING_ORDER; i++)
    {
        state[i][1] ^= state[i][2];
        state[i][0] ^= state[i][1];
    }
}

void masked_pyjamask_96_dec(const uint8_t *ciphertext, const uint8_t masked_key[MASKING_ORDER][16], uint8_t *plaintext)
{
    int i, r;

    uint32_t state[MASKING_ORDER][STATE_SIZE_96];
    uint32_t round_keys[MASKING_ORDER][4*(NB_ROUNDS_KS+1)];

    // Load masked key

    for (i=0; i<MASKING_ORDER; i++)
    {
        load_state(masked_key[i], round_keys[i], 4); 
    }

    // Key schedule

    key_schedule(round_keys[0], WITH_CONST_ADD);

    for (i=1; i<MASKING_ORDER; i++)
    {
        key_schedule(round_keys[i], WOUT_CONST_ADD);
    }

    // Load and mask state

    load_state(ciphertext, state[0], STATE_SIZE_96);
    mask_state_96(state);

    // Main loop

    for (r=NB_ROUNDS_96; r>0; r--)
    {
        for (i=0; i<MASKING_ORDER; i++)
        {
            add_round_key_96(state[i], round_keys[i], r); 
            inv_mix_rows_96(state[i]);
        }

        masked_inv_sub_bytes_96(state);
    }

    // Final AddRoundKey

    for (i=0; i<MASKING_ORDER; i++)
    {
        add_round_key_96(state[i], round_keys[i], 0); 
    }

    // Unmask and unload state 

    unmask_state_96(state);
    unload_state(plaintext, state[0], STATE_SIZE_96);
}



//==============================================================================
//=== Pyjamask-128 (encryption)
//==============================================================================

void mix_rows_128(uint32_t *state)
{
    state[0] = mat_mult(COL_M0, state[0]);
    state[1] = mat_mult(COL_M1, state[1]);
    state[2] = mat_mult(COL_M2, state[2]);
    state[3] = mat_mult(COL_M3, state[3]);
}

void add_round_key_128(uint32_t *state, const uint32_t *round_key, int r)
{
    state[0] ^= round_key[4*r+0];
    state[1] ^= round_key[4*r+1];
    state[2] ^= round_key[4*r+2];
    state[3] ^= round_key[4*r+3];
}

void masked_sub_bytes_128(uint32_t state[MASKING_ORDER][STATE_SIZE_128])
{
    int i;

    for (i=0; i<MASKING_ORDER; i++)
    {
        state[i][0] ^= state[i][3];
    }

    isw_macc_128(state, 3, 0, 1);
    isw_macc_128(state, 0, 1, 2);
    isw_macc_128(state, 1, 2, 3);
    isw_macc_128(state, 2, 0, 3);

    for (i=0; i<MASKING_ORDER; i++)
    {
        state[i][2] ^= state[i][1];
        state[i][1] ^= state[i][0];

        // swap state[i][2] <-> state[i][3]
        state[i][2] ^= state[i][3];
        state[i][3] ^= state[i][2];
        state[i][2] ^= state[i][3];
    }

    state[0][2] = ~state[0][2];
}

void masked_pyjamask_128_enc(const uint8_t *plaintext, const uint8_t masked_key[MASKING_ORDER][16], uint8_t *ciphertext)
{
    int i, r;
    
    uint32_t state[MASKING_ORDER][STATE_SIZE_128];
    uint32_t round_keys[MASKING_ORDER][4*(NB_ROUNDS_KS+1)];

    // Load masked key

    for (i=0; i<MASKING_ORDER; i++)
    {
        load_state(masked_key[i], round_keys[i], 4); 
    }

    // Key schedule

    key_schedule(round_keys[0], WITH_CONST_ADD);

    for (i=1; i<MASKING_ORDER; i++)
    {
        key_schedule(round_keys[i], WOUT_CONST_ADD);
    }

    // Load and mask state

    load_state(plaintext, state[0], STATE_SIZE_128);
    mask_state_128(state);

    // Initial AddRoundKey

    for (i=0; i<MASKING_ORDER; i++)
    {
        add_round_key_128(state[i], round_keys[i], 0); 
    }

    // Main loop

    for (r=1; r<=NB_ROUNDS_128; r++)
    {
        masked_sub_bytes_128(state);

        for (i=0; i<MASKING_ORDER; i++)
        {
            mix_rows_128(state[i]);
            add_round_key_128(state[i], round_keys[i], r); 
        }
    }

    // Unmask and unload state 

    unmask_state_128(state);
    unload_state(ciphertext, state[0], STATE_SIZE_128);
}

//==============================================================================
//=== Pyjamask-128 (decryption)
//==============================================================================

void inv_mix_rows_128(uint32_t *state)
{
    state[0] = mat_mult(COL_INV_M0, state[0]);
    state[1] = mat_mult(COL_INV_M1, state[1]);
    state[2] = mat_mult(COL_INV_M2, state[2]);
    state[3] = mat_mult(COL_INV_M3, state[3]);
}

void masked_inv_sub_bytes_128(uint32_t state[MASKING_ORDER][STATE_SIZE_128])
{
    int i;

    state[0][2] = ~state[0][2];

    for (i=0; i<MASKING_ORDER; i++)
    {
        // swap state[i][2] <-> state[i][3]
        state[i][2] ^= state[i][3];
        state[i][3] ^= state[i][2];
        state[i][2] ^= state[i][3];

        state[i][1] ^= state[i][0];
        state[i][2] ^= state[i][1];
    }

    isw_macc_128(state, 2, 3, 0);
    isw_macc_128(state, 1, 2, 3);
    isw_macc_128(state, 0, 1, 2);
    isw_macc_128(state, 3, 0, 1);

    for (i=0; i<MASKING_ORDER; i++)
    {
        state[i][0] ^= state[i][3];
    }
}

void masked_pyjamask_128_dec(const uint8_t *ciphertext, const uint8_t masked_key[MASKING_ORDER][16], uint8_t *plaintext)
{
    int i, r;

    uint32_t state[MASKING_ORDER][STATE_SIZE_128];
    uint32_t round_keys[MASKING_ORDER][4*(NB_ROUNDS_KS+1)];

    // Load masked key

    for (i=0; i<MASKING_ORDER; i++)
    {
        load_state(masked_key[i], round_keys[i], 4); 
    }

    // Key schedule

    key_schedule(round_keys[0], WITH_CONST_ADD);

    for (i=1; i<MASKING_ORDER; i++)
    {
        key_schedule(round_keys[i], WOUT_CONST_ADD);
    }

    // Load and mask state

    load_state(ciphertext, state[0], STATE_SIZE_128);
    mask_state_128(state);

    // Main loop

    for (r=NB_ROUNDS_128; r>0; r--)
    {
        for (i=0; i<MASKING_ORDER; i++)
        {
            add_round_key_128(state[i], round_keys[i], r); 
            inv_mix_rows_128(state[i]);
        }

        masked_inv_sub_bytes_128(state);
    }

    // Final AddRoundKey

    for (i=0; i<MASKING_ORDER; i++)
    {
        add_round_key_128(state[i], round_keys[i], 0); 
    }

    // Unmask and unload state 

    unmask_state_128(state);
    unload_state(plaintext, state[0], STATE_SIZE_128);
}