SymmetricKeyWrap.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. //
  2. // SymmetricKeyWrap.cs - Implements symmetric key wrap algorithms
  3. //
  4. // Author:
  5. // Tim Coleman ([email protected])
  6. //
  7. // Copyright (C) Tim Coleman, 2004
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. #if NET_2_0
  30. using System.IO;
  31. using System.Security.Cryptography;
  32. namespace System.Security.Cryptography.Xml {
  33. internal class SymmetricKeyWrap {
  34. public SymmetricKeyWrap ()
  35. {
  36. }
  37. public static byte[] AESKeyWrapEncrypt (byte[] rgbKey, byte[] rgbWrappedKeyData)
  38. {
  39. SymmetricAlgorithm symAlg = SymmetricAlgorithm.Create ("Rijndael");
  40. // Apparently no one felt the need to document that this requires Electronic Codebook mode.
  41. symAlg.Mode = CipherMode.ECB;
  42. // This was also not documented anywhere.
  43. symAlg.IV = new byte [16] {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  44. ICryptoTransform transform = symAlg.CreateEncryptor (rgbKey, symAlg.IV);
  45. int N = rgbWrappedKeyData.Length / 8;
  46. byte[] A;
  47. byte[] B = new Byte [16];
  48. byte [] C = new byte [8 * (N + 1)];
  49. // 1. if N is 1:
  50. // B = AES(K)enc(0xA6A6A6A6A6A6A6A6|P(1))
  51. // C(0) = MSB(B)
  52. // C(1) = LSB(B)
  53. if (N == 1) {
  54. A = new byte [8] {0xa6, 0xa6, 0xa6, 0xa6, 0xa6, 0xa6, 0xa6, 0xa6};
  55. transform.TransformBlock (Concatenate (A, rgbWrappedKeyData), 0, 16, B, 0);
  56. Buffer.BlockCopy (MSB(B), 0, C, 0, 8);
  57. Buffer.BlockCopy (LSB(B), 0, C, 8, 8);
  58. } else {
  59. // if N > 1, perform the following steps:
  60. // 2. Initialize variables:
  61. // Set A to 0xA6A6A6A6A6A6A6A6
  62. // For i = 1 to N,
  63. // R(i) = P(i)
  64. A = new byte [8] {0xa6, 0xa6, 0xa6, 0xa6, 0xa6, 0xa6, 0xa6, 0xa6};
  65. byte[][] R = new byte [N + 1][];
  66. for (int i = 1; i <= N; i += 1) {
  67. R [i] = new byte [8];
  68. Buffer.BlockCopy (rgbWrappedKeyData, 8 * (i - 1), R [i], 0, 8);
  69. }
  70. // 3. Calculate intermediate values:
  71. // For j = 0 to 5
  72. // For i = 1 to N
  73. // t = i + j * N
  74. // B = AES(K)enc(A|R(i))
  75. // A = XOR(t, MSB(B))
  76. // R(i) = LSB(B)
  77. for (int j = 0; j <= 5; j += 1) {
  78. for (int i = 1; i <= N; i += 1) {
  79. transform.TransformBlock (Concatenate (A, R [i]), 0, 16, B, 0);
  80. // Yawn. It was nice of those at NIST to document how exactly we should XOR
  81. // an integer value with a byte array. Not.
  82. byte[] T = BitConverter.GetBytes ((long) (N * j + i));
  83. // This is nice.
  84. if (BitConverter.IsLittleEndian)
  85. Array.Reverse (T);
  86. A = Xor (T, MSB(B));
  87. R [i] = LSB (B);
  88. }
  89. }
  90. // 4. Output the results:
  91. // Set C(0) = A
  92. // For i = 1 to N
  93. // C(i) = R(i)
  94. Buffer.BlockCopy (A, 0, C, 0, 8);
  95. for (int i = 1; i <= N; i += 1)
  96. Buffer.BlockCopy (R [i], 0, C, 8 * i, 8);
  97. }
  98. return C;
  99. }
  100. public static byte[] AESKeyWrapDecrypt (byte[] rgbKey, byte[] rgbEncryptedWrappedKeyData)
  101. {
  102. SymmetricAlgorithm symAlg = SymmetricAlgorithm.Create ("Rijndael");
  103. symAlg.Mode = CipherMode.ECB;
  104. symAlg.Key = rgbKey;
  105. int N = ( rgbEncryptedWrappedKeyData.Length / 8 ) - 1;
  106. // From RFC 3394 - Advanced Encryption Standard (AES) Key Wrap Algorithm
  107. //
  108. // Inputs: Ciphertext, (n+1) 64-bit values (C0, C1, ..., Cn), and Key, K (the KEK)
  109. // Outputs: Plaintext, n 64-bit values (P1, P2, ..., Pn)
  110. //
  111. // 1. Initialize variables.
  112. // Set A = C[0]
  113. byte[] A = new byte [8];
  114. Buffer.BlockCopy (rgbEncryptedWrappedKeyData, 0, A, 0, 8);
  115. // For i = 1 to n
  116. // R[i] = C[i]
  117. byte[] R = new byte [N * 8];
  118. Buffer.BlockCopy (rgbEncryptedWrappedKeyData, 8, R, 0, rgbEncryptedWrappedKeyData.Length - 8);
  119. // 2. Compute intermediate values.
  120. // For j = 5 to 0
  121. // For i = n to 1
  122. // B = AES-1(K, (A^t) | R[i]) where t = n*j+i
  123. // A = MSB (64,B)
  124. // R[i] = LSB (64,B)
  125. ICryptoTransform transform = symAlg.CreateDecryptor ();
  126. for (int j = 5; j >= 0; j -= 1) {
  127. for (int i = N; i >= 1; i -= 1) {
  128. byte[] T = BitConverter.GetBytes ((long) N * j + i);
  129. if (BitConverter.IsLittleEndian)
  130. Array.Reverse (T);
  131. byte[] B = new Byte [16];
  132. byte[] r = new Byte [8];
  133. Buffer.BlockCopy (R, 8 * (i - 1), r, 0, 8);
  134. byte[] ciphertext = Concatenate (Xor (A, T), r);
  135. transform.TransformBlock (ciphertext, 0, 16, B, 0);
  136. A = MSB (B);
  137. Buffer.BlockCopy (LSB (B), 0, R, 8 * (i - 1), 8);
  138. }
  139. }
  140. // 3. Output results
  141. // If A is an appropriate initial value
  142. // Then
  143. // For i = 1 to n
  144. // P[i] = R[i]
  145. // Else
  146. // Return an error
  147. return R;
  148. }
  149. public static byte[] TripleDESKeyWrapEncrypt (byte[] rgbKey, byte[] rgbWrappedKeyData)
  150. {
  151. SymmetricAlgorithm symAlg = SymmetricAlgorithm.Create ("TripleDES");
  152. // Algorithm from http://www.w3.org/TR/xmlenc-core/#sec-Alg-SymmetricKeyWrap
  153. // The following algorithm wraps (encrypts) a key (the wrapped key, WK) under a TRIPLEDES
  154. // key-encryption-key (KEK) as adopted from [CMS-Algorithms].
  155. // 1. Represent the key being wrapped as an octet sequence. If it is a TRIPLEDES key,
  156. // this is 24 octets (192 bits) with odd parity bit as the bottom bit of each octet.
  157. // rgbWrappedKeyData is the key being wrapped.
  158. // 2. Compute the CMS key checksum (Section 5.6.1) call this CKS.
  159. byte[] cks = ComputeCMSKeyChecksum (rgbWrappedKeyData);
  160. // 3. Let WKCKS = WK || CKS, where || is concatenation.
  161. byte[] wkcks = Concatenate (rgbWrappedKeyData, cks);
  162. // 4. Generate 8 random octets and call this IV.
  163. symAlg.GenerateIV ();
  164. // 5. Encrypt WKCKS in CBC mode using KEK as the key and IV as the initialization vector.
  165. // Call the results TEMP1.
  166. symAlg.Mode = CipherMode.CBC;
  167. symAlg.Padding = PaddingMode.None;
  168. symAlg.Key = rgbKey;
  169. byte[] temp1 = Transform (wkcks, symAlg.CreateEncryptor ());
  170. // 6. Let TEMP2 = IV || TEMP1.
  171. byte[] temp2 = Concatenate (symAlg.IV, temp1);
  172. // 7. Reverse the order of the octets in TEMP2 and call the result TEMP3.
  173. Array.Reverse (temp2); // TEMP3 is TEMP2
  174. // 8. Encrypt TEMP3 in CBC mode using the KEK and an initialization vector of 0x4adda22c79e82105.
  175. // The resulting cipher text is the desired result. It is 40 octets long if a 168 bit key
  176. // is being wrapped.
  177. symAlg.IV = new Byte [8] {0x4a, 0xdd, 0xa2, 0x2c, 0x79, 0xe8, 0x21, 0x05};
  178. byte[] rtnval = Transform (temp2, symAlg.CreateEncryptor ());
  179. return rtnval;
  180. }
  181. public static byte[] TripleDESKeyWrapDecrypt (byte[] rgbKey, byte[] rgbEncryptedWrappedKeyData)
  182. {
  183. SymmetricAlgorithm symAlg = SymmetricAlgorithm.Create ("TripleDES");
  184. // Algorithm from http://www.w3.org/TR/xmlenc-core/#sec-Alg-SymmetricKeyWrap
  185. // The following algorithm unwraps (decrypts) a key as adopted from [CMS-Algorithms].
  186. // 1. Check the length of the cipher text is reasonable given the key type. It must be
  187. // 40 bytes for a 168 bit key and either 32, 40, or 48 bytes for a 128, 192, or 256 bit
  188. // key. If the length is not supported or inconsistent with the algorithm for which the
  189. // key is intended, return error.
  190. // 2. Decrypt the cipher text with TRIPLEDES in CBC mode using the KEK and an initialization
  191. // vector (IV) of 0x4adda22c79e82105. Call the output TEMP3.
  192. symAlg.Mode = CipherMode.CBC;
  193. symAlg.Padding = PaddingMode.None;
  194. symAlg.Key = rgbKey;
  195. symAlg.IV = new Byte [8] {0x4a, 0xdd, 0xa2, 0x2c, 0x79, 0xe8, 0x21, 0x05};
  196. byte[] temp3 = Transform (rgbEncryptedWrappedKeyData, symAlg.CreateDecryptor ());
  197. // 3. Reverse the order of the octets in TEMP3 and call the result TEMP2.
  198. Array.Reverse (temp3); // TEMP2 is TEMP3.
  199. // 4. Decompose TEMP2 into IV, the first 8 octets, and TEMP1, the remaining octets.
  200. byte[] temp1 = new Byte [temp3.Length - 8];
  201. byte[] iv = new Byte [8];
  202. Buffer.BlockCopy (temp3, 0, iv, 0, 8);
  203. Buffer.BlockCopy (temp3, 8, temp1, 0, temp1.Length);
  204. // 5. Decrypt TEMP1 using TRIPLEDES in CBC mode using the KEK and the IV found in the previous step.
  205. // Call the result WKCKS.
  206. symAlg.IV = iv;
  207. byte[] wkcks = Transform (temp1, symAlg.CreateDecryptor ());
  208. // 6. Decompose WKCKS. CKS is the last 8 octets and WK, the wrapped key, are those octets before
  209. // the CKS.
  210. byte[] cks = new byte [8];
  211. byte[] wk = new byte [wkcks.Length - 8];
  212. Buffer.BlockCopy (wkcks, 0, wk, 0, wk.Length);
  213. Buffer.BlockCopy (wkcks, wk.Length, cks, 0, 8);
  214. // 7. Calculate the CMS key checksum over the WK and compare with the CKS extracted in the above
  215. // step. If they are not equal, return error.
  216. // 8. WK is the wrapped key, now extracted for use in data decryption.
  217. return wk;
  218. }
  219. private static byte[] Transform (byte[] data, ICryptoTransform t)
  220. {
  221. MemoryStream output = new MemoryStream ();
  222. CryptoStream crypto = new CryptoStream (output, t, CryptoStreamMode.Write);
  223. crypto.Write (data, 0, data.Length);
  224. crypto.FlushFinalBlock ();
  225. byte[] result = output.ToArray ();
  226. output.Close ();
  227. crypto.Close ();
  228. return result;
  229. }
  230. private static byte[] ComputeCMSKeyChecksum (byte[] data)
  231. {
  232. byte[] hash = HashAlgorithm.Create ("SHA1").ComputeHash (data);
  233. byte[] output = new byte [8];
  234. Buffer.BlockCopy (hash, 0, output, 0, 8);
  235. return output;
  236. }
  237. private static byte[] Concatenate (byte[] buf1, byte[] buf2)
  238. {
  239. byte[] output = new byte [buf1.Length + buf2.Length];
  240. Buffer.BlockCopy (buf1, 0, output, 0, buf1.Length);
  241. Buffer.BlockCopy (buf2, 0, output, buf1.Length, buf2.Length);
  242. return output;
  243. }
  244. private static byte[] MSB (byte[] input)
  245. {
  246. return MSB (input, 8);
  247. }
  248. private static byte[] MSB (byte[] input, int bytes)
  249. {
  250. byte[] output = new byte [bytes];
  251. Buffer.BlockCopy (input, 0, output, 0, bytes);
  252. return output;
  253. }
  254. private static byte[] LSB (byte[] input)
  255. {
  256. return LSB (input, 8);
  257. }
  258. private static byte[] LSB (byte[] input, int bytes)
  259. {
  260. byte[] output = new byte [bytes];
  261. Buffer.BlockCopy (input, bytes, output, 0, bytes);
  262. return output;
  263. }
  264. private static byte[] Xor (byte[] x, byte[] y)
  265. {
  266. // This should *not* happen.
  267. if (x.Length != y.Length)
  268. throw new CryptographicException ("Error performing Xor: arrays different length.");
  269. byte[] output = new byte [x.Length];
  270. for (int i = 0; i < x.Length; i += 1)
  271. output [i] = (byte) (x [i] ^ y [i]);
  272. return output;
  273. }
  274. /* private static byte[] Xor (byte[] x, int n)
  275. {
  276. byte[] output = new Byte [x.Length];
  277. for (int i = 0; i < x.Length; i += 1)
  278. output [i] = (byte) ((int) x [i] ^ n);
  279. return output;
  280. }*/
  281. }
  282. }
  283. #endif