1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Glue Code for assembler optimized version of Blowfish
5 * Copyright (c) 2011 Jussi Kivilinna <jussi.kivilinna@mbnet.fi>
7 * CBC & ECB parts based on code (crypto/cbc.c,ecb.c) by:
8 * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
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>
19 /* regular block cipher functions */
20 asmlinkage void __blowfish_enc_blk(struct bf_ctx *ctx, u8 *dst, const u8 *src,
22 asmlinkage void blowfish_dec_blk(struct bf_ctx *ctx, u8 *dst, const u8 *src);
24 /* 4-way parallel cipher functions */
25 asmlinkage void __blowfish_enc_blk_4way(struct bf_ctx *ctx, u8 *dst,
26 const u8 *src, bool xor);
27 asmlinkage void blowfish_dec_blk_4way(struct bf_ctx *ctx, u8 *dst,
30 static inline void blowfish_enc_blk(struct bf_ctx *ctx, u8 *dst, const u8 *src)
32 __blowfish_enc_blk(ctx, dst, src, false);
35 static inline void blowfish_enc_blk_4way(struct bf_ctx *ctx, u8 *dst,
38 __blowfish_enc_blk_4way(ctx, dst, src, false);
41 static void blowfish_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
43 blowfish_enc_blk(crypto_tfm_ctx(tfm), dst, src);
46 static void blowfish_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
48 blowfish_dec_blk(crypto_tfm_ctx(tfm), dst, src);
51 static int blowfish_setkey_skcipher(struct crypto_skcipher *tfm,
52 const u8 *key, unsigned int keylen)
54 return blowfish_setkey(&tfm->base, key, keylen);
57 static int ecb_crypt(struct skcipher_request *req,
58 void (*fn)(struct bf_ctx *, u8 *, const u8 *),
59 void (*fn_4way)(struct bf_ctx *, u8 *, const u8 *))
61 unsigned int bsize = BF_BLOCK_SIZE;
62 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
63 struct bf_ctx *ctx = crypto_skcipher_ctx(tfm);
64 struct skcipher_walk walk;
68 err = skcipher_walk_virt(&walk, req, false);
70 while ((nbytes = walk.nbytes)) {
71 u8 *wsrc = walk.src.virt.addr;
72 u8 *wdst = walk.dst.virt.addr;
74 /* Process four block batch */
75 if (nbytes >= bsize * 4) {
77 fn_4way(ctx, wdst, wsrc);
82 } while (nbytes >= bsize * 4);
88 /* Handle leftovers */
95 } while (nbytes >= bsize);
98 err = skcipher_walk_done(&walk, nbytes);
104 static int ecb_encrypt(struct skcipher_request *req)
106 return ecb_crypt(req, blowfish_enc_blk, blowfish_enc_blk_4way);
109 static int ecb_decrypt(struct skcipher_request *req)
111 return ecb_crypt(req, blowfish_dec_blk, blowfish_dec_blk_4way);
114 static unsigned int __cbc_encrypt(struct bf_ctx *ctx,
115 struct skcipher_walk *walk)
117 unsigned int bsize = BF_BLOCK_SIZE;
118 unsigned int nbytes = walk->nbytes;
119 u64 *src = (u64 *)walk->src.virt.addr;
120 u64 *dst = (u64 *)walk->dst.virt.addr;
121 u64 *iv = (u64 *)walk->iv;
125 blowfish_enc_blk(ctx, (u8 *)dst, (u8 *)dst);
131 } while (nbytes >= bsize);
133 *(u64 *)walk->iv = *iv;
137 static int cbc_encrypt(struct skcipher_request *req)
139 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
140 struct bf_ctx *ctx = crypto_skcipher_ctx(tfm);
141 struct skcipher_walk walk;
145 err = skcipher_walk_virt(&walk, req, false);
147 while ((nbytes = walk.nbytes)) {
148 nbytes = __cbc_encrypt(ctx, &walk);
149 err = skcipher_walk_done(&walk, nbytes);
155 static unsigned int __cbc_decrypt(struct bf_ctx *ctx,
156 struct skcipher_walk *walk)
158 unsigned int bsize = BF_BLOCK_SIZE;
159 unsigned int nbytes = walk->nbytes;
160 u64 *src = (u64 *)walk->src.virt.addr;
161 u64 *dst = (u64 *)walk->dst.virt.addr;
165 /* Start of the last block. */
166 src += nbytes / bsize - 1;
167 dst += nbytes / bsize - 1;
171 /* Process four block batch */
172 if (nbytes >= bsize * 4) {
174 nbytes -= bsize * 4 - bsize;
182 blowfish_dec_blk_4way(ctx, (u8 *)dst, (u8 *)src);
195 } while (nbytes >= bsize * 4);
198 /* Handle leftovers */
200 blowfish_dec_blk(ctx, (u8 *)dst, (u8 *)src);
212 *dst ^= *(u64 *)walk->iv;
213 *(u64 *)walk->iv = last_iv;
218 static int cbc_decrypt(struct skcipher_request *req)
220 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
221 struct bf_ctx *ctx = crypto_skcipher_ctx(tfm);
222 struct skcipher_walk walk;
226 err = skcipher_walk_virt(&walk, req, false);
228 while ((nbytes = walk.nbytes)) {
229 nbytes = __cbc_decrypt(ctx, &walk);
230 err = skcipher_walk_done(&walk, nbytes);
236 static struct crypto_alg bf_cipher_alg = {
237 .cra_name = "blowfish",
238 .cra_driver_name = "blowfish-asm",
240 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
241 .cra_blocksize = BF_BLOCK_SIZE,
242 .cra_ctxsize = sizeof(struct bf_ctx),
244 .cra_module = THIS_MODULE,
247 .cia_min_keysize = BF_MIN_KEY_SIZE,
248 .cia_max_keysize = BF_MAX_KEY_SIZE,
249 .cia_setkey = blowfish_setkey,
250 .cia_encrypt = blowfish_encrypt,
251 .cia_decrypt = blowfish_decrypt,
256 static struct skcipher_alg bf_skcipher_algs[] = {
258 .base.cra_name = "ecb(blowfish)",
259 .base.cra_driver_name = "ecb-blowfish-asm",
260 .base.cra_priority = 300,
261 .base.cra_blocksize = BF_BLOCK_SIZE,
262 .base.cra_ctxsize = sizeof(struct bf_ctx),
263 .base.cra_module = THIS_MODULE,
264 .min_keysize = BF_MIN_KEY_SIZE,
265 .max_keysize = BF_MAX_KEY_SIZE,
266 .setkey = blowfish_setkey_skcipher,
267 .encrypt = ecb_encrypt,
268 .decrypt = ecb_decrypt,
270 .base.cra_name = "cbc(blowfish)",
271 .base.cra_driver_name = "cbc-blowfish-asm",
272 .base.cra_priority = 300,
273 .base.cra_blocksize = BF_BLOCK_SIZE,
274 .base.cra_ctxsize = sizeof(struct bf_ctx),
275 .base.cra_module = THIS_MODULE,
276 .min_keysize = BF_MIN_KEY_SIZE,
277 .max_keysize = BF_MAX_KEY_SIZE,
278 .ivsize = BF_BLOCK_SIZE,
279 .setkey = blowfish_setkey_skcipher,
280 .encrypt = cbc_encrypt,
281 .decrypt = cbc_decrypt,
285 static bool is_blacklisted_cpu(void)
287 if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL)
290 if (boot_cpu_data.x86 == 0x0f) {
292 * On Pentium 4, blowfish-x86_64 is slower than generic C
293 * implementation because use of 64bit rotates (which are really
294 * slow on P4). Therefore blacklist P4s.
303 module_param(force, int, 0);
304 MODULE_PARM_DESC(force, "Force module load, ignore CPU blacklist");
306 static int __init blowfish_init(void)
310 if (!force && is_blacklisted_cpu()) {
312 "blowfish-x86_64: performance on this CPU "
313 "would be suboptimal: disabling "
314 "blowfish-x86_64.\n");
318 err = crypto_register_alg(&bf_cipher_alg);
322 err = crypto_register_skciphers(bf_skcipher_algs,
323 ARRAY_SIZE(bf_skcipher_algs));
325 crypto_unregister_alg(&bf_cipher_alg);
330 static void __exit blowfish_fini(void)
332 crypto_unregister_alg(&bf_cipher_alg);
333 crypto_unregister_skciphers(bf_skcipher_algs,
334 ARRAY_SIZE(bf_skcipher_algs));
337 module_init(blowfish_init);
338 module_exit(blowfish_fini);
340 MODULE_LICENSE("GPL");
341 MODULE_DESCRIPTION("Blowfish Cipher Algorithm, asm optimized");
342 MODULE_ALIAS_CRYPTO("blowfish");
343 MODULE_ALIAS_CRYPTO("blowfish-asm");