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

using NUnit.Framework;

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

namespace Org.BouncyCastle.Tests
{
	/// <remarks>
	/// Basic FIPS test class for a block cipher, just to make sure ECB/CBC/OFB/CFB are behaving
	/// correctly. Tests from <a href="http://www.itl.nist.gov/fipspubs/fip81.htm">FIPS 81</a>.
	/// </remarks>
	[TestFixture]
	public class FipsDesTest
		: ITest
	{
		private static readonly string[] fips1Tests =
		{
			"DES/ECB/NoPadding",
			"3fa40e8a984d48156a271787ab8883f9893d51ec4b563b53",
			"DES/CBC/NoPadding",
			"e5c7cdde872bf27c43e934008c389c0f683788499a7c05f6",
			"DES/CFB/NoPadding",
			"f3096249c7f46e51a69e839b1a92f78403467133898ea622"
		};

		private static readonly string[] fips2Tests =
		{
			"DES/CFB8/NoPadding",
			"f31fda07011462ee187f",
			"DES/OFB8/NoPadding",
			"f34a2850c9c64985d684"
		};

		private static readonly byte[] input1 = Hex.Decode("4e6f77206973207468652074696d6520666f7220616c6c20");
		private static readonly byte[] input2 = Hex.Decode("4e6f7720697320746865");

		public string Name
		{
			get { return "FIPSDES"; }
		}

		public ITestResult doTest(
			string	algorithm,
			byte[]	input,
			byte[]	output)
		{
			KeyParameter key;
			IBufferedCipher inCipher, outCipher;
			CipherStream cIn, cOut;
			MemoryStream bIn, bOut;

//			IvParameterSpec spec = new IvParameterSpec();
			byte[] spec = Hex.Decode("1234567890abcdef");

			try
			{
				key = new DesParameters(Hex.Decode("0123456789abcdef"));

				inCipher = CipherUtilities.GetCipher(algorithm);
				outCipher = CipherUtilities.GetCipher(algorithm);

				if (algorithm.StartsWith("DES/ECB"))
				{
					outCipher.Init(true, key);
				}
				else
				{
					outCipher.Init(true, new ParametersWithIV(key, spec));
				}
			}
			catch (Exception e)
			{
				return new SimpleTestResult(false, Name + ": " + algorithm + " failed initialisation - " + e.ToString(), e);
			}

			try
			{
				if (algorithm.StartsWith("DES/ECB"))
				{
					inCipher.Init(false, key);
				}
				else
				{
					inCipher.Init(false, new ParametersWithIV(key, spec));
				}
			}
			catch (Exception e)
			{
				return new SimpleTestResult(false, Name + ": " + algorithm + " failed initialisation - " + e.ToString(), e);
			}

			//
			// 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)
			{
				return new SimpleTestResult(false, Name + ": " + algorithm + " failed encryption - " + e.ToString());
			}

			byte[] bytes = bOut.ToArray();

			if (!Arrays.AreEqual(bytes, output))
			{
				return new SimpleTestResult(false, Name + ": " + algorithm + " failed encryption - expected "
					+ Hex.ToHexString(output) + " got " + Hex.ToHexString(bytes));
			}

			//
			// decryption pass
			//
			bIn = new MemoryStream(bytes, false);
			cIn = new CipherStream(bIn, inCipher, null);

			try
			{
				BinaryReader dIn = new BinaryReader(cIn);

				bytes = new byte[input.Length];

				for (int i = 0; i != input.Length / 2; i++)
				{
					bytes[i] = dIn.ReadByte();
				}

				int remaining = bytes.Length - input.Length / 2;
				byte[] extra = dIn.ReadBytes(remaining);
				if (extra.Length < remaining)
					throw new EndOfStreamException();
				extra.CopyTo(bytes, input.Length / 2);
			}
			catch (Exception e)
			{
				return new SimpleTestResult(false, Name + ": " + algorithm + " failed encryption - " + e.ToString());
			}

			if (!Arrays.AreEqual(bytes, input))
			{
				return new SimpleTestResult(false, Name + ": " + algorithm + " failed decryption - expected "
					+ Hex.ToHexString(input) + " got " + Hex.ToHexString(bytes));
			}

			return new SimpleTestResult(true, Name + ": " + algorithm + " Okay");
		}

		public ITestResult Perform()
		{
			for (int i = 0; i != fips1Tests.Length; i += 2)
			{
				ITestResult result = doTest(fips1Tests[i], input1, Hex.Decode(fips1Tests[i + 1]));
				if (!result.IsSuccessful())
				{
					return result;
				}
			}

			for (int i = 0; i != fips2Tests.Length; i += 2)
			{
				ITestResult result = doTest(fips2Tests[i], input2, Hex.Decode(fips2Tests[i + 1]));
				if (!result.IsSuccessful())
				{
					return result;
				}
			}

			return new SimpleTestResult(true, Name + ": Okay");
		}

		public static void MainOld(
			string[] args)
		{
			ITest test = new FipsDesTest();
			ITestResult result = test.Perform();

			Console.WriteLine(result);
		}

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

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