SymmetricKeyWrap.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. #if NET_1_2
  10. using System.IO;
  11. using System.Security.Cryptography;
  12. namespace System.Security.Cryptography.Xml {
  13. internal class SymmetricKeyWrap {
  14. public SymmetricKeyWrap ()
  15. {
  16. }
  17. public static byte[] AESKeyWrapEncrypt (byte[] rgbKey, byte[] rgbWrappedKeyData)
  18. {
  19. SymmetricAlgorithm symAlg = SymmetricAlgorithm.Create ("Rijndael");
  20. // Apparently no one felt the need to document that this requires Electronic Codebook mode.
  21. symAlg.Mode = CipherMode.ECB;
  22. // This was also not documented anywhere.
  23. symAlg.IV = new byte [16] {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  24. ICryptoTransform transform = symAlg.CreateEncryptor (rgbKey, symAlg.IV);
  25. int N = rgbWrappedKeyData.Length / 8;
  26. byte[] A;
  27. byte[] B = new Byte [16];
  28. byte [] C = new byte [8 * (N + 1)];
  29. // 1. if N is 1:
  30. // B = AES(K)enc(0xA6A6A6A6A6A6A6A6|P(1))
  31. // C(0) = MSB(B)
  32. // C(1) = LSB(B)
  33. if (N == 1) {
  34. A = new byte [8] {0xa6, 0xa6, 0xa6, 0xa6, 0xa6, 0xa6, 0xa6, 0xa6};
  35. transform.TransformBlock (Concatenate (A, rgbWrappedKeyData), 0, 16, B, 0);
  36. Buffer.BlockCopy (MSB(B), 0, C, 0, 8);
  37. Buffer.BlockCopy (LSB(B), 0, C, 8, 8);
  38. } else {
  39. // if N > 1, perform the following steps:
  40. // 2. Initialize variables:
  41. // Set A to 0xA6A6A6A6A6A6A6A6
  42. // For i = 1 to N,
  43. // R(i) = P(i)
  44. A = new byte [8] {0xa6, 0xa6, 0xa6, 0xa6, 0xa6, 0xa6, 0xa6, 0xa6};
  45. byte[][] R = new byte [N + 1][];
  46. for (int i = 1; i <= N; i += 1) {
  47. R [i] = new byte [8];
  48. Buffer.BlockCopy (rgbWrappedKeyData, 8 * (i - 1), R [i], 0, 8);
  49. }
  50. // 3. Calculate intermediate values:
  51. // For j = 0 to 5
  52. // For i = 1 to N
  53. // t = i + j * N
  54. // B = AES(K)enc(A|R(i))
  55. // A = XOR(t, MSB(B))
  56. // R(i) = LSB(B)
  57. for (int j = 0; j <= 5; j += 1) {
  58. for (int i = 1; i <= N; i += 1) {
  59. transform.TransformBlock (Concatenate (A, R [i]), 0, 16, B, 0);
  60. // Yawn. It was nice of those at NIST to document how exactly we should XOR
  61. // an integer value with a byte array. Not.
  62. byte[] T = BitConverter.GetBytes ((long) (N * j + i));
  63. // This is nice.
  64. if (BitConverter.IsLittleEndian)
  65. Array.Reverse (T);
  66. A = Xor (T, MSB(B));
  67. R [i] = LSB (B);
  68. }
  69. }
  70. // 4. Output the results:
  71. // Set C(0) = A
  72. // For i = 1 to N
  73. // C(i) = R(i)
  74. Buffer.BlockCopy (A, 0, C, 0, 8);
  75. for (int i = 1; i <= N; i += 1)
  76. Buffer.BlockCopy (R [i], 0, C, 8 * i, 8);
  77. }
  78. return C;
  79. }
  80. [MonoTODO]
  81. public static byte[] AESKeyWrapDecrypt (byte[] rgbKey, byte[] rgbEncryptedWrappedKeyData)
  82. {
  83. throw new NotImplementedException ();
  84. }
  85. [MonoTODO]
  86. public static byte[] TripleDESKeyWrapEncrypt (byte[] rgbKey, byte[] rgbWrappedKeyData)
  87. {
  88. throw new NotImplementedException ();
  89. }
  90. [MonoTODO]
  91. public static byte[] TripleDESKeyWrapDecrypt (byte[] rgbKey, byte[] rgbEncryptedWrappedKeyData)
  92. {
  93. throw new NotImplementedException ();
  94. }
  95. private static byte[] Transform (byte[] data, ICryptoTransform t, bool flush)
  96. {
  97. MemoryStream output = new MemoryStream ();
  98. CryptoStream crypto = new CryptoStream (output, t, CryptoStreamMode.Write);
  99. crypto.Write (data, 0, data.Length);
  100. byte[] buf;
  101. if (flush) {
  102. crypto.Close ();
  103. output.Close ();
  104. buf = output.ToArray ();
  105. } else {
  106. buf = output.ToArray ();
  107. crypto.Close ();
  108. output.Close ();
  109. }
  110. return buf;
  111. }
  112. private static byte[] ComputeCMSKeyChecksum (byte[] data)
  113. {
  114. byte[] hash = HashAlgorithm.Create ("SHA1").ComputeHash (data);
  115. byte[] output = new byte [8];
  116. Buffer.BlockCopy (hash, 0, output, 0, 8);
  117. return output;
  118. }
  119. private static byte[] Concatenate (byte[] buf1, byte[] buf2)
  120. {
  121. byte[] output = new byte [buf1.Length + buf2.Length];
  122. Buffer.BlockCopy (buf1, 0, output, 0, buf1.Length);
  123. Buffer.BlockCopy (buf2, 0, output, buf1.Length, buf2.Length);
  124. return output;
  125. }
  126. private static byte[] MSB (byte[] input)
  127. {
  128. byte[] output = new byte [8];
  129. Buffer.BlockCopy (input, 0, output, 0, 8);
  130. return output;
  131. }
  132. private static byte[] LSB (byte[] input)
  133. {
  134. byte[] output = new byte [8];
  135. Buffer.BlockCopy (input, 8, output, 0, 8);
  136. return output;
  137. }
  138. private static byte[] Xor (byte[] x, byte[] y)
  139. {
  140. // This should *not* happen.
  141. if (x.Length != y.Length)
  142. throw new CryptographicException ("Error performing Xor: arrays different length.");
  143. byte[] output = new byte [x.Length];
  144. for (int i = 0; i < x.Length; i += 1)
  145. output [i] = (byte) (x [i] ^ y [i]);
  146. return output;
  147. }
  148. }
  149. }
  150. #endif