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

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

namespace Org.BouncyCastle.Crypto.Tests
{
	public abstract class CipherTest
		: SimpleTest
	{
		private SimpleTest[]      _tests;
		private IBlockCipher _engine;
		private KeyParameter _validKey;

//		protected CipherTest(
//			SimpleTest[]	tests)
//		{
//			_tests = tests;
//		}

		protected CipherTest(
			SimpleTest[]	tests,
			IBlockCipher	engine,
			KeyParameter	validKey)
		{
			_tests = tests;
			_engine = engine;
			_validKey = validKey;
		}

		public override void PerformTest()
		{
			for (int i = 0; i != _tests.Length; i++)
			{
				_tests[i].PerformTest();
			}

			if (_engine != null)
			{
				//
				// state tests
				//
				byte[] buf = new byte[_engine.GetBlockSize()];

				try
				{
					_engine.ProcessBlock(buf, 0, buf, 0);

					Fail("failed initialisation check");
				}
				catch (InvalidOperationException)
				{
					// expected
				}

				bufferSizeCheck((_engine));
			}
		}

		private void bufferSizeCheck(
			IBlockCipher engine)
		{
			byte[] correctBuf = new byte[engine.GetBlockSize()];
			byte[] shortBuf = new byte[correctBuf.Length / 2];

			engine.Init(true, _validKey);

			try
			{
				engine.ProcessBlock(shortBuf, 0, correctBuf, 0);

				Fail("failed short input check");
			}
			catch (DataLengthException)
			{
				// expected
			}

			try
			{
				engine.ProcessBlock(correctBuf, 0, shortBuf, 0);

				Fail("failed short output check");
			}
			catch (DataLengthException)
			{
				// expected
			}

			engine.Init(false, _validKey);

			try
			{
				engine.ProcessBlock(shortBuf, 0, correctBuf, 0);

				Fail("failed short input check");
			}
			catch (DataLengthException)
			{
				// expected
			}

			try
			{
				engine.ProcessBlock(correctBuf, 0, shortBuf, 0);

				Fail("failed short output check");
			}
			catch (DataLengthException)
			{
				// expected
			}
		}
	}
}