Updates referencesource to .NET 4.7
[mono.git] / mcs / class / referencesource / mscorlib / system / security / cryptography / rijndaelmanaged.cs
1 using System.Diagnostics.Contracts;
2 // ==++==
3 // 
4 //   Copyright (c) Microsoft Corporation.  All rights reserved.
5 // 
6 // ==--==
7 // <OWNER>Microsoft</OWNER>
8 // 
9
10 //
11 // RijndaelManaged.cs
12 //
13
14 namespace System.Security.Cryptography
15 {
16 [System.Runtime.InteropServices.ComVisible(true)]
17     public sealed class RijndaelManaged : Rijndael {
18         public RijndaelManaged () {
19 #if FEATURE_CRYPTO
20             if (CryptoConfig.AllowOnlyFipsAlgorithms)
21                 throw new InvalidOperationException(Environment.GetResourceString("Cryptography_NonCompliantFIPSAlgorithm"));
22             Contract.EndContractBlock();
23 #endif // FEATURE_CRYPTO
24         }
25
26         // #CoreCLRRijndaelModes
27         // 
28         // On CoreCLR we limit the supported cipher modes and padding modes for the AES algorithm to a
29         // single hard coded value.  This allows us to remove a lot of code by removing support for the
30         // uncommon cases and forcing everyone to use the same common padding and ciper modes:
31         // 
32         //  - CipherMode: CipherMode.CBC
33         //  - PaddingMode: PaddingMode.PKCS7
34  
35         public override ICryptoTransform CreateEncryptor (byte[] rgbKey, byte[] rgbIV) {
36             return NewEncryptor(rgbKey,
37                                 ModeValue,
38                                 rgbIV,
39                                 FeedbackSizeValue,
40                                 RijndaelManagedTransformMode.Encrypt);
41         }
42
43         public override ICryptoTransform CreateDecryptor (byte[] rgbKey, byte[] rgbIV) {
44             return NewEncryptor(rgbKey,
45                                 ModeValue,
46                                 rgbIV,
47                                 FeedbackSizeValue,
48                                 RijndaelManagedTransformMode.Decrypt);
49         }
50
51         public override void GenerateKey () {
52             KeyValue = Utils.GenerateRandom(KeySizeValue / 8);
53         }
54
55         public override void GenerateIV () {
56             IVValue = Utils.GenerateRandom(BlockSizeValue / 8);
57         }
58
59         private ICryptoTransform NewEncryptor (byte[] rgbKey,
60                                                CipherMode mode,
61                                                byte[] rgbIV,
62                                                int feedbackSize,
63                                                RijndaelManagedTransformMode encryptMode) {
64             // Build the key if one does not already exist
65             if (rgbKey == null) {
66                 rgbKey = Utils.GenerateRandom(KeySizeValue / 8);
67             }
68
69             // If not ECB mode, make sure we have an IV. In CoreCLR we do not support ECB, so we must have
70             // an IV in all cases.
71 #if !FEATURE_CRYPTO
72             if (mode != CipherMode.ECB) {
73 #endif // !FEATURE_CRYPTO
74                 if (rgbIV == null) {
75                     rgbIV = Utils.GenerateRandom(BlockSizeValue / 8);
76                 }
77 #if !FEATURE_CRYPTO
78             }
79 #endif // !FEATURE_CRYPTO
80
81             // Create the encryptor/decryptor object
82             return new RijndaelManagedTransform (rgbKey,
83                                                  mode,
84                                                  rgbIV,
85                                                  BlockSizeValue,
86                                                  feedbackSize,
87                                                  PaddingValue,
88                                                  encryptMode);
89         }
90     }
91 }