GMac.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Collections;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Modes;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  8. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  9. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Macs
  10. {
  11. /// <summary>
  12. /// The GMAC specialisation of Galois/Counter mode (GCM) detailed in NIST Special Publication
  13. /// 800-38D.
  14. /// </summary>
  15. /// <remarks>
  16. /// GMac is an invocation of the GCM mode where no data is encrypted (i.e. all input data to the Mac
  17. /// is processed as additional authenticated data with the underlying GCM block cipher).
  18. /// </remarks>
  19. public class GMac
  20. : IMac
  21. {
  22. private readonly GcmBlockCipher cipher;
  23. private readonly int macSizeBits;
  24. /// <summary>
  25. /// Creates a GMAC based on the operation of a block cipher in GCM mode.
  26. /// </summary>
  27. /// <remarks>
  28. /// This will produce an authentication code the length of the block size of the cipher.
  29. /// </remarks>
  30. /// <param name="cipher">the cipher to be used in GCM mode to generate the MAC.</param>
  31. public GMac(GcmBlockCipher cipher)
  32. : this(cipher, 128)
  33. {
  34. }
  35. /// <summary>
  36. /// Creates a GMAC based on the operation of a 128 bit block cipher in GCM mode.
  37. /// </summary>
  38. /// <remarks>
  39. /// This will produce an authentication code the length of the block size of the cipher.
  40. /// </remarks>
  41. /// <param name="cipher">the cipher to be used in GCM mode to generate the MAC.</param>
  42. /// <param name="macSizeBits">the mac size to generate, in bits. Must be a multiple of 8, between 32 and 128 (inclusive).
  43. /// Sizes less than 96 are not recommended, but are supported for specialized applications.</param>
  44. public GMac(GcmBlockCipher cipher, int macSizeBits)
  45. {
  46. this.cipher = cipher;
  47. this.macSizeBits = macSizeBits;
  48. }
  49. /// <summary>
  50. /// Initialises the GMAC - requires a <see cref="BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters.ParametersWithIV"/>
  51. /// providing a <see cref="BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters.KeyParameter"/> and a nonce.
  52. /// </summary>
  53. public void Init(ICipherParameters parameters)
  54. {
  55. if (parameters is ParametersWithIV)
  56. {
  57. ParametersWithIV param = (ParametersWithIV)parameters;
  58. byte[] iv = param.GetIV();
  59. KeyParameter keyParam = (KeyParameter)param.Parameters;
  60. // GCM is always operated in encrypt mode to calculate MAC
  61. cipher.Init(true, new AeadParameters(keyParam, macSizeBits, iv));
  62. }
  63. else
  64. {
  65. throw new ArgumentException("GMAC requires ParametersWithIV");
  66. }
  67. }
  68. public string AlgorithmName
  69. {
  70. get { return cipher.GetUnderlyingCipher().AlgorithmName + "-GMAC"; }
  71. }
  72. public int GetMacSize()
  73. {
  74. return macSizeBits / 8;
  75. }
  76. public void Update(byte input)
  77. {
  78. cipher.ProcessAadByte(input);
  79. }
  80. public void BlockUpdate(byte[] input, int inOff, int len)
  81. {
  82. cipher.ProcessAadBytes(input, inOff, len);
  83. }
  84. public int DoFinal(byte[] output, int outOff)
  85. {
  86. try
  87. {
  88. return cipher.DoFinal(output, outOff);
  89. }
  90. catch (InvalidCipherTextException e)
  91. {
  92. // Impossible in encrypt mode
  93. throw new InvalidOperationException(e.ToString());
  94. }
  95. }
  96. public void Reset()
  97. {
  98. cipher.Reset();
  99. }
  100. }
  101. }
  102. #pragma warning restore
  103. #endif