Pkcs12ParametersGenerator.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Math;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Security;
  8. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Generators
  9. {
  10. /**
  11. * Generator for Pbe derived keys and ivs as defined by Pkcs 12 V1.0.
  12. * <p>
  13. * The document this implementation is based on can be found at
  14. * <a href="http://www.rsasecurity.com/rsalabs/pkcs/pkcs-12/index.html">
  15. * RSA's Pkcs12 Page</a>
  16. * </p>
  17. */
  18. public class Pkcs12ParametersGenerator
  19. : PbeParametersGenerator
  20. {
  21. public const int KeyMaterial = 1;
  22. public const int IVMaterial = 2;
  23. public const int MacMaterial = 3;
  24. private readonly IDigest digest;
  25. private readonly int u;
  26. private readonly int v;
  27. /**
  28. * Construct a Pkcs 12 Parameters generator.
  29. *
  30. * @param digest the digest to be used as the source of derived keys.
  31. * @exception ArgumentException if an unknown digest is passed in.
  32. */
  33. public Pkcs12ParametersGenerator(
  34. IDigest digest)
  35. {
  36. this.digest = digest;
  37. u = digest.GetDigestSize();
  38. v = digest.GetByteLength();
  39. }
  40. /**
  41. * add a + b + 1, returning the result in a. The a value is treated
  42. * as a BigInteger of length (b.Length * 8) bits. The result is
  43. * modulo 2^b.Length in case of overflow.
  44. */
  45. private void Adjust(
  46. byte[] a,
  47. int aOff,
  48. byte[] b)
  49. {
  50. int x = (b[b.Length - 1] & 0xff) + (a[aOff + b.Length - 1] & 0xff) + 1;
  51. a[aOff + b.Length - 1] = (byte)x;
  52. x = (int) ((uint) x >> 8);
  53. for (int i = b.Length - 2; i >= 0; i--)
  54. {
  55. x += (b[i] & 0xff) + (a[aOff + i] & 0xff);
  56. a[aOff + i] = (byte)x;
  57. x = (int) ((uint) x >> 8);
  58. }
  59. }
  60. /**
  61. * generation of a derived key ala Pkcs12 V1.0.
  62. */
  63. private byte[] GenerateDerivedKey(
  64. int idByte,
  65. int n)
  66. {
  67. byte[] D = new byte[v];
  68. byte[] dKey = new byte[n];
  69. for (int i = 0; i != D.Length; i++)
  70. {
  71. D[i] = (byte)idByte;
  72. }
  73. byte[] S;
  74. if ((mSalt != null) && (mSalt.Length != 0))
  75. {
  76. S = new byte[v * ((mSalt.Length + v - 1) / v)];
  77. for (int i = 0; i != S.Length; i++)
  78. {
  79. S[i] = mSalt[i % mSalt.Length];
  80. }
  81. }
  82. else
  83. {
  84. S = new byte[0];
  85. }
  86. byte[] P;
  87. if ((mPassword != null) && (mPassword.Length != 0))
  88. {
  89. P = new byte[v * ((mPassword.Length + v - 1) / v)];
  90. for (int i = 0; i != P.Length; i++)
  91. {
  92. P[i] = mPassword[i % mPassword.Length];
  93. }
  94. }
  95. else
  96. {
  97. P = new byte[0];
  98. }
  99. byte[] I = new byte[S.Length + P.Length];
  100. Array.Copy(S, 0, I, 0, S.Length);
  101. Array.Copy(P, 0, I, S.Length, P.Length);
  102. byte[] B = new byte[v];
  103. int c = (n + u - 1) / u;
  104. byte[] A = new byte[u];
  105. for (int i = 1; i <= c; i++)
  106. {
  107. digest.BlockUpdate(D, 0, D.Length);
  108. digest.BlockUpdate(I, 0, I.Length);
  109. digest.DoFinal(A, 0);
  110. for (int j = 1; j != mIterationCount; j++)
  111. {
  112. digest.BlockUpdate(A, 0, A.Length);
  113. digest.DoFinal(A, 0);
  114. }
  115. for (int j = 0; j != B.Length; j++)
  116. {
  117. B[j] = A[j % A.Length];
  118. }
  119. for (int j = 0; j != I.Length / v; j++)
  120. {
  121. Adjust(I, j * v, B);
  122. }
  123. if (i == c)
  124. {
  125. Array.Copy(A, 0, dKey, (i - 1) * u, dKey.Length - ((i - 1) * u));
  126. }
  127. else
  128. {
  129. Array.Copy(A, 0, dKey, (i - 1) * u, A.Length);
  130. }
  131. }
  132. return dKey;
  133. }
  134. /**
  135. * Generate a key parameter derived from the password, salt, and iteration
  136. * count we are currently initialised with.
  137. *
  138. * @param keySize the size of the key we want (in bits)
  139. * @return a KeyParameter object.
  140. */
  141. public override ICipherParameters GenerateDerivedParameters(
  142. int keySize)
  143. {
  144. keySize /= 8;
  145. byte[] dKey = GenerateDerivedKey(KeyMaterial, keySize);
  146. return new KeyParameter(dKey, 0, keySize);
  147. }
  148. public override ICipherParameters GenerateDerivedParameters(
  149. string algorithm,
  150. int keySize)
  151. {
  152. keySize /= 8;
  153. byte[] dKey = GenerateDerivedKey(KeyMaterial, keySize);
  154. return ParameterUtilities.CreateKeyParameter(algorithm, dKey, 0, keySize);
  155. }
  156. /**
  157. * Generate a key with initialisation vector parameter derived from
  158. * the password, salt, and iteration count we are currently initialised
  159. * with.
  160. *
  161. * @param keySize the size of the key we want (in bits)
  162. * @param ivSize the size of the iv we want (in bits)
  163. * @return a ParametersWithIV object.
  164. */
  165. public override ICipherParameters GenerateDerivedParameters(
  166. int keySize,
  167. int ivSize)
  168. {
  169. keySize /= 8;
  170. ivSize /= 8;
  171. byte[] dKey = GenerateDerivedKey(KeyMaterial, keySize);
  172. byte[] iv = GenerateDerivedKey(IVMaterial, ivSize);
  173. return new ParametersWithIV(new KeyParameter(dKey, 0, keySize), iv, 0, ivSize);
  174. }
  175. public override ICipherParameters GenerateDerivedParameters(
  176. string algorithm,
  177. int keySize,
  178. int ivSize)
  179. {
  180. keySize /= 8;
  181. ivSize /= 8;
  182. byte[] dKey = GenerateDerivedKey(KeyMaterial, keySize);
  183. KeyParameter key = ParameterUtilities.CreateKeyParameter(algorithm, dKey, 0, keySize);
  184. byte[] iv = GenerateDerivedKey(IVMaterial, ivSize);
  185. return new ParametersWithIV(key, iv, 0, ivSize);
  186. }
  187. /**
  188. * Generate a key parameter for use with a MAC derived from the password,
  189. * salt, and iteration count we are currently initialised with.
  190. *
  191. * @param keySize the size of the key we want (in bits)
  192. * @return a KeyParameter object.
  193. */
  194. public override ICipherParameters GenerateDerivedMacParameters(
  195. int keySize)
  196. {
  197. keySize /= 8;
  198. byte[] dKey = GenerateDerivedKey(MacMaterial, keySize);
  199. return new KeyParameter(dKey, 0, keySize);
  200. }
  201. }
  202. }
  203. #pragma warning restore
  204. #endif