RijndaelEngine.cs 19 KB

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