serpent.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979
  1. /* serpent.c - Implementation of the Serpent encryption algorithm.
  2. * Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
  3. *
  4. * This file is part of Libgcrypt.
  5. *
  6. * Libgcrypt is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser general Public License as
  8. * published by the Free Software Foundation; either version 2.1 of
  9. * the License, or (at your option) any later version.
  10. *
  11. * Libgcrypt is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  19. * 02111-1307, USA.
  20. */
  21. #include <config.h>
  22. #include <string.h>
  23. #include <stdio.h>
  24. #include "types.h"
  25. #include "g10lib.h"
  26. #include "cipher.h"
  27. #include "bithelp.h"
  28. /* Number of rounds per Serpent encrypt/decrypt operation. */
  29. #define ROUNDS 32
  30. /* Magic number, used during generating of the subkeys. */
  31. #define PHI 0x9E3779B9
  32. /* Serpent works on 128 bit blocks. */
  33. typedef u32 serpent_block_t[4];
  34. /* Serpent key, provided by the user. If the original key is shorter
  35. than 256 bits, it is padded. */
  36. typedef u32 serpent_key_t[8];
  37. /* The key schedule consists of 33 128 bit subkeys. */
  38. typedef u32 serpent_subkeys_t[ROUNDS + 1][4];
  39. /* A Serpent context. */
  40. typedef struct serpent_context
  41. {
  42. serpent_subkeys_t keys; /* Generated subkeys. */
  43. } serpent_context_t;
  44. /* A prototype. */
  45. static const char *serpent_test (void);
  46. #define byte_swap_32(x) \
  47. (0 \
  48. | (((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) \
  49. | (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24))
  50. /* These are the S-Boxes of Serpent. They are copied from Serpents
  51. reference implementation (the optimized one, contained in
  52. `floppy2') and are therefore:
  53. Copyright (C) 1998 Ross Anderson, Eli Biham, Lars Knudsen.
  54. To quote the Serpent homepage
  55. (http://www.cl.cam.ac.uk/~rja14/serpent.html):
  56. "Serpent is now completely in the public domain, and we impose no
  57. restrictions on its use. This was announced on the 21st August at
  58. the First AES Candidate Conference. The optimised implementations
  59. in the submission package are now under the GNU PUBLIC LICENSE
  60. (GPL), although some comments in the code still say otherwise. You
  61. are welcome to use Serpent for any application." */
  62. #define SBOX0(a, b, c, d, w, x, y, z) \
  63. { \
  64. u32 t02, t03, t05, t06, t07, t08, t09; \
  65. u32 t11, t12, t13, t14, t15, t17, t01; \
  66. t01 = b ^ c ; \
  67. t02 = a | d ; \
  68. t03 = a ^ b ; \
  69. z = t02 ^ t01; \
  70. t05 = c | z ; \
  71. t06 = a ^ d ; \
  72. t07 = b | c ; \
  73. t08 = d & t05; \
  74. t09 = t03 & t07; \
  75. y = t09 ^ t08; \
  76. t11 = t09 & y ; \
  77. t12 = c ^ d ; \
  78. t13 = t07 ^ t11; \
  79. t14 = b & t06; \
  80. t15 = t06 ^ t13; \
  81. w = ~ t15; \
  82. t17 = w ^ t14; \
  83. x = t12 ^ t17; \
  84. }
  85. #define SBOX0_INVERSE(a, b, c, d, w, x, y, z) \
  86. { \
  87. u32 t02, t03, t04, t05, t06, t08, t09, t10; \
  88. u32 t12, t13, t14, t15, t17, t18, t01; \
  89. t01 = c ^ d ; \
  90. t02 = a | b ; \
  91. t03 = b | c ; \
  92. t04 = c & t01; \
  93. t05 = t02 ^ t01; \
  94. t06 = a | t04; \
  95. y = ~ t05; \
  96. t08 = b ^ d ; \
  97. t09 = t03 & t08; \
  98. t10 = d | y ; \
  99. x = t09 ^ t06; \
  100. t12 = a | t05; \
  101. t13 = x ^ t12; \
  102. t14 = t03 ^ t10; \
  103. t15 = a ^ c ; \
  104. z = t14 ^ t13; \
  105. t17 = t05 & t13; \
  106. t18 = t14 | t17; \
  107. w = t15 ^ t18; \
  108. }
  109. #define SBOX1(a, b, c, d, w, x, y, z) \
  110. { \
  111. u32 t02, t03, t04, t05, t06, t07, t08; \
  112. u32 t10, t11, t12, t13, t16, t17, t01; \
  113. t01 = a | d ; \
  114. t02 = c ^ d ; \
  115. t03 = ~ b ; \
  116. t04 = a ^ c ; \
  117. t05 = a | t03; \
  118. t06 = d & t04; \
  119. t07 = t01 & t02; \
  120. t08 = b | t06; \
  121. y = t02 ^ t05; \
  122. t10 = t07 ^ t08; \
  123. t11 = t01 ^ t10; \
  124. t12 = y ^ t11; \
  125. t13 = b & d ; \
  126. z = ~ t10; \
  127. x = t13 ^ t12; \
  128. t16 = t10 | x ; \
  129. t17 = t05 & t16; \
  130. w = c ^ t17; \
  131. }
  132. #define SBOX1_INVERSE(a, b, c, d, w, x, y, z) \
  133. { \
  134. u32 t02, t03, t04, t05, t06, t07, t08; \
  135. u32 t09, t10, t11, t14, t15, t17, t01; \
  136. t01 = a ^ b ; \
  137. t02 = b | d ; \
  138. t03 = a & c ; \
  139. t04 = c ^ t02; \
  140. t05 = a | t04; \
  141. t06 = t01 & t05; \
  142. t07 = d | t03; \
  143. t08 = b ^ t06; \
  144. t09 = t07 ^ t06; \
  145. t10 = t04 | t03; \
  146. t11 = d & t08; \
  147. y = ~ t09; \
  148. x = t10 ^ t11; \
  149. t14 = a | y ; \
  150. t15 = t06 ^ x ; \
  151. z = t01 ^ t04; \
  152. t17 = c ^ t15; \
  153. w = t14 ^ t17; \
  154. }
  155. #define SBOX2(a, b, c, d, w, x, y, z) \
  156. { \
  157. u32 t02, t03, t05, t06, t07, t08; \
  158. u32 t09, t10, t12, t13, t14, t01; \
  159. t01 = a | c ; \
  160. t02 = a ^ b ; \
  161. t03 = d ^ t01; \
  162. w = t02 ^ t03; \
  163. t05 = c ^ w ; \
  164. t06 = b ^ t05; \
  165. t07 = b | t05; \
  166. t08 = t01 & t06; \
  167. t09 = t03 ^ t07; \
  168. t10 = t02 | t09; \
  169. x = t10 ^ t08; \
  170. t12 = a | d ; \
  171. t13 = t09 ^ x ; \
  172. t14 = b ^ t13; \
  173. z = ~ t09; \
  174. y = t12 ^ t14; \
  175. }
  176. #define SBOX2_INVERSE(a, b, c, d, w, x, y, z) \
  177. { \
  178. u32 t02, t03, t04, t06, t07, t08, t09; \
  179. u32 t10, t11, t12, t15, t16, t17, t01; \
  180. t01 = a ^ d ; \
  181. t02 = c ^ d ; \
  182. t03 = a & c ; \
  183. t04 = b | t02; \
  184. w = t01 ^ t04; \
  185. t06 = a | c ; \
  186. t07 = d | w ; \
  187. t08 = ~ d ; \
  188. t09 = b & t06; \
  189. t10 = t08 | t03; \
  190. t11 = b & t07; \
  191. t12 = t06 & t02; \
  192. z = t09 ^ t10; \
  193. x = t12 ^ t11; \
  194. t15 = c & z ; \
  195. t16 = w ^ x ; \
  196. t17 = t10 ^ t15; \
  197. y = t16 ^ t17; \
  198. }
  199. #define SBOX3(a, b, c, d, w, x, y, z) \
  200. { \
  201. u32 t02, t03, t04, t05, t06, t07, t08; \
  202. u32 t09, t10, t11, t13, t14, t15, t01; \
  203. t01 = a ^ c ; \
  204. t02 = a | d ; \
  205. t03 = a & d ; \
  206. t04 = t01 & t02; \
  207. t05 = b | t03; \
  208. t06 = a & b ; \
  209. t07 = d ^ t04; \
  210. t08 = c | t06; \
  211. t09 = b ^ t07; \
  212. t10 = d & t05; \
  213. t11 = t02 ^ t10; \
  214. z = t08 ^ t09; \
  215. t13 = d | z ; \
  216. t14 = a | t07; \
  217. t15 = b & t13; \
  218. y = t08 ^ t11; \
  219. w = t14 ^ t15; \
  220. x = t05 ^ t04; \
  221. }
  222. #define SBOX3_INVERSE(a, b, c, d, w, x, y, z) \
  223. { \
  224. u32 t02, t03, t04, t05, t06, t07, t09; \
  225. u32 t11, t12, t13, t14, t16, t01; \
  226. t01 = c | d ; \
  227. t02 = a | d ; \
  228. t03 = c ^ t02; \
  229. t04 = b ^ t02; \
  230. t05 = a ^ d ; \
  231. t06 = t04 & t03; \
  232. t07 = b & t01; \
  233. y = t05 ^ t06; \
  234. t09 = a ^ t03; \
  235. w = t07 ^ t03; \
  236. t11 = w | t05; \
  237. t12 = t09 & t11; \
  238. t13 = a & y ; \
  239. t14 = t01 ^ t05; \
  240. x = b ^ t12; \
  241. t16 = b | t13; \
  242. z = t14 ^ t16; \
  243. }
  244. #define SBOX4(a, b, c, d, w, x, y, z) \
  245. { \
  246. u32 t02, t03, t04, t05, t06, t08, t09; \
  247. u32 t10, t11, t12, t13, t14, t15, t16, t01; \
  248. t01 = a | b ; \
  249. t02 = b | c ; \
  250. t03 = a ^ t02; \
  251. t04 = b ^ d ; \
  252. t05 = d | t03; \
  253. t06 = d & t01; \
  254. z = t03 ^ t06; \
  255. t08 = z & t04; \
  256. t09 = t04 & t05; \
  257. t10 = c ^ t06; \
  258. t11 = b & c ; \
  259. t12 = t04 ^ t08; \
  260. t13 = t11 | t03; \
  261. t14 = t10 ^ t09; \
  262. t15 = a & t05; \
  263. t16 = t11 | t12; \
  264. y = t13 ^ t08; \
  265. x = t15 ^ t16; \
  266. w = ~ t14; \
  267. }
  268. #define SBOX4_INVERSE(a, b, c, d, w, x, y, z) \
  269. { \
  270. u32 t02, t03, t04, t05, t06, t07, t09; \
  271. u32 t10, t11, t12, t13, t15, t01; \
  272. t01 = b | d ; \
  273. t02 = c | d ; \
  274. t03 = a & t01; \
  275. t04 = b ^ t02; \
  276. t05 = c ^ d ; \
  277. t06 = ~ t03; \
  278. t07 = a & t04; \
  279. x = t05 ^ t07; \
  280. t09 = x | t06; \
  281. t10 = a ^ t07; \
  282. t11 = t01 ^ t09; \
  283. t12 = d ^ t04; \
  284. t13 = c | t10; \
  285. z = t03 ^ t12; \
  286. t15 = a ^ t04; \
  287. y = t11 ^ t13; \
  288. w = t15 ^ t09; \
  289. }
  290. #define SBOX5(a, b, c, d, w, x, y, z) \
  291. { \
  292. u32 t02, t03, t04, t05, t07, t08, t09; \
  293. u32 t10, t11, t12, t13, t14, t01; \
  294. t01 = b ^ d ; \
  295. t02 = b | d ; \
  296. t03 = a & t01; \
  297. t04 = c ^ t02; \
  298. t05 = t03 ^ t04; \
  299. w = ~ t05; \
  300. t07 = a ^ t01; \
  301. t08 = d | w ; \
  302. t09 = b | t05; \
  303. t10 = d ^ t08; \
  304. t11 = b | t07; \
  305. t12 = t03 | w ; \
  306. t13 = t07 | t10; \
  307. t14 = t01 ^ t11; \
  308. y = t09 ^ t13; \
  309. x = t07 ^ t08; \
  310. z = t12 ^ t14; \
  311. }
  312. #define SBOX5_INVERSE(a, b, c, d, w, x, y, z) \
  313. { \
  314. u32 t02, t03, t04, t05, t07, t08, t09; \
  315. u32 t10, t12, t13, t15, t16, t01; \
  316. t01 = a & d ; \
  317. t02 = c ^ t01; \
  318. t03 = a ^ d ; \
  319. t04 = b & t02; \
  320. t05 = a & c ; \
  321. w = t03 ^ t04; \
  322. t07 = a & w ; \
  323. t08 = t01 ^ w ; \
  324. t09 = b | t05; \
  325. t10 = ~ b ; \
  326. x = t08 ^ t09; \
  327. t12 = t10 | t07; \
  328. t13 = w | x ; \
  329. z = t02 ^ t12; \
  330. t15 = t02 ^ t13; \
  331. t16 = b ^ d ; \
  332. y = t16 ^ t15; \
  333. }
  334. #define SBOX6(a, b, c, d, w, x, y, z) \
  335. { \
  336. u32 t02, t03, t04, t05, t07, t08, t09, t10; \
  337. u32 t11, t12, t13, t15, t17, t18, t01; \
  338. t01 = a & d ; \
  339. t02 = b ^ c ; \
  340. t03 = a ^ d ; \
  341. t04 = t01 ^ t02; \
  342. t05 = b | c ; \
  343. x = ~ t04; \
  344. t07 = t03 & t05; \
  345. t08 = b & x ; \
  346. t09 = a | c ; \
  347. t10 = t07 ^ t08; \
  348. t11 = b | d ; \
  349. t12 = c ^ t11; \
  350. t13 = t09 ^ t10; \
  351. y = ~ t13; \
  352. t15 = x & t03; \
  353. z = t12 ^ t07; \
  354. t17 = a ^ b ; \
  355. t18 = y ^ t15; \
  356. w = t17 ^ t18; \
  357. }
  358. #define SBOX6_INVERSE(a, b, c, d, w, x, y, z) \
  359. { \
  360. u32 t02, t03, t04, t05, t06, t07, t08, t09; \
  361. u32 t12, t13, t14, t15, t16, t17, t01; \
  362. t01 = a ^ c ; \
  363. t02 = ~ c ; \
  364. t03 = b & t01; \
  365. t04 = b | t02; \
  366. t05 = d | t03; \
  367. t06 = b ^ d ; \
  368. t07 = a & t04; \
  369. t08 = a | t02; \
  370. t09 = t07 ^ t05; \
  371. x = t06 ^ t08; \
  372. w = ~ t09; \
  373. t12 = b & w ; \
  374. t13 = t01 & t05; \
  375. t14 = t01 ^ t12; \
  376. t15 = t07 ^ t13; \
  377. t16 = d | t02; \
  378. t17 = a ^ x ; \
  379. z = t17 ^ t15; \
  380. y = t16 ^ t14; \
  381. }
  382. #define SBOX7(a, b, c, d, w, x, y, z) \
  383. { \
  384. u32 t02, t03, t04, t05, t06, t08, t09, t10; \
  385. u32 t11, t13, t14, t15, t16, t17, t01; \
  386. t01 = a & c ; \
  387. t02 = ~ d ; \
  388. t03 = a & t02; \
  389. t04 = b | t01; \
  390. t05 = a & b ; \
  391. t06 = c ^ t04; \
  392. z = t03 ^ t06; \
  393. t08 = c | z ; \
  394. t09 = d | t05; \
  395. t10 = a ^ t08; \
  396. t11 = t04 & z ; \
  397. x = t09 ^ t10; \
  398. t13 = b ^ x ; \
  399. t14 = t01 ^ x ; \
  400. t15 = c ^ t05; \
  401. t16 = t11 | t13; \
  402. t17 = t02 | t14; \
  403. w = t15 ^ t17; \
  404. y = a ^ t16; \
  405. }
  406. #define SBOX7_INVERSE(a, b, c, d, w, x, y, z) \
  407. { \
  408. u32 t02, t03, t04, t06, t07, t08, t09; \
  409. u32 t10, t11, t13, t14, t15, t16, t01; \
  410. t01 = a & b ; \
  411. t02 = a | b ; \
  412. t03 = c | t01; \
  413. t04 = d & t02; \
  414. z = t03 ^ t04; \
  415. t06 = b ^ t04; \
  416. t07 = d ^ z ; \
  417. t08 = ~ t07; \
  418. t09 = t06 | t08; \
  419. t10 = b ^ d ; \
  420. t11 = a | d ; \
  421. x = a ^ t09; \
  422. t13 = c ^ t06; \
  423. t14 = c & t11; \
  424. t15 = d | x ; \
  425. t16 = t01 | t10; \
  426. w = t13 ^ t15; \
  427. y = t14 ^ t16; \
  428. }
  429. /* XOR BLOCK1 into BLOCK0. */
  430. #define BLOCK_XOR(block0, block1) \
  431. { \
  432. block0[0] ^= block1[0]; \
  433. block0[1] ^= block1[1]; \
  434. block0[2] ^= block1[2]; \
  435. block0[3] ^= block1[3]; \
  436. }
  437. /* Copy BLOCK_SRC to BLOCK_DST. */
  438. #define BLOCK_COPY(block_dst, block_src) \
  439. { \
  440. block_dst[0] = block_src[0]; \
  441. block_dst[1] = block_src[1]; \
  442. block_dst[2] = block_src[2]; \
  443. block_dst[3] = block_src[3]; \
  444. }
  445. /* Apply SBOX number WHICH to to the block found in ARRAY0 at index
  446. INDEX, writing the output to the block found in ARRAY1 at index
  447. INDEX. */
  448. #define SBOX(which, array0, array1, index) \
  449. SBOX##which (array0[index + 0], array0[index + 1], \
  450. array0[index + 2], array0[index + 3], \
  451. array1[index + 0], array1[index + 1], \
  452. array1[index + 2], array1[index + 3]);
  453. /* Apply inverse SBOX number WHICH to to the block found in ARRAY0 at
  454. index INDEX, writing the output to the block found in ARRAY1 at
  455. index INDEX. */
  456. #define SBOX_INVERSE(which, array0, array1, index) \
  457. SBOX##which##_INVERSE (array0[index + 0], array0[index + 1], \
  458. array0[index + 2], array0[index + 3], \
  459. array1[index + 0], array1[index + 1], \
  460. array1[index + 2], array1[index + 3]);
  461. /* Apply the linear transformation to BLOCK. */
  462. #define LINEAR_TRANSFORMATION(block) \
  463. { \
  464. block[0] = rol (block[0], 13); \
  465. block[2] = rol (block[2], 3); \
  466. block[1] = block[1] ^ block[0] ^ block[2]; \
  467. block[3] = block[3] ^ block[2] ^ (block[0] << 3); \
  468. block[1] = rol (block[1], 1); \
  469. block[3] = rol (block[3], 7); \
  470. block[0] = block[0] ^ block[1] ^ block[3]; \
  471. block[2] = block[2] ^ block[3] ^ (block[1] << 7); \
  472. block[0] = rol (block[0], 5); \
  473. block[2] = rol (block[2], 22); \
  474. }
  475. /* Apply the inverse linear transformation to BLOCK. */
  476. #define LINEAR_TRANSFORMATION_INVERSE(block) \
  477. { \
  478. block[2] = ror (block[2], 22); \
  479. block[0] = ror (block[0] , 5); \
  480. block[2] = block[2] ^ block[3] ^ (block[1] << 7); \
  481. block[0] = block[0] ^ block[1] ^ block[3]; \
  482. block[3] = ror (block[3], 7); \
  483. block[1] = ror (block[1], 1); \
  484. block[3] = block[3] ^ block[2] ^ (block[0] << 3); \
  485. block[1] = block[1] ^ block[0] ^ block[2]; \
  486. block[2] = ror (block[2], 3); \
  487. block[0] = ror (block[0], 13); \
  488. }
  489. /* Apply a Serpent round to BLOCK, using the SBOX number WHICH and the
  490. subkeys contained in SUBKEYS. Use BLOCK_TMP as temporary storage.
  491. This macro increments `round'. */
  492. #define ROUND(which, subkeys, block, block_tmp) \
  493. { \
  494. BLOCK_XOR (block, subkeys[round]); \
  495. round++; \
  496. SBOX (which, block, block_tmp, 0); \
  497. LINEAR_TRANSFORMATION (block_tmp); \
  498. BLOCK_COPY (block, block_tmp); \
  499. }
  500. /* Apply the last Serpent round to BLOCK, using the SBOX number WHICH
  501. and the subkeys contained in SUBKEYS. Use BLOCK_TMP as temporary
  502. storage. The result will be stored in BLOCK_TMP. This macro
  503. increments `round'. */
  504. #define ROUND_LAST(which, subkeys, block, block_tmp) \
  505. { \
  506. BLOCK_XOR (block, subkeys[round]); \
  507. round++; \
  508. SBOX (which, block, block_tmp, 0); \
  509. BLOCK_XOR (block_tmp, subkeys[round]); \
  510. round++; \
  511. }
  512. /* Apply an inverse Serpent round to BLOCK, using the SBOX number
  513. WHICH and the subkeys contained in SUBKEYS. Use BLOCK_TMP as
  514. temporary storage. This macro increments `round'. */
  515. #define ROUND_INVERSE(which, subkey, block, block_tmp) \
  516. { \
  517. LINEAR_TRANSFORMATION_INVERSE (block); \
  518. SBOX_INVERSE (which, block, block_tmp, 0); \
  519. BLOCK_XOR (block_tmp, subkey[round]); \
  520. round--; \
  521. BLOCK_COPY (block, block_tmp); \
  522. }
  523. /* Apply the first Serpent round to BLOCK, using the SBOX number WHICH
  524. and the subkeys contained in SUBKEYS. Use BLOCK_TMP as temporary
  525. storage. The result will be stored in BLOCK_TMP. This macro
  526. increments `round'. */
  527. #define ROUND_FIRST_INVERSE(which, subkeys, block, block_tmp) \
  528. { \
  529. BLOCK_XOR (block, subkeys[round]); \
  530. round--; \
  531. SBOX_INVERSE (which, block, block_tmp, 0); \
  532. BLOCK_XOR (block_tmp, subkeys[round]); \
  533. round--; \
  534. }
  535. /* Convert the user provided key KEY of KEY_LENGTH bytes into the
  536. internally used format. */
  537. static void
  538. serpent_key_prepare (const byte *key, unsigned int key_length,
  539. serpent_key_t key_prepared)
  540. {
  541. int i;
  542. /* Copy key. */
  543. for (i = 0; i < key_length / 4; i++)
  544. {
  545. #ifdef WORDS_BIGENDIAN
  546. key_prepared[i] = byte_swap_32 (((u32 *) key)[i]);
  547. #else
  548. key_prepared[i] = ((u32 *) key)[i];
  549. #endif
  550. }
  551. if (i < 8)
  552. {
  553. /* Key must be padded according to the Serpent
  554. specification. */
  555. key_prepared[i] = 0x00000001;
  556. for (i++; i < 8; i++)
  557. key_prepared[i] = 0;
  558. }
  559. }
  560. /* Derive the 33 subkeys from KEY and store them in SUBKEYS. */
  561. static void
  562. serpent_subkeys_generate (serpent_key_t key, serpent_subkeys_t subkeys)
  563. {
  564. u32 w_real[140]; /* The `prekey'. */
  565. u32 k[132];
  566. u32 *w = &w_real[8];
  567. int i, j;
  568. /* Initialize with key values. */
  569. for (i = 0; i < 8; i++)
  570. w[i - 8] = key[i];
  571. /* Expand to intermediate key using the affine recurrence. */
  572. for (i = 0; i < 132; i++)
  573. w[i] = rol (w[i - 8] ^ w[i - 5] ^ w[i - 3] ^ w[i - 1] ^ PHI ^ i, 11);
  574. /* Calculate subkeys via S-Boxes, in bitslice mode. */
  575. SBOX (3, w, k, 0);
  576. SBOX (2, w, k, 4);
  577. SBOX (1, w, k, 8);
  578. SBOX (0, w, k, 12);
  579. SBOX (7, w, k, 16);
  580. SBOX (6, w, k, 20);
  581. SBOX (5, w, k, 24);
  582. SBOX (4, w, k, 28);
  583. SBOX (3, w, k, 32);
  584. SBOX (2, w, k, 36);
  585. SBOX (1, w, k, 40);
  586. SBOX (0, w, k, 44);
  587. SBOX (7, w, k, 48);
  588. SBOX (6, w, k, 52);
  589. SBOX (5, w, k, 56);
  590. SBOX (4, w, k, 60);
  591. SBOX (3, w, k, 64);
  592. SBOX (2, w, k, 68);
  593. SBOX (1, w, k, 72);
  594. SBOX (0, w, k, 76);
  595. SBOX (7, w, k, 80);
  596. SBOX (6, w, k, 84);
  597. SBOX (5, w, k, 88);
  598. SBOX (4, w, k, 92);
  599. SBOX (3, w, k, 96);
  600. SBOX (2, w, k, 100);
  601. SBOX (1, w, k, 104);
  602. SBOX (0, w, k, 108);
  603. SBOX (7, w, k, 112);
  604. SBOX (6, w, k, 116);
  605. SBOX (5, w, k, 120);
  606. SBOX (4, w, k, 124);
  607. SBOX (3, w, k, 128);
  608. /* Renumber subkeys. */
  609. for (i = 0; i < ROUNDS + 1; i++)
  610. for (j = 0; j < 4; j++)
  611. subkeys[i][j] = k[4 * i + j];
  612. }
  613. /* Initialize CONTEXT with the key KEY of KEY_LENGTH bits. */
  614. static void
  615. serpent_setkey_internal (serpent_context_t *context,
  616. const byte *key, unsigned int key_length)
  617. {
  618. serpent_key_t key_prepared;
  619. serpent_key_prepare (key, key_length, key_prepared);
  620. serpent_subkeys_generate (key_prepared, context->keys);
  621. _gcry_burn_stack (272 * sizeof (u32));
  622. }
  623. /* Initialize CTX with the key KEY of KEY_LENGTH bytes. */
  624. static gcry_err_code_t
  625. serpent_setkey (void *ctx,
  626. const byte *key, unsigned int key_length)
  627. {
  628. serpent_context_t *context = ctx;
  629. static const char *serpent_test_ret;
  630. static int serpent_init_done;
  631. gcry_err_code_t ret = GPG_ERR_NO_ERROR;
  632. if (! serpent_init_done)
  633. {
  634. /* Execute a self-test the first time, Serpent is used. */
  635. serpent_test_ret = serpent_test ();
  636. if (serpent_test_ret)
  637. log_error ("Serpent test failure: %s\n", serpent_test_ret);
  638. serpent_init_done = 1;
  639. }
  640. if (serpent_test_ret)
  641. ret = GPG_ERR_SELFTEST_FAILED;
  642. else
  643. {
  644. serpent_setkey_internal (context, key, key_length);
  645. _gcry_burn_stack (sizeof (serpent_key_t));
  646. }
  647. return ret;
  648. }
  649. static void
  650. serpent_encrypt_internal (serpent_context_t *context,
  651. const serpent_block_t input, serpent_block_t output)
  652. {
  653. serpent_block_t b, b_next;
  654. int round = 0;
  655. #ifdef WORDS_BIGENDIAN
  656. b[0] = byte_swap_32 (input[0]);
  657. b[1] = byte_swap_32 (input[1]);
  658. b[2] = byte_swap_32 (input[2]);
  659. b[3] = byte_swap_32 (input[3]);
  660. #else
  661. b[0] = input[0];
  662. b[1] = input[1];
  663. b[2] = input[2];
  664. b[3] = input[3];
  665. #endif
  666. ROUND (0, context->keys, b, b_next);
  667. ROUND (1, context->keys, b, b_next);
  668. ROUND (2, context->keys, b, b_next);
  669. ROUND (3, context->keys, b, b_next);
  670. ROUND (4, context->keys, b, b_next);
  671. ROUND (5, context->keys, b, b_next);
  672. ROUND (6, context->keys, b, b_next);
  673. ROUND (7, context->keys, b, b_next);
  674. ROUND (0, context->keys, b, b_next);
  675. ROUND (1, context->keys, b, b_next);
  676. ROUND (2, context->keys, b, b_next);
  677. ROUND (3, context->keys, b, b_next);
  678. ROUND (4, context->keys, b, b_next);
  679. ROUND (5, context->keys, b, b_next);
  680. ROUND (6, context->keys, b, b_next);
  681. ROUND (7, context->keys, b, b_next);
  682. ROUND (0, context->keys, b, b_next);
  683. ROUND (1, context->keys, b, b_next);
  684. ROUND (2, context->keys, b, b_next);
  685. ROUND (3, context->keys, b, b_next);
  686. ROUND (4, context->keys, b, b_next);
  687. ROUND (5, context->keys, b, b_next);
  688. ROUND (6, context->keys, b, b_next);
  689. ROUND (7, context->keys, b, b_next);
  690. ROUND (0, context->keys, b, b_next);
  691. ROUND (1, context->keys, b, b_next);
  692. ROUND (2, context->keys, b, b_next);
  693. ROUND (3, context->keys, b, b_next);
  694. ROUND (4, context->keys, b, b_next);
  695. ROUND (5, context->keys, b, b_next);
  696. ROUND (6, context->keys, b, b_next);
  697. ROUND_LAST (7, context->keys, b, b_next);
  698. #ifdef WORDS_BIGENDIAN
  699. output[0] = byte_swap_32 (b_next[0]);
  700. output[1] = byte_swap_32 (b_next[1]);
  701. output[2] = byte_swap_32 (b_next[2]);
  702. output[3] = byte_swap_32 (b_next[3]);
  703. #else
  704. output[0] = b_next[0];
  705. output[1] = b_next[1];
  706. output[2] = b_next[2];
  707. output[3] = b_next[3];
  708. #endif
  709. }
  710. static void
  711. serpent_decrypt_internal (serpent_context_t *context,
  712. const serpent_block_t input, serpent_block_t output)
  713. {
  714. serpent_block_t b, b_next;
  715. int round = ROUNDS;
  716. #ifdef WORDS_BIGENDIAN
  717. b_next[0] = byte_swap_32 (input[0]);
  718. b_next[1] = byte_swap_32 (input[1]);
  719. b_next[2] = byte_swap_32 (input[2]);
  720. b_next[3] = byte_swap_32 (input[3]);
  721. #else
  722. b_next[0] = input[0];
  723. b_next[1] = input[1];
  724. b_next[2] = input[2];
  725. b_next[3] = input[3];
  726. #endif
  727. ROUND_FIRST_INVERSE (7, context->keys, b_next, b);
  728. ROUND_INVERSE (6, context->keys, b, b_next);
  729. ROUND_INVERSE (5, context->keys, b, b_next);
  730. ROUND_INVERSE (4, context->keys, b, b_next);
  731. ROUND_INVERSE (3, context->keys, b, b_next);
  732. ROUND_INVERSE (2, context->keys, b, b_next);
  733. ROUND_INVERSE (1, context->keys, b, b_next);
  734. ROUND_INVERSE (0, context->keys, b, b_next);
  735. ROUND_INVERSE (7, context->keys, b, b_next);
  736. ROUND_INVERSE (6, context->keys, b, b_next);
  737. ROUND_INVERSE (5, context->keys, b, b_next);
  738. ROUND_INVERSE (4, context->keys, b, b_next);
  739. ROUND_INVERSE (3, context->keys, b, b_next);
  740. ROUND_INVERSE (2, context->keys, b, b_next);
  741. ROUND_INVERSE (1, context->keys, b, b_next);
  742. ROUND_INVERSE (0, context->keys, b, b_next);
  743. ROUND_INVERSE (7, context->keys, b, b_next);
  744. ROUND_INVERSE (6, context->keys, b, b_next);
  745. ROUND_INVERSE (5, context->keys, b, b_next);
  746. ROUND_INVERSE (4, context->keys, b, b_next);
  747. ROUND_INVERSE (3, context->keys, b, b_next);
  748. ROUND_INVERSE (2, context->keys, b, b_next);
  749. ROUND_INVERSE (1, context->keys, b, b_next);
  750. ROUND_INVERSE (0, context->keys, b, b_next);
  751. ROUND_INVERSE (7, context->keys, b, b_next);
  752. ROUND_INVERSE (6, context->keys, b, b_next);
  753. ROUND_INVERSE (5, context->keys, b, b_next);
  754. ROUND_INVERSE (4, context->keys, b, b_next);
  755. ROUND_INVERSE (3, context->keys, b, b_next);
  756. ROUND_INVERSE (2, context->keys, b, b_next);
  757. ROUND_INVERSE (1, context->keys, b, b_next);
  758. ROUND_INVERSE (0, context->keys, b, b_next);
  759. #ifdef WORDS_BIGENDIAN
  760. output[0] = byte_swap_32 (b_next[0]);
  761. output[1] = byte_swap_32 (b_next[1]);
  762. output[2] = byte_swap_32 (b_next[2]);
  763. output[3] = byte_swap_32 (b_next[3]);
  764. #else
  765. output[0] = b_next[0];
  766. output[1] = b_next[1];
  767. output[2] = b_next[2];
  768. output[3] = b_next[3];
  769. #endif
  770. }
  771. static void
  772. serpent_encrypt (void *ctx, byte *buffer_out, const byte *buffer_in)
  773. {
  774. serpent_context_t *context = ctx;
  775. serpent_encrypt_internal (context,
  776. (const u32 *) buffer_in, (u32 *) buffer_out);
  777. _gcry_burn_stack (2 * sizeof (serpent_block_t));
  778. }
  779. static void
  780. serpent_decrypt (void *ctx, byte *buffer_out, const byte *buffer_in)
  781. {
  782. serpent_context_t *context = ctx;
  783. serpent_decrypt_internal (context,
  784. (const u32 *) buffer_in,
  785. (u32 *) buffer_out);
  786. _gcry_burn_stack (2 * sizeof (serpent_block_t));
  787. }
  788. /* Serpent test. */
  789. static const char *
  790. serpent_test (void)
  791. {
  792. serpent_context_t context;
  793. unsigned char scratch[16];
  794. unsigned int i;
  795. static struct test
  796. {
  797. int key_length;
  798. unsigned char key[32];
  799. unsigned char text_plain[16];
  800. unsigned char text_cipher[16];
  801. } test_data[] =
  802. {
  803. {
  804. 16,
  805. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  806. "\xD2\x9D\x57\x6F\xCE\xA3\xA3\xA7\xED\x90\x99\xF2\x92\x73\xD7\x8E",
  807. "\xB2\x28\x8B\x96\x8A\xE8\xB0\x86\x48\xD1\xCE\x96\x06\xFD\x99\x2D"
  808. },
  809. {
  810. 24,
  811. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  812. "\x00\x00\x00\x00\x00\x00\x00\x00",
  813. "\xD2\x9D\x57\x6F\xCE\xAB\xA3\xA7\xED\x98\x99\xF2\x92\x7B\xD7\x8E",
  814. "\x13\x0E\x35\x3E\x10\x37\xC2\x24\x05\xE8\xFA\xEF\xB2\xC3\xC3\xE9"
  815. },
  816. {
  817. 32,
  818. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  819. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  820. "\xD0\x95\x57\x6F\xCE\xA3\xE3\xA7\xED\x98\xD9\xF2\x90\x73\xD7\x8E",
  821. "\xB9\x0E\xE5\x86\x2D\xE6\x91\x68\xF2\xBD\xD5\x12\x5B\x45\x47\x2B"
  822. },
  823. {
  824. 32,
  825. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  826. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  827. "\x00\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00",
  828. "\x20\x61\xA4\x27\x82\xBD\x52\xEC\x69\x1E\xC3\x83\xB0\x3B\xA7\x7C"
  829. },
  830. {
  831. 0
  832. },
  833. };
  834. for (i = 0; test_data[i].key_length; i++)
  835. {
  836. serpent_setkey_internal (&context, test_data[i].key,
  837. test_data[i].key_length);
  838. serpent_encrypt_internal (&context,
  839. (const u32 *) test_data[i].text_plain,
  840. (u32 *) scratch);
  841. if (memcmp (scratch, test_data[i].text_cipher, sizeof (serpent_block_t)))
  842. switch (test_data[i].key_length)
  843. {
  844. case 16:
  845. return "Serpent-128 test encryption failed.";
  846. case 24:
  847. return "Serpent-192 test encryption failed.";
  848. case 32:
  849. return "Serpent-256 test encryption failed.";
  850. }
  851. serpent_decrypt_internal (&context,
  852. (const u32 *) test_data[i].text_cipher,
  853. (u32 *) scratch);
  854. if (memcmp (scratch, test_data[i].text_plain, sizeof (serpent_block_t)))
  855. switch (test_data[i].key_length)
  856. {
  857. case 16:
  858. return "Serpent-128 test decryption failed.";
  859. case 24:
  860. return "Serpent-192 test decryption failed.";
  861. case 32:
  862. return "Serpent-256 test decryption failed.";
  863. }
  864. }
  865. return NULL;
  866. }
  867. /* "SERPENT" is an alias for "SERPENT128". */
  868. static const char *cipher_spec_serpent128_aliases[] =
  869. {
  870. "SERPENT",
  871. NULL
  872. };
  873. gcry_cipher_spec_t _gcry_cipher_spec_serpent128 =
  874. {
  875. "SERPENT128", cipher_spec_serpent128_aliases, NULL, 16, 128,
  876. sizeof (serpent_context_t),
  877. serpent_setkey, serpent_encrypt, serpent_decrypt
  878. };
  879. gcry_cipher_spec_t _gcry_cipher_spec_serpent192 =
  880. {
  881. "SERPENT192", NULL, NULL, 16, 192,
  882. sizeof (serpent_context_t),
  883. serpent_setkey, serpent_encrypt, serpent_decrypt
  884. };
  885. gcry_cipher_spec_t _gcry_cipher_spec_serpent256 =
  886. {
  887. "SERPENT256", NULL, NULL, 16, 256,
  888. sizeof (serpent_context_t),
  889. serpent_setkey, serpent_encrypt, serpent_decrypt
  890. };