Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mcs / class / corlib / Test / System.Security.Cryptography / SHA512Test.cs
1 //
2 // SHA512Test.cs - NUnit Test Cases for SHA512
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // (C) 2002 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2004, 2007-2008 Novell, Inc (http://www.novell.com)
9 //
10
11 using NUnit.Framework;
12 using System;
13 using System.IO;
14 using System.Security.Cryptography;
15 using System.Text;
16
17 namespace MonoTests.System.Security.Cryptography {
18
19 // References:
20 // a.   FIPS PUB 180-2: Secure Hash Standard
21 //      http://csrc.nist.gov/publications/fips/fips180-2/fip180-2.txt
22
23 // SHA512 is a abstract class - so most of the test included here wont be tested
24 // on the abstract class but should be tested in ALL its descendants.
25
26 [TestFixture]
27 public class SHA512Test : HashAlgorithmTest {
28
29         [SetUp]
30         public override void SetUp () 
31         {
32                 hash = SHA512.Create ();
33         }
34
35         // the hash algorithm only exists as a managed implementation
36         public override bool ManagedHashImplementation {
37                 get { return true; }
38         }
39
40         // test vectors from NIST FIPS 186-2
41
42         private string input1 = "abc";
43         private string input2 = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
44
45         public void FIPS186_Test1 (SHA512 hash) 
46         {
47                 string className = hash.ToString ();
48                 byte[] result = { 0xdd, 0xaf, 0x35, 0xa1, 0x93, 0x61, 0x7a, 0xba,
49                                   0xcc, 0x41, 0x73, 0x49, 0xae, 0x20, 0x41, 0x31, 
50                                   0x12, 0xe6, 0xfa, 0x4e, 0x89, 0xa9, 0x7e, 0xa2, 
51                                   0x0a, 0x9e, 0xee, 0xe6, 0x4b, 0x55, 0xd3, 0x9a, 
52                                   0x21, 0x92, 0x99, 0x2a, 0x27, 0x4f, 0xc1, 0xa8,
53                                   0x36, 0xba, 0x3c, 0x23, 0xa3, 0xfe, 0xeb, 0xbd, 
54                                   0x45, 0x4d, 0x44, 0x23, 0x64, 0x3c, 0xe8, 0x0e, 
55                                   0x2a, 0x9a, 0xc9, 0x4f, 0xa5, 0x4c, 0xa4, 0x9f };
56                 byte[] input = Encoding.Default.GetBytes (input1);
57
58                 string testName = className + " 1";
59                 FIPS186_a (testName, hash, input, result);
60                 FIPS186_b (testName, hash, input, result);
61                 FIPS186_c (testName, hash, input, result);
62                 FIPS186_d (testName, hash, input, result);
63                 FIPS186_e (testName, hash, input, result);
64         }
65
66         public void FIPS186_Test2 (SHA512 hash) 
67         {
68                 string className = hash.ToString ();
69                 byte[] result = { 0x8e, 0x95, 0x9b, 0x75, 0xda, 0xe3, 0x13, 0xda, 
70                                   0x8c, 0xf4, 0xf7, 0x28, 0x14, 0xfc, 0x14, 0x3f, 
71                                   0x8f, 0x77, 0x79, 0xc6, 0xeb, 0x9f, 0x7f, 0xa1, 
72                                   0x72, 0x99, 0xae, 0xad, 0xb6, 0x88, 0x90, 0x18, 
73                                   0x50, 0x1d, 0x28, 0x9e, 0x49, 0x00, 0xf7, 0xe4, 
74                                   0x33, 0x1b, 0x99, 0xde, 0xc4, 0xb5, 0x43, 0x3a, 
75                                   0xc7, 0xd3, 0x29, 0xee, 0xb6, 0xdd, 0x26, 0x54, 
76                                   0x5e, 0x96, 0xe5, 0x5b, 0x87, 0x4b, 0xe9, 0x09 };
77                 byte[] input = Encoding.Default.GetBytes (input2);
78
79                 string testName = className + " 2";
80                 FIPS186_a (testName, hash, input, result);
81                 FIPS186_b (testName, hash, input, result);
82                 FIPS186_c (testName, hash, input, result);
83                 FIPS186_d (testName, hash, input, result);
84                 FIPS186_e (testName, hash, input, result);
85         }
86
87         public void FIPS186_Test3 (SHA512 hash) 
88         {
89                 string className = hash.ToString ();
90                 byte[] result = { 0xe7, 0x18, 0x48, 0x3d, 0x0c, 0xe7, 0x69, 0x64, 
91                                   0x4e, 0x2e, 0x42, 0xc7, 0xbc, 0x15, 0xb4, 0x63, 
92                                   0x8e, 0x1f, 0x98, 0xb1, 0x3b, 0x20, 0x44, 0x28,
93                                   0x56, 0x32, 0xa8, 0x03, 0xaf, 0xa9, 0x73, 0xeb,
94                                   0xde, 0x0f, 0xf2, 0x44, 0x87, 0x7e, 0xa6, 0x0a, 
95                                   0x4c, 0xb0, 0x43, 0x2c, 0xe5, 0x77, 0xc3, 0x1b, 
96                                   0xeb, 0x00, 0x9c, 0x5c, 0x2c, 0x49, 0xaa, 0x2e, 
97                                   0x4e, 0xad, 0xb2, 0x17, 0xad, 0x8c, 0xc0, 0x9b };
98                 byte[] input = new byte [1000000];
99                 for (int i = 0; i < 1000000; i++)
100                         input[i] = 0x61; // a
101
102                 string testName = className + " 3";
103                 FIPS186_a (testName, hash, input, result);
104                 FIPS186_b (testName, hash, input, result);
105                 FIPS186_c (testName, hash, input, result);
106                 FIPS186_d (testName, hash, input, result);
107                 FIPS186_e (testName, hash, input, result);
108         }
109
110         public void FIPS186_a (string testName, SHA512 hash, byte[] input, byte[] result) 
111         {
112                 byte[] output = hash.ComputeHash (input); 
113                 Assert.AreEqual (result, output, testName + ".a.1");
114                 Assert.AreEqual (result, hash.Hash, testName + ".a.2");
115                 // required or next operation will still return old hash
116                 hash.Initialize ();
117         }
118
119         public void FIPS186_b (string testName, SHA512 hash, byte[] input, byte[] result) 
120         {
121                 byte[] output = hash.ComputeHash (input, 0, input.Length); 
122                 Assert.AreEqual (result, output, testName + ".b.1");
123                 Assert.AreEqual (result, hash.Hash, testName + ".b.2");
124                 // required or next operation will still return old hash
125                 hash.Initialize ();
126         }
127
128         public void FIPS186_c (string testName, SHA512 hash, byte[] input, byte[] result) 
129         {
130                 MemoryStream ms = new MemoryStream (input);
131                 byte[] output = hash.ComputeHash (ms); 
132                 Assert.AreEqual (result, output, testName + ".c.1");
133                 Assert.AreEqual (result, hash.Hash, testName + ".c.2");
134                 // required or next operation will still return old hash
135                 hash.Initialize ();
136         }
137
138         public void FIPS186_d (string testName, SHA512 hash, byte[] input, byte[] result) 
139         {
140                 byte[] output = hash.TransformFinalBlock (input, 0, input.Length);
141                 // LAMESPEC or FIXME: TransformFinalBlock doesn't return HashValue !
142                 // AssertEquals( testName + ".d.1", result, output );
143                 Assert.IsNotNull (output, testName + ".d.1");
144                 Assert.AreEqual (result, hash.Hash, testName + ".d.2");
145                 // required or next operation will still return old hash
146                 hash.Initialize ();
147         }
148
149         public void FIPS186_e (string testName, SHA512 hash, byte[] input, byte[] result) 
150         {
151                 byte[] copy = new byte [input.Length];
152                 for (int i=0; i < input.Length - 1; i++)
153                         hash.TransformBlock (input, i, 1, copy, i);
154                 byte[] output = hash.TransformFinalBlock (input, input.Length - 1, 1);
155                 // LAMESPEC or FIXME: TransformFinalBlock doesn't return HashValue !
156                 // AssertEquals (testName + ".e.1", result, output);
157                 Assert.IsNotNull (output, testName + ".e.1");
158                 Assert.AreEqual (result, hash.Hash, testName + ".e.2");
159                 // required or next operation will still return old hash
160                 hash.Initialize ();
161         }
162
163         [Test]
164         public override void Create () 
165         {
166                 // Note: These tests will only be valid without a "machine.config" file
167                 // or a "machine.config" file that do not modify the default algorithm
168                 // configuration.
169                 const string defaultSHA512 = "System.Security.Cryptography.SHA512Managed";
170
171                 // try to build the default implementation
172                 SHA512 hash = SHA512.Create ();
173                 Assert.AreEqual (hash.ToString (), defaultSHA512, "SHA512.Create()");
174
175                 // try to build, in every way, a SHA512 implementation
176                 hash = SHA512.Create ("SHA512");
177                 Assert.AreEqual (hash.ToString (), defaultSHA512, "SHA512.Create('SHA512')");
178                 hash = SHA512.Create ("SHA-512");
179                 Assert.AreEqual (hash.ToString (), defaultSHA512, "SHA512.Create('SHA-512')");
180         }
181
182         [Test]
183         [ExpectedException (typeof (InvalidCastException))]
184         public void CreateIncorrect () 
185         {
186                 // try to build an incorrect hash algorithms
187                 hash = SHA512.Create ("MD5");
188         }
189
190         [Test]
191         public void CreateInvalid () 
192         {
193                 // try to build invalid implementation
194                 hash = SHA512.Create ("InvalidHash");
195                 Assert.IsNull (hash, "SHA512.Create('InvalidHash')");
196         }
197
198         [Test]
199         [ExpectedException (typeof (ArgumentNullException))]
200         public override void CreateNull () 
201         {
202                 // try to build null implementation
203                 hash = SHA512.Create (null);
204         }
205
206         // none of those values changes for any implementation of defaultSHA512
207         [Test]
208         public virtual void StaticInfo () 
209         {
210                 string className = hash.ToString ();
211                 Assert.AreEqual (512, hash.HashSize, className + ".HashSize");
212                 Assert.AreEqual (1, hash.InputBlockSize, className + ".InputBlockSize");
213                 Assert.AreEqual (1, hash.OutputBlockSize, className + ".OutputBlockSize");
214         }
215 }
216
217 }