forkskinny.c 7.69 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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

#include "forkskinny.h"
#include "extra_api.h"
#include "skinny_round.h"
#include "helpers.h"

//#define DEBUG_FORK


/* === Print intermediate results for debugging purposes === */
void print_fork(unsigned char s[4][4]){
    #ifdef DEBUG_FORK
    int j,k;
    printf("\n === At the fork: ");
    for (j = 0; j < 4; j++)
        for (k = 0; k < 4; k++)
            printf("%02x ", s[j][k]);
    printf(" ===");
    #endif
}

void AddBranchConstant(unsigned char state[4][4]){
	int i, j;
    #ifdef CRYPTO_BLOCKSIZE_8
    const unsigned char BC[16] = {0x01,0x02,0x04,0x09,0x03,0x06,0x0d,0x0a,0x05,0x0b,0x07,0x0f,0x0e,0x0c,0x08,0x01};
    #else
    const unsigned char BC[16] = {0x01,0x02,0x04,0x08,0x10,0x20,0x41,0x82,0x05,0x0a,0x14,0x28,0x51,0xa2,0x44,0x88};
    #endif

    for(i = 0; i < 4; i++)
        for(j = 0; j < 4; j++){
            state[i][j] ^= BC[4*i+j];
        }
}

void loadStateAndKey(unsigned char state[4][4], unsigned char keyCells[TWEAKEY_BLOCKSIZE_RATIO][4][4], unsigned char* input, const unsigned char* userkey){

    int i;

	for(i = 0; i < 16; i++) {
        #ifdef CRYPTO_BLOCKSIZE_8
            // For BS = 64, cells are only half-bytes so every input byte needs to be spread over two cells
            if(i&1){
                state[i>>2][i&0x3] = input[i>>1]&0xF;
                keyCells[0][i>>2][i&0x3] = userkey[i>>1]&0xF;
                if (TWEAKEY_BLOCKSIZE_RATIO >= 2)
                    keyCells[1][i>>2][i&0x3] = userkey[(i+16)>>1]&0xF;
                if (TWEAKEY_BLOCKSIZE_RATIO >= 3)
                    keyCells[2][i>>2][i&0x3] = userkey[(i+32)>>1]&0xF;
            }
            else {
                state[i>>2][i&0x3] = (input[i>>1]>>4)&0xF;
                keyCells[0][i>>2][i&0x3] = (userkey[i>>1]>>4)&0xF;
                if (TWEAKEY_BLOCKSIZE_RATIO >= 2)
                    keyCells[1][i>>2][i&0x3] = (userkey[(i+16)>>1]>>4)&0xF;
                if (TWEAKEY_BLOCKSIZE_RATIO >= 3)
                    keyCells[2][i>>2][i&0x3] = (userkey[(i+32)>>1]>>4)&0xF;
            }
        #else
            state[i>>2][i&0x3] = input[i]&0xFF;
            keyCells[0][i>>2][i&0x3] = userkey[i]&0xFF;
            if (TWEAKEY_BLOCKSIZE_RATIO >= 2)
                keyCells[1][i>>2][i&0x3] = userkey[i+16]&0xFF;
            if (TWEAKEY_BLOCKSIZE_RATIO >= 3)
                keyCells[2][i>>2][i&0x3] = userkey[i+32]&0xFF;
        #endif
    }
}

void forkEncrypt(unsigned char* C0, unsigned char* C1, unsigned char* input, const unsigned char* userkey, const enum encrypt_selector s){

	unsigned char state[4][4], L[4][4], keyCells[TWEAKEY_BLOCKSIZE_RATIO][4][4]; 
    int i;

    loadStateAndKey(state, keyCells, input, userkey);

    /* Before fork */
	for(i = 0; i < CRYPTO_NBROUNDS_BEFORE; i++)
        skinny_round(state, keyCells, i);

    /* Save fork if both output blocks are needed */
    if (s == ENC_BOTH)
        stateCopy(L, state);

    print_fork(state);

    /* Right branch (C1) */
    if ((s == ENC_C1) | (s == ENC_BOTH)){
        for(i = CRYPTO_NBROUNDS_BEFORE; i < CRYPTO_NBROUNDS_BEFORE+CRYPTO_NBROUNDS_AFTER; i++) 
            skinny_round(state, keyCells, i);

        /* Move result to output buffer*/
        stateToCharArray(C1, state);
    }

    /* Reinstall L as state if necessary */
    if (s == ENC_BOTH)
        stateCopy(state, L);

    /* Left branch (C0) */
    if ((s == ENC_C0) | (s == ENC_BOTH)){

        /* Add branch constant */
        AddBranchConstant(state);

        for(i = CRYPTO_NBROUNDS_BEFORE+CRYPTO_NBROUNDS_AFTER; i < CRYPTO_NBROUNDS_BEFORE+2*CRYPTO_NBROUNDS_AFTER; i++) 
            skinny_round(state, keyCells, i);

        /* Move result to output buffer */
        stateToCharArray(C0, state);
    }

    /* Null pointer for invalid outputs */
    if (s == ENC_C0) 
        C1 = NULL;
    else if (s == ENC_C1) 
        C0 = NULL;

}


