xref: /openbmc/linux/arch/x86/crypto/blowfish_glue.c (revision 26b32974)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Glue Code for assembler optimized version of Blowfish
4  *
5  * Copyright (c) 2011 Jussi Kivilinna <jussi.kivilinna@mbnet.fi>
6  *
7  * CBC & ECB parts based on code (crypto/cbc.c,ecb.c) by:
8  *   Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
9  */
10 
11 #include <crypto/algapi.h>
12 #include <crypto/blowfish.h>
13 #include <crypto/internal/skcipher.h>
14 #include <linux/crypto.h>
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/types.h>
18 
19 #include "ecb_cbc_helpers.h"
20 
21 /* regular block cipher functions */
22 asmlinkage void blowfish_enc_blk(struct bf_ctx *ctx, u8 *dst, const u8 *src);
23 asmlinkage void blowfish_dec_blk(struct bf_ctx *ctx, u8 *dst, const u8 *src);
24 
25 /* 4-way parallel cipher functions */
26 asmlinkage void blowfish_enc_blk_4way(struct bf_ctx *ctx, u8 *dst,
27 				      const u8 *src);
28 asmlinkage void __blowfish_dec_blk_4way(struct bf_ctx *ctx, u8 *dst,
29 					const u8 *src, bool cbc);
30 
31 static inline void blowfish_dec_ecb_4way(struct bf_ctx *ctx, u8 *dst,
32 					     const u8 *src)
33 {
34 	return __blowfish_dec_blk_4way(ctx, dst, src, false);
35 }
36 
37 static inline void blowfish_dec_cbc_4way(struct bf_ctx *ctx, u8 *dst,
38 					     const u8 *src)
39 {
40 	return __blowfish_dec_blk_4way(ctx, dst, src, true);
41 }
42 
43 static void blowfish_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
44 {
45 	blowfish_enc_blk(crypto_tfm_ctx(tfm), dst, src);
46 }
47 
48 static void blowfish_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
49 {
50 	blowfish_dec_blk(crypto_tfm_ctx(tfm), dst, src);
51 }
52 
53 static int blowfish_setkey_skcipher(struct crypto_skcipher *tfm,
54 				    const u8 *key, unsigned int keylen)
55 {
56 	return blowfish_setkey(&tfm->base, key, keylen);
57 }
58 
59 static int ecb_encrypt(struct skcipher_request *req)
60 {
61 	ECB_WALK_START(req, BF_BLOCK_SIZE, -1);
62 	ECB_BLOCK(4, blowfish_enc_blk_4way);
63 	ECB_BLOCK(1, blowfish_enc_blk);
64 	ECB_WALK_END();
65 }
66 
67 static int ecb_decrypt(struct skcipher_request *req)
68 {
69 	ECB_WALK_START(req, BF_BLOCK_SIZE, -1);
70 	ECB_BLOCK(4, blowfish_dec_ecb_4way);
71 	ECB_BLOCK(1, blowfish_dec_blk);
72 	ECB_WALK_END();
73 }
74 
75 static int cbc_encrypt(struct skcipher_request *req)
76 {
77 	CBC_WALK_START(req, BF_BLOCK_SIZE, -1);
78 	CBC_ENC_BLOCK(blowfish_enc_blk);
79 	CBC_WALK_END();
80 }
81 
82 static int cbc_decrypt(struct skcipher_request *req)
83 {
84 	CBC_WALK_START(req, BF_BLOCK_SIZE, -1);
85 	CBC_DEC_BLOCK(4, blowfish_dec_cbc_4way);
86 	CBC_DEC_BLOCK(1, blowfish_dec_blk);
87 	CBC_WALK_END();
88 }
89 
90 static struct crypto_alg bf_cipher_alg = {
91 	.cra_name		= "blowfish",
92 	.cra_driver_name	= "blowfish-asm",
93 	.cra_priority		= 200,
94 	.cra_flags		= CRYPTO_ALG_TYPE_CIPHER,
95 	.cra_blocksize		= BF_BLOCK_SIZE,
96 	.cra_ctxsize		= sizeof(struct bf_ctx),
97 	.cra_alignmask		= 0,
98 	.cra_module		= THIS_MODULE,
99 	.cra_u = {
100 		.cipher = {
101 			.cia_min_keysize	= BF_MIN_KEY_SIZE,
102 			.cia_max_keysize	= BF_MAX_KEY_SIZE,
103 			.cia_setkey		= blowfish_setkey,
104 			.cia_encrypt		= blowfish_encrypt,
105 			.cia_decrypt		= blowfish_decrypt,
106 		}
107 	}
108 };
109 
110 static struct skcipher_alg bf_skcipher_algs[] = {
111 	{
112 		.base.cra_name		= "ecb(blowfish)",
113 		.base.cra_driver_name	= "ecb-blowfish-asm",
114 		.base.cra_priority	= 300,
115 		.base.cra_blocksize	= BF_BLOCK_SIZE,
116 		.base.cra_ctxsize	= sizeof(struct bf_ctx),
117 		.base.cra_module	= THIS_MODULE,
118 		.min_keysize		= BF_MIN_KEY_SIZE,
119 		.max_keysize		= BF_MAX_KEY_SIZE,
120 		.setkey			= blowfish_setkey_skcipher,
121 		.encrypt		= ecb_encrypt,
122 		.decrypt		= ecb_decrypt,
123 	}, {
124 		.base.cra_name		= "cbc(blowfish)",
125 		.base.cra_driver_name	= "cbc-blowfish-asm",
126 		.base.cra_priority	= 300,
127 		.base.cra_blocksize	= BF_BLOCK_SIZE,
128 		.base.cra_ctxsize	= sizeof(struct bf_ctx),
129 		.base.cra_module	= THIS_MODULE,
130 		.min_keysize		= BF_MIN_KEY_SIZE,
131 		.max_keysize		= BF_MAX_KEY_SIZE,
132 		.ivsize			= BF_BLOCK_SIZE,
133 		.setkey			= blowfish_setkey_skcipher,
134 		.encrypt		= cbc_encrypt,
135 		.decrypt		= cbc_decrypt,
136 	},
137 };
138 
139 static bool is_blacklisted_cpu(void)
140 {
141 	if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL)
142 		return false;
143 
144 	if (boot_cpu_data.x86 == 0x0f) {
145 		/*
146 		 * On Pentium 4, blowfish-x86_64 is slower than generic C
147 		 * implementation because use of 64bit rotates (which are really
148 		 * slow on P4). Therefore blacklist P4s.
149 		 */
150 		return true;
151 	}
152 
153 	return false;
154 }
155 
156 static int force;
157 module_param(force, int, 0);
158 MODULE_PARM_DESC(force, "Force module load, ignore CPU blacklist");
159 
160 static int __init blowfish_init(void)
161 {
162 	int err;
163 
164 	if (!force && is_blacklisted_cpu()) {
165 		printk(KERN_INFO
166 			"blowfish-x86_64: performance on this CPU "
167 			"would be suboptimal: disabling "
168 			"blowfish-x86_64.\n");
169 		return -ENODEV;
170 	}
171 
172 	err = crypto_register_alg(&bf_cipher_alg);
173 	if (err)
174 		return err;
175 
176 	err = crypto_register_skciphers(bf_skcipher_algs,
177 					ARRAY_SIZE(bf_skcipher_algs));
178 	if (err)
179 		crypto_unregister_alg(&bf_cipher_alg);
180 
181 	return err;
182 }
183 
184 static void __exit blowfish_fini(void)
185 {
186 	crypto_unregister_alg(&bf_cipher_alg);
187 	crypto_unregister_skciphers(bf_skcipher_algs,
188 				    ARRAY_SIZE(bf_skcipher_algs));
189 }
190 
191 module_init(blowfish_init);
192 module_exit(blowfish_fini);
193 
194 MODULE_LICENSE("GPL");
195 MODULE_DESCRIPTION("Blowfish Cipher Algorithm, asm optimized");
196 MODULE_ALIAS_CRYPTO("blowfish");
197 MODULE_ALIAS_CRYPTO("blowfish-asm");
198