imported everything from my branch (which is slightly harmless).
[mono.git] / mcs / class / corlib / Test / System.Security.Cryptography / RijndaelManagedTest.cs
1 //
2 // TestSuite.System.Security.Cryptography.RijndaelManaged.cs
3 //
4 // Author:
5 //      Andrew Birkett (andy@nobugs.org)
6 //
7
8 using System;
9 using System.Security.Cryptography;
10
11 using NUnit.Framework;
12
13 namespace MonoTests.System.Security.Cryptography {
14
15         public class RijndaelManagedTest : TestCase {
16                 public void CheckCBC(ICryptoTransform encryptor, ICryptoTransform decryptor, 
17                                            byte[] plaintext, byte[] expected) 
18                 {
19         
20                         if ((plaintext.Length % encryptor.InputBlockSize) != 0) {
21                                 throw new ArgumentException("Must have complete blocks");
22                         }
23         
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);
27                         }
28         
29                         for (int i=0; i<32; i++) {
30                                 AssertEquals("CBC-" + i, expected[i], ciphertext[i]);
31                         }
32         
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);
36                         }
37         
38                         for (int i=0; i<32; i++) {
39                                 AssertEquals("CBC-rt-" + i, roundtrip[i], plaintext[i]);
40                         }
41         
42                 }
43         
44                 public void TestCBC_0() {
45         
46                         byte[] plaintext = new byte[32];
47                         for (int i=0; i < plaintext.Length; i++) plaintext[i] = 0;
48         
49                         byte[] iv = new byte[16];
50                         for (byte i=0; i < iv.Length; i++) {
51                                 iv[i] = 0;
52                         }
53         
54                         RijndaelManaged r = new RijndaelManaged();
55                         byte[] key = new byte[16];      
56         
57                         for (int i=0; i < 16; i++) r.Key[i] = 0;
58                         r.BlockSize = 128;
59                         r.Mode = CipherMode.CBC;
60                         r.Padding = PaddingMode.Zeros;
61                         r.Key = key;
62         
63                         byte[] expected = { 
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 };
68         
69                         CheckCBC(r.CreateEncryptor(key, iv), r.CreateDecryptor(key, iv), plaintext, expected);
70                 }
71         
72                 public void TestCBC_1() {
73         
74                         byte[] plaintext = new byte[32];
75                         for (int i=0; i < plaintext.Length; i++) plaintext[i] = 0;
76         
77                         byte[] iv = new byte[16];
78                         for (byte i=0; i < iv.Length; i++) {
79                                 iv[i] = i;
80                         }
81         
82                         RijndaelManaged r = new RijndaelManaged();
83                         byte[] key = new byte[16];
84                         for (byte i=0; i < 16; i++) key[i] = 0;
85
86                         r.Key = key;
87                         r.BlockSize = 128;
88                         r.Mode = CipherMode.CBC;
89                         r.Padding = PaddingMode.Zeros;
90         
91                         byte[] expected = { 
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 };
96         
97                         CheckCBC(r.CreateEncryptor(key, iv), r.CreateDecryptor(key, iv), plaintext, expected);
98                 }
99         
100                 public void CheckECBRoundtrip(ICryptoTransform encryptor, ICryptoTransform decryptor, 
101                                            byte[] plaintext, byte[] expected)
102                 {
103                         byte[] ciphertext = new byte[plaintext.Length];
104                         int n = encryptor.TransformBlock(plaintext, 0, plaintext.Length, ciphertext, 0);
105         
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]);
109                         }
110         
111                         byte[] roundtrip = new byte[plaintext.Length];
112                         n = decryptor.TransformBlock(ciphertext, 0, ciphertext.Length, roundtrip, 0);
113         
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]);
117                         }
118                 }
119         
120                 public void TestECB() {
121         
122                         byte[] plaintext = new byte[16];
123                         byte[] iv = new byte[16];
124         
125                         for (int i=0; i < 16; i++) {
126                                 plaintext[i] = (byte) (i*16 + i);
127                         }
128         
129                         RijndaelManaged r = new RijndaelManaged();
130                         r.Mode = CipherMode.ECB;
131                         r.Padding = PaddingMode.Zeros;
132         
133                         byte[] key16 = new byte[16];
134                         byte[] key24 = new byte[24];
135                         byte[] key32 = new byte[32];
136         
137                         for (int i=0; i < 32; i++) {
138                                 if (i < 16) key16[i] = (byte) i;
139                                 if (i < 24) key24[i] = (byte) i;
140                                 key32[i] = (byte) i;
141                         }
142         
143                                 
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 }; 
150         
151                         r.Key = key16;
152                         r.KeySize = 128;        
153                         CheckECBRoundtrip(
154                                 r.CreateEncryptor(key16, iv), r.CreateDecryptor(key16, iv), 
155                                 plaintext, exp16
156                         );
157         
158         
159                         r.Key = key24;
160                         r.KeySize = 192;
161                         CheckECBRoundtrip(
162                                 r.CreateEncryptor(key24, iv), r.CreateDecryptor(key24, iv), 
163                                 plaintext, exp24
164                         );
165         
166         
167                         r.Key = key32;
168                         r.KeySize = 256;
169                         CheckECBRoundtrip(
170                                 r.CreateEncryptor(key32, iv), r.CreateDecryptor(key32, iv), 
171                                 plaintext, exp32
172                         );
173                 }
174         }
175 }