serpent_generic.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  1. /*
  2. * Cryptographic API.
  3. *
  4. * Serpent Cipher Algorithm.
  5. *
  6. * Copyright (C) 2002 Dag Arne Osvik <osvik@ii.uib.no>
  7. * 2003 Herbert Valerio Riedel <hvr@gnu.org>
  8. *
  9. * Added tnepres support:
  10. * Ruben Jesus Garcia Hernandez <ruben@ugr.es>, 18.10.2004
  11. * Based on code by hvr
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. */
  18. #include <linux/init.h>
  19. #include <linux/module.h>
  20. #include <linux/errno.h>
  21. #include <asm/byteorder.h>
  22. #include <linux/crypto.h>
  23. #include <linux/types.h>
  24. #include <crypto/serpent.h>
  25. /* Key is padded to the maximum of 256 bits before round key generation.
  26. * Any key length <= 256 bits (32 bytes) is allowed by the algorithm.
  27. */
  28. #define PHI 0x9e3779b9UL
  29. #define keyiter(a, b, c, d, i, j) \
  30. ({ b ^= d; b ^= c; b ^= a; b ^= PHI ^ i; b = rol32(b, 11); k[j] = b; })
  31. #define loadkeys(x0, x1, x2, x3, i) \
  32. ({ x0 = k[i]; x1 = k[i+1]; x2 = k[i+2]; x3 = k[i+3]; })
  33. #define storekeys(x0, x1, x2, x3, i) \
  34. ({ k[i] = x0; k[i+1] = x1; k[i+2] = x2; k[i+3] = x3; })
  35. #define store_and_load_keys(x0, x1, x2, x3, s, l) \
  36. ({ storekeys(x0, x1, x2, x3, s); loadkeys(x0, x1, x2, x3, l); })
  37. #define K(x0, x1, x2, x3, i) ({ \
  38. x3 ^= k[4*(i)+3]; x2 ^= k[4*(i)+2]; \
  39. x1 ^= k[4*(i)+1]; x0 ^= k[4*(i)+0]; \
  40. })
  41. #define LK(x0, x1, x2, x3, x4, i) ({ \
  42. x0 = rol32(x0, 13);\
  43. x2 = rol32(x2, 3); x1 ^= x0; x4 = x0 << 3; \
  44. x3 ^= x2; x1 ^= x2; \
  45. x1 = rol32(x1, 1); x3 ^= x4; \
  46. x3 = rol32(x3, 7); x4 = x1; \
  47. x0 ^= x1; x4 <<= 7; x2 ^= x3; \
  48. x0 ^= x3; x2 ^= x4; x3 ^= k[4*i+3]; \
  49. x1 ^= k[4*i+1]; x0 = rol32(x0, 5); x2 = rol32(x2, 22);\
  50. x0 ^= k[4*i+0]; x2 ^= k[4*i+2]; \
  51. })
  52. #define KL(x0, x1, x2, x3, x4, i) ({ \
  53. x0 ^= k[4*i+0]; x1 ^= k[4*i+1]; x2 ^= k[4*i+2]; \
  54. x3 ^= k[4*i+3]; x0 = ror32(x0, 5); x2 = ror32(x2, 22);\
  55. x4 = x1; x2 ^= x3; x0 ^= x3; \
  56. x4 <<= 7; x0 ^= x1; x1 = ror32(x1, 1); \
  57. x2 ^= x4; x3 = ror32(x3, 7); x4 = x0 << 3; \
  58. x1 ^= x0; x3 ^= x4; x0 = ror32(x0, 13);\
  59. x1 ^= x2; x3 ^= x2; x2 = ror32(x2, 3); \
  60. })
  61. #define S0(x0, x1, x2, x3, x4) ({ \
  62. x4 = x3; \
  63. x3 |= x0; x0 ^= x4; x4 ^= x2; \
  64. x4 = ~x4; x3 ^= x1; x1 &= x0; \
  65. x1 ^= x4; x2 ^= x0; x0 ^= x3; \
  66. x4 |= x0; x0 ^= x2; x2 &= x1; \
  67. x3 ^= x2; x1 = ~x1; x2 ^= x4; \
  68. x1 ^= x2; \
  69. })
  70. #define S1(x0, x1, x2, x3, x4) ({ \
  71. x4 = x1; \
  72. x1 ^= x0; x0 ^= x3; x3 = ~x3; \
  73. x4 &= x1; x0 |= x1; x3 ^= x2; \
  74. x0 ^= x3; x1 ^= x3; x3 ^= x4; \
  75. x1 |= x4; x4 ^= x2; x2 &= x0; \
  76. x2 ^= x1; x1 |= x0; x0 = ~x0; \
  77. x0 ^= x2; x4 ^= x1; \
  78. })
  79. #define S2(x0, x1, x2, x3, x4) ({ \
  80. x3 = ~x3; \
  81. x1 ^= x0; x4 = x0; x0 &= x2; \
  82. x0 ^= x3; x3 |= x4; x2 ^= x1; \
  83. x3 ^= x1; x1 &= x0; x0 ^= x2; \
  84. x2 &= x3; x3 |= x1; x0 = ~x0; \
  85. x3 ^= x0; x4 ^= x0; x0 ^= x2; \
  86. x1 |= x2; \
  87. })
  88. #define S3(x0, x1, x2, x3, x4) ({ \
  89. x4 = x1; \
  90. x1 ^= x3; x3 |= x0; x4 &= x0; \
  91. x0 ^= x2; x2 ^= x1; x1 &= x3; \
  92. x2 ^= x3; x0 |= x4; x4 ^= x3; \
  93. x1 ^= x0; x0 &= x3; x3 &= x4; \
  94. x3 ^= x2; x4 |= x1; x2 &= x1; \
  95. x4 ^= x3; x0 ^= x3; x3 ^= x2; \
  96. })
  97. #define S4(x0, x1, x2, x3, x4) ({ \
  98. x4 = x3; \
  99. x3 &= x0; x0 ^= x4; \
  100. x3 ^= x2; x2 |= x4; x0 ^= x1; \
  101. x4 ^= x3; x2 |= x0; \
  102. x2 ^= x1; x1 &= x0; \
  103. x1 ^= x4; x4 &= x2; x2 ^= x3; \
  104. x4 ^= x0; x3 |= x1; x1 = ~x1; \
  105. x3 ^= x0; \
  106. })
  107. #define S5(x0, x1, x2, x3, x4) ({ \
  108. x4 = x1; x1 |= x0; \
  109. x2 ^= x1; x3 = ~x3; x4 ^= x0; \
  110. x0 ^= x2; x1 &= x4; x4 |= x3; \
  111. x4 ^= x0; x0 &= x3; x1 ^= x3; \
  112. x3 ^= x2; x0 ^= x1; x2 &= x4; \
  113. x1 ^= x2; x2 &= x0; \
  114. x3 ^= x2; \
  115. })
  116. #define S6(x0, x1, x2, x3, x4) ({ \
  117. x4 = x1; \
  118. x3 ^= x0; x1 ^= x2; x2 ^= x0; \
  119. x0 &= x3; x1 |= x3; x4 = ~x4; \
  120. x0 ^= x1; x1 ^= x2; \
  121. x3 ^= x4; x4 ^= x0; x2 &= x0; \
  122. x4 ^= x1; x2 ^= x3; x3 &= x1; \
  123. x3 ^= x0; x1 ^= x2; \
  124. })
  125. #define S7(x0, x1, x2, x3, x4) ({ \
  126. x1 = ~x1; \
  127. x4 = x1; x0 = ~x0; x1 &= x2; \
  128. x1 ^= x3; x3 |= x4; x4 ^= x2; \
  129. x2 ^= x3; x3 ^= x0; x0 |= x1; \
  130. x2 &= x0; x0 ^= x4; x4 ^= x3; \
  131. x3 &= x0; x4 ^= x1; \
  132. x2 ^= x4; x3 ^= x1; x4 |= x0; \
  133. x4 ^= x1; \
  134. })
  135. #define SI0(x0, x1, x2, x3, x4) ({ \
  136. x4 = x3; x1 ^= x0; \
  137. x3 |= x1; x4 ^= x1; x0 = ~x0; \
  138. x2 ^= x3; x3 ^= x0; x0 &= x1; \
  139. x0 ^= x2; x2 &= x3; x3 ^= x4; \
  140. x2 ^= x3; x1 ^= x3; x3 &= x0; \
  141. x1 ^= x0; x0 ^= x2; x4 ^= x3; \
  142. })
  143. #define SI1(x0, x1, x2, x3, x4) ({ \
  144. x1 ^= x3; x4 = x0; \
  145. x0 ^= x2; x2 = ~x2; x4 |= x1; \
  146. x4 ^= x3; x3 &= x1; x1 ^= x2; \
  147. x2 &= x4; x4 ^= x1; x1 |= x3; \
  148. x3 ^= x0; x2 ^= x0; x0 |= x4; \
  149. x2 ^= x4; x1 ^= x0; \
  150. x4 ^= x1; \
  151. })
  152. #define SI2(x0, x1, x2, x3, x4) ({ \
  153. x2 ^= x1; x4 = x3; x3 = ~x3; \
  154. x3 |= x2; x2 ^= x4; x4 ^= x0; \
  155. x3 ^= x1; x1 |= x2; x2 ^= x0; \
  156. x1 ^= x4; x4 |= x3; x2 ^= x3; \
  157. x4 ^= x2; x2 &= x1; \
  158. x2 ^= x3; x3 ^= x4; x4 ^= x0; \
  159. })
  160. #define SI3(x0, x1, x2, x3, x4) ({ \
  161. x2 ^= x1; \
  162. x4 = x1; x1 &= x2; \
  163. x1 ^= x0; x0 |= x4; x4 ^= x3; \
  164. x0 ^= x3; x3 |= x1; x1 ^= x2; \
  165. x1 ^= x3; x0 ^= x2; x2 ^= x3; \
  166. x3 &= x1; x1 ^= x0; x0 &= x2; \
  167. x4 ^= x3; x3 ^= x0; x0 ^= x1; \
  168. })
  169. #define SI4(x0, x1, x2, x3, x4) ({ \
  170. x2 ^= x3; x4 = x0; x0 &= x1; \
  171. x0 ^= x2; x2 |= x3; x4 = ~x4; \
  172. x1 ^= x0; x0 ^= x2; x2 &= x4; \
  173. x2 ^= x0; x0 |= x4; \
  174. x0 ^= x3; x3 &= x2; \
  175. x4 ^= x3; x3 ^= x1; x1 &= x0; \
  176. x4 ^= x1; x0 ^= x3; \
  177. })
  178. #define SI5(x0, x1, x2, x3, x4) ({ \
  179. x4 = x1; x1 |= x2; \
  180. x2 ^= x4; x1 ^= x3; x3 &= x4; \
  181. x2 ^= x3; x3 |= x0; x0 = ~x0; \
  182. x3 ^= x2; x2 |= x0; x4 ^= x1; \
  183. x2 ^= x4; x4 &= x0; x0 ^= x1; \
  184. x1 ^= x3; x0 &= x2; x2 ^= x3; \
  185. x0 ^= x2; x2 ^= x4; x4 ^= x3; \
  186. })
  187. #define SI6(x0, x1, x2, x3, x4) ({ \
  188. x0 ^= x2; \
  189. x4 = x0; x0 &= x3; x2 ^= x3; \
  190. x0 ^= x2; x3 ^= x1; x2 |= x4; \
  191. x2 ^= x3; x3 &= x0; x0 = ~x0; \
  192. x3 ^= x1; x1 &= x2; x4 ^= x0; \
  193. x3 ^= x4; x4 ^= x2; x0 ^= x1; \
  194. x2 ^= x0; \
  195. })
  196. #define SI7(x0, x1, x2, x3, x4) ({ \
  197. x4 = x3; x3 &= x0; x0 ^= x2; \
  198. x2 |= x4; x4 ^= x1; x0 = ~x0; \
  199. x1 |= x3; x4 ^= x0; x0 &= x2; \
  200. x0 ^= x1; x1 &= x2; x3 ^= x2; \
  201. x4 ^= x3; x2 &= x3; x3 |= x0; \
  202. x1 ^= x4; x3 ^= x4; x4 &= x0; \
  203. x4 ^= x2; \
  204. })
  205. int __serpent_setkey(struct serpent_ctx *ctx, const u8 *key,
  206. unsigned int keylen)
  207. {
  208. u32 *k = ctx->expkey;
  209. u8 *k8 = (u8 *)k;
  210. u32 r0, r1, r2, r3, r4;
  211. int i;
  212. /* Copy key, add padding */
  213. for (i = 0; i < keylen; ++i)
  214. k8[i] = key[i];
  215. if (i < SERPENT_MAX_KEY_SIZE)
  216. k8[i++] = 1;
  217. while (i < SERPENT_MAX_KEY_SIZE)
  218. k8[i++] = 0;
  219. /* Expand key using polynomial */
  220. r0 = le32_to_cpu(k[3]);
  221. r1 = le32_to_cpu(k[4]);
  222. r2 = le32_to_cpu(k[5]);
  223. r3 = le32_to_cpu(k[6]);
  224. r4 = le32_to_cpu(k[7]);
  225. keyiter(le32_to_cpu(k[0]), r0, r4, r2, 0, 0);
  226. keyiter(le32_to_cpu(k[1]), r1, r0, r3, 1, 1);
  227. keyiter(le32_to_cpu(k[2]), r2, r1, r4, 2, 2);
  228. keyiter(le32_to_cpu(k[3]), r3, r2, r0, 3, 3);
  229. keyiter(le32_to_cpu(k[4]), r4, r3, r1, 4, 4);
  230. keyiter(le32_to_cpu(k[5]), r0, r4, r2, 5, 5);
  231. keyiter(le32_to_cpu(k[6]), r1, r0, r3, 6, 6);
  232. keyiter(le32_to_cpu(k[7]), r2, r1, r4, 7, 7);
  233. keyiter(k[0], r3, r2, r0, 8, 8);
  234. keyiter(k[1], r4, r3, r1, 9, 9);
  235. keyiter(k[2], r0, r4, r2, 10, 10);
  236. keyiter(k[3], r1, r0, r3, 11, 11);
  237. keyiter(k[4], r2, r1, r4, 12, 12);
  238. keyiter(k[5], r3, r2, r0, 13, 13);
  239. keyiter(k[6], r4, r3, r1, 14, 14);
  240. keyiter(k[7], r0, r4, r2, 15, 15);
  241. keyiter(k[8], r1, r0, r3, 16, 16);
  242. keyiter(k[9], r2, r1, r4, 17, 17);
  243. keyiter(k[10], r3, r2, r0, 18, 18);
  244. keyiter(k[11], r4, r3, r1, 19, 19);
  245. keyiter(k[12], r0, r4, r2, 20, 20);
  246. keyiter(k[13], r1, r0, r3, 21, 21);
  247. keyiter(k[14], r2, r1, r4, 22, 22);
  248. keyiter(k[15], r3, r2, r0, 23, 23);
  249. keyiter(k[16], r4, r3, r1, 24, 24);
  250. keyiter(k[17], r0, r4, r2, 25, 25);
  251. keyiter(k[18], r1, r0, r3, 26, 26);
  252. keyiter(k[19], r2, r1, r4, 27, 27);
  253. keyiter(k[20], r3, r2, r0, 28, 28);
  254. keyiter(k[21], r4, r3, r1, 29, 29);
  255. keyiter(k[22], r0, r4, r2, 30, 30);
  256. keyiter(k[23], r1, r0, r3, 31, 31);
  257. k += 50;
  258. keyiter(k[-26], r2, r1, r4, 32, -18);
  259. keyiter(k[-25], r3, r2, r0, 33, -17);
  260. keyiter(k[-24], r4, r3, r1, 34, -16);
  261. keyiter(k[-23], r0, r4, r2, 35, -15);
  262. keyiter(k[-22], r1, r0, r3, 36, -14);
  263. keyiter(k[-21], r2, r1, r4, 37, -13);
  264. keyiter(k[-20], r3, r2, r0, 38, -12);
  265. keyiter(k[-19], r4, r3, r1, 39, -11);
  266. keyiter(k[-18], r0, r4, r2, 40, -10);
  267. keyiter(k[-17], r1, r0, r3, 41, -9);
  268. keyiter(k[-16], r2, r1, r4, 42, -8);
  269. keyiter(k[-15], r3, r2, r0, 43, -7);
  270. keyiter(k[-14], r4, r3, r1, 44, -6);
  271. keyiter(k[-13], r0, r4, r2, 45, -5);
  272. keyiter(k[-12], r1, r0, r3, 46, -4);
  273. keyiter(k[-11], r2, r1, r4, 47, -3);
  274. keyiter(k[-10], r3, r2, r0, 48, -2);
  275. keyiter(k[-9], r4, r3, r1, 49, -1);
  276. keyiter(k[-8], r0, r4, r2, 50, 0);
  277. keyiter(k[-7], r1, r0, r3, 51, 1);
  278. keyiter(k[-6], r2, r1, r4, 52, 2);
  279. keyiter(k[-5], r3, r2, r0, 53, 3);
  280. keyiter(k[-4], r4, r3, r1, 54, 4);
  281. keyiter(k[-3], r0, r4, r2, 55, 5);
  282. keyiter(k[-2], r1, r0, r3, 56, 6);
  283. keyiter(k[-1], r2, r1, r4, 57, 7);
  284. keyiter(k[0], r3, r2, r0, 58, 8);
  285. keyiter(k[1], r4, r3, r1, 59, 9);
  286. keyiter(k[2], r0, r4, r2, 60, 10);
  287. keyiter(k[3], r1, r0, r3, 61, 11);
  288. keyiter(k[4], r2, r1, r4, 62, 12);
  289. keyiter(k[5], r3, r2, r0, 63, 13);
  290. keyiter(k[6], r4, r3, r1, 64, 14);
  291. keyiter(k[7], r0, r4, r2, 65, 15);
  292. keyiter(k[8], r1, r0, r3, 66, 16);
  293. keyiter(k[9], r2, r1, r4, 67, 17);
  294. keyiter(k[10], r3, r2, r0, 68, 18);
  295. keyiter(k[11], r4, r3, r1, 69, 19);
  296. keyiter(k[12], r0, r4, r2, 70, 20);
  297. keyiter(k[13], r1, r0, r3, 71, 21);
  298. keyiter(k[14], r2, r1, r4, 72, 22);
  299. keyiter(k[15], r3, r2, r0, 73, 23);
  300. keyiter(k[16], r4, r3, r1, 74, 24);
  301. keyiter(k[17], r0, r4, r2, 75, 25);
  302. keyiter(k[18], r1, r0, r3, 76, 26);
  303. keyiter(k[19], r2, r1, r4, 77, 27);
  304. keyiter(k[20], r3, r2, r0, 78, 28);
  305. keyiter(k[21], r4, r3, r1, 79, 29);
  306. keyiter(k[22], r0, r4, r2, 80, 30);
  307. keyiter(k[23], r1, r0, r3, 81, 31);
  308. k += 50;
  309. keyiter(k[-26], r2, r1, r4, 82, -18);
  310. keyiter(k[-25], r3, r2, r0, 83, -17);
  311. keyiter(k[-24], r4, r3, r1, 84, -16);
  312. keyiter(k[-23], r0, r4, r2, 85, -15);
  313. keyiter(k[-22], r1, r0, r3, 86, -14);
  314. keyiter(k[-21], r2, r1, r4, 87, -13);
  315. keyiter(k[-20], r3, r2, r0, 88, -12);
  316. keyiter(k[-19], r4, r3, r1, 89, -11);
  317. keyiter(k[-18], r0, r4, r2, 90, -10);
  318. keyiter(k[-17], r1, r0, r3, 91, -9);
  319. keyiter(k[-16], r2, r1, r4, 92, -8);
  320. keyiter(k[-15], r3, r2, r0, 93, -7);
  321. keyiter(k[-14], r4, r3, r1, 94, -6);
  322. keyiter(k[-13], r0, r4, r2, 95, -5);
  323. keyiter(k[-12], r1, r0, r3, 96, -4);
  324. keyiter(k[-11], r2, r1, r4, 97, -3);
  325. keyiter(k[-10], r3, r2, r0, 98, -2);
  326. keyiter(k[-9], r4, r3, r1, 99, -1);
  327. keyiter(k[-8], r0, r4, r2, 100, 0);
  328. keyiter(k[-7], r1, r0, r3, 101, 1);
  329. keyiter(k[-6], r2, r1, r4, 102, 2);
  330. keyiter(k[-5], r3, r2, r0, 103, 3);
  331. keyiter(k[-4], r4, r3, r1, 104, 4);
  332. keyiter(k[-3], r0, r4, r2, 105, 5);
  333. keyiter(k[-2], r1, r0, r3, 106, 6);
  334. keyiter(k[-1], r2, r1, r4, 107, 7);
  335. keyiter(k[0], r3, r2, r0, 108, 8);
  336. keyiter(k[1], r4, r3, r1, 109, 9);
  337. keyiter(k[2], r0, r4, r2, 110, 10);
  338. keyiter(k[3], r1, r0, r3, 111, 11);
  339. keyiter(k[4], r2, r1, r4, 112, 12);
  340. keyiter(k[5], r3, r2, r0, 113, 13);
  341. keyiter(k[6], r4, r3, r1, 114, 14);
  342. keyiter(k[7], r0, r4, r2, 115, 15);
  343. keyiter(k[8], r1, r0, r3, 116, 16);
  344. keyiter(k[9], r2, r1, r4, 117, 17);
  345. keyiter(k[10], r3, r2, r0, 118, 18);
  346. keyiter(k[11], r4, r3, r1, 119, 19);
  347. keyiter(k[12], r0, r4, r2, 120, 20);
  348. keyiter(k[13], r1, r0, r3, 121, 21);
  349. keyiter(k[14], r2, r1, r4, 122, 22);
  350. keyiter(k[15], r3, r2, r0, 123, 23);
  351. keyiter(k[16], r4, r3, r1, 124, 24);
  352. keyiter(k[17], r0, r4, r2, 125, 25);
  353. keyiter(k[18], r1, r0, r3, 126, 26);
  354. keyiter(k[19], r2, r1, r4, 127, 27);
  355. keyiter(k[20], r3, r2, r0, 128, 28);
  356. keyiter(k[21], r4, r3, r1, 129, 29);
  357. keyiter(k[22], r0, r4, r2, 130, 30);
  358. keyiter(k[23], r1, r0, r3, 131, 31);
  359. /* Apply S-boxes */
  360. S3(r3, r4, r0, r1, r2); store_and_load_keys(r1, r2, r4, r3, 28, 24);
  361. S4(r1, r2, r4, r3, r0); store_and_load_keys(r2, r4, r3, r0, 24, 20);
  362. S5(r2, r4, r3, r0, r1); store_and_load_keys(r1, r2, r4, r0, 20, 16);
  363. S6(r1, r2, r4, r0, r3); store_and_load_keys(r4, r3, r2, r0, 16, 12);
  364. S7(r4, r3, r2, r0, r1); store_and_load_keys(r1, r2, r0, r4, 12, 8);
  365. S0(r1, r2, r0, r4, r3); store_and_load_keys(r0, r2, r4, r1, 8, 4);
  366. S1(r0, r2, r4, r1, r3); store_and_load_keys(r3, r4, r1, r0, 4, 0);
  367. S2(r3, r4, r1, r0, r2); store_and_load_keys(r2, r4, r3, r0, 0, -4);
  368. S3(r2, r4, r3, r0, r1); store_and_load_keys(r0, r1, r4, r2, -4, -8);
  369. S4(r0, r1, r4, r2, r3); store_and_load_keys(r1, r4, r2, r3, -8, -12);
  370. S5(r1, r4, r2, r3, r0); store_and_load_keys(r0, r1, r4, r3, -12, -16);
  371. S6(r0, r1, r4, r3, r2); store_and_load_keys(r4, r2, r1, r3, -16, -20);
  372. S7(r4, r2, r1, r3, r0); store_and_load_keys(r0, r1, r3, r4, -20, -24);
  373. S0(r0, r1, r3, r4, r2); store_and_load_keys(r3, r1, r4, r0, -24, -28);
  374. k -= 50;
  375. S1(r3, r1, r4, r0, r2); store_and_load_keys(r2, r4, r0, r3, 22, 18);
  376. S2(r2, r4, r0, r3, r1); store_and_load_keys(r1, r4, r2, r3, 18, 14);
  377. S3(r1, r4, r2, r3, r0); store_and_load_keys(r3, r0, r4, r1, 14, 10);
  378. S4(r3, r0, r4, r1, r2); store_and_load_keys(r0, r4, r1, r2, 10, 6);
  379. S5(r0, r4, r1, r2, r3); store_and_load_keys(r3, r0, r4, r2, 6, 2);
  380. S6(r3, r0, r4, r2, r1); store_and_load_keys(r4, r1, r0, r2, 2, -2);
  381. S7(r4, r1, r0, r2, r3); store_and_load_keys(r3, r0, r2, r4, -2, -6);
  382. S0(r3, r0, r2, r4, r1); store_and_load_keys(r2, r0, r4, r3, -6, -10);
  383. S1(r2, r0, r4, r3, r1); store_and_load_keys(r1, r4, r3, r2, -10, -14);
  384. S2(r1, r4, r3, r2, r0); store_and_load_keys(r0, r4, r1, r2, -14, -18);
  385. S3(r0, r4, r1, r2, r3); store_and_load_keys(r2, r3, r4, r0, -18, -22);
  386. k -= 50;
  387. S4(r2, r3, r4, r0, r1); store_and_load_keys(r3, r4, r0, r1, 28, 24);
  388. S5(r3, r4, r0, r1, r2); store_and_load_keys(r2, r3, r4, r1, 24, 20);
  389. S6(r2, r3, r4, r1, r0); store_and_load_keys(r4, r0, r3, r1, 20, 16);
  390. S7(r4, r0, r3, r1, r2); store_and_load_keys(r2, r3, r1, r4, 16, 12);
  391. S0(r2, r3, r1, r4, r0); store_and_load_keys(r1, r3, r4, r2, 12, 8);
  392. S1(r1, r3, r4, r2, r0); store_and_load_keys(r0, r4, r2, r1, 8, 4);
  393. S2(r0, r4, r2, r1, r3); store_and_load_keys(r3, r4, r0, r1, 4, 0);
  394. S3(r3, r4, r0, r1, r2); storekeys(r1, r2, r4, r3, 0);
  395. return 0;
  396. }
  397. EXPORT_SYMBOL_GPL(__serpent_setkey);
  398. int serpent_setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen)
  399. {
  400. return __serpent_setkey(crypto_tfm_ctx(tfm), key, keylen);
  401. }
  402. EXPORT_SYMBOL_GPL(serpent_setkey);
  403. void __serpent_encrypt(struct serpent_ctx *ctx, u8 *dst, const u8 *src)
  404. {
  405. const u32 *k = ctx->expkey;
  406. const __le32 *s = (const __le32 *)src;
  407. __le32 *d = (__le32 *)dst;
  408. u32 r0, r1, r2, r3, r4;
  409. /*
  410. * Note: The conversions between u8* and u32* might cause trouble
  411. * on architectures with stricter alignment rules than x86
  412. */
  413. r0 = le32_to_cpu(s[0]);
  414. r1 = le32_to_cpu(s[1]);
  415. r2 = le32_to_cpu(s[2]);
  416. r3 = le32_to_cpu(s[3]);
  417. K(r0, r1, r2, r3, 0);
  418. S0(r0, r1, r2, r3, r4); LK(r2, r1, r3, r0, r4, 1);
  419. S1(r2, r1, r3, r0, r4); LK(r4, r3, r0, r2, r1, 2);
  420. S2(r4, r3, r0, r2, r1); LK(r1, r3, r4, r2, r0, 3);
  421. S3(r1, r3, r4, r2, r0); LK(r2, r0, r3, r1, r4, 4);
  422. S4(r2, r0, r3, r1, r4); LK(r0, r3, r1, r4, r2, 5);
  423. S5(r0, r3, r1, r4, r2); LK(r2, r0, r3, r4, r1, 6);
  424. S6(r2, r0, r3, r4, r1); LK(r3, r1, r0, r4, r2, 7);
  425. S7(r3, r1, r0, r4, r2); LK(r2, r0, r4, r3, r1, 8);
  426. S0(r2, r0, r4, r3, r1); LK(r4, r0, r3, r2, r1, 9);
  427. S1(r4, r0, r3, r2, r1); LK(r1, r3, r2, r4, r0, 10);
  428. S2(r1, r3, r2, r4, r0); LK(r0, r3, r1, r4, r2, 11);
  429. S3(r0, r3, r1, r4, r2); LK(r4, r2, r3, r0, r1, 12);
  430. S4(r4, r2, r3, r0, r1); LK(r2, r3, r0, r1, r4, 13);
  431. S5(r2, r3, r0, r1, r4); LK(r4, r2, r3, r1, r0, 14);
  432. S6(r4, r2, r3, r1, r0); LK(r3, r0, r2, r1, r4, 15);
  433. S7(r3, r0, r2, r1, r4); LK(r4, r2, r1, r3, r0, 16);
  434. S0(r4, r2, r1, r3, r0); LK(r1, r2, r3, r4, r0, 17);
  435. S1(r1, r2, r3, r4, r0); LK(r0, r3, r4, r1, r2, 18);
  436. S2(r0, r3, r4, r1, r2); LK(r2, r3, r0, r1, r4, 19);
  437. S3(r2, r3, r0, r1, r4); LK(r1, r4, r3, r2, r0, 20);
  438. S4(r1, r4, r3, r2, r0); LK(r4, r3, r2, r0, r1, 21);
  439. S5(r4, r3, r2, r0, r1); LK(r1, r4, r3, r0, r2, 22);
  440. S6(r1, r4, r3, r0, r2); LK(r3, r2, r4, r0, r1, 23);
  441. S7(r3, r2, r4, r0, r1); LK(r1, r4, r0, r3, r2, 24);
  442. S0(r1, r4, r0, r3, r2); LK(r0, r4, r3, r1, r2, 25);
  443. S1(r0, r4, r3, r1, r2); LK(r2, r3, r1, r0, r4, 26);
  444. S2(r2, r3, r1, r0, r4); LK(r4, r3, r2, r0, r1, 27);
  445. S3(r4, r3, r2, r0, r1); LK(r0, r1, r3, r4, r2, 28);
  446. S4(r0, r1, r3, r4, r2); LK(r1, r3, r4, r2, r0, 29);
  447. S5(r1, r3, r4, r2, r0); LK(r0, r1, r3, r2, r4, 30);
  448. S6(r0, r1, r3, r2, r4); LK(r3, r4, r1, r2, r0, 31);
  449. S7(r3, r4, r1, r2, r0); K(r0, r1, r2, r3, 32);
  450. d[0] = cpu_to_le32(r0);
  451. d[1] = cpu_to_le32(r1);
  452. d[2] = cpu_to_le32(r2);
  453. d[3] = cpu_to_le32(r3);
  454. }
  455. EXPORT_SYMBOL_GPL(__serpent_encrypt);
  456. static void serpent_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  457. {
  458. struct serpent_ctx *ctx = crypto_tfm_ctx(tfm);
  459. __serpent_encrypt(ctx, dst, src);
  460. }
  461. void __serpent_decrypt(struct serpent_ctx *ctx, u8 *dst, const u8 *src)
  462. {
  463. const u32 *k = ctx->expkey;
  464. const __le32 *s = (const __le32 *)src;
  465. __le32 *d = (__le32 *)dst;
  466. u32 r0, r1, r2, r3, r4;
  467. r0 = le32_to_cpu(s[0]);
  468. r1 = le32_to_cpu(s[1]);
  469. r2 = le32_to_cpu(s[2]);
  470. r3 = le32_to_cpu(s[3]);
  471. K(r0, r1, r2, r3, 32);
  472. SI7(r0, r1, r2, r3, r4); KL(r1, r3, r0, r4, r2, 31);
  473. SI6(r1, r3, r0, r4, r2); KL(r0, r2, r4, r1, r3, 30);
  474. SI5(r0, r2, r4, r1, r3); KL(r2, r3, r0, r4, r1, 29);
  475. SI4(r2, r3, r0, r4, r1); KL(r2, r0, r1, r4, r3, 28);
  476. SI3(r2, r0, r1, r4, r3); KL(r1, r2, r3, r4, r0, 27);
  477. SI2(r1, r2, r3, r4, r0); KL(r2, r0, r4, r3, r1, 26);
  478. SI1(r2, r0, r4, r3, r1); KL(r1, r0, r4, r3, r2, 25);
  479. SI0(r1, r0, r4, r3, r2); KL(r4, r2, r0, r1, r3, 24);
  480. SI7(r4, r2, r0, r1, r3); KL(r2, r1, r4, r3, r0, 23);
  481. SI6(r2, r1, r4, r3, r0); KL(r4, r0, r3, r2, r1, 22);
  482. SI5(r4, r0, r3, r2, r1); KL(r0, r1, r4, r3, r2, 21);
  483. SI4(r0, r1, r4, r3, r2); KL(r0, r4, r2, r3, r1, 20);
  484. SI3(r0, r4, r2, r3, r1); KL(r2, r0, r1, r3, r4, 19);
  485. SI2(r2, r0, r1, r3, r4); KL(r0, r4, r3, r1, r2, 18);
  486. SI1(r0, r4, r3, r1, r2); KL(r2, r4, r3, r1, r0, 17);
  487. SI0(r2, r4, r3, r1, r0); KL(r3, r0, r4, r2, r1, 16);
  488. SI7(r3, r0, r4, r2, r1); KL(r0, r2, r3, r1, r4, 15);
  489. SI6(r0, r2, r3, r1, r4); KL(r3, r4, r1, r0, r2, 14);
  490. SI5(r3, r4, r1, r0, r2); KL(r4, r2, r3, r1, r0, 13);
  491. SI4(r4, r2, r3, r1, r0); KL(r4, r3, r0, r1, r2, 12);
  492. SI3(r4, r3, r0, r1, r2); KL(r0, r4, r2, r1, r3, 11);
  493. SI2(r0, r4, r2, r1, r3); KL(r4, r3, r1, r2, r0, 10);
  494. SI1(r4, r3, r1, r2, r0); KL(r0, r3, r1, r2, r4, 9);
  495. SI0(r0, r3, r1, r2, r4); KL(r1, r4, r3, r0, r2, 8);
  496. SI7(r1, r4, r3, r0, r2); KL(r4, r0, r1, r2, r3, 7);
  497. SI6(r4, r0, r1, r2, r3); KL(r1, r3, r2, r4, r0, 6);
  498. SI5(r1, r3, r2, r4, r0); KL(r3, r0, r1, r2, r4, 5);
  499. SI4(r3, r0, r1, r2, r4); KL(r3, r1, r4, r2, r0, 4);
  500. SI3(r3, r1, r4, r2, r0); KL(r4, r3, r0, r2, r1, 3);
  501. SI2(r4, r3, r0, r2, r1); KL(r3, r1, r2, r0, r4, 2);
  502. SI1(r3, r1, r2, r0, r4); KL(r4, r1, r2, r0, r3, 1);
  503. SI0(r4, r1, r2, r0, r3); K(r2, r3, r1, r4, 0);
  504. d[0] = cpu_to_le32(r2);
  505. d[1] = cpu_to_le32(r3);
  506. d[2] = cpu_to_le32(r1);
  507. d[3] = cpu_to_le32(r4);
  508. }
  509. EXPORT_SYMBOL_GPL(__serpent_decrypt);
  510. static void serpent_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  511. {
  512. struct serpent_ctx *ctx = crypto_tfm_ctx(tfm);
  513. __serpent_decrypt(ctx, dst, src);
  514. }
  515. static int tnepres_setkey(struct crypto_tfm *tfm, const u8 *key,
  516. unsigned int keylen)
  517. {
  518. u8 rev_key[SERPENT_MAX_KEY_SIZE];
  519. int i;
  520. for (i = 0; i < keylen; ++i)
  521. rev_key[keylen - i - 1] = key[i];
  522. return serpent_setkey(tfm, rev_key, keylen);
  523. }
  524. static void tnepres_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  525. {
  526. const u32 * const s = (const u32 * const)src;
  527. u32 * const d = (u32 * const)dst;
  528. u32 rs[4], rd[4];
  529. rs[0] = swab32(s[3]);
  530. rs[1] = swab32(s[2]);
  531. rs[2] = swab32(s[1]);
  532. rs[3] = swab32(s[0]);
  533. serpent_encrypt(tfm, (u8 *)rd, (u8 *)rs);
  534. d[0] = swab32(rd[3]);
  535. d[1] = swab32(rd[2]);
  536. d[2] = swab32(rd[1]);
  537. d[3] = swab32(rd[0]);
  538. }
  539. static void tnepres_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  540. {
  541. const u32 * const s = (const u32 * const)src;
  542. u32 * const d = (u32 * const)dst;
  543. u32 rs[4], rd[4];
  544. rs[0] = swab32(s[3]);
  545. rs[1] = swab32(s[2]);
  546. rs[2] = swab32(s[1]);
  547. rs[3] = swab32(s[0]);
  548. serpent_decrypt(tfm, (u8 *)rd, (u8 *)rs);
  549. d[0] = swab32(rd[3]);
  550. d[1] = swab32(rd[2]);
  551. d[2] = swab32(rd[1]);
  552. d[3] = swab32(rd[0]);
  553. }
  554. static struct crypto_alg srp_algs[2] = { {
  555. .cra_name = "serpent",
  556. .cra_driver_name = "serpent-generic",
  557. .cra_priority = 100,
  558. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  559. .cra_blocksize = SERPENT_BLOCK_SIZE,
  560. .cra_ctxsize = sizeof(struct serpent_ctx),
  561. .cra_alignmask = 3,
  562. .cra_module = THIS_MODULE,
  563. .cra_u = { .cipher = {
  564. .cia_min_keysize = SERPENT_MIN_KEY_SIZE,
  565. .cia_max_keysize = SERPENT_MAX_KEY_SIZE,
  566. .cia_setkey = serpent_setkey,
  567. .cia_encrypt = serpent_encrypt,
  568. .cia_decrypt = serpent_decrypt } }
  569. }, {
  570. .cra_name = "tnepres",
  571. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  572. .cra_blocksize = SERPENT_BLOCK_SIZE,
  573. .cra_ctxsize = sizeof(struct serpent_ctx),
  574. .cra_alignmask = 3,
  575. .cra_module = THIS_MODULE,
  576. .cra_u = { .cipher = {
  577. .cia_min_keysize = SERPENT_MIN_KEY_SIZE,
  578. .cia_max_keysize = SERPENT_MAX_KEY_SIZE,
  579. .cia_setkey = tnepres_setkey,
  580. .cia_encrypt = tnepres_encrypt,
  581. .cia_decrypt = tnepres_decrypt } }
  582. } };
  583. static int __init serpent_mod_init(void)
  584. {
  585. return crypto_register_algs(srp_algs, ARRAY_SIZE(srp_algs));
  586. }
  587. static void __exit serpent_mod_fini(void)
  588. {
  589. crypto_unregister_algs(srp_algs, ARRAY_SIZE(srp_algs));
  590. }
  591. module_init(serpent_mod_init);
  592. module_exit(serpent_mod_fini);
  593. MODULE_LICENSE("GPL");
  594. MODULE_DESCRIPTION("Serpent and tnepres (kerneli compatible serpent reversed) Cipher Algorithm");
  595. MODULE_AUTHOR("Dag Arne Osvik <osvik@ii.uib.no>");
  596. MODULE_ALIAS_CRYPTO("tnepres");
  597. MODULE_ALIAS_CRYPTO("serpent");
  598. MODULE_ALIAS_CRYPTO("serpent-generic");