RijndaelManagedTest.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. //
  2. // TestSuite.System.Security.Cryptography.RijndaelManaged.cs
  3. //
  4. // Author:
  5. // Andrew Birkett ([email protected])
  6. //
  7. using System;
  8. using System.Security.Cryptography;
  9. using NUnit.Framework;
  10. namespace MonoTests.System.Security.Cryptography {
  11. public class RijndaelManagedTest : TestCase {
  12. public void CheckCBC(ICryptoTransform encryptor, ICryptoTransform decryptor,
  13. byte[] plaintext, byte[] expected)
  14. {
  15. if ((plaintext.Length % encryptor.InputBlockSize) != 0) {
  16. throw new ArgumentException("Must have complete blocks");
  17. }
  18. byte[] ciphertext = new byte[plaintext.Length];
  19. for (int i=0; i < plaintext.Length; i += encryptor.InputBlockSize) {
  20. encryptor.TransformBlock(plaintext, i, encryptor.InputBlockSize, ciphertext, i);
  21. }
  22. for (int i=0; i<32; i++) {
  23. AssertEquals("CBC-" + i, expected[i], ciphertext[i]);
  24. }
  25. byte[] roundtrip = new byte[plaintext.Length];
  26. for (int i=0; i < ciphertext.Length; i += decryptor.InputBlockSize) {
  27. decryptor.TransformBlock(ciphertext, i, decryptor.InputBlockSize, roundtrip, i);
  28. }
  29. for (int i=0; i<32; i++) {
  30. AssertEquals("CBC-rt-" + i, roundtrip[i], plaintext[i]);
  31. }
  32. }
  33. public void TestCBC_0() {
  34. byte[] plaintext = new byte[32];
  35. for (int i=0; i < plaintext.Length; i++) plaintext[i] = 0;
  36. byte[] iv = new byte[16];
  37. for (byte i=0; i < iv.Length; i++) {
  38. iv[i] = 0;
  39. }
  40. RijndaelManaged r = new RijndaelManaged();
  41. byte[] key = new byte[16];
  42. for (int i=0; i < 16; i++) r.Key[i] = 0;
  43. r.BlockSize = 128;
  44. r.Mode = CipherMode.CBC;
  45. r.Padding = PaddingMode.Zeros;
  46. r.Key = key;
  47. byte[] expected = {
  48. 0x66, 0xe9, 0x4b, 0xd4, 0xef, 0x8a, 0x2c, 0x3b,
  49. 0x88, 0x4c, 0xfa, 0x59, 0xca, 0x34, 0x2b, 0x2e,
  50. 0xf7, 0x95, 0xbd, 0x4a, 0x52, 0xe2, 0x9e, 0xd7,
  51. 0x13, 0xd3, 0x13, 0xfa, 0x20, 0xe9, 0x8d, 0xbc };
  52. CheckCBC(r.CreateEncryptor(key, iv), r.CreateDecryptor(key, iv), plaintext, expected);
  53. }
  54. public void TestCBC_1() {
  55. byte[] plaintext = new byte[32];
  56. for (int i=0; i < plaintext.Length; i++) plaintext[i] = 0;
  57. byte[] iv = new byte[16];
  58. for (byte i=0; i < iv.Length; i++) {
  59. iv[i] = i;
  60. }
  61. RijndaelManaged r = new RijndaelManaged();
  62. byte[] key = new byte[16];
  63. for (byte i=0; i < 16; i++) key[i] = 0;
  64. r.Key = key;
  65. r.BlockSize = 128;
  66. r.Mode = CipherMode.CBC;
  67. r.Padding = PaddingMode.Zeros;
  68. byte[] expected = {
  69. 0x7a, 0xca, 0x0f, 0xd9, 0xbc, 0xd6, 0xec, 0x7c,
  70. 0x9f, 0x97, 0x46, 0x66, 0x16, 0xe6, 0xa2, 0x82,
  71. 0x66, 0xc5, 0x84, 0x17, 0x1d, 0x3c, 0x20, 0x53,
  72. 0x6f, 0x0a, 0x09, 0xdc, 0x4d, 0x1e, 0x45, 0x3b };
  73. CheckCBC(r.CreateEncryptor(key, iv), r.CreateDecryptor(key, iv), plaintext, expected);
  74. }
  75. public void CheckECBRoundtrip(ICryptoTransform encryptor, ICryptoTransform decryptor,
  76. byte[] plaintext, byte[] expected)
  77. {
  78. byte[] ciphertext = new byte[plaintext.Length];
  79. int n = encryptor.TransformBlock(plaintext, 0, plaintext.Length, ciphertext, 0);
  80. AssertEquals("ECB-len", n, expected.Length);
  81. for (int i=0; i < ciphertext.Length; i++) {
  82. AssertEquals("ECB-encrypt-" + i, ciphertext[i], expected[i]);
  83. }
  84. byte[] roundtrip = new byte[plaintext.Length];
  85. n = decryptor.TransformBlock(ciphertext, 0, ciphertext.Length, roundtrip, 0);
  86. AssertEquals("ECB-rt-len", n, plaintext.Length);
  87. for (int i=0; i < roundtrip.Length; i++) {
  88. AssertEquals("ECB-rt-" + i, roundtrip[i], plaintext[i]);
  89. }
  90. }
  91. public void TestECB() {
  92. byte[] plaintext = new byte[16];
  93. byte[] iv = new byte[16];
  94. for (int i=0; i < 16; i++) {
  95. plaintext[i] = (byte) (i*16 + i);
  96. }
  97. RijndaelManaged r = new RijndaelManaged();
  98. r.Mode = CipherMode.ECB;
  99. r.Padding = PaddingMode.Zeros;
  100. byte[] key16 = new byte[16];
  101. byte[] key24 = new byte[24];
  102. byte[] key32 = new byte[32];
  103. for (int i=0; i < 32; i++) {
  104. if (i < 16) key16[i] = (byte) i;
  105. if (i < 24) key24[i] = (byte) i;
  106. key32[i] = (byte) i;
  107. }
  108. byte[] exp16 = { 0x69, 0xc4, 0xe0, 0xd8, 0x6a, 0x7b, 0x04, 0x30,
  109. 0xd8, 0xcd, 0xb7, 0x80, 0x70, 0xb4, 0xc5, 0x5a };
  110. byte[] exp24 = { 0xdd, 0xa9, 0x7c, 0xa4, 0x86, 0x4c, 0xdf, 0xe0,
  111. 0x6e, 0xaf, 0x70, 0xa0, 0xec, 0x0d, 0x71, 0x91 };
  112. byte[] exp32 = { 0x8e, 0xa2, 0xb7, 0xca, 0x51, 0x67, 0x45, 0xbf,
  113. 0xea, 0xfc, 0x49, 0x90, 0x4b, 0x49, 0x60, 0x89 };
  114. r.Key = key16;
  115. r.KeySize = 128;
  116. CheckECBRoundtrip(
  117. r.CreateEncryptor(key16, iv), r.CreateDecryptor(key16, iv),
  118. plaintext, exp16
  119. );
  120. r.Key = key24;
  121. r.KeySize = 192;
  122. CheckECBRoundtrip(
  123. r.CreateEncryptor(key24, iv), r.CreateDecryptor(key24, iv),
  124. plaintext, exp24
  125. );
  126. r.Key = key32;
  127. r.KeySize = 256;
  128. CheckECBRoundtrip(
  129. r.CreateEncryptor(key32, iv), r.CreateDecryptor(key32, iv),
  130. plaintext, exp32
  131. );
  132. }
  133. }
  134. }