GOST28147Mac.cs 6.3 KB

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