Xoodoo-optimized.c 12.1 KB
Newer Older
Gilles Van Assche 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
/*
The eXtended Keccak Code Package (XKCP)
https://github.com/XKCP/XKCP

The Xoodoo permutation, designed by Joan Daemen, Seth Hoffert, Gilles Van Assche and Ronny Van Keer.

Implementation by Ronny Van Keer, hereby denoted as "the implementer".

For more information, feedback or questions, please refer to the Keccak Team website:
https://keccak.team/

To the extent possible under law, the implementer has waived all copyright
and related or neighboring rights to the source code in this file.
http://creativecommons.org/publicdomain/zero/1.0/
*/

#include <stdio.h>
#include <string.h>
#include "Xoodoo.h"

#define VERBOSE         0

#if (VERBOSE > 0)
    #define    Dump(__t)    printf(__t "\n"); \
                            printf("a00 %08x, a01 %08x, a02 %08x, a03 %08x\n", a00, a01, a02, a03 ); \
                            printf("a10 %08x, a11 %08x, a12 %08x, a13 %08x\n", a10, a11, a12, a13 ); \
                            printf("a20 %08x, a21 %08x, a22 %08x, a23 %08x\n\n", a20, a21, a22, a23 );
#else
    #define    Dump(__t)
#endif

#if (VERBOSE >= 1)
    #define    Dump1(__t)    Dump(__t)
#else
    #define    Dump1(__t)
#endif

#if (VERBOSE >= 2)
    #define    Dump2(__t)    Dump(__t)
#else
    #define    Dump2(__t)
#endif

#if (VERBOSE >= 3)
    #define    Dump3(__t)    Dump(__t)
#else
    #define    Dump3(__t)
#endif

/* ---------------------------------------------------------------- */

void Xoodoo_Initialize(void *state)
{
    memset(state, 0, NLANES*sizeof(tXoodooLane));
}

/* ---------------------------------------------------------------- */

void Xoodoo_AddBytes(void *argState, const unsigned char *argdata, unsigned int offset, unsigned int length)
{
#if (PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN)
    if (length == (3*4*4)) {
        uint32_t *state = (uint32_t *)argState;
        uint32_t *data = (uint32_t *)argdata;
        state[0] ^= data[0];
        state[1] ^= data[1];
        state[2] ^= data[2];
        state[3] ^= data[3];
        state[4] ^= data[4];
        state[5] ^= data[5];
        state[6] ^= data[6];
        state[7] ^= data[7];
        state[8] ^= data[8];
        state[9] ^= data[9];
        state[10] ^= data[10];
        state[11] ^= data[11];
    }
    else {
        unsigned int sizeLeft = length;
        unsigned int lanePosition = offset/4;
        unsigned int offsetInLane = offset%4;
        const unsigned char *curData = argdata;
        uint32_t *state = (uint32_t*)argState;

        state += lanePosition;
        if ((sizeLeft > 0) && (offsetInLane != 0)) {
            unsigned int bytesInLane = 4 - offsetInLane;
            uint32_t lane = 0;
            if (bytesInLane > sizeLeft)
                bytesInLane = sizeLeft;
            memcpy((unsigned char*)&lane + offsetInLane, curData, bytesInLane);
            *state++ ^= lane;
            sizeLeft -= bytesInLane;
            curData += bytesInLane;
        }

        while(sizeLeft >= 4) {
            *state++ ^= READ32_UNALIGNED( curData );
            sizeLeft -= 4;
            curData += 4;
        }

        if (sizeLeft > 0) {
            uint32_t lane = 0;
            memcpy(&lane, curData, sizeLeft);
            *state ^= lane;
        }
    }
#else
    #error "Not yet implemented"
#endif
}

/* ---------------------------------------------------------------- */

void Xoodoo_OverwriteBytes(void *argstate, const unsigned char *argdata, unsigned int offset, unsigned int length)
{
#if (PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN)
    if (length == (3*4*4)) {
        uint32_t *state = (uint32_t *)argstate;
        uint32_t *data = (uint32_t *)argdata;
        state[0] = data[0];
        state[1] = data[1];
        state[2] = data[2];
        state[3] = data[3];
        state[4] = data[4];
        state[5] = data[5];
        state[6] = data[6];
        state[7] = data[7];
        state[8] = data[8];
        state[9] = data[9];
        state[10] = data[10];
        state[11] = data[11];
    }
    else
        memcpy((unsigned char*)argstate+offset, argdata, length);
#else
    #error "Not yet implemented"
#endif
}

