CamelliaLightEngine.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  5. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Engines
  6. {
  7. /**
  8. * Camellia - based on RFC 3713, smaller implementation, about half the size of CamelliaEngine.
  9. */
  10. public class CamelliaLightEngine
  11. : IBlockCipher
  12. {
  13. private const int BLOCK_SIZE = 16;
  14. // private const int MASK8 = 0xff;
  15. private bool initialised;
  16. private bool _keyis128;
  17. private uint[] subkey = new uint[24 * 4];
  18. private uint[] kw = new uint[4 * 2]; // for whitening
  19. private uint[] ke = new uint[6 * 2]; // for FL and FL^(-1)
  20. private uint[] state = new uint[4]; // for encryption and decryption
  21. private static readonly uint[] SIGMA = {
  22. 0xa09e667f, 0x3bcc908b,
  23. 0xb67ae858, 0x4caa73b2,
  24. 0xc6ef372f, 0xe94f82be,
  25. 0x54ff53a5, 0xf1d36f1c,
  26. 0x10e527fa, 0xde682d1d,
  27. 0xb05688c2, 0xb3e6c1fd
  28. };
  29. /*
  30. *
  31. * S-box data
  32. *
  33. */
  34. private static readonly byte[] SBOX1 = {
  35. (byte)112, (byte)130, (byte)44, (byte)236,
  36. (byte)179, (byte)39, (byte)192, (byte)229,
  37. (byte)228, (byte)133, (byte)87, (byte)53,
  38. (byte)234, (byte)12, (byte)174, (byte)65,
  39. (byte)35, (byte)239, (byte)107, (byte)147,
  40. (byte)69, (byte)25, (byte)165, (byte)33,
  41. (byte)237, (byte)14, (byte)79, (byte)78,
  42. (byte)29, (byte)101, (byte)146, (byte)189,
  43. (byte)134, (byte)184, (byte)175, (byte)143,
  44. (byte)124, (byte)235, (byte)31, (byte)206,
  45. (byte)62, (byte)48, (byte)220, (byte)95,
  46. (byte)94, (byte)197, (byte)11, (byte)26,
  47. (byte)166, (byte)225, (byte)57, (byte)202,
  48. (byte)213, (byte)71, (byte)93, (byte)61,
  49. (byte)217, (byte)1, (byte)90, (byte)214,
  50. (byte)81, (byte)86, (byte)108, (byte)77,
  51. (byte)139, (byte)13, (byte)154, (byte)102,
  52. (byte)251, (byte)204, (byte)176, (byte)45,
  53. (byte)116, (byte)18, (byte)43, (byte)32,
  54. (byte)240, (byte)177, (byte)132, (byte)153,
  55. (byte)223, (byte)76, (byte)203, (byte)194,
  56. (byte)52, (byte)126, (byte)118, (byte)5,
  57. (byte)109, (byte)183, (byte)169, (byte)49,
  58. (byte)209, (byte)23, (byte)4, (byte)215,
  59. (byte)20, (byte)88, (byte)58, (byte)97,
  60. (byte)222, (byte)27, (byte)17, (byte)28,
  61. (byte)50, (byte)15, (byte)156, (byte)22,
  62. (byte)83, (byte)24, (byte)242, (byte)34,
  63. (byte)254, (byte)68, (byte)207, (byte)178,
  64. (byte)195, (byte)181, (byte)122, (byte)145,
  65. (byte)36, (byte)8, (byte)232, (byte)168,
  66. (byte)96, (byte)252, (byte)105, (byte)80,
  67. (byte)170, (byte)208, (byte)160, (byte)125,
  68. (byte)161, (byte)137, (byte)98, (byte)151,
  69. (byte)84, (byte)91, (byte)30, (byte)149,
  70. (byte)224, (byte)255, (byte)100, (byte)210,
  71. (byte)16, (byte)196, (byte)0, (byte)72,
  72. (byte)163, (byte)247, (byte)117, (byte)219,
  73. (byte)138, (byte)3, (byte)230, (byte)218,
  74. (byte)9, (byte)63, (byte)221, (byte)148,
  75. (byte)135, (byte)92, (byte)131, (byte)2,
  76. (byte)205, (byte)74, (byte)144, (byte)51,
  77. (byte)115, (byte)103, (byte)246, (byte)243,
  78. (byte)157, (byte)127, (byte)191, (byte)226,
  79. (byte)82, (byte)155, (byte)216, (byte)38,
  80. (byte)200, (byte)55, (byte)198, (byte)59,
  81. (byte)129, (byte)150, (byte)111, (byte)75,
  82. (byte)19, (byte)190, (byte)99, (byte)46,
  83. (byte)233, (byte)121, (byte)167, (byte)140,
  84. (byte)159, (byte)110, (byte)188, (byte)142,
  85. (byte)41, (byte)245, (byte)249, (byte)182,
  86. (byte)47, (byte)253, (byte)180, (byte)89,
  87. (byte)120, (byte)152, (byte)6, (byte)106,
  88. (byte)231, (byte)70, (byte)113, (byte)186,
  89. (byte)212, (byte)37, (byte)171, (byte)66,
  90. (byte)136, (byte)162, (byte)141, (byte)250,
  91. (byte)114, (byte)7, (byte)185, (byte)85,
  92. (byte)248, (byte)238, (byte)172, (byte)10,
  93. (byte)54, (byte)73, (byte)42, (byte)104,
  94. (byte)60, (byte)56, (byte)241, (byte)164,
  95. (byte)64, (byte)40, (byte)211, (byte)123,
  96. (byte)187, (byte)201, (byte)67, (byte)193,
  97. (byte)21, (byte)227, (byte)173, (byte)244,
  98. (byte)119, (byte)199, (byte)128, (byte)158
  99. };
  100. private static uint rightRotate(uint x, int s)
  101. {
  102. return ((x >> s) + (x << (32 - s)));
  103. }
  104. private static uint leftRotate(uint x, int s)
  105. {
  106. return (x << s) + (x >> (32 - s));
  107. }
  108. private static void roldq(int rot, uint[] ki, int ioff, uint[] ko, int ooff)
  109. {
  110. ko[0 + ooff] = (ki[0 + ioff] << rot) | (ki[1 + ioff] >> (32 - rot));
  111. ko[1 + ooff] = (ki[1 + ioff] << rot) | (ki[2 + ioff] >> (32 - rot));
  112. ko[2 + ooff] = (ki[2 + ioff] << rot) | (ki[3 + ioff] >> (32 - rot));
  113. ko[3 + ooff] = (ki[3 + ioff] << rot) | (ki[0 + ioff] >> (32 - rot));
  114. ki[0 + ioff] = ko[0 + ooff];
  115. ki[1 + ioff] = ko[1 + ooff];
  116. ki[2 + ioff] = ko[2 + ooff];
  117. ki[3 + ioff] = ko[3 + ooff];
  118. }
  119. private static void decroldq(int rot, uint[] ki, int ioff, uint[] ko, int ooff)
  120. {
  121. ko[2 + ooff] = (ki[0 + ioff] << rot) | (ki[1 + ioff] >> (32 - rot));
  122. ko[3 + ooff] = (ki[1 + ioff] << rot) | (ki[2 + ioff] >> (32 - rot));
  123. ko[0 + ooff] = (ki[2 + ioff] << rot) | (ki[3 + ioff] >> (32 - rot));
  124. ko[1 + ooff] = (ki[3 + ioff] << rot) | (ki[0 + ioff] >> (32 - rot));
  125. ki[0 + ioff] = ko[2 + ooff];
  126. ki[1 + ioff] = ko[3 + ooff];
  127. ki[2 + ioff] = ko[0 + ooff];
  128. ki[3 + ioff] = ko[1 + ooff];
  129. }
  130. private static void roldqo32(int rot, uint[] ki, int ioff, uint[] ko, int ooff)
  131. {
  132. ko[0 + ooff] = (ki[1 + ioff] << (rot - 32)) | (ki[2 + ioff] >> (64 - rot));
  133. ko[1 + ooff] = (ki[2 + ioff] << (rot - 32)) | (ki[3 + ioff] >> (64 - rot));
  134. ko[2 + ooff] = (ki[3 + ioff] << (rot - 32)) | (ki[0 + ioff] >> (64 - rot));
  135. ko[3 + ooff] = (ki[0 + ioff] << (rot - 32)) | (ki[1 + ioff] >> (64 - rot));
  136. ki[0 + ioff] = ko[0 + ooff];
  137. ki[1 + ioff] = ko[1 + ooff];
  138. ki[2 + ioff] = ko[2 + ooff];
  139. ki[3 + ioff] = ko[3 + ooff];
  140. }
  141. private static void decroldqo32(int rot, uint[] ki, int ioff, uint[] ko, int ooff)
  142. {
  143. ko[2 + ooff] = (ki[1 + ioff] << (rot - 32)) | (ki[2 + ioff] >> (64 - rot));
  144. ko[3 + ooff] = (ki[2 + ioff] << (rot - 32)) | (ki[3 + ioff] >> (64 - rot));
  145. ko[0 + ooff] = (ki[3 + ioff] << (rot - 32)) | (ki[0 + ioff] >> (64 - rot));
  146. ko[1 + ooff] = (ki[0 + ioff] << (rot - 32)) | (ki[1 + ioff] >> (64 - rot));
  147. ki[0 + ioff] = ko[2 + ooff];
  148. ki[1 + ioff] = ko[3 + ooff];
  149. ki[2 + ioff] = ko[0 + ooff];
  150. ki[3 + ioff] = ko[1 + ooff];
  151. }
  152. private static uint bytes2uint(byte[] src, int offset)
  153. {
  154. uint word = 0;
  155. for (int i = 0; i < 4; i++)
  156. {
  157. word = (word << 8) + (uint)src[i + offset];
  158. }
  159. return word;
  160. }
  161. private static void uint2bytes(uint word, byte[] dst, int offset)
  162. {
  163. for (int i = 0; i < 4; i++)
  164. {
  165. dst[(3 - i) + offset] = (byte)word;
  166. word >>= 8;
  167. }
  168. }
  169. private byte lRot8(byte v, int rot)
  170. {
  171. return (byte)(((uint)v << rot) | ((uint)v >> (8 - rot)));
  172. }
  173. private uint sbox2(int x)
  174. {
  175. return (uint)lRot8(SBOX1[x], 1);
  176. }
  177. private uint sbox3(int x)
  178. {
  179. return (uint)lRot8(SBOX1[x], 7);
  180. }
  181. private uint sbox4(int x)
  182. {
  183. return (uint)SBOX1[lRot8((byte)x, 1)];
  184. }
  185. private void camelliaF2(uint[] s, uint[] skey, int keyoff)
  186. {
  187. uint t1, t2, u, v;
  188. t1 = s[0] ^ skey[0 + keyoff];
  189. u = sbox4((byte)t1);
  190. u |= (sbox3((byte)(t1 >> 8)) << 8);
  191. u |= (sbox2((byte)(t1 >> 16)) << 16);
  192. u |= ((uint)(SBOX1[(byte)(t1 >> 24)]) << 24);
  193. t2 = s[1] ^ skey[1 + keyoff];
  194. v = (uint)SBOX1[(byte)t2];
  195. v |= (sbox4((byte)(t2 >> 8)) << 8);
  196. v |= (sbox3((byte)(t2 >> 16)) << 16);
  197. v |= (sbox2((byte)(t2 >> 24)) << 24);
  198. v = leftRotate(v, 8);
  199. u ^= v;
  200. v = leftRotate(v, 8) ^ u;
  201. u = rightRotate(u, 8) ^ v;
  202. s[2] ^= leftRotate(v, 16) ^ u;
  203. s[3] ^= leftRotate(u, 8);
  204. t1 = s[2] ^ skey[2 + keyoff];
  205. u = sbox4((byte)t1);
  206. u |= sbox3((byte)(t1 >> 8)) << 8;
  207. u |= sbox2((byte)(t1 >> 16)) << 16;
  208. u |= ((uint)SBOX1[(byte)(t1 >> 24)]) << 24;
  209. t2 = s[3] ^ skey[3 + keyoff];
  210. v = (uint)SBOX1[(byte)t2];
  211. v |= sbox4((byte)(t2 >> 8)) << 8;
  212. v |= sbox3((byte)(t2 >> 16)) << 16;
  213. v |= sbox2((byte)(t2 >> 24)) << 24;
  214. v = leftRotate(v, 8);
  215. u ^= v;
  216. v = leftRotate(v, 8) ^ u;
  217. u = rightRotate(u, 8) ^ v;
  218. s[0] ^= leftRotate(v, 16) ^ u;
  219. s[1] ^= leftRotate(u, 8);
  220. }
  221. private void camelliaFLs(uint[] s, uint[] fkey, int keyoff)
  222. {
  223. s[1] ^= leftRotate(s[0] & fkey[0 + keyoff], 1);
  224. s[0] ^= fkey[1 + keyoff] | s[1];
  225. s[2] ^= fkey[3 + keyoff] | s[3];
  226. s[3] ^= leftRotate(fkey[2 + keyoff] & s[2], 1);
  227. }
  228. private void setKey(bool forEncryption, byte[] key)
  229. {
  230. uint[] k = new uint[8];
  231. uint[] ka = new uint[4];
  232. uint[] kb = new uint[4];
  233. uint[] t = new uint[4];
  234. switch (key.Length)
  235. {
  236. case 16:
  237. _keyis128 = true;
  238. k[0] = bytes2uint(key, 0);
  239. k[1] = bytes2uint(key, 4);
  240. k[2] = bytes2uint(key, 8);
  241. k[3] = bytes2uint(key, 12);
  242. k[4] = k[5] = k[6] = k[7] = 0;
  243. break;
  244. case 24:
  245. k[0] = bytes2uint(key, 0);
  246. k[1] = bytes2uint(key, 4);
  247. k[2] = bytes2uint(key, 8);
  248. k[3] = bytes2uint(key, 12);
  249. k[4] = bytes2uint(key, 16);
  250. k[5] = bytes2uint(key, 20);
  251. k[6] = ~k[4];
  252. k[7] = ~k[5];
  253. _keyis128 = false;
  254. break;
  255. case 32:
  256. k[0] = bytes2uint(key, 0);
  257. k[1] = bytes2uint(key, 4);
  258. k[2] = bytes2uint(key, 8);
  259. k[3] = bytes2uint(key, 12);
  260. k[4] = bytes2uint(key, 16);
  261. k[5] = bytes2uint(key, 20);
  262. k[6] = bytes2uint(key, 24);
  263. k[7] = bytes2uint(key, 28);
  264. _keyis128 = false;
  265. break;
  266. default:
  267. throw new ArgumentException("key sizes are only 16/24/32 bytes.");
  268. }
  269. for (int i = 0; i < 4; i++)
  270. {
  271. ka[i] = k[i] ^ k[i + 4];
  272. }
  273. /* compute KA */
  274. camelliaF2(ka, SIGMA, 0);
  275. for (int i = 0; i < 4; i++)
  276. {
  277. ka[i] ^= k[i];
  278. }
  279. camelliaF2(ka, SIGMA, 4);
  280. if (_keyis128)
  281. {
  282. if (forEncryption)
  283. {
  284. /* KL dependant keys */
  285. kw[0] = k[0];
  286. kw[1] = k[1];
  287. kw[2] = k[2];
  288. kw[3] = k[3];
  289. roldq(15, k, 0, subkey, 4);
  290. roldq(30, k, 0, subkey, 12);
  291. roldq(15, k, 0, t, 0);
  292. subkey[18] = t[2];
  293. subkey[19] = t[3];
  294. roldq(17, k, 0, ke, 4);
  295. roldq(17, k, 0, subkey, 24);
  296. roldq(17, k, 0, subkey, 32);
  297. /* KA dependant keys */
  298. subkey[0] = ka[0];
  299. subkey[1] = ka[1];
  300. subkey[2] = ka[2];
  301. subkey[3] = ka[3];
  302. roldq(15, ka, 0, subkey, 8);
  303. roldq(15, ka, 0, ke, 0);
  304. roldq(15, ka, 0, t, 0);
  305. subkey[16] = t[0];
  306. subkey[17] = t[1];
  307. roldq(15, ka, 0, subkey, 20);
  308. roldqo32(34, ka, 0, subkey, 28);
  309. roldq(17, ka, 0, kw, 4);
  310. }
  311. else
  312. { // decryption
  313. /* KL dependant keys */
  314. kw[4] = k[0];
  315. kw[5] = k[1];
  316. kw[6] = k[2];
  317. kw[7] = k[3];
  318. decroldq(15, k, 0, subkey, 28);
  319. decroldq(30, k, 0, subkey, 20);
  320. decroldq(15, k, 0, t, 0);
  321. subkey[16] = t[0];
  322. subkey[17] = t[1];
  323. decroldq(17, k, 0, ke, 0);
  324. decroldq(17, k, 0, subkey, 8);
  325. decroldq(17, k, 0, subkey, 0);
  326. /* KA dependant keys */
  327. subkey[34] = ka[0];
  328. subkey[35] = ka[1];
  329. subkey[32] = ka[2];
  330. subkey[33] = ka[3];
  331. decroldq(15, ka, 0, subkey, 24);
  332. decroldq(15, ka, 0, ke, 4);
  333. decroldq(15, ka, 0, t, 0);
  334. subkey[18] = t[2];
  335. subkey[19] = t[3];
  336. decroldq(15, ka, 0, subkey, 12);
  337. decroldqo32(34, ka, 0, subkey, 4);
  338. roldq(17, ka, 0, kw, 0);
  339. }
  340. }
  341. else
  342. { // 192bit or 256bit
  343. /* compute KB */
  344. for (int i = 0; i < 4; i++)
  345. {
  346. kb[i] = ka[i] ^ k[i + 4];
  347. }
  348. camelliaF2(kb, SIGMA, 8);
  349. if (forEncryption)
  350. {
  351. /* KL dependant keys */
  352. kw[0] = k[0];
  353. kw[1] = k[1];
  354. kw[2] = k[2];
  355. kw[3] = k[3];
  356. roldqo32(45, k, 0, subkey, 16);
  357. roldq(15, k, 0, ke, 4);
  358. roldq(17, k, 0, subkey, 32);
  359. roldqo32(34, k, 0, subkey, 44);
  360. /* KR dependant keys */
  361. roldq(15, k, 4, subkey, 4);
  362. roldq(15, k, 4, ke, 0);
  363. roldq(30, k, 4, subkey, 24);
  364. roldqo32(34, k, 4, subkey, 36);
  365. /* KA dependant keys */
  366. roldq(15, ka, 0, subkey, 8);
  367. roldq(30, ka, 0, subkey, 20);
  368. /* 32bit rotation */
  369. ke[8] = ka[1];
  370. ke[9] = ka[2];
  371. ke[10] = ka[3];
  372. ke[11] = ka[0];
  373. roldqo32(49, ka, 0, subkey, 40);
  374. /* KB dependant keys */
  375. subkey[0] = kb[0];
  376. subkey[1] = kb[1];
  377. subkey[2] = kb[2];
  378. subkey[3] = kb[3];
  379. roldq(30, kb, 0, subkey, 12);
  380. roldq(30, kb, 0, subkey, 28);
  381. roldqo32(51, kb, 0, kw, 4);
  382. }
  383. else
  384. { // decryption
  385. /* KL dependant keys */
  386. kw[4] = k[0];
  387. kw[5] = k[1];
  388. kw[6] = k[2];
  389. kw[7] = k[3];
  390. decroldqo32(45, k, 0, subkey, 28);
  391. decroldq(15, k, 0, ke, 4);
  392. decroldq(17, k, 0, subkey, 12);
  393. decroldqo32(34, k, 0, subkey, 0);
  394. /* KR dependant keys */
  395. decroldq(15, k, 4, subkey, 40);
  396. decroldq(15, k, 4, ke, 8);
  397. decroldq(30, k, 4, subkey, 20);
  398. decroldqo32(34, k, 4, subkey, 8);
  399. /* KA dependant keys */
  400. decroldq(15, ka, 0, subkey, 36);
  401. decroldq(30, ka, 0, subkey, 24);
  402. /* 32bit rotation */
  403. ke[2] = ka[1];
  404. ke[3] = ka[2];
  405. ke[0] = ka[3];
  406. ke[1] = ka[0];
  407. decroldqo32(49, ka, 0, subkey, 4);
  408. /* KB dependant keys */
  409. subkey[46] = kb[0];
  410. subkey[47] = kb[1];
  411. subkey[44] = kb[2];
  412. subkey[45] = kb[3];
  413. decroldq(30, kb, 0, subkey, 32);
  414. decroldq(30, kb, 0, subkey, 16);
  415. roldqo32(51, kb, 0, kw, 0);
  416. }
  417. }
  418. }
  419. private int processBlock128(byte[] input, int inOff, byte[] output, int outOff)
  420. {
  421. for (int i = 0; i < 4; i++)
  422. {
  423. state[i] = bytes2uint(input, inOff + (i * 4));
  424. state[i] ^= kw[i];
  425. }
  426. camelliaF2(state, subkey, 0);
  427. camelliaF2(state, subkey, 4);
  428. camelliaF2(state, subkey, 8);
  429. camelliaFLs(state, ke, 0);
  430. camelliaF2(state, subkey, 12);
  431. camelliaF2(state, subkey, 16);
  432. camelliaF2(state, subkey, 20);
  433. camelliaFLs(state, ke, 4);
  434. camelliaF2(state, subkey, 24);
  435. camelliaF2(state, subkey, 28);
  436. camelliaF2(state, subkey, 32);
  437. state[2] ^= kw[4];
  438. state[3] ^= kw[5];
  439. state[0] ^= kw[6];
  440. state[1] ^= kw[7];
  441. uint2bytes(state[2], output, outOff);
  442. uint2bytes(state[3], output, outOff + 4);
  443. uint2bytes(state[0], output, outOff + 8);
  444. uint2bytes(state[1], output, outOff + 12);
  445. return BLOCK_SIZE;
  446. }
  447. private int processBlock192or256(byte[] input, int inOff, byte[] output, int outOff)
  448. {
  449. for (int i = 0; i < 4; i++)
  450. {
  451. state[i] = bytes2uint(input, inOff + (i * 4));
  452. state[i] ^= kw[i];
  453. }
  454. camelliaF2(state, subkey, 0);
  455. camelliaF2(state, subkey, 4);
  456. camelliaF2(state, subkey, 8);
  457. camelliaFLs(state, ke, 0);
  458. camelliaF2(state, subkey, 12);
  459. camelliaF2(state, subkey, 16);
  460. camelliaF2(state, subkey, 20);
  461. camelliaFLs(state, ke, 4);
  462. camelliaF2(state, subkey, 24);
  463. camelliaF2(state, subkey, 28);
  464. camelliaF2(state, subkey, 32);
  465. camelliaFLs(state, ke, 8);
  466. camelliaF2(state, subkey, 36);
  467. camelliaF2(state, subkey, 40);
  468. camelliaF2(state, subkey, 44);
  469. state[2] ^= kw[4];
  470. state[3] ^= kw[5];
  471. state[0] ^= kw[6];
  472. state[1] ^= kw[7];
  473. uint2bytes(state[2], output, outOff);
  474. uint2bytes(state[3], output, outOff + 4);
  475. uint2bytes(state[0], output, outOff + 8);
  476. uint2bytes(state[1], output, outOff + 12);
  477. return BLOCK_SIZE;
  478. }
  479. public CamelliaLightEngine()
  480. {
  481. initialised = false;
  482. }
  483. public virtual string AlgorithmName
  484. {
  485. get { return "Camellia"; }
  486. }
  487. public virtual bool IsPartialBlockOkay
  488. {
  489. get { return false; }
  490. }
  491. public virtual int GetBlockSize()
  492. {
  493. return BLOCK_SIZE;
  494. }
  495. public virtual void Init(
  496. bool forEncryption,
  497. ICipherParameters parameters)
  498. {
  499. if (!(parameters is KeyParameter))
  500. throw new ArgumentException("only simple KeyParameter expected.");
  501. setKey(forEncryption, ((KeyParameter)parameters).GetKey());
  502. initialised = true;
  503. }
  504. public virtual int ProcessBlock(
  505. byte[] input,
  506. int inOff,
  507. byte[] output,
  508. int outOff)
  509. {
  510. if (!initialised)
  511. throw new InvalidOperationException("Camellia engine not initialised");
  512. Check.DataLength(input, inOff, BLOCK_SIZE, "input buffer too short");
  513. Check.OutputLength(output, outOff, BLOCK_SIZE, "output buffer too short");
  514. if (_keyis128)
  515. {
  516. return processBlock128(input, inOff, output, outOff);
  517. }
  518. else
  519. {
  520. return processBlock192or256(input, inOff, output, outOff);
  521. }
  522. }
  523. public virtual void Reset()
  524. {
  525. }
  526. }
  527. }
  528. #pragma warning restore
  529. #endif