GOST28147Mac.cs 7.2 KB

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