2 * Glue Code for the AVX assembler implemention of the Cast6 Cipher
4 * Copyright (C) 2012 Johannes Goetzfried
5 * <Johannes.Goetzfried@informatik.stud.uni-erlangen.de>
7 * Copyright © 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/cast6.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/glue_helper.h>
42 #define CAST6_PARALLEL_BLOCKS 8
44 asmlinkage void cast6_ecb_enc_8way(struct cast6_ctx *ctx, u8 *dst,
46 asmlinkage void cast6_ecb_dec_8way(struct cast6_ctx *ctx, u8 *dst,
49 asmlinkage void cast6_cbc_dec_8way(struct cast6_ctx *ctx, u8 *dst,
51 asmlinkage void cast6_ctr_8way(struct cast6_ctx *ctx, u8 *dst, const u8 *src,
54 asmlinkage void cast6_xts_enc_8way(struct cast6_ctx *ctx, u8 *dst,
55 const u8 *src, le128 *iv);
56 asmlinkage void cast6_xts_dec_8way(struct cast6_ctx *ctx, u8 *dst,
57 const u8 *src, le128 *iv);
59 static void cast6_xts_enc(void *ctx, u128 *dst, const u128 *src, le128 *iv)
61 glue_xts_crypt_128bit_one(ctx, dst, src, iv,
62 GLUE_FUNC_CAST(__cast6_encrypt));
65 static void cast6_xts_dec(void *ctx, u128 *dst, const u128 *src, le128 *iv)
67 glue_xts_crypt_128bit_one(ctx, dst, src, iv,
68 GLUE_FUNC_CAST(__cast6_decrypt));
71 static void cast6_crypt_ctr(void *ctx, u128 *dst, const u128 *src, le128 *iv)
75 le128_to_be128(&ctrblk, iv);
78 __cast6_encrypt(ctx, (u8 *)&ctrblk, (u8 *)&ctrblk);
79 u128_xor(dst, src, (u128 *)&ctrblk);
82 static const struct common_glue_ctx cast6_enc = {
84 .fpu_blocks_limit = CAST6_PARALLEL_BLOCKS,
87 .num_blocks = CAST6_PARALLEL_BLOCKS,
88 .fn_u = { .ecb = GLUE_FUNC_CAST(cast6_ecb_enc_8way) }
91 .fn_u = { .ecb = GLUE_FUNC_CAST(__cast6_encrypt) }
95 static const struct common_glue_ctx cast6_ctr = {
97 .fpu_blocks_limit = CAST6_PARALLEL_BLOCKS,
100 .num_blocks = CAST6_PARALLEL_BLOCKS,
101 .fn_u = { .ctr = GLUE_CTR_FUNC_CAST(cast6_ctr_8way) }
104 .fn_u = { .ctr = GLUE_CTR_FUNC_CAST(cast6_crypt_ctr) }
108 static const struct common_glue_ctx cast6_enc_xts = {
110 .fpu_blocks_limit = CAST6_PARALLEL_BLOCKS,
113 .num_blocks = CAST6_PARALLEL_BLOCKS,
114 .fn_u = { .xts = GLUE_XTS_FUNC_CAST(cast6_xts_enc_8way) }
117 .fn_u = { .xts = GLUE_XTS_FUNC_CAST(cast6_xts_enc) }
121 static const struct common_glue_ctx cast6_dec = {
123 .fpu_blocks_limit = CAST6_PARALLEL_BLOCKS,
126 .num_blocks = CAST6_PARALLEL_BLOCKS,
127 .fn_u = { .ecb = GLUE_FUNC_CAST(cast6_ecb_dec_8way) }
130 .fn_u = { .ecb = GLUE_FUNC_CAST(__cast6_decrypt) }
134 static const struct common_glue_ctx cast6_dec_cbc = {
136 .fpu_blocks_limit = CAST6_PARALLEL_BLOCKS,
139 .num_blocks = CAST6_PARALLEL_BLOCKS,
140 .fn_u = { .cbc = GLUE_CBC_FUNC_CAST(cast6_cbc_dec_8way) }
143 .fn_u = { .cbc = GLUE_CBC_FUNC_CAST(__cast6_decrypt) }
147 static const struct common_glue_ctx cast6_dec_xts = {
149 .fpu_blocks_limit = CAST6_PARALLEL_BLOCKS,
152 .num_blocks = CAST6_PARALLEL_BLOCKS,
153 .fn_u = { .xts = GLUE_XTS_FUNC_CAST(cast6_xts_dec_8way) }
156 .fn_u = { .xts = GLUE_XTS_FUNC_CAST(cast6_xts_dec) }
160 static int ecb_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
161 struct scatterlist *src, unsigned int nbytes)
163 return glue_ecb_crypt_128bit(&cast6_enc, desc, dst, src, nbytes);
166 static int ecb_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
167 struct scatterlist *src, unsigned int nbytes)
169 return glue_ecb_crypt_128bit(&cast6_dec, desc, dst, src, nbytes);
172 static int cbc_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
173 struct scatterlist *src, unsigned int nbytes)
175 return glue_cbc_encrypt_128bit(GLUE_FUNC_CAST(__cast6_encrypt), desc,
179 static int cbc_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
180 struct scatterlist *src, unsigned int nbytes)
182 return glue_cbc_decrypt_128bit(&cast6_dec_cbc, desc, dst, src,
186 static int ctr_crypt(struct blkcipher_desc *desc, struct scatterlist *dst,
187 struct scatterlist *src, unsigned int nbytes)
189 return glue_ctr_crypt_128bit(&cast6_ctr, desc, dst, src, nbytes);
192 static inline bool cast6_fpu_begin(bool fpu_enabled, unsigned int nbytes)
194 return glue_fpu_begin(CAST6_BLOCK_SIZE, CAST6_PARALLEL_BLOCKS,
195 NULL, fpu_enabled, nbytes);
198 static inline void cast6_fpu_end(bool fpu_enabled)
200 glue_fpu_end(fpu_enabled);
204 struct cast6_ctx *ctx;
208 static void encrypt_callback(void *priv, u8 *srcdst, unsigned int nbytes)
210 const unsigned int bsize = CAST6_BLOCK_SIZE;
211 struct crypt_priv *ctx = priv;
214 ctx->fpu_enabled = cast6_fpu_begin(ctx->fpu_enabled, nbytes);
216 if (nbytes == bsize * CAST6_PARALLEL_BLOCKS) {
217 cast6_ecb_enc_8way(ctx->ctx, srcdst, srcdst);
221 for (i = 0; i < nbytes / bsize; i++, srcdst += bsize)
222 __cast6_encrypt(ctx->ctx, srcdst, srcdst);
225 static void decrypt_callback(void *priv, u8 *srcdst, unsigned int nbytes)
227 const unsigned int bsize = CAST6_BLOCK_SIZE;
228 struct crypt_priv *ctx = priv;
231 ctx->fpu_enabled = cast6_fpu_begin(ctx->fpu_enabled, nbytes);
233 if (nbytes == bsize * CAST6_PARALLEL_BLOCKS) {
234 cast6_ecb_dec_8way(ctx->ctx, srcdst, srcdst);
238 for (i = 0; i < nbytes / bsize; i++, srcdst += bsize)
239 __cast6_decrypt(ctx->ctx, srcdst, srcdst);
242 struct cast6_lrw_ctx {
243 struct lrw_table_ctx lrw_table;
244 struct cast6_ctx cast6_ctx;
247 static int lrw_cast6_setkey(struct crypto_tfm *tfm, const u8 *key,
250 struct cast6_lrw_ctx *ctx = crypto_tfm_ctx(tfm);
253 err = __cast6_setkey(&ctx->cast6_ctx, key, keylen - CAST6_BLOCK_SIZE,
258 return lrw_init_table(&ctx->lrw_table, key + keylen - CAST6_BLOCK_SIZE);
261 static int lrw_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
262 struct scatterlist *src, unsigned int nbytes)
264 struct cast6_lrw_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
265 be128 buf[CAST6_PARALLEL_BLOCKS];
266 struct crypt_priv crypt_ctx = {
267 .ctx = &ctx->cast6_ctx,
268 .fpu_enabled = false,
270 struct lrw_crypt_req req = {
272 .tbuflen = sizeof(buf),
274 .table_ctx = &ctx->lrw_table,
275 .crypt_ctx = &crypt_ctx,
276 .crypt_fn = encrypt_callback,
280 desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
281 ret = lrw_crypt(desc, dst, src, nbytes, &req);
282 cast6_fpu_end(crypt_ctx.fpu_enabled);
287 static int lrw_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
288 struct scatterlist *src, unsigned int nbytes)
290 struct cast6_lrw_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
291 be128 buf[CAST6_PARALLEL_BLOCKS];
292 struct crypt_priv crypt_ctx = {
293 .ctx = &ctx->cast6_ctx,
294 .fpu_enabled = false,
296 struct lrw_crypt_req req = {
298 .tbuflen = sizeof(buf),
300 .table_ctx = &ctx->lrw_table,
301 .crypt_ctx = &crypt_ctx,
302 .crypt_fn = decrypt_callback,
306 desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
307 ret = lrw_crypt(desc, dst, src, nbytes, &req);
308 cast6_fpu_end(crypt_ctx.fpu_enabled);
313 static void lrw_exit_tfm(struct crypto_tfm *tfm)
315 struct cast6_lrw_ctx *ctx = crypto_tfm_ctx(tfm);
317 lrw_free_table(&ctx->lrw_table);
320 struct cast6_xts_ctx {
321 struct cast6_ctx tweak_ctx;
322 struct cast6_ctx crypt_ctx;
325 static int xts_cast6_setkey(struct crypto_tfm *tfm, const u8 *key,
328 struct cast6_xts_ctx *ctx = crypto_tfm_ctx(tfm);
329 u32 *flags = &tfm->crt_flags;
332 err = xts_check_key(tfm, key, keylen);
336 /* first half of xts-key is for crypt */
337 err = __cast6_setkey(&ctx->crypt_ctx, key, keylen / 2, flags);
341 /* second half of xts-key is for tweak */
342 return __cast6_setkey(&ctx->tweak_ctx, key + keylen / 2, keylen / 2,
346 static int xts_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
347 struct scatterlist *src, unsigned int nbytes)
349 struct cast6_xts_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
351 return glue_xts_crypt_128bit(&cast6_enc_xts, desc, dst, src, nbytes,
352 XTS_TWEAK_CAST(__cast6_encrypt),
353 &ctx->tweak_ctx, &ctx->crypt_ctx);
356 static int xts_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
357 struct scatterlist *src, unsigned int nbytes)
359 struct cast6_xts_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
361 return glue_xts_crypt_128bit(&cast6_dec_xts, desc, dst, src, nbytes,
362 XTS_TWEAK_CAST(__cast6_encrypt),
363 &ctx->tweak_ctx, &ctx->crypt_ctx);
366 static struct crypto_alg cast6_algs[10] = { {
367 .cra_name = "__ecb-cast6-avx",
368 .cra_driver_name = "__driver-ecb-cast6-avx",
370 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
372 .cra_blocksize = CAST6_BLOCK_SIZE,
373 .cra_ctxsize = sizeof(struct cast6_ctx),
375 .cra_type = &crypto_blkcipher_type,
376 .cra_module = THIS_MODULE,
379 .min_keysize = CAST6_MIN_KEY_SIZE,
380 .max_keysize = CAST6_MAX_KEY_SIZE,
381 .setkey = cast6_setkey,
382 .encrypt = ecb_encrypt,
383 .decrypt = ecb_decrypt,
387 .cra_name = "__cbc-cast6-avx",
388 .cra_driver_name = "__driver-cbc-cast6-avx",
390 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
392 .cra_blocksize = CAST6_BLOCK_SIZE,
393 .cra_ctxsize = sizeof(struct cast6_ctx),
395 .cra_type = &crypto_blkcipher_type,
396 .cra_module = THIS_MODULE,
399 .min_keysize = CAST6_MIN_KEY_SIZE,
400 .max_keysize = CAST6_MAX_KEY_SIZE,
401 .setkey = cast6_setkey,
402 .encrypt = cbc_encrypt,
403 .decrypt = cbc_decrypt,
407 .cra_name = "__ctr-cast6-avx",
408 .cra_driver_name = "__driver-ctr-cast6-avx",
410 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
413 .cra_ctxsize = sizeof(struct cast6_ctx),
415 .cra_type = &crypto_blkcipher_type,
416 .cra_module = THIS_MODULE,
419 .min_keysize = CAST6_MIN_KEY_SIZE,
420 .max_keysize = CAST6_MAX_KEY_SIZE,
421 .ivsize = CAST6_BLOCK_SIZE,
422 .setkey = cast6_setkey,
423 .encrypt = ctr_crypt,
424 .decrypt = ctr_crypt,
428 .cra_name = "__lrw-cast6-avx",
429 .cra_driver_name = "__driver-lrw-cast6-avx",
431 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
433 .cra_blocksize = CAST6_BLOCK_SIZE,
434 .cra_ctxsize = sizeof(struct cast6_lrw_ctx),
436 .cra_type = &crypto_blkcipher_type,
437 .cra_module = THIS_MODULE,
438 .cra_exit = lrw_exit_tfm,
441 .min_keysize = CAST6_MIN_KEY_SIZE +
443 .max_keysize = CAST6_MAX_KEY_SIZE +
445 .ivsize = CAST6_BLOCK_SIZE,
446 .setkey = lrw_cast6_setkey,
447 .encrypt = lrw_encrypt,
448 .decrypt = lrw_decrypt,
452 .cra_name = "__xts-cast6-avx",
453 .cra_driver_name = "__driver-xts-cast6-avx",
455 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
457 .cra_blocksize = CAST6_BLOCK_SIZE,
458 .cra_ctxsize = sizeof(struct cast6_xts_ctx),
460 .cra_type = &crypto_blkcipher_type,
461 .cra_module = THIS_MODULE,
464 .min_keysize = CAST6_MIN_KEY_SIZE * 2,
465 .max_keysize = CAST6_MAX_KEY_SIZE * 2,
466 .ivsize = CAST6_BLOCK_SIZE,
467 .setkey = xts_cast6_setkey,
468 .encrypt = xts_encrypt,
469 .decrypt = xts_decrypt,
473 .cra_name = "ecb(cast6)",
474 .cra_driver_name = "ecb-cast6-avx",
476 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
477 .cra_blocksize = CAST6_BLOCK_SIZE,
478 .cra_ctxsize = sizeof(struct async_helper_ctx),
480 .cra_type = &crypto_ablkcipher_type,
481 .cra_module = THIS_MODULE,
482 .cra_init = ablk_init,
483 .cra_exit = ablk_exit,
486 .min_keysize = CAST6_MIN_KEY_SIZE,
487 .max_keysize = CAST6_MAX_KEY_SIZE,
488 .setkey = ablk_set_key,
489 .encrypt = ablk_encrypt,
490 .decrypt = ablk_decrypt,
494 .cra_name = "cbc(cast6)",
495 .cra_driver_name = "cbc-cast6-avx",
497 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
498 .cra_blocksize = CAST6_BLOCK_SIZE,
499 .cra_ctxsize = sizeof(struct async_helper_ctx),
501 .cra_type = &crypto_ablkcipher_type,
502 .cra_module = THIS_MODULE,
503 .cra_init = ablk_init,
504 .cra_exit = ablk_exit,
507 .min_keysize = CAST6_MIN_KEY_SIZE,
508 .max_keysize = CAST6_MAX_KEY_SIZE,
509 .ivsize = CAST6_BLOCK_SIZE,
510 .setkey = ablk_set_key,
511 .encrypt = __ablk_encrypt,
512 .decrypt = ablk_decrypt,
516 .cra_name = "ctr(cast6)",
517 .cra_driver_name = "ctr-cast6-avx",
519 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
521 .cra_ctxsize = sizeof(struct async_helper_ctx),
523 .cra_type = &crypto_ablkcipher_type,
524 .cra_module = THIS_MODULE,
525 .cra_init = ablk_init,
526 .cra_exit = ablk_exit,
529 .min_keysize = CAST6_MIN_KEY_SIZE,
530 .max_keysize = CAST6_MAX_KEY_SIZE,
531 .ivsize = CAST6_BLOCK_SIZE,
532 .setkey = ablk_set_key,
533 .encrypt = ablk_encrypt,
534 .decrypt = ablk_encrypt,
539 .cra_name = "lrw(cast6)",
540 .cra_driver_name = "lrw-cast6-avx",
542 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
543 .cra_blocksize = CAST6_BLOCK_SIZE,
544 .cra_ctxsize = sizeof(struct async_helper_ctx),
546 .cra_type = &crypto_ablkcipher_type,
547 .cra_module = THIS_MODULE,
548 .cra_init = ablk_init,
549 .cra_exit = ablk_exit,
552 .min_keysize = CAST6_MIN_KEY_SIZE +
554 .max_keysize = CAST6_MAX_KEY_SIZE +
556 .ivsize = CAST6_BLOCK_SIZE,
557 .setkey = ablk_set_key,
558 .encrypt = ablk_encrypt,
559 .decrypt = ablk_decrypt,
563 .cra_name = "xts(cast6)",
564 .cra_driver_name = "xts-cast6-avx",
566 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
567 .cra_blocksize = CAST6_BLOCK_SIZE,
568 .cra_ctxsize = sizeof(struct async_helper_ctx),
570 .cra_type = &crypto_ablkcipher_type,
571 .cra_module = THIS_MODULE,
572 .cra_init = ablk_init,
573 .cra_exit = ablk_exit,
576 .min_keysize = CAST6_MIN_KEY_SIZE * 2,
577 .max_keysize = CAST6_MAX_KEY_SIZE * 2,
578 .ivsize = CAST6_BLOCK_SIZE,
579 .setkey = ablk_set_key,
580 .encrypt = ablk_encrypt,
581 .decrypt = ablk_decrypt,
586 static int __init cast6_init(void)
588 const char *feature_name;
590 if (!cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM,
592 pr_info("CPU feature '%s' is not supported.\n", feature_name);
596 return crypto_register_algs(cast6_algs, ARRAY_SIZE(cast6_algs));
599 static void __exit cast6_exit(void)
601 crypto_unregister_algs(cast6_algs, ARRAY_SIZE(cast6_algs));
604 module_init(cast6_init);
605 module_exit(cast6_exit);
607 MODULE_DESCRIPTION("Cast6 Cipher Algorithm, AVX optimized");
608 MODULE_LICENSE("GPL");
609 MODULE_ALIAS_CRYPTO("cast6");