void forkInvert(unsigned char* inverse, unsigned char* C_other, unsigned char* input, const unsigned char* userkey, uint8_t b, const enum inversion_selector s){

	unsigned char state[4][4], L[4][4], keyCells[TWEAKEY_BLOCKSIZE_RATIO][4][4];
	int i;

    loadStateAndKey(state, keyCells, input, userkey);

    if (b == 1){

        /* Advance the key schedule in order to decrypt */
        for(i = 0; i < CRYPTO_NBROUNDS_BEFORE+CRYPTO_NBROUNDS_AFTER; i++)
            advanceKeySchedule(keyCells);

        /* From C1 to fork*/
        for(i = CRYPTO_NBROUNDS_BEFORE+CRYPTO_NBROUNDS_AFTER-1; i >= CRYPTO_NBROUNDS_BEFORE; i--)
            skinny_round_inv(state, keyCells, i);

        /* Save fork if both blocks are needed */
        if (s == INV_BOTH) 
            stateCopy(L, state);

        print_fork(state);

        if ((s == INV_INVERSE) | (s == INV_BOTH)) {
            /* From fork to M */
            for(i = CRYPTO_NBROUNDS_BEFORE-1; i >= 0; i--)
                skinny_round_inv(state, keyCells, i);

            /* Move result to output buffer */
            stateToCharArray(inverse, state);
        }

        /* Reinstall fork if necessary */
        if (s == INV_BOTH) {
            stateCopy(state, L);

            for (i=0; i<CRYPTO_NBROUNDS_BEFORE; i++)
                advanceKeySchedule(keyCells);
        }

        if ((s == INV_OTHER) | (s == INV_BOTH)) {
            /* Set correct keyschedule */
            for (i=0; i<CRYPTO_NBROUNDS_AFTER; i++)
                advanceKeySchedule(keyCells);

            /* Add branch constant */
            AddBranchConstant(state);

            /* From fork to C0 */
            for(i = CRYPTO_NBROUNDS_BEFORE+CRYPTO_NBROUNDS_AFTER; i < CRYPTO_NBROUNDS_BEFORE+2*CRYPTO_NBROUNDS_AFTER; i++) 
                skinny_round(state, keyCells, i);

            /* Move result to output buffer */
            stateToCharArray(C_other, state);
        }
    }
    else {
        /* Advance the key schedule in order to decrypt */
        for(i = 0; i < CRYPTO_NBROUNDS_BEFORE+2*CRYPTO_NBROUNDS_AFTER; i++)
            advanceKeySchedule(keyCells);

        /* From C0 to fork */
        for(i = CRYPTO_NBROUNDS_BEFORE+2*CRYPTO_NBROUNDS_AFTER-1; i >= CRYPTO_NBROUNDS_BEFORE+CRYPTO_NBROUNDS_AFTER; i--)
            skinny_round_inv(state, keyCells, i);

        /* Add branch constant */
        AddBranchConstant(state);

        /* Save fork if both blocks are needed */
        if (s == INV_BOTH) 
            stateCopy(L, state);

        print_fork(state);

        /* Set correct keyschedule */
        for(i = 0; i < CRYPTO_NBROUNDS_AFTER; i++)
            reverseKeySchedule(keyCells);

        if ((s == INV_BOTH) | (s == INV_INVERSE)) {
            /* From fork to M */
            for(i = CRYPTO_NBROUNDS_BEFORE-1; i >= 0; i--)
                skinny_round_inv(state, keyCells, i);

            /* Move result into output buffer */
            stateToCharArray(inverse, state);

        }

        /* Reinstall fork and correct key schedule if necessary */
        if (s == INV_BOTH) {
            stateCopy(state, L);

            for (i=0; i<CRYPTO_NBROUNDS_BEFORE; i++)
                advanceKeySchedule(keyCells);
        }

        if ((s == INV_BOTH) | (s == INV_OTHER)) {
            /* From fork to C1 */
            for(i = CRYPTO_NBROUNDS_BEFORE; i < CRYPTO_NBROUNDS_BEFORE+CRYPTO_NBROUNDS_AFTER; i++) // for i in range(nbRounds)
                skinny_round(state, keyCells, i);

            /* Move result to output buffer */
            stateToCharArray(C_other, state);
        }
    }
    
    /* Null pointer for invalid outputs */
    if (s == INV_INVERSE) 
        C_other = NULL;
    else if (s == INV_OTHER) 
        inverse = NULL;
}