3 // Copyright (c) Microsoft Corporation. All rights reserved.
6 // <OWNER>[....]</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) {
73 return new RC2Transform (this, true, rgbKey, rgbIV);
75 return _NewEncryptor(rgbKey, ModeValue, rgbIV, EffectiveKeySizeValue,
76 FeedbackSizeValue, CryptoAPITransformMode.Encrypt);
80 [System.Security.SecuritySafeCritical] // auto-generated
81 public override ICryptoTransform CreateDecryptor (byte[] rgbKey, byte[] rgbIV) {
83 return new RC2Transform (this, false, rgbKey, rgbIV);
85 return _NewEncryptor(rgbKey, ModeValue, rgbIV, EffectiveKeySizeValue,
86 FeedbackSizeValue, CryptoAPITransformMode.Decrypt);
90 public override void GenerateKey () {
91 KeyValue = new byte[KeySizeValue/8];
92 Utils.StaticRandomNumberGenerator.GetBytes(KeyValue);
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);
105 [System.Security.SecurityCritical] // auto-generated
106 private ICryptoTransform _NewEncryptor (byte[] rgbKey, CipherMode mode, byte[] rgbIV,
107 int effectiveKeySize, int feedbackSize, CryptoAPITransformMode encryptMode) {
109 int[] rgArgIds = new int[10];
110 Object[] rgArgValues = new Object[10];
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"));
120 if (rgbKey == null) {
121 rgbKey = new byte[KeySizeValue/8];
122 Utils.StaticRandomNumberGenerator.GetBytes(rgbKey);
125 // Check the rgbKey size
126 int keySizeValue = rgbKey.Length * 8;
127 if (!ValidKeySize(keySizeValue))
128 throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidKeySize"));
130 // Deal with effective key length questions
131 rgArgIds[cArgs] = Constants.KP_EFFECTIVE_KEYLEN;
132 if (EffectiveKeySizeValue == 0) {
133 rgArgValues[cArgs] = keySizeValue;
135 rgArgValues[cArgs] = effectiveKeySize;
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;
146 // If not ECB mode -- pass in an IV
147 if (mode != CipherMode.ECB) {
150 Utils.StaticRandomNumberGenerator.GetBytes(rgbIV);
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.
159 if (rgbIV.Length < 8)
160 throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidIVSize"));
162 rgArgIds[cArgs] = Constants.KP_IV;
163 rgArgValues[cArgs] = rgbIV;
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;
174 if (!Utils.HasAlgorithm(Constants.CALG_RC2, keySizeValue))
175 throw new CryptographicException(Environment.GetResourceString("Cryptography_CSP_AlgKeySizeNotAvailable", keySizeValue));
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,