summary refs log tree commit diff
path: root/crypto/test/src/test/SP80038GTest.cs
blob: 0d2426c609c00d2830fc83f50960115dab2fef9d (plain) (blame)
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
using NUnit.Framework;

using System;

using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Digests;
using Org.BouncyCastle.Crypto.Utilities;
using Org.BouncyCastle.Crypto.Fpe;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Utilities;
using Org.BouncyCastle.Utilities.Encoders;
using Org.BouncyCastle.Utilities.Test;

namespace Org.BouncyCastle.Crypto.Tests
{
    [TestFixture]
	public class SP80038GTest
	    : SimpleTest
{
    private class FFSample
    {
        private readonly int radix;
        private readonly byte[] key;
        private readonly byte[] plaintext;
        private readonly byte[] ciphertext;
        private readonly byte[] tweak;

        public static FFSample from(int radix, String hexKey, String asciiPT, String asciiCT, String hexTweak)
        {
            return new FFSample(radix, fromHex(hexKey), fromAscii(radix, asciiPT), fromAscii(radix, asciiCT), fromHex(hexTweak));
        }

        private static byte fromAlphaNumeric(char c)
        {
            if (c >= '0' && c <= '9')
            {
                return (byte)(c - '0');
            }
            else if (c >= 'a' && c <= 'z')
            {
                return (byte)(10 + (c - 'a'));
            }
            else if (c >= 'A' && c <= 'Z')
            {
                return (byte)(36 + (c - 'A'));
            }
            else
            {
                throw new ArgumentException();
            }
        }

        private static byte[] fromAscii(int radix, string ascii)
        {
            byte[] result = new byte[ascii.Length];
            for (int i = 0; i < result.Length; ++i)
            {
                result[i] = fromAlphaNumeric(ascii[i]);
                if (result[i] < 0 || result[i] >= radix)
                {
                    throw new ArgumentException();
                }
            }
            return result;
        }

        private static byte[] fromHex(string hex)
        {
            return Hex.Decode(hex);
        }

        private FFSample(int radix, byte[] key, byte[] plaintext, byte[] ciphertext, byte[] tweak)
        {
            this.radix = radix;
            this.key = key;
            this.plaintext = plaintext;
            this.ciphertext = ciphertext;
            this.tweak = tweak;
        }

        public byte[] getCiphertext()
        {
            return ciphertext;
        }

        public byte[] getKey()
        {
            return key;
        }

        public byte[] getPlaintext()
        {
            return plaintext;
        }

        public int getRadix()
        {
            return radix;
        }

        public byte[] getTweak()
        {
            return tweak;
        }
    }

    private static FFSample[] ff1Samples = new FFSample[]
        {
            // FF1-AES128
            FFSample.from(10, "2B7E151628AED2A6ABF7158809CF4F3C", "0123456789", "2433477484", ""),
            FFSample.from(10, "2B7E151628AED2A6ABF7158809CF4F3C", "0123456789", "6124200773", "39383736353433323130"),
            FFSample.from(36, "2B7E151628AED2A6ABF7158809CF4F3C", "0123456789abcdefghi", "a9tv40mll9kdu509eum", "3737373770717273373737"),

            // FF1-AES192
            FFSample.from(10, "2B7E151628AED2A6ABF7158809CF4F3CEF4359D8D580AA4F", "0123456789", "2830668132", ""),
            FFSample.from(10, "2B7E151628AED2A6ABF7158809CF4F3CEF4359D8D580AA4F", "0123456789", "2496655549", "39383736353433323130"),
            FFSample.from(36, "2B7E151628AED2A6ABF7158809CF4F3CEF4359D8D580AA4F", "0123456789abcdefghi", "xbj3kv35jrawxv32ysr", "3737373770717273373737"),

            // FF1-AES256
            FFSample.from(10, "2B7E151628AED2A6ABF7158809CF4F3CEF4359D8D580AA4F7F036D6F04FC6A94", "0123456789", "6657667009", ""),
            FFSample.from(10, "2B7E151628AED2A6ABF7158809CF4F3CEF4359D8D580AA4F7F036D6F04FC6A94", "0123456789", "1001623463", "39383736353433323130"),
            FFSample.from(36, "2B7E151628AED2A6ABF7158809CF4F3CEF4359D8D580AA4F7F036D6F04FC6A94", "0123456789abcdefghi", "xs8a0azh2avyalyzuwd", "3737373770717273373737"),
        };

    private static FFSample[] ff3_1Samples = new FFSample[]
        {
            // FF3-AES128
            FFSample.from(62, "7793833CE891B496381BD5B882F77EA1", "YbpT3hDo0J9xwCQ5qUWt93iv", "dDEYxViK56lGbV1WdZTPTe4w", "C58797C2580174"),
        };

    private void testFF1()
    {
        for (int i = 0; i < ff1Samples.Length; ++i)
        {
            testFF1Sample(ff1Samples[i]);
        }

        byte[] key = Hex.Decode("EF4359D8D580AA4F7F036D6F04FC6A942B7E151628AED2A6");
        byte[] plainText = Hex.Decode("0327035100210215");
        byte[] tweak = Hex.Decode("39383736353433323130");

        FpeEngine fpeEngine = new FpeFf1Engine();

        fpeEngine.Init(true, new FpeParameters(new KeyParameter(key), 24, tweak));

        try
        {
            fpeEngine.ProcessBlock(plainText, 0, plainText.Length, plainText, 0);
            Fail("no exception");
        }
        catch (ArgumentException e)
        {
            IsEquals("input data outside of radix", e.Message);
        }

        try
        {
            fpeEngine.ProcessBlock(new byte[] { 1 }, 0, 1, plainText, 0);
            Fail("no exception");
        }
        catch (ArgumentException e)
        {
            IsEquals("input too short", e.Message);
        }
    }

    public void testFF1w()
    {
        byte[] key = Hex.Decode("EF4359D8D580AA4F7F036D6F04FC6A942B7E151628AED2A6");
        byte[] plainText = Hex.Decode("0327035100210215");
        byte[] cipherText = Hex.Decode("022701f80217020a");
        byte[] tweak = Hex.Decode("39383736353433323130");

        FpeEngine fpeEngine = new FpeFf1Engine();

        fpeEngine.Init(true, new FpeParameters(new KeyParameter(key), 1024, tweak));

        byte[] enc = new byte[plainText.Length];

        fpeEngine.ProcessBlock(plainText, 0, plainText.Length, enc, 0);

        AreEqual(cipherText, enc);

        fpeEngine.Init(false, new FpeParameters(new KeyParameter(key), 1024, tweak));

        fpeEngine.ProcessBlock(cipherText, 0, cipherText.Length, enc, 0);

        AreEqual(plainText, enc);

        byte[] outPt = Hex.Decode("03270F5100210215");

        try
        {
            fpeEngine.ProcessBlock(outPt, 0, outPt.Length, enc, 0);
        }
        catch (ArgumentException e)
        {
            IsEquals("input data outside of radix", e.Message);
        }
    }

    public void testFF3_1()
    {
        for (int i = 0; i < ff3_1Samples.Length; ++i)
        {
            testFF3_1Sample(ff3_1Samples[i]);
        }

        byte[] key = Hex.Decode("EF4359D8D580AA4F7F036D6F04FC6A942B7E151628AED2A6");
        byte[] plainText = Hex.Decode("0327035100210215");
        byte[] tweak = Hex.Decode("39383736353433");

        FpeEngine fpeEngine = new FpeFf3_1Engine();

        fpeEngine.Init(true, new FpeParameters(new KeyParameter(key), 24, tweak));

        try
        {
            fpeEngine.ProcessBlock(plainText, 0, plainText.Length, plainText, 0);
            Fail("no exception");
        }
        catch (ArgumentException e)
        {
            IsEquals("input data outside of radix", e.Message);
        }

        try
        {
            fpeEngine.Init(true, new FpeParameters(new KeyParameter(key), 24, Hex.Decode("beef")));

            Fail("no exception");
        }
        catch (ArgumentException e)
        {
            IsEquals("tweak should be 56 bits", e.Message);
        }
    }

    private void testFF3_1w()
    {
        byte[] key = Hex.Decode("EF4359D8D580AA4F7F036D6F04FC6A942B7E151628AED2A6");
        byte[] plainText = Hex.Decode("0327035100210215");
        byte[] cipherText = Hex.Decode("02fb024900310220");
        byte[] tweak = Hex.Decode("39383736353433");

        FpeEngine fpeEngine = new FpeFf3_1Engine();

        fpeEngine.Init(true, new FpeParameters(new KeyParameter(key), 1024, tweak));

        byte[] enc = new byte[plainText.Length];

        fpeEngine.ProcessBlock(plainText, 0, plainText.Length, enc, 0);

        IsTrue("enc failed: " + Hex.ToHexString(enc), AreEqual(cipherText, enc));

        fpeEngine.Init(false, new FpeParameters(new KeyParameter(key), 1024, tweak));

        fpeEngine.ProcessBlock(cipherText, 0, cipherText.Length, enc, 0);

        IsTrue(AreEqual(plainText, enc));

        byte[] outPt = Hex.Decode("03270F5100210215");

        try
        {
            fpeEngine.ProcessBlock(outPt, 0, outPt.Length, enc, 0);
        }
        catch (ArgumentException e)
        {
            IsEquals("input data outside of radix", e.Message);
        }
    }

    private void testDisable()
    {
        Environment.SetEnvironmentVariable("org.bouncycastle.fpe.disable", "true");
        try
        {
            testFF1();
            Fail("no exception");
        }
        catch (InvalidOperationException e)
        {
            IsEquals("FF1 encryption disabled", e.Message);
        }

        try
        {
            testFF3_1();
            Fail("no exception");
        }
        catch (InvalidOperationException e)
        {
            IsEquals("Fpe disabled", e.Message);
        }
        Environment.SetEnvironmentVariable("org.bouncycastle.fpe.disable", "false");

        Environment.SetEnvironmentVariable("org.bouncycastle.fpe.disable_ff1", "true");
        try
        {
            testFF1();
            Fail("no exception");
        }
        catch (InvalidOperationException e)
        {
            IsEquals("FF1 encryption disabled", e.Message);
        }

        testFF3_1();
        Environment.SetEnvironmentVariable("org.bouncycastle.fpe.disable_ff1", "false");
    }

    private void testFF3_1_255()
    {
        byte[] key = Hex.Decode("339BB5B1F2D44BAABF87CA1B7380CDC8");
        byte[] tweak = Hex.Decode("3F096DE35BFA31");
        int radix = 256;

        FpeEngine fpeEngine = new FpeFf3_1Engine();

        fpeEngine.Init(true, new FpeParameters(new KeyParameter(key), radix, tweak));

        ulong valueToEncrypt = 0x31009155FFL;

        byte[] bytes = Hex.Decode("31009155FF");
        byte[] enc = new byte[bytes.Length];
        //Encrypt

        fpeEngine.ProcessBlock(bytes, 0, bytes.Length, enc, 0);

        IsTrue(Arrays.AreEqual(Hex.Decode("18fa139dc978a681"), enc));

        //Decrypt
        fpeEngine.Init(false, new FpeParameters(new KeyParameter(key), radix, tweak));

        fpeEngine.ProcessBlock(enc, 0, enc.Length, enc, 0);

        IsTrue(Arrays.AreEqual(bytes, enc));
    }

    private void testFF1Sample(FFSample ff1)
    {
        FpeEngine fpeEngine = new FpeFf1Engine();

        fpeEngine.Init(true, new FpeParameters(new KeyParameter(ff1.getKey()), ff1.getRadix(), ff1.getTweak()));

        byte[] plain = ff1.getPlaintext();
        byte[] enc = new byte[plain.Length];

        fpeEngine.ProcessBlock(plain, 0, plain.Length, enc, 0);

        IsTrue(AreEqual(ff1.getCiphertext(), enc));

        fpeEngine.Init(false, new FpeParameters(new KeyParameter(ff1.getKey()), ff1.getRadix(), ff1.getTweak()));

        fpeEngine.ProcessBlock(ff1.getCiphertext(), 0, ff1.getCiphertext().Length, enc, 0);

        IsTrue(AreEqual(ff1.getPlaintext(), enc));
    }

    private void testFF3_1Sample(FFSample ff3_1)
    {
        FpeEngine fpeEngine = new FpeFf3_1Engine();

        fpeEngine.Init(true, new FpeParameters(new KeyParameter(ff3_1.getKey()), ff3_1.getRadix(), ff3_1.getTweak()));

        byte[] plain = ff3_1.getPlaintext();
        byte[] enc = new byte[plain.Length];

        fpeEngine.ProcessBlock(plain, 0, plain.Length, enc, 0);

        IsTrue(AreEqual(ff3_1.getCiphertext(), enc));

        fpeEngine.Init(false, new FpeParameters(new KeyParameter(ff3_1.getKey()), ff3_1.getRadix(), ff3_1.getTweak()));

        fpeEngine.ProcessBlock(ff3_1.getCiphertext(), 0, plain.Length, enc, 0);

        IsTrue(AreEqual(ff3_1.getPlaintext(), enc));
    }

    public void testFF1Bounds()
    {
        byte[] key = Hex.Decode("339BB5B1F2D44BAABF87CA1B7380CDC8");
        byte[] tweak = Hex.Decode("3F096DE35BFA31");

        FpeEngine fpeEngine = new FpeFf1Engine();

        try
        {
            IAlphabetMapper alphabetMapper = new BasicAlphabetMapper("ABCDEFGHI");

            fpeEngine.Init(true, new FpeParameters(new KeyParameter(key),
                        alphabetMapper.Radix, tweak));

            process(fpeEngine, new byte[] { 1, 2, 3 });
            Fail("no exception");
        }
        catch (ArgumentException e)
        {
           IsEquals("input too short", e.Message);
        }

        try
        {
            IAlphabetMapper alphabetMapper = new BasicAlphabetMapper("ABCD");

            fpeEngine.Init(true, new FpeParameters(new KeyParameter(key),
                        alphabetMapper.Radix, tweak));

            process(fpeEngine, new byte[] { 1, 2, 3 });
            Fail("no exception");
        }
        catch (ArgumentException e)
        {
            IsEquals("input too short", e.Message);
        }
    }

    private void testFF3_1Bounds()
    {
        String bigAlpha = "+-ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";

        IAlphabetMapper alphabetMapper = new BasicAlphabetMapper(bigAlpha);

        ff3_1Test(alphabetMapper, "467094C27E47978FE616F475215BF4F1", "ECC8AA7B87B41C", "9RwG+t8cKfa9JweBYgHAA6fHUShNZ5tc", "-DXMBhb3AFPq5Xf4oUva4WbB8eagGK2u");
        ff3_1Test(alphabetMapper, "4DB04B58E97819015A08BA7A39A79C303968A34DB0936FAD", "26B3A632FAADFE", "k5Kop6xYpT0skr1zHHPEt5rPWQ4s4O-3", "JyWzuPL6SNsciOXdEgwnKZJxHiKaTu4Z");
        ff3_1Test(alphabetMapper, "15567AA6CD8CCA401ADB6A10730655AEEC10E9101FD3969A", "379B9572B687A6", "ZpztPp90Oo5ekoNRzqArsAqAbnmM--W6", "NPxEDufvnYzVX3jxupv+iJOuPVpWRPjD");
        try
        {
            ff3_1Test(alphabetMapper, "15567AA6CD8CCA401ADB6A10730655AEEC10E9101FD3969A", "379B9572B687A6", "ZpztPp90Oo5ekoNRzqArsAqAbnmM+-W6ZZ", "L1yx-4YLQG9W1P5yTI7Wp2h0IDcRoBq1kk");
            Fail("no exception 1");
        }
        catch (ArgumentException e)
        {
           IsEquals("maximum input length is 32", e.Message);
        }

        try
        {
            ff3_1Test(alphabetMapper, "15567AA6CD8CCA401ADB6A10730655AEEC10E9101FD3969A", "379B9572B687A6", "Z", "L");
            Fail("no exception 2");
        }
        catch (ArgumentException e)
        {
           IsEquals("input too short", e.Message);
        }

        try
        {
            alphabetMapper = new BasicAlphabetMapper("ABCDEFGHI");

            ff3_1Test(alphabetMapper, "15567AA6CD8CCA401ADB6A10730655AEEC10E9101FD3969A", "379B9572B687A6", "AB", "ZZ");
            Fail("no exception 3");
        }
        catch (ArgumentException e)
        {
           IsEquals("input too short", e.Message);
        }
    }

    private void ff3_1Test(IAlphabetMapper alphabetMapper, String skey, String stweak, String input, String output)
    {
        FpeEngine fpeEncEngine = new FpeFf3_1Engine();
        FpeEngine fpeDecEngine = new FpeFf3_1Engine();

        byte[] key = Hex.Decode(skey);
        byte[] tweak = Hex.Decode(stweak);
        int radix = alphabetMapper.Radix;

        fpeEncEngine.Init(true, new FpeParameters(new KeyParameter(key), radix, tweak));
        fpeDecEngine.Init(false, new FpeParameters(new KeyParameter(key), radix, tweak));

        byte[] bytes = alphabetMapper.ConvertToIndexes(input.ToCharArray());

        byte[] encryptedBytes = process(fpeEncEngine, bytes);
        IsEquals(output, new String(alphabetMapper.ConvertToChars(encryptedBytes)));

        byte[] decryptedBytes = process(fpeDecEngine, encryptedBytes);
        IsTrue(Arrays.AreEqual(bytes, decryptedBytes));
        char[] chars = alphabetMapper.ConvertToChars(decryptedBytes);
        IsEquals(input, new String(chars));
    }

    private byte[] process(FpeEngine fpeEngine, byte[] bytes)
    {
        byte[] rv = new byte[bytes.Length];

        fpeEngine.ProcessBlock(bytes, 0, bytes.Length, rv, 0);

        return rv;
    }

    public void testUtility()
    {
        FpeCharEncryptor fpeEnc = new FpeCharEncryptor(new FpeFf1Engine(), Hex.Decode("2B7E151628AED2A6ABF7158809CF4F3C"), "0123456789".ToCharArray());

        char[] input = "01234567890123456".ToCharArray();
        char[] encrypted = fpeEnc.Process(input);

        FpeCharDecryptor fpeDec = new FpeCharDecryptor(new FpeFf1Engine(), Hex.Decode("2B7E151628AED2A6ABF7158809CF4F3C"), "0123456789".ToCharArray());
        char[] decrypted = fpeDec.Process(encrypted);

        IsTrue("no match", Arrays.AreEqual(input, decrypted));
    }

    public override string Name
    {
        get { return "SP80038GTest"; }
    }

    public override void PerformTest()
    {
        testFF1();
        testFF1w();
        testFF1Bounds();
        testFF3_1();
        testFF3_1w();
        testFF3_1_255();
        testFF3_1Bounds();
        testDisable();
        testUtility();
    }

    public class FpeCharEncryptor
    {
        private readonly FpeEngine fpeEngine;
        private IAlphabetMapper alphabetMapper;

        public FpeCharEncryptor(FpeEngine fpeEngine, byte[] key, char[] alphabet): this(fpeEngine, key, new byte[0], alphabet)
        {

        }

        public FpeCharEncryptor(FpeEngine fpeEngine, byte[] key, byte[] tweak, char[] alphabet)
        {
            this.fpeEngine = fpeEngine;

            alphabetMapper = new BasicAlphabetMapper(alphabet);

            fpeEngine.Init(true, new FpeParameters(new KeyParameter(key), alphabetMapper.Radix, tweak));
        }

        public char[] Process(char[] input)
        {
            byte[] bytes = alphabetMapper.ConvertToIndexes(input);

            fpeEngine.ProcessBlock(bytes, 0, bytes.Length, bytes, 0);

            return alphabetMapper.ConvertToChars(bytes);
        }
    }

    public class FpeCharDecryptor
    {
        private readonly FpeEngine fpeEngine;
        private IAlphabetMapper alphabetMapper;

        public FpeCharDecryptor(FpeEngine fpeEngine, byte[] key, char[] alphabet): this(fpeEngine, key, new byte[0], alphabet)
        {
        }

        public FpeCharDecryptor(FpeEngine fpeEngine, byte[] key, byte[] tweak, char[] alphabet)
        {
            this.fpeEngine = fpeEngine;

            alphabetMapper = new BasicAlphabetMapper(alphabet);

            fpeEngine.Init(false, new FpeParameters(new KeyParameter(key), alphabetMapper.Radix, tweak));
        }

        public char[] Process(char[] input)
        {
            byte[] bytes = alphabetMapper.ConvertToIndexes(input);

            fpeEngine.ProcessBlock(bytes, 0, bytes.Length, bytes, 0);

            return alphabetMapper.ConvertToChars(bytes);
        }
    }
}
}