Cast6Engine.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Utilities;
  5. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Engines
  6. {
  7. /**
  8. * A class that provides CAST6 key encryption operations,
  9. * such as encoding data and generating keys.
  10. *
  11. * All the algorithms herein are from the Internet RFC
  12. *
  13. * RFC2612 - CAST6 (128bit block, 128-256bit key)
  14. *
  15. * and implement a simplified cryptography interface.
  16. */
  17. public sealed class Cast6Engine
  18. : Cast5Engine
  19. {
  20. //====================================
  21. // Useful constants
  22. //====================================
  23. private const int ROUNDS = 12;
  24. private const int BLOCK_SIZE = 16; // bytes = 128 bits
  25. /*
  26. * Put the round and mask keys into an array.
  27. * Kr0[i] => _Kr[i*4 + 0]
  28. */
  29. private int []_Kr = new int[ROUNDS*4]; // the rotating round key(s)
  30. private uint []_Km = new uint[ROUNDS*4]; // the masking round key(s)
  31. /*
  32. * Key setup
  33. */
  34. private int []_Tr = new int[24 * 8];
  35. private uint []_Tm = new uint[24 * 8];
  36. private uint[] _workingKey = new uint[8];
  37. public Cast6Engine()
  38. {
  39. }
  40. public override string AlgorithmName
  41. {
  42. get { return "CAST6"; }
  43. }
  44. public override void Reset()
  45. {
  46. }
  47. public override int GetBlockSize()
  48. {
  49. return BLOCK_SIZE;
  50. }
  51. //==================================
  52. // Private Implementation
  53. //==================================
  54. /*
  55. * Creates the subkeys using the same nomenclature
  56. * as described in RFC2612.
  57. *
  58. * See section 2.4
  59. */
  60. internal override void SetKey(
  61. byte[] key)
  62. {
  63. uint Cm = 0x5a827999;
  64. uint Mm = 0x6ed9eba1;
  65. int Cr = 19;
  66. int Mr = 17;
  67. /*
  68. * Determine the key size here, if required
  69. *
  70. * if keysize < 256 bytes, pad with 0
  71. *
  72. * Typical key sizes => 128, 160, 192, 224, 256
  73. */
  74. for (int i=0; i< 24; i++)
  75. {
  76. for (int j=0; j< 8; j++)
  77. {
  78. _Tm[i*8 + j] = Cm;
  79. Cm += Mm; //mod 2^32;
  80. _Tr[i*8 + j] = Cr;
  81. Cr = (Cr + Mr) & 0x1f; // mod 32
  82. }
  83. }
  84. byte[] tmpKey = new byte[64];
  85. key.CopyTo(tmpKey, 0);
  86. // now create ABCDEFGH
  87. for (int i = 0; i < 8; i++)
  88. {
  89. _workingKey[i] = Pack.BE_To_UInt32(tmpKey, i*4);
  90. }
  91. // Generate the key schedule
  92. for (int i = 0; i < 12; i++)
  93. {
  94. // KAPPA <- W2i(KAPPA)
  95. int i2 = i*2 *8;
  96. _workingKey[6] ^= F1(_workingKey[7], _Tm[i2], _Tr[i2]);
  97. _workingKey[5] ^= F2(_workingKey[6], _Tm[i2+1], _Tr[i2+1]);
  98. _workingKey[4] ^= F3(_workingKey[5], _Tm[i2+2], _Tr[i2+2]);
  99. _workingKey[3] ^= F1(_workingKey[4], _Tm[i2+3], _Tr[i2+3]);
  100. _workingKey[2] ^= F2(_workingKey[3], _Tm[i2+4], _Tr[i2+4]);
  101. _workingKey[1] ^= F3(_workingKey[2], _Tm[i2+5], _Tr[i2+5]);
  102. _workingKey[0] ^= F1(_workingKey[1], _Tm[i2+6], _Tr[i2+6]);
  103. _workingKey[7] ^= F2(_workingKey[0], _Tm[i2+7], _Tr[i2+7]);
  104. // KAPPA <- W2i+1(KAPPA)
  105. i2 = (i*2 + 1)*8;
  106. _workingKey[6] ^= F1(_workingKey[7], _Tm[i2], _Tr[i2]);
  107. _workingKey[5] ^= F2(_workingKey[6], _Tm[i2+1], _Tr[i2+1]);
  108. _workingKey[4] ^= F3(_workingKey[5], _Tm[i2+2], _Tr[i2+2]);
  109. _workingKey[3] ^= F1(_workingKey[4], _Tm[i2+3], _Tr[i2+3]);
  110. _workingKey[2] ^= F2(_workingKey[3], _Tm[i2+4], _Tr[i2+4]);
  111. _workingKey[1] ^= F3(_workingKey[2], _Tm[i2+5], _Tr[i2+5]);
  112. _workingKey[0] ^= F1(_workingKey[1], _Tm[i2+6], _Tr[i2+6]);
  113. _workingKey[7] ^= F2(_workingKey[0], _Tm[i2+7], _Tr[i2+7]);
  114. // Kr_(i) <- KAPPA
  115. _Kr[i*4] = (int)(_workingKey[0] & 0x1f);
  116. _Kr[i*4 + 1] = (int)(_workingKey[2] & 0x1f);
  117. _Kr[i*4 + 2] = (int)(_workingKey[4] & 0x1f);
  118. _Kr[i*4 + 3] = (int)(_workingKey[6] & 0x1f);
  119. // Km_(i) <- KAPPA
  120. _Km[i*4] = _workingKey[7];
  121. _Km[i*4 + 1] = _workingKey[5];
  122. _Km[i*4 + 2] = _workingKey[3];
  123. _Km[i*4 + 3] = _workingKey[1];
  124. }
  125. }
  126. /**
  127. * Encrypt the given input starting at the given offset and place
  128. * the result in the provided buffer starting at the given offset.
  129. *
  130. * @param src The plaintext buffer
  131. * @param srcIndex An offset into src
  132. * @param dst The ciphertext buffer
  133. * @param dstIndex An offset into dst
  134. */
  135. internal override int EncryptBlock(
  136. byte[] src,
  137. int srcIndex,
  138. byte[] dst,
  139. int dstIndex)
  140. {
  141. // process the input block
  142. // batch the units up into 4x32 bit chunks and go for it
  143. uint A = Pack.BE_To_UInt32(src, srcIndex);
  144. uint B = Pack.BE_To_UInt32(src, srcIndex + 4);
  145. uint C = Pack.BE_To_UInt32(src, srcIndex + 8);
  146. uint D = Pack.BE_To_UInt32(src, srcIndex + 12);
  147. uint[] result = new uint[4];
  148. CAST_Encipher(A, B, C, D, result);
  149. // now stuff them into the destination block
  150. Pack.UInt32_To_BE(result[0], dst, dstIndex);
  151. Pack.UInt32_To_BE(result[1], dst, dstIndex + 4);
  152. Pack.UInt32_To_BE(result[2], dst, dstIndex + 8);
  153. Pack.UInt32_To_BE(result[3], dst, dstIndex + 12);
  154. return BLOCK_SIZE;
  155. }
  156. /**
  157. * Decrypt the given input starting at the given offset and place
  158. * the result in the provided buffer starting at the given offset.
  159. *
  160. * @param src The plaintext buffer
  161. * @param srcIndex An offset into src
  162. * @param dst The ciphertext buffer
  163. * @param dstIndex An offset into dst
  164. */
  165. internal override int DecryptBlock(
  166. byte[] src,
  167. int srcIndex,
  168. byte[] dst,
  169. int dstIndex)
  170. {
  171. // process the input block
  172. // batch the units up into 4x32 bit chunks and go for it
  173. uint A = Pack.BE_To_UInt32(src, srcIndex);
  174. uint B = Pack.BE_To_UInt32(src, srcIndex + 4);
  175. uint C = Pack.BE_To_UInt32(src, srcIndex + 8);
  176. uint D = Pack.BE_To_UInt32(src, srcIndex + 12);
  177. uint[] result = new uint[4];
  178. CAST_Decipher(A, B, C, D, result);
  179. // now stuff them into the destination block
  180. Pack.UInt32_To_BE(result[0], dst, dstIndex);
  181. Pack.UInt32_To_BE(result[1], dst, dstIndex + 4);
  182. Pack.UInt32_To_BE(result[2], dst, dstIndex + 8);
  183. Pack.UInt32_To_BE(result[3], dst, dstIndex + 12);
  184. return BLOCK_SIZE;
  185. }
  186. /**
  187. * Does the 12 quad rounds rounds to encrypt the block.
  188. *
  189. * @param A the 00-31 bits of the plaintext block
  190. * @param B the 32-63 bits of the plaintext block
  191. * @param C the 64-95 bits of the plaintext block
  192. * @param D the 96-127 bits of the plaintext block
  193. * @param result the resulting ciphertext
  194. */
  195. private void CAST_Encipher(
  196. uint A,
  197. uint B,
  198. uint C,
  199. uint D,
  200. uint[] result)
  201. {
  202. for (int i = 0; i < 6; i++)
  203. {
  204. int x = i*4;
  205. // BETA <- Qi(BETA)
  206. C ^= F1(D, _Km[x], _Kr[x]);
  207. B ^= F2(C, _Km[x + 1], _Kr[x + 1]);
  208. A ^= F3(B, _Km[x + 2], _Kr[x + 2]);
  209. D ^= F1(A, _Km[x + 3], _Kr[x + 3]);
  210. }
  211. for (int i = 6; i < 12; i++)
  212. {
  213. int x = i*4;
  214. // BETA <- QBARi(BETA)
  215. D ^= F1(A, _Km[x + 3], _Kr[x + 3]);
  216. A ^= F3(B, _Km[x + 2], _Kr[x + 2]);
  217. B ^= F2(C, _Km[x + 1], _Kr[x + 1]);
  218. C ^= F1(D, _Km[x], _Kr[x]);
  219. }
  220. result[0] = A;
  221. result[1] = B;
  222. result[2] = C;
  223. result[3] = D;
  224. }
  225. /**
  226. * Does the 12 quad rounds rounds to decrypt the block.
  227. *
  228. * @param A the 00-31 bits of the ciphertext block
  229. * @param B the 32-63 bits of the ciphertext block
  230. * @param C the 64-95 bits of the ciphertext block
  231. * @param D the 96-127 bits of the ciphertext block
  232. * @param result the resulting plaintext
  233. */
  234. private void CAST_Decipher(
  235. uint A,
  236. uint B,
  237. uint C,
  238. uint D,
  239. uint[] result)
  240. {
  241. for (int i = 0; i < 6; i++)
  242. {
  243. int x = (11-i)*4;
  244. // BETA <- Qi(BETA)
  245. C ^= F1(D, _Km[x], _Kr[x]);
  246. B ^= F2(C, _Km[x + 1], _Kr[x + 1]);
  247. A ^= F3(B, _Km[x + 2], _Kr[x + 2]);
  248. D ^= F1(A, _Km[x + 3], _Kr[x + 3]);
  249. }
  250. for (int i=6; i<12; i++)
  251. {
  252. int x = (11-i)*4;
  253. // BETA <- QBARi(BETA)
  254. D ^= F1(A, _Km[x + 3], _Kr[x + 3]);
  255. A ^= F3(B, _Km[x + 2], _Kr[x + 2]);
  256. B ^= F2(C, _Km[x + 1], _Kr[x + 1]);
  257. C ^= F1(D, _Km[x], _Kr[x]);
  258. }
  259. result[0] = A;
  260. result[1] = B;
  261. result[2] = C;
  262. result[3] = D;
  263. }
  264. }
  265. }
  266. #pragma warning restore
  267. #endif