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

using NUnit.Framework;

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

namespace Org.BouncyCastle.Crypto.Tests
{
	/**
	* General Padding tests.
	*/
	[TestFixture]
	public class PaddingTest : SimpleTest
	{
		public PaddingTest()
		{
		}

		private void blockCheck(
			PaddedBufferedBlockCipher   cipher,
			IBlockCipherPadding          padding,
			KeyParameter                key,
			byte[]                      data)
		{
			byte[]  outBytes = new byte[data.Length + 8];
			byte[]  dec = new byte[data.Length];

			try
			{
				cipher.Init(true, key);

				int    len = cipher.ProcessBytes(data, 0, data.Length, outBytes, 0);

				len += cipher.DoFinal(outBytes, len);

				cipher.Init(false, key);

				int    decLen = cipher.ProcessBytes(outBytes, 0, len, dec, 0);

				decLen += cipher.DoFinal(dec, decLen);

				if (!AreEqual(data, dec))
				{
					Fail("failed to decrypt - i = " + data.Length + ", padding = " + padding.PaddingName);
				}
			}
			catch (Exception e)
			{
				Fail("Exception - " + e.ToString(), e);
			}
		}

		public void doTestPadding(
			IBlockCipherPadding  padding,
			SecureRandom        rand,
			byte[]              ffVector,
			byte[]              ZeroVector)
		{
			PaddedBufferedBlockCipher    cipher = new PaddedBufferedBlockCipher(new DesEngine(), padding);
			KeyParameter                 key = new KeyParameter(Hex.Decode("0011223344556677"));

			//
			// ff test
			//
			byte[]    data = { (byte)0xff, (byte)0xff, (byte)0xff, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0 };

			if (ffVector != null)
			{
				padding.AddPadding(data, 3);

				if (!AreEqual(data, ffVector))
				{
					Fail("failed ff test for " + padding.PaddingName);
				}
			}

			//
			// zero test
			//
			if (ZeroVector != null)
			{
				data = new byte[8];
				padding.AddPadding(data, 4);

				if (!AreEqual(data, ZeroVector))
				{
					Fail("failed zero test for " + padding.PaddingName);
				}
			}

			for (int i = 1; i != 200; i++)
			{
				data = new byte[i];

				rand.NextBytes(data);

				blockCheck(cipher, padding, key, data);
			}
		}

		public override void PerformTest()
		{
			SecureRandom    rand = new SecureRandom(new byte[20]);

			rand.SetSeed(DateTime.Now.Ticks);

			doTestPadding(new Pkcs7Padding(), rand,
				Hex.Decode("ffffff0505050505"),
				Hex.Decode("0000000004040404"));

			Pkcs7Padding padder = new Pkcs7Padding();
			try
			{
				padder.PadCount(new byte[8]);

				Fail("invalid padding not detected");
			}
			catch (InvalidCipherTextException e)
			{
				if (!"pad block corrupted".Equals(e.Message))
				{
					Fail("wrong exception for corrupt padding: " + e);
				}
			} 

			doTestPadding(new ISO10126d2Padding(), rand,
				null,
				null);

			doTestPadding(new X923Padding(), rand,
				null,
				null);

			doTestPadding(new TbcPadding(), rand,
				Hex.Decode("ffffff0000000000"),
				Hex.Decode("00000000ffffffff"));

			doTestPadding(new ZeroBytePadding(), rand,
				Hex.Decode("ffffff0000000000"),
				null);

			doTestPadding(new ISO7816d4Padding(), rand,
				Hex.Decode("ffffff8000000000"),
				Hex.Decode("0000000080000000"));
		}

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

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

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

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