2 // RijndaelTest.cs - NUnit Test Cases for Rijndael
5 // Sebastien Pouliot (sebastien@ximian.com)
7 // (C) 2002 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using NUnit
.Framework
;
32 using System
.Security
.Cryptography
;
35 namespace MonoTests
.System
.Security
.Cryptography
{
38 public class RijndaelTest
{
40 protected Rijndael aes
;
45 aes
= Rijndael
.Create ();
48 public void AssertEquals (string msg
, byte[] array1
, byte[] array2
)
50 AllTests
.AssertEquals (msg
, array1
, array2
);
54 public void DefaultProperties ()
57 Assert
.AreEqual (128, algo
.BlockSize
, "BlockSize");
58 Assert
.AreEqual (128, algo
.FeedbackSize
, "FeedbackSize");
59 Assert
.AreEqual (256, algo
.KeySize
, "KeySize");
60 Assert
.AreEqual (CipherMode
.CBC
, algo
.Mode
, "Mode");
61 Assert
.AreEqual (PaddingMode
.PKCS7
, algo
.Padding
, "Padding");
62 Assert
.AreEqual (1, algo
.LegalBlockSizes
.Length
, "LegalBlockSizes");
63 Assert
.AreEqual (256, algo
.LegalBlockSizes
[0].MaxSize
, "LegalBlockSizes.MaxSize");
64 Assert
.AreEqual (128, algo
.LegalBlockSizes
[0].MinSize
, "LegalBlockSizes.MinSize");
65 Assert
.AreEqual (64, algo
.LegalBlockSizes
[0].SkipSize
, "LegalBlockSizes.SkipSize");
66 Assert
.AreEqual (1, algo
.LegalKeySizes
.Length
, "LegalKeySizes");
67 Assert
.AreEqual (256, algo
.LegalKeySizes
[0].MaxSize
, "LegalKeySizes.MaxSize");
68 Assert
.AreEqual (128, algo
.LegalKeySizes
[0].MinSize
, "LegalKeySizes.MinSize");
69 Assert
.AreEqual (64, algo
.LegalKeySizes
[0].SkipSize
, "LegalKeySizes.SkipSize");
73 public void TestFIPS197_AppendixB ()
75 byte[] key
= { 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c }
;
76 byte[] iv
= new byte[16]; // empty - not used for ECB
77 byte[] input
= { 0x32, 0x43, 0xf6, 0xa8, 0x88, 0x5a, 0x30, 0x8d, 0x31, 0x31, 0x98, 0xa2, 0xe0, 0x37, 0x07, 0x34 }
;
78 byte[] expected
= { 0x39, 0x25, 0x84, 0x1d, 0x02, 0xdc, 0x09, 0xfb, 0xdc, 0x11, 0x85, 0x97, 0x19, 0x6a, 0x0b, 0x32 }
;
80 aes
.Mode
= CipherMode
.ECB
;
82 aes
.Padding
= PaddingMode
.Zeros
;
84 byte[] output
= new byte [input
.Length
];
85 ICryptoTransform encryptor
= aes
.CreateEncryptor (key
, iv
);
86 encryptor
.TransformBlock (input
, 0, input
.Length
, output
, 0);
87 AssertEquals ("FIPS197 B Encrypt", expected
, output
);
89 byte[] original
= new byte [output
.Length
];
90 ICryptoTransform decryptor
= aes
.CreateDecryptor(key
, iv
);
91 decryptor
.TransformBlock (output
, 0, output
.Length
, original
, 0);
92 AssertEquals ("FIPS197 B Decrypt", input
, original
);
95 // FIPS197 C.1 AES-128 (Nk=4, Nr=10)
96 public void TestFIPS197_AppendixC1 ()
98 byte[] key
= { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }
;
99 byte[] iv
= new byte[16]; // empty - not used for ECB
100 byte[] input
= { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff }
;
101 byte[] expected
= { 0x69, 0xc4, 0xe0, 0xd8, 0x6a, 0x7b, 0x04, 0x30, 0xd8, 0xcd, 0xb7, 0x80, 0x70, 0xb4, 0xc5, 0x5a }
;
103 aes
.Mode
= CipherMode
.ECB
;
105 aes
.Padding
= PaddingMode
.Zeros
;
107 byte[] output
= new byte [input
.Length
];
108 ICryptoTransform encryptor
= aes
.CreateEncryptor (key
, iv
);
109 encryptor
.TransformBlock(input
, 0, input
.Length
, output
, 0);
110 AssertEquals ("FIPS197 C1 Encrypt", expected
, output
);
112 byte[] original
= new byte [output
.Length
];
113 ICryptoTransform decryptor
= aes
.CreateDecryptor(key
, iv
);
114 decryptor
.TransformBlock(output
, 0, output
.Length
, original
, 0);
115 AssertEquals ("FIPS197 C1 Decrypt", input
, original
);
118 // FIPS197 C.2 AES-192 (Nk=6, Nr=12)
119 public void TestFIPS197_AppendixC2 ()
121 byte[] key
= { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 }
;
122 byte[] iv
= new byte[16]; // empty - not used for ECB
123 byte[] input
= { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff }
;
124 byte[] expected
= { 0xdd, 0xa9, 0x7c, 0xa4, 0x86, 0x4c, 0xdf, 0xe0, 0x6e, 0xaf, 0x70, 0xa0, 0xec, 0x0d, 0x71, 0x91 }
;
126 aes
.Mode
= CipherMode
.ECB
;
128 aes
.Padding
= PaddingMode
.Zeros
;
130 byte[] output
= new byte [input
.Length
];
131 ICryptoTransform encryptor
= aes
.CreateEncryptor (key
, iv
);
132 encryptor
.TransformBlock(input
, 0, input
.Length
, output
, 0);
133 AssertEquals ("FIPS197 C2 Encrypt", expected
, output
);
135 byte[] original
= new byte [output
.Length
];
136 ICryptoTransform decryptor
= aes
.CreateDecryptor(key
, iv
);
137 decryptor
.TransformBlock(output
, 0, output
.Length
, original
, 0);
138 AssertEquals ("FIPS197 C2 Decrypt", input
, original
);
141 // C.3 AES-256 (Nk=8, Nr=14)
142 public void TestFIPS197_AppendixC3 ()
144 byte[] key
= { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f }
;
145 byte[] iv
= new byte[16]; // empty - not used for ECB
146 byte[] input
= { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff }
;
147 byte[] expected
= { 0x8e, 0xa2, 0xb7, 0xca, 0x51, 0x67, 0x45, 0xbf, 0xea, 0xfc, 0x49, 0x90, 0x4b, 0x49, 0x60, 0x89 }
;
149 aes
.Mode
= CipherMode
.ECB
;
151 aes
.Padding
= PaddingMode
.Zeros
;
153 byte[] output
= new byte [input
.Length
];
154 ICryptoTransform encryptor
= aes
.CreateEncryptor (key
, iv
);
155 encryptor
.TransformBlock(input
, 0, input
.Length
, output
, 0);
156 AssertEquals ("FIPS197 C3 Encrypt", expected
, output
);
158 byte[] original
= new byte [output
.Length
];
159 ICryptoTransform decryptor
= aes
.CreateDecryptor(key
, iv
);
160 decryptor
.TransformBlock(output
, 0, output
.Length
, original
, 0);
161 AssertEquals ("FIPS197 C3 Decrypt", input
, original
);