Updates referencesource to .NET 4.7
[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>Microsoft</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             if (m_use40bitSalt)
74                 throw new NotImplementedException ("UseSalt=true is not implemented on Mono yet");
75
76             return new RC2Transform (this, true, rgbKey, rgbIV);
77 #else
78             return _NewEncryptor(rgbKey, ModeValue, rgbIV, EffectiveKeySizeValue, 
79                                  FeedbackSizeValue, CryptoAPITransformMode.Encrypt);
80 #endif
81         }
82
83         [System.Security.SecuritySafeCritical]  // auto-generated
84         public override ICryptoTransform CreateDecryptor (byte[] rgbKey, byte[] rgbIV) {
85 #if MONO
86             if (m_use40bitSalt)
87                 throw new NotImplementedException ("UseSalt=true is not implemented on Mono yet");
88
89             return new RC2Transform (this, false, rgbKey, rgbIV);
90 #else
91             return _NewEncryptor(rgbKey, ModeValue, rgbIV, EffectiveKeySizeValue,
92                                  FeedbackSizeValue, CryptoAPITransformMode.Decrypt);
93 #endif
94         }
95
96         public override void GenerateKey () {
97             KeyValue = new byte[KeySizeValue/8];
98             Utils.StaticRandomNumberGenerator.GetBytes(KeyValue);
99         }
100
101         public override void GenerateIV () {
102             // block size is always 64 bits so IV is always 64 bits == 8 bytes
103             IVValue = new byte[8];
104             Utils.StaticRandomNumberGenerator.GetBytes(IVValue);
105         }
106
107         //
108         // private methods
109         //
110 #if !MONO
111         [System.Security.SecurityCritical]  // auto-generated
112         private ICryptoTransform _NewEncryptor (byte[] rgbKey, CipherMode mode, byte[] rgbIV,
113                                                 int effectiveKeySize, int feedbackSize, CryptoAPITransformMode encryptMode) {
114             int cArgs = 0;
115             int[] rgArgIds = new int[10];
116             Object[] rgArgValues = new Object[10];
117
118             // Check for bad values
119             // 1) we don't support OFB mode in RC2_CSP
120             if (mode == CipherMode.OFB)
121                 throw new CryptographicException(Environment.GetResourceString("Cryptography_CSP_OFBNotSupported"));
122             // 2) we only support CFB with a feedback size of 8 bits
123             if ((mode == CipherMode.CFB) && (feedbackSize != 8)) 
124                 throw new CryptographicException(Environment.GetResourceString("Cryptography_CSP_CFBSizeNotSupported"));
125
126             if (rgbKey == null) {
127                 rgbKey = new byte[KeySizeValue/8];
128                 Utils.StaticRandomNumberGenerator.GetBytes(rgbKey);
129             }
130
131             // Check the rgbKey size
132             int keySizeValue = rgbKey.Length * 8;
133             if (!ValidKeySize(keySizeValue))
134                 throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidKeySize"));
135
136             // Deal with effective key length questions
137             rgArgIds[cArgs] = Constants.KP_EFFECTIVE_KEYLEN;
138             if (EffectiveKeySizeValue == 0) {
139                 rgArgValues[cArgs] = keySizeValue;
140             } else {
141                 rgArgValues[cArgs] = effectiveKeySize;
142             }
143             cArgs += 1;
144
145             // Set the mode for the encryptor (defaults to CBC)
146             if (mode != CipherMode.CBC) {
147                 rgArgIds[cArgs] = Constants.KP_MODE;
148                 rgArgValues[cArgs] = mode;
149                 cArgs += 1;
150             }
151
152             // If not ECB mode -- pass in an IV
153             if (mode != CipherMode.ECB) {
154                 if (rgbIV == null) {
155                     rgbIV = new byte[8];
156                     Utils.StaticRandomNumberGenerator.GetBytes(rgbIV);
157                 }
158
159                 //
160                 // We truncate IV's that are longer than the block size to 8 bytes : this is
161                 // done to maintain backward compatibility with the behavior shipped in V1.x.
162                 // The call to set the IV in CryptoAPI will ignore any bytes after the first 8
163                 // bytes. We'll still reject IV's that are shorter than the block size though.
164                 //
165                 if (rgbIV.Length < 8)
166                     throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidIVSize"));
167
168                 rgArgIds[cArgs] = Constants.KP_IV;
169                 rgArgValues[cArgs] = rgbIV;
170                 cArgs += 1;
171             }
172
173             // If doing OFB or CFB, then we need to set the feed back loop size
174             if ((mode == CipherMode.OFB) || (mode == CipherMode.CFB)) {
175                 rgArgIds[cArgs] = Constants.KP_MODE_BITS;
176                 rgArgValues[cArgs] = feedbackSize;
177                 cArgs += 1;
178             }
179
180             if (!Utils.HasAlgorithm(Constants.CALG_RC2, keySizeValue))
181                 throw new CryptographicException(Environment.GetResourceString("Cryptography_CSP_AlgKeySizeNotAvailable", keySizeValue));
182
183             //  Create the encryptor/decryptor object
184             return new CryptoAPITransform(Constants.CALG_RC2, cArgs, rgArgIds, 
185                                           rgArgValues, rgbKey, PaddingValue, 
186                                           mode, BlockSizeValue, feedbackSize, m_use40bitSalt,
187                                           encryptMode);
188         }
189 #endif
190     }
191 }