serpent_sse2_glue.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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/types.h>
  33. #include <linux/crypto.h>
  34. #include <linux/err.h>
  35. #include <crypto/algapi.h>
  36. #include <crypto/b128ops.h>
  37. #include <crypto/internal/simd.h>
  38. #include <crypto/serpent.h>
  39. #include <asm/crypto/serpent-sse2.h>
  40. #include <asm/crypto/glue_helper.h>
  41. static int serpent_setkey_skcipher(struct crypto_skcipher *tfm,
  42. const u8 *key, unsigned int keylen)
  43. {
  44. return __serpent_setkey(crypto_skcipher_ctx(tfm), key, keylen);
  45. }
  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 skcipher_request *req)
  122. {
  123. return glue_ecb_req_128bit(&serpent_enc, req);
  124. }
  125. static int ecb_decrypt(struct skcipher_request *req)
  126. {
  127. return glue_ecb_req_128bit(&serpent_dec, req);
  128. }
  129. static int cbc_encrypt(struct skcipher_request *req)
  130. {
  131. return glue_cbc_encrypt_req_128bit(GLUE_FUNC_CAST(__serpent_encrypt),
  132. req);
  133. }
  134. static int cbc_decrypt(struct skcipher_request *req)
  135. {
  136. return glue_cbc_decrypt_req_128bit(&serpent_dec_cbc, req);
  137. }
  138. static int ctr_crypt(struct skcipher_request *req)
  139. {
  140. return glue_ctr_req_128bit(&serpent_ctr, req);
  141. }
  142. static struct skcipher_alg serpent_algs[] = {
  143. {
  144. .base.cra_name = "__ecb(serpent)",
  145. .base.cra_driver_name = "__ecb-serpent-sse2",
  146. .base.cra_priority = 400,
  147. .base.cra_flags = CRYPTO_ALG_INTERNAL,
  148. .base.cra_blocksize = SERPENT_BLOCK_SIZE,
  149. .base.cra_ctxsize = sizeof(struct serpent_ctx),
  150. .base.cra_module = THIS_MODULE,
  151. .min_keysize = SERPENT_MIN_KEY_SIZE,
  152. .max_keysize = SERPENT_MAX_KEY_SIZE,
  153. .setkey = serpent_setkey_skcipher,
  154. .encrypt = ecb_encrypt,
  155. .decrypt = ecb_decrypt,
  156. }, {
  157. .base.cra_name = "__cbc(serpent)",
  158. .base.cra_driver_name = "__cbc-serpent-sse2",
  159. .base.cra_priority = 400,
  160. .base.cra_flags = CRYPTO_ALG_INTERNAL,
  161. .base.cra_blocksize = SERPENT_BLOCK_SIZE,
  162. .base.cra_ctxsize = sizeof(struct serpent_ctx),
  163. .base.cra_module = THIS_MODULE,
  164. .min_keysize = SERPENT_MIN_KEY_SIZE,
  165. .max_keysize = SERPENT_MAX_KEY_SIZE,
  166. .ivsize = SERPENT_BLOCK_SIZE,
  167. .setkey = serpent_setkey_skcipher,
  168. .encrypt = cbc_encrypt,
  169. .decrypt = cbc_decrypt,
  170. }, {
  171. .base.cra_name = "__ctr(serpent)",
  172. .base.cra_driver_name = "__ctr-serpent-sse2",
  173. .base.cra_priority = 400,
  174. .base.cra_flags = CRYPTO_ALG_INTERNAL,
  175. .base.cra_blocksize = 1,
  176. .base.cra_ctxsize = sizeof(struct serpent_ctx),
  177. .base.cra_module = THIS_MODULE,
  178. .min_keysize = SERPENT_MIN_KEY_SIZE,
  179. .max_keysize = SERPENT_MAX_KEY_SIZE,
  180. .ivsize = SERPENT_BLOCK_SIZE,
  181. .chunksize = SERPENT_BLOCK_SIZE,
  182. .setkey = serpent_setkey_skcipher,
  183. .encrypt = ctr_crypt,
  184. .decrypt = ctr_crypt,
  185. },
  186. };
  187. static struct simd_skcipher_alg *serpent_simd_algs[ARRAY_SIZE(serpent_algs)];
  188. static int __init serpent_sse2_init(void)
  189. {
  190. if (!boot_cpu_has(X86_FEATURE_XMM2)) {
  191. printk(KERN_INFO "SSE2 instructions are not detected.\n");
  192. return -ENODEV;
  193. }
  194. return simd_register_skciphers_compat(serpent_algs,
  195. ARRAY_SIZE(serpent_algs),
  196. serpent_simd_algs);
  197. }
  198. static void __exit serpent_sse2_exit(void)
  199. {
  200. simd_unregister_skciphers(serpent_algs, ARRAY_SIZE(serpent_algs),
  201. serpent_simd_algs);
  202. }
  203. module_init(serpent_sse2_init);
  204. module_exit(serpent_sse2_exit);
  205. MODULE_DESCRIPTION("Serpent Cipher Algorithm, SSE2 optimized");
  206. MODULE_LICENSE("GPL");
  207. MODULE_ALIAS_CRYPTO("serpent");