summaryrefslogtreecommitdiff
path: root/src/cipher.c
blob: bb2d46a76ae37113598d4541b6206590623648f0 (plain)
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
#include <stdint.h>
#include <string.h>

#include "cipher.h"
#include "parameters.h"
#include "tweakey.h"


enum permutation
{
    PERMUTATION_ENCRYPTION = 0,
    PERMUTATION_DECRYPTION = 1,
    PERMUTATION_NONE
};

typedef enum permutation permutation;

static const uint8_t PERMUTATIONS[2][BLOCK_BYTES] = {
    /* PI(i) */
    [0] = { 13, 9, 14, 8, 10, 11, 12, 15,
            4,  5, 3,  1, 2,  6,  0,  7 },
    /* PI^-1(i) */
    [1] = { 14, 11, 12, 10, 8, 9, 13, 15,
            3,  1,  4,  5,  6, 0, 2,  7 }
};

static const uint8_t S[256] = {
    32,  0,   178, 133, 59,  53,  166, 164,
    48,  228, 106, 44,  255, 89,  226, 14,
    248, 30,  122, 128, 21,  189, 62,  177,
    232, 243, 162, 194, 218, 81,  42,  16,
    33,  1,   35,  120, 92,  36,  39,  181,
    55,  199, 43,  31,  174, 10,  119, 95,
    111, 9,   157, 129, 4,   90,  41,  220,
    57,  156, 5,   87,  151, 116, 121, 23,
    68,  198, 230, 233, 221, 65,  242, 138,
    84,  202, 110, 74,  225, 173, 182, 136,
    28,  152, 126, 206, 99,  73,  58,  93,
    12,  239, 246, 52,  86,  37,  46,  214,
    103, 117, 85,  118, 184, 210, 97,  217,
    113, 139, 205, 11,  114, 108, 49,  75,
    105, 253, 123, 109, 96,  60,  47,  98,
    63,  34,  115, 19,  201, 130, 127, 83,
    50,  18,  160, 124, 2,   135, 132, 134,
    147, 78,  104, 70,  141, 195, 219, 236,
    155, 183, 137, 146, 167, 190, 61,  216,
    234, 80,  145, 241, 51,  56,  224, 169,
    163, 131, 161, 27,  207, 6,   149, 7,
    158, 237, 185, 245, 76,  192, 244, 45,
    22,  250, 180, 3,   38,  179, 144, 79,
    171, 101, 252, 254, 20,  247, 227, 148,
    238, 172, 140, 26,  222, 203, 40,  64,
    125, 200, 196, 72,  107, 223, 165, 82,
    229, 251, 215, 100, 249, 240, 211, 94,
    102, 150, 143, 29,  69,  54,  204, 197,
    77,  159, 191, 15,  209, 8,   235, 67,
    66,  25,  231, 153, 168, 142, 88,  193,
    154, 212, 24,  71,  170, 175, 188, 91,
    213, 17,  208, 176, 112, 187, 13,  186
};


static void _state_init(uint8_t X[BLOCK_BYTES], const uint8_t message[BLOCK_BYTES])
{
    memcpy(X, message, BLOCK_BYTES);
}


static void _compute_round_tweakeys(
    const uint8_t key[KEY_BYTES],
    const uint8_t tweak[TWEAK_BYTES],
    uint8_t RTK[ROUNDS][ROUND_TWEAKEY_BYTES]
)
{
    uint8_t TK[TWEAKEY_BYTES];
    tweakey_state_init(TK, key, tweak);
    tweakey_state_extract(TK, 0, RTK[0]);

    for (uint8_t i=1; i<ROUNDS; i++)
    {
        tweakey_state_update(TK);
        tweakey_state_extract(TK, i, RTK[i]);
    }
}


static void _nonlinear_layer(uint8_t X[BLOCK_BYTES], const uint8_t RTK[ROUND_TWEAKEY_BYTES])
{
    uint8_t F[ROUND_TWEAKEY_BYTES];
    for (size_t j=0; j<ROUND_TWEAKEY_BYTES; j++)
    {
        F[j] = X[j] ^ RTK[j];
    }

    for (size_t j=0; j<ROUND_TWEAKEY_BYTES; j++)
    {
        F[j] = S[F[j]];
    }

    for (size_t j=0; j<8; j++)
    {
        size_t dest_j = 15-j;
        X[dest_j] ^= F[j];
    }
}

static void _linear_layer(uint8_t X[BLOCK_BYTES])
{
    X[15] ^= X[1];
    X[15] ^= X[2];
    X[15] ^= X[3];
    X[15] ^= X[4];
    X[15] ^= X[5];
    X[15] ^= X[6];
    X[15] ^= X[7];

    X[14] ^= X[7];
    X[13] ^= X[7];
    X[12] ^= X[7];
    X[11] ^= X[7];
    X[10] ^= X[7];
    X[9]  ^= X[7];
}

static void _permutation_layer(uint8_t X[BLOCK_BYTES], permutation p)
{
    if (p == PERMUTATION_NONE)
    {
        return;
    }

    uint8_t X_old[BLOCK_BYTES];
    memcpy(X_old, X, BLOCK_BYTES);

    const uint8_t *pi = PERMUTATIONS[p];

    for (size_t j=0; j<BLOCK_BYTES; j++)
    {
        X[pi[j]] = X_old[j];
    }
}

static void _one_round_egfn(uint8_t X[BLOCK_BYTES], const uint8_t RTK[ROUND_TWEAKEY_BYTES], permutation p)
{
    _nonlinear_layer(X, RTK);
    _linear_layer(X);
    _permutation_layer(X, p);
}


void lilliput_tbc_encrypt(
    const uint8_t key[KEY_BYTES],
    const uint8_t tweak[TWEAK_BYTES],
    const uint8_t message[BLOCK_BYTES],
    uint8_t ciphertext[BLOCK_BYTES]
)
{
    uint8_t X[BLOCK_BYTES];
    _state_init(X, message);

    uint8_t RTK[ROUNDS][ROUND_TWEAKEY_BYTES];
    _compute_round_tweakeys(key, tweak, RTK);

    for (uint8_t i=0; i<ROUNDS-1; i++)
    {
        _one_round_egfn(X, RTK[i], PERMUTATION_ENCRYPTION);
    }

    _one_round_egfn(X, RTK[ROUNDS-1], PERMUTATION_NONE);

    memcpy(ciphertext, X, BLOCK_BYTES);
}

void lilliput_tbc_decrypt(
    const uint8_t key[KEY_BYTES],
    const uint8_t tweak[TWEAK_BYTES],
    const uint8_t ciphertext[BLOCK_BYTES],
    uint8_t message[BLOCK_BYTES]
)
{
    uint8_t X[BLOCK_BYTES];
    _state_init(X, ciphertext);

    uint8_t RTK[ROUNDS][ROUND_TWEAKEY_BYTES];
    _compute_round_tweakeys(key, tweak, RTK);

    for (uint8_t i=0; i<ROUNDS-1; i++)
    {
        _one_round_egfn(X, RTK[ROUNDS-1-i], PERMUTATION_DECRYPTION);
    }

    _one_round_egfn(X, RTK[0], PERMUTATION_NONE);

    memcpy(message, X, BLOCK_BYTES);
}