AesCryptoServiceProviderTest.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //
  2. // Unit tests for AesCryptoServiceProvider
  3. //
  4. // Author:
  5. // Sebastien Pouliot <[email protected]>
  6. //
  7. // Copyright (C) 2013 Xamarin Inc (http://www.xamarin.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. #if !MOBILE
  29. using System;
  30. using System.Security.Cryptography;
  31. using NUnit.Framework;
  32. namespace MonoTests.System.Security.Cryptography {
  33. [TestFixture]
  34. public class AesCryptoServiceProviderTest {
  35. [Test]
  36. [ExpectedException (typeof (CryptographicException))]
  37. public void CTS_NotAllowed ()
  38. {
  39. // this check is normally (e.g. RijndaelManaged) done later
  40. using (var aes = new AesCryptoServiceProvider ()) {
  41. aes.Mode = CipherMode.CTS;
  42. }
  43. }
  44. [Test]
  45. public void CBC_Allowed ()
  46. {
  47. using (var aes = new AesCryptoServiceProvider ()) {
  48. aes.Mode = CipherMode.CBC;
  49. }
  50. }
  51. [Test]
  52. public void ECB_Allowed ()
  53. {
  54. using (var aes = new AesCryptoServiceProvider ()) {
  55. aes.Mode = CipherMode.ECB;
  56. }
  57. }
  58. [Test]
  59. public void OFB_Allowed ()
  60. {
  61. // this not supported by AesManaged
  62. using (var aes = new AesCryptoServiceProvider ()) {
  63. aes.Mode = CipherMode.OFB;
  64. // FIXME: check is really implemented (or if the check is only done later, like RjindaelManaged)
  65. }
  66. }
  67. [Test]
  68. public void CFB_Allowed ()
  69. {
  70. using (var aes = new AesCryptoServiceProvider ()) {
  71. // AesManaged does not support CFB at all
  72. aes.Mode = CipherMode.CFB;
  73. Assert.AreEqual (8, aes.FeedbackSize, "FeedbackSize (default)");
  74. int block_size = aes.BlockSize / 8;
  75. for (int i = 8; i <= 64; i += 8) {
  76. aes.FeedbackSize = i;
  77. using (ICryptoTransform t = aes.CreateEncryptor ()) {
  78. // RjindaelManaged transform block size are different!
  79. Assert.AreEqual (block_size, t.InputBlockSize, "InputBlockSize CFB{0}", i);
  80. Assert.AreEqual (block_size, t.OutputBlockSize, "OutputBlockSize CFB{0}", i);
  81. }
  82. }
  83. }
  84. }
  85. [Test]
  86. [ExpectedException (typeof (CryptographicException))]
  87. public void CFB_TooSmall ()
  88. {
  89. using (var aes = new AesCryptoServiceProvider ()) {
  90. aes.Mode = CipherMode.CFB;
  91. aes.FeedbackSize = 0;
  92. }
  93. }
  94. [Test]
  95. [ExpectedException (typeof (CryptographicException))]
  96. public void CFB_TooBig ()
  97. {
  98. using (var aes = new AesCryptoServiceProvider ()) {
  99. aes.Mode = CipherMode.CFB;
  100. aes.FeedbackSize = 72;
  101. Assert.AreEqual (72, aes.FeedbackSize, "FeedbackSize");
  102. // we can't set it but can't use it
  103. aes.CreateEncryptor (aes.Key, aes.IV);
  104. }
  105. }
  106. }
  107. }
  108. #endif