RijndaelEngine.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. #if UNITY_WSA && !UNITY_EDITOR && !ENABLE_IL2CPP
  5. using System.TypeFix;
  6. #endif
  7. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  8. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  9. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Engines
  10. {
  11. /**
  12. * an implementation of Rijndael, based on the documentation and reference implementation
  13. * by Paulo Barreto, Vincent Rijmen, for v2.0 August '99.
  14. * <p>
  15. * Note: this implementation is based on information prior to readonly NIST publication.
  16. * </p>
  17. */
  18. public class RijndaelEngine
  19. : IBlockCipher
  20. {
  21. private static readonly int MAXROUNDS = 14;
  22. private static readonly int MAXKC = (256/4);
  23. private static readonly byte[] Logtable =
  24. {
  25. 0, 0, 25, 1, 50, 2, 26, 198,
  26. 75, 199, 27, 104, 51, 238, 223, 3,
  27. 100, 4, 224, 14, 52, 141, 129, 239,
  28. 76, 113, 8, 200, 248, 105, 28, 193,
  29. 125, 194, 29, 181, 249, 185, 39, 106,
  30. 77, 228, 166, 114, 154, 201, 9, 120,
  31. 101, 47, 138, 5, 33, 15, 225, 36,
  32. 18, 240, 130, 69, 53, 147, 218, 142,
  33. 150, 143, 219, 189, 54, 208, 206, 148,
  34. 19, 92, 210, 241, 64, 70, 131, 56,
  35. 102, 221, 253, 48, 191, 6, 139, 98,
  36. 179, 37, 226, 152, 34, 136, 145, 16,
  37. 126, 110, 72, 195, 163, 182, 30, 66,
  38. 58, 107, 40, 84, 250, 133, 61, 186,
  39. 43, 121, 10, 21, 155, 159, 94, 202,
  40. 78, 212, 172, 229, 243, 115, 167, 87,
  41. 175, 88, 168, 80, 244, 234, 214, 116,
  42. 79, 174, 233, 213, 231, 230, 173, 232,
  43. 44, 215, 117, 122, 235, 22, 11, 245,
  44. 89, 203, 95, 176, 156, 169, 81, 160,
  45. 127, 12, 246, 111, 23, 196, 73, 236,
  46. 216, 67, 31, 45, 164, 118, 123, 183,
  47. 204, 187, 62, 90, 251, 96, 177, 134,
  48. 59, 82, 161, 108, 170, 85, 41, 157,
  49. 151, 178, 135, 144, 97, 190, 220, 252,
  50. 188, 149, 207, 205, 55, 63, 91, 209,
  51. 83, 57, 132, 60, 65, 162, 109, 71,
  52. 20, 42, 158, 93, 86, 242, 211, 171,
  53. 68, 17, 146, 217, 35, 32, 46, 137,
  54. 180, 124, 184, 38, 119, 153, 227, 165,
  55. 103, 74, 237, 222, 197, 49, 254, 24,
  56. 13, 99, 140, 128, 192, 247, 112, 7
  57. };
  58. private static readonly byte[] Alogtable =
  59. {
  60. 0, 3, 5, 15, 17, 51, 85, 255, 26, 46, 114, 150, 161, 248, 19, 53,
  61. 95, 225, 56, 72, 216, 115, 149, 164, 247, 2, 6, 10, 30, 34, 102, 170,
  62. 229, 52, 92, 228, 55, 89, 235, 38, 106, 190, 217, 112, 144, 171, 230, 49,
  63. 83, 245, 4, 12, 20, 60, 68, 204, 79, 209, 104, 184, 211, 110, 178, 205,
  64. 76, 212, 103, 169, 224, 59, 77, 215, 98, 166, 241, 8, 24, 40, 120, 136,
  65. 131, 158, 185, 208, 107, 189, 220, 127, 129, 152, 179, 206, 73, 219, 118, 154,
  66. 181, 196, 87, 249, 16, 48, 80, 240, 11, 29, 39, 105, 187, 214, 97, 163,
  67. 254, 25, 43, 125, 135, 146, 173, 236, 47, 113, 147, 174, 233, 32, 96, 160,
  68. 251, 22, 58, 78, 210, 109, 183, 194, 93, 231, 50, 86, 250, 21, 63, 65,
  69. 195, 94, 226, 61, 71, 201, 64, 192, 91, 237, 44, 116, 156, 191, 218, 117,
  70. 159, 186, 213, 100, 172, 239, 42, 126, 130, 157, 188, 223, 122, 142, 137, 128,
  71. 155, 182, 193, 88, 232, 35, 101, 175, 234, 37, 111, 177, 200, 67, 197, 84,
  72. 252, 31, 33, 99, 165, 244, 7, 9, 27, 45, 119, 153, 176, 203, 70, 202,
  73. 69, 207, 74, 222, 121, 139, 134, 145, 168, 227, 62, 66, 198, 81, 243, 14,
  74. 18, 54, 90, 238, 41, 123, 141, 140, 143, 138, 133, 148, 167, 242, 13, 23,
  75. 57, 75, 221, 124, 132, 151, 162, 253, 28, 36, 108, 180, 199, 82, 246, 1,
  76. 3, 5, 15, 17, 51, 85, 255, 26, 46, 114, 150, 161, 248, 19, 53,
  77. 95, 225, 56, 72, 216, 115, 149, 164, 247, 2, 6, 10, 30, 34, 102, 170,
  78. 229, 52, 92, 228, 55, 89, 235, 38, 106, 190, 217, 112, 144, 171, 230, 49,
  79. 83, 245, 4, 12, 20, 60, 68, 204, 79, 209, 104, 184, 211, 110, 178, 205,
  80. 76, 212, 103, 169, 224, 59, 77, 215, 98, 166, 241, 8, 24, 40, 120, 136,
  81. 131, 158, 185, 208, 107, 189, 220, 127, 129, 152, 179, 206, 73, 219, 118, 154,
  82. 181, 196, 87, 249, 16, 48, 80, 240, 11, 29, 39, 105, 187, 214, 97, 163,
  83. 254, 25, 43, 125, 135, 146, 173, 236, 47, 113, 147, 174, 233, 32, 96, 160,
  84. 251, 22, 58, 78, 210, 109, 183, 194, 93, 231, 50, 86, 250, 21, 63, 65,
  85. 195, 94, 226, 61, 71, 201, 64, 192, 91, 237, 44, 116, 156, 191, 218, 117,
  86. 159, 186, 213, 100, 172, 239, 42, 126, 130, 157, 188, 223, 122, 142, 137, 128,
  87. 155, 182, 193, 88, 232, 35, 101, 175, 234, 37, 111, 177, 200, 67, 197, 84,
  88. 252, 31, 33, 99, 165, 244, 7, 9, 27, 45, 119, 153, 176, 203, 70, 202,
  89. 69, 207, 74, 222, 121, 139, 134, 145, 168, 227, 62, 66, 198, 81, 243, 14,
  90. 18, 54, 90, 238, 41, 123, 141, 140, 143, 138, 133, 148, 167, 242, 13, 23,
  91. 57, 75, 221, 124, 132, 151, 162, 253, 28, 36, 108, 180, 199, 82, 246, 1,
  92. };
  93. private static readonly byte[] S =
  94. {
  95. 99, 124, 119, 123, 242, 107, 111, 197, 48, 1, 103, 43, 254, 215, 171, 118,
  96. 202, 130, 201, 125, 250, 89, 71, 240, 173, 212, 162, 175, 156, 164, 114, 192,
  97. 183, 253, 147, 38, 54, 63, 247, 204, 52, 165, 229, 241, 113, 216, 49, 21,
  98. 4, 199, 35, 195, 24, 150, 5, 154, 7, 18, 128, 226, 235, 39, 178, 117,
  99. 9, 131, 44, 26, 27, 110, 90, 160, 82, 59, 214, 179, 41, 227, 47, 132,
  100. 83, 209, 0, 237, 32, 252, 177, 91, 106, 203, 190, 57, 74, 76, 88, 207,
  101. 208, 239, 170, 251, 67, 77, 51, 133, 69, 249, 2, 127, 80, 60, 159, 168,
  102. 81, 163, 64, 143, 146, 157, 56, 245, 188, 182, 218, 33, 16, 255, 243, 210,
  103. 205, 12, 19, 236, 95, 151, 68, 23, 196, 167, 126, 61, 100, 93, 25, 115,
  104. 96, 129, 79, 220, 34, 42, 144, 136, 70, 238, 184, 20, 222, 94, 11, 219,
  105. 224, 50, 58, 10, 73, 6, 36, 92, 194, 211, 172, 98, 145, 149, 228, 121,
  106. 231, 200, 55, 109, 141, 213, 78, 169, 108, 86, 244, 234, 101, 122, 174, 8,
  107. 186, 120, 37, 46, 28, 166, 180, 198, 232, 221, 116, 31, 75, 189, 139, 138,
  108. 112, 62, 181, 102, 72, 3, 246, 14, 97, 53, 87, 185, 134, 193, 29, 158,
  109. 225, 248, 152, 17, 105, 217, 142, 148, 155, 30, 135, 233, 206, 85, 40, 223,
  110. 140, 161, 137, 13, 191, 230, 66, 104, 65, 153, 45, 15, 176, 84, 187, 22,
  111. };
  112. private static readonly byte[] Si =
  113. {
  114. 82, 9, 106, 213, 48, 54, 165, 56, 191, 64, 163, 158, 129, 243, 215, 251,
  115. 124, 227, 57, 130, 155, 47, 255, 135, 52, 142, 67, 68, 196, 222, 233, 203,
  116. 84, 123, 148, 50, 166, 194, 35, 61, 238, 76, 149, 11, 66, 250, 195, 78,
  117. 8, 46, 161, 102, 40, 217, 36, 178, 118, 91, 162, 73, 109, 139, 209, 37,
  118. 114, 248, 246, 100, 134, 104, 152, 22, 212, 164, 92, 204, 93, 101, 182, 146,
  119. 108, 112, 72, 80, 253, 237, 185, 218, 94, 21, 70, 87, 167, 141, 157, 132,
  120. 144, 216, 171, 0, 140, 188, 211, 10, 247, 228, 88, 5, 184, 179, 69, 6,
  121. 208, 44, 30, 143, 202, 63, 15, 2, 193, 175, 189, 3, 1, 19, 138, 107,
  122. 58, 145, 17, 65, 79, 103, 220, 234, 151, 242, 207, 206, 240, 180, 230, 115,
  123. 150, 172, 116, 34, 231, 173, 53, 133, 226, 249, 55, 232, 28, 117, 223, 110,
  124. 71, 241, 26, 113, 29, 41, 197, 137, 111, 183, 98, 14, 170, 24, 190, 27,
  125. 252, 86, 62, 75, 198, 210, 121, 32, 154, 219, 192, 254, 120, 205, 90, 244,
  126. 31, 221, 168, 51, 136, 7, 199, 49, 177, 18, 16, 89, 39, 128, 236, 95,
  127. 96, 81, 127, 169, 25, 181, 74, 13, 45, 229, 122, 159, 147, 201, 156, 239,
  128. 160, 224, 59, 77, 174, 42, 245, 176, 200, 235, 187, 60, 131, 83, 153, 97,
  129. 23, 43, 4, 126, 186, 119, 214, 38, 225, 105, 20, 99, 85, 33, 12, 125,
  130. };
  131. private static readonly byte[] rcon =
  132. {
  133. 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a,
  134. 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91
  135. };
  136. static readonly byte[][] shifts0 = new byte [][]
  137. {
  138. new byte[]{ 0, 8, 16, 24 },
  139. new byte[]{ 0, 8, 16, 24 },
  140. new byte[]{ 0, 8, 16, 24 },
  141. new byte[]{ 0, 8, 16, 32 },
  142. new byte[]{ 0, 8, 24, 32 }
  143. };
  144. static readonly byte[][] shifts1 =
  145. {
  146. new byte[]{ 0, 24, 16, 8 },
  147. new byte[]{ 0, 32, 24, 16 },
  148. new byte[]{ 0, 40, 32, 24 },
  149. new byte[]{ 0, 48, 40, 24 },
  150. new byte[]{ 0, 56, 40, 32 }
  151. };
  152. /**
  153. * multiply two elements of GF(2^m)
  154. * needed for MixColumn and InvMixColumn
  155. */
  156. private byte Mul0x2(
  157. int b)
  158. {
  159. if (b != 0)
  160. {
  161. return Alogtable[25 + (Logtable[b] & 0xff)];
  162. }
  163. else
  164. {
  165. return 0;
  166. }
  167. }
  168. private byte Mul0x3(
  169. int b)
  170. {
  171. if (b != 0)
  172. {
  173. return Alogtable[1 + (Logtable[b] & 0xff)];
  174. }
  175. else
  176. {
  177. return 0;
  178. }
  179. }
  180. private byte Mul0x9(
  181. int b)
  182. {
  183. if (b >= 0)
  184. {
  185. return Alogtable[199 + b];
  186. }
  187. else
  188. {
  189. return 0;
  190. }
  191. }
  192. private byte Mul0xb(
  193. int b)
  194. {
  195. if (b >= 0)
  196. {
  197. return Alogtable[104 + b];
  198. }
  199. else
  200. {
  201. return 0;
  202. }
  203. }
  204. private byte Mul0xd(
  205. int b)
  206. {
  207. if (b >= 0)
  208. {
  209. return Alogtable[238 + b];
  210. }
  211. else
  212. {
  213. return 0;
  214. }
  215. }
  216. private byte Mul0xe(
  217. int b)
  218. {
  219. if (b >= 0)
  220. {
  221. return Alogtable[223 + b];
  222. }
  223. else
  224. {
  225. return 0;
  226. }
  227. }
  228. /**
  229. * xor corresponding text input and round key input bytes
  230. */
  231. private void KeyAddition(
  232. long[] rk)
  233. {
  234. A0 ^= rk[0];
  235. A1 ^= rk[1];
  236. A2 ^= rk[2];
  237. A3 ^= rk[3];
  238. }
  239. private long Shift(
  240. long r,
  241. int shift)
  242. {
  243. //return (((long)((ulong) r >> shift) | (r << (BC - shift)))) & BC_MASK;
  244. ulong temp = (ulong) r >> shift;
  245. // NB: This corrects for Mono Bug #79087 (fixed in 1.1.17)
  246. if (shift > 31)
  247. {
  248. temp &= 0xFFFFFFFFUL;
  249. }
  250. return ((long) temp | (r << (BC - shift))) & BC_MASK;
  251. }
  252. /**
  253. * Row 0 remains unchanged
  254. * The other three rows are shifted a variable amount
  255. */
  256. private void ShiftRow(
  257. byte[] shiftsSC)
  258. {
  259. A1 = Shift(A1, shiftsSC[1]);
  260. A2 = Shift(A2, shiftsSC[2]);
  261. A3 = Shift(A3, shiftsSC[3]);
  262. }
  263. private long ApplyS(
  264. long r,
  265. byte[] box)
  266. {
  267. long res = 0;
  268. for (int j = 0; j < BC; j += 8)
  269. {
  270. res |= (long)(box[(int)((r >> j) & 0xff)] & 0xff) << j;
  271. }
  272. return res;
  273. }
  274. /**
  275. * Replace every byte of the input by the byte at that place
  276. * in the nonlinear S-box
  277. */
  278. private void Substitution(
  279. byte[] box)
  280. {
  281. A0 = ApplyS(A0, box);
  282. A1 = ApplyS(A1, box);
  283. A2 = ApplyS(A2, box);
  284. A3 = ApplyS(A3, box);
  285. }
  286. /**
  287. * Mix the bytes of every column in a linear way
  288. */
  289. private void MixColumn()
  290. {
  291. long r0, r1, r2, r3;
  292. r0 = r1 = r2 = r3 = 0;
  293. for (int j = 0; j < BC; j += 8)
  294. {
  295. int a0 = (int)((A0 >> j) & 0xff);
  296. int a1 = (int)((A1 >> j) & 0xff);
  297. int a2 = (int)((A2 >> j) & 0xff);
  298. int a3 = (int)((A3 >> j) & 0xff);
  299. r0 |= (long)((Mul0x2(a0) ^ Mul0x3(a1) ^ a2 ^ a3) & 0xff) << j;
  300. r1 |= (long)((Mul0x2(a1) ^ Mul0x3(a2) ^ a3 ^ a0) & 0xff) << j;
  301. r2 |= (long)((Mul0x2(a2) ^ Mul0x3(a3) ^ a0 ^ a1) & 0xff) << j;
  302. r3 |= (long)((Mul0x2(a3) ^ Mul0x3(a0) ^ a1 ^ a2) & 0xff) << j;
  303. }
  304. A0 = r0;
  305. A1 = r1;
  306. A2 = r2;
  307. A3 = r3;
  308. }
  309. /**
  310. * Mix the bytes of every column in a linear way
  311. * This is the opposite operation of Mixcolumn
  312. */
  313. private void InvMixColumn()
  314. {
  315. long r0, r1, r2, r3;
  316. r0 = r1 = r2 = r3 = 0;
  317. for (int j = 0; j < BC; j += 8)
  318. {
  319. int a0 = (int)((A0 >> j) & 0xff);
  320. int a1 = (int)((A1 >> j) & 0xff);
  321. int a2 = (int)((A2 >> j) & 0xff);
  322. int a3 = (int)((A3 >> j) & 0xff);
  323. //
  324. // pre-lookup the log table
  325. //
  326. a0 = (a0 != 0) ? (Logtable[a0 & 0xff] & 0xff) : -1;
  327. a1 = (a1 != 0) ? (Logtable[a1 & 0xff] & 0xff) : -1;
  328. a2 = (a2 != 0) ? (Logtable[a2 & 0xff] & 0xff) : -1;
  329. a3 = (a3 != 0) ? (Logtable[a3 & 0xff] & 0xff) : -1;
  330. r0 |= (long)((Mul0xe(a0) ^ Mul0xb(a1) ^ Mul0xd(a2) ^ Mul0x9(a3)) & 0xff) << j;
  331. r1 |= (long)((Mul0xe(a1) ^ Mul0xb(a2) ^ Mul0xd(a3) ^ Mul0x9(a0)) & 0xff) << j;
  332. r2 |= (long)((Mul0xe(a2) ^ Mul0xb(a3) ^ Mul0xd(a0) ^ Mul0x9(a1)) & 0xff) << j;
  333. r3 |= (long)((Mul0xe(a3) ^ Mul0xb(a0) ^ Mul0xd(a1) ^ Mul0x9(a2)) & 0xff) << j;
  334. }
  335. A0 = r0;
  336. A1 = r1;
  337. A2 = r2;
  338. A3 = r3;
  339. }
  340. /**
  341. * Calculate the necessary round keys
  342. * The number of calculations depends on keyBits and blockBits
  343. */
  344. private long[][] GenerateWorkingKey(
  345. byte[] key)
  346. {
  347. int KC;
  348. int t, rconpointer = 0;
  349. int keyBits = key.Length * 8;
  350. byte[,] tk = new byte[4,MAXKC];
  351. //long[,] W = new long[MAXROUNDS+1,4];
  352. long[][] W = new long[MAXROUNDS+1][];
  353. for (int i = 0; i < MAXROUNDS+1; i++) W[i] = new long[4];
  354. switch (keyBits)
  355. {
  356. case 128:
  357. KC = 4;
  358. break;
  359. case 160:
  360. KC = 5;
  361. break;
  362. case 192:
  363. KC = 6;
  364. break;
  365. case 224:
  366. KC = 7;
  367. break;
  368. case 256:
  369. KC = 8;
  370. break;
  371. default :
  372. throw new ArgumentException("Key length not 128/160/192/224/256 bits.");
  373. }
  374. if (keyBits >= blockBits)
  375. {
  376. ROUNDS = KC + 6;
  377. }
  378. else
  379. {
  380. ROUNDS = (BC / 8) + 6;
  381. }
  382. //
  383. // copy the key into the processing area
  384. //
  385. int index = 0;
  386. for (int i = 0; i < key.Length; i++)
  387. {
  388. tk[i % 4,i / 4] = key[index++];
  389. }
  390. t = 0;
  391. //
  392. // copy values into round key array
  393. //
  394. for (int j = 0; (j < KC) && (t < (ROUNDS+1)*(BC / 8)); j++, t++)
  395. {
  396. for (int i = 0; i < 4; i++)
  397. {
  398. W[t / (BC / 8)][i] |= (long)(tk[i,j] & 0xff) << ((t * 8) % BC);
  399. }
  400. }
  401. //
  402. // while not enough round key material calculated
  403. // calculate new values
  404. //
  405. while (t < (ROUNDS+1)*(BC/8))
  406. {
  407. for (int i = 0; i < 4; i++)
  408. {
  409. tk[i,0] ^= S[tk[(i+1)%4,KC-1] & 0xff];
  410. }
  411. tk[0,0] ^= (byte) rcon[rconpointer++];
  412. if (KC <= 6)
  413. {
  414. for (int j = 1; j < KC; j++)
  415. {
  416. for (int i = 0; i < 4; i++)
  417. {
  418. tk[i,j] ^= tk[i,j-1];
  419. }
  420. }
  421. }
  422. else
  423. {
  424. for (int j = 1; j < 4; j++)
  425. {
  426. for (int i = 0; i < 4; i++)
  427. {
  428. tk[i,j] ^= tk[i,j-1];
  429. }
  430. }
  431. for (int i = 0; i < 4; i++)
  432. {
  433. tk[i,4] ^= S[tk[i,3] & 0xff];
  434. }
  435. for (int j = 5; j < KC; j++)
  436. {
  437. for (int i = 0; i < 4; i++)
  438. {
  439. tk[i,j] ^= tk[i,j-1];
  440. }
  441. }
  442. }
  443. //
  444. // copy values into round key array
  445. //
  446. for (int j = 0; (j < KC) && (t < (ROUNDS+1)*(BC/8)); j++, t++)
  447. {
  448. for (int i = 0; i < 4; i++)
  449. {
  450. W[t / (BC/8)][i] |= (long)(tk[i,j] & 0xff) << ((t * 8) % (BC));
  451. }
  452. }
  453. }
  454. return W;
  455. }
  456. private int BC;
  457. private long BC_MASK;
  458. private int ROUNDS;
  459. private int blockBits;
  460. private long[][] workingKey;
  461. private long A0, A1, A2, A3;
  462. private bool forEncryption;
  463. private byte[] shifts0SC;
  464. private byte[] shifts1SC;
  465. /**
  466. * default constructor - 128 bit block size.
  467. */
  468. public RijndaelEngine() : this(128) {}
  469. /**
  470. * basic constructor - set the cipher up for a given blocksize
  471. *
  472. * @param blocksize the blocksize in bits, must be 128, 192, or 256.
  473. */
  474. public RijndaelEngine(
  475. int blockBits)
  476. {
  477. switch (blockBits)
  478. {
  479. case 128:
  480. BC = 32;
  481. BC_MASK = 0xffffffffL;
  482. shifts0SC = shifts0[0];
  483. shifts1SC = shifts1[0];
  484. break;
  485. case 160:
  486. BC = 40;
  487. BC_MASK = 0xffffffffffL;
  488. shifts0SC = shifts0[1];
  489. shifts1SC = shifts1[1];
  490. break;
  491. case 192:
  492. BC = 48;
  493. BC_MASK = 0xffffffffffffL;
  494. shifts0SC = shifts0[2];
  495. shifts1SC = shifts1[2];
  496. break;
  497. case 224:
  498. BC = 56;
  499. BC_MASK = 0xffffffffffffffL;
  500. shifts0SC = shifts0[3];
  501. shifts1SC = shifts1[3];
  502. break;
  503. case 256:
  504. BC = 64;
  505. BC_MASK = unchecked( (long)0xffffffffffffffffL);
  506. shifts0SC = shifts0[4];
  507. shifts1SC = shifts1[4];
  508. break;
  509. default:
  510. throw new ArgumentException("unknown blocksize to Rijndael");
  511. }
  512. this.blockBits = blockBits;
  513. }
  514. /**
  515. * initialise a Rijndael cipher.
  516. *
  517. * @param forEncryption whether or not we are for encryption.
  518. * @param parameters the parameters required to set up the cipher.
  519. * @exception ArgumentException if the parameters argument is
  520. * inappropriate.
  521. */
  522. public virtual void Init(
  523. bool forEncryption,
  524. ICipherParameters parameters)
  525. {
  526. if (typeof(KeyParameter).IsInstanceOfType(parameters))
  527. {
  528. workingKey = GenerateWorkingKey(((KeyParameter)parameters).GetKey());
  529. this.forEncryption = forEncryption;
  530. return;
  531. }
  532. throw new ArgumentException("invalid parameter passed to Rijndael init - " + Org.BouncyCastle.Utilities.Platform.GetTypeName(parameters));
  533. }
  534. public virtual string AlgorithmName
  535. {
  536. get { return "Rijndael"; }
  537. }
  538. public virtual int GetBlockSize()
  539. {
  540. return BC / 2;
  541. }
  542. public virtual int ProcessBlock(byte[] input, int inOff, byte[] output, int outOff)
  543. {
  544. if (workingKey == null)
  545. throw new InvalidOperationException("Rijndael engine not initialised");
  546. Check.DataLength(input, inOff, (BC / 2), "input buffer too short");
  547. Check.OutputLength(output, outOff, (BC / 2), "output buffer too short");
  548. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  549. UnPackBlock(input.AsSpan(inOff));
  550. #else
  551. UnPackBlock(input, inOff);
  552. #endif
  553. if (forEncryption)
  554. {
  555. EncryptBlock(workingKey);
  556. }
  557. else
  558. {
  559. DecryptBlock(workingKey);
  560. }
  561. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  562. PackBlock(output.AsSpan(outOff));
  563. #else
  564. PackBlock(output, outOff);
  565. #endif
  566. return BC / 2;
  567. }
  568. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  569. public virtual int ProcessBlock(ReadOnlySpan<byte> input, Span<byte> output)
  570. {
  571. if (workingKey == null)
  572. throw new InvalidOperationException("Rijndael engine not initialised");
  573. Check.DataLength(input, (BC / 2), "input buffer too short");
  574. Check.OutputLength(output, (BC / 2), "output buffer too short");
  575. UnPackBlock(input);
  576. if (forEncryption)
  577. {
  578. EncryptBlock(workingKey);
  579. }
  580. else
  581. {
  582. DecryptBlock(workingKey);
  583. }
  584. PackBlock(output);
  585. return BC / 2;
  586. }
  587. #endif
  588. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  589. private void UnPackBlock(ReadOnlySpan<byte> input)
  590. {
  591. int index = 0;
  592. A0 = (long)(input[index++] & 0xff);
  593. A1 = (long)(input[index++] & 0xff);
  594. A2 = (long)(input[index++] & 0xff);
  595. A3 = (long)(input[index++] & 0xff);
  596. for (int j = 8; j != BC; j += 8)
  597. {
  598. A0 |= (long)(input[index++] & 0xff) << j;
  599. A1 |= (long)(input[index++] & 0xff) << j;
  600. A2 |= (long)(input[index++] & 0xff) << j;
  601. A3 |= (long)(input[index++] & 0xff) << j;
  602. }
  603. }
  604. private void PackBlock(Span<byte> output)
  605. {
  606. int index = 0;
  607. for (int j = 0; j != BC; j += 8)
  608. {
  609. output[index++] = (byte)(A0 >> j);
  610. output[index++] = (byte)(A1 >> j);
  611. output[index++] = (byte)(A2 >> j);
  612. output[index++] = (byte)(A3 >> j);
  613. }
  614. }
  615. #else
  616. private void UnPackBlock(byte[] bytes, int off)
  617. {
  618. int index = off;
  619. A0 = (long)(bytes[index++] & 0xff);
  620. A1 = (long)(bytes[index++] & 0xff);
  621. A2 = (long)(bytes[index++] & 0xff);
  622. A3 = (long)(bytes[index++] & 0xff);
  623. for (int j = 8; j != BC; j += 8)
  624. {
  625. A0 |= (long)(bytes[index++] & 0xff) << j;
  626. A1 |= (long)(bytes[index++] & 0xff) << j;
  627. A2 |= (long)(bytes[index++] & 0xff) << j;
  628. A3 |= (long)(bytes[index++] & 0xff) << j;
  629. }
  630. }
  631. private void PackBlock(byte[] bytes, int off)
  632. {
  633. int index = off;
  634. for (int j = 0; j != BC; j += 8)
  635. {
  636. bytes[index++] = (byte)(A0 >> j);
  637. bytes[index++] = (byte)(A1 >> j);
  638. bytes[index++] = (byte)(A2 >> j);
  639. bytes[index++] = (byte)(A3 >> j);
  640. }
  641. }
  642. #endif
  643. private void EncryptBlock(
  644. long[][] rk)
  645. {
  646. int r;
  647. //
  648. // begin with a key addition
  649. //
  650. KeyAddition(rk[0]);
  651. //
  652. // ROUNDS-1 ordinary rounds
  653. //
  654. for (r = 1; r < ROUNDS; r++)
  655. {
  656. Substitution(S);
  657. ShiftRow(shifts0SC);
  658. MixColumn();
  659. KeyAddition(rk[r]);
  660. }
  661. //
  662. // Last round is special: there is no MixColumn
  663. //
  664. Substitution(S);
  665. ShiftRow(shifts0SC);
  666. KeyAddition(rk[ROUNDS]);
  667. }
  668. private void DecryptBlock(
  669. long[][] rk)
  670. {
  671. int r;
  672. // To decrypt: apply the inverse operations of the encrypt routine,
  673. // in opposite order
  674. //
  675. // (KeyAddition is an involution: it 's equal to its inverse)
  676. // (the inverse of Substitution with table S is Substitution with the inverse table of S)
  677. // (the inverse of Shiftrow is Shiftrow over a suitable distance)
  678. //
  679. // First the special round:
  680. // without InvMixColumn
  681. // with extra KeyAddition
  682. //
  683. KeyAddition(rk[ROUNDS]);
  684. Substitution(Si);
  685. ShiftRow(shifts1SC);
  686. //
  687. // ROUNDS-1 ordinary rounds
  688. //
  689. for (r = ROUNDS-1; r > 0; r--)
  690. {
  691. KeyAddition(rk[r]);
  692. InvMixColumn();
  693. Substitution(Si);
  694. ShiftRow(shifts1SC);
  695. }
  696. //
  697. // End with the extra key addition
  698. //
  699. KeyAddition(rk[0]);
  700. }
  701. }
  702. }
  703. #pragma warning restore
  704. #endif