S2k.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.IO;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.IO;
  7. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Bcpg
  8. {
  9. /// <remarks>The string to key specifier class.</remarks>
  10. public class S2k
  11. : BcpgObject
  12. {
  13. private const int ExpBias = 6;
  14. public const int Simple = 0;
  15. public const int Salted = 1;
  16. public const int SaltedAndIterated = 3;
  17. public const int GnuDummyS2K = 101;
  18. public const int GnuProtectionModeNoPrivateKey = 1;
  19. public const int GnuProtectionModeDivertToCard = 2;
  20. internal int type;
  21. internal HashAlgorithmTag algorithm;
  22. internal byte[] iv;
  23. internal int itCount = -1;
  24. internal int protectionMode = -1;
  25. internal S2k(
  26. Stream inStr)
  27. {
  28. type = inStr.ReadByte();
  29. algorithm = (HashAlgorithmTag) inStr.ReadByte();
  30. //
  31. // if this happens we have a dummy-S2k packet.
  32. //
  33. if (type != GnuDummyS2K)
  34. {
  35. if (type != 0)
  36. {
  37. iv = new byte[8];
  38. if (Streams.ReadFully(inStr, iv, 0, iv.Length) < iv.Length)
  39. throw new EndOfStreamException();
  40. if (type == 3)
  41. {
  42. itCount = inStr.ReadByte();
  43. }
  44. }
  45. }
  46. else
  47. {
  48. inStr.ReadByte(); // G
  49. inStr.ReadByte(); // N
  50. inStr.ReadByte(); // U
  51. protectionMode = inStr.ReadByte(); // protection mode
  52. }
  53. }
  54. public S2k(
  55. HashAlgorithmTag algorithm)
  56. {
  57. this.type = 0;
  58. this.algorithm = algorithm;
  59. }
  60. public S2k(
  61. HashAlgorithmTag algorithm,
  62. byte[] iv)
  63. {
  64. this.type = 1;
  65. this.algorithm = algorithm;
  66. this.iv = iv;
  67. }
  68. public S2k(
  69. HashAlgorithmTag algorithm,
  70. byte[] iv,
  71. int itCount)
  72. {
  73. this.type = 3;
  74. this.algorithm = algorithm;
  75. this.iv = iv;
  76. this.itCount = itCount;
  77. }
  78. public virtual int Type
  79. {
  80. get { return type; }
  81. }
  82. /// <summary>The hash algorithm.</summary>
  83. public virtual HashAlgorithmTag HashAlgorithm
  84. {
  85. get { return algorithm; }
  86. }
  87. /// <summary>The IV for the key generation algorithm.</summary>
  88. public virtual byte[] GetIV()
  89. {
  90. return Arrays.Clone(iv);
  91. }
  92. public long GetIterationCount()
  93. {
  94. return IterationCount;
  95. }
  96. /// <summary>The iteration count</summary>
  97. public virtual long IterationCount
  98. {
  99. get { return (16 + (itCount & 15)) << ((itCount >> 4) + ExpBias); }
  100. }
  101. /// <summary>The protection mode - only if GnuDummyS2K</summary>
  102. public virtual int ProtectionMode
  103. {
  104. get { return protectionMode; }
  105. }
  106. public override void Encode(
  107. BcpgOutputStream bcpgOut)
  108. {
  109. bcpgOut.WriteByte((byte) type);
  110. bcpgOut.WriteByte((byte) algorithm);
  111. if (type != GnuDummyS2K)
  112. {
  113. if (type != 0)
  114. {
  115. bcpgOut.Write(iv);
  116. }
  117. if (type == 3)
  118. {
  119. bcpgOut.WriteByte((byte) itCount);
  120. }
  121. }
  122. else
  123. {
  124. bcpgOut.WriteByte((byte) 'G');
  125. bcpgOut.WriteByte((byte) 'N');
  126. bcpgOut.WriteByte((byte) 'U');
  127. bcpgOut.WriteByte((byte) protectionMode);
  128. }
  129. }
  130. }
  131. }
  132. #pragma warning restore
  133. #endif