xref: /linux/arch/x86/crypto/cast6_avx_glue.c (revision e78f70bad29c5ae1e1076698b690b15794e9b81e)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Glue Code for the AVX assembler implementation of the Cast6 Cipher
4  *
5  * Copyright (C) 2012 Johannes Goetzfried
6  *     <Johannes.Goetzfried@informatik.stud.uni-erlangen.de>
7  *
8  * Copyright © 2013 Jussi Kivilinna <jussi.kivilinna@iki.fi>
9  */
10 
11 #include <linux/module.h>
12 #include <linux/types.h>
13 #include <linux/crypto.h>
14 #include <linux/err.h>
15 #include <crypto/algapi.h>
16 #include <crypto/cast6.h>
17 
18 #include "ecb_cbc_helpers.h"
19 
20 #define CAST6_PARALLEL_BLOCKS 8
21 
22 asmlinkage void cast6_ecb_enc_8way(const void *ctx, u8 *dst, const u8 *src);
23 asmlinkage void cast6_ecb_dec_8way(const void *ctx, u8 *dst, const u8 *src);
24 
25 asmlinkage void cast6_cbc_dec_8way(const void *ctx, u8 *dst, const u8 *src);
26 
27 static int cast6_setkey_skcipher(struct crypto_skcipher *tfm,
28 				 const u8 *key, unsigned int keylen)
29 {
30 	return cast6_setkey(&tfm->base, key, keylen);
31 }
32 
33 static int ecb_encrypt(struct skcipher_request *req)
34 {
35 	ECB_WALK_START(req, CAST6_BLOCK_SIZE, CAST6_PARALLEL_BLOCKS);
36 	ECB_BLOCK(CAST6_PARALLEL_BLOCKS, cast6_ecb_enc_8way);
37 	ECB_BLOCK(1, __cast6_encrypt);
38 	ECB_WALK_END();
39 }
40 
41 static int ecb_decrypt(struct skcipher_request *req)
42 {
43 	ECB_WALK_START(req, CAST6_BLOCK_SIZE, CAST6_PARALLEL_BLOCKS);
44 	ECB_BLOCK(CAST6_PARALLEL_BLOCKS, cast6_ecb_dec_8way);
45 	ECB_BLOCK(1, __cast6_decrypt);
46 	ECB_WALK_END();
47 }
48 
49 static int cbc_encrypt(struct skcipher_request *req)
50 {
51 	CBC_WALK_START(req, CAST6_BLOCK_SIZE, -1);
52 	CBC_ENC_BLOCK(__cast6_encrypt);
53 	CBC_WALK_END();
54 }
55 
56 static int cbc_decrypt(struct skcipher_request *req)
57 {
58 	CBC_WALK_START(req, CAST6_BLOCK_SIZE, CAST6_PARALLEL_BLOCKS);
59 	CBC_DEC_BLOCK(CAST6_PARALLEL_BLOCKS, cast6_cbc_dec_8way);
60 	CBC_DEC_BLOCK(1, __cast6_decrypt);
61 	CBC_WALK_END();
62 }
63 
64 static struct skcipher_alg cast6_algs[] = {
65 	{
66 		.base.cra_name		= "ecb(cast6)",
67 		.base.cra_driver_name	= "ecb-cast6-avx",
68 		.base.cra_priority	= 200,
69 		.base.cra_blocksize	= CAST6_BLOCK_SIZE,
70 		.base.cra_ctxsize	= sizeof(struct cast6_ctx),
71 		.base.cra_module	= THIS_MODULE,
72 		.min_keysize		= CAST6_MIN_KEY_SIZE,
73 		.max_keysize		= CAST6_MAX_KEY_SIZE,
74 		.setkey			= cast6_setkey_skcipher,
75 		.encrypt		= ecb_encrypt,
76 		.decrypt		= ecb_decrypt,
77 	}, {
78 		.base.cra_name		= "cbc(cast6)",
79 		.base.cra_driver_name	= "cbc-cast6-avx",
80 		.base.cra_priority	= 200,
81 		.base.cra_blocksize	= CAST6_BLOCK_SIZE,
82 		.base.cra_ctxsize	= sizeof(struct cast6_ctx),
83 		.base.cra_module	= THIS_MODULE,
84 		.min_keysize		= CAST6_MIN_KEY_SIZE,
85 		.max_keysize		= CAST6_MAX_KEY_SIZE,
86 		.ivsize			= CAST6_BLOCK_SIZE,
87 		.setkey			= cast6_setkey_skcipher,
88 		.encrypt		= cbc_encrypt,
89 		.decrypt		= cbc_decrypt,
90 	},
91 };
92 
93 static int __init cast6_init(void)
94 {
95 	const char *feature_name;
96 
97 	if (!cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM,
98 				&feature_name)) {
99 		pr_info("CPU feature '%s' is not supported.\n", feature_name);
100 		return -ENODEV;
101 	}
102 
103 	return crypto_register_skciphers(cast6_algs, ARRAY_SIZE(cast6_algs));
104 }
105 
106 static void __exit cast6_exit(void)
107 {
108 	crypto_unregister_skciphers(cast6_algs, ARRAY_SIZE(cast6_algs));
109 }
110 
111 module_init(cast6_init);
112 module_exit(cast6_exit);
113 
114 MODULE_DESCRIPTION("Cast6 Cipher Algorithm, AVX optimized");
115 MODULE_LICENSE("GPL");
116 MODULE_ALIAS_CRYPTO("cast6");
117