2 * Glue Code for AVX assembler versions of Serpent Cipher
4 * Copyright (C) 2012 Johannes Goetzfried
5 * <Johannes.Goetzfried@informatik.stud.uni-erlangen.de>
7 * Copyright © 2011-2013 Jussi Kivilinna <jussi.kivilinna@iki.fi>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
26 #include <linux/module.h>
27 #include <linux/hardirq.h>
28 #include <linux/types.h>
29 #include <linux/crypto.h>
30 #include <linux/err.h>
31 #include <crypto/ablk_helper.h>
32 #include <crypto/algapi.h>
33 #include <crypto/serpent.h>
34 #include <crypto/cryptd.h>
35 #include <crypto/b128ops.h>
36 #include <crypto/ctr.h>
37 #include <crypto/lrw.h>
38 #include <crypto/xts.h>
39 #include <asm/fpu/api.h>
40 #include <asm/crypto/serpent-avx.h>
41 #include <asm/crypto/glue_helper.h>
43 /* 8-way parallel cipher functions */
44 asmlinkage void serpent_ecb_enc_8way_avx(struct serpent_ctx *ctx, u8 *dst,
46 EXPORT_SYMBOL_GPL(serpent_ecb_enc_8way_avx);
48 asmlinkage void serpent_ecb_dec_8way_avx(struct serpent_ctx *ctx, u8 *dst,
50 EXPORT_SYMBOL_GPL(serpent_ecb_dec_8way_avx);
52 asmlinkage void serpent_cbc_dec_8way_avx(struct serpent_ctx *ctx, u8 *dst,
54 EXPORT_SYMBOL_GPL(serpent_cbc_dec_8way_avx);
56 asmlinkage void serpent_ctr_8way_avx(struct serpent_ctx *ctx, u8 *dst,
57 const u8 *src, le128 *iv);
58 EXPORT_SYMBOL_GPL(serpent_ctr_8way_avx);
60 asmlinkage void serpent_xts_enc_8way_avx(struct serpent_ctx *ctx, u8 *dst,
61 const u8 *src, le128 *iv);
62 EXPORT_SYMBOL_GPL(serpent_xts_enc_8way_avx);
64 asmlinkage void serpent_xts_dec_8way_avx(struct serpent_ctx *ctx, u8 *dst,
65 const u8 *src, le128 *iv);
66 EXPORT_SYMBOL_GPL(serpent_xts_dec_8way_avx);
68 void __serpent_crypt_ctr(void *ctx, u128 *dst, const u128 *src, le128 *iv)
72 le128_to_be128(&ctrblk, iv);
75 __serpent_encrypt(ctx, (u8 *)&ctrblk, (u8 *)&ctrblk);
76 u128_xor(dst, src, (u128 *)&ctrblk);
78 EXPORT_SYMBOL_GPL(__serpent_crypt_ctr);
80 void serpent_xts_enc(void *ctx, u128 *dst, const u128 *src, le128 *iv)
82 glue_xts_crypt_128bit_one(ctx, dst, src, iv,
83 GLUE_FUNC_CAST(__serpent_encrypt));
85 EXPORT_SYMBOL_GPL(serpent_xts_enc);
87 void serpent_xts_dec(void *ctx, u128 *dst, const u128 *src, le128 *iv)
89 glue_xts_crypt_128bit_one(ctx, dst, src, iv,
90 GLUE_FUNC_CAST(__serpent_decrypt));
92 EXPORT_SYMBOL_GPL(serpent_xts_dec);
95 static const struct common_glue_ctx serpent_enc = {
97 .fpu_blocks_limit = SERPENT_PARALLEL_BLOCKS,
100 .num_blocks = SERPENT_PARALLEL_BLOCKS,
101 .fn_u = { .ecb = GLUE_FUNC_CAST(serpent_ecb_enc_8way_avx) }
104 .fn_u = { .ecb = GLUE_FUNC_CAST(__serpent_encrypt) }
108 static const struct common_glue_ctx serpent_ctr = {
110 .fpu_blocks_limit = SERPENT_PARALLEL_BLOCKS,
113 .num_blocks = SERPENT_PARALLEL_BLOCKS,
114 .fn_u = { .ctr = GLUE_CTR_FUNC_CAST(serpent_ctr_8way_avx) }
117 .fn_u = { .ctr = GLUE_CTR_FUNC_CAST(__serpent_crypt_ctr) }
121 static const struct common_glue_ctx serpent_enc_xts = {
123 .fpu_blocks_limit = SERPENT_PARALLEL_BLOCKS,
126 .num_blocks = SERPENT_PARALLEL_BLOCKS,
127 .fn_u = { .xts = GLUE_XTS_FUNC_CAST(serpent_xts_enc_8way_avx) }
130 .fn_u = { .xts = GLUE_XTS_FUNC_CAST(serpent_xts_enc) }
134 static const struct common_glue_ctx serpent_dec = {
136 .fpu_blocks_limit = SERPENT_PARALLEL_BLOCKS,
139 .num_blocks = SERPENT_PARALLEL_BLOCKS,
140 .fn_u = { .ecb = GLUE_FUNC_CAST(serpent_ecb_dec_8way_avx) }
143 .fn_u = { .ecb = GLUE_FUNC_CAST(__serpent_decrypt) }
147 static const struct common_glue_ctx serpent_dec_cbc = {
149 .fpu_blocks_limit = SERPENT_PARALLEL_BLOCKS,
152 .num_blocks = SERPENT_PARALLEL_BLOCKS,
153 .fn_u = { .cbc = GLUE_CBC_FUNC_CAST(serpent_cbc_dec_8way_avx) }
156 .fn_u = { .cbc = GLUE_CBC_FUNC_CAST(__serpent_decrypt) }
160 static const struct common_glue_ctx serpent_dec_xts = {
162 .fpu_blocks_limit = SERPENT_PARALLEL_BLOCKS,
165 .num_blocks = SERPENT_PARALLEL_BLOCKS,
166 .fn_u = { .xts = GLUE_XTS_FUNC_CAST(serpent_xts_dec_8way_avx) }
169 .fn_u = { .xts = GLUE_XTS_FUNC_CAST(serpent_xts_dec) }
173 static int ecb_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
174 struct scatterlist *src, unsigned int nbytes)
176 return glue_ecb_crypt_128bit(&serpent_enc, desc, dst, src, nbytes);
179 static int ecb_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
180 struct scatterlist *src, unsigned int nbytes)
182 return glue_ecb_crypt_128bit(&serpent_dec, desc, dst, src, nbytes);
185 static int cbc_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
186 struct scatterlist *src, unsigned int nbytes)
188 return glue_cbc_encrypt_128bit(GLUE_FUNC_CAST(__serpent_encrypt), desc,
192 static int cbc_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
193 struct scatterlist *src, unsigned int nbytes)
195 return glue_cbc_decrypt_128bit(&serpent_dec_cbc, desc, dst, src,
199 static int ctr_crypt(struct blkcipher_desc *desc, struct scatterlist *dst,
200 struct scatterlist *src, unsigned int nbytes)
202 return glue_ctr_crypt_128bit(&serpent_ctr, desc, dst, src, nbytes);
205 static inline bool serpent_fpu_begin(bool fpu_enabled, unsigned int nbytes)
207 return glue_fpu_begin(SERPENT_BLOCK_SIZE, SERPENT_PARALLEL_BLOCKS,
208 NULL, fpu_enabled, nbytes);
211 static inline void serpent_fpu_end(bool fpu_enabled)
213 glue_fpu_end(fpu_enabled);
217 struct serpent_ctx *ctx;
221 static void encrypt_callback(void *priv, u8 *srcdst, unsigned int nbytes)
223 const unsigned int bsize = SERPENT_BLOCK_SIZE;
224 struct crypt_priv *ctx = priv;
227 ctx->fpu_enabled = serpent_fpu_begin(ctx->fpu_enabled, nbytes);
229 if (nbytes == bsize * SERPENT_PARALLEL_BLOCKS) {
230 serpent_ecb_enc_8way_avx(ctx->ctx, srcdst, srcdst);
234 for (i = 0; i < nbytes / bsize; i++, srcdst += bsize)
235 __serpent_encrypt(ctx->ctx, srcdst, srcdst);
238 static void decrypt_callback(void *priv, u8 *srcdst, unsigned int nbytes)
240 const unsigned int bsize = SERPENT_BLOCK_SIZE;
241 struct crypt_priv *ctx = priv;
244 ctx->fpu_enabled = serpent_fpu_begin(ctx->fpu_enabled, nbytes);
246 if (nbytes == bsize * SERPENT_PARALLEL_BLOCKS) {
247 serpent_ecb_dec_8way_avx(ctx->ctx, srcdst, srcdst);
251 for (i = 0; i < nbytes / bsize; i++, srcdst += bsize)
252 __serpent_decrypt(ctx->ctx, srcdst, srcdst);
255 int lrw_serpent_setkey(struct crypto_tfm *tfm, const u8 *key,
258 struct serpent_lrw_ctx *ctx = crypto_tfm_ctx(tfm);
261 err = __serpent_setkey(&ctx->serpent_ctx, key, keylen -
266 return lrw_init_table(&ctx->lrw_table, key + keylen -
269 EXPORT_SYMBOL_GPL(lrw_serpent_setkey);
271 static int lrw_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
272 struct scatterlist *src, unsigned int nbytes)
274 struct serpent_lrw_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
275 be128 buf[SERPENT_PARALLEL_BLOCKS];
276 struct crypt_priv crypt_ctx = {
277 .ctx = &ctx->serpent_ctx,
278 .fpu_enabled = false,
280 struct lrw_crypt_req req = {
282 .tbuflen = sizeof(buf),
284 .table_ctx = &ctx->lrw_table,
285 .crypt_ctx = &crypt_ctx,
286 .crypt_fn = encrypt_callback,
290 desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
291 ret = lrw_crypt(desc, dst, src, nbytes, &req);
292 serpent_fpu_end(crypt_ctx.fpu_enabled);
297 static int lrw_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
298 struct scatterlist *src, unsigned int nbytes)
300 struct serpent_lrw_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
301 be128 buf[SERPENT_PARALLEL_BLOCKS];
302 struct crypt_priv crypt_ctx = {
303 .ctx = &ctx->serpent_ctx,
304 .fpu_enabled = false,
306 struct lrw_crypt_req req = {
308 .tbuflen = sizeof(buf),
310 .table_ctx = &ctx->lrw_table,
311 .crypt_ctx = &crypt_ctx,
312 .crypt_fn = decrypt_callback,
316 desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
317 ret = lrw_crypt(desc, dst, src, nbytes, &req);
318 serpent_fpu_end(crypt_ctx.fpu_enabled);
323 void lrw_serpent_exit_tfm(struct crypto_tfm *tfm)
325 struct serpent_lrw_ctx *ctx = crypto_tfm_ctx(tfm);
327 lrw_free_table(&ctx->lrw_table);
329 EXPORT_SYMBOL_GPL(lrw_serpent_exit_tfm);
331 int xts_serpent_setkey(struct crypto_tfm *tfm, const u8 *key,
334 struct serpent_xts_ctx *ctx = crypto_tfm_ctx(tfm);
337 err = xts_check_key(tfm, key, keylen);
341 /* first half of xts-key is for crypt */
342 err = __serpent_setkey(&ctx->crypt_ctx, key, keylen / 2);
346 /* second half of xts-key is for tweak */
347 return __serpent_setkey(&ctx->tweak_ctx, key + keylen / 2, keylen / 2);
349 EXPORT_SYMBOL_GPL(xts_serpent_setkey);
351 static int xts_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
352 struct scatterlist *src, unsigned int nbytes)
354 struct serpent_xts_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
356 return glue_xts_crypt_128bit(&serpent_enc_xts, desc, dst, src, nbytes,
357 XTS_TWEAK_CAST(__serpent_encrypt),
358 &ctx->tweak_ctx, &ctx->crypt_ctx);
361 static int xts_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
362 struct scatterlist *src, unsigned int nbytes)
364 struct serpent_xts_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
366 return glue_xts_crypt_128bit(&serpent_dec_xts, desc, dst, src, nbytes,
367 XTS_TWEAK_CAST(__serpent_encrypt),
368 &ctx->tweak_ctx, &ctx->crypt_ctx);
371 static struct crypto_alg serpent_algs[10] = { {
372 .cra_name = "__ecb-serpent-avx",
373 .cra_driver_name = "__driver-ecb-serpent-avx",
375 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
377 .cra_blocksize = SERPENT_BLOCK_SIZE,
378 .cra_ctxsize = sizeof(struct serpent_ctx),
380 .cra_type = &crypto_blkcipher_type,
381 .cra_module = THIS_MODULE,
384 .min_keysize = SERPENT_MIN_KEY_SIZE,
385 .max_keysize = SERPENT_MAX_KEY_SIZE,
386 .setkey = serpent_setkey,
387 .encrypt = ecb_encrypt,
388 .decrypt = ecb_decrypt,
392 .cra_name = "__cbc-serpent-avx",
393 .cra_driver_name = "__driver-cbc-serpent-avx",
395 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
397 .cra_blocksize = SERPENT_BLOCK_SIZE,
398 .cra_ctxsize = sizeof(struct serpent_ctx),
400 .cra_type = &crypto_blkcipher_type,
401 .cra_module = THIS_MODULE,
404 .min_keysize = SERPENT_MIN_KEY_SIZE,
405 .max_keysize = SERPENT_MAX_KEY_SIZE,
406 .setkey = serpent_setkey,
407 .encrypt = cbc_encrypt,
408 .decrypt = cbc_decrypt,
412 .cra_name = "__ctr-serpent-avx",
413 .cra_driver_name = "__driver-ctr-serpent-avx",
415 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
418 .cra_ctxsize = sizeof(struct serpent_ctx),
420 .cra_type = &crypto_blkcipher_type,
421 .cra_module = THIS_MODULE,
424 .min_keysize = SERPENT_MIN_KEY_SIZE,
425 .max_keysize = SERPENT_MAX_KEY_SIZE,
426 .ivsize = SERPENT_BLOCK_SIZE,
427 .setkey = serpent_setkey,
428 .encrypt = ctr_crypt,
429 .decrypt = ctr_crypt,
433 .cra_name = "__lrw-serpent-avx",
434 .cra_driver_name = "__driver-lrw-serpent-avx",
436 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
438 .cra_blocksize = SERPENT_BLOCK_SIZE,
439 .cra_ctxsize = sizeof(struct serpent_lrw_ctx),
441 .cra_type = &crypto_blkcipher_type,
442 .cra_module = THIS_MODULE,
443 .cra_exit = lrw_serpent_exit_tfm,
446 .min_keysize = SERPENT_MIN_KEY_SIZE +
448 .max_keysize = SERPENT_MAX_KEY_SIZE +
450 .ivsize = SERPENT_BLOCK_SIZE,
451 .setkey = lrw_serpent_setkey,
452 .encrypt = lrw_encrypt,
453 .decrypt = lrw_decrypt,
457 .cra_name = "__xts-serpent-avx",
458 .cra_driver_name = "__driver-xts-serpent-avx",
460 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
462 .cra_blocksize = SERPENT_BLOCK_SIZE,
463 .cra_ctxsize = sizeof(struct serpent_xts_ctx),
465 .cra_type = &crypto_blkcipher_type,
466 .cra_module = THIS_MODULE,
469 .min_keysize = SERPENT_MIN_KEY_SIZE * 2,
470 .max_keysize = SERPENT_MAX_KEY_SIZE * 2,
471 .ivsize = SERPENT_BLOCK_SIZE,
472 .setkey = xts_serpent_setkey,
473 .encrypt = xts_encrypt,
474 .decrypt = xts_decrypt,
478 .cra_name = "ecb(serpent)",
479 .cra_driver_name = "ecb-serpent-avx",
481 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
482 .cra_blocksize = SERPENT_BLOCK_SIZE,
483 .cra_ctxsize = sizeof(struct async_helper_ctx),
485 .cra_type = &crypto_ablkcipher_type,
486 .cra_module = THIS_MODULE,
487 .cra_init = ablk_init,
488 .cra_exit = ablk_exit,
491 .min_keysize = SERPENT_MIN_KEY_SIZE,
492 .max_keysize = SERPENT_MAX_KEY_SIZE,
493 .setkey = ablk_set_key,
494 .encrypt = ablk_encrypt,
495 .decrypt = ablk_decrypt,
499 .cra_name = "cbc(serpent)",
500 .cra_driver_name = "cbc-serpent-avx",
502 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
503 .cra_blocksize = SERPENT_BLOCK_SIZE,
504 .cra_ctxsize = sizeof(struct async_helper_ctx),
506 .cra_type = &crypto_ablkcipher_type,
507 .cra_module = THIS_MODULE,
508 .cra_init = ablk_init,
509 .cra_exit = ablk_exit,
512 .min_keysize = SERPENT_MIN_KEY_SIZE,
513 .max_keysize = SERPENT_MAX_KEY_SIZE,
514 .ivsize = SERPENT_BLOCK_SIZE,
515 .setkey = ablk_set_key,
516 .encrypt = __ablk_encrypt,
517 .decrypt = ablk_decrypt,
521 .cra_name = "ctr(serpent)",
522 .cra_driver_name = "ctr-serpent-avx",
524 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
526 .cra_ctxsize = sizeof(struct async_helper_ctx),
528 .cra_type = &crypto_ablkcipher_type,
529 .cra_module = THIS_MODULE,
530 .cra_init = ablk_init,
531 .cra_exit = ablk_exit,
534 .min_keysize = SERPENT_MIN_KEY_SIZE,
535 .max_keysize = SERPENT_MAX_KEY_SIZE,
536 .ivsize = SERPENT_BLOCK_SIZE,
537 .setkey = ablk_set_key,
538 .encrypt = ablk_encrypt,
539 .decrypt = ablk_encrypt,
544 .cra_name = "lrw(serpent)",
545 .cra_driver_name = "lrw-serpent-avx",
547 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
548 .cra_blocksize = SERPENT_BLOCK_SIZE,
549 .cra_ctxsize = sizeof(struct async_helper_ctx),
551 .cra_type = &crypto_ablkcipher_type,
552 .cra_module = THIS_MODULE,
553 .cra_init = ablk_init,
554 .cra_exit = ablk_exit,
557 .min_keysize = SERPENT_MIN_KEY_SIZE +
559 .max_keysize = SERPENT_MAX_KEY_SIZE +
561 .ivsize = SERPENT_BLOCK_SIZE,
562 .setkey = ablk_set_key,
563 .encrypt = ablk_encrypt,
564 .decrypt = ablk_decrypt,
568 .cra_name = "xts(serpent)",
569 .cra_driver_name = "xts-serpent-avx",
571 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
572 .cra_blocksize = SERPENT_BLOCK_SIZE,
573 .cra_ctxsize = sizeof(struct async_helper_ctx),
575 .cra_type = &crypto_ablkcipher_type,
576 .cra_module = THIS_MODULE,
577 .cra_init = ablk_init,
578 .cra_exit = ablk_exit,
581 .min_keysize = SERPENT_MIN_KEY_SIZE * 2,
582 .max_keysize = SERPENT_MAX_KEY_SIZE * 2,
583 .ivsize = SERPENT_BLOCK_SIZE,
584 .setkey = ablk_set_key,
585 .encrypt = ablk_encrypt,
586 .decrypt = ablk_decrypt,
591 static int __init serpent_init(void)
593 const char *feature_name;
595 if (!cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM,
597 pr_info("CPU feature '%s' is not supported.\n", feature_name);
601 return crypto_register_algs(serpent_algs, ARRAY_SIZE(serpent_algs));
604 static void __exit serpent_exit(void)
606 crypto_unregister_algs(serpent_algs, ARRAY_SIZE(serpent_algs));
609 module_init(serpent_init);
610 module_exit(serpent_exit);
612 MODULE_DESCRIPTION("Serpent Cipher Algorithm, AVX optimized");
613 MODULE_LICENSE("GPL");
614 MODULE_ALIAS_CRYPTO("serpent");