HKDFParameters.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Macs;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  6. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters
  7. {
  8. /**
  9. * Parameter class for the HkdfBytesGenerator class.
  10. */
  11. public class HkdfParameters
  12. : IDerivationParameters
  13. {
  14. private readonly byte[] ikm;
  15. private readonly bool skipExpand;
  16. private readonly byte[] salt;
  17. private readonly byte[] info;
  18. private HkdfParameters(byte[] ikm, bool skip, byte[] salt, byte[] info)
  19. {
  20. if (ikm == null)
  21. throw new ArgumentNullException("ikm");
  22. this.ikm = Arrays.Clone(ikm);
  23. this.skipExpand = skip;
  24. if (salt == null || salt.Length == 0)
  25. {
  26. this.salt = null;
  27. }
  28. else
  29. {
  30. this.salt = Arrays.Clone(salt);
  31. }
  32. if (info == null)
  33. {
  34. this.info = new byte[0];
  35. }
  36. else
  37. {
  38. this.info = Arrays.Clone(info);
  39. }
  40. }
  41. /**
  42. * Generates parameters for HKDF, specifying both the optional salt and
  43. * optional info. Step 1: Extract won't be skipped.
  44. *
  45. * @param ikm the input keying material or seed
  46. * @param salt the salt to use, may be null for a salt for hashLen zeros
  47. * @param info the info to use, may be null for an info field of zero bytes
  48. */
  49. public HkdfParameters(byte[] ikm, byte[] salt, byte[] info)
  50. : this(ikm, false, salt, info)
  51. {
  52. }
  53. /**
  54. * Factory method that makes the HKDF skip the extract part of the key
  55. * derivation function.
  56. *
  57. * @param ikm the input keying material or seed, directly used for step 2:
  58. * Expand
  59. * @param info the info to use, may be null for an info field of zero bytes
  60. * @return HKDFParameters that makes the implementation skip step 1
  61. */
  62. public static HkdfParameters SkipExtractParameters(byte[] ikm, byte[] info)
  63. {
  64. return new HkdfParameters(ikm, true, null, info);
  65. }
  66. public static HkdfParameters DefaultParameters(byte[] ikm)
  67. {
  68. return new HkdfParameters(ikm, false, null, null);
  69. }
  70. /**
  71. * Returns the input keying material or seed.
  72. *
  73. * @return the keying material
  74. */
  75. public virtual byte[] GetIkm()
  76. {
  77. return Arrays.Clone(ikm);
  78. }
  79. /**
  80. * Returns if step 1: extract has to be skipped or not
  81. *
  82. * @return true for skipping, false for no skipping of step 1
  83. */
  84. public virtual bool SkipExtract
  85. {
  86. get { return skipExpand; }
  87. }
  88. /**
  89. * Returns the salt, or null if the salt should be generated as a byte array
  90. * of HashLen zeros.
  91. *
  92. * @return the salt, or null
  93. */
  94. public virtual byte[] GetSalt()
  95. {
  96. return Arrays.Clone(salt);
  97. }
  98. /**
  99. * Returns the info field, which may be empty (null is converted to empty).
  100. *
  101. * @return the info field, never null
  102. */
  103. public virtual byte[] GetInfo()
  104. {
  105. return Arrays.Clone(info);
  106. }
  107. }
  108. }
  109. #pragma warning restore
  110. #endif