RFC3394WrapEngine.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. using Org.BouncyCastle.Crypto.Parameters;
  4. using Org.BouncyCastle.Utilities;
  5. namespace Org.BouncyCastle.Crypto.Engines
  6. {
  7. /// <remarks>
  8. /// An implementation of the AES Key Wrapper from the NIST Key Wrap
  9. /// Specification as described in RFC 3394.
  10. /// <p/>
  11. /// For further details see: <a href="http://www.ietf.org/rfc/rfc3394.txt">http://www.ietf.org/rfc/rfc3394.txt</a>
  12. /// and <a href="http://csrc.nist.gov/encryption/kms/key-wrap.pdf">http://csrc.nist.gov/encryption/kms/key-wrap.pdf</a>.
  13. /// </remarks>
  14. public class Rfc3394WrapEngine
  15. : IWrapper
  16. {
  17. private readonly IBlockCipher engine;
  18. private KeyParameter param;
  19. private bool forWrapping;
  20. private byte[] iv =
  21. {
  22. 0xa6, 0xa6, 0xa6, 0xa6,
  23. 0xa6, 0xa6, 0xa6, 0xa6
  24. };
  25. public Rfc3394WrapEngine(
  26. IBlockCipher engine)
  27. {
  28. this.engine = engine;
  29. }
  30. public virtual void Init(
  31. bool forWrapping,
  32. ICipherParameters parameters)
  33. {
  34. this.forWrapping = forWrapping;
  35. if (parameters is ParametersWithRandom)
  36. {
  37. parameters = ((ParametersWithRandom) parameters).Parameters;
  38. }
  39. if (parameters is KeyParameter)
  40. {
  41. this.param = (KeyParameter) parameters;
  42. }
  43. else if (parameters is ParametersWithIV)
  44. {
  45. ParametersWithIV pIV = (ParametersWithIV) parameters;
  46. byte[] iv = pIV.GetIV();
  47. if (iv.Length != 8)
  48. throw new ArgumentException("IV length not equal to 8", "parameters");
  49. this.iv = iv;
  50. this.param = (KeyParameter) pIV.Parameters;
  51. }
  52. else
  53. {
  54. // TODO Throw an exception for bad parameters?
  55. }
  56. }
  57. public virtual string AlgorithmName
  58. {
  59. get { return engine.AlgorithmName; }
  60. }
  61. public virtual byte[] Wrap(
  62. byte[] input,
  63. int inOff,
  64. int inLen)
  65. {
  66. if (!forWrapping)
  67. {
  68. throw new InvalidOperationException("not set for wrapping");
  69. }
  70. int n = inLen / 8;
  71. if ((n * 8) != inLen)
  72. {
  73. throw new DataLengthException("wrap data must be a multiple of 8 bytes");
  74. }
  75. byte[] block = new byte[inLen + iv.Length];
  76. byte[] buf = new byte[8 + iv.Length];
  77. Array.Copy(iv, 0, block, 0, iv.Length);
  78. Array.Copy(input, inOff, block, iv.Length, inLen);
  79. engine.Init(true, param);
  80. for (int j = 0; j != 6; j++)
  81. {
  82. for (int i = 1; i <= n; i++)
  83. {
  84. Array.Copy(block, 0, buf, 0, iv.Length);
  85. Array.Copy(block, 8 * i, buf, iv.Length, 8);
  86. engine.ProcessBlock(buf, 0, buf, 0);
  87. int t = n * j + i;
  88. for (int k = 1; t != 0; k++)
  89. {
  90. byte v = (byte)t;
  91. buf[iv.Length - k] ^= v;
  92. t = (int) ((uint)t >> 8);
  93. }
  94. Array.Copy(buf, 0, block, 0, 8);
  95. Array.Copy(buf, 8, block, 8 * i, 8);
  96. }
  97. }
  98. return block;
  99. }
  100. public virtual byte[] Unwrap(
  101. byte[] input,
  102. int inOff,
  103. int inLen)
  104. {
  105. if (forWrapping)
  106. {
  107. throw new InvalidOperationException("not set for unwrapping");
  108. }
  109. int n = inLen / 8;
  110. if ((n * 8) != inLen)
  111. {
  112. throw new InvalidCipherTextException("unwrap data must be a multiple of 8 bytes");
  113. }
  114. byte[] block = new byte[inLen - iv.Length];
  115. byte[] a = new byte[iv.Length];
  116. byte[] buf = new byte[8 + iv.Length];
  117. Array.Copy(input, inOff, a, 0, iv.Length);
  118. Array.Copy(input, inOff + iv.Length, block, 0, inLen - iv.Length);
  119. engine.Init(false, param);
  120. n = n - 1;
  121. for (int j = 5; j >= 0; j--)
  122. {
  123. for (int i = n; i >= 1; i--)
  124. {
  125. Array.Copy(a, 0, buf, 0, iv.Length);
  126. Array.Copy(block, 8 * (i - 1), buf, iv.Length, 8);
  127. int t = n * j + i;
  128. for (int k = 1; t != 0; k++)
  129. {
  130. byte v = (byte)t;
  131. buf[iv.Length - k] ^= v;
  132. t = (int) ((uint)t >> 8);
  133. }
  134. engine.ProcessBlock(buf, 0, buf, 0);
  135. Array.Copy(buf, 0, a, 0, 8);
  136. Array.Copy(buf, 8, block, 8 * (i - 1), 8);
  137. }
  138. }
  139. if (!Arrays.ConstantTimeAreEqual(a, iv))
  140. throw new InvalidCipherTextException("checksum failed");
  141. return block;
  142. }
  143. }
  144. }
  145. #endif