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 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
|
//
// MACTripleDESTest.cs - NUnit Test Cases for MACTripleDES
//
// Author:
// Sebastien Pouliot (sebastien@ximian.com)
//
// (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using NUnit.Framework;
using System;
using System.IO;
using System.Security.Cryptography;
namespace MonoTests.System.Security.Cryptography {
[TestFixture]
public class MACTripleDESTest {
protected MACTripleDES algo;
private static byte[] key1 = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
private static byte[] key2 = { 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01 };
private static byte[] key3 = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
public void AssertEquals (string msg, byte[] array1, byte[] array2)
{
AllTests.AssertEquals (msg, array1, array2);
}
protected byte[] CombineKeys (byte[] key1, byte[] key2, byte[] key3)
{
int k1l = key1.Length;
int k2l = key2.Length;
int k3l = key3.Length;
byte[] key = new byte [k1l + k2l + k3l];
Array.Copy (key1, 0, key, 0, k1l);
Array.Copy (key2, 0, key, k1l, k2l);
Array.Copy (key3, 0, key, k1l + k2l, k3l);
return key;
}
[Test]
public void ConstructorEmpty ()
{
algo = new MACTripleDES ();
}
[Test]
public void ConstructorKey ()
{
byte[] key = CombineKeys (key1, key2, key3);
algo = new MACTripleDES (key);
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void ConstructorKeyNull ()
{
algo = new MACTripleDES (null);
}
[Test]
public void ConstructorNameKey ()
{
byte[] key = CombineKeys (key1, key2, key3);
algo = new MACTripleDES ("TripleDES", key);
}
[Test]
// LAMESPEC: [ExpectedException (typeof (ArgumentNullException))]
public void ConstructorNameNullKey ()
{
byte[] key = CombineKeys (key1, key2, key3);
// funny null is a valid name!
algo = new MACTripleDES (null, key);
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void ConstructorNameNullKeyNull ()
{
algo = new MACTripleDES (null, null);
}
[Test]
public void Invariants ()
{
algo = new MACTripleDES ();
Assert.IsTrue (algo.CanReuseTransform, "MACTripleDES.CanReuseTransform");
Assert.IsTrue (algo.CanTransformMultipleBlocks, "MACTripleDES.CanTransformMultipleBlocks");
Assert.AreEqual (64, algo.HashSize, "MACTripleDES.HashSize");
Assert.AreEqual (1, algo.InputBlockSize, "MACTripleDES.InputBlockSize");
Assert.AreEqual (1, algo.OutputBlockSize, "MACTripleDES.OutputBlockSize");
Assert.AreEqual ("System.Security.Cryptography.MACTripleDES", algo.ToString (), "MACTripleDES.ToString()");
Assert.IsNotNull (algo.Key, "MACTripleDES.Key");
}
[Test]
[ExpectedException (typeof (InvalidCastException))]
public void InvalidAlgorithmName ()
{
byte[] key = CombineKeys (key1, key2, key3);
algo = new MACTripleDES ("DES", key);
}
[Test]
[ExpectedException (typeof (ObjectDisposedException))]
public void ObjectDisposed ()
{
byte[] key = CombineKeys (key1, key2, key3);
algo = new MACTripleDES (key);
algo.Clear ();
algo.ComputeHash (new byte[1]);
}
public void Check (string testName, byte[] key, byte[] data, byte[] result)
{
string classTestName = "MACTripleDES-" + testName;
CheckA (testName, key, data, result);
CheckB (testName, key, data, result);
CheckC (testName, key, data, result);
CheckD (testName, key, data, result);
CheckE (testName, key, data, result);
}
// - Test constructor #1 ()
// - Test ComputeHash (byte[]);
public void CheckA (string testName, byte[] key, byte[] data, byte[] result)
{
algo = new MACTripleDES ();
algo.Key = key;
byte[] hmac = algo.ComputeHash (data);
AssertEquals (testName + "a1", result, hmac);
AssertEquals (testName + "a2", result, algo.Hash);
}
// - Test constructor #2 (byte[])
// - Test ComputeHash (byte[], int, int);
public void CheckB (string testName, byte[] key, byte[] data, byte[] result)
{
algo = new MACTripleDES (key);
byte[] hmac = algo.ComputeHash (data, 0, data.Length);
AssertEquals (testName + "b1", result, hmac);
AssertEquals (testName + "b2", result, algo.Hash);
}
// - Test constructor #3 (string, byte[])
// - Test ComputeHash (stream);
public void CheckC (string testName, byte[] key, byte[] data, byte[] result)
{
algo = new MACTripleDES ("TripleDES", key);
algo.Key = key;
MemoryStream ms = new MemoryStream (data);
byte[] hmac = algo.ComputeHash (ms);
AssertEquals (testName + "c1", result, hmac);
AssertEquals (testName + "c2", result, algo.Hash);
}
// - Test TransformFinalBlock - alone;
public void CheckD (string testName, byte[] key, byte[] data, byte[] result)
{
algo = new MACTripleDES ();
algo.Key = key;
// LAMESPEC or FIXME: TransformFinalBlock doesn't return HashValue !
algo.TransformFinalBlock (data, 0, data.Length);
AssertEquals (testName + "d", result, algo.Hash);
}
// - Test TransformBlock/TransformFinalBlock
public void CheckE (string testName, byte[] key, byte[] data, byte[] result)
{
algo = new MACTripleDES ();
algo.Key = key;
byte[] copy = new byte [data.Length];
// LAMESPEC or FIXME: TransformFinalBlock doesn't return HashValue !
for (int i=0; i < data.Length - 1; i++)
algo.TransformBlock (data, i, 1, copy, i);
algo.TransformFinalBlock (data, data.Length - 1, 1);
AssertEquals (testName + "e", result, algo.Hash);
}
// Here data is smaller than the 3DES block size (8 bytes)
[Test]
public void SmallerThanOneBlockSize ()
{
byte[] key = CombineKeys (key1, key2, key3);
byte[] expected = { 0x86, 0xE9, 0x65, 0xBD, 0x1E, 0xC4, 0x44, 0x61 };
byte[] data = new byte [7];
Check ("3DESMAC-A1", key, data, expected);
}
// Here data is exactly one 3DES block size (8 bytes)
[Test]
public void ExactlyOneBlockSize ()
{
byte[] key = CombineKeys (key1, key2, key3);
byte [] expected = { 0x86, 0xE9, 0x65, 0xBD, 0x1E, 0xC4, 0x44, 0x61 };
byte[] data = new byte [8];
Check ("3DESMAC-A2", key, data, expected);
}
// Here data is more then one 3DES block size (8 bytes)
[Test]
public void MoreThanOneBlockSize ()
{
byte[] key = CombineKeys (key1, key2, key3);
// note: same result as A2 because of the Zero padding (and that
// we use zeros as data
byte[] expected = { 0x23, 0xD6, 0x92, 0xA0, 0x80, 0x6E, 0xC9, 0x30 };
byte[] data = new byte [14];
Check ("3DESMAC-A3", key, data, expected);
}
// Here data is a multiple of 3DES block size (8 bytes)
[Test]
public void ExactMultipleBlockSize ()
{
byte[] key = CombineKeys (key1, key2, key3);
byte [] expected = { 0xA3, 0x0E, 0x34, 0x26, 0x8B, 0x49, 0xEF, 0x49 };
byte[] data = new byte [48];
Check ("3DESMAC-A4", key, data, expected);
}
}
}
|