MacUtilities.cs 8.9 KB

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