SkeinMac.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Digests;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Utilities;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  8. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Macs
  9. {
  10. /// <summary>
  11. /// Implementation of the Skein parameterised MAC function in 256, 512 and 1024 bit block sizes,
  12. /// based on the <see cref="BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Engines.ThreefishEngine">Threefish</see> tweakable block cipher.
  13. /// </summary>
  14. /// <remarks>
  15. /// This is the 1.3 version of Skein defined in the Skein hash function submission to the NIST SHA-3
  16. /// competition in October 2010.
  17. /// <p/>
  18. /// Skein was designed by Niels Ferguson - Stefan Lucks - Bruce Schneier - Doug Whiting - Mihir
  19. /// Bellare - Tadayoshi Kohno - Jon Callas - Jesse Walker.
  20. /// </remarks>
  21. /// <seealso cref="BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Digests.SkeinEngine"/>
  22. /// <seealso cref="BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters.SkeinParameters"/>
  23. public class SkeinMac
  24. : IMac
  25. {
  26. /// <summary>
  27. /// 256 bit block size - Skein-256
  28. /// </summary>
  29. public const int SKEIN_256 = SkeinEngine.SKEIN_256;
  30. /// <summary>
  31. /// 512 bit block size - Skein-512
  32. /// </summary>
  33. public const int SKEIN_512 = SkeinEngine.SKEIN_512;
  34. /// <summary>
  35. /// 1024 bit block size - Skein-1024
  36. /// </summary>
  37. public const int SKEIN_1024 = SkeinEngine.SKEIN_1024;
  38. private readonly SkeinEngine engine;
  39. /// <summary>
  40. /// Constructs a Skein MAC with an internal state size and output size.
  41. /// </summary>
  42. /// <param name="stateSizeBits">the internal state size in bits - one of <see cref="SKEIN_256"/> <see cref="SKEIN_512"/> or
  43. /// <see cref="SKEIN_1024"/>.</param>
  44. /// <param name="digestSizeBits">the output/MAC size to produce in bits, which must be an integral number of
  45. /// bytes.</param>
  46. public SkeinMac(int stateSizeBits, int digestSizeBits)
  47. {
  48. this.engine = new SkeinEngine(stateSizeBits, digestSizeBits);
  49. }
  50. public SkeinMac(SkeinMac mac)
  51. {
  52. this.engine = new SkeinEngine(mac.engine);
  53. }
  54. public string AlgorithmName
  55. {
  56. get { return "Skein-MAC-" + (engine.BlockSize * 8) + "-" + (engine.OutputSize * 8); }
  57. }
  58. /// <summary>
  59. /// Optionally initialises the Skein digest with the provided parameters.
  60. /// </summary>
  61. /// See <see cref="BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters.SkeinParameters"></see> for details on the parameterisation of the Skein hash function.
  62. /// <param name="parameters">the parameters to apply to this engine, or <code>null</code> to use no parameters.</param>
  63. public void Init(ICipherParameters parameters)
  64. {
  65. SkeinParameters skeinParameters;
  66. if (parameters is SkeinParameters)
  67. {
  68. skeinParameters = (SkeinParameters)parameters;
  69. }
  70. else if (parameters is KeyParameter)
  71. {
  72. skeinParameters = new SkeinParameters.Builder().SetKey(((KeyParameter)parameters).GetKey()).Build();
  73. }
  74. else
  75. {
  76. throw new ArgumentException("Invalid parameter passed to Skein MAC init - "
  77. + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(parameters));
  78. }
  79. if (skeinParameters.GetKey() == null)
  80. {
  81. throw new ArgumentException("Skein MAC requires a key parameter.");
  82. }
  83. engine.Init(skeinParameters);
  84. }
  85. public int GetMacSize()
  86. {
  87. return engine.OutputSize;
  88. }
  89. public void Reset()
  90. {
  91. engine.Reset();
  92. }
  93. public void Update(byte inByte)
  94. {
  95. engine.Update(inByte);
  96. }
  97. public void BlockUpdate(byte[] input, int inOff, int len)
  98. {
  99. engine.Update(input, inOff, len);
  100. }
  101. public int DoFinal(byte[] output, int outOff)
  102. {
  103. return engine.DoFinal(output, outOff);
  104. }
  105. }
  106. }
  107. #pragma warning restore
  108. #endif