GOST28147Engine.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Collections;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  7. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Engines
  8. {
  9. /**
  10. * implementation of GOST 28147-89
  11. */
  12. public class Gost28147Engine
  13. : IBlockCipher
  14. {
  15. private const int BlockSize = 8;
  16. private int[] workingKey = null;
  17. private bool forEncryption;
  18. private byte[] S = Sbox_Default;
  19. // these are the S-boxes given in Applied Cryptography 2nd Ed., p. 333
  20. // This is default S-box!
  21. private static readonly byte[] Sbox_Default = {
  22. 0x4,0xA,0x9,0x2,0xD,0x8,0x0,0xE,0x6,0xB,0x1,0xC,0x7,0xF,0x5,0x3,
  23. 0xE,0xB,0x4,0xC,0x6,0xD,0xF,0xA,0x2,0x3,0x8,0x1,0x0,0x7,0x5,0x9,
  24. 0x5,0x8,0x1,0xD,0xA,0x3,0x4,0x2,0xE,0xF,0xC,0x7,0x6,0x0,0x9,0xB,
  25. 0x7,0xD,0xA,0x1,0x0,0x8,0x9,0xF,0xE,0x4,0x6,0xC,0xB,0x2,0x5,0x3,
  26. 0x6,0xC,0x7,0x1,0x5,0xF,0xD,0x8,0x4,0xA,0x9,0xE,0x0,0x3,0xB,0x2,
  27. 0x4,0xB,0xA,0x0,0x7,0x2,0x1,0xD,0x3,0x6,0x8,0x5,0x9,0xC,0xF,0xE,
  28. 0xD,0xB,0x4,0x1,0x3,0xF,0x5,0x9,0x0,0xA,0xE,0x7,0x6,0x8,0x2,0xC,
  29. 0x1,0xF,0xD,0x0,0x5,0x7,0xA,0x4,0x9,0x2,0x3,0xE,0x6,0xB,0x8,0xC
  30. };
  31. /*
  32. * class content S-box parameters for encrypting
  33. * getting from, see: http://tools.ietf.org/id/draft-popov-cryptopro-cpalgs-01.txt
  34. * http://tools.ietf.org/id/draft-popov-cryptopro-cpalgs-02.txt
  35. */
  36. private static readonly byte[] ESbox_Test = {
  37. 0x4,0x2,0xF,0x5,0x9,0x1,0x0,0x8,0xE,0x3,0xB,0xC,0xD,0x7,0xA,0x6,
  38. 0xC,0x9,0xF,0xE,0x8,0x1,0x3,0xA,0x2,0x7,0x4,0xD,0x6,0x0,0xB,0x5,
  39. 0xD,0x8,0xE,0xC,0x7,0x3,0x9,0xA,0x1,0x5,0x2,0x4,0x6,0xF,0x0,0xB,
  40. 0xE,0x9,0xB,0x2,0x5,0xF,0x7,0x1,0x0,0xD,0xC,0x6,0xA,0x4,0x3,0x8,
  41. 0x3,0xE,0x5,0x9,0x6,0x8,0x0,0xD,0xA,0xB,0x7,0xC,0x2,0x1,0xF,0x4,
  42. 0x8,0xF,0x6,0xB,0x1,0x9,0xC,0x5,0xD,0x3,0x7,0xA,0x0,0xE,0x2,0x4,
  43. 0x9,0xB,0xC,0x0,0x3,0x6,0x7,0x5,0x4,0x8,0xE,0xF,0x1,0xA,0x2,0xD,
  44. 0xC,0x6,0x5,0x2,0xB,0x0,0x9,0xD,0x3,0xE,0x7,0xA,0xF,0x4,0x1,0x8
  45. };
  46. private static readonly byte[] ESbox_A = {
  47. 0x9,0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5,0x4,0xE,0xF,0x1,
  82. 0x8,0x0,0xF,0x3,0x2,0x5,0xE,0xB,0x1,0xA,0x4,0x7,0xC,0x9,0xD,0x6,
  83. 0x3,0x0,0x6,0xF,0x1,0xE,0x9,0x2,0xD,0x8,0xC,0x4,0xB,0xA,0x5,0x7,
  84. 0x1,0xA,0x6,0x8,0xF,0xB,0x0,0x4,0xC,0x3,0x5,0x9,0x7,0xD,0x2,0xE
  85. };
  86. //S-box for digest
  87. private static readonly byte[] DSbox_Test = {
  88. 0x4,0xA,0x9,0x2,0xD,0x8,0x0,0xE,0x6,0xB,0x1,0xC,0x7,0xF,0x5,0x3,
  89. 0xE,0xB,0x4,0xC,0x6,0xD,0xF,0xA,0x2,0x3,0x8,0x1,0x0,0x7,0x5,0x9,
  90. 0x5,0x8,0x1,0xD,0xA,0x3,0x4,0x2,0xE,0xF,0xC,0x7,0x6,0x0,0x9,0xB,
  91. 0x7,0xD,0xA,0x1,0x0,0x8,0x9,0xF,0xE,0x4,0x6,0xC,0xB,0x2,0x5,0x3,
  92. 0x6,0xC,0x7,0x1,0x5,0xF,0xD,0x8,0x4,0xA,0x9,0xE,0x0,0x3,0xB,0x2,
  93. 0x4,0xB,0xA,0x0,0x7,0x2,0x1,0xD,0x3,0x6,0x8,0x5,0x9,0xC,0xF,0xE,
  94. 0xD,0xB,0x4,0x1,0x3,0xF,0x5,0x9,0x0,0xA,0xE,0x7,0x6,0x8,0x2,0xC,
  95. 0x1,0xF,0xD,0x0,0x5,0x7,0xA,0x4,0x9,0x2,0x3,0xE,0x6,0xB,0x8,0xC
  96. };
  97. private static readonly byte[] DSbox_A = {
  98. 0xA,0x4,0x5,0x6,0x8,0x1,0x3,0x7,0xD,0xC,0xE,0x0,0x9,0x2,0xB,0xF,
  99. 0x5,0xF,0x4,0x0,0x2,0xD,0xB,0x9,0x1,0x7,0x6,0x3,0xC,0xE,0xA,0x8,
  100. 0x7,0xF,0xC,0xE,0x9,0x4,0x1,0x0,0x3,0xB,0x5,0x2,0x6,0xA,0x8,0xD,
  101. 0x4,0xA,0x7,0xC,0x0,0xF,0x2,0x8,0xE,0x1,0x6,0x5,0xD,0xB,0x9,0x3,
  102. 0x7,0x6,0x4,0xB,0x9,0xC,0x2,0xA,0x1,0x8,0x0,0xE,0xF,0xD,0x3,0x5,
  103. 0x7,0x6,0x2,0x4,0xD,0x9,0xF,0x0,0xA,0x1,0x5,0xB,0x8,0xE,0xC,0x3,
  104. 0xD,0xE,0x4,0x1,0x7,0x0,0x5,0xA,0x3,0xC,0x8,0xF,0x6,0x2,0x9,0xB,
  105. 0x1,0x3,0xA,0x9,0x5,0xB,0x4,0xF,0x8,0x6,0x7,0xE,0xD,0x0,0x2,0xC
  106. };
  107. //
  108. // pre-defined sbox table
  109. //
  110. private static readonly IDictionary sBoxes = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateHashtable();
  111. static Gost28147Engine()
  112. {
  113. AddSBox("Default", Sbox_Default);
  114. AddSBox("E-TEST", ESbox_Test);
  115. AddSBox("E-A", ESbox_A);
  116. AddSBox("E-B", ESbox_B);
  117. AddSBox("E-C", ESbox_C);
  118. AddSBox("E-D", ESbox_D);
  119. AddSBox("D-TEST", DSbox_Test);
  120. AddSBox("D-A", DSbox_A);
  121. }
  122. private static void AddSBox(string sBoxName, byte[] sBox)
  123. {
  124. sBoxes.Add(BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.ToUpperInvariant(sBoxName), sBox);
  125. }
  126. /**
  127. * standard constructor.
  128. */
  129. public Gost28147Engine()
  130. {
  131. }
  132. /**
  133. * initialise an Gost28147 cipher.
  134. *
  135. * @param forEncryption whether or not we are for encryption.
  136. * @param parameters the parameters required to set up the cipher.
  137. * @exception ArgumentException if the parameters argument is inappropriate.
  138. */
  139. public virtual void Init(
  140. bool forEncryption,
  141. ICipherParameters parameters)
  142. {
  143. if (parameters is ParametersWithSBox)
  144. {
  145. ParametersWithSBox param = (ParametersWithSBox)parameters;
  146. //
  147. // Set the S-Box
  148. //
  149. byte[] sBox = param.GetSBox();
  150. if (sBox.Length != Sbox_Default.Length)
  151. throw new ArgumentException("invalid S-box passed to GOST28147 init");
  152. this.S = Arrays.Clone(sBox);
  153. //
  154. // set key if there is one
  155. //
  156. if (param.Parameters != null)
  157. {
  158. workingKey = generateWorkingKey(forEncryption,
  159. ((KeyParameter)param.Parameters).GetKey());
  160. }
  161. }
  162. else if (parameters is KeyParameter)
  163. {
  164. workingKey = generateWorkingKey(forEncryption,
  165. ((KeyParameter)parameters).GetKey());
  166. }
  167. else if (parameters != null)
  168. {
  169. throw new ArgumentException("invalid parameter passed to Gost28147 init - "
  170. + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(parameters));
  171. }
  172. }
  173. public virtual string AlgorithmName
  174. {
  175. get { return "Gost28147"; }
  176. }
  177. public virtual bool IsPartialBlockOkay
  178. {
  179. get { return false; }
  180. }
  181. public virtual int GetBlockSize()
  182. {
  183. return BlockSize;
  184. }
  185. public virtual int ProcessBlock(
  186. byte[] input,
  187. int inOff,
  188. byte[] output,
  189. int outOff)
  190. {
  191. if (workingKey == null)
  192. throw new InvalidOperationException("Gost28147 engine not initialised");
  193. Check.DataLength(input, inOff, BlockSize, "input buffer too short");
  194. Check.OutputLength(output, outOff, BlockSize, "output buffer too short");
  195. Gost28147Func(workingKey, input, inOff, output, outOff);
  196. return BlockSize;
  197. }
  198. public virtual void Reset()
  199. {
  200. }
  201. private int[] generateWorkingKey(
  202. bool forEncryption,
  203. byte[] userKey)
  204. {
  205. this.forEncryption = forEncryption;
  206. if (userKey.Length != 32)
  207. {
  208. throw new ArgumentException("Key length invalid. Key needs to be 32 byte - 256 bit!!!");
  209. }
  210. int[] key = new int[8];
  211. for(int i=0; i!=8; i++)
  212. {
  213. key[i] = bytesToint(userKey,i*4);
  214. }
  215. return key;
  216. }
  217. private int Gost28147_mainStep(int n1, int key)
  218. {
  219. int cm = (key + n1); // CM1
  220. // S-box replacing
  221. int om = S[ 0 + ((cm >> (0 * 4)) & 0xF)] << (0 * 4);
  222. om += S[ 16 + ((cm >> (1 * 4)) & 0xF)] << (1 * 4);
  223. om += S[ 32 + ((cm >> (2 * 4)) & 0xF)] << (2 * 4);
  224. om += S[ 48 + ((cm >> (3 * 4)) & 0xF)] << (3 * 4);
  225. om += S[ 64 + ((cm >> (4 * 4)) & 0xF)] << (4 * 4);
  226. om += S[ 80 + ((cm >> (5 * 4)) & 0xF)] << (5 * 4);
  227. om += S[ 96 + ((cm >> (6 * 4)) & 0xF)] << (6 * 4);
  228. om += S[112 + ((cm >> (7 * 4)) & 0xF)] << (7 * 4);
  229. // return om << 11 | om >>> (32-11); // 11-leftshift
  230. int omLeft = om << 11;
  231. int omRight = (int)(((uint) om) >> (32 - 11)); // Note: Casts required to get unsigned bit rotation
  232. return omLeft | omRight;
  233. }
  234. private void Gost28147Func(
  235. int[] workingKey,
  236. byte[] inBytes,
  237. int inOff,
  238. byte[] outBytes,
  239. int outOff)
  240. {
  241. int N1, N2, tmp; //tmp -> for saving N1
  242. N1 = bytesToint(inBytes, inOff);
  243. N2 = bytesToint(inBytes, inOff + 4);
  244. if (this.forEncryption)
  245. {
  246. for(int k = 0; k < 3; k++) // 1-24 steps
  247. {
  248. for(int j = 0; j < 8; j++)
  249. {
  250. tmp = N1;
  251. int step = Gost28147_mainStep(N1, workingKey[j]);
  252. N1 = N2 ^ step; // CM2
  253. N2 = tmp;
  254. }
  255. }
  256. for(int j = 7; j > 0; j--) // 25-31 steps
  257. {
  258. tmp = N1;
  259. N1 = N2 ^ Gost28147_mainStep(N1, workingKey[j]); // CM2
  260. N2 = tmp;
  261. }
  262. }
  263. else //decrypt
  264. {
  265. for(int j = 0; j < 8; j++) // 1-8 steps
  266. {
  267. tmp = N1;
  268. N1 = N2 ^ Gost28147_mainStep(N1, workingKey[j]); // CM2
  269. N2 = tmp;
  270. }
  271. for(int k = 0; k < 3; k++) //9-31 steps
  272. {
  273. for(int j = 7; j >= 0; j--)
  274. {
  275. if ((k == 2) && (j==0))
  276. {
  277. break; // break 32 step
  278. }
  279. tmp = N1;
  280. N1 = N2 ^ Gost28147_mainStep(N1, workingKey[j]); // CM2
  281. N2 = tmp;
  282. }
  283. }
  284. }
  285. N2 = N2 ^ Gost28147_mainStep(N1, workingKey[0]); // 32 step (N1=N1)
  286. intTobytes(N1, outBytes, outOff);
  287. intTobytes(N2, outBytes, outOff + 4);
  288. }
  289. //array of bytes to type int
  290. private static int bytesToint(
  291. byte[] inBytes,
  292. int inOff)
  293. {
  294. return (int)((inBytes[inOff + 3] << 24) & 0xff000000) + ((inBytes[inOff + 2] << 16) & 0xff0000) +
  295. ((inBytes[inOff + 1] << 8) & 0xff00) + (inBytes[inOff] & 0xff);
  296. }
  297. //int to array of bytes
  298. private static void intTobytes(
  299. int num,
  300. byte[] outBytes,
  301. int outOff)
  302. {
  303. outBytes[outOff + 3] = (byte)(num >> 24);
  304. outBytes[outOff + 2] = (byte)(num >> 16);
  305. outBytes[outOff + 1] = (byte)(num >> 8);
  306. outBytes[outOff] = (byte)num;
  307. }
  308. /**
  309. * Return the S-Box associated with SBoxName
  310. * @param sBoxName name of the S-Box
  311. * @return byte array representing the S-Box
  312. */
  313. public static byte[] GetSBox(
  314. string sBoxName)
  315. {
  316. byte[] sBox = (byte[])sBoxes[BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.ToUpperInvariant(sBoxName)];
  317. if (sBox == null)
  318. {
  319. throw new ArgumentException("Unknown S-Box - possible types: "
  320. + "\"Default\", \"E-Test\", \"E-A\", \"E-B\", \"E-C\", \"E-D\", \"D-Test\", \"D-A\".");
  321. }
  322. return Arrays.Clone(sBox);
  323. }
  324. public static string GetSBoxName(byte[] sBox)
  325. {
  326. foreach (string name in sBoxes.Keys)
  327. {
  328. byte[] sb = (byte[])sBoxes[name];
  329. if (Arrays.AreEqual(sb, sBox))
  330. {
  331. return name;
  332. }
  333. }
  334. throw new ArgumentException("SBOX provided did not map to a known one");
  335. }
  336. }
  337. }
  338. #pragma warning restore
  339. #endif