Initial commit
[mono.git] / mcs / class / referencesource / System.Core / System / Security / Cryptography / AesManaged.cs
1 // ==++==
2 // 
3 //   Copyright (c) Microsoft Corporation.  All rights reserved.
4 // 
5 // ==--==
6
7 using System;
8 #if !SILVERLIGHT
9 using System.Diagnostics.Contracts;
10 #endif // !SILVERLIGHT
11
12 namespace System.Security.Cryptography {
13     /// <summary>
14     ///     Managed implementation of the AES algorithm. AES is esentially Rijndael with a fixed block size
15     ///     and iteration count, so we just wrap the RijndaelManaged class and allow only 128 bit blocks to
16     ///     be used.
17     /// </summary>
18     public sealed class AesManaged : Aes {
19         private RijndaelManaged m_rijndael;
20
21         public AesManaged() {
22 #if !SILVERLIGHT
23             Contract.Ensures(m_rijndael != null);
24
25             if (CryptoConfig.AllowOnlyFipsAlgorithms) {
26                 throw new InvalidOperationException(SR.GetString(SR.Cryptography_NonCompliantFIPSAlgorithm));
27             }
28 #endif // !SILVERLIGHT
29
30             m_rijndael = new RijndaelManaged();
31             m_rijndael.BlockSize = BlockSize;
32             m_rijndael.KeySize = KeySize;
33         }
34
35 #if !SILVERLIGHT
36         public override int FeedbackSize {
37             get { return m_rijndael.FeedbackSize; }
38             set { m_rijndael.FeedbackSize = value; }
39         }
40 #endif // !SILVERLIGHT
41
42         public override byte[] IV {
43             get { return m_rijndael.IV; }
44             set { m_rijndael.IV = value; }
45         }
46
47         public override byte[] Key {
48             get { return m_rijndael.Key; }
49             set { m_rijndael.Key = value; }
50         }
51
52         public override int KeySize {
53             get { return m_rijndael.KeySize; }
54             set { m_rijndael.KeySize = value; }
55         }
56
57 #if !SILVERLIGHT
58         public override CipherMode Mode {
59             get { return m_rijndael.Mode; }
60
61             set {
62                 Contract.Ensures(m_rijndael.Mode != CipherMode.CFB && m_rijndael.Mode != CipherMode.OFB);
63
64                 // RijndaelManaged will implicitly change the block size of an algorithm to match the number
65                 // of feedback bits being used. Since AES requires a block size of 128 bits, we cannot allow
66                 // the user to use the feedback modes, as this will end up breaking that invarient.
67                 if (value == CipherMode.CFB || value == CipherMode.OFB) {
68                     throw new CryptographicException(SR.GetString(SR.Cryptography_InvalidCipherMode));
69                 }
70
71                 m_rijndael.Mode = value;
72             }
73         }
74
75         public override PaddingMode Padding {
76             get { return m_rijndael.Padding; }
77             set { m_rijndael.Padding = value; }
78         }
79 #endif // !SILVERLIGHT
80
81         public override ICryptoTransform CreateDecryptor() {
82             return m_rijndael.CreateDecryptor();
83         }
84
85         public override ICryptoTransform CreateDecryptor(byte[] key, byte[] iv) {
86             if (key == null) {
87                 throw new ArgumentNullException("key");
88             }
89 #if !SILVERLIGHT
90             if (!ValidKeySize(key.Length * 8)) {
91                 throw new ArgumentException(SR.GetString(SR.Cryptography_InvalidKeySize), "key");
92             }
93             if (iv != null && iv.Length * 8 != BlockSizeValue) {
94                 throw new ArgumentException(SR.GetString(SR.Cryptography_InvalidIVSize), "iv");
95             }
96 #endif
97
98             return m_rijndael.CreateDecryptor(key, iv);
99         }
100
101
102         public override ICryptoTransform CreateEncryptor() {
103             return m_rijndael.CreateEncryptor();
104         }
105
106         public override ICryptoTransform CreateEncryptor(byte[] key, byte[] iv) {
107             if (key == null) {
108                 throw new ArgumentNullException("key");
109             }
110 #if !SILVERLIGHT
111             if (!ValidKeySize(key.Length * 8)) {
112                 throw new ArgumentException(SR.GetString(SR.Cryptography_InvalidKeySize), "key");
113             }
114             if (iv != null && iv.Length * 8 != BlockSizeValue) {
115                 throw new ArgumentException(SR.GetString(SR.Cryptography_InvalidIVSize), "iv");
116             }
117 #endif // SILVERLIGHT
118
119             return m_rijndael.CreateEncryptor(key, iv);
120         }
121
122         protected override void Dispose(bool disposing) {
123             try {
124                 if (disposing) {
125                     (m_rijndael as IDisposable).Dispose();
126                 }
127             }
128             finally {
129                 base.Dispose(disposing);
130             }
131         }
132
133         public override void GenerateIV() {
134             m_rijndael.GenerateIV();
135         }
136
137         public override void GenerateKey() {
138             m_rijndael.GenerateKey();
139         }
140     }
141 }