SymmetricAlgorithm.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. public virtual byte[] IV {
  71. get {
  72. if (this.IVValue == null)
  73. GenerateIV();
  74. return this.IVValue;
  75. }
  76. set {
  77. if (value == null)
  78. throw new ArgumentNullException("tried setting initial vector to null");
  79. // FIXME: dont know if to compare with block or key size
  80. if (value.Length != this.KeySizeValue)
  81. throw new CryptographicException("tried setting initial vector with illegal size");
  82. this.IVValue = new byte [value.Length];
  83. System.Array.Copy (value, 0, this.IVValue, 0, value.Length);
  84. }
  85. }
  86. /// <summary>
  87. /// Gets or sets the actual key
  88. /// </summary>
  89. public virtual byte[] Key {
  90. get {
  91. if (this.KeyValue == null)
  92. GenerateKey();
  93. return this.KeyValue;
  94. }
  95. set {
  96. if (value == null)
  97. throw new ArgumentNullException("tried setting key to null");
  98. if (!IsLegalKeySize(this.LegalKeySizesValue, value.Length))
  99. throw new CryptographicException("key size not supported by algorithm");
  100. this.KeySizeValue = value.Length;
  101. this.KeyValue = new byte [this.KeySizeValue];
  102. System.Array.Copy (value, 0, this.KeyValue, 0, this.KeySizeValue);
  103. }
  104. }
  105. /// <summary>
  106. /// Gets or sets the actual key size
  107. /// </summary>
  108. public virtual int KeySize {
  109. get {
  110. return this.KeySizeValue;
  111. }
  112. set {
  113. if (!IsLegalKeySize(this.LegalKeySizesValue, value))
  114. throw new CryptographicException("key size not supported by algorithm");
  115. this.KeyValue = null;
  116. this.KeySizeValue = value;
  117. }
  118. }
  119. /// <summary>
  120. /// Gets all legal block sizes
  121. /// </summary>
  122. public virtual KeySizes[] LegalBlockSizes {
  123. get {
  124. return this.LegalBlockSizesValue;
  125. }
  126. }
  127. /// <summary>
  128. /// Gets all legal key sizes
  129. /// </summary>
  130. public virtual KeySizes[] LegalKeySizes {
  131. get {
  132. return this.LegalKeySizesValue;
  133. }
  134. }
  135. /// <summary>
  136. /// Gets or sets the actual cipher mode
  137. /// </summary>
  138. public virtual CipherMode Mode {
  139. get {
  140. return this.ModeValue;
  141. }
  142. set {
  143. if (Enum.IsDefined(ModeValue.GetType(), value))
  144. this.ModeValue = value;
  145. else
  146. throw new CryptographicException("padding mode not available");
  147. }
  148. }
  149. /// <summary>
  150. /// Gets or sets the actual padding
  151. /// </summary>
  152. public virtual PaddingMode Padding {
  153. get {
  154. return this.PaddingValue;
  155. }
  156. set {
  157. if (Enum.IsDefined(PaddingValue.GetType(), value))
  158. this.PaddingValue = value;
  159. else
  160. throw new CryptographicException("padding mode not available");
  161. }
  162. }
  163. /// <summary>
  164. /// Gets an Decryptor transform object to work with a CryptoStream
  165. /// </summary>
  166. public virtual ICryptoTransform CreateDecryptor() {
  167. return CreateDecryptor(Key, IV);
  168. }
  169. /// <summary>
  170. /// Gets an Decryptor transform object to work with a CryptoStream
  171. /// </summary>
  172. public abstract ICryptoTransform CreateDecryptor(byte[] rgbKey, byte[] rgbIV);
  173. /// <summary>
  174. /// Gets an Encryptor transform object to work with a CryptoStream
  175. /// </summary>
  176. public virtual ICryptoTransform CreateEncryptor() {
  177. return CreateEncryptor(Key, IV);
  178. }
  179. /// <summary>
  180. /// Gets an Encryptor transform object to work with a CryptoStream
  181. /// </summary>
  182. public abstract ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[] rgbIV);
  183. /// <summary>
  184. /// used to generate an inital vector if none is specified
  185. /// </summary>
  186. public abstract void GenerateIV();
  187. /// </summary>
  188. /// used to generate a random key if none is specified
  189. /// </summary>
  190. public abstract void GenerateKey();
  191. internal bool IsLegalKeySize(KeySizes[] LegalKeys, int Size) {
  192. foreach (KeySizes LegalKeySize in LegalKeys) {
  193. for (int i=LegalKeySize.MinSize; i<=LegalKeySize.MaxSize; i+=LegalKeySize.SkipSize) {
  194. if (i == Size)
  195. return true;
  196. }
  197. }
  198. return false;
  199. }
  200. /// <summary>
  201. /// Checks wether the given keyLength is valid for the current algorithm
  202. /// </summary>
  203. /// <param name="bitLength">the given keyLength</param>
  204. public bool ValidKeySize(int bitLength) {
  205. return IsLegalKeySize(LegalKeySizesValue, bitLength);
  206. }
  207. /// <summary>
  208. /// Creates the default implementation of the default symmetric algorithm (RC2).
  209. /// </summary>
  210. public static SymmetricAlgorithm Create () {
  211. return Rijndael.Create();
  212. }
  213. /// <summary>
  214. /// Creates a specific implementation of the given symmetric algorithm.
  215. /// </summary>
  216. /// <param name="algName">the given algorithm</param>
  217. public static SymmetricAlgorithm Create (string algName) {
  218. // TODO: Use Reflection to create a new algorithm instance
  219. return null;
  220. }
  221. }
  222. }