KDFCounterParameters.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 KdfCounterParameters : IDerivationParameters
  8. {
  9. private byte[] ki;
  10. private byte[] fixedInputDataCounterPrefix;
  11. private byte[] fixedInputDataCounterSuffix;
  12. private int r;
  13. /// <summary>
  14. /// Base constructor - suffix fixed input data only.
  15. /// </summary>
  16. /// <param name="ki">the KDF seed</param>
  17. /// <param name="fixedInputDataCounterSuffix">fixed input data to follow counter.</param>
  18. /// <param name="r">length of the counter in bits</param>
  19. public KdfCounterParameters(byte[] ki, byte[] fixedInputDataCounterSuffix, int r) : this(ki, null, fixedInputDataCounterSuffix, r)
  20. {
  21. }
  22. /// <summary>
  23. /// Base constructor - prefix and suffix fixed input data.
  24. /// </summary>
  25. /// <param name="ki">the KDF seed</param>
  26. /// <param name="fixedInputDataCounterPrefix">fixed input data to precede counter</param>
  27. /// <param name="fixedInputDataCounterSuffix">fixed input data to follow counter.</param>
  28. /// <param name="r">length of the counter in bits.</param>
  29. public KdfCounterParameters(byte[] ki, byte[] fixedInputDataCounterPrefix, byte[] fixedInputDataCounterSuffix, int r)
  30. {
  31. if (ki == null)
  32. {
  33. throw new ArgumentException("A KDF requires Ki (a seed) as input");
  34. }
  35. this.ki = Arrays.Clone(ki);
  36. if (fixedInputDataCounterPrefix == null)
  37. {
  38. this.fixedInputDataCounterPrefix = new byte[0];
  39. }
  40. else
  41. {
  42. this.fixedInputDataCounterPrefix = Arrays.Clone(fixedInputDataCounterPrefix);
  43. }
  44. if (fixedInputDataCounterSuffix == null)
  45. {
  46. this.fixedInputDataCounterSuffix = new byte[0];
  47. }
  48. else
  49. {
  50. this.fixedInputDataCounterSuffix = Arrays.Clone(fixedInputDataCounterSuffix);
  51. }
  52. if (r != 8 && r != 16 && r != 24 && r != 32)
  53. {
  54. throw new ArgumentException("Length of counter should be 8, 16, 24 or 32");
  55. }
  56. this.r = r;
  57. }
  58. public byte[] Ki
  59. {
  60. get { return ki; }
  61. }
  62. public byte[] FixedInputData
  63. {
  64. get { return Arrays.Clone(fixedInputDataCounterSuffix); }
  65. }
  66. public byte[] FixedInputDataCounterPrefix
  67. {
  68. get { return Arrays.Clone(fixedInputDataCounterPrefix); }
  69. }
  70. public byte[] FixedInputDataCounterSuffix
  71. {
  72. get { return Arrays.Clone(fixedInputDataCounterSuffix); }
  73. }
  74. public int R
  75. {
  76. get { return r; }
  77. }
  78. }
  79. }
  80. #pragma warning restore
  81. #endif