You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

417 lines
13 KiB

  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.]. */
  56. #include <openssl/cast.h>
  57. #if defined(OPENSSL_WINDOWS)
  58. #pragma warning(push, 3)
  59. #include <intrin.h>
  60. #pragma warning(pop)
  61. #endif
  62. #include "../macros.h"
  63. void CAST_ecb_encrypt(const uint8_t *in, uint8_t *out, const CAST_KEY *ks,
  64. int enc) {
  65. uint32_t d[2];
  66. n2l(in, d[0]);
  67. n2l(in, d[1]);
  68. if (enc) {
  69. CAST_encrypt(d, ks);
  70. } else {
  71. CAST_decrypt(d, ks);
  72. }
  73. l2n(d[0], out);
  74. l2n(d[1], out);
  75. }
  76. extern const uint32_t CAST_S_table0[256];
  77. extern const uint32_t CAST_S_table1[256];
  78. extern const uint32_t CAST_S_table2[256];
  79. extern const uint32_t CAST_S_table3[256];
  80. extern const uint32_t CAST_S_table4[256];
  81. extern const uint32_t CAST_S_table5[256];
  82. extern const uint32_t CAST_S_table6[256];
  83. extern const uint32_t CAST_S_table7[256];
  84. #if defined(OPENSSL_WINDOWS) && defined(_MSC_VER)
  85. #define ROTL(a, n) (_lrotl(a, n))
  86. #else
  87. #define ROTL(a, n) ((((a) << (n)) | ((a) >> ((-(n))&31))) & 0xffffffffL)
  88. #endif
  89. #define E_CAST(n, key, L, R, OP1, OP2, OP3) \
  90. { \
  91. uint32_t a, b, c, d; \
  92. t = (key[n * 2] OP1 R) & 0xffffffff; \
  93. t = ROTL(t, (key[n * 2 + 1])); \
  94. a = CAST_S_table0[(t >> 8) & 0xff]; \
  95. b = CAST_S_table1[(t)&0xff]; \
  96. c = CAST_S_table2[(t >> 24) & 0xff]; \
  97. d = CAST_S_table3[(t >> 16) & 0xff]; \
  98. L ^= (((((a OP2 b)&0xffffffffL)OP3 c) & 0xffffffffL)OP1 d) & 0xffffffffL; \
  99. }
  100. void CAST_encrypt(uint32_t *data, const CAST_KEY *key) {
  101. uint32_t l, r, t;
  102. const uint32_t *k;
  103. k = &key->data[0];
  104. l = data[0];
  105. r = data[1];
  106. E_CAST(0, k, l, r, +, ^, -);
  107. E_CAST(1, k, r, l, ^, -, +);
  108. E_CAST(2, k, l, r, -, +, ^);
  109. E_CAST(3, k, r, l, +, ^, -);
  110. E_CAST(4, k, l, r, ^, -, +);
  111. E_CAST(5, k, r, l, -, +, ^);
  112. E_CAST(6, k, l, r, +, ^, -);
  113. E_CAST(7, k, r, l, ^, -, +);
  114. E_CAST(8, k, l, r, -, +, ^);
  115. E_CAST(9, k, r, l, +, ^, -);
  116. E_CAST(10, k, l, r, ^, -, +);
  117. E_CAST(11, k, r, l, -, +, ^);
  118. if (!key->short_key) {
  119. E_CAST(12, k, l, r, +, ^, -);
  120. E_CAST(13, k, r, l, ^, -, +);
  121. E_CAST(14, k, l, r, -, +, ^);
  122. E_CAST(15, k, r, l, +, ^, -);
  123. }
  124. data[1] = l & 0xffffffffL;
  125. data[0] = r & 0xffffffffL;
  126. }
  127. void CAST_decrypt(uint32_t *data, const CAST_KEY *key) {
  128. uint32_t l, r, t;
  129. const uint32_t *k;
  130. k = &key->data[0];
  131. l = data[0];
  132. r = data[1];
  133. if (!key->short_key) {
  134. E_CAST(15, k, l, r, +, ^, -);
  135. E_CAST(14, k, r, l, -, +, ^);
  136. E_CAST(13, k, l, r, ^, -, +);
  137. E_CAST(12, k, r, l, +, ^, -);
  138. }
  139. E_CAST(11, k, l, r, -, +, ^);
  140. E_CAST(10, k, r, l, ^, -, +);
  141. E_CAST(9, k, l, r, +, ^, -);
  142. E_CAST(8, k, r, l, -, +, ^);
  143. E_CAST(7, k, l, r, ^, -, +);
  144. E_CAST(6, k, r, l, +, ^, -);
  145. E_CAST(5, k, l, r, -, +, ^);
  146. E_CAST(4, k, r, l, ^, -, +);
  147. E_CAST(3, k, l, r, +, ^, -);
  148. E_CAST(2, k, r, l, -, +, ^);
  149. E_CAST(1, k, l, r, ^, -, +);
  150. E_CAST(0, k, r, l, +, ^, -);
  151. data[1] = l & 0xffffffffL;
  152. data[0] = r & 0xffffffffL;
  153. }
  154. void CAST_cbc_encrypt(const uint8_t *in, uint8_t *out, long length,
  155. const CAST_KEY *ks, uint8_t *iv, int enc) {
  156. uint32_t tin0, tin1;
  157. uint32_t tout0, tout1, xor0, xor1;
  158. long l = length;
  159. uint32_t tin[2];
  160. if (enc) {
  161. n2l(iv, tout0);
  162. n2l(iv, tout1);
  163. iv -= 8;
  164. for (l -= 8; l >= 0; l -= 8) {
  165. n2l(in, tin0);
  166. n2l(in, tin1);
  167. tin0 ^= tout0;
  168. tin1 ^= tout1;
  169. tin[0] = tin0;
  170. tin[1] = tin1;
  171. CAST_encrypt(tin, ks);
  172. tout0 = tin[0];
  173. tout1 = tin[1];
  174. l2n(tout0, out);
  175. l2n(tout1, out);
  176. }
  177. if (l != -8) {
  178. n2ln(in, tin0, tin1, l + 8);
  179. tin0 ^= tout0;
  180. tin1 ^= tout1;
  181. tin[0] = tin0;
  182. tin[1] = tin1;
  183. CAST_encrypt(tin, ks);
  184. tout0 = tin[0];
  185. tout1 = tin[1];
  186. l2n(tout0, out);
  187. l2n(tout1, out);
  188. }
  189. l2n(tout0, iv);
  190. l2n(tout1, iv);
  191. } else {
  192. n2l(iv, xor0);
  193. n2l(iv, xor1);
  194. iv -= 8;
  195. for (l -= 8; l >= 0; l -= 8) {
  196. n2l(in, tin0);
  197. n2l(in, tin1);
  198. tin[0] = tin0;
  199. tin[1] = tin1;
  200. CAST_decrypt(tin, ks);
  201. tout0 = tin[0] ^ xor0;
  202. tout1 = tin[1] ^ xor1;
  203. l2n(tout0, out);
  204. l2n(tout1, out);
  205. xor0 = tin0;
  206. xor1 = tin1;
  207. }
  208. if (l != -8) {
  209. n2l(in, tin0);
  210. n2l(in, tin1);
  211. tin[0] = tin0;
  212. tin[1] = tin1;
  213. CAST_decrypt(tin, ks);
  214. tout0 = tin[0] ^ xor0;
  215. tout1 = tin[1] ^ xor1;
  216. l2nn(tout0, tout1, out, l + 8);
  217. xor0 = tin0;
  218. xor1 = tin1;
  219. }
  220. l2n(xor0, iv);
  221. l2n(xor1, iv);
  222. }
  223. tin0 = tin1 = tout0 = tout1 = xor0 = xor1 = 0;
  224. tin[0] = tin[1] = 0;
  225. }
  226. #define CAST_exp(l, A, a, n) \
  227. A[n / 4] = l; \
  228. a[n + 3] = (l)&0xff; \
  229. a[n + 2] = (l >> 8) & 0xff; \
  230. a[n + 1] = (l >> 16) & 0xff; \
  231. a[n + 0] = (l >> 24) & 0xff;
  232. #define S4 CAST_S_table4
  233. #define S5 CAST_S_table5
  234. #define S6 CAST_S_table6
  235. #define S7 CAST_S_table7
  236. void CAST_set_key(CAST_KEY *key, size_t len, const uint8_t *data) {
  237. uint32_t x[16];
  238. uint32_t z[16];
  239. uint32_t k[32];
  240. uint32_t X[4], Z[4];
  241. uint32_t l, *K;
  242. size_t i;
  243. for (i = 0; i < 16; i++) {
  244. x[i] = 0;
  245. }
  246. if (len > 16) {
  247. len = 16;
  248. }
  249. for (i = 0; i < len; i++) {
  250. x[i] = data[i];
  251. }
  252. if (len <= 10) {
  253. key->short_key = 1;
  254. } else {
  255. key->short_key = 0;
  256. }
  257. K = &k[0];
  258. X[0] = ((x[0] << 24) | (x[1] << 16) | (x[2] << 8) | x[3]) & 0xffffffffL;
  259. X[1] = ((x[4] << 24) | (x[5] << 16) | (x[6] << 8) | x[7]) & 0xffffffffL;
  260. X[2] = ((x[8] << 24) | (x[9] << 16) | (x[10] << 8) | x[11]) & 0xffffffffL;
  261. X[3] = ((x[12] << 24) | (x[13] << 16) | (x[14] << 8) | x[15]) & 0xffffffffL;
  262. for (;;) {
  263. l = X[0] ^ S4[x[13]] ^ S5[x[15]] ^ S6[x[12]] ^ S7[x[14]] ^ S6[x[8]];
  264. CAST_exp(l, Z, z, 0);
  265. l = X[2] ^ S4[z[0]] ^ S5[z[2]] ^ S6[z[1]] ^ S7[z[3]] ^ S7[x[10]];
  266. CAST_exp(l, Z, z, 4);
  267. l = X[3] ^ S4[z[7]] ^ S5[z[6]] ^ S6[z[5]] ^ S7[z[4]] ^ S4[x[9]];
  268. CAST_exp(l, Z, z, 8);
  269. l = X[1] ^ S4[z[10]] ^ S5[z[9]] ^ S6[z[11]] ^ S7[z[8]] ^ S5[x[11]];
  270. CAST_exp(l, Z, z, 12);
  271. K[0] = S4[z[8]] ^ S5[z[9]] ^ S6[z[7]] ^ S7[z[6]] ^ S4[z[2]];
  272. K[1] = S4[z[10]] ^ S5[z[11]] ^ S6[z[5]] ^ S7[z[4]] ^ S5[z[6]];
  273. K[2] = S4[z[12]] ^ S5[z[13]] ^ S6[z[3]] ^ S7[z[2]] ^ S6[z[9]];
  274. K[3] = S4[z[14]] ^ S5[z[15]] ^ S6[z[1]] ^ S7[z[0]] ^ S7[z[12]];
  275. l = Z[2] ^ S4[z[5]] ^ S5[z[7]] ^ S6[z[4]] ^ S7[z[6]] ^ S6[z[0]];
  276. CAST_exp(l, X, x, 0);
  277. l = Z[0] ^ S4[x[0]] ^ S5[x[2]] ^ S6[x[1]] ^ S7[x[3]] ^ S7[z[2]];
  278. CAST_exp(l, X, x, 4);
  279. l = Z[1] ^ S4[x[7]] ^ S5[x[6]] ^ S6[x[5]] ^ S7[x[4]] ^ S4[z[1]];
  280. CAST_exp(l, X, x, 8);
  281. l = Z[3] ^ S4[x[10]] ^ S5[x[9]] ^ S6[x[11]] ^ S7[x[8]] ^ S5[z[3]];
  282. CAST_exp(l, X, x, 12);
  283. K[4] = S4[x[3]] ^ S5[x[2]] ^ S6[x[12]] ^ S7[x[13]] ^ S4[x[8]];
  284. K[5] = S4[x[1]] ^ S5[x[0]] ^ S6[x[14]] ^ S7[x[15]] ^ S5[x[13]];
  285. K[6] = S4[x[7]] ^ S5[x[6]] ^ S6[x[8]] ^ S7[x[9]] ^ S6[x[3]];
  286. K[7] = S4[x[5]] ^ S5[x[4]] ^ S6[x[10]] ^ S7[x[11]] ^ S7[x[7]];
  287. l = X[0] ^ S4[x[13]] ^ S5[x[15]] ^ S6[x[12]] ^ S7[x[14]] ^ S6[x[8]];
  288. CAST_exp(l, Z, z, 0);
  289. l = X[2] ^ S4[z[0]] ^ S5[z[2]] ^ S6[z[1]] ^ S7[z[3]] ^ S7[x[10]];
  290. CAST_exp(l, Z, z, 4);
  291. l = X[3] ^ S4[z[7]] ^ S5[z[6]] ^ S6[z[5]] ^ S7[z[4]] ^ S4[x[9]];
  292. CAST_exp(l, Z, z, 8);
  293. l = X[1] ^ S4[z[10]] ^ S5[z[9]] ^ S6[z[11]] ^ S7[z[8]] ^ S5[x[11]];
  294. CAST_exp(l, Z, z, 12);
  295. K[8] = S4[z[3]] ^ S5[z[2]] ^ S6[z[12]] ^ S7[z[13]] ^ S4[z[9]];
  296. K[9] = S4[z[1]] ^ S5[z[0]] ^ S6[z[14]] ^ S7[z[15]] ^ S5[z[12]];
  297. K[10] = S4[z[7]] ^ S5[z[6]] ^ S6[z[8]] ^ S7[z[9]] ^ S6[z[2]];
  298. K[11] = S4[z[5]] ^ S5[z[4]] ^ S6[z[10]] ^ S7[z[11]] ^ S7[z[6]];
  299. l = Z[2] ^ S4[z[5]] ^ S5[z[7]] ^ S6[z[4]] ^ S7[z[6]] ^ S6[z[0]];
  300. CAST_exp(l, X, x, 0);
  301. l = Z[0] ^ S4[x[0]] ^ S5[x[2]] ^ S6[x[1]] ^ S7[x[3]] ^ S7[z[2]];
  302. CAST_exp(l, X, x, 4);
  303. l = Z[1] ^ S4[x[7]] ^ S5[x[6]] ^ S6[x[5]] ^ S7[x[4]] ^ S4[z[1]];
  304. CAST_exp(l, X, x, 8);
  305. l = Z[3] ^ S4[x[10]] ^ S5[x[9]] ^ S6[x[11]] ^ S7[x[8]] ^ S5[z[3]];
  306. CAST_exp(l, X, x, 12);
  307. K[12] = S4[x[8]] ^ S5[x[9]] ^ S6[x[7]] ^ S7[x[6]] ^ S4[x[3]];
  308. K[13] = S4[x[10]] ^ S5[x[11]] ^ S6[x[5]] ^ S7[x[4]] ^ S5[x[7]];
  309. K[14] = S4[x[12]] ^ S5[x[13]] ^ S6[x[3]] ^ S7[x[2]] ^ S6[x[8]];
  310. K[15] = S4[x[14]] ^ S5[x[15]] ^ S6[x[1]] ^ S7[x[0]] ^ S7[x[13]];
  311. if (K != k) {
  312. break;
  313. }
  314. K += 16;
  315. }
  316. for (i = 0; i < 16; i++) {
  317. key->data[i * 2] = k[i];
  318. key->data[i * 2 + 1] = ((k[i + 16]) + 16) & 0x1f;
  319. }
  320. }
  321. /* The input and output encrypted as though 64bit cfb mode is being used. The
  322. * extra state information to record how much of the 64bit block we have used
  323. * is contained in *num. */
  324. void CAST_cfb64_encrypt(const uint8_t *in, uint8_t *out, long length,
  325. const CAST_KEY *schedule, uint8_t *ivec, int *num,
  326. int enc) {
  327. uint32_t v0, v1, t;
  328. int n = *num;
  329. long l = length;
  330. uint32_t ti[2];
  331. uint8_t *iv, c, cc;
  332. iv = ivec;
  333. if (enc) {
  334. while (l--) {
  335. if (n == 0) {
  336. n2l(iv, v0);
  337. ti[0] = v0;
  338. n2l(iv, v1);
  339. ti[1] = v1;
  340. CAST_encrypt((uint32_t *)ti, schedule);
  341. iv = ivec;
  342. t = ti[0];
  343. l2n(t, iv);
  344. t = ti[1];
  345. l2n(t, iv);
  346. iv = ivec;
  347. }
  348. c = *(in++) ^ iv[n];
  349. *(out++) = c;
  350. iv[n] = c;
  351. n = (n + 1) & 0x07;
  352. }
  353. } else {
  354. while (l--) {
  355. if (n == 0) {
  356. n2l(iv, v0);
  357. ti[0] = v0;
  358. n2l(iv, v1);
  359. ti[1] = v1;
  360. CAST_encrypt((uint32_t *)ti, schedule);
  361. iv = ivec;
  362. t = ti[0];
  363. l2n(t, iv);
  364. t = ti[1];
  365. l2n(t, iv);
  366. iv = ivec;
  367. }
  368. cc = *(in++);
  369. c = iv[n];
  370. iv[n] = cc;
  371. *(out++) = c ^ cc;
  372. n = (n + 1) & 0x07;
  373. }
  374. }
  375. v0 = v1 = ti[0] = ti[1] = t = c = cc = 0;
  376. *num = n;
  377. }