2 // TestSuite.System.Security.Cryptography.RijndaelManaged.cs
5 // Andrew Birkett (andy@nobugs.org)
9 using System.Security.Cryptography;
11 using NUnit.Framework;
13 namespace MonoTests.System.Security.Cryptography {
15 public class RijndaelManagedTest : TestCase {
16 public void CheckCBC(ICryptoTransform encryptor, ICryptoTransform decryptor,
17 byte[] plaintext, byte[] expected)
20 if ((plaintext.Length % encryptor.InputBlockSize) != 0) {
21 throw new ArgumentException("Must have complete blocks");
24 byte[] ciphertext = new byte[plaintext.Length];
25 for (int i=0; i < plaintext.Length; i += encryptor.InputBlockSize) {
26 encryptor.TransformBlock(plaintext, i, encryptor.InputBlockSize, ciphertext, i);
29 for (int i=0; i<32; i++) {
30 AssertEquals("CBC-" + i, expected[i], ciphertext[i]);
33 byte[] roundtrip = new byte[plaintext.Length];
34 for (int i=0; i < ciphertext.Length; i += decryptor.InputBlockSize) {
35 decryptor.TransformBlock(ciphertext, i, decryptor.InputBlockSize, roundtrip, i);
38 for (int i=0; i<32; i++) {
39 AssertEquals("CBC-rt-" + i, roundtrip[i], plaintext[i]);
44 public void TestCBC_0() {
46 byte[] plaintext = new byte[32];
47 for (int i=0; i < plaintext.Length; i++) plaintext[i] = 0;
49 byte[] iv = new byte[16];
50 for (byte i=0; i < iv.Length; i++) {
54 RijndaelManaged r = new RijndaelManaged();
55 byte[] key = new byte[16];
57 for (int i=0; i < 16; i++) r.Key[i] = 0;
59 r.Mode = CipherMode.CBC;
60 r.Padding = PaddingMode.Zeros;
64 0x66, 0xe9, 0x4b, 0xd4, 0xef, 0x8a, 0x2c, 0x3b,
65 0x88, 0x4c, 0xfa, 0x59, 0xca, 0x34, 0x2b, 0x2e,
66 0xf7, 0x95, 0xbd, 0x4a, 0x52, 0xe2, 0x9e, 0xd7,
67 0x13, 0xd3, 0x13, 0xfa, 0x20, 0xe9, 0x8d, 0xbc };
69 CheckCBC(r.CreateEncryptor(key, iv), r.CreateDecryptor(key, iv), plaintext, expected);
72 public void TestCBC_1() {
74 byte[] plaintext = new byte[32];
75 for (int i=0; i < plaintext.Length; i++) plaintext[i] = 0;
77 byte[] iv = new byte[16];
78 for (byte i=0; i < iv.Length; i++) {
82 RijndaelManaged r = new RijndaelManaged();
83 byte[] key = new byte[16];
84 for (byte i=0; i < 16; i++) key[i] = 0;
88 r.Mode = CipherMode.CBC;
89 r.Padding = PaddingMode.Zeros;
92 0x7a, 0xca, 0x0f, 0xd9, 0xbc, 0xd6, 0xec, 0x7c,
93 0x9f, 0x97, 0x46, 0x66, 0x16, 0xe6, 0xa2, 0x82,
94 0x66, 0xc5, 0x84, 0x17, 0x1d, 0x3c, 0x20, 0x53,
95 0x6f, 0x0a, 0x09, 0xdc, 0x4d, 0x1e, 0x45, 0x3b };
97 CheckCBC(r.CreateEncryptor(key, iv), r.CreateDecryptor(key, iv), plaintext, expected);
100 public void CheckECBRoundtrip(ICryptoTransform encryptor, ICryptoTransform decryptor,
101 byte[] plaintext, byte[] expected)
103 byte[] ciphertext = new byte[plaintext.Length];
104 int n = encryptor.TransformBlock(plaintext, 0, plaintext.Length, ciphertext, 0);
106 AssertEquals("ECB-len", n, expected.Length);
107 for (int i=0; i < ciphertext.Length; i++) {
108 AssertEquals("ECB-encrypt-" + i, ciphertext[i], expected[i]);
111 byte[] roundtrip = new byte[plaintext.Length];
112 n = decryptor.TransformBlock(ciphertext, 0, ciphertext.Length, roundtrip, 0);
114 AssertEquals("ECB-rt-len", n, plaintext.Length);
115 for (int i=0; i < roundtrip.Length; i++) {
116 AssertEquals("ECB-rt-" + i, roundtrip[i], plaintext[i]);
120 public void TestECB() {
122 byte[] plaintext = new byte[16];
123 byte[] iv = new byte[16];
125 for (int i=0; i < 16; i++) {
126 plaintext[i] = (byte) (i*16 + i);
129 RijndaelManaged r = new RijndaelManaged();
130 r.Mode = CipherMode.ECB;
131 r.Padding = PaddingMode.Zeros;
133 byte[] key16 = new byte[16];
134 byte[] key24 = new byte[24];
135 byte[] key32 = new byte[32];
137 for (int i=0; i < 32; i++) {
138 if (i < 16) key16[i] = (byte) i;
139 if (i < 24) key24[i] = (byte) i;
144 byte[] exp16 = { 0x69, 0xc4, 0xe0, 0xd8, 0x6a, 0x7b, 0x04, 0x30,
145 0xd8, 0xcd, 0xb7, 0x80, 0x70, 0xb4, 0xc5, 0x5a };
146 byte[] exp24 = { 0xdd, 0xa9, 0x7c, 0xa4, 0x86, 0x4c, 0xdf, 0xe0,
147 0x6e, 0xaf, 0x70, 0xa0, 0xec, 0x0d, 0x71, 0x91 };
148 byte[] exp32 = { 0x8e, 0xa2, 0xb7, 0xca, 0x51, 0x67, 0x45, 0xbf,
149 0xea, 0xfc, 0x49, 0x90, 0x4b, 0x49, 0x60, 0x89 };
154 r.CreateEncryptor(key16, iv), r.CreateDecryptor(key16, iv),
162 r.CreateEncryptor(key24, iv), r.CreateDecryptor(key24, iv),
170 r.CreateEncryptor(key32, iv), r.CreateDecryptor(key32, iv),