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

using NUnit.Framework;

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

namespace Org.BouncyCastle.Tests
{
	/// <remarks>
	/// MAC tester - vectors from
	/// <a href="http://www.itl.nist.gov/fipspubs/fip81.htm">FIP 81</a> and
	/// <a href="http://www.itl.nist.gov/fipspubs/fip113.htm">FIP 113</a>.
	/// </remarks>
	[TestFixture]
	public class MacTest
		: SimpleTest
	{
		private static readonly byte[] keyBytes = Hex.Decode("0123456789abcdef");
		private static readonly byte[] ivBytes = Hex.Decode("1234567890abcdef");

		private static readonly byte[] input = Hex.Decode("37363534333231204e6f77206973207468652074696d6520666f7220");

		private static readonly byte[] output1 = Hex.Decode("f1d30f68");
		private static readonly byte[] output2 = Hex.Decode("58d2e77e");
		private static readonly byte[] output3 = Hex.Decode("cd647403");

		private static readonly byte[] keyBytesISO9797 = Hex.Decode("7CA110454A1A6E570131D9619DC1376E");

		private static readonly byte[] inputISO9797 = Encoding.ASCII.GetBytes("Hello World !!!!");

		private static readonly byte[] outputISO9797 = Hex.Decode("F09B856213BAB83B");

		private static readonly byte[] inputDesEDE64 = Encoding.ASCII.GetBytes("Hello World !!!!");

		private static readonly byte[] outputDesEDE64 = Hex.Decode("862304d33af01096");

		private void aliasTest(
			KeyParameter	key,
			string			primary,
			params string[]	aliases)
		{
			IMac mac = MacUtilities.GetMac(primary);

			//
			// standard DAC - zero IV
			//
			mac.Init(key);

			mac.BlockUpdate(input, 0, input.Length);

			byte[] refBytes = new byte[mac.GetMacSize()];
			mac.DoFinal(refBytes, 0);

			for (int i = 0; i != aliases.Length; i++)
			{
				mac = MacUtilities.GetMac(aliases[i]);

				mac.Init(key);

				mac.BlockUpdate(input, 0, input.Length);

				byte[] outBytes = new byte[mac.GetMacSize()];
				mac.DoFinal(outBytes, 0);

				if (!AreEqual(outBytes, refBytes))
				{
					Fail("Failed - expected "
						+ Hex.ToHexString(refBytes) + " got "
						+ Hex.ToHexString(outBytes));
				}
			}
		}

		public override void PerformTest()
		{
			KeyParameter key = new DesParameters(keyBytes);
			IMac mac = MacUtilities.GetMac("DESMac");

			//
			// standard DAC - zero IV
			//
			mac.Init(key);

			mac.BlockUpdate(input, 0, input.Length);

			//byte[] outBytes = mac.DoFinal();
			byte[] outBytes = new byte[mac.GetMacSize()];
			mac.DoFinal(outBytes, 0);

			if (!AreEqual(outBytes, output1))
			{
				Fail("Failed - expected "
					+ Hex.ToHexString(output1) + " got "
					+ Hex.ToHexString(outBytes));
			}

			//
			// mac with IV.
			//
			mac.Init(new ParametersWithIV(key, ivBytes));

			mac.BlockUpdate(input, 0, input.Length);

			//outBytes = mac.DoFinal();
			outBytes = new byte[mac.GetMacSize()];
			mac.DoFinal(outBytes, 0);

			if (!AreEqual(outBytes, output2))
			{
				Fail("Failed - expected "
					+ Hex.ToHexString(output2) + " got "
					+ Hex.ToHexString(outBytes));
			}

			//
			// CFB mac with IV - 8 bit CFB mode
			//
			mac = MacUtilities.GetMac("DESMac/CFB8");

			mac.Init(new ParametersWithIV(key, ivBytes));

			mac.BlockUpdate(input, 0, input.Length);

			//outBytes = mac.DoFinal();
			outBytes = new byte[mac.GetMacSize()];
			mac.DoFinal(outBytes, 0);

			if (!AreEqual(outBytes, output3))
			{
				Fail("Failed - expected "
					+ Hex.ToHexString(output3) + " got "
					+ Hex.ToHexString(outBytes));
			}

			//
			// ISO9797 algorithm 3 using DESEDE
			//
			key = new DesEdeParameters(keyBytesISO9797);

			mac = MacUtilities.GetMac("ISO9797ALG3");

			mac.Init(key);

			mac.BlockUpdate(inputISO9797, 0, inputISO9797.Length);

			//outBytes = mac.DoFinal();
			outBytes = new byte[mac.GetMacSize()];
			mac.DoFinal(outBytes, 0);

			if (!AreEqual(outBytes, outputISO9797))
			{
				Fail("Failed - expected "
					+ Hex.ToHexString(outputISO9797) + " got "
					+ Hex.ToHexString(outBytes));
			}

			//
			// 64bit DESede Mac
			//
			key = new DesEdeParameters(keyBytesISO9797);

			mac = MacUtilities.GetMac("DESEDE64");

			mac.Init(key);

			mac.BlockUpdate(inputDesEDE64, 0, inputDesEDE64.Length);

			//outBytes = mac.DoFinal();
			outBytes = new byte[mac.GetMacSize()];
			mac.DoFinal(outBytes, 0);

			if (!AreEqual(outBytes, outputDesEDE64))
			{
				Fail("Failed - expected "
					+ Hex.ToHexString(outputDesEDE64) + " got "
					+ Hex.ToHexString(outBytes));
			}

			aliasTest(
				ParameterUtilities.CreateKeyParameter("DESede", keyBytesISO9797),
				"DESedeMac64withISO7816-4Padding",
				"DESEDE64WITHISO7816-4PADDING",
				"DESEDEISO9797ALG1MACWITHISO7816-4PADDING",
				"DESEDEISO9797ALG1WITHISO7816-4PADDING");

			aliasTest(
				ParameterUtilities.CreateKeyParameter("DESede", keyBytesISO9797),
				"ISO9797ALG3WITHISO7816-4PADDING",
				"ISO9797ALG3MACWITHISO7816-4PADDING");
		}

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

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

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

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