aboutsummaryrefslogtreecommitdiffstats
path: root/libraries/spongycastle/core/src/test/java/org/spongycastle/crypto/test/speedy/ThreefishReferenceEngine.java
blob: 768bb9542e8aaa0b0616079e30c7db4779b980f1 (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
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
package org.spongycastle.crypto.test.speedy;

import org.spongycastle.crypto.BlockCipher;
import org.spongycastle.crypto.CipherParameters;
import org.spongycastle.crypto.DataLengthException;
import org.spongycastle.crypto.params.KeyParameter;
import org.spongycastle.crypto.params.TweakableBlockCipherParameters;

public class ThreefishReferenceEngine
    implements BlockCipher
{

    /**
     * The tweak input is always 128 bits
     */
    private static final int TWEAK_SIZE = 16;

    private static long C_240 = 0x1BD11BDAA9FC1A22L;

    private final int blocksize = 64;
    private final int rounds = 72;
    private final int words = 8;

    private boolean forEncryption;

    private long[] block = new long[words];

    private int[][] rotations = R8;

    /**
     * Rotation constants Rd,j for Nw = 8.
     */
    private static final int[][] R8 = {
        {46, 36, 19, 37},
        {33, 27, 14, 42},
        {17, 49, 36, 39},
        {44, 9, 54, 56},
        {39, 30, 34, 24},
        {13, 50, 10, 17},
        {25, 29, 39, 43},
        {8, 35, 56, 22}};

    private long[] t;

    private long kw[];

    public void init(boolean forEncryption, CipherParameters params)
        throws IllegalArgumentException
    {
        if (params instanceof TweakableBlockCipherParameters)
        {
            init(forEncryption, (TweakableBlockCipherParameters)params);
        }
        else if (params instanceof KeyParameter)
        {
            init(forEncryption, new TweakableBlockCipherParameters((KeyParameter)params, new byte[TWEAK_SIZE]));
        }
        else
        {
            throw new IllegalArgumentException("Invalid parameter passed to Threefish init - "
                + params.getClass().getName());
        }
    }

    public void init(boolean forEncryption, TweakableBlockCipherParameters params)
        throws IllegalArgumentException
    {
        // TODO: Remove some of the NPEs that can be avoided in the Params
        // classes
        if ((params.getKey() == null) || (params.getKey().getKey() == null)
            || (params.getKey().getKey().length != blocksize))
        {
            throw new IllegalArgumentException("Threefish key must be same size as block (%d bytes)" + blocksize);
        }

        if ((params.getTweak() == null) || (params.getTweak().length != TWEAK_SIZE))
        {
            throw new IllegalArgumentException("Threefish tweak must be %d bytes" + TWEAK_SIZE);
        }

        this.forEncryption = forEncryption;

        generateKeySchedule(params.getKey().getKey(), params.getTweak());
    }

    private void generateKeySchedule(byte[] key, byte[] tweak)
    {
        // TODO: This key schedule can/should be generated incrementally/on demand during encrypt/decrypt
        // to reduce memory overhead (currently 1.2MB = (rounds/4+1)=19 * words=8 * 8 bytes/word)

        t = new long[3];
        t[0] = BytesToWord(tweak, 0);
        t[1] = BytesToWord(tweak, 8);
        t[2] = t[0] ^ t[1];

        kw = new long[words + 1];

        long knw = C_240;
        for (int i = 0; i < words; i++)
        {
            kw[i] = BytesToWord(key, i * 8);
            knw = knw ^ kw[i];
        }
        kw[kw.length - 1] = knw;
    }

    private static long BytesToWord(byte[] bytes, int off)
    {
        long word = 0;
        int index = off;

        word = (bytes[index++] & 0xffL);
        word |= (bytes[index++] & 0xffL) << 8;
        word |= (bytes[index++] & 0xffL) << 16;
        word |= (bytes[index++] & 0xffL) << 24;
        word |= (bytes[index++] & 0xffL) << 32;
        word |= (bytes[index++] & 0xffL) << 40;
        word |= (bytes[index++] & 0xffL) << 48;
        word |= (bytes[index++] & 0xffL) << 56;

        return word;
    }

    private static void WordToBytes(long word, byte[] bytes, int off)
    {
        int index = off;

        bytes[index++] = (byte)word;
        bytes[index++] = (byte)(word >> 8);
        bytes[index++] = (byte)(word >> 16);
        bytes[index++] = (byte)(word >> 24);
        bytes[index++] = (byte)(word >> 32);
        bytes[index++] = (byte)(word >> 40);
        bytes[index++] = (byte)(word >> 48);
        bytes[index++] = (byte)(word >> 56);
    }

    public String getAlgorithmName()
    {
        return "Threefish";
    }

    public int getBlockSize()
    {
        return blocksize;
    }

    public int processBlock(byte[] in, int inOff, byte[] out, int outOff)
        throws DataLengthException,
        IllegalStateException
    {
        // TODO: Check init state
        if (kw == null)
        {
            throw new IllegalStateException("Threefish engine not initialised");
        }

        if ((inOff + blocksize) > in.length)
        {
            throw new DataLengthException("Input buffer too short");
        }

        if ((outOff + blocksize) > out.length)
        {
            throw new DataLengthException("Output buffer too short");
        }

        if (forEncryption)
        {
            unpackBlock(in, inOff);
            encryptBlock();
            packBlock(out, outOff);
        }
        else
        {
            unpackBlock(in, inOff);
            decryptBlock();
            packBlock(out, outOff);
        }

        return blocksize;
    }

    private void decryptBlock()
    {
        for (int d = rounds; d > 0; d--)
        {
            // Add subkey every 4 rounds
            if ((d % 4) == 0)
            {
                uninjectSubkey(d / 4);
            }

            // Permute
            unpermute();

            // Mix
            for (int j = 0; j < words / 2; j++)
            {
                unmix(j, d - 1);
            }
        }

        // Remove first subkey
        uninjectSubkey(0);
    }

    private void injectSubkey(int s)
    {
        for (int i = 0; i < (words - 3); i++)
        {
            block[i] += kw[(s + i) % (words + 1)];
        }
        block[words - 3] += kw[(s + words - 3) % (words + 1)] + t[s % 3];
        block[words - 2] += kw[(s + words - 2) % (words + 1)] + t[(s + 1) % 3];
        block[words - 1] += kw[(s + words - 1) % (words + 1)] + s;
    }

    private void uninjectSubkey(int s)
    {
        for (int i = 0; i < (words - 3); i++)
        {
            block[i] -= kw[(s + i) % (words + 1)];
        }
        block[words - 3] -= kw[(s + words - 3) % (words + 1)] + t[s % 3];
        block[words - 2] -= kw[(s + words - 2) % (words + 1)] + t[(s + 1) % 3];
        block[words - 1] -= kw[(s + words - 1) % (words + 1)] + s;
    }

    private void encryptBlock()
    {
        for (int d = 0; d < rounds; d++)
        {
            // Add subkey every 4 rounds
            if ((d % 4) == 0)
            {
                injectSubkey(d / 4);
            }

            // Mix
            for (int j = 0; j < words / 2; j++)
            {
                mix(j, d);
            }

            // Permute
            permute();
        }

        // Final key addition
        injectSubkey(rounds / 4);
    }

    private void permute()
    {
        // Permute in place for Nw = 8
        long f0 = block[0];
        long f3 = block[3];

        block[0] = block[2];
        block[1] = block[1];
        block[2] = block[4];
        block[3] = block[7];
        block[4] = block[6];
        block[5] = block[5];
        block[6] = f0;
        block[7] = f3;
    }

    private void unpermute()
    {
        // TODO: Change these to tables
        // Permute in place for Nw = 8
        long f6 = block[6];
        long f7 = block[7];

        block[7] = block[3];
        block[6] = block[4];
        block[5] = block[5];
        block[4] = block[2];
        block[3] = f7;
        block[2] = block[0];
        block[1] = block[1];
        block[0] = f6;
    }

    private void mix(int j, int d)
    {
        // ed,2j and ed,2j+1
        int b0 = 2 * j;
        int b1 = b0 + 1;

        // y0 = x0 + x1
        block[b0] = block[b0] + block[b1];

        // y1 = (x1 <<< R(d mod 8,j)) xor y0
        block[b1] = Long.rotateLeft(block[b1], rotations[d % 8][j]) ^ block[b0];
    }

    private void unmix(int j, int d)
    {
        // ed,2j and ed,2j+1
        int b0 = 2 * j;
        int b1 = b0 + 1;

        // x1 = (y1 ^ y0) >>> R(d mod 8, j))
        block[b1] = Long.rotateRight(block[b1] ^ block[b0], rotations[d % 8][j]);

        // x0 = y0 - x1
        block[b0] = block[b0] - block[b1];

    }

    public static void main(String[] args)
    {
        ThreefishReferenceEngine engine = new ThreefishReferenceEngine();
        engine.fu();
    }

    private void fu()
    {
        block[0] = 0x12;
        block[1] = 0x34;
        block[2] = 0x56;
        block[3] = 0x78;
        block[4] = 0x90;
        block[5] = 0xAB;
        block[6] = 0xCD;
        block[7] = 0xEF;

        for (int i = 0; i < block.length; i++)
        {
            System.err.println(i + " : " + Long.toHexString(block[i]));
        }
        mix(0, 4);
        System.err.println("=========");
        for (int i = 0; i < block.length; i++)
        {
            System.err.println(i + " : " + Long.toHexString(block[i]));
        }
        unmix(0, 4);
        System.err.println("=========");
        for (int i = 0; i < block.length; i++)
        {
            System.err.println(i + " : " + Long.toHexString(block[i]));
        }
        permute();
        System.err.println("=========");
        for (int i = 0; i < block.length; i++)
        {
            System.err.println(i + " : " + Long.toHexString(block[i]));
        }
        unpermute();
        System.err.println("=========");
        for (int i = 0; i < block.length; i++)
        {
            System.err.println(i + " : " + Long.toHexString(block[i]));
        }
        generateKeySchedule(new byte[blocksize], new byte[TWEAK_SIZE]);
        injectSubkey(5);
        System.err.println("=========");
        for (int i = 0; i < block.length; i++)
        {
            System.err.println(i + " : " + Long.toHexString(block[i]));
        }
        uninjectSubkey(5);
        System.err.println("=========");
        for (int i = 0; i < block.length; i++)
        {
            System.err.println(i + " : " + Long.toHexString(block[i]));
        }
    }

    private void packBlock(byte[] out, int outOff)
    {
        for (int i = 0; i < block.length; i++)
        {
            WordToBytes(block[i], out, outOff + (i * 8));
        }
    }

    private long[] unpackBlock(byte[] bytes, int index)
    {
        for (int i = 0; i < block.length; i++)
        {
            block[i] = BytesToWord(bytes, index + (i * 8));
        }
        return block;
    }

    public void reset()
    {
    }

}