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

using NUnit.Framework;

using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.Crypto.Generators;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.Utilities.Encoders;
using Org.BouncyCastle.Utilities.Test;

namespace Org.BouncyCastle.Crypto.Tests
{
/**
 * DESede tester
 */
	[TestFixture]
	public class DesEdeTest
		:CipherTest
	{
		private static readonly byte[] weakKey =     // first 8 bytes non-weak
		{
			(byte)0x06,(byte)0x01,(byte)0x01,(byte)0x01, (byte)0x01,(byte)0x01,(byte)0x01,(byte)0x01,
			(byte)0x1f,(byte)0x1f,(byte)0x1f,(byte)0x1f, (byte)0x0e,(byte)0x0e,(byte)0x0e,(byte)0x0e,
			(byte)0xe0,(byte)0xe0,(byte)0xe0,(byte)0xe0, (byte)0xf1,(byte)0xf1,(byte)0xf1,(byte)0xf1,
		};

		private const string input1 = "4e6f77206973207468652074696d6520666f7220616c6c20";
//		private const string input2 = "4e6f7720697320746865";

		private static SimpleTest[] tests =
		{
			new BlockCipherVectorTest(0, new DesEdeEngine(),
				new DesEdeParameters(Hex.Decode("0123456789abcdef0123456789abcdef")),
				input1, "3fa40e8a984d48156a271787ab8883f9893d51ec4b563b53"),
			new BlockCipherVectorTest(1, new DesEdeEngine(),
				new DesEdeParameters(Hex.Decode("0123456789abcdeffedcba9876543210")),
				input1, "d80a0d8b2bae5e4e6a0094171abcfc2775d2235a706e232c"),
			new BlockCipherVectorTest(2, new DesEdeEngine(),
				new DesEdeParameters(Hex.Decode("0123456789abcdef0123456789abcdef0123456789abcdef")),
				input1, "3fa40e8a984d48156a271787ab8883f9893d51ec4b563b53"),
			new BlockCipherVectorTest(3, new DesEdeEngine(),
				new DesEdeParameters(Hex.Decode("0123456789abcdeffedcba98765432100123456789abcdef")),
				input1, "d80a0d8b2bae5e4e6a0094171abcfc2775d2235a706e232c")
		};

		public DesEdeTest()
			: base(tests, new DesEdeEngine(), new DesEdeParameters(new byte[16]))
		{
		}

		private void WrapTest(
			int     id,
			byte[]  kek,
			byte[]  iv,
			byte[]  input,
			byte[]  output)
		{
			IWrapper wrapper = new DesEdeWrapEngine();

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

			try
			{
				byte[] cText = wrapper.Wrap(input, 0, input.Length);
				if (!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(), e);
			}

			wrapper.Init(false, new DesEdeParameters(kek));

			try
			{
				byte[] pText = wrapper.Unwrap(output, 0, output.Length);
				if (!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(), e);
			}
		}

		public override void PerformTest()
		{
			base.PerformTest();

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

			WrapTest(1, kek1, iv1, in1, out1);

			//
			// key generation
			//
			SecureRandom random = new SecureRandom();
			DesEdeKeyGenerator keyGen = new DesEdeKeyGenerator();

			keyGen.Init(new KeyGenerationParameters(random, 112));

			byte[] kB = keyGen.GenerateKey();
	        
			if (kB.Length != 16)
			{
				Fail("112 bit key wrong length.");
			}
	        
			keyGen.Init(new KeyGenerationParameters(random, 168));
	        
			kB = keyGen.GenerateKey();
	        
			if (kB.Length != 24)
			{
				Fail("168 bit key wrong length.");
			}
	        
			try
			{
				keyGen.Init(new KeyGenerationParameters(random, 200));
	            
				Fail("invalid key length not detected.");
			}
			catch (ArgumentException)
			{
				// expected
			}

			try
			{
				DesEdeParameters.IsWeakKey(new byte[4], 0);
				Fail("no exception on small key");
			}
			catch (ArgumentException e)
			{
				if (!e.Message.Equals("key material too short."))
				{
					Fail("wrong exception");
				}
			}

			try
			{
				new DesEdeParameters(weakKey);
				Fail("no exception on weak key");
			}
			catch (ArgumentException e)
			{
				if (!e.Message.Equals("attempt to create weak DESede key"))
				{
					Fail("wrong exception");
				}
			}
		}

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

		public static void Main(
			string[] args)
		{
			RunTest(new DesEdeTest());
		}

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

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