RijndaelEngine.cs 24 KB

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