Merge pull request #517 from getsometoast/master
[mono.git] / mcs / class / System.Core / System.Security.Cryptography / AesManaged.cs
1 //
2 // System.Security.Cryptography.AesManaged.cs
3 //
4 // Authors:
5 //      Mark Crichton (crichton@gimp.org)
6 //      Andrew Birkett (andy@nobugs.org)
7 //      Sebastien Pouliot (sebastien@xamarin.com)
8 //      Kazuki Oikawa (kazuki@panicode.com)
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
35 using System.Security.Permissions;
36
37 using Mono.Security.Cryptography;
38
39 namespace System.Security.Cryptography {
40         
41         // References:
42         // a.   FIPS PUB 197: Advanced Encryption Standard
43         //      http://csrc.nist.gov/publications/fips/fips197/fips-197.pdf
44         // b.   Aes Specification
45         //      http://csrc.nist.gov/CryptoToolkit/aes/Aes/Aes-ammended.pdf
46         
47         [HostProtection (SecurityAction.LinkDemand, MayLeakOnAbort=true)]
48         public sealed class AesManaged : Aes {
49                 
50                 public AesManaged ()
51                 {
52                 }
53                 
54                 public override void GenerateIV ()
55                 {
56                         IVValue = KeyBuilder.IV (BlockSizeValue >> 3);
57                 }
58                 
59                 public override void GenerateKey ()
60                 {
61                         KeyValue = KeyBuilder.Key (KeySizeValue >> 3);
62                 }
63                 
64                 public override ICryptoTransform CreateDecryptor (byte[] key, byte[] iv) 
65                 {
66                         return new AesTransform (this, false, key, iv);
67                 }
68                 
69                 public override ICryptoTransform CreateEncryptor (byte[] key, byte[] iv) 
70                 {
71                         return new AesTransform (this, true, key, iv);
72                 }
73
74                 // I suppose some attributes differs ?!? because this does not look required
75
76                 public override byte[] IV {
77                         get { return base.IV; }
78                         set { base.IV = value; }
79                 }
80
81                 public override byte[] Key {
82                         get { return base.Key; }
83                         set { base.Key = value; }
84                 }
85
86                 public override int KeySize {
87                         get { return base.KeySize; }
88                         set { base.KeySize = value; }
89                 }
90
91                 public override int FeedbackSize {
92                         get { return base.FeedbackSize; }
93                         set { base.FeedbackSize = value; }
94                 }
95
96                 public override CipherMode Mode {
97                         get { return base.Mode; }
98                         set {
99                                 switch (value) {
100                                 case CipherMode.CBC:
101                                 case CipherMode.ECB:
102                                         base.Mode = value;
103                                         break;
104                                 default:
105                                         throw new CryptographicException ("CipherMode is not supported");
106                                 }
107                         }
108                 }
109
110                 public override PaddingMode Padding {
111                         get { return base.Padding; }
112                         set { base.Padding = value; }
113                 }
114
115                 public override ICryptoTransform CreateDecryptor () 
116                 {
117                         return CreateDecryptor (Key, IV);
118                 }
119
120                 public override ICryptoTransform CreateEncryptor() 
121                 {
122                         return CreateEncryptor (Key, IV);
123                 }
124
125                 protected override void Dispose (bool disposing) 
126                 {
127                         base.Dispose (disposing);
128                 }
129         }
130 }