AesEngine.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. using System.Diagnostics;
  4. using Org.BouncyCastle.Crypto.Parameters;
  5. using Org.BouncyCastle.Crypto.Utilities;
  6. using Org.BouncyCastle.Utilities;
  7. namespace Org.BouncyCastle.Crypto.Engines
  8. {
  9. /**
  10. * an implementation of the AES (Rijndael), from FIPS-197.
  11. * <p>
  12. * For further details see: <a href="http://csrc.nist.gov/encryption/aes/">http://csrc.nist.gov/encryption/aes/</a>.
  13. *
  14. * This implementation is based on optimizations from Dr. Brian Gladman's paper and C code at
  15. * <a href="http://fp.gladman.plus.com/cryptography_technology/rijndael/">http://fp.gladman.plus.com/cryptography_technology/rijndael/</a>
  16. *
  17. * There are three levels of tradeoff of speed vs memory
  18. * Because java has no preprocessor, they are written as three separate classes from which to choose
  19. *
  20. * The fastest uses 8Kbytes of static tables to precompute round calculations, 4 256 word tables for encryption
  21. * and 4 for decryption.
  22. *
  23. * The middle performance version uses only one 256 word table for each, for a total of 2Kbytes,
  24. * adding 12 rotate operations per round to compute the values contained in the other tables from
  25. * the contents of the first.
  26. *
  27. * The slowest version uses no static tables at all and computes the values in each round.
  28. * </p>
  29. * <p>
  30. * This file contains the middle performance version with 2Kbytes of static tables for round precomputation.
  31. * </p>
  32. */
  33. public class AesEngine
  34. : IBlockCipher
  35. {
  36. // The S box
  37. private static readonly byte[] S =
  38. {
  39. 99, 124, 119, 123, 242, 107, 111, 197,
  40. 48, 1, 103, 43, 254, 215, 171, 118,
  41. 202, 130, 201, 125, 250, 89, 71, 240,
  42. 173, 212, 162, 175, 156, 164, 114, 192,
  43. 183, 253, 147, 38, 54, 63, 247, 204,
  44. 52, 165, 229, 241, 113, 216, 49, 21,
  45. 4, 199, 35, 195, 24, 150, 5, 154,
  46. 7, 18, 128, 226, 235, 39, 178, 117,
  47. 9, 131, 44, 26, 27, 110, 90, 160,
  48. 82, 59, 214, 179, 41, 227, 47, 132,
  49. 83, 209, 0, 237, 32, 252, 177, 91,
  50. 106, 203, 190, 57, 74, 76, 88, 207,
  51. 208, 239, 170, 251, 67, 77, 51, 133,
  52. 69, 249, 2, 127, 80, 60, 159, 168,
  53. 81, 163, 64, 143, 146, 157, 56, 245,
  54. 188, 182, 218, 33, 16, 255, 243, 210,
  55. 205, 12, 19, 236, 95, 151, 68, 23,
  56. 196, 167, 126, 61, 100, 93, 25, 115,
  57. 96, 129, 79, 220, 34, 42, 144, 136,
  58. 70, 238, 184, 20, 222, 94, 11, 219,
  59. 224, 50, 58, 10, 73, 6, 36, 92,
  60. 194, 211, 172, 98, 145, 149, 228, 121,
  61. 231, 200, 55, 109, 141, 213, 78, 169,
  62. 108, 86, 244, 234, 101, 122, 174, 8,
  63. 186, 120, 37, 46, 28, 166, 180, 198,
  64. 232, 221, 116, 31, 75, 189, 139, 138,
  65. 112, 62, 181, 102, 72, 3, 246, 14,
  66. 97, 53, 87, 185, 134, 193, 29, 158,
  67. 225, 248, 152, 17, 105, 217, 142, 148,
  68. 155, 30, 135, 233, 206, 85, 40, 223,
  69. 140, 161, 137, 13, 191, 230, 66, 104,
  70. 65, 153, 45, 15, 176, 84, 187, 22,
  71. };
  72. // The inverse S-box
  73. private static readonly byte[] Si =
  74. {
  75. 82, 9, 106, 213, 48, 54, 165, 56,
  76. 191, 64, 163, 158, 129, 243, 215, 251,
  77. 124, 227, 57, 130, 155, 47, 255, 135,
  78. 52, 142, 67, 68, 196, 222, 233, 203,
  79. 84, 123, 148, 50, 166, 194, 35, 61,
  80. 238, 76, 149, 11, 66, 250, 195, 78,
  81. 8, 46, 161, 102, 40, 217, 36, 178,
  82. 118, 91, 162, 73, 109, 139, 209, 37,
  83. 114, 248, 246, 100, 134, 104, 152, 22,
  84. 212, 164, 92, 204, 93, 101, 182, 146,
  85. 108, 112, 72, 80, 253, 237, 185, 218,
  86. 94, 21, 70, 87, 167, 141, 157, 132,
  87. 144, 216, 171, 0, 140, 188, 211, 10,
  88. 247, 228, 88, 5, 184, 179, 69, 6,
  89. 208, 44, 30, 143, 202, 63, 15, 2,
  90. 193, 175, 189, 3, 1, 19, 138, 107,
  91. 58, 145, 17, 65, 79, 103, 220, 234,
  92. 151, 242, 207, 206, 240, 180, 230, 115,
  93. 150, 172, 116, 34, 231, 173, 53, 133,
  94. 226, 249, 55, 232, 28, 117, 223, 110,
  95. 71, 241, 26, 113, 29, 41, 197, 137,
  96. 111, 183, 98, 14, 170, 24, 190, 27,
  97. 252, 86, 62, 75, 198, 210, 121, 32,
  98. 154, 219, 192, 254, 120, 205, 90, 244,
  99. 31, 221, 168, 51, 136, 7, 199, 49,
  100. 177, 18, 16, 89, 39, 128, 236, 95,
  101. 96, 81, 127, 169, 25, 181, 74, 13,
  102. 45, 229, 122, 159, 147, 201, 156, 239,
  103. 160, 224, 59, 77, 174, 42, 245, 176,
  104. 200, 235, 187, 60, 131, 83, 153, 97,
  105. 23, 43, 4, 126, 186, 119, 214, 38,
  106. 225, 105, 20, 99, 85, 33, 12, 125,
  107. };
  108. // vector used in calculating key schedule (powers of x in GF(256))
  109. private static readonly byte[] rcon =
  110. {
  111. 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a,
  112. 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91
  113. };
  114. // precomputation tables of calculations for rounds
  115. private static readonly uint[] T0 =
  116. {
  117. 0xa56363c6, 0x847c7cf8, 0x997777ee, 0x8d7b7bf6, 0x0df2f2ff,
  118. 0xbd6b6bd6, 0xb16f6fde, 0x54c5c591, 0x50303060, 0x03010102,
  119. 0xa96767ce, 0x7d2b2b56, 0x19fefee7, 0x62d7d7b5, 0xe6abab4d,
  120. 0x9a7676ec, 0x45caca8f, 0x9d82821f, 0x40c9c989, 0x877d7dfa,
  121. 0x15fafaef, 0xeb5959b2, 0xc947478e, 0x0bf0f0fb, 0xecadad41,
  122. 0x67d4d4b3, 0xfda2a25f, 0xeaafaf45, 0xbf9c9c23, 0xf7a4a453,
  123. 0x967272e4, 0x5bc0c09b, 0xc2b7b775, 0x1cfdfde1, 0xae93933d,
  124. 0x6a26264c, 0x5a36366c, 0x413f3f7e, 0x02f7f7f5, 0x4fcccc83,
  125. 0x5c343468, 0xf4a5a551, 0x34e5e5d1, 0x08f1f1f9, 0x937171e2,
  126. 0x73d8d8ab, 0x53313162, 0x3f15152a, 0x0c040408, 0x52c7c795,
  127. 0x65232346, 0x5ec3c39d, 0x28181830, 0xa1969637, 0x0f05050a,
  128. 0xb59a9a2f, 0x0907070e, 0x36121224, 0x9b80801b, 0x3de2e2df,
  129. 0x26ebebcd, 0x6927274e, 0xcdb2b27f, 0x9f7575ea, 0x1b090912,
  130. 0x9e83831d, 0x742c2c58, 0x2e1a1a34, 0x2d1b1b36, 0xb26e6edc,
  131. 0xee5a5ab4, 0xfba0a05b, 0xf65252a4, 0x4d3b3b76, 0x61d6d6b7,
  132. 0xceb3b37d, 0x7b292952, 0x3ee3e3dd, 0x712f2f5e, 0x97848413,
  133. 0xf55353a6, 0x68d1d1b9, 0x00000000, 0x2cededc1, 0x60202040,
  134. 0x1ffcfce3, 0xc8b1b179, 0xed5b5bb6, 0xbe6a6ad4, 0x46cbcb8d,
  135. 0xd9bebe67, 0x4b393972, 0xde4a4a94, 0xd44c4c98, 0xe85858b0,
  136. 0x4acfcf85, 0x6bd0d0bb, 0x2aefefc5, 0xe5aaaa4f, 0x16fbfbed,
  137. 0xc5434386, 0xd74d4d9a, 0x55333366, 0x94858511, 0xcf45458a,
  138. 0x10f9f9e9, 0x06020204, 0x817f7ffe, 0xf05050a0, 0x443c3c78,
  139. 0xba9f9f25, 0xe3a8a84b, 0xf35151a2, 0xfea3a35d, 0xc0404080,
  140. 0x8a8f8f05, 0xad92923f, 0xbc9d9d21, 0x48383870, 0x04f5f5f1,
  141. 0xdfbcbc63, 0xc1b6b677, 0x75dadaaf, 0x63212142, 0x30101020,
  142. 0x1affffe5, 0x0ef3f3fd, 0x6dd2d2bf, 0x4ccdcd81, 0x140c0c18,
  143. 0x35131326, 0x2fececc3, 0xe15f5fbe, 0xa2979735, 0xcc444488,
  144. 0x3917172e, 0x57c4c493, 0xf2a7a755, 0x827e7efc, 0x473d3d7a,
  145. 0xac6464c8, 0xe75d5dba, 0x2b191932, 0x957373e6, 0xa06060c0,
  146. 0x98818119, 0xd14f4f9e, 0x7fdcdca3, 0x66222244, 0x7e2a2a54,
  147. 0xab90903b, 0x8388880b, 0xca46468c, 0x29eeeec7, 0xd3b8b86b,
  148. 0x3c141428, 0x79dedea7, 0xe25e5ebc, 0x1d0b0b16, 0x76dbdbad,
  149. 0x3be0e0db, 0x56323264, 0x4e3a3a74, 0x1e0a0a14, 0xdb494992,
  150. 0x0a06060c, 0x6c242448, 0xe45c5cb8, 0x5dc2c29f, 0x6ed3d3bd,
  151. 0xefacac43, 0xa66262c4, 0xa8919139, 0xa4959531, 0x37e4e4d3,
  152. 0x8b7979f2, 0x32e7e7d5, 0x43c8c88b, 0x5937376e, 0xb76d6dda,
  153. 0x8c8d8d01, 0x64d5d5b1, 0xd24e4e9c, 0xe0a9a949, 0xb46c6cd8,
  154. 0xfa5656ac, 0x07f4f4f3, 0x25eaeacf, 0xaf6565ca, 0x8e7a7af4,
  155. 0xe9aeae47, 0x18080810, 0xd5baba6f, 0x887878f0, 0x6f25254a,
  156. 0x722e2e5c, 0x241c1c38, 0xf1a6a657, 0xc7b4b473, 0x51c6c697,
  157. 0x23e8e8cb, 0x7cdddda1, 0x9c7474e8, 0x211f1f3e, 0xdd4b4b96,
  158. 0xdcbdbd61, 0x868b8b0d, 0x858a8a0f, 0x907070e0, 0x423e3e7c,
  159. 0xc4b5b571, 0xaa6666cc, 0xd8484890, 0x05030306, 0x01f6f6f7,
  160. 0x120e0e1c, 0xa36161c2, 0x5f35356a, 0xf95757ae, 0xd0b9b969,
  161. 0x91868617, 0x58c1c199, 0x271d1d3a, 0xb99e9e27, 0x38e1e1d9,
  162. 0x13f8f8eb, 0xb398982b, 0x33111122, 0xbb6969d2, 0x70d9d9a9,
  163. 0x898e8e07, 0xa7949433, 0xb69b9b2d, 0x221e1e3c, 0x92878715,
  164. 0x20e9e9c9, 0x49cece87, 0xff5555aa, 0x78282850, 0x7adfdfa5,
  165. 0x8f8c8c03, 0xf8a1a159, 0x80898909, 0x170d0d1a, 0xdabfbf65,
  166. 0x31e6e6d7, 0xc6424284, 0xb86868d0, 0xc3414182, 0xb0999929,
  167. 0x772d2d5a, 0x110f0f1e, 0xcbb0b07b, 0xfc5454a8, 0xd6bbbb6d,
  168. 0x3a16162c
  169. };
  170. private static readonly uint[] Tinv0 =
  171. {
  172. 0x50a7f451, 0x5365417e, 0xc3a4171a, 0x965e273a, 0xcb6bab3b,
  173. 0xf1459d1f, 0xab58faac, 0x9303e34b, 0x55fa3020, 0xf66d76ad,
  174. 0x9176cc88, 0x254c02f5, 0xfcd7e54f, 0xd7cb2ac5, 0x80443526,
  175. 0x8fa362b5, 0x495ab1de, 0x671bba25, 0x980eea45, 0xe1c0fe5d,
  176. 0x02752fc3, 0x12f04c81, 0xa397468d, 0xc6f9d36b, 0xe75f8f03,
  177. 0x959c9215, 0xeb7a6dbf, 0xda595295, 0x2d83bed4, 0xd3217458,
  178. 0x2969e049, 0x44c8c98e, 0x6a89c275, 0x78798ef4, 0x6b3e5899,
  179. 0xdd71b927, 0xb64fe1be, 0x17ad88f0, 0x66ac20c9, 0xb43ace7d,
  180. 0x184adf63, 0x82311ae5, 0x60335197, 0x457f5362, 0xe07764b1,
  181. 0x84ae6bbb, 0x1ca081fe, 0x942b08f9, 0x58684870, 0x19fd458f,
  182. 0x876cde94, 0xb7f87b52, 0x23d373ab, 0xe2024b72, 0x578f1fe3,
  183. 0x2aab5566, 0x0728ebb2, 0x03c2b52f, 0x9a7bc586, 0xa50837d3,
  184. 0xf2872830, 0xb2a5bf23, 0xba6a0302, 0x5c8216ed, 0x2b1ccf8a,
  185. 0x92b479a7, 0xf0f207f3, 0xa1e2694e, 0xcdf4da65, 0xd5be0506,
  186. 0x1f6234d1, 0x8afea6c4, 0x9d532e34, 0xa055f3a2, 0x32e18a05,
  187. 0x75ebf6a4, 0x39ec830b, 0xaaef6040, 0x069f715e, 0x51106ebd,
  188. 0xf98a213e, 0x3d06dd96, 0xae053edd, 0x46bde64d, 0xb58d5491,
  189. 0x055dc471, 0x6fd40604, 0xff155060, 0x24fb9819, 0x97e9bdd6,
  190. 0xcc434089, 0x779ed967, 0xbd42e8b0, 0x888b8907, 0x385b19e7,
  191. 0xdbeec879, 0x470a7ca1, 0xe90f427c, 0xc91e84f8, 0x00000000,
  192. 0x83868009, 0x48ed2b32, 0xac70111e, 0x4e725a6c, 0xfbff0efd,
  193. 0x5638850f, 0x1ed5ae3d, 0x27392d36, 0x64d90f0a, 0x21a65c68,
  194. 0xd1545b9b, 0x3a2e3624, 0xb1670a0c, 0x0fe75793, 0xd296eeb4,
  195. 0x9e919b1b, 0x4fc5c080, 0xa220dc61, 0x694b775a, 0x161a121c,
  196. 0x0aba93e2, 0xe52aa0c0, 0x43e0223c, 0x1d171b12, 0x0b0d090e,
  197. 0xadc78bf2, 0xb9a8b62d, 0xc8a91e14, 0x8519f157, 0x4c0775af,
  198. 0xbbdd99ee, 0xfd607fa3, 0x9f2601f7, 0xbcf5725c, 0xc53b6644,
  199. 0x347efb5b, 0x7629438b, 0xdcc623cb, 0x68fcedb6, 0x63f1e4b8,
  200. 0xcadc31d7, 0x10856342, 0x40229713, 0x2011c684, 0x7d244a85,
  201. 0xf83dbbd2, 0x1132f9ae, 0x6da129c7, 0x4b2f9e1d, 0xf330b2dc,
  202. 0xec52860d, 0xd0e3c177, 0x6c16b32b, 0x99b970a9, 0xfa489411,
  203. 0x2264e947, 0xc48cfca8, 0x1a3ff0a0, 0xd82c7d56, 0xef903322,
  204. 0xc74e4987, 0xc1d138d9, 0xfea2ca8c, 0x360bd498, 0xcf81f5a6,
  205. 0x28de7aa5, 0x268eb7da, 0xa4bfad3f, 0xe49d3a2c, 0x0d927850,
  206. 0x9bcc5f6a, 0x62467e54, 0xc2138df6, 0xe8b8d890, 0x5ef7392e,
  207. 0xf5afc382, 0xbe805d9f, 0x7c93d069, 0xa92dd56f, 0xb31225cf,
  208. 0x3b99acc8, 0xa77d1810, 0x6e639ce8, 0x7bbb3bdb, 0x097826cd,
  209. 0xf418596e, 0x01b79aec, 0xa89a4f83, 0x656e95e6, 0x7ee6ffaa,
  210. 0x08cfbc21, 0xe6e815ef, 0xd99be7ba, 0xce366f4a, 0xd4099fea,
  211. 0xd67cb029, 0xafb2a431, 0x31233f2a, 0x3094a5c6, 0xc066a235,
  212. 0x37bc4e74, 0xa6ca82fc, 0xb0d090e0, 0x15d8a733, 0x4a9804f1,
  213. 0xf7daec41, 0x0e50cd7f, 0x2ff69117, 0x8dd64d76, 0x4db0ef43,
  214. 0x544daacc, 0xdf0496e4, 0xe3b5d19e, 0x1b886a4c, 0xb81f2cc1,
  215. 0x7f516546, 0x04ea5e9d, 0x5d358c01, 0x737487fa, 0x2e410bfb,
  216. 0x5a1d67b3, 0x52d2db92, 0x335610e9, 0x1347d66d, 0x8c61d79a,
  217. 0x7a0ca137, 0x8e14f859, 0x893c13eb, 0xee27a9ce, 0x35c961b7,
  218. 0xede51ce1, 0x3cb1477a, 0x59dfd29c, 0x3f73f255, 0x79ce1418,
  219. 0xbf37c773, 0xeacdf753, 0x5baafd5f, 0x146f3ddf, 0x86db4478,
  220. 0x81f3afca, 0x3ec468b9, 0x2c342438, 0x5f40a3c2, 0x72c31d16,
  221. 0x0c25e2bc, 0x8b493c28, 0x41950dff, 0x7101a839, 0xdeb30c08,
  222. 0x9ce4b4d8, 0x90c15664, 0x6184cb7b, 0x70b632d5, 0x745c6c48,
  223. 0x4257b8d0
  224. };
  225. private static uint Shift(uint r, int shift)
  226. {
  227. return (r >> shift) | (r << (32 - shift));
  228. }
  229. /* multiply four bytes in GF(2^8) by 'x' {02} in parallel */
  230. private const uint m1 = 0x80808080;
  231. private const uint m2 = 0x7f7f7f7f;
  232. private const uint m3 = 0x0000001b;
  233. private const uint m4 = 0xC0C0C0C0;
  234. private const uint m5 = 0x3f3f3f3f;
  235. private static uint FFmulX(uint x)
  236. {
  237. return ((x & m2) << 1) ^ (((x & m1) >> 7) * m3);
  238. }
  239. private static uint FFmulX2(uint x)
  240. {
  241. uint t0 = (x & m5) << 2;
  242. uint t1 = (x & m4);
  243. t1 ^= (t1 >> 1);
  244. return t0 ^ (t1 >> 2) ^ (t1 >> 5);
  245. }
  246. /*
  247. The following defines provide alternative definitions of FFmulX that might
  248. give improved performance if a fast 32-bit multiply is not available.
  249. private int FFmulX(int x) { int u = x & m1; u |= (u >> 1); return ((x & m2) << 1) ^ ((u >>> 3) | (u >>> 6)); }
  250. private static final int m4 = 0x1b1b1b1b;
  251. private int FFmulX(int x) { int u = x & m1; return ((x & m2) << 1) ^ ((u - (u >>> 7)) & m4); }
  252. */
  253. private static uint Inv_Mcol(uint x)
  254. {
  255. uint t0, t1;
  256. t0 = x;
  257. t1 = t0 ^ Shift(t0, 8);
  258. t0 ^= FFmulX(t1);
  259. t1 ^= FFmulX2(t0);
  260. t0 ^= t1 ^ Shift(t1, 16);
  261. return t0;
  262. }
  263. private static uint SubWord(uint x)
  264. {
  265. return (uint)S[x&255]
  266. | (((uint)S[(x>>8)&255]) << 8)
  267. | (((uint)S[(x>>16)&255]) << 16)
  268. | (((uint)S[(x>>24)&255]) << 24);
  269. }
  270. /**
  271. * Calculate the necessary round keys
  272. * The number of calculations depends on key size and block size
  273. * AES specified a fixed block size of 128 bits and key sizes 128/192/256 bits
  274. * This code is written assuming those are the only possible values
  275. */
  276. private uint[][] GenerateWorkingKey(byte[] key, bool forEncryption)
  277. {
  278. int keyLen = key.Length;
  279. if (keyLen < 16 || keyLen > 32 || (keyLen & 7) != 0)
  280. throw new ArgumentException("Key length not 128/192/256 bits.");
  281. int KC = keyLen >> 2;
  282. this.ROUNDS = KC + 6; // This is not always true for the generalized Rijndael that allows larger block sizes
  283. uint[][] W = new uint[ROUNDS + 1][]; // 4 words in a block
  284. for (int i = 0; i <= ROUNDS; ++i)
  285. {
  286. W[i] = new uint[4];
  287. }
  288. switch (KC)
  289. {
  290. case 4:
  291. {
  292. uint t0 = Pack.LE_To_UInt32(key, 0); W[0][0] = t0;
  293. uint t1 = Pack.LE_To_UInt32(key, 4); W[0][1] = t1;
  294. uint t2 = Pack.LE_To_UInt32(key, 8); W[0][2] = t2;
  295. uint t3 = Pack.LE_To_UInt32(key, 12); W[0][3] = t3;
  296. for (int i = 1; i <= 10; ++i)
  297. {
  298. uint u = SubWord(Shift(t3, 8)) ^ rcon[i - 1];
  299. t0 ^= u; W[i][0] = t0;
  300. t1 ^= t0; W[i][1] = t1;
  301. t2 ^= t1; W[i][2] = t2;
  302. t3 ^= t2; W[i][3] = t3;
  303. }
  304. break;
  305. }
  306. case 6:
  307. {
  308. uint t0 = Pack.LE_To_UInt32(key, 0); W[0][0] = t0;
  309. uint t1 = Pack.LE_To_UInt32(key, 4); W[0][1] = t1;
  310. uint t2 = Pack.LE_To_UInt32(key, 8); W[0][2] = t2;
  311. uint t3 = Pack.LE_To_UInt32(key, 12); W[0][3] = t3;
  312. uint t4 = Pack.LE_To_UInt32(key, 16); W[1][0] = t4;
  313. uint t5 = Pack.LE_To_UInt32(key, 20); W[1][1] = t5;
  314. uint rcon = 1;
  315. uint u = SubWord(Shift(t5, 8)) ^ rcon; rcon <<= 1;
  316. t0 ^= u; W[1][2] = t0;
  317. t1 ^= t0; W[1][3] = t1;
  318. t2 ^= t1; W[2][0] = t2;
  319. t3 ^= t2; W[2][1] = t3;
  320. t4 ^= t3; W[2][2] = t4;
  321. t5 ^= t4; W[2][3] = t5;
  322. for (int i = 3; i < 12; i += 3)
  323. {
  324. u = SubWord(Shift(t5, 8)) ^ rcon; rcon <<= 1;
  325. t0 ^= u; W[i ][0] = t0;
  326. t1 ^= t0; W[i ][1] = t1;
  327. t2 ^= t1; W[i ][2] = t2;
  328. t3 ^= t2; W[i ][3] = t3;
  329. t4 ^= t3; W[i + 1][0] = t4;
  330. t5 ^= t4; W[i + 1][1] = t5;
  331. u = SubWord(Shift(t5, 8)) ^ rcon; rcon <<= 1;
  332. t0 ^= u; W[i + 1][2] = t0;
  333. t1 ^= t0; W[i + 1][3] = t1;
  334. t2 ^= t1; W[i + 2][0] = t2;
  335. t3 ^= t2; W[i + 2][1] = t3;
  336. t4 ^= t3; W[i + 2][2] = t4;
  337. t5 ^= t4; W[i + 2][3] = t5;
  338. }
  339. u = SubWord(Shift(t5, 8)) ^ rcon;
  340. t0 ^= u; W[12][0] = t0;
  341. t1 ^= t0; W[12][1] = t1;
  342. t2 ^= t1; W[12][2] = t2;
  343. t3 ^= t2; W[12][3] = t3;
  344. break;
  345. }
  346. case 8:
  347. {
  348. uint t0 = Pack.LE_To_UInt32(key, 0); W[0][0] = t0;
  349. uint t1 = Pack.LE_To_UInt32(key, 4); W[0][1] = t1;
  350. uint t2 = Pack.LE_To_UInt32(key, 8); W[0][2] = t2;
  351. uint t3 = Pack.LE_To_UInt32(key, 12); W[0][3] = t3;
  352. uint t4 = Pack.LE_To_UInt32(key, 16); W[1][0] = t4;
  353. uint t5 = Pack.LE_To_UInt32(key, 20); W[1][1] = t5;
  354. uint t6 = Pack.LE_To_UInt32(key, 24); W[1][2] = t6;
  355. uint t7 = Pack.LE_To_UInt32(key, 28); W[1][3] = t7;
  356. uint u, rcon = 1;
  357. for (int i = 2; i < 14; i += 2)
  358. {
  359. u = SubWord(Shift(t7, 8)) ^ rcon; rcon <<= 1;
  360. t0 ^= u; W[i ][0] = t0;
  361. t1 ^= t0; W[i ][1] = t1;
  362. t2 ^= t1; W[i ][2] = t2;
  363. t3 ^= t2; W[i ][3] = t3;
  364. u = SubWord(t3);
  365. t4 ^= u; W[i + 1][0] = t4;
  366. t5 ^= t4; W[i + 1][1] = t5;
  367. t6 ^= t5; W[i + 1][2] = t6;
  368. t7 ^= t6; W[i + 1][3] = t7;
  369. }
  370. u = SubWord(Shift(t7, 8)) ^ rcon;
  371. t0 ^= u; W[14][0] = t0;
  372. t1 ^= t0; W[14][1] = t1;
  373. t2 ^= t1; W[14][2] = t2;
  374. t3 ^= t2; W[14][3] = t3;
  375. break;
  376. }
  377. default:
  378. {
  379. throw new InvalidOperationException("Should never get here");
  380. }
  381. }
  382. if (!forEncryption)
  383. {
  384. for (int j = 1; j < ROUNDS; j++)
  385. {
  386. uint[] w = W[j];
  387. for (int i = 0; i < 4; i++)
  388. {
  389. w[i] = Inv_Mcol(w[i]);
  390. }
  391. }
  392. }
  393. return W;
  394. }
  395. private int ROUNDS;
  396. private uint[][] WorkingKey;
  397. private uint C0, C1, C2, C3;
  398. private bool forEncryption;
  399. private const int BLOCK_SIZE = 16;
  400. /**
  401. * default constructor - 128 bit block size.
  402. */
  403. public AesEngine()
  404. {
  405. }
  406. /**
  407. * initialise an AES cipher.
  408. *
  409. * @param forEncryption whether or not we are for encryption.
  410. * @param parameters the parameters required to set up the cipher.
  411. * @exception ArgumentException if the parameters argument is
  412. * inappropriate.
  413. */
  414. public virtual void Init(
  415. bool forEncryption,
  416. ICipherParameters parameters)
  417. {
  418. KeyParameter keyParameter = parameters as KeyParameter;
  419. if (keyParameter == null)
  420. throw new ArgumentException("invalid parameter passed to AES init - "
  421. + Org.BouncyCastle.Utilities.Platform.GetTypeName(parameters));
  422. WorkingKey = GenerateWorkingKey(keyParameter.GetKey(), forEncryption);
  423. this.forEncryption = forEncryption;
  424. }
  425. public virtual string AlgorithmName
  426. {
  427. get { return "AES"; }
  428. }
  429. public virtual bool IsPartialBlockOkay
  430. {
  431. get { return false; }
  432. }
  433. public virtual int GetBlockSize()
  434. {
  435. return BLOCK_SIZE;
  436. }
  437. public virtual int ProcessBlock(
  438. byte[] input,
  439. int inOff,
  440. byte[] output,
  441. int outOff)
  442. {
  443. if (WorkingKey == null)
  444. throw new InvalidOperationException("AES engine not initialised");
  445. Check.DataLength(input, inOff, 16, "input buffer too short");
  446. Check.OutputLength(output, outOff, 16, "output buffer too short");
  447. UnPackBlock(input, inOff);
  448. if (forEncryption)
  449. {
  450. EncryptBlock(WorkingKey);
  451. }
  452. else
  453. {
  454. DecryptBlock(WorkingKey);
  455. }
  456. PackBlock(output, outOff);
  457. return BLOCK_SIZE;
  458. }
  459. public virtual void Reset()
  460. {
  461. }
  462. private void UnPackBlock(
  463. byte[] bytes,
  464. int off)
  465. {
  466. C0 = Pack.LE_To_UInt32(bytes, off);
  467. C1 = Pack.LE_To_UInt32(bytes, off + 4);
  468. C2 = Pack.LE_To_UInt32(bytes, off + 8);
  469. C3 = Pack.LE_To_UInt32(bytes, off + 12);
  470. }
  471. private void PackBlock(
  472. byte[] bytes,
  473. int off)
  474. {
  475. Pack.UInt32_To_LE(C0, bytes, off);
  476. Pack.UInt32_To_LE(C1, bytes, off + 4);
  477. Pack.UInt32_To_LE(C2, bytes, off + 8);
  478. Pack.UInt32_To_LE(C3, bytes, off + 12);
  479. }
  480. private void EncryptBlock(uint[][] KW)
  481. {
  482. uint[] kw = KW[0];
  483. uint t0 = this.C0 ^ kw[0];
  484. uint t1 = this.C1 ^ kw[1];
  485. uint t2 = this.C2 ^ kw[2];
  486. uint r0, r1, r2, r3 = this.C3 ^ kw[3];
  487. int r = 1;
  488. while (r < ROUNDS - 1)
  489. {
  490. kw = KW[r++];
  491. r0 = T0[t0 & 255] ^ Shift(T0[(t1 >> 8) & 255], 24) ^ Shift(T0[(t2 >> 16) & 255], 16) ^ Shift(T0[(r3 >> 24) & 255], 8) ^ kw[0];
  492. r1 = T0[t1 & 255] ^ Shift(T0[(t2 >> 8) & 255], 24) ^ Shift(T0[(r3 >> 16) & 255], 16) ^ Shift(T0[(t0 >> 24) & 255], 8) ^ kw[1];
  493. r2 = T0[t2 & 255] ^ Shift(T0[(r3 >> 8) & 255], 24) ^ Shift(T0[(t0 >> 16) & 255], 16) ^ Shift(T0[(t1 >> 24) & 255], 8) ^ kw[2];
  494. r3 = T0[r3 & 255] ^ Shift(T0[(t0 >> 8) & 255], 24) ^ Shift(T0[(t1 >> 16) & 255], 16) ^ Shift(T0[(t2 >> 24) & 255], 8) ^ kw[3];
  495. kw = KW[r++];
  496. t0 = T0[r0 & 255] ^ Shift(T0[(r1 >> 8) & 255], 24) ^ Shift(T0[(r2 >> 16) & 255], 16) ^ Shift(T0[(r3 >> 24) & 255], 8) ^ kw[0];
  497. t1 = T0[r1 & 255] ^ Shift(T0[(r2 >> 8) & 255], 24) ^ Shift(T0[(r3 >> 16) & 255], 16) ^ Shift(T0[(r0 >> 24) & 255], 8) ^ kw[1];
  498. t2 = T0[r2 & 255] ^ Shift(T0[(r3 >> 8) & 255], 24) ^ Shift(T0[(r0 >> 16) & 255], 16) ^ Shift(T0[(r1 >> 24) & 255], 8) ^ kw[2];
  499. r3 = T0[r3 & 255] ^ Shift(T0[(r0 >> 8) & 255], 24) ^ Shift(T0[(r1 >> 16) & 255], 16) ^ Shift(T0[(r2 >> 24) & 255], 8) ^ kw[3];
  500. }
  501. kw = KW[r++];
  502. r0 = T0[t0 & 255] ^ Shift(T0[(t1 >> 8) & 255], 24) ^ Shift(T0[(t2 >> 16) & 255], 16) ^ Shift(T0[(r3 >> 24) & 255], 8) ^ kw[0];
  503. r1 = T0[t1 & 255] ^ Shift(T0[(t2 >> 8) & 255], 24) ^ Shift(T0[(r3 >> 16) & 255], 16) ^ Shift(T0[(t0 >> 24) & 255], 8) ^ kw[1];
  504. r2 = T0[t2 & 255] ^ Shift(T0[(r3 >> 8) & 255], 24) ^ Shift(T0[(t0 >> 16) & 255], 16) ^ Shift(T0[(t1 >> 24) & 255], 8) ^ kw[2];
  505. r3 = T0[r3 & 255] ^ Shift(T0[(t0 >> 8) & 255], 24) ^ Shift(T0[(t1 >> 16) & 255], 16) ^ Shift(T0[(t2 >> 24) & 255], 8) ^ kw[3];
  506. // the final round's table is a simple function of S so we don't use a whole other four tables for it
  507. kw = KW[r];
  508. this.C0 = (uint)S[r0 & 255] ^ (((uint)S[(r1 >> 8) & 255]) << 8) ^ (((uint)S[(r2 >> 16) & 255]) << 16) ^ (((uint)S[(r3 >> 24) & 255]) << 24) ^ kw[0];
  509. this.C1 = (uint)S[r1 & 255] ^ (((uint)S[(r2 >> 8) & 255]) << 8) ^ (((uint)S[(r3 >> 16) & 255]) << 16) ^ (((uint)S[(r0 >> 24) & 255]) << 24) ^ kw[1];
  510. this.C2 = (uint)S[r2 & 255] ^ (((uint)S[(r3 >> 8) & 255]) << 8) ^ (((uint)S[(r0 >> 16) & 255]) << 16) ^ (((uint)S[(r1 >> 24) & 255]) << 24) ^ kw[2];
  511. this.C3 = (uint)S[r3 & 255] ^ (((uint)S[(r0 >> 8) & 255]) << 8) ^ (((uint)S[(r1 >> 16) & 255]) << 16) ^ (((uint)S[(r2 >> 24) & 255]) << 24) ^ kw[3];
  512. }
  513. private void DecryptBlock(uint[][] KW)
  514. {
  515. uint[] kw = KW[ROUNDS];
  516. uint t0 = this.C0 ^ kw[0];
  517. uint t1 = this.C1 ^ kw[1];
  518. uint t2 = this.C2 ^ kw[2];
  519. uint r0, r1, r2, r3 = this.C3 ^ kw[3];
  520. int r = ROUNDS - 1;
  521. while (r > 1)
  522. {
  523. kw = KW[r--];
  524. r0 = Tinv0[t0 & 255] ^ Shift(Tinv0[(r3 >> 8) & 255], 24) ^ Shift(Tinv0[(t2 >> 16) & 255], 16) ^ Shift(Tinv0[(t1 >> 24) & 255], 8) ^ kw[0];
  525. r1 = Tinv0[t1 & 255] ^ Shift(Tinv0[(t0 >> 8) & 255], 24) ^ Shift(Tinv0[(r3 >> 16) & 255], 16) ^ Shift(Tinv0[(t2 >> 24) & 255], 8) ^ kw[1];
  526. r2 = Tinv0[t2 & 255] ^ Shift(Tinv0[(t1 >> 8) & 255], 24) ^ Shift(Tinv0[(t0 >> 16) & 255], 16) ^ Shift(Tinv0[(r3 >> 24) & 255], 8) ^ kw[2];
  527. r3 = Tinv0[r3 & 255] ^ Shift(Tinv0[(t2 >> 8) & 255], 24) ^ Shift(Tinv0[(t1 >> 16) & 255], 16) ^ Shift(Tinv0[(t0 >> 24) & 255], 8) ^ kw[3];
  528. kw = KW[r--];
  529. t0 = Tinv0[r0 & 255] ^ Shift(Tinv0[(r3 >> 8) & 255], 24) ^ Shift(Tinv0[(r2 >> 16) & 255], 16) ^ Shift(Tinv0[(r1 >> 24) & 255], 8) ^ kw[0];
  530. t1 = Tinv0[r1 & 255] ^ Shift(Tinv0[(r0 >> 8) & 255], 24) ^ Shift(Tinv0[(r3 >> 16) & 255], 16) ^ Shift(Tinv0[(r2 >> 24) & 255], 8) ^ kw[1];
  531. t2 = Tinv0[r2 & 255] ^ Shift(Tinv0[(r1 >> 8) & 255], 24) ^ Shift(Tinv0[(r0 >> 16) & 255], 16) ^ Shift(Tinv0[(r3 >> 24) & 255], 8) ^ kw[2];
  532. r3 = Tinv0[r3 & 255] ^ Shift(Tinv0[(r2 >> 8) & 255], 24) ^ Shift(Tinv0[(r1 >> 16) & 255], 16) ^ Shift(Tinv0[(r0 >> 24) & 255], 8) ^ kw[3];
  533. }
  534. kw = KW[1];
  535. r0 = Tinv0[t0 & 255] ^ Shift(Tinv0[(r3 >> 8) & 255], 24) ^ Shift(Tinv0[(t2 >> 16) & 255], 16) ^ Shift(Tinv0[(t1 >> 24) & 255], 8) ^ kw[0];
  536. r1 = Tinv0[t1 & 255] ^ Shift(Tinv0[(t0 >> 8) & 255], 24) ^ Shift(Tinv0[(r3 >> 16) & 255], 16) ^ Shift(Tinv0[(t2 >> 24) & 255], 8) ^ kw[1];
  537. r2 = Tinv0[t2 & 255] ^ Shift(Tinv0[(t1 >> 8) & 255], 24) ^ Shift(Tinv0[(t0 >> 16) & 255], 16) ^ Shift(Tinv0[(r3 >> 24) & 255], 8) ^ kw[2];
  538. r3 = Tinv0[r3 & 255] ^ Shift(Tinv0[(t2 >> 8) & 255], 24) ^ Shift(Tinv0[(t1 >> 16) & 255], 16) ^ Shift(Tinv0[(t0 >> 24) & 255], 8) ^ kw[3];
  539. // the final round's table is a simple function of Si so we don't use a whole other four tables for it
  540. kw = KW[0];
  541. this.C0 = (uint)Si[r0 & 255] ^ (((uint)Si[(r3 >> 8) & 255]) << 8) ^ (((uint)Si[(r2 >> 16) & 255]) << 16) ^ (((uint)Si[(r1 >> 24) & 255]) << 24) ^ kw[0];
  542. this.C1 = (uint)Si[r1 & 255] ^ (((uint)Si[(r0 >> 8) & 255]) << 8) ^ (((uint)Si[(r3 >> 16) & 255]) << 16) ^ (((uint)Si[(r2 >> 24) & 255]) << 24) ^ kw[1];
  543. this.C2 = (uint)Si[r2 & 255] ^ (((uint)Si[(r1 >> 8) & 255]) << 8) ^ (((uint)Si[(r0 >> 16) & 255]) << 16) ^ (((uint)Si[(r3 >> 24) & 255]) << 24) ^ kw[2];
  544. this.C3 = (uint)Si[r3 & 255] ^ (((uint)Si[(r2 >> 8) & 255]) << 8) ^ (((uint)Si[(r1 >> 16) & 255]) << 16) ^ (((uint)Si[(r0 >> 24) & 255]) << 24) ^ kw[3];
  545. }
  546. }
  547. }
  548. #endif