GOST28147Engine.cs 11 KB

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