MacUtilities.cs 10.0 KB

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