/* ---------------------------------------------------------------- */

void Xoodoo_OverwriteWithZeroes(void *argstate, unsigned int byteCount)
{
#if (PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN)
    memset(argstate, 0, byteCount);
#else
    #error "Not yet implemented"
#endif
}

/* ---------------------------------------------------------------- */

void Xoodoo_ExtractBytes(const void *state, unsigned char *data, unsigned int offset, unsigned int length)
{
#if (PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN)
    memcpy(data, (unsigned char*)state+offset, length);
#else
    #error "Not yet implemented"
#endif
}

/* ---------------------------------------------------------------- */

void Xoodoo_ExtractAndAddBytes(const void *argState, const unsigned char *input, unsigned char *output, unsigned int offset, unsigned int length)
{
#if (PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN)
    if (length == (3*4*4)) {
        uint32_t *state = (uint32_t *)argState;
        const uint32_t *ii = (const uint32_t *)input;
        uint32_t *oo = (uint32_t *)output;

        oo[0] = state[0] ^ ii[0];
        oo[1] = state[1] ^ ii[1];
        oo[2] = state[2] ^ ii[2];
        oo[3] = state[3] ^ ii[3];
        oo[4] = state[4] ^ ii[4];
        oo[5] = state[5] ^ ii[5];
        oo[6] = state[6] ^ ii[6];
        oo[7] = state[7] ^ ii[7];
        oo[8] = state[8] ^ ii[8];
        oo[9] = state[9] ^ ii[9];
        oo[10] = state[10] ^ ii[10];
        oo[11] = state[11] ^ ii[11];
    }
    else {
        unsigned int sizeLeft = length;
        unsigned int lanePosition = offset/4;
        unsigned int offsetInLane = offset%4;
        const unsigned char *curInput = input;
        unsigned char *curOutput = output;
        const uint32_t *state = (const uint32_t*)argState;

        state += lanePosition;
        if ((sizeLeft > 0) && (offsetInLane != 0)) {
            unsigned int bytesInLane = 4 - offsetInLane;
            uint32_t  lane = *state++ >> (offsetInLane * 8);
            if (bytesInLane > sizeLeft)
                bytesInLane = sizeLeft;
            sizeLeft -= bytesInLane;
            do {
                *curOutput++ = (*curInput++) ^ (unsigned char)lane;
                lane >>= 8;
            }
            while ( --bytesInLane != 0);
        }

        while(sizeLeft >= 4) {
            WRITE32_UNALIGNED( curOutput, READ32_UNALIGNED( curInput ) ^ *state++ );
            sizeLeft -= 4;
            curInput += 4;
            curOutput += 4;
        }

        if (sizeLeft > 0) {
            uint32_t  lane = *state;
            do {
                *curOutput++ = (*curInput++) ^ (unsigned char)lane;
                lane >>= 8;
            }
            while ( --sizeLeft != 0 );
        }
    }
#else
    #error "Not yet implemented"
#endif
}

/* ---------------------------------------------------------------- */

#define    DeclareVars  uint32_t    a00, a01, a02, a03; \
                        uint32_t    a10, a11, a12, a13; \
                        uint32_t    a20, a21, a22, a23; \
                        uint32_t    v1, v2

#define    State2Vars   a00 = state[0+0], a01 = state[0+1], a02 = state[0+2], a03 = state[0+3]; \
                        a10 = state[4+0], a11 = state[4+1], a12 = state[4+2], a13 = state[4+3]; \
                        a20 = state[8+0], a21 = state[8+1], a22 = state[8+2], a23 = state[8+3]

#define    Vars2State   state[0+0] = a00, state[0+1] = a01, state[0+2] = a02, state[0+3] = a03; \
                        state[4+0] = a10, state[4+1] = a11, state[4+2] = a12, state[4+3] = a13; \
                        state[8+0] = a20, state[8+1] = a21, state[8+2] = a22, state[8+3] = a23

/*
** Theta: Column Parity Mixer
*/
#define Theta()                                             \
                    v1 = a03 ^ a13 ^ a23;                   \
                    v2 = a00 ^ a10 ^ a20;                   \
                    v1 = ROTL32(v1, 5) ^ ROTL32(v1, 14);    \
                    a00 ^= v1;                              \
                    a10 ^= v1;                              \
                    a20 ^= v1;                              \
                    v1 = a01 ^ a11 ^ a21;                   \
                    v2 = ROTL32(v2, 5) ^ ROTL32(v2, 14);    \
                    a01 ^= v2;                              \
                    a11 ^= v2;                              \
                    a21 ^= v2;                              \
                    v2 = a02 ^ a12 ^ a22;                   \
                    v1 = ROTL32(v1, 5) ^ ROTL32(v1, 14);    \
                    a02 ^= v1;                              \
                    a12 ^= v1;                              \
                    a22 ^= v1;                              \
                    v2 = ROTL32(v2, 5) ^ ROTL32(v2, 14);    \
                    a03 ^= v2;                              \
                    a13 ^= v2;                              \
                    a23 ^= v2

/*
** Rho-west: Plane shift
*/
#define Rho_west()                          \
                    a20 = ROTL32(a20, 11);  \
                    a21 = ROTL32(a21, 11);  \
                    a22 = ROTL32(a22, 11);  \
                    a23 = ROTL32(a23, 11);  \
                    v1 = a13;               \
                    a13 = a12;              \
                    a12 = a11;              \
                    a11 = a10;              \
                    a10 = v1

/*
** Iota: Round constants
*/
#define Iota(__rc)  a00 ^= __rc

/*
** Chi: Non linear step, on colums
*/
#define Chi()                               \
                    a00 ^= ~a10 & a20;      \
                    a10 ^= ~a20 & a00;      \
                    a20 ^= ~a00 & a10;      \
                                            \
                    a01 ^= ~a11 & a21;      \
                    a11 ^= ~a21 & a01;      \
                    a21 ^= ~a01 & a11;      \
                                            \
                    a02 ^= ~a12 & a22;      \
                    a12 ^= ~a22 & a02;      \
                    a22 ^= ~a02 & a12;      \
                                            \
                    a03 ^= ~a13 & a23;      \
                    a13 ^= ~a23 & a03;      \
                    a23 ^= ~a03 & a13

/*
** Rho-east: Plane shift
*/
#define Rho_east()                          \
                    a10 = ROTL32(a10, 1);   \
                    a11 = ROTL32(a11, 1);   \
                    a12 = ROTL32(a12, 1);   \
                    a13 = ROTL32(a13, 1);   \
                    v1  = ROTL32(a23, 8);   \
                    a23 = ROTL32(a21, 8);   \
                    a21 = v1;               \
                    v1  = ROTL32(a22, 8);   \
                    a22 = ROTL32(a20, 8);   \
                    a20 = v1

#define Round(__rc)                         \
                    Theta();                \
                    Dump3("Theta");         \
                    Rho_west();             \
                    Dump3("Rho-west");      \
                    Iota(__rc);             \
                    Dump3("Iota");          \
                    Chi();                  \
                    Dump3("Chi");           \
                    Rho_east();             \
                    Dump3("Rho-east")

static const uint32_t    RC[MAXROUNDS] = {
    _rc12,
    _rc11,
    _rc10,
    _rc9,
    _rc8,
    _rc7,
    _rc6,
    _rc5,
    _rc4,
    _rc3,
    _rc2,
    _rc1
};

void Xoodoo_Permute_Nrounds( uint32_t * state, uint32_t nr )
{
    DeclareVars;
    uint32_t    i;

    State2Vars;
    for (i = MAXROUNDS - nr; i < MAXROUNDS; ++i ) {
        Round(RC[i]);
        Dump2("Round");
    }
    Dump1("Permutation");
    Vars2State;
}

void Xoodoo_Permute_6rounds( uint32_t * state)
{
    DeclareVars;

    State2Vars;
    Round(_rc6);
    Round(_rc5);
    Round(_rc4);
    Round(_rc3);
    Round(_rc2);
    Round(_rc1);
    Dump1("Permutation");
    Vars2State;
}

void Xoodoo_Permute_12rounds( uint32_t * state)
{
    DeclareVars;

    State2Vars;
    Round(_rc12);
    Round(_rc11);
    Round(_rc10);
    Round(_rc9);
    Round(_rc8);
    Round(_rc7);
    Round(_rc6);
    Round(_rc5);
    Round(_rc4);
    Round(_rc3);
    Round(_rc2);
    Round(_rc1);
    Dump1("Permutation");
    Vars2State;
}