Update Reference Sources to .NET Framework 4.6.1
[mono.git] / mcs / class / referencesource / mscorlib / system / security / cryptography / rc2cryptoserviceprovider.cs
1 // ==++==
2 // 
3 //   Copyright (c) Microsoft Corporation.  All rights reserved.
4 // 
5 // ==--==
6 // <OWNER>[....]</OWNER>
7 // 
8
9 //
10 // RC2CryptoServiceProvider.cs
11 //
12
13 namespace System.Security.Cryptography {
14
15     using System.Globalization;
16     using System.Diagnostics.Contracts;
17
18     [System.Runtime.InteropServices.ComVisible(true)]
19     public sealed class RC2CryptoServiceProvider : RC2 {
20         private bool m_use40bitSalt = false;
21
22         private static  KeySizes[] s_legalKeySizes = {
23             new KeySizes(40, 128, 8)  // cryptoAPI implementation only goes up to 128
24         };
25
26         //
27         // public constructors
28         //
29
30         [System.Security.SecuritySafeCritical]  // auto-generated
31         public RC2CryptoServiceProvider () {
32             if (CryptoConfig.AllowOnlyFipsAlgorithms)
33                 throw new InvalidOperationException(Environment.GetResourceString("Cryptography_NonCompliantFIPSAlgorithm"));
34             Contract.EndContractBlock();
35             if (!Utils.HasAlgorithm(Constants.CALG_RC2, 0))
36                 throw new CryptographicException(Environment.GetResourceString("Cryptography_CSP_AlgorithmNotAvailable"));
37
38             // Acquire a Type 1 provider. This will be the Enhanced provider if available, otherwise 
39             // it will be the base provider.
40             LegalKeySizesValue = s_legalKeySizes;
41
42             // Since the CSP only supports a CFB feedback of 8, make that the default
43             FeedbackSizeValue = 8;
44         }
45
46         //
47         // public methods
48         //
49
50         public override int EffectiveKeySize {
51             get {
52                 return KeySizeValue;
53             }
54             set {
55                 if (value != KeySizeValue)
56                     throw new CryptographicUnexpectedOperationException(Environment.GetResourceString("Cryptography_RC2_EKSKS2"));
57             }
58         }
59
60         [System.Runtime.InteropServices.ComVisible(false)]
61         public bool UseSalt {
62             get {
63                 return m_use40bitSalt;
64             }
65             set {
66                 m_use40bitSalt = value;
67             }
68         }
69
70         [System.Security.SecuritySafeCritical]  // auto-generated
71         public override ICryptoTransform CreateEncryptor (byte[] rgbKey, byte[] rgbIV) {
72 #if MONO
73             return new RC2Transform (this, true, rgbKey, rgbIV);
74 #else
75             return _NewEncryptor(rgbKey, ModeValue, rgbIV, EffectiveKeySizeValue, 
76                                  FeedbackSizeValue, CryptoAPITransformMode.Encrypt);
77 #endif
78         }
79
80         [System.Security.SecuritySafeCritical]  // auto-generated
81         public override ICryptoTransform CreateDecryptor (byte[] rgbKey, byte[] rgbIV) {
82 #if MONO
83             return new RC2Transform (this, false, rgbKey, rgbIV);
84 #else
85             return _NewEncryptor(rgbKey, ModeValue, rgbIV, EffectiveKeySizeValue,
86                                  FeedbackSizeValue, CryptoAPITransformMode.Decrypt);
87 #endif
88         }
89
90         public override void GenerateKey () {
91             KeyValue = new byte[KeySizeValue/8];
92             Utils.StaticRandomNumberGenerator.GetBytes(KeyValue);
93         }
94
95         public override void GenerateIV () {
96             // block size is always 64 bits so IV is always 64 bits == 8 bytes
97             IVValue = new byte[8];
98             Utils.StaticRandomNumberGenerator.GetBytes(IVValue);
99         }
100
101         //
102         // private methods
103         //
104 #if !MONO
105         [System.Security.SecurityCritical]  // auto-generated
106         private ICryptoTransform _NewEncryptor (byte[] rgbKey, CipherMode mode, byte[] rgbIV,
107                                                 int effectiveKeySize, int feedbackSize, CryptoAPITransformMode encryptMode) {
108             int cArgs = 0;
109             int[] rgArgIds = new int[10];
110             Object[] rgArgValues = new Object[10];
111
112             // Check for bad values
113             // 1) we don't support OFB mode in RC2_CSP
114             if (mode == CipherMode.OFB)
115                 throw new CryptographicException(Environment.GetResourceString("Cryptography_CSP_OFBNotSupported"));
116             // 2) we only support CFB with a feedback size of 8 bits
117             if ((mode == CipherMode.CFB) && (feedbackSize != 8)) 
118                 throw new CryptographicException(Environment.GetResourceString("Cryptography_CSP_CFBSizeNotSupported"));
119
120             if (rgbKey == null) {
121                 rgbKey = new byte[KeySizeValue/8];
122                 Utils.StaticRandomNumberGenerator.GetBytes(rgbKey);
123             }
124
125             // Check the rgbKey size
126             int keySizeValue = rgbKey.Length * 8;
127             if (!ValidKeySize(keySizeValue))
128                 throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidKeySize"));
129
130             // Deal with effective key length questions
131             rgArgIds[cArgs] = Constants.KP_EFFECTIVE_KEYLEN;
132             if (EffectiveKeySizeValue == 0) {
133                 rgArgValues[cArgs] = keySizeValue;
134             } else {
135                 rgArgValues[cArgs] = effectiveKeySize;
136             }
137             cArgs += 1;
138
139             // Set the mode for the encryptor (defaults to CBC)
140             if (mode != CipherMode.CBC) {
141                 rgArgIds[cArgs] = Constants.KP_MODE;
142                 rgArgValues[cArgs] = mode;
143                 cArgs += 1;
144             }
145
146             // If not ECB mode -- pass in an IV
147             if (mode != CipherMode.ECB) {
148                 if (rgbIV == null) {
149                     rgbIV = new byte[8];
150                     Utils.StaticRandomNumberGenerator.GetBytes(rgbIV);
151                 }
152
153                 //
154                 // We truncate IV's that are longer than the block size to 8 bytes : this is
155                 // done to maintain backward compatibility with the behavior shipped in V1.x.
156                 // The call to set the IV in CryptoAPI will ignore any bytes after the first 8
157                 // bytes. We'll still reject IV's that are shorter than the block size though.
158                 //
159                 if (rgbIV.Length < 8)
160                     throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidIVSize"));
161
162                 rgArgIds[cArgs] = Constants.KP_IV;
163                 rgArgValues[cArgs] = rgbIV;
164                 cArgs += 1;
165             }
166
167             // If doing OFB or CFB, then we need to set the feed back loop size
168             if ((mode == CipherMode.OFB) || (mode == CipherMode.CFB)) {
169                 rgArgIds[cArgs] = Constants.KP_MODE_BITS;
170                 rgArgValues[cArgs] = feedbackSize;
171                 cArgs += 1;
172             }
173
174             if (!Utils.HasAlgorithm(Constants.CALG_RC2, keySizeValue))
175                 throw new CryptographicException(Environment.GetResourceString("Cryptography_CSP_AlgKeySizeNotAvailable", keySizeValue));
176
177             //  Create the encryptor/decryptor object
178             return new CryptoAPITransform(Constants.CALG_RC2, cArgs, rgArgIds, 
179                                           rgArgValues, rgbKey, PaddingValue, 
180                                           mode, BlockSizeValue, feedbackSize, m_use40bitSalt,
181                                           encryptMode);
182         }
183 #endif
184     }
185 }