GOST28147Mac.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Utilities;
  6. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  7. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Macs
  8. {
  9. /**
  10. * implementation of GOST 28147-89 MAC
  11. */
  12. public class Gost28147Mac
  13. : IMac
  14. {
  15. private const int BlockSize = 8;
  16. private const int MacSize = 4;
  17. private int bufOff;
  18. private byte[] buf;
  19. private byte[] mac;
  20. private bool firstStep = true;
  21. private int[] workingKey;
  22. private byte[] macIV = null;
  23. //
  24. // This is default S-box - E_A.
  25. private byte[] S =
  26. {
  27. 0x9,0x6,0x3,0x2,0x8,0xB,0x1,0x7,0xA,0x4,0xE,0xF,0xC,0x0,0xD,0x5,
  28. 0x3,0x7,0xE,0x9,0x8,0xA,0xF,0x0,0x5,0x2,0x6,0xC,0xB,0x4,0xD,0x1,
  29. 0xE,0x4,0x6,0x2,0xB,0x3,0xD,0x8,0xC,0xF,0x5,0xA,0x0,0x7,0x1,0x9,
  30. 0xE,0x7,0xA,0xC,0xD,0x1,0x3,0x9,0x0,0x2,0xB,0x4,0xF,0x8,0x5,0x6,
  31. 0xB,0x5,0x1,0x9,0x8,0xD,0xF,0x0,0xE,0x4,0x2,0x3,0xC,0x7,0xA,0x6,
  32. 0x3,0xA,0xD,0xC,0x1,0x2,0x0,0xB,0x7,0x5,0x9,0x4,0x8,0xF,0xE,0x6,
  33. 0x1,0xD,0x2,0x9,0x7,0xA,0x6,0x0,0x8,0xC,0x4,0x5,0xF,0x3,0xB,0xE,
  34. 0xB,0xA,0xF,0x5,0x0,0xC,0xE,0x8,0x6,0x2,0x3,0x9,0x1,0x7,0xD,0x4
  35. };
  36. public Gost28147Mac()
  37. {
  38. mac = new byte[BlockSize];
  39. buf = new byte[BlockSize];
  40. bufOff = 0;
  41. }
  42. private static int[] GenerateWorkingKey(
  43. byte[] userKey)
  44. {
  45. if (userKey.Length != 32)
  46. throw new ArgumentException("Key length invalid. Key needs to be 32 byte - 256 bit!!!");
  47. int[] key = new int[8];
  48. for(int i=0; i!=8; i++)
  49. {
  50. key[i] = (int)Pack.LE_To_UInt32(userKey, i * 4);
  51. }
  52. return key;
  53. }
  54. public void Init(ICipherParameters parameters)
  55. {
  56. Reset();
  57. buf = new byte[BlockSize];
  58. macIV = null;
  59. if (parameters is ParametersWithSBox param)
  60. {
  61. //
  62. // Set the S-Box
  63. //
  64. param.GetSBox().CopyTo(this.S, 0);
  65. //
  66. // set key if there is one
  67. //
  68. if (param.Parameters != null)
  69. {
  70. workingKey = GenerateWorkingKey(((KeyParameter)param.Parameters).GetKey());
  71. }
  72. }
  73. else if (parameters is KeyParameter keyParameter)
  74. {
  75. workingKey = GenerateWorkingKey(keyParameter.GetKey());
  76. }
  77. else if (parameters is ParametersWithIV ivParam)
  78. {
  79. workingKey = GenerateWorkingKey(((KeyParameter)ivParam.Parameters).GetKey());
  80. macIV = ivParam.GetIV(); // don't skip the initial CM5Func
  81. Array.Copy(macIV, 0, mac, 0, mac.Length);
  82. }
  83. else
  84. {
  85. throw new ArgumentException("invalid parameter passed to Gost28147 init - "
  86. + Org.BouncyCastle.Utilities.Platform.GetTypeName(parameters));
  87. }
  88. }
  89. public string AlgorithmName
  90. {
  91. get { return "Gost28147Mac"; }
  92. }
  93. public int GetMacSize()
  94. {
  95. return MacSize;
  96. }
  97. private int Gost28147_mainStep(int n1, int key)
  98. {
  99. int cm = (key + n1); // CM1
  100. // S-box replacing
  101. int om = S[ 0 + ((cm >> (0 * 4)) & 0xF)] << (0 * 4);
  102. om += S[ 16 + ((cm >> (1 * 4)) & 0xF)] << (1 * 4);
  103. om += S[ 32 + ((cm >> (2 * 4)) & 0xF)] << (2 * 4);
  104. om += S[ 48 + ((cm >> (3 * 4)) & 0xF)] << (3 * 4);
  105. om += S[ 64 + ((cm >> (4 * 4)) & 0xF)] << (4 * 4);
  106. om += S[ 80 + ((cm >> (5 * 4)) & 0xF)] << (5 * 4);
  107. om += S[ 96 + ((cm >> (6 * 4)) & 0xF)] << (6 * 4);
  108. om += S[112 + ((cm >> (7 * 4)) & 0xF)] << (7 * 4);
  109. // return om << 11 | om >>> (32-11); // 11-leftshift
  110. int omLeft = om << 11;
  111. int omRight = (int)(((uint) om) >> (32 - 11)); // Note: Casts required to get unsigned bit rotation
  112. return omLeft | omRight;
  113. }
  114. private void Gost28147MacFunc(
  115. int[] workingKey,
  116. byte[] input,
  117. int inOff,
  118. byte[] output,
  119. int outOff)
  120. {
  121. int N1 = (int)Pack.LE_To_UInt32(input, inOff);
  122. int N2 = (int)Pack.LE_To_UInt32(input, inOff + 4);
  123. int tmp; //tmp -> for saving N1
  124. for (int k = 0; k < 2; k++) // 1-16 steps
  125. {
  126. for (int j = 0; j < 8; j++)
  127. {
  128. tmp = N1;
  129. N1 = N2 ^ Gost28147_mainStep(N1, workingKey[j]); // CM2
  130. N2 = tmp;
  131. }
  132. }
  133. Pack.UInt32_To_LE((uint)N1, output, outOff);
  134. Pack.UInt32_To_LE((uint)N2, output, outOff + 4);
  135. }
  136. public void Update(byte input)
  137. {
  138. if (bufOff == buf.Length)
  139. {
  140. byte[] sum = new byte[buf.Length];
  141. if (firstStep)
  142. {
  143. firstStep = false;
  144. if (macIV != null)
  145. {
  146. Cm5Func(buf, 0, macIV, sum);
  147. }
  148. else
  149. {
  150. Array.Copy(buf, 0, sum, 0, mac.Length);
  151. }
  152. }
  153. else
  154. {
  155. Cm5Func(buf, 0, mac, sum);
  156. }
  157. Gost28147MacFunc(workingKey, sum, 0, mac, 0);
  158. bufOff = 0;
  159. }
  160. buf[bufOff++] = input;
  161. }
  162. public void BlockUpdate(byte[] input, int inOff, int len)
  163. {
  164. if (len < 0)
  165. throw new ArgumentException("Can't have a negative input length!");
  166. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  167. BlockUpdate(input.AsSpan(inOff, len));
  168. #else
  169. int gapLen = BlockSize - bufOff;
  170. if (len > gapLen)
  171. {
  172. Array.Copy(input, inOff, buf, bufOff, gapLen);
  173. byte[] sum = new byte[buf.Length];
  174. if (firstStep)
  175. {
  176. firstStep = false;
  177. if (macIV != null)
  178. {
  179. Cm5Func(buf, 0, macIV, sum);
  180. }
  181. else
  182. {
  183. Array.Copy(buf, 0, sum, 0, mac.Length);
  184. }
  185. }
  186. else
  187. {
  188. Cm5Func(buf, 0, mac, sum);
  189. }
  190. Gost28147MacFunc(workingKey, sum, 0, mac, 0);
  191. bufOff = 0;
  192. len -= gapLen;
  193. inOff += gapLen;
  194. while (len > BlockSize)
  195. {
  196. Cm5Func(input, inOff, mac, sum);
  197. Gost28147MacFunc(workingKey, sum, 0, mac, 0);
  198. len -= BlockSize;
  199. inOff += BlockSize;
  200. }
  201. }
  202. Array.Copy(input, inOff, buf, bufOff, len);
  203. bufOff += len;
  204. #endif
  205. }
  206. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  207. public void BlockUpdate(ReadOnlySpan<byte> input)
  208. {
  209. int gapLen = BlockSize - bufOff;
  210. if (input.Length > gapLen)
  211. {
  212. input[..gapLen].CopyTo(buf.AsSpan(bufOff));
  213. byte[] sum = new byte[buf.Length];
  214. if (firstStep)
  215. {
  216. firstStep = false;
  217. if (macIV != null)
  218. {
  219. Cm5Func(buf, macIV, sum);
  220. }
  221. else
  222. {
  223. Array.Copy(buf, 0, sum, 0, mac.Length);
  224. }
  225. }
  226. else
  227. {
  228. Cm5Func(buf, mac, sum);
  229. }
  230. Gost28147MacFunc(workingKey, sum, 0, mac, 0);
  231. bufOff = 0;
  232. input = input[gapLen..];
  233. while (input.Length > BlockSize)
  234. {
  235. Cm5Func(input, mac, sum);
  236. Gost28147MacFunc(workingKey, sum, 0, mac, 0);
  237. input = input[BlockSize..];
  238. }
  239. }
  240. input.CopyTo(buf.AsSpan(bufOff));
  241. bufOff += input.Length;
  242. }
  243. #endif
  244. public int DoFinal(byte[] output, int outOff)
  245. {
  246. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  247. return DoFinal(output.AsSpan(outOff));
  248. #else
  249. //padding with zero
  250. while (bufOff < BlockSize)
  251. {
  252. buf[bufOff++] = 0;
  253. }
  254. byte[] sum = new byte[buf.Length];
  255. if (firstStep)
  256. {
  257. firstStep = false;
  258. Array.Copy(buf, 0, sum, 0, mac.Length);
  259. }
  260. else
  261. {
  262. Cm5Func(buf, 0, mac, sum);
  263. }
  264. Gost28147MacFunc(workingKey, sum, 0, mac, 0);
  265. Array.Copy(mac, (mac.Length/2)-MacSize, output, outOff, MacSize);
  266. Reset();
  267. return MacSize;
  268. #endif
  269. }
  270. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  271. public int DoFinal(Span<byte> output)
  272. {
  273. //padding with zero
  274. while (bufOff < BlockSize)
  275. {
  276. buf[bufOff++] = 0;
  277. }
  278. byte[] sum = new byte[buf.Length];
  279. if (firstStep)
  280. {
  281. firstStep = false;
  282. Array.Copy(buf, 0, sum, 0, mac.Length);
  283. }
  284. else
  285. {
  286. Cm5Func(buf, 0, mac, sum);
  287. }
  288. Gost28147MacFunc(workingKey, sum, 0, mac, 0);
  289. mac.AsSpan((mac.Length / 2) - MacSize, MacSize).CopyTo(output);
  290. Reset();
  291. return MacSize;
  292. }
  293. #endif
  294. public void Reset()
  295. {
  296. // Clear the buffer.
  297. Array.Clear(buf, 0, buf.Length);
  298. bufOff = 0;
  299. firstStep = true;
  300. }
  301. private static void Cm5Func(byte[] buf, int bufOff, byte[] mac, byte[] sum)
  302. {
  303. for (int i = 0; i < BlockSize; ++i)
  304. {
  305. sum[i] = (byte)(buf[bufOff + i] ^ mac[i]);
  306. }
  307. }
  308. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  309. private static void Cm5Func(ReadOnlySpan<byte> buffer, ReadOnlySpan<byte> mac, Span<byte> sum)
  310. {
  311. for (int i = 0; i < BlockSize; ++i)
  312. {
  313. sum[i] = (byte)(buffer[i] ^ mac[i]);
  314. }
  315. }
  316. #endif
  317. }
  318. }
  319. #pragma warning restore
  320. #endif