AesManaged.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //
  2. // System.Security.Cryptography.AesManaged.cs
  3. //
  4. // Authors:
  5. // Mark Crichton ([email protected])
  6. // Andrew Birkett ([email protected])
  7. // Sebastien Pouliot ([email protected])
  8. // Kazuki Oikawa ([email protected])
  9. //
  10. // (C) 2002
  11. // Portions (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
  12. // Copyright (C) 2004-2005,2008 Novell, Inc (http://www.novell.com)
  13. // Copyright 2013 Xamarin Inc (http://www.xamarin.com)
  14. //
  15. // Permission is hereby granted, free of charge, to any person obtaining
  16. // a copy of this software and associated documentation files (the
  17. // "Software"), to deal in the Software without restriction, including
  18. // without limitation the rights to use, copy, modify, merge, publish,
  19. // distribute, sublicense, and/or sell copies of the Software, and to
  20. // permit persons to whom the Software is furnished to do so, subject to
  21. // the following conditions:
  22. //
  23. // The above copyright notice and this permission notice shall be
  24. // included in all copies or substantial portions of the Software.
  25. //
  26. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  27. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  28. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  29. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  30. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  31. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  32. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  33. //
  34. using System.Security.Permissions;
  35. using Mono.Security.Cryptography;
  36. namespace System.Security.Cryptography {
  37. // References:
  38. // a. FIPS PUB 197: Advanced Encryption Standard
  39. // http://csrc.nist.gov/publications/fips/fips197/fips-197.pdf
  40. // b. Aes Specification
  41. // http://csrc.nist.gov/CryptoToolkit/aes/Aes/Aes-ammended.pdf
  42. [HostProtection (SecurityAction.LinkDemand, MayLeakOnAbort=true)]
  43. public sealed class AesManaged : Aes {
  44. public AesManaged ()
  45. {
  46. }
  47. public override void GenerateIV ()
  48. {
  49. IVValue = KeyBuilder.IV (BlockSizeValue >> 3);
  50. }
  51. public override void GenerateKey ()
  52. {
  53. KeyValue = KeyBuilder.Key (KeySizeValue >> 3);
  54. }
  55. public override ICryptoTransform CreateDecryptor (byte[] key, byte[] iv)
  56. {
  57. return new AesTransform (this, false, key, iv);
  58. }
  59. public override ICryptoTransform CreateEncryptor (byte[] key, byte[] iv)
  60. {
  61. return new AesTransform (this, true, key, iv);
  62. }
  63. // I suppose some attributes differs ?!? because this does not look required
  64. public override byte[] IV {
  65. get { return base.IV; }
  66. set { base.IV = value; }
  67. }
  68. public override byte[] Key {
  69. get { return base.Key; }
  70. set { base.Key = value; }
  71. }
  72. public override int KeySize {
  73. get { return base.KeySize; }
  74. set { base.KeySize = value; }
  75. }
  76. public override int FeedbackSize {
  77. get { return base.FeedbackSize; }
  78. set { base.FeedbackSize = value; }
  79. }
  80. public override CipherMode Mode {
  81. get { return base.Mode; }
  82. set {
  83. switch (value) {
  84. case CipherMode.CBC:
  85. case CipherMode.ECB:
  86. base.Mode = value;
  87. break;
  88. default:
  89. throw new CryptographicException ("CipherMode is not supported");
  90. }
  91. }
  92. }
  93. public override PaddingMode Padding {
  94. get { return base.Padding; }
  95. set { base.Padding = value; }
  96. }
  97. public override ICryptoTransform CreateDecryptor ()
  98. {
  99. return CreateDecryptor (Key, IV);
  100. }
  101. public override ICryptoTransform CreateEncryptor()
  102. {
  103. return CreateEncryptor (Key, IV);
  104. }
  105. protected override void Dispose (bool disposing)
  106. {
  107. base.Dispose (disposing);
  108. }
  109. }
  110. }