3 // Copyright (c) Microsoft Corporation. All rights reserved.
6 // <OWNER>Microsoft</OWNER>
10 // RC2CryptoServiceProvider.cs
13 namespace System.Security.Cryptography {
15 using System.Globalization;
16 using System.Diagnostics.Contracts;
18 [System.Runtime.InteropServices.ComVisible(true)]
19 public sealed class RC2CryptoServiceProvider : RC2 {
20 private bool m_use40bitSalt = false;
22 private static KeySizes[] s_legalKeySizes = {
23 new KeySizes(40, 128, 8) // cryptoAPI implementation only goes up to 128
27 // public constructors
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"));
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;
42 // Since the CSP only supports a CFB feedback of 8, make that the default
43 FeedbackSizeValue = 8;
50 public override int EffectiveKeySize {
55 if (value != KeySizeValue)
56 throw new CryptographicUnexpectedOperationException(Environment.GetResourceString("Cryptography_RC2_EKSKS2"));
60 [System.Runtime.InteropServices.ComVisible(false)]
63 return m_use40bitSalt;
66 m_use40bitSalt = value;
70 [System.Security.SecuritySafeCritical] // auto-generated
71 public override ICryptoTransform CreateEncryptor (byte[] rgbKey, byte[] rgbIV) {
74 throw new NotImplementedException ("UseSalt=true is not implemented on Mono yet");
76 return new RC2Transform (this, true, rgbKey, rgbIV);
78 return _NewEncryptor(rgbKey, ModeValue, rgbIV, EffectiveKeySizeValue,
79 FeedbackSizeValue, CryptoAPITransformMode.Encrypt);
83 [System.Security.SecuritySafeCritical] // auto-generated
84 public override ICryptoTransform CreateDecryptor (byte[] rgbKey, byte[] rgbIV) {
87 throw new NotImplementedException ("UseSalt=true is not implemented on Mono yet");
89 return new RC2Transform (this, false, rgbKey, rgbIV);
91 return _NewEncryptor(rgbKey, ModeValue, rgbIV, EffectiveKeySizeValue,
92 FeedbackSizeValue, CryptoAPITransformMode.Decrypt);
96 public override void GenerateKey () {
97 KeyValue = new byte[KeySizeValue/8];
98 Utils.StaticRandomNumberGenerator.GetBytes(KeyValue);
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);
111 [System.Security.SecurityCritical] // auto-generated
112 private ICryptoTransform _NewEncryptor (byte[] rgbKey, CipherMode mode, byte[] rgbIV,
113 int effectiveKeySize, int feedbackSize, CryptoAPITransformMode encryptMode) {
115 int[] rgArgIds = new int[10];
116 Object[] rgArgValues = new Object[10];
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"));
126 if (rgbKey == null) {
127 rgbKey = new byte[KeySizeValue/8];
128 Utils.StaticRandomNumberGenerator.GetBytes(rgbKey);
131 // Check the rgbKey size
132 int keySizeValue = rgbKey.Length * 8;
133 if (!ValidKeySize(keySizeValue))
134 throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidKeySize"));
136 // Deal with effective key length questions
137 rgArgIds[cArgs] = Constants.KP_EFFECTIVE_KEYLEN;
138 if (EffectiveKeySizeValue == 0) {
139 rgArgValues[cArgs] = keySizeValue;
141 rgArgValues[cArgs] = effectiveKeySize;
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;
152 // If not ECB mode -- pass in an IV
153 if (mode != CipherMode.ECB) {
156 Utils.StaticRandomNumberGenerator.GetBytes(rgbIV);
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.
165 if (rgbIV.Length < 8)
166 throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidIVSize"));
168 rgArgIds[cArgs] = Constants.KP_IV;
169 rgArgValues[cArgs] = rgbIV;
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;
180 if (!Utils.HasAlgorithm(Constants.CALG_RC2, keySizeValue))
181 throw new CryptographicException(Environment.GetResourceString("Cryptography_CSP_AlgKeySizeNotAvailable", keySizeValue));
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,