KDFFeedbackParameters.cs 2.5 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 KdfFeedbackParameters : IDerivationParameters
  8. {
  9. // could be any valid value, using 32, don't know why
  10. private static readonly int UNUSED_R = -1;
  11. private readonly byte[] ki;
  12. private readonly byte[] iv;
  13. private readonly bool useCounter;
  14. private readonly int r;
  15. private readonly byte[] fixedInputData;
  16. private KdfFeedbackParameters(byte[] ki, byte[] iv, byte[] fixedInputData, int r, bool useCounter)
  17. {
  18. if (ki == null)
  19. {
  20. throw new ArgumentException("A KDF requires Ki (a seed) as input");
  21. }
  22. this.ki = Arrays.Clone(ki);
  23. if (fixedInputData == null)
  24. {
  25. this.fixedInputData = new byte[0];
  26. }
  27. else
  28. {
  29. this.fixedInputData = Arrays.Clone(fixedInputData);
  30. }
  31. this.r = r;
  32. if (iv == null)
  33. {
  34. this.iv = new byte[0];
  35. }
  36. else
  37. {
  38. this.iv = Arrays.Clone(iv);
  39. }
  40. this.useCounter = useCounter;
  41. }
  42. public static KdfFeedbackParameters CreateWithCounter(
  43. byte[] ki, byte[] iv, byte[] fixedInputData, int r)
  44. {
  45. if (r != 8 && r != 16 && r != 24 && r != 32)
  46. {
  47. throw new ArgumentException("Length of counter should be 8, 16, 24 or 32");
  48. }
  49. return new KdfFeedbackParameters(ki, iv, fixedInputData, r, true);
  50. }
  51. public static KdfFeedbackParameters CreateWithoutCounter(
  52. byte[] ki, byte[] iv, byte[] fixedInputData)
  53. {
  54. return new KdfFeedbackParameters(ki, iv, fixedInputData, UNUSED_R, false);
  55. }
  56. public byte[] Ki
  57. {
  58. get { return Arrays.Clone(ki); }
  59. }
  60. public byte[] Iv
  61. {
  62. get { return Arrays.Clone(iv); }
  63. }
  64. public bool UseCounter
  65. {
  66. get { return useCounter; }
  67. }
  68. public int R
  69. {
  70. get { return r; }
  71. }
  72. public byte[] FixedInputData
  73. {
  74. get { return Arrays.Clone(fixedInputData); }
  75. }
  76. }
  77. }
  78. #pragma warning restore
  79. #endif