2 // SymmetricKeyWrap.cs - Implements symmetric key wrap algorithms
5 // Tim Coleman (tim@timcoleman.com)
7 // Copyright (C) Tim Coleman, 2004
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
33 using System.Security.Cryptography;
35 namespace System.Security.Cryptography.Xml {
37 internal class SymmetricKeyWrap {
39 public SymmetricKeyWrap ()
43 public static byte[] AESKeyWrapEncrypt (byte[] rgbKey, byte[] rgbWrappedKeyData)
45 SymmetricAlgorithm symAlg = SymmetricAlgorithm.Create ("Rijndael");
47 // Apparently no one felt the need to document that this requires Electronic Codebook mode.
48 symAlg.Mode = CipherMode.ECB;
50 // This was also not documented anywhere.
51 symAlg.IV = new byte [16] {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
53 ICryptoTransform transform = symAlg.CreateEncryptor (rgbKey, symAlg.IV);
55 int N = rgbWrappedKeyData.Length / 8;
57 byte[] B = new Byte [16];
58 byte [] C = new byte [8 * (N + 1)];
61 // B = AES(K)enc(0xA6A6A6A6A6A6A6A6|P(1))
65 A = new byte [8] {0xa6, 0xa6, 0xa6, 0xa6, 0xa6, 0xa6, 0xa6, 0xa6};
66 transform.TransformBlock (Concatenate (A, rgbWrappedKeyData), 0, 16, B, 0);
67 Buffer.BlockCopy (MSB(B), 0, C, 0, 8);
68 Buffer.BlockCopy (LSB(B), 0, C, 8, 8);
70 // if N > 1, perform the following steps:
71 // 2. Initialize variables:
72 // Set A to 0xA6A6A6A6A6A6A6A6
75 A = new byte [8] {0xa6, 0xa6, 0xa6, 0xa6, 0xa6, 0xa6, 0xa6, 0xa6};
77 byte[][] R = new byte [N + 1][];
78 for (int i = 1; i <= N; i += 1) {
80 Buffer.BlockCopy (rgbWrappedKeyData, 8 * (i - 1), R [i], 0, 8);
83 // 3. Calculate intermediate values:
87 // B = AES(K)enc(A|R(i))
91 for (int j = 0; j <= 5; j += 1) {
92 for (int i = 1; i <= N; i += 1) {
93 transform.TransformBlock (Concatenate (A, R [i]), 0, 16, B, 0);
95 // Yawn. It was nice of those at NIST to document how exactly we should XOR
96 // an integer value with a byte array. Not.
97 byte[] T = BitConverter.GetBytes ((long) (N * j + i));
100 if (BitConverter.IsLittleEndian)
108 // 4. Output the results:
112 Buffer.BlockCopy (A, 0, C, 0, 8);
113 for (int i = 1; i <= N; i += 1)
114 Buffer.BlockCopy (R [i], 0, C, 8 * i, 8);
119 public static byte[] AESKeyWrapDecrypt (byte[] rgbKey, byte[] rgbEncryptedWrappedKeyData)
121 SymmetricAlgorithm symAlg = SymmetricAlgorithm.Create ("Rijndael");
122 symAlg.Mode = CipherMode.ECB;
125 int N = ( rgbEncryptedWrappedKeyData.Length / 8 ) - 1;
127 // From RFC 3394 - Advanced Encryption Standard (AES) Key Wrap Algorithm
129 // Inputs: Ciphertext, (n+1) 64-bit values (C0, C1, ..., Cn), and Key, K (the KEK)
130 // Outputs: Plaintext, n 64-bit values (P1, P2, ..., Pn)
132 // 1. Initialize variables.
135 byte[] A = new byte [8];
136 Buffer.BlockCopy (rgbEncryptedWrappedKeyData, 0, A, 0, 8);
141 byte[] R = new byte [N * 8];
142 Buffer.BlockCopy (rgbEncryptedWrappedKeyData, 8, R, 0, rgbEncryptedWrappedKeyData.Length - 8);
144 // 2. Compute intermediate values.
147 // B = AES-1(K, (A^t) | R[i]) where t = n*j+i
151 ICryptoTransform transform = symAlg.CreateDecryptor ();
153 for (int j = 5; j >= 0; j -= 1) {
154 for (int i = N; i >= 1; i -= 1) {
155 byte[] T = BitConverter.GetBytes ((long) N * j + i);
156 if (BitConverter.IsLittleEndian)
159 byte[] B = new Byte [16];
160 byte[] r = new Byte [8];
161 Buffer.BlockCopy (R, 8 * (i - 1), r, 0, 8);
162 byte[] ciphertext = Concatenate (Xor (A, T), r);
163 transform.TransformBlock (ciphertext, 0, 16, B, 0);
165 Buffer.BlockCopy (LSB (B), 0, R, 8 * (i - 1), 8);
170 // If A is an appropriate initial value
180 public static byte[] TripleDESKeyWrapEncrypt (byte[] rgbKey, byte[] rgbWrappedKeyData)
182 SymmetricAlgorithm symAlg = SymmetricAlgorithm.Create ("TripleDES");
184 // Algorithm from http://www.w3.org/TR/xmlenc-core/#sec-Alg-SymmetricKeyWrap
185 // The following algorithm wraps (encrypts) a key (the wrapped key, WK) under a TRIPLEDES
186 // key-encryption-key (KEK) as adopted from [CMS-Algorithms].
188 // 1. Represent the key being wrapped as an octet sequence. If it is a TRIPLEDES key,
189 // this is 24 octets (192 bits) with odd parity bit as the bottom bit of each octet.
191 // rgbWrappedKeyData is the key being wrapped.
193 // 2. Compute the CMS key checksum (Section 5.6.1) call this CKS.
195 byte[] cks = ComputeCMSKeyChecksum (rgbWrappedKeyData);
197 // 3. Let WKCKS = WK || CKS, where || is concatenation.
199 byte[] wkcks = Concatenate (rgbWrappedKeyData, cks);
201 // 4. Generate 8 random octets and call this IV.
202 symAlg.GenerateIV ();
204 // 5. Encrypt WKCKS in CBC mode using KEK as the key and IV as the initialization vector.
205 // Call the results TEMP1.
207 symAlg.Mode = CipherMode.CBC;
208 symAlg.Padding = PaddingMode.None;
210 byte[] temp1 = Transform (wkcks, symAlg.CreateEncryptor ());
212 // 6. Let TEMP2 = IV || TEMP1.
214 byte[] temp2 = Concatenate (symAlg.IV, temp1);
216 // 7. Reverse the order of the octets in TEMP2 and call the result TEMP3.
218 Array.Reverse (temp2); // TEMP3 is TEMP2
220 // 8. Encrypt TEMP3 in CBC mode using the KEK and an initialization vector of 0x4adda22c79e82105.
221 // The resulting cipher text is the desired result. It is 40 octets long if a 168 bit key
224 symAlg.IV = new Byte [8] {0x4a, 0xdd, 0xa2, 0x2c, 0x79, 0xe8, 0x21, 0x05};
226 byte[] rtnval = Transform (temp2, symAlg.CreateEncryptor ());
231 public static byte[] TripleDESKeyWrapDecrypt (byte[] rgbKey, byte[] rgbEncryptedWrappedKeyData)
233 SymmetricAlgorithm symAlg = SymmetricAlgorithm.Create ("TripleDES");
235 // Algorithm from http://www.w3.org/TR/xmlenc-core/#sec-Alg-SymmetricKeyWrap
236 // The following algorithm unwraps (decrypts) a key as adopted from [CMS-Algorithms].
238 // 1. Check the length of the cipher text is reasonable given the key type. It must be
239 // 40 bytes for a 168 bit key and either 32, 40, or 48 bytes for a 128, 192, or 256 bit
240 // key. If the length is not supported or inconsistent with the algorithm for which the
241 // key is intended, return error.
243 // 2. Decrypt the cipher text with TRIPLEDES in CBC mode using the KEK and an initialization
244 // vector (IV) of 0x4adda22c79e82105. Call the output TEMP3.
246 symAlg.Mode = CipherMode.CBC;
247 symAlg.Padding = PaddingMode.None;
249 symAlg.IV = new Byte [8] {0x4a, 0xdd, 0xa2, 0x2c, 0x79, 0xe8, 0x21, 0x05};
251 byte[] temp3 = Transform (rgbEncryptedWrappedKeyData, symAlg.CreateDecryptor ());
253 // 3. Reverse the order of the octets in TEMP3 and call the result TEMP2.
255 Array.Reverse (temp3); // TEMP2 is TEMP3.
257 // 4. Decompose TEMP2 into IV, the first 8 octets, and TEMP1, the remaining octets.
259 byte[] temp1 = new Byte [temp3.Length - 8];
260 byte[] iv = new Byte [8];
262 Buffer.BlockCopy (temp3, 0, iv, 0, 8);
263 Buffer.BlockCopy (temp3, 8, temp1, 0, temp1.Length);
265 // 5. Decrypt TEMP1 using TRIPLEDES in CBC mode using the KEK and the IV found in the previous step.
266 // Call the result WKCKS.
269 byte[] wkcks = Transform (temp1, symAlg.CreateDecryptor ());
271 // 6. Decompose WKCKS. CKS is the last 8 octets and WK, the wrapped key, are those octets before
274 byte[] cks = new byte [8];
275 byte[] wk = new byte [wkcks.Length - 8];
277 Buffer.BlockCopy (wkcks, 0, wk, 0, wk.Length);
278 Buffer.BlockCopy (wkcks, wk.Length, cks, 0, 8);
280 // 7. Calculate the CMS key checksum over the WK and compare with the CKS extracted in the above
281 // step. If they are not equal, return error.
283 // 8. WK is the wrapped key, now extracted for use in data decryption.
287 private static byte[] Transform (byte[] data, ICryptoTransform t)
289 MemoryStream output = new MemoryStream ();
290 CryptoStream crypto = new CryptoStream (output, t, CryptoStreamMode.Write);
292 crypto.Write (data, 0, data.Length);
293 crypto.FlushFinalBlock ();
295 byte[] result = output.ToArray ();
303 private static byte[] ComputeCMSKeyChecksum (byte[] data)
305 byte[] hash = HashAlgorithm.Create ("SHA1").ComputeHash (data);
306 byte[] output = new byte [8];
308 Buffer.BlockCopy (hash, 0, output, 0, 8);
313 private static byte[] Concatenate (byte[] buf1, byte[] buf2)
315 byte[] output = new byte [buf1.Length + buf2.Length];
316 Buffer.BlockCopy (buf1, 0, output, 0, buf1.Length);
317 Buffer.BlockCopy (buf2, 0, output, buf1.Length, buf2.Length);
321 private static byte[] MSB (byte[] input)
323 return MSB (input, 8);
326 private static byte[] MSB (byte[] input, int bytes)
328 byte[] output = new byte [bytes];
329 Buffer.BlockCopy (input, 0, output, 0, bytes);
333 private static byte[] LSB (byte[] input)
335 return LSB (input, 8);
338 private static byte[] LSB (byte[] input, int bytes)
340 byte[] output = new byte [bytes];
341 Buffer.BlockCopy (input, bytes, output, 0, bytes);
345 private static byte[] Xor (byte[] x, byte[] y)
347 // This should *not* happen.
348 if (x.Length != y.Length)
349 throw new CryptographicException ("Error performing Xor: arrays different length.");
351 byte[] output = new byte [x.Length];
352 for (int i = 0; i < x.Length; i += 1)
353 output [i] = (byte) (x [i] ^ y [i]);
357 /* private static byte[] Xor (byte[] x, int n)
359 byte[] output = new Byte [x.Length];
360 for (int i = 0; i < x.Length; i += 1)
361 output [i] = (byte) ((int) x [i] ^ n);