asconp.h 3.01 KB
Newer Older
Robert Primas committed
1 2 3 4
#ifndef ASCONP_H_
#define ASCONP_H_

#include <inttypes.h>
5
#include "forceinline.h"
Robert Primas committed
6

7 8 9 10 11 12
typedef union
{
    uint64_t x;
    uint32_t w[2];
    uint8_t b[8];
} lane_t;
Robert Primas committed
13

14
typedef union
Robert Primas committed
15
{
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
    lane_t l[5];
    uint64_t x[5];
    uint32_t w[5][2];
    uint8_t b[5][8];
} state_t;

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

#define P_sH P12ROUNDS(s)
#define P_sB P1ROUNDS(s)
#define P_sE P6ROUNDS(s)
#define P_sK P12ROUNDS(s)

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

#define U64TOWORD(x) U64BIG(x)
#define WORDTOU64(x) U64BIG(x)
Robert Primas committed
33

34 35 36 37
/* ---------------------------------------------------------------- */

#define TOBI(x) (x)
#define FROMBI(x) (x)
Robert Primas committed
38 39 40

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

41
forceinline lane_t U64BIG(lane_t x)
Robert Primas committed
42
{
43 44 45 46 47
    x.x = ((((x.x) & 0x00000000000000FFULL) << 56) | (((x.x) & 0x000000000000FF00ULL) << 40) |
           (((x.x) & 0x0000000000FF0000ULL) << 24) | (((x.x) & 0x00000000FF000000ULL) << 8) |
           (((x.x) & 0x000000FF00000000ULL) >> 8) | (((x.x) & 0x0000FF0000000000ULL) >> 24) |
           (((x.x) & 0x00FF000000000000ULL) >> 40) | (((x.x) & 0xFF00000000000000ULL) >> 56));
    return x;
Robert Primas committed
48 49 50 51
}

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

52
forceinline void ROUND(state_t *s, uint8_t C)
Robert Primas committed
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
    uint64_t xtemp;
    /* round constant */
    s->x[2] ^= C;
    /* s-box layer */
    s->x[0] ^= s->x[4];
    s->x[4] ^= s->x[3];
    s->x[2] ^= s->x[1];
    xtemp = s->x[0] & ~s->x[4];
    s->x[0] ^= s->x[2] & ~s->x[1];
    s->x[2] ^= s->x[4] & ~s->x[3];
    s->x[4] ^= s->x[1] & ~s->x[0];
    s->x[1] ^= s->x[3] & ~s->x[2];
    s->x[3] ^= xtemp;
    s->x[1] ^= s->x[0];
    s->x[3] ^= s->x[2];
    s->x[0] ^= s->x[4];
    s->x[2] = ~s->x[2];
    /* linear layer */
    s->x[0] ^=
        (s->x[0] >> 19) ^ (s->x[0] << 45) ^ (s->x[0] >> 28) ^ (s->x[0] << 36);
    s->x[1] ^=
        (s->x[1] >> 61) ^ (s->x[1] << 3) ^ (s->x[1] >> 39) ^ (s->x[1] << 25);
    s->x[2] ^=
        (s->x[2] >> 1) ^ (s->x[2] << 63) ^ (s->x[2] >> 6) ^ (s->x[2] << 58);
    s->x[3] ^=
        (s->x[3] >> 10) ^ (s->x[3] << 54) ^ (s->x[3] >> 17) ^ (s->x[3] << 47);
    s->x[4] ^=
        (s->x[4] >> 7) ^ (s->x[4] << 57) ^ (s->x[4] >> 41) ^ (s->x[4] << 23);
Robert Primas committed
82 83 84 85
}

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

86
forceinline void P12ROUNDS(state_t *s)
Robert Primas committed
87
{
88 89 90 91 92 93 94 95 96 97 98 99
    ROUND(s, 0xf0);
    ROUND(s, 0xe1);
    ROUND(s, 0xd2);
    ROUND(s, 0xc3);
    ROUND(s, 0xb4);
    ROUND(s, 0xa5);
    ROUND(s, 0x96);
    ROUND(s, 0x87);
    ROUND(s, 0x78);
    ROUND(s, 0x69);
    ROUND(s, 0x5a);
    ROUND(s, 0x4b);
Robert Primas committed
100 101 102 103
}

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

104 105 106 107 108 109 110 111 112
forceinline void P6ROUNDS(state_t *s)
{
    ROUND(s, 0x96);
    ROUND(s, 0x87);
    ROUND(s, 0x78);
    ROUND(s, 0x69);
    ROUND(s, 0x5a);
    ROUND(s, 0x4b);
}
Robert Primas committed
113 114 115

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

116 117 118
forceinline void P1ROUNDS(state_t *s)
{
    ROUND(s, 0x4b);
Robert Primas committed
119 120
}

121 122
/* ---------------------------------------------------------------- */

Robert Primas committed
123
#endif // ASCONP_H_