MacUtilities.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Collections.Generic;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1;
  6. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.Iana;
  7. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.Misc;
  8. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.Nist;
  9. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.Pkcs;
  10. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.Rosstandart;
  11. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  12. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Engines;
  13. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Macs;
  14. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Paddings;
  15. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  16. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities.Collections;
  17. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Security
  18. {
  19. /// <remarks>
  20. /// Utility class for creating HMac object from their names/Oids
  21. /// </remarks>
  22. public static class MacUtilities
  23. {
  24. private static readonly IDictionary<string, string> Algorithms =
  25. new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
  26. static MacUtilities()
  27. {
  28. Algorithms[IanaObjectIdentifiers.HmacMD5.Id] = "HMAC-MD5";
  29. Algorithms[IanaObjectIdentifiers.HmacRipeMD160.Id] = "HMAC-RIPEMD160";
  30. Algorithms[IanaObjectIdentifiers.HmacSha1.Id] = "HMAC-SHA1";
  31. Algorithms[IanaObjectIdentifiers.HmacTiger.Id] = "HMAC-TIGER";
  32. Algorithms[PkcsObjectIdentifiers.IdHmacWithSha1.Id] = "HMAC-SHA1";
  33. Algorithms[MiscObjectIdentifiers.HMAC_SHA1.Id] = "HMAC-SHA1";
  34. Algorithms[PkcsObjectIdentifiers.IdHmacWithSha224.Id] = "HMAC-SHA224";
  35. Algorithms[PkcsObjectIdentifiers.IdHmacWithSha256.Id] = "HMAC-SHA256";
  36. Algorithms[PkcsObjectIdentifiers.IdHmacWithSha384.Id] = "HMAC-SHA384";
  37. Algorithms[PkcsObjectIdentifiers.IdHmacWithSha512.Id] = "HMAC-SHA512";
  38. Algorithms[NistObjectIdentifiers.IdHMacWithSha3_224.Id] = "HMAC-SHA3-224";
  39. Algorithms[NistObjectIdentifiers.IdHMacWithSha3_256.Id] = "HMAC-SHA3-256";
  40. Algorithms[NistObjectIdentifiers.IdHMacWithSha3_384.Id] = "HMAC-SHA3-384";
  41. Algorithms[NistObjectIdentifiers.IdHMacWithSha3_512.Id] = "HMAC-SHA3-512";
  42. Algorithms[RosstandartObjectIdentifiers.id_tc26_hmac_gost_3411_12_256.Id] = "HMAC-GOST3411-2012-256";
  43. Algorithms[RosstandartObjectIdentifiers.id_tc26_hmac_gost_3411_12_512.Id] = "HMAC-GOST3411-2012-512";
  44. // TODO AESMAC?
  45. Algorithms["DES"] = "DESMAC";
  46. Algorithms["DES/CFB8"] = "DESMAC/CFB8";
  47. Algorithms["DES64"] = "DESMAC64";
  48. Algorithms["DESEDE"] = "DESEDEMAC";
  49. Algorithms[PkcsObjectIdentifiers.DesEde3Cbc.Id] = "DESEDEMAC";
  50. Algorithms["DESEDE/CFB8"] = "DESEDEMAC/CFB8";
  51. Algorithms["DESISO9797MAC"] = "DESWITHISO9797";
  52. Algorithms["DESEDE64"] = "DESEDEMAC64";
  53. Algorithms["DESEDE64WITHISO7816-4PADDING"] = "DESEDEMAC64WITHISO7816-4PADDING";
  54. Algorithms["DESEDEISO9797ALG1MACWITHISO7816-4PADDING"] = "DESEDEMAC64WITHISO7816-4PADDING";
  55. Algorithms["DESEDEISO9797ALG1WITHISO7816-4PADDING"] = "DESEDEMAC64WITHISO7816-4PADDING";
  56. Algorithms["ISO9797ALG3"] = "ISO9797ALG3MAC";
  57. Algorithms["ISO9797ALG3MACWITHISO7816-4PADDING"] = "ISO9797ALG3WITHISO7816-4PADDING";
  58. Algorithms["SKIPJACK"] = "SKIPJACKMAC";
  59. Algorithms["SKIPJACK/CFB8"] = "SKIPJACKMAC/CFB8";
  60. Algorithms["IDEA"] = "IDEAMAC";
  61. Algorithms["IDEA/CFB8"] = "IDEAMAC/CFB8";
  62. Algorithms["RC2"] = "RC2MAC";
  63. Algorithms["RC2/CFB8"] = "RC2MAC/CFB8";
  64. Algorithms["RC5"] = "RC5MAC";
  65. Algorithms["RC5/CFB8"] = "RC5MAC/CFB8";
  66. Algorithms["GOST28147"] = "GOST28147MAC";
  67. Algorithms["VMPC"] = "VMPCMAC";
  68. Algorithms["VMPC-MAC"] = "VMPCMAC";
  69. Algorithms["SIPHASH"] = "SIPHASH-2-4";
  70. Algorithms["PBEWITHHMACSHA"] = "PBEWITHHMACSHA1";
  71. Algorithms["1.3.14.3.2.26"] = "PBEWITHHMACSHA1";
  72. }
  73. public static IMac GetMac(DerObjectIdentifier id)
  74. {
  75. return GetMac(id.Id);
  76. }
  77. public static IMac GetMac(string algorithm)
  78. {
  79. if (algorithm == null)
  80. throw new ArgumentNullException(nameof(algorithm));
  81. string mechanism = CollectionUtilities.GetValueOrKey(Algorithms, algorithm).ToUpperInvariant();
  82. if (Org.BouncyCastle.Utilities.Platform.StartsWith(mechanism, "PBEWITH"))
  83. {
  84. mechanism = mechanism.Substring("PBEWITH".Length);
  85. }
  86. if (Org.BouncyCastle.Utilities.Platform.StartsWith(mechanism, "HMAC"))
  87. {
  88. string digestName;
  89. if (Org.BouncyCastle.Utilities.Platform.StartsWith(mechanism, "HMAC-") || Org.BouncyCastle.Utilities.Platform.StartsWith(mechanism, "HMAC/"))
  90. {
  91. digestName = mechanism.Substring(5);
  92. }
  93. else
  94. {
  95. digestName = mechanism.Substring(4);
  96. }
  97. return new HMac(DigestUtilities.GetDigest(digestName));
  98. }
  99. if (mechanism == "AESCMAC")
  100. {
  101. return new CMac(AesUtilities.CreateEngine());
  102. }
  103. if (mechanism == "DESMAC")
  104. {
  105. return new CbcBlockCipherMac(new DesEngine());
  106. }
  107. if (mechanism == "DESMAC/CFB8")
  108. {
  109. return new CfbBlockCipherMac(new DesEngine());
  110. }
  111. if (mechanism == "DESMAC64")
  112. {
  113. return new CbcBlockCipherMac(new DesEngine(), 64);
  114. }
  115. if (mechanism == "DESEDECMAC")
  116. {
  117. return new CMac(new DesEdeEngine());
  118. }
  119. if (mechanism == "DESEDEMAC")
  120. {
  121. return new CbcBlockCipherMac(new DesEdeEngine());
  122. }
  123. if (mechanism == "DESEDEMAC/CFB8")
  124. {
  125. return new CfbBlockCipherMac(new DesEdeEngine());
  126. }
  127. if (mechanism == "DESEDEMAC64")
  128. {
  129. return new CbcBlockCipherMac(new DesEdeEngine(), 64);
  130. }
  131. if (mechanism == "DESEDEMAC64WITHISO7816-4PADDING")
  132. {
  133. return new CbcBlockCipherMac(new DesEdeEngine(), 64, new ISO7816d4Padding());
  134. }
  135. if (mechanism == "DESWITHISO9797"
  136. || mechanism == "ISO9797ALG3MAC")
  137. {
  138. return new ISO9797Alg3Mac(new DesEngine());
  139. }
  140. if (mechanism == "ISO9797ALG3WITHISO7816-4PADDING")
  141. {
  142. return new ISO9797Alg3Mac(new DesEngine(), new ISO7816d4Padding());
  143. }
  144. if (mechanism == "SKIPJACKMAC")
  145. {
  146. return new CbcBlockCipherMac(new SkipjackEngine());
  147. }
  148. if (mechanism == "SKIPJACKMAC/CFB8")
  149. {
  150. return new CfbBlockCipherMac(new SkipjackEngine());
  151. }
  152. if (mechanism == "IDEAMAC")
  153. {
  154. return new CbcBlockCipherMac(new IdeaEngine());
  155. }
  156. if (mechanism == "IDEAMAC/CFB8")
  157. {
  158. return new CfbBlockCipherMac(new IdeaEngine());
  159. }
  160. if (mechanism == "RC2MAC")
  161. {
  162. return new CbcBlockCipherMac(new RC2Engine());
  163. }
  164. if (mechanism == "RC2MAC/CFB8")
  165. {
  166. return new CfbBlockCipherMac(new RC2Engine());
  167. }
  168. if (mechanism == "RC5MAC")
  169. {
  170. return new CbcBlockCipherMac(new RC532Engine());
  171. }
  172. if (mechanism == "RC5MAC/CFB8")
  173. {
  174. return new CfbBlockCipherMac(new RC532Engine());
  175. }
  176. if (mechanism == "GOST28147MAC")
  177. {
  178. return new Gost28147Mac();
  179. }
  180. if (mechanism == "VMPCMAC")
  181. {
  182. return new VmpcMac();
  183. }
  184. if (mechanism == "SIPHASH-2-4")
  185. {
  186. return new SipHash();
  187. }
  188. throw new SecurityUtilityException("Mac " + mechanism + " not recognised.");
  189. }
  190. public static string GetAlgorithmName(DerObjectIdentifier oid)
  191. {
  192. return CollectionUtilities.GetValueOrNull(Algorithms, oid.Id);
  193. }
  194. public static byte[] CalculateMac(string algorithm, ICipherParameters cp, byte[] input)
  195. {
  196. IMac mac = GetMac(algorithm);
  197. mac.Init(cp);
  198. mac.BlockUpdate(input, 0, input.Length);
  199. return DoFinal(mac);
  200. }
  201. public static byte[] DoFinal(IMac mac)
  202. {
  203. byte[] b = new byte[mac.GetMacSize()];
  204. mac.DoFinal(b, 0);
  205. return b;
  206. }
  207. public static byte[] DoFinal(IMac mac, byte[] input)
  208. {
  209. mac.BlockUpdate(input, 0, input.Length);
  210. return DoFinal(mac);
  211. }
  212. }
  213. }
  214. #pragma warning restore
  215. #endif