serpent_sse2_glue.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. /*
  2. * Glue Code for SSE2 assembler versions of Serpent Cipher
  3. *
  4. * Copyright (c) 2011 Jussi Kivilinna <jussi.kivilinna@mbnet.fi>
  5. *
  6. * Glue code based on aesni-intel_glue.c by:
  7. * Copyright (C) 2008, Intel Corp.
  8. * Author: Huang Ying <ying.huang@intel.com>
  9. *
  10. * CBC & ECB parts based on code (crypto/cbc.c,ecb.c) by:
  11. * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
  12. * CTR part based on code (crypto/ctr.c) by:
  13. * (C) Copyright IBM Corp. 2007 - Joy Latten <latten@us.ibm.com>
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; either version 2 of the License, or
  18. * (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  28. * USA
  29. *
  30. */
  31. #include <linux/module.h>
  32. #include <linux/hardirq.h>
  33. #include <linux/types.h>
  34. #include <linux/crypto.h>
  35. #include <linux/err.h>
  36. #include <crypto/ablk_helper.h>
  37. #include <crypto/algapi.h>
  38. #include <crypto/serpent.h>
  39. #include <crypto/cryptd.h>
  40. #include <crypto/b128ops.h>
  41. #include <crypto/ctr.h>
  42. #include <crypto/lrw.h>
  43. #include <crypto/xts.h>
  44. #include <asm/crypto/serpent-sse2.h>
  45. #include <asm/crypto/glue_helper.h>
  46. static void serpent_decrypt_cbc_xway(void *ctx, u128 *dst, const u128 *src)
  47. {
  48. u128 ivs[SERPENT_PARALLEL_BLOCKS - 1];
  49. unsigned int j;
  50. for (j = 0; j < SERPENT_PARALLEL_BLOCKS - 1; j++)
  51. ivs[j] = src[j];
  52. serpent_dec_blk_xway(ctx, (u8 *)dst, (u8 *)src);
  53. for (j = 0; j < SERPENT_PARALLEL_BLOCKS - 1; j++)
  54. u128_xor(dst + (j + 1), dst + (j + 1), ivs + j);
  55. }
  56. static void serpent_crypt_ctr(void *ctx, u128 *dst, const u128 *src, le128 *iv)
  57. {
  58. be128 ctrblk;
  59. le128_to_be128(&ctrblk, iv);
  60. le128_inc(iv);
  61. __serpent_encrypt(ctx, (u8 *)&ctrblk, (u8 *)&ctrblk);
  62. u128_xor(dst, src, (u128 *)&ctrblk);
  63. }
  64. static void serpent_crypt_ctr_xway(void *ctx, u128 *dst, const u128 *src,
  65. le128 *iv)
  66. {
  67. be128 ctrblks[SERPENT_PARALLEL_BLOCKS];
  68. unsigned int i;
  69. for (i = 0; i < SERPENT_PARALLEL_BLOCKS; i++) {
  70. if (dst != src)
  71. dst[i] = src[i];
  72. le128_to_be128(&ctrblks[i], iv);
  73. le128_inc(iv);
  74. }
  75. serpent_enc_blk_xway_xor(ctx, (u8 *)dst, (u8 *)ctrblks);
  76. }
  77. static const struct common_glue_ctx serpent_enc = {
  78. .num_funcs = 2,
  79. .fpu_blocks_limit = SERPENT_PARALLEL_BLOCKS,
  80. .funcs = { {
  81. .num_blocks = SERPENT_PARALLEL_BLOCKS,
  82. .fn_u = { .ecb = GLUE_FUNC_CAST(serpent_enc_blk_xway) }
  83. }, {
  84. .num_blocks = 1,
  85. .fn_u = { .ecb = GLUE_FUNC_CAST(__serpent_encrypt) }
  86. } }
  87. };
  88. static const struct common_glue_ctx serpent_ctr = {
  89. .num_funcs = 2,
  90. .fpu_blocks_limit = SERPENT_PARALLEL_BLOCKS,
  91. .funcs = { {
  92. .num_blocks = SERPENT_PARALLEL_BLOCKS,
  93. .fn_u = { .ctr = GLUE_CTR_FUNC_CAST(serpent_crypt_ctr_xway) }
  94. }, {
  95. .num_blocks = 1,
  96. .fn_u = { .ctr = GLUE_CTR_FUNC_CAST(serpent_crypt_ctr) }
  97. } }
  98. };
  99. static const struct common_glue_ctx serpent_dec = {
  100. .num_funcs = 2,
  101. .fpu_blocks_limit = SERPENT_PARALLEL_BLOCKS,
  102. .funcs = { {
  103. .num_blocks = SERPENT_PARALLEL_BLOCKS,
  104. .fn_u = { .ecb = GLUE_FUNC_CAST(serpent_dec_blk_xway) }
  105. }, {
  106. .num_blocks = 1,
  107. .fn_u = { .ecb = GLUE_FUNC_CAST(__serpent_decrypt) }
  108. } }
  109. };
  110. static const struct common_glue_ctx serpent_dec_cbc = {
  111. .num_funcs = 2,
  112. .fpu_blocks_limit = SERPENT_PARALLEL_BLOCKS,
  113. .funcs = { {
  114. .num_blocks = SERPENT_PARALLEL_BLOCKS,
  115. .fn_u = { .cbc = GLUE_CBC_FUNC_CAST(serpent_decrypt_cbc_xway) }
  116. }, {
  117. .num_blocks = 1,
  118. .fn_u = { .cbc = GLUE_CBC_FUNC_CAST(__serpent_decrypt) }
  119. } }
  120. };
  121. static int ecb_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  122. struct scatterlist *src, unsigned int nbytes)
  123. {
  124. return glue_ecb_crypt_128bit(&serpent_enc, desc, dst, src, nbytes);
  125. }
  126. static int ecb_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  127. struct scatterlist *src, unsigned int nbytes)
  128. {
  129. return glue_ecb_crypt_128bit(&serpent_dec, desc, dst, src, nbytes);
  130. }
  131. static int cbc_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  132. struct scatterlist *src, unsigned int nbytes)
  133. {
  134. return glue_cbc_encrypt_128bit(GLUE_FUNC_CAST(__serpent_encrypt), desc,
  135. dst, src, nbytes);
  136. }
  137. static int cbc_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  138. struct scatterlist *src, unsigned int nbytes)
  139. {
  140. return glue_cbc_decrypt_128bit(&serpent_dec_cbc, desc, dst, src,
  141. nbytes);
  142. }
  143. static int ctr_crypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  144. struct scatterlist *src, unsigned int nbytes)
  145. {
  146. return glue_ctr_crypt_128bit(&serpent_ctr, desc, dst, src, nbytes);
  147. }
  148. static inline bool serpent_fpu_begin(bool fpu_enabled, unsigned int nbytes)
  149. {
  150. return glue_fpu_begin(SERPENT_BLOCK_SIZE, SERPENT_PARALLEL_BLOCKS,
  151. NULL, fpu_enabled, nbytes);
  152. }
  153. static inline void serpent_fpu_end(bool fpu_enabled)
  154. {
  155. glue_fpu_end(fpu_enabled);
  156. }
  157. struct crypt_priv {
  158. struct serpent_ctx *ctx;
  159. bool fpu_enabled;
  160. };
  161. static void encrypt_callback(void *priv, u8 *srcdst, unsigned int nbytes)
  162. {
  163. const unsigned int bsize = SERPENT_BLOCK_SIZE;
  164. struct crypt_priv *ctx = priv;
  165. int i;
  166. ctx->fpu_enabled = serpent_fpu_begin(ctx->fpu_enabled, nbytes);
  167. if (nbytes == bsize * SERPENT_PARALLEL_BLOCKS) {
  168. serpent_enc_blk_xway(ctx->ctx, srcdst, srcdst);
  169. return;
  170. }
  171. for (i = 0; i < nbytes / bsize; i++, srcdst += bsize)
  172. __serpent_encrypt(ctx->ctx, srcdst, srcdst);
  173. }
  174. static void decrypt_callback(void *priv, u8 *srcdst, unsigned int nbytes)
  175. {
  176. const unsigned int bsize = SERPENT_BLOCK_SIZE;
  177. struct crypt_priv *ctx = priv;
  178. int i;
  179. ctx->fpu_enabled = serpent_fpu_begin(ctx->fpu_enabled, nbytes);
  180. if (nbytes == bsize * SERPENT_PARALLEL_BLOCKS) {
  181. serpent_dec_blk_xway(ctx->ctx, srcdst, srcdst);
  182. return;
  183. }
  184. for (i = 0; i < nbytes / bsize; i++, srcdst += bsize)
  185. __serpent_decrypt(ctx->ctx, srcdst, srcdst);
  186. }
  187. struct serpent_lrw_ctx {
  188. struct lrw_table_ctx lrw_table;
  189. struct serpent_ctx serpent_ctx;
  190. };
  191. static int lrw_serpent_setkey(struct crypto_tfm *tfm, const u8 *key,
  192. unsigned int keylen)
  193. {
  194. struct serpent_lrw_ctx *ctx = crypto_tfm_ctx(tfm);
  195. int err;
  196. err = __serpent_setkey(&ctx->serpent_ctx, key, keylen -
  197. SERPENT_BLOCK_SIZE);
  198. if (err)
  199. return err;
  200. return lrw_init_table(&ctx->lrw_table, key + keylen -
  201. SERPENT_BLOCK_SIZE);
  202. }
  203. static int lrw_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  204. struct scatterlist *src, unsigned int nbytes)
  205. {
  206. struct serpent_lrw_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  207. be128 buf[SERPENT_PARALLEL_BLOCKS];
  208. struct crypt_priv crypt_ctx = {
  209. .ctx = &ctx->serpent_ctx,
  210. .fpu_enabled = false,
  211. };
  212. struct lrw_crypt_req req = {
  213. .tbuf = buf,
  214. .tbuflen = sizeof(buf),
  215. .table_ctx = &ctx->lrw_table,
  216. .crypt_ctx = &crypt_ctx,
  217. .crypt_fn = encrypt_callback,
  218. };
  219. int ret;
  220. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  221. ret = lrw_crypt(desc, dst, src, nbytes, &req);
  222. serpent_fpu_end(crypt_ctx.fpu_enabled);
  223. return ret;
  224. }
  225. static int lrw_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  226. struct scatterlist *src, unsigned int nbytes)
  227. {
  228. struct serpent_lrw_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  229. be128 buf[SERPENT_PARALLEL_BLOCKS];
  230. struct crypt_priv crypt_ctx = {
  231. .ctx = &ctx->serpent_ctx,
  232. .fpu_enabled = false,
  233. };
  234. struct lrw_crypt_req req = {
  235. .tbuf = buf,
  236. .tbuflen = sizeof(buf),
  237. .table_ctx = &ctx->lrw_table,
  238. .crypt_ctx = &crypt_ctx,
  239. .crypt_fn = decrypt_callback,
  240. };
  241. int ret;
  242. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  243. ret = lrw_crypt(desc, dst, src, nbytes, &req);
  244. serpent_fpu_end(crypt_ctx.fpu_enabled);
  245. return ret;
  246. }
  247. static void lrw_exit_tfm(struct crypto_tfm *tfm)
  248. {
  249. struct serpent_lrw_ctx *ctx = crypto_tfm_ctx(tfm);
  250. lrw_free_table(&ctx->lrw_table);
  251. }
  252. struct serpent_xts_ctx {
  253. struct serpent_ctx tweak_ctx;
  254. struct serpent_ctx crypt_ctx;
  255. };
  256. static int xts_serpent_setkey(struct crypto_tfm *tfm, const u8 *key,
  257. unsigned int keylen)
  258. {
  259. struct serpent_xts_ctx *ctx = crypto_tfm_ctx(tfm);
  260. int err;
  261. err = xts_check_key(tfm, key, keylen);
  262. if (err)
  263. return err;
  264. /* first half of xts-key is for crypt */
  265. err = __serpent_setkey(&ctx->crypt_ctx, key, keylen / 2);
  266. if (err)
  267. return err;
  268. /* second half of xts-key is for tweak */
  269. return __serpent_setkey(&ctx->tweak_ctx, key + keylen / 2, keylen / 2);
  270. }
  271. static int xts_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  272. struct scatterlist *src, unsigned int nbytes)
  273. {
  274. struct serpent_xts_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  275. be128 buf[SERPENT_PARALLEL_BLOCKS];
  276. struct crypt_priv crypt_ctx = {
  277. .ctx = &ctx->crypt_ctx,
  278. .fpu_enabled = false,
  279. };
  280. struct xts_crypt_req req = {
  281. .tbuf = buf,
  282. .tbuflen = sizeof(buf),
  283. .tweak_ctx = &ctx->tweak_ctx,
  284. .tweak_fn = XTS_TWEAK_CAST(__serpent_encrypt),
  285. .crypt_ctx = &crypt_ctx,
  286. .crypt_fn = encrypt_callback,
  287. };
  288. int ret;
  289. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  290. ret = xts_crypt(desc, dst, src, nbytes, &req);
  291. serpent_fpu_end(crypt_ctx.fpu_enabled);
  292. return ret;
  293. }
  294. static int xts_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  295. struct scatterlist *src, unsigned int nbytes)
  296. {
  297. struct serpent_xts_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  298. be128 buf[SERPENT_PARALLEL_BLOCKS];
  299. struct crypt_priv crypt_ctx = {
  300. .ctx = &ctx->crypt_ctx,
  301. .fpu_enabled = false,
  302. };
  303. struct xts_crypt_req req = {
  304. .tbuf = buf,
  305. .tbuflen = sizeof(buf),
  306. .tweak_ctx = &ctx->tweak_ctx,
  307. .tweak_fn = XTS_TWEAK_CAST(__serpent_encrypt),
  308. .crypt_ctx = &crypt_ctx,
  309. .crypt_fn = decrypt_callback,
  310. };
  311. int ret;
  312. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  313. ret = xts_crypt(desc, dst, src, nbytes, &req);
  314. serpent_fpu_end(crypt_ctx.fpu_enabled);
  315. return ret;
  316. }
  317. static struct crypto_alg serpent_algs[10] = { {
  318. .cra_name = "__ecb-serpent-sse2",
  319. .cra_driver_name = "__driver-ecb-serpent-sse2",
  320. .cra_priority = 0,
  321. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
  322. CRYPTO_ALG_INTERNAL,
  323. .cra_blocksize = SERPENT_BLOCK_SIZE,
  324. .cra_ctxsize = sizeof(struct serpent_ctx),
  325. .cra_alignmask = 0,
  326. .cra_type = &crypto_blkcipher_type,
  327. .cra_module = THIS_MODULE,
  328. .cra_u = {
  329. .blkcipher = {
  330. .min_keysize = SERPENT_MIN_KEY_SIZE,
  331. .max_keysize = SERPENT_MAX_KEY_SIZE,
  332. .setkey = serpent_setkey,
  333. .encrypt = ecb_encrypt,
  334. .decrypt = ecb_decrypt,
  335. },
  336. },
  337. }, {
  338. .cra_name = "__cbc-serpent-sse2",
  339. .cra_driver_name = "__driver-cbc-serpent-sse2",
  340. .cra_priority = 0,
  341. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
  342. CRYPTO_ALG_INTERNAL,
  343. .cra_blocksize = SERPENT_BLOCK_SIZE,
  344. .cra_ctxsize = sizeof(struct serpent_ctx),
  345. .cra_alignmask = 0,
  346. .cra_type = &crypto_blkcipher_type,
  347. .cra_module = THIS_MODULE,
  348. .cra_u = {
  349. .blkcipher = {
  350. .min_keysize = SERPENT_MIN_KEY_SIZE,
  351. .max_keysize = SERPENT_MAX_KEY_SIZE,
  352. .setkey = serpent_setkey,
  353. .encrypt = cbc_encrypt,
  354. .decrypt = cbc_decrypt,
  355. },
  356. },
  357. }, {
  358. .cra_name = "__ctr-serpent-sse2",
  359. .cra_driver_name = "__driver-ctr-serpent-sse2",
  360. .cra_priority = 0,
  361. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
  362. CRYPTO_ALG_INTERNAL,
  363. .cra_blocksize = 1,
  364. .cra_ctxsize = sizeof(struct serpent_ctx),
  365. .cra_alignmask = 0,
  366. .cra_type = &crypto_blkcipher_type,
  367. .cra_module = THIS_MODULE,
  368. .cra_u = {
  369. .blkcipher = {
  370. .min_keysize = SERPENT_MIN_KEY_SIZE,
  371. .max_keysize = SERPENT_MAX_KEY_SIZE,
  372. .ivsize = SERPENT_BLOCK_SIZE,
  373. .setkey = serpent_setkey,
  374. .encrypt = ctr_crypt,
  375. .decrypt = ctr_crypt,
  376. },
  377. },
  378. }, {
  379. .cra_name = "__lrw-serpent-sse2",
  380. .cra_driver_name = "__driver-lrw-serpent-sse2",
  381. .cra_priority = 0,
  382. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
  383. CRYPTO_ALG_INTERNAL,
  384. .cra_blocksize = SERPENT_BLOCK_SIZE,
  385. .cra_ctxsize = sizeof(struct serpent_lrw_ctx),
  386. .cra_alignmask = 0,
  387. .cra_type = &crypto_blkcipher_type,
  388. .cra_module = THIS_MODULE,
  389. .cra_exit = lrw_exit_tfm,
  390. .cra_u = {
  391. .blkcipher = {
  392. .min_keysize = SERPENT_MIN_KEY_SIZE +
  393. SERPENT_BLOCK_SIZE,
  394. .max_keysize = SERPENT_MAX_KEY_SIZE +
  395. SERPENT_BLOCK_SIZE,
  396. .ivsize = SERPENT_BLOCK_SIZE,
  397. .setkey = lrw_serpent_setkey,
  398. .encrypt = lrw_encrypt,
  399. .decrypt = lrw_decrypt,
  400. },
  401. },
  402. }, {
  403. .cra_name = "__xts-serpent-sse2",
  404. .cra_driver_name = "__driver-xts-serpent-sse2",
  405. .cra_priority = 0,
  406. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
  407. CRYPTO_ALG_INTERNAL,
  408. .cra_blocksize = SERPENT_BLOCK_SIZE,
  409. .cra_ctxsize = sizeof(struct serpent_xts_ctx),
  410. .cra_alignmask = 0,
  411. .cra_type = &crypto_blkcipher_type,
  412. .cra_module = THIS_MODULE,
  413. .cra_u = {
  414. .blkcipher = {
  415. .min_keysize = SERPENT_MIN_KEY_SIZE * 2,
  416. .max_keysize = SERPENT_MAX_KEY_SIZE * 2,
  417. .ivsize = SERPENT_BLOCK_SIZE,
  418. .setkey = xts_serpent_setkey,
  419. .encrypt = xts_encrypt,
  420. .decrypt = xts_decrypt,
  421. },
  422. },
  423. }, {
  424. .cra_name = "ecb(serpent)",
  425. .cra_driver_name = "ecb-serpent-sse2",
  426. .cra_priority = 400,
  427. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  428. .cra_blocksize = SERPENT_BLOCK_SIZE,
  429. .cra_ctxsize = sizeof(struct async_helper_ctx),
  430. .cra_alignmask = 0,
  431. .cra_type = &crypto_ablkcipher_type,
  432. .cra_module = THIS_MODULE,
  433. .cra_init = ablk_init,
  434. .cra_exit = ablk_exit,
  435. .cra_u = {
  436. .ablkcipher = {
  437. .min_keysize = SERPENT_MIN_KEY_SIZE,
  438. .max_keysize = SERPENT_MAX_KEY_SIZE,
  439. .setkey = ablk_set_key,
  440. .encrypt = ablk_encrypt,
  441. .decrypt = ablk_decrypt,
  442. },
  443. },
  444. }, {
  445. .cra_name = "cbc(serpent)",
  446. .cra_driver_name = "cbc-serpent-sse2",
  447. .cra_priority = 400,
  448. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  449. .cra_blocksize = SERPENT_BLOCK_SIZE,
  450. .cra_ctxsize = sizeof(struct async_helper_ctx),
  451. .cra_alignmask = 0,
  452. .cra_type = &crypto_ablkcipher_type,
  453. .cra_module = THIS_MODULE,
  454. .cra_init = ablk_init,
  455. .cra_exit = ablk_exit,
  456. .cra_u = {
  457. .ablkcipher = {
  458. .min_keysize = SERPENT_MIN_KEY_SIZE,
  459. .max_keysize = SERPENT_MAX_KEY_SIZE,
  460. .ivsize = SERPENT_BLOCK_SIZE,
  461. .setkey = ablk_set_key,
  462. .encrypt = __ablk_encrypt,
  463. .decrypt = ablk_decrypt,
  464. },
  465. },
  466. }, {
  467. .cra_name = "ctr(serpent)",
  468. .cra_driver_name = "ctr-serpent-sse2",
  469. .cra_priority = 400,
  470. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  471. .cra_blocksize = 1,
  472. .cra_ctxsize = sizeof(struct async_helper_ctx),
  473. .cra_alignmask = 0,
  474. .cra_type = &crypto_ablkcipher_type,
  475. .cra_module = THIS_MODULE,
  476. .cra_init = ablk_init,
  477. .cra_exit = ablk_exit,
  478. .cra_u = {
  479. .ablkcipher = {
  480. .min_keysize = SERPENT_MIN_KEY_SIZE,
  481. .max_keysize = SERPENT_MAX_KEY_SIZE,
  482. .ivsize = SERPENT_BLOCK_SIZE,
  483. .setkey = ablk_set_key,
  484. .encrypt = ablk_encrypt,
  485. .decrypt = ablk_encrypt,
  486. .geniv = "chainiv",
  487. },
  488. },
  489. }, {
  490. .cra_name = "lrw(serpent)",
  491. .cra_driver_name = "lrw-serpent-sse2",
  492. .cra_priority = 400,
  493. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  494. .cra_blocksize = SERPENT_BLOCK_SIZE,
  495. .cra_ctxsize = sizeof(struct async_helper_ctx),
  496. .cra_alignmask = 0,
  497. .cra_type = &crypto_ablkcipher_type,
  498. .cra_module = THIS_MODULE,
  499. .cra_init = ablk_init,
  500. .cra_exit = ablk_exit,
  501. .cra_u = {
  502. .ablkcipher = {
  503. .min_keysize = SERPENT_MIN_KEY_SIZE +
  504. SERPENT_BLOCK_SIZE,
  505. .max_keysize = SERPENT_MAX_KEY_SIZE +
  506. SERPENT_BLOCK_SIZE,
  507. .ivsize = SERPENT_BLOCK_SIZE,
  508. .setkey = ablk_set_key,
  509. .encrypt = ablk_encrypt,
  510. .decrypt = ablk_decrypt,
  511. },
  512. },
  513. }, {
  514. .cra_name = "xts(serpent)",
  515. .cra_driver_name = "xts-serpent-sse2",
  516. .cra_priority = 400,
  517. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  518. .cra_blocksize = SERPENT_BLOCK_SIZE,
  519. .cra_ctxsize = sizeof(struct async_helper_ctx),
  520. .cra_alignmask = 0,
  521. .cra_type = &crypto_ablkcipher_type,
  522. .cra_module = THIS_MODULE,
  523. .cra_init = ablk_init,
  524. .cra_exit = ablk_exit,
  525. .cra_u = {
  526. .ablkcipher = {
  527. .min_keysize = SERPENT_MIN_KEY_SIZE * 2,
  528. .max_keysize = SERPENT_MAX_KEY_SIZE * 2,
  529. .ivsize = SERPENT_BLOCK_SIZE,
  530. .setkey = ablk_set_key,
  531. .encrypt = ablk_encrypt,
  532. .decrypt = ablk_decrypt,
  533. },
  534. },
  535. } };
  536. static int __init serpent_sse2_init(void)
  537. {
  538. if (!boot_cpu_has(X86_FEATURE_XMM2)) {
  539. printk(KERN_INFO "SSE2 instructions are not detected.\n");
  540. return -ENODEV;
  541. }
  542. return crypto_register_algs(serpent_algs, ARRAY_SIZE(serpent_algs));
  543. }
  544. static void __exit serpent_sse2_exit(void)
  545. {
  546. crypto_unregister_algs(serpent_algs, ARRAY_SIZE(serpent_algs));
  547. }
  548. module_init(serpent_sse2_init);
  549. module_exit(serpent_sse2_exit);
  550. MODULE_DESCRIPTION("Serpent Cipher Algorithm, SSE2 optimized");
  551. MODULE_LICENSE("GPL");
  552. MODULE_ALIAS_CRYPTO("serpent");