GOST28147Engine.cs 13 KB

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