xref: /linux/arch/x86/crypto/cast5_avx_glue.c (revision e7e86d7697c6ed1dbbde18d7185c35b6967945ed)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Glue Code for the AVX assembler implementation of the Cast5 Cipher
4  *
5  * Copyright (C) 2012 Johannes Goetzfried
6  *     <Johannes.Goetzfried@informatik.stud.uni-erlangen.de>
7  */
8 
9 #include <crypto/algapi.h>
10 #include <crypto/cast5.h>
11 #include <linux/crypto.h>
12 #include <linux/err.h>
13 #include <linux/module.h>
14 #include <linux/types.h>
15 
16 #include "ecb_cbc_helpers.h"
17 
18 #define CAST5_PARALLEL_BLOCKS 16
19 
20 asmlinkage void cast5_ecb_enc_16way(struct cast5_ctx *ctx, u8 *dst,
21 				    const u8 *src);
22 asmlinkage void cast5_ecb_dec_16way(struct cast5_ctx *ctx, u8 *dst,
23 				    const u8 *src);
24 asmlinkage void cast5_cbc_dec_16way(struct cast5_ctx *ctx, u8 *dst,
25 				    const u8 *src);
26 
27 static int cast5_setkey_skcipher(struct crypto_skcipher *tfm, const u8 *key,
28 				 unsigned int keylen)
29 {
30 	return cast5_setkey(&tfm->base, key, keylen);
31 }
32 
33 static int ecb_encrypt(struct skcipher_request *req)
34 {
35 	ECB_WALK_START(req, CAST5_BLOCK_SIZE, CAST5_PARALLEL_BLOCKS);
36 	ECB_BLOCK(CAST5_PARALLEL_BLOCKS, cast5_ecb_enc_16way);
37 	ECB_BLOCK(1, __cast5_encrypt);
38 	ECB_WALK_END();
39 }
40 
41 static int ecb_decrypt(struct skcipher_request *req)
42 {
43 	ECB_WALK_START(req, CAST5_BLOCK_SIZE, CAST5_PARALLEL_BLOCKS);
44 	ECB_BLOCK(CAST5_PARALLEL_BLOCKS, cast5_ecb_dec_16way);
45 	ECB_BLOCK(1, __cast5_decrypt);
46 	ECB_WALK_END();
47 }
48 
49 static int cbc_encrypt(struct skcipher_request *req)
50 {
51 	CBC_WALK_START(req, CAST5_BLOCK_SIZE, -1);
52 	CBC_ENC_BLOCK(__cast5_encrypt);
53 	CBC_WALK_END();
54 }
55 
56 static int cbc_decrypt(struct skcipher_request *req)
57 {
58 	CBC_WALK_START(req, CAST5_BLOCK_SIZE, CAST5_PARALLEL_BLOCKS);
59 	CBC_DEC_BLOCK(CAST5_PARALLEL_BLOCKS, cast5_cbc_dec_16way);
60 	CBC_DEC_BLOCK(1, __cast5_decrypt);
61 	CBC_WALK_END();
62 }
63 
64 static struct skcipher_alg cast5_algs[] = {
65 	{
66 		.base.cra_name		= "ecb(cast5)",
67 		.base.cra_driver_name	= "ecb-cast5-avx",
68 		.base.cra_priority	= 200,
69 		.base.cra_blocksize	= CAST5_BLOCK_SIZE,
70 		.base.cra_ctxsize	= sizeof(struct cast5_ctx),
71 		.base.cra_module	= THIS_MODULE,
72 		.min_keysize		= CAST5_MIN_KEY_SIZE,
73 		.max_keysize		= CAST5_MAX_KEY_SIZE,
74 		.setkey			= cast5_setkey_skcipher,
75 		.encrypt		= ecb_encrypt,
76 		.decrypt		= ecb_decrypt,
77 	}, {
78 		.base.cra_name		= "cbc(cast5)",
79 		.base.cra_driver_name	= "cbc-cast5-avx",
80 		.base.cra_priority	= 200,
81 		.base.cra_blocksize	= CAST5_BLOCK_SIZE,
82 		.base.cra_ctxsize	= sizeof(struct cast5_ctx),
83 		.base.cra_module	= THIS_MODULE,
84 		.min_keysize		= CAST5_MIN_KEY_SIZE,
85 		.max_keysize		= CAST5_MAX_KEY_SIZE,
86 		.ivsize			= CAST5_BLOCK_SIZE,
87 		.setkey			= cast5_setkey_skcipher,
88 		.encrypt		= cbc_encrypt,
89 		.decrypt		= cbc_decrypt,
90 	}
91 };
92 
93 static int __init cast5_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(cast5_algs,
104 					 ARRAY_SIZE(cast5_algs));
105 }
106 
107 static void __exit cast5_exit(void)
108 {
109 	crypto_unregister_skciphers(cast5_algs, ARRAY_SIZE(cast5_algs));
110 }
111 
112 module_init(cast5_init);
113 module_exit(cast5_exit);
114 
115 MODULE_DESCRIPTION("Cast5 Cipher Algorithm, AVX optimized");
116 MODULE_LICENSE("GPL");
117 MODULE_ALIAS_CRYPTO("cast5");
118