SymmetricAlgorithm.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. //
  2. // System.Security.Cryptography SymmetricAlgorithm Class implementation
  3. //
  4. // Authors:
  5. // Thomas Neidhart ([email protected])
  6. // Sebastien Pouliot <[email protected]>
  7. //
  8. // Portions (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
  9. // Copyright (C) 2004-2006 Novell, Inc (http://www.novell.com)
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System.Globalization;
  31. using System.Runtime.InteropServices;
  32. using Mono.Security.Cryptography;
  33. namespace System.Security.Cryptography {
  34. #if NET_2_0
  35. [ComVisible (true)]
  36. #endif
  37. public abstract class SymmetricAlgorithm : IDisposable {
  38. protected int BlockSizeValue;
  39. protected byte[] IVValue;
  40. protected int KeySizeValue;
  41. protected byte[] KeyValue;
  42. protected KeySizes[] LegalBlockSizesValue;
  43. protected KeySizes[] LegalKeySizesValue;
  44. #if NET_2_1 && !MONOTOUCH
  45. // Silverlight 2.0 only supports CBC
  46. internal int FeedbackSizeValue;
  47. internal CipherMode ModeValue;
  48. internal PaddingMode PaddingValue;
  49. #else
  50. protected int FeedbackSizeValue;
  51. protected CipherMode ModeValue;
  52. protected PaddingMode PaddingValue;
  53. #endif
  54. private bool m_disposed;
  55. #if NET_2_0
  56. protected SymmetricAlgorithm ()
  57. #else
  58. public SymmetricAlgorithm ()
  59. #endif
  60. {
  61. ModeValue = CipherMode.CBC;
  62. PaddingValue = PaddingMode.PKCS7;
  63. m_disposed = false;
  64. }
  65. #if NET_2_1 && !MONOTOUCH
  66. // No Finalizer or IDisposable.Dispose in Silverlight 2.0
  67. // Documentation makes it "clear" that Clear MUST BE CALLED to zero out sensitive information
  68. #else
  69. ~SymmetricAlgorithm ()
  70. {
  71. Dispose (false);
  72. }
  73. #endif
  74. void IDisposable.Dispose ()
  75. {
  76. Dispose (true);
  77. GC.SuppressFinalize (this); // Finalization is now unnecessary
  78. }
  79. public void Clear()
  80. {
  81. Dispose (true);
  82. }
  83. protected virtual void Dispose (bool disposing)
  84. {
  85. if (!m_disposed) {
  86. // always zeroize keys
  87. if (KeyValue != null) {
  88. // Zeroize the secret key and free
  89. Array.Clear (KeyValue, 0, KeyValue.Length);
  90. KeyValue = null;
  91. }
  92. // dispose unmanaged managed objects
  93. if (disposing) {
  94. // dispose managed objects
  95. }
  96. m_disposed = true;
  97. }
  98. }
  99. public virtual int BlockSize {
  100. get { return this.BlockSizeValue; }
  101. set {
  102. if (!KeySizes.IsLegalKeySize (this.LegalBlockSizesValue, value)) {
  103. throw new CryptographicException (
  104. Locale.GetText ("block size not supported by algorithm"));
  105. }
  106. // re-setting the same BlockSize *doesn't* regenerate the IV
  107. if (BlockSizeValue != value) {
  108. BlockSizeValue = value;
  109. IVValue = null;
  110. }
  111. }
  112. }
  113. public virtual int FeedbackSize {
  114. get { return this.FeedbackSizeValue; }
  115. set {
  116. #if NET_2_0
  117. if ((value <= 0) || (value > this.BlockSizeValue)) {
  118. #else
  119. if (value > this.BlockSizeValue) {
  120. #endif
  121. throw new CryptographicException (
  122. Locale.GetText ("feedback size larger than block size"));
  123. }
  124. this.FeedbackSizeValue = value;
  125. }
  126. }
  127. public virtual byte[] IV {
  128. get {
  129. if (this.IVValue == null)
  130. GenerateIV();
  131. return (byte[]) this.IVValue.Clone ();
  132. }
  133. set {
  134. if (value == null)
  135. throw new ArgumentNullException ("IV");
  136. #if NET_2_0
  137. // 2.0 is stricter for IV length - which is bad for IV-less stream ciphers like RC4
  138. if ((value.Length << 3) != this.BlockSizeValue) {
  139. throw new CryptographicException (
  140. Locale.GetText ("IV length is different than block size"));
  141. }
  142. #else
  143. if ((value.Length << 3) > this.BlockSizeValue) {
  144. throw new CryptographicException (
  145. Locale.GetText ("IV length cannot be larger than block size"));
  146. }
  147. #endif
  148. this.IVValue = (byte[]) value.Clone ();
  149. }
  150. }
  151. public virtual byte[] Key {
  152. get {
  153. if (this.KeyValue == null)
  154. GenerateKey();
  155. return (byte[]) this.KeyValue.Clone ();
  156. }
  157. set {
  158. if (value == null)
  159. throw new ArgumentNullException ("Key");
  160. int length = (value.Length << 3);
  161. if (!KeySizes.IsLegalKeySize (this.LegalKeySizesValue, length)) {
  162. throw new CryptographicException (
  163. Locale.GetText ("Key size not supported by algorithm"));
  164. }
  165. this.KeySizeValue = length;
  166. this.KeyValue = (byte[]) value.Clone ();
  167. }
  168. }
  169. public virtual int KeySize {
  170. get { return this.KeySizeValue; }
  171. set {
  172. if (!KeySizes.IsLegalKeySize (this.LegalKeySizesValue, value)) {
  173. throw new CryptographicException (
  174. Locale.GetText ("Key size not supported by algorithm"));
  175. }
  176. // re-setting the same KeySize *does* regenerate the key
  177. KeySizeValue = value;
  178. KeyValue = null;
  179. }
  180. }
  181. public virtual KeySizes[] LegalBlockSizes {
  182. get { return this.LegalBlockSizesValue; }
  183. }
  184. public virtual KeySizes[] LegalKeySizes {
  185. get { return this.LegalKeySizesValue; }
  186. }
  187. public virtual CipherMode Mode {
  188. get { return this.ModeValue; }
  189. set {
  190. if (!Enum.IsDefined (ModeValue.GetType (), value)) {
  191. throw new CryptographicException (
  192. Locale.GetText ("Cipher mode not available"));
  193. }
  194. this.ModeValue = value;
  195. }
  196. }
  197. public virtual PaddingMode Padding {
  198. get { return this.PaddingValue; }
  199. set {
  200. if (!Enum.IsDefined (PaddingValue.GetType (), value)) {
  201. throw new CryptographicException (
  202. Locale.GetText ("Padding mode not available"));
  203. }
  204. this.PaddingValue = value;
  205. }
  206. }
  207. public virtual ICryptoTransform CreateDecryptor ()
  208. {
  209. return CreateDecryptor (Key, IV);
  210. }
  211. public abstract ICryptoTransform CreateDecryptor (byte[] rgbKey, byte[] rgbIV);
  212. public virtual ICryptoTransform CreateEncryptor()
  213. {
  214. return CreateEncryptor (Key, IV);
  215. }
  216. public abstract ICryptoTransform CreateEncryptor (byte[] rgbKey, byte[] rgbIV);
  217. public abstract void GenerateIV ();
  218. public abstract void GenerateKey ();
  219. public bool ValidKeySize (int bitLength)
  220. {
  221. return KeySizes.IsLegalKeySize (LegalKeySizesValue, bitLength);
  222. }
  223. // LAMESPEC: Default is Rijndael - not TripleDES
  224. public static SymmetricAlgorithm Create ()
  225. {
  226. return Create ("System.Security.Cryptography.SymmetricAlgorithm");
  227. }
  228. public static SymmetricAlgorithm Create (string algName)
  229. {
  230. return (SymmetricAlgorithm) CryptoConfig.CreateFromName (algName);
  231. }
  232. }
  233. }