summary refs log tree commit diff
path: root/crypto/test/src/test/DESedeTest.cs
blob: 39d55e4240ae87437af19171b4c059b8be185fff (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
using System;
using System.IO;
using System.Text;

using NUnit.Framework;

using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Crypto.IO;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.Utilities;
using Org.BouncyCastle.Utilities.Encoders;
using Org.BouncyCastle.Utilities.Test;

namespace Org.BouncyCastle.Tests
{
    /// <remarks>
    /// Basic test class for key generation for a DES-EDE block cipher, basically
    /// this just exercises the provider, and makes sure we are behaving sensibly,
    /// correctness of the implementation is shown in the lightweight test classes.
    /// </remarks>
    [TestFixture]
    public class DesEdeTest
        : SimpleTest
    {
        private static string[] cipherTests1 =
    {
        "112",
        "2f4bc6b30c893fa549d82c560d61cf3eb088aed020603de249d82c560d61cf3e529e95ecd8e05394",
        "128",
        "2f4bc6b30c893fa549d82c560d61cf3eb088aed020603de249d82c560d61cf3e529e95ecd8e05394",
        "168",
        "50ddb583a25c21e6c9233f8e57a86d40bb034af421c03096c9233f8e57a86d402fce91e8eb639f89",
        "192",
        "50ddb583a25c21e6c9233f8e57a86d40bb034af421c03096c9233f8e57a86d402fce91e8eb639f89",
        };

        private static byte[] input1 = Hex.Decode("000102030405060708090a0b0c0d0e0fff0102030405060708090a0b0c0d0e0f");

        /**
         * a fake random number generator - we just want to make sure the random numbers
         * aren't random so that we get the same output, while still getting to test the
         * key generation facilities.
         */
        private class FixedSecureRandom
            : SecureRandom
        {
            private byte[] seed =
            {
                (byte)0xaa, (byte)0xfd, (byte)0x12, (byte)0xf6, (byte)0x59,
                (byte)0xca, (byte)0xe6, (byte)0x34, (byte)0x89, (byte)0xb4,
                (byte)0x79, (byte)0xe5, (byte)0x07, (byte)0x6d, (byte)0xde,
                (byte)0xc2, (byte)0xf0, (byte)0x6c, (byte)0xb5, (byte)0x8f
            };

            public override void NextBytes(byte[] buf)
            {
                NextBytes(buf, 0, buf.Length);
            }

            public override void NextBytes(byte[] buf, int off, int len)
            {
                int pos = 0;
                while ((pos + seed.Length) < len)
                {
                    Array.Copy(seed, 0, buf, off + pos, seed.Length);
                    pos += seed.Length;
                }

                Array.Copy(seed, 0, buf, off + pos, len - pos);
            }
        }

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

        private void wrapTest(
            string  alg,
            int     id,
            byte[]  kek,
            byte[]  iv,
            byte[]  input,
            byte[]  output)
        {
            try
            {
                IWrapper wrapper = WrapperUtilities.GetWrapper(alg + "Wrap");

                KeyParameter desEdeKey = new DesEdeParameters(kek);
                wrapper.Init(true, new ParametersWithIV(desEdeKey, iv));

                try
                {
//					byte[] cText = wrapper.Wrap(new SecretKeySpec(input, alg));
                    byte[] cText = wrapper.Wrap(input, 0, input.Length);

                    if (!Arrays.AreEqual(cText, output))
                    {
                        Fail("failed wrap test " + id  + " expected "
                            + Hex.ToHexString(output) + " got " + Hex.ToHexString(cText));
                    }
                }
                catch (Exception e)
                {
                    Fail("failed wrap test exception " + e.ToString());
                }

                wrapper.Init(false, desEdeKey);

                try
                {
//					Key pText = wrapper.unwrap(output, alg, IBufferedCipher.SECRET_KEY);
                    byte[] pText = wrapper.Unwrap(output, 0, output.Length);
//					if (!Arrays.AreEqual(pText.getEncoded(), input))
                    if (!Arrays.AreEqual(pText, input))
                    {
                        Fail("failed unwrap test " + id  + " expected "
                            + Hex.ToHexString(input) + " got "
                            + Hex.ToHexString(pText));
                    }
                }
                catch (Exception e)
                {
                    Fail("failed unwrap test exception " + e.ToString());
                }
            }
            catch (Exception ex)
            {
                Fail("failed exception " + ex.ToString());
            }
        }

        private void doTest(
            string  alg,
            int     strength,
            byte[]  input,
            byte[]  output)
        {
            KeyParameter		key = null;
            CipherKeyGenerator	keyGen;
            SecureRandom		rand;
            IBufferedCipher		inCipher = null;
            IBufferedCipher		outCipher = null;
            CipherStream		cIn;
            CipherStream		cOut;
            MemoryStream		bIn;
            MemoryStream		bOut;

            rand = new FixedSecureRandom();

            try
            {
                keyGen = GeneratorUtilities.GetKeyGenerator(alg);
                keyGen.Init(new KeyGenerationParameters(rand, strength));

                key = new DesEdeParameters(keyGen.GenerateKey());

                inCipher = CipherUtilities.GetCipher(alg + "/ECB/PKCS7Padding");
                outCipher = CipherUtilities.GetCipher(alg + "/ECB/PKCS7Padding");

                outCipher.Init(true, new ParametersWithRandom(key, rand));
            }
            catch (Exception e)
            {
                Fail(alg + " failed initialisation - " + e.ToString());
            }

            try
            {
                inCipher.Init(false, key);
            }
            catch (Exception e)
            {
                Fail(alg + " failed initialisation - " + e.ToString());
            }

            //
            // encryption pass
            //
            bOut = new MemoryStream();

            cOut = new CipherStream(bOut, null, outCipher);

            try
            {
                for (int i = 0; i != input.Length / 2; i++)
                {
                    cOut.WriteByte(input[i]);
                }
                cOut.Write(input, input.Length / 2, input.Length - input.Length / 2);
                cOut.Close();
            }
            catch (IOException e)
            {
                Fail(alg + " failed encryption - " + e.ToString());
            }

            byte[] bytes = bOut.ToArray();

            if (!Arrays.AreEqual(bytes, output))
            {
                Fail(alg + " failed encryption - expected "
                    + Hex.ToHexString(output) + " got "
                    + Hex.ToHexString(bytes));
            }

            //
            // decryption pass
            //
            bIn = new MemoryStream(bytes, false);

            cIn = new CipherStream(bIn, inCipher, null);

            try
            {
//				DataInputStream dIn = new DataInputStream(cIn);
                BinaryReader dIn = new BinaryReader(cIn);

                bytes = new byte[input.Length];

                for (int i = 0; i != input.Length / 2; i++)
                {
                    bytes[i] = (byte)dIn.ReadByte();
                }
//				dIn.readFully(bytes, input.Length / 2, bytes.Length - input.Length / 2);
                int remaining = bytes.Length - input.Length / 2;
                byte[] rest = dIn.ReadBytes(remaining);
                if (rest.Length != remaining)
                    throw new Exception("IO problem with BinaryReader");
                rest.CopyTo(bytes, input.Length / 2);
            }
            catch (Exception e)
            {
                Fail(alg + " failed encryption - " + e.ToString());
            }

            if (!Arrays.AreEqual(bytes, input))
            {
                Fail(alg + " failed decryption - expected "
                    + Hex.ToHexString(input) + " got "
                    + Hex.ToHexString(bytes));
            }

            // TODO Put back in
//			//
//			// keyspec test
//			//
//			try
//			{
//				SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(alg);
//				DESedeKeySpec keySpec = (DESedeKeySpec)keyFactory.getKeySpec((SecretKey)key, DESedeKeySpec.class);
//
//				if (!equalArray(key.getEncoded(), keySpec.getKey(), 16))
//				{
//					Fail(alg + " KeySpec does not match key.");
//				}
//			}
//			catch (Exception e)
//			{
//				Fail(alg + " failed keyspec - " + e.ToString());
//			}
        }

        public override void PerformTest()
        {
            for (int i = 0; i != cipherTests1.Length; i += 2)
            {
                doTest("DESEDE", int.Parse(cipherTests1[i]), input1, Hex.Decode(cipherTests1[i + 1]));
            }

            for (int i = 0; i != cipherTests1.Length; i += 2)
            {
                doTest("TDEA", int.Parse(cipherTests1[i]), input1, Hex.Decode(cipherTests1[i + 1]));
            }

            byte[] kek1 = Hex.Decode("255e0d1c07b646dfb3134cc843ba8aa71f025b7c0838251f");
            byte[] iv1 = Hex.Decode("5dd4cbfc96f5453b");
            byte[] in1 = Hex.Decode("2923bf85e06dd6ae529149f1f1bae9eab3a7da3d860d3e98");
            byte[] out1 = Hex.Decode("690107618ef092b3b48ca1796b234ae9fa33ebb4159604037db5d6a84eb3aac2768c632775a467d4");

            wrapTest("DESEDE", 1, kek1, iv1, in1, out1);
            wrapTest("TDEA", 1, kek1, iv1, in1, out1);
        }

        [Test]
        public void TestFunction()
        {
            string resultText = Perform().ToString();

            Assert.AreEqual(Name + ": Okay", resultText);
        }
    }
}