KDFDoublePipelineIterationParameters.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  5. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters
  6. {
  7. public class KdfDoublePipelineIterationParameters : IDerivationParameters
  8. {
  9. // could be any valid value, using 32, don't know why
  10. private static readonly int UNUSED_R = 32;
  11. private readonly byte[] ki;
  12. private readonly bool useCounter;
  13. private readonly int r;
  14. private readonly byte[] fixedInputData;
  15. private KdfDoublePipelineIterationParameters(byte[] ki, byte[] fixedInputData, int r, bool useCounter)
  16. {
  17. if (ki == null)
  18. {
  19. throw new ArgumentException("A KDF requires Ki (a seed) as input");
  20. }
  21. this.ki = Arrays.Clone(ki);
  22. if (fixedInputData == null)
  23. {
  24. this.fixedInputData = new byte[0];
  25. }
  26. else
  27. {
  28. this.fixedInputData = Arrays.Clone(fixedInputData);
  29. }
  30. if (r != 8 && r != 16 && r != 24 && r != 32)
  31. {
  32. throw new ArgumentException("Length of counter should be 8, 16, 24 or 32");
  33. }
  34. this.r = r;
  35. this.useCounter = useCounter;
  36. }
  37. public static KdfDoublePipelineIterationParameters CreateWithCounter(
  38. byte[] ki, byte[] fixedInputData, int r)
  39. {
  40. return new KdfDoublePipelineIterationParameters(ki, fixedInputData, r, true);
  41. }
  42. public static KdfDoublePipelineIterationParameters CreateWithoutCounter(
  43. byte[] ki, byte[] fixedInputData)
  44. {
  45. return new KdfDoublePipelineIterationParameters(ki, fixedInputData, UNUSED_R, false);
  46. }
  47. public byte[] Ki
  48. {
  49. get { return Arrays.Clone(ki); }
  50. }
  51. public bool UseCounter
  52. {
  53. get { return useCounter; }
  54. }
  55. public int R
  56. {
  57. get { return r; }
  58. }
  59. public byte[] FixedInputData
  60. {
  61. get { return Arrays.Clone(fixedInputData); }
  62. }
  63. }
  64. }
  65. #pragma warning restore
  66. #endif