2 * Glue Code for SSE2 assembler versions of Serpent Cipher
4 * Copyright (c) 2011 Jussi Kivilinna <jussi.kivilinna@mbnet.fi>
6 * Glue code based on aesni-intel_glue.c by:
7 * Copyright (C) 2008, Intel Corp.
8 * Author: Huang Ying <ying.huang@intel.com>
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>
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.
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.
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
32 #include <linux/module.h>
33 #include <linux/hardirq.h>
34 #include <linux/types.h>
35 #include <linux/crypto.h>
36 #include <linux/err.h>
37 #include <crypto/ablk_helper.h>
38 #include <crypto/algapi.h>
39 #include <crypto/serpent.h>
40 #include <crypto/cryptd.h>
41 #include <crypto/b128ops.h>
42 #include <crypto/ctr.h>
43 #include <crypto/lrw.h>
44 #include <crypto/xts.h>
45 #include <asm/crypto/serpent-sse2.h>
46 #include <asm/crypto/glue_helper.h>
48 static void serpent_decrypt_cbc_xway(void *ctx
, u128
*dst
, const u128
*src
)
50 u128 ivs
[SERPENT_PARALLEL_BLOCKS
- 1];
53 for (j
= 0; j
< SERPENT_PARALLEL_BLOCKS
- 1; j
++)
56 serpent_dec_blk_xway(ctx
, (u8
*)dst
, (u8
*)src
);
58 for (j
= 0; j
< SERPENT_PARALLEL_BLOCKS
- 1; j
++)
59 u128_xor(dst
+ (j
+ 1), dst
+ (j
+ 1), ivs
+ j
);
62 static void serpent_crypt_ctr(void *ctx
, u128
*dst
, const u128
*src
, le128
*iv
)
66 le128_to_be128(&ctrblk
, iv
);
69 __serpent_encrypt(ctx
, (u8
*)&ctrblk
, (u8
*)&ctrblk
);
70 u128_xor(dst
, src
, (u128
*)&ctrblk
);
73 static void serpent_crypt_ctr_xway(void *ctx
, u128
*dst
, const u128
*src
,
76 be128 ctrblks
[SERPENT_PARALLEL_BLOCKS
];
79 for (i
= 0; i
< SERPENT_PARALLEL_BLOCKS
; i
++) {
83 le128_to_be128(&ctrblks
[i
], iv
);
87 serpent_enc_blk_xway_xor(ctx
, (u8
*)dst
, (u8
*)ctrblks
);
90 static const struct common_glue_ctx serpent_enc
= {
92 .fpu_blocks_limit
= SERPENT_PARALLEL_BLOCKS
,
95 .num_blocks
= SERPENT_PARALLEL_BLOCKS
,
96 .fn_u
= { .ecb
= GLUE_FUNC_CAST(serpent_enc_blk_xway
) }
99 .fn_u
= { .ecb
= GLUE_FUNC_CAST(__serpent_encrypt
) }
103 static const struct common_glue_ctx serpent_ctr
= {
105 .fpu_blocks_limit
= SERPENT_PARALLEL_BLOCKS
,
108 .num_blocks
= SERPENT_PARALLEL_BLOCKS
,
109 .fn_u
= { .ctr
= GLUE_CTR_FUNC_CAST(serpent_crypt_ctr_xway
) }
112 .fn_u
= { .ctr
= GLUE_CTR_FUNC_CAST(serpent_crypt_ctr
) }
116 static const struct common_glue_ctx serpent_dec
= {
118 .fpu_blocks_limit
= SERPENT_PARALLEL_BLOCKS
,
121 .num_blocks
= SERPENT_PARALLEL_BLOCKS
,
122 .fn_u
= { .ecb
= GLUE_FUNC_CAST(serpent_dec_blk_xway
) }
125 .fn_u
= { .ecb
= GLUE_FUNC_CAST(__serpent_decrypt
) }
129 static const struct common_glue_ctx serpent_dec_cbc
= {
131 .fpu_blocks_limit
= SERPENT_PARALLEL_BLOCKS
,
134 .num_blocks
= SERPENT_PARALLEL_BLOCKS
,
135 .fn_u
= { .cbc
= GLUE_CBC_FUNC_CAST(serpent_decrypt_cbc_xway
) }
138 .fn_u
= { .cbc
= GLUE_CBC_FUNC_CAST(__serpent_decrypt
) }
142 static int ecb_encrypt(struct blkcipher_desc
*desc
, struct scatterlist
*dst
,
143 struct scatterlist
*src
, unsigned int nbytes
)
145 return glue_ecb_crypt_128bit(&serpent_enc
, desc
, dst
, src
, nbytes
);
148 static int ecb_decrypt(struct blkcipher_desc
*desc
, struct scatterlist
*dst
,
149 struct scatterlist
*src
, unsigned int nbytes
)
151 return glue_ecb_crypt_128bit(&serpent_dec
, desc
, dst
, src
, nbytes
);
154 static int cbc_encrypt(struct blkcipher_desc
*desc
, struct scatterlist
*dst
,
155 struct scatterlist
*src
, unsigned int nbytes
)
157 return glue_cbc_encrypt_128bit(GLUE_FUNC_CAST(__serpent_encrypt
), desc
,
161 static int cbc_decrypt(struct blkcipher_desc
*desc
, struct scatterlist
*dst
,
162 struct scatterlist
*src
, unsigned int nbytes
)
164 return glue_cbc_decrypt_128bit(&serpent_dec_cbc
, desc
, dst
, src
,
168 static int ctr_crypt(struct blkcipher_desc
*desc
, struct scatterlist
*dst
,
169 struct scatterlist
*src
, unsigned int nbytes
)
171 return glue_ctr_crypt_128bit(&serpent_ctr
, desc
, dst
, src
, nbytes
);
174 static inline bool serpent_fpu_begin(bool fpu_enabled
, unsigned int nbytes
)
176 return glue_fpu_begin(SERPENT_BLOCK_SIZE
, SERPENT_PARALLEL_BLOCKS
,
177 NULL
, fpu_enabled
, nbytes
);
180 static inline void serpent_fpu_end(bool fpu_enabled
)
182 glue_fpu_end(fpu_enabled
);
186 struct serpent_ctx
*ctx
;
190 static void encrypt_callback(void *priv
, u8
*srcdst
, unsigned int nbytes
)
192 const unsigned int bsize
= SERPENT_BLOCK_SIZE
;
193 struct crypt_priv
*ctx
= priv
;
196 ctx
->fpu_enabled
= serpent_fpu_begin(ctx
->fpu_enabled
, nbytes
);
198 if (nbytes
== bsize
* SERPENT_PARALLEL_BLOCKS
) {
199 serpent_enc_blk_xway(ctx
->ctx
, srcdst
, srcdst
);
203 for (i
= 0; i
< nbytes
/ bsize
; i
++, srcdst
+= bsize
)
204 __serpent_encrypt(ctx
->ctx
, srcdst
, srcdst
);
207 static void decrypt_callback(void *priv
, u8
*srcdst
, unsigned int nbytes
)
209 const unsigned int bsize
= SERPENT_BLOCK_SIZE
;
210 struct crypt_priv
*ctx
= priv
;
213 ctx
->fpu_enabled
= serpent_fpu_begin(ctx
->fpu_enabled
, nbytes
);
215 if (nbytes
== bsize
* SERPENT_PARALLEL_BLOCKS
) {
216 serpent_dec_blk_xway(ctx
->ctx
, srcdst
, srcdst
);
220 for (i
= 0; i
< nbytes
/ bsize
; i
++, srcdst
+= bsize
)
221 __serpent_decrypt(ctx
->ctx
, srcdst
, srcdst
);
224 struct serpent_lrw_ctx
{
225 struct lrw_table_ctx lrw_table
;
226 struct serpent_ctx serpent_ctx
;
229 static int lrw_serpent_setkey(struct crypto_tfm
*tfm
, const u8
*key
,
232 struct serpent_lrw_ctx
*ctx
= crypto_tfm_ctx(tfm
);
235 err
= __serpent_setkey(&ctx
->serpent_ctx
, key
, keylen
-
240 return lrw_init_table(&ctx
->lrw_table
, key
+ keylen
-
244 static int lrw_encrypt(struct blkcipher_desc
*desc
, struct scatterlist
*dst
,
245 struct scatterlist
*src
, unsigned int nbytes
)
247 struct serpent_lrw_ctx
*ctx
= crypto_blkcipher_ctx(desc
->tfm
);
248 be128 buf
[SERPENT_PARALLEL_BLOCKS
];
249 struct crypt_priv crypt_ctx
= {
250 .ctx
= &ctx
->serpent_ctx
,
251 .fpu_enabled
= false,
253 struct lrw_crypt_req req
= {
255 .tbuflen
= sizeof(buf
),
257 .table_ctx
= &ctx
->lrw_table
,
258 .crypt_ctx
= &crypt_ctx
,
259 .crypt_fn
= encrypt_callback
,
263 desc
->flags
&= ~CRYPTO_TFM_REQ_MAY_SLEEP
;
264 ret
= lrw_crypt(desc
, dst
, src
, nbytes
, &req
);
265 serpent_fpu_end(crypt_ctx
.fpu_enabled
);
270 static int lrw_decrypt(struct blkcipher_desc
*desc
, struct scatterlist
*dst
,
271 struct scatterlist
*src
, unsigned int nbytes
)
273 struct serpent_lrw_ctx
*ctx
= crypto_blkcipher_ctx(desc
->tfm
);
274 be128 buf
[SERPENT_PARALLEL_BLOCKS
];
275 struct crypt_priv crypt_ctx
= {
276 .ctx
= &ctx
->serpent_ctx
,
277 .fpu_enabled
= false,
279 struct lrw_crypt_req req
= {
281 .tbuflen
= sizeof(buf
),
283 .table_ctx
= &ctx
->lrw_table
,
284 .crypt_ctx
= &crypt_ctx
,
285 .crypt_fn
= decrypt_callback
,
289 desc
->flags
&= ~CRYPTO_TFM_REQ_MAY_SLEEP
;
290 ret
= lrw_crypt(desc
, dst
, src
, nbytes
, &req
);
291 serpent_fpu_end(crypt_ctx
.fpu_enabled
);
296 static void lrw_exit_tfm(struct crypto_tfm
*tfm
)
298 struct serpent_lrw_ctx
*ctx
= crypto_tfm_ctx(tfm
);
300 lrw_free_table(&ctx
->lrw_table
);
303 struct serpent_xts_ctx
{
304 struct serpent_ctx tweak_ctx
;
305 struct serpent_ctx crypt_ctx
;
308 static int xts_serpent_setkey(struct crypto_tfm
*tfm
, const u8
*key
,
311 struct serpent_xts_ctx
*ctx
= crypto_tfm_ctx(tfm
);
314 err
= xts_check_key(tfm
, key
, keylen
);
318 /* first half of xts-key is for crypt */
319 err
= __serpent_setkey(&ctx
->crypt_ctx
, key
, keylen
/ 2);
323 /* second half of xts-key is for tweak */
324 return __serpent_setkey(&ctx
->tweak_ctx
, key
+ keylen
/ 2, keylen
/ 2);
327 static int xts_encrypt(struct blkcipher_desc
*desc
, struct scatterlist
*dst
,
328 struct scatterlist
*src
, unsigned int nbytes
)
330 struct serpent_xts_ctx
*ctx
= crypto_blkcipher_ctx(desc
->tfm
);
331 le128 buf
[SERPENT_PARALLEL_BLOCKS
];
332 struct crypt_priv crypt_ctx
= {
333 .ctx
= &ctx
->crypt_ctx
,
334 .fpu_enabled
= false,
336 struct xts_crypt_req req
= {
338 .tbuflen
= sizeof(buf
),
340 .tweak_ctx
= &ctx
->tweak_ctx
,
341 .tweak_fn
= XTS_TWEAK_CAST(__serpent_encrypt
),
342 .crypt_ctx
= &crypt_ctx
,
343 .crypt_fn
= encrypt_callback
,
347 desc
->flags
&= ~CRYPTO_TFM_REQ_MAY_SLEEP
;
348 ret
= xts_crypt(desc
, dst
, src
, nbytes
, &req
);
349 serpent_fpu_end(crypt_ctx
.fpu_enabled
);
354 static int xts_decrypt(struct blkcipher_desc
*desc
, struct scatterlist
*dst
,
355 struct scatterlist
*src
, unsigned int nbytes
)
357 struct serpent_xts_ctx
*ctx
= crypto_blkcipher_ctx(desc
->tfm
);
358 le128 buf
[SERPENT_PARALLEL_BLOCKS
];
359 struct crypt_priv crypt_ctx
= {
360 .ctx
= &ctx
->crypt_ctx
,
361 .fpu_enabled
= false,
363 struct xts_crypt_req req
= {
365 .tbuflen
= sizeof(buf
),
367 .tweak_ctx
= &ctx
->tweak_ctx
,
368 .tweak_fn
= XTS_TWEAK_CAST(__serpent_encrypt
),
369 .crypt_ctx
= &crypt_ctx
,
370 .crypt_fn
= decrypt_callback
,
374 desc
->flags
&= ~CRYPTO_TFM_REQ_MAY_SLEEP
;
375 ret
= xts_crypt(desc
, dst
, src
, nbytes
, &req
);
376 serpent_fpu_end(crypt_ctx
.fpu_enabled
);
381 static struct crypto_alg serpent_algs
[10] = { {
382 .cra_name
= "__ecb-serpent-sse2",
383 .cra_driver_name
= "__driver-ecb-serpent-sse2",
385 .cra_flags
= CRYPTO_ALG_TYPE_BLKCIPHER
|
387 .cra_blocksize
= SERPENT_BLOCK_SIZE
,
388 .cra_ctxsize
= sizeof(struct serpent_ctx
),
390 .cra_type
= &crypto_blkcipher_type
,
391 .cra_module
= THIS_MODULE
,
394 .min_keysize
= SERPENT_MIN_KEY_SIZE
,
395 .max_keysize
= SERPENT_MAX_KEY_SIZE
,
396 .setkey
= serpent_setkey
,
397 .encrypt
= ecb_encrypt
,
398 .decrypt
= ecb_decrypt
,
402 .cra_name
= "__cbc-serpent-sse2",
403 .cra_driver_name
= "__driver-cbc-serpent-sse2",
405 .cra_flags
= CRYPTO_ALG_TYPE_BLKCIPHER
|
407 .cra_blocksize
= SERPENT_BLOCK_SIZE
,
408 .cra_ctxsize
= sizeof(struct serpent_ctx
),
410 .cra_type
= &crypto_blkcipher_type
,
411 .cra_module
= THIS_MODULE
,
414 .min_keysize
= SERPENT_MIN_KEY_SIZE
,
415 .max_keysize
= SERPENT_MAX_KEY_SIZE
,
416 .setkey
= serpent_setkey
,
417 .encrypt
= cbc_encrypt
,
418 .decrypt
= cbc_decrypt
,
422 .cra_name
= "__ctr-serpent-sse2",
423 .cra_driver_name
= "__driver-ctr-serpent-sse2",
425 .cra_flags
= CRYPTO_ALG_TYPE_BLKCIPHER
|
428 .cra_ctxsize
= sizeof(struct serpent_ctx
),
430 .cra_type
= &crypto_blkcipher_type
,
431 .cra_module
= THIS_MODULE
,
434 .min_keysize
= SERPENT_MIN_KEY_SIZE
,
435 .max_keysize
= SERPENT_MAX_KEY_SIZE
,
436 .ivsize
= SERPENT_BLOCK_SIZE
,
437 .setkey
= serpent_setkey
,
438 .encrypt
= ctr_crypt
,
439 .decrypt
= ctr_crypt
,
443 .cra_name
= "__lrw-serpent-sse2",
444 .cra_driver_name
= "__driver-lrw-serpent-sse2",
446 .cra_flags
= CRYPTO_ALG_TYPE_BLKCIPHER
|
448 .cra_blocksize
= SERPENT_BLOCK_SIZE
,
449 .cra_ctxsize
= sizeof(struct serpent_lrw_ctx
),
451 .cra_type
= &crypto_blkcipher_type
,
452 .cra_module
= THIS_MODULE
,
453 .cra_exit
= lrw_exit_tfm
,
456 .min_keysize
= SERPENT_MIN_KEY_SIZE
+
458 .max_keysize
= SERPENT_MAX_KEY_SIZE
+
460 .ivsize
= SERPENT_BLOCK_SIZE
,
461 .setkey
= lrw_serpent_setkey
,
462 .encrypt
= lrw_encrypt
,
463 .decrypt
= lrw_decrypt
,
467 .cra_name
= "__xts-serpent-sse2",
468 .cra_driver_name
= "__driver-xts-serpent-sse2",
470 .cra_flags
= CRYPTO_ALG_TYPE_BLKCIPHER
|
472 .cra_blocksize
= SERPENT_BLOCK_SIZE
,
473 .cra_ctxsize
= sizeof(struct serpent_xts_ctx
),
475 .cra_type
= &crypto_blkcipher_type
,
476 .cra_module
= THIS_MODULE
,
479 .min_keysize
= SERPENT_MIN_KEY_SIZE
* 2,
480 .max_keysize
= SERPENT_MAX_KEY_SIZE
* 2,
481 .ivsize
= SERPENT_BLOCK_SIZE
,
482 .setkey
= xts_serpent_setkey
,
483 .encrypt
= xts_encrypt
,
484 .decrypt
= xts_decrypt
,
488 .cra_name
= "ecb(serpent)",
489 .cra_driver_name
= "ecb-serpent-sse2",
491 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
| CRYPTO_ALG_ASYNC
,
492 .cra_blocksize
= SERPENT_BLOCK_SIZE
,
493 .cra_ctxsize
= sizeof(struct async_helper_ctx
),
495 .cra_type
= &crypto_ablkcipher_type
,
496 .cra_module
= THIS_MODULE
,
497 .cra_init
= ablk_init
,
498 .cra_exit
= ablk_exit
,
501 .min_keysize
= SERPENT_MIN_KEY_SIZE
,
502 .max_keysize
= SERPENT_MAX_KEY_SIZE
,
503 .setkey
= ablk_set_key
,
504 .encrypt
= ablk_encrypt
,
505 .decrypt
= ablk_decrypt
,
509 .cra_name
= "cbc(serpent)",
510 .cra_driver_name
= "cbc-serpent-sse2",
512 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
| CRYPTO_ALG_ASYNC
,
513 .cra_blocksize
= SERPENT_BLOCK_SIZE
,
514 .cra_ctxsize
= sizeof(struct async_helper_ctx
),
516 .cra_type
= &crypto_ablkcipher_type
,
517 .cra_module
= THIS_MODULE
,
518 .cra_init
= ablk_init
,
519 .cra_exit
= ablk_exit
,
522 .min_keysize
= SERPENT_MIN_KEY_SIZE
,
523 .max_keysize
= SERPENT_MAX_KEY_SIZE
,
524 .ivsize
= SERPENT_BLOCK_SIZE
,
525 .setkey
= ablk_set_key
,
526 .encrypt
= __ablk_encrypt
,
527 .decrypt
= ablk_decrypt
,
531 .cra_name
= "ctr(serpent)",
532 .cra_driver_name
= "ctr-serpent-sse2",
534 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
| CRYPTO_ALG_ASYNC
,
536 .cra_ctxsize
= sizeof(struct async_helper_ctx
),
538 .cra_type
= &crypto_ablkcipher_type
,
539 .cra_module
= THIS_MODULE
,
540 .cra_init
= ablk_init
,
541 .cra_exit
= ablk_exit
,
544 .min_keysize
= SERPENT_MIN_KEY_SIZE
,
545 .max_keysize
= SERPENT_MAX_KEY_SIZE
,
546 .ivsize
= SERPENT_BLOCK_SIZE
,
547 .setkey
= ablk_set_key
,
548 .encrypt
= ablk_encrypt
,
549 .decrypt
= ablk_encrypt
,
554 .cra_name
= "lrw(serpent)",
555 .cra_driver_name
= "lrw-serpent-sse2",
557 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
| CRYPTO_ALG_ASYNC
,
558 .cra_blocksize
= SERPENT_BLOCK_SIZE
,
559 .cra_ctxsize
= sizeof(struct async_helper_ctx
),
561 .cra_type
= &crypto_ablkcipher_type
,
562 .cra_module
= THIS_MODULE
,
563 .cra_init
= ablk_init
,
564 .cra_exit
= ablk_exit
,
567 .min_keysize
= SERPENT_MIN_KEY_SIZE
+
569 .max_keysize
= SERPENT_MAX_KEY_SIZE
+
571 .ivsize
= SERPENT_BLOCK_SIZE
,
572 .setkey
= ablk_set_key
,
573 .encrypt
= ablk_encrypt
,
574 .decrypt
= ablk_decrypt
,
578 .cra_name
= "xts(serpent)",
579 .cra_driver_name
= "xts-serpent-sse2",
581 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
| CRYPTO_ALG_ASYNC
,
582 .cra_blocksize
= SERPENT_BLOCK_SIZE
,
583 .cra_ctxsize
= sizeof(struct async_helper_ctx
),
585 .cra_type
= &crypto_ablkcipher_type
,
586 .cra_module
= THIS_MODULE
,
587 .cra_init
= ablk_init
,
588 .cra_exit
= ablk_exit
,
591 .min_keysize
= SERPENT_MIN_KEY_SIZE
* 2,
592 .max_keysize
= SERPENT_MAX_KEY_SIZE
* 2,
593 .ivsize
= SERPENT_BLOCK_SIZE
,
594 .setkey
= ablk_set_key
,
595 .encrypt
= ablk_encrypt
,
596 .decrypt
= ablk_decrypt
,
601 static int __init
serpent_sse2_init(void)
603 if (!boot_cpu_has(X86_FEATURE_XMM2
)) {
604 printk(KERN_INFO
"SSE2 instructions are not detected.\n");
608 return crypto_register_algs(serpent_algs
, ARRAY_SIZE(serpent_algs
));
611 static void __exit
serpent_sse2_exit(void)
613 crypto_unregister_algs(serpent_algs
, ARRAY_SIZE(serpent_algs
));
616 module_init(serpent_sse2_init
);
617 module_exit(serpent_sse2_exit
);
619 MODULE_DESCRIPTION("Serpent Cipher Algorithm, SSE2 optimized");
620 MODULE_LICENSE("GPL");
621 MODULE_ALIAS_CRYPTO("serpent");