FastAesEngine.cs 51 KB

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