SymmetricAlgorithm.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. //
  2. // System.Security.Cryptography SymmetricAlgorithm Class implementation
  3. //
  4. // Authors:
  5. // Thomas Neidhart ([email protected])
  6. //
  7. using System;
  8. namespace System.Security.Cryptography {
  9. /// <summary>
  10. /// Abstract base class for all cryptographic symmetric algorithms.
  11. /// Available algorithms include:
  12. /// DES, RC2, Rijndael, TripleDES
  13. /// </summary>
  14. public abstract class SymmetricAlgorithm {
  15. protected int BlockSizeValue; // The block size of the cryptographic operation in bits.
  16. protected int FeedbackSizeValue; // The feedback size of the cryptographic operation in bits.
  17. protected byte[] IVValue; // The initialization vector ( IV) for the symmetric algorithm.
  18. protected int KeySizeValue; // The size of the secret key used by the symmetric algorithm in bits.
  19. protected byte[] KeyValue; // The secret key for the symmetric algorithm.
  20. protected KeySizes[] LegalBlockSizesValue; // Specifies the block sizes that are supported by the symmetric algorithm.
  21. protected KeySizes[] LegalKeySizesValue; // Specifies the key sizes that are supported by the symmetric algorithm.
  22. protected CipherMode ModeValue; // Represents the cipher mode used in the symmetric algorithm.
  23. protected PaddingMode PaddingValue; // Represents the padding mode used in the symmetric algorithm.
  24. /// <summary>
  25. /// Called from constructor of derived class.
  26. /// </summary>
  27. public SymmetricAlgorithm () {
  28. throw new CryptographicException();
  29. }
  30. /// <summary>
  31. /// Called from constructor of derived class.
  32. /// </summary>
  33. ~SymmetricAlgorithm () {
  34. if (KeyValue != null) {
  35. Array.Clear(KeyValue, 0, KeyValue.Length);
  36. KeyValue = null;
  37. }
  38. }
  39. /// <summary>
  40. /// Gets or sets the actual BlockSize
  41. /// </summary>
  42. public virtual int BlockSize {
  43. get {
  44. return this.BlockSizeValue;
  45. }
  46. set {
  47. if (IsLegalKeySize(this.LegalBlockSizesValue, value))
  48. this.BlockSizeValue = value;
  49. else
  50. throw new CryptographicException("block size not supported by algorithm");
  51. }
  52. }
  53. /// <summary>
  54. /// Gets or sets the actual FeedbackSize
  55. /// </summary>
  56. public virtual int FeedbackSize {
  57. get {
  58. return this.FeedbackSizeValue;
  59. }
  60. set {
  61. if (value > this.BlockSizeValue)
  62. throw new CryptographicException("feedback size larger than block size");
  63. else
  64. this.FeedbackSizeValue = value;
  65. }
  66. }
  67. /// <summary>
  68. /// Gets or sets the actual Initial Vector
  69. /// </summary>
  70. [MonoTODO]
  71. public virtual byte[] IV {
  72. get {
  73. if (this.IVValue == null)
  74. GenerateIV();
  75. return this.IVValue;
  76. }
  77. set {
  78. if (value == null)
  79. throw new ArgumentNullException("tried setting initial vector to null");
  80. // FIXME: dont know if to compare with block or key size
  81. if (value.Length != this.KeySizeValue)
  82. throw new CryptographicException("tried setting initial vector with illegal size");
  83. this.IVValue = new byte [value.Length];
  84. System.Array.Copy (value, 0, this.IVValue, 0, value.Length);
  85. }
  86. }
  87. /// <summary>
  88. /// Gets or sets the actual key
  89. /// </summary>
  90. public virtual byte[] Key {
  91. get {
  92. if (this.KeyValue == null)
  93. GenerateKey();
  94. return this.KeyValue;
  95. }
  96. set {
  97. if (value == null)
  98. throw new ArgumentNullException("tried setting key to null");
  99. if (!IsLegalKeySize(this.LegalKeySizesValue, value.Length))
  100. throw new CryptographicException("key size not supported by algorithm");
  101. this.KeySizeValue = value.Length;
  102. this.KeyValue = new byte [this.KeySizeValue];
  103. System.Array.Copy (value, 0, this.KeyValue, 0, this.KeySizeValue);
  104. }
  105. }
  106. /// <summary>
  107. /// Gets or sets the actual key size
  108. /// </summary>
  109. public virtual int KeySize {
  110. get {
  111. return this.KeySizeValue;
  112. }
  113. set {
  114. if (!IsLegalKeySize(this.LegalKeySizesValue, value))
  115. throw new CryptographicException("key size not supported by algorithm");
  116. this.KeyValue = null;
  117. this.KeySizeValue = value;
  118. }
  119. }
  120. /// <summary>
  121. /// Gets all legal block sizes
  122. /// </summary>
  123. public virtual KeySizes[] LegalBlockSizes {
  124. get {
  125. return this.LegalBlockSizesValue;
  126. }
  127. }
  128. /// <summary>
  129. /// Gets all legal key sizes
  130. /// </summary>
  131. public virtual KeySizes[] LegalKeySizes {
  132. get {
  133. return this.LegalKeySizesValue;
  134. }
  135. }
  136. /// <summary>
  137. /// Gets or sets the actual cipher mode
  138. /// </summary>
  139. public virtual CipherMode Mode {
  140. get {
  141. return this.ModeValue;
  142. }
  143. set {
  144. if (Enum.IsDefined(ModeValue.GetType(), value))
  145. this.ModeValue = value;
  146. else
  147. throw new CryptographicException("padding mode not available");
  148. }
  149. }
  150. /// <summary>
  151. /// Gets or sets the actual padding
  152. /// </summary>
  153. public virtual PaddingMode Padding {
  154. get {
  155. return this.PaddingValue;
  156. }
  157. set {
  158. if (Enum.IsDefined(PaddingValue.GetType(), value))
  159. this.PaddingValue = value;
  160. else
  161. throw new CryptographicException("padding mode not available");
  162. }
  163. }
  164. /// <summary>
  165. /// Gets an Decryptor transform object to work with a CryptoStream
  166. /// </summary>
  167. public virtual ICryptoTransform CreateDecryptor() {
  168. return CreateDecryptor(Key, IV);
  169. }
  170. /// <summary>
  171. /// Gets an Decryptor transform object to work with a CryptoStream
  172. /// </summary>
  173. public abstract ICryptoTransform CreateDecryptor(byte[] rgbKey, byte[] rgbIV);
  174. /// <summary>
  175. /// Gets an Encryptor transform object to work with a CryptoStream
  176. /// </summary>
  177. public virtual ICryptoTransform CreateEncryptor() {
  178. return CreateEncryptor(Key, IV);
  179. }
  180. /// <summary>
  181. /// Gets an Encryptor transform object to work with a CryptoStream
  182. /// </summary>
  183. public abstract ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[] rgbIV);
  184. /// <summary>
  185. /// used to generate an inital vector if none is specified
  186. /// </summary>
  187. public abstract void GenerateIV();
  188. /// </summary>
  189. /// used to generate a random key if none is specified
  190. /// </summary>
  191. public abstract void GenerateKey();
  192. internal bool IsLegalKeySize(KeySizes[] LegalKeys, int Size) {
  193. foreach (KeySizes LegalKeySize in LegalKeys) {
  194. for (int i=LegalKeySize.MinSize; i<=LegalKeySize.MaxSize; i+=LegalKeySize.SkipSize) {
  195. if (i == Size)
  196. return true;
  197. }
  198. }
  199. return false;
  200. }
  201. /// <summary>
  202. /// Checks wether the given keyLength is valid for the current algorithm
  203. /// </summary>
  204. /// <param name="bitLength">the given keyLength</param>
  205. public bool ValidKeySize(int bitLength) {
  206. return IsLegalKeySize(LegalKeySizesValue, bitLength);
  207. }
  208. /// <summary>
  209. /// Creates the default implementation of the default symmetric algorithm (RC2).
  210. /// </summary>
  211. public static SymmetricAlgorithm Create () {
  212. return Rijndael.Create();
  213. }
  214. /// <summary>
  215. /// Creates a specific implementation of the given symmetric algorithm.
  216. /// </summary>
  217. /// <param name="algName">the given algorithm</param>
  218. [MonoTODO]
  219. public static SymmetricAlgorithm Create (string algName) {
  220. // TODO: Use Reflection to create a new algorithm instance
  221. return null;
  222. }
  223. }
  224. }