1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Glue Code for assembler optimized version of Blowfish
7 * CBC & ECB parts based on code (crypto/cbc.c,ecb.c) by:
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_xor(struct bf_ctx *ctx, u8 *dst,
38 __blowfish_enc_blk(ctx, dst, src, true);
41 static inline void blowfish_enc_blk_4way(struct bf_ctx *ctx, u8 *dst,
44 __blowfish_enc_blk_4way(ctx, dst, src, false);
47 static inline void blowfish_enc_blk_xor_4way(struct bf_ctx *ctx, u8 *dst,
50 __blowfish_enc_blk_4way(ctx, dst, src, true);
53 static void blowfish_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
55 blowfish_enc_blk(crypto_tfm_ctx(tfm), dst, src);
58 static void blowfish_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
60 blowfish_dec_blk(crypto_tfm_ctx(tfm), dst, src);
63 static int blowfish_setkey_skcipher(struct crypto_skcipher *tfm,
64 const u8 *key, unsigned int keylen)
66 return blowfish_setkey(&tfm->base, key, keylen);
69 static int ecb_crypt(struct skcipher_request *req,
70 void (*fn)(struct bf_ctx *, u8 *, const u8 *),
71 void (*fn_4way)(struct bf_ctx *, u8 *, const u8 *))
73 unsigned int bsize = BF_BLOCK_SIZE;
74 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
75 struct bf_ctx *ctx = crypto_skcipher_ctx(tfm);
76 struct skcipher_walk walk;
80 err = skcipher_walk_virt(&walk, req, false);
82 while ((nbytes = walk.nbytes)) {
83 u8 *wsrc = walk.src.virt.addr;
84 u8 *wdst = walk.dst.virt.addr;
86 /* Process four block batch */
87 if (nbytes >= bsize * 4) {
89 fn_4way(ctx, wdst, wsrc);
94 } while (nbytes >= bsize * 4);
100 /* Handle leftovers */
107 } while (nbytes >= bsize);
110 err = skcipher_walk_done(&walk, nbytes);
116 static int ecb_encrypt(struct skcipher_request *req)
118 return ecb_crypt(req, blowfish_enc_blk, blowfish_enc_blk_4way);
121 static int ecb_decrypt(struct skcipher_request *req)
123 return ecb_crypt(req, blowfish_dec_blk, blowfish_dec_blk_4way);
126 static unsigned int __cbc_encrypt(struct bf_ctx *ctx,
127 struct skcipher_walk *walk)
129 unsigned int bsize = BF_BLOCK_SIZE;
130 unsigned int nbytes = walk->nbytes;
131 u64 *src = (u64 *)walk->src.virt.addr;
132 u64 *dst = (u64 *)walk->dst.virt.addr;
133 u64 *iv = (u64 *)walk->iv;
137 blowfish_enc_blk(ctx, (u8 *)dst, (u8 *)dst);
143 } while (nbytes >= bsize);
145 *(u64 *)walk->iv = *iv;
149 static int cbc_encrypt(struct skcipher_request *req)
151 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
152 struct bf_ctx *ctx = crypto_skcipher_ctx(tfm);
153 struct skcipher_walk walk;
157 err = skcipher_walk_virt(&walk, req, false);
159 while ((nbytes = walk.nbytes)) {
160 nbytes = __cbc_encrypt(ctx, &walk);
161 err = skcipher_walk_done(&walk, nbytes);
167 static unsigned int __cbc_decrypt(struct bf_ctx *ctx,
168 struct skcipher_walk *walk)
170 unsigned int bsize = BF_BLOCK_SIZE;
171 unsigned int nbytes = walk->nbytes;
172 u64 *src = (u64 *)walk->src.virt.addr;
173 u64 *dst = (u64 *)walk->dst.virt.addr;
177 /* Start of the last block. */
178 src += nbytes / bsize - 1;
179 dst += nbytes / bsize - 1;
183 /* Process four block batch */
184 if (nbytes >= bsize * 4) {
186 nbytes -= bsize * 4 - bsize;
194 blowfish_dec_blk_4way(ctx, (u8 *)dst, (u8 *)src);
207 } while (nbytes >= bsize * 4);
210 /* Handle leftovers */
212 blowfish_dec_blk(ctx, (u8 *)dst, (u8 *)src);
224 *dst ^= *(u64 *)walk->iv;
225 *(u64 *)walk->iv = last_iv;
230 static int cbc_decrypt(struct skcipher_request *req)
232 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
233 struct bf_ctx *ctx = crypto_skcipher_ctx(tfm);
234 struct skcipher_walk walk;
238 err = skcipher_walk_virt(&walk, req, false);
240 while ((nbytes = walk.nbytes)) {
241 nbytes = __cbc_decrypt(ctx, &walk);
242 err = skcipher_walk_done(&walk, nbytes);
248 static struct crypto_alg bf_cipher_alg = {
249 .cra_name = "blowfish",
250 .cra_driver_name = "blowfish-asm",
252 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
253 .cra_blocksize = BF_BLOCK_SIZE,
254 .cra_ctxsize = sizeof(struct bf_ctx),
256 .cra_module = THIS_MODULE,
259 .cia_min_keysize = BF_MIN_KEY_SIZE,
260 .cia_max_keysize = BF_MAX_KEY_SIZE,
261 .cia_setkey = blowfish_setkey,
262 .cia_encrypt = blowfish_encrypt,
263 .cia_decrypt = blowfish_decrypt,
268 static struct skcipher_alg bf_skcipher_algs[] = {
270 .base.cra_name = "ecb(blowfish)",
271 .base.cra_driver_name = "ecb-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 .setkey = blowfish_setkey_skcipher,
279 .encrypt = ecb_encrypt,
280 .decrypt = ecb_decrypt,
282 .base.cra_name = "cbc(blowfish)",
283 .base.cra_driver_name = "cbc-blowfish-asm",
284 .base.cra_priority = 300,
285 .base.cra_blocksize = BF_BLOCK_SIZE,
286 .base.cra_ctxsize = sizeof(struct bf_ctx),
287 .base.cra_module = THIS_MODULE,
288 .min_keysize = BF_MIN_KEY_SIZE,
289 .max_keysize = BF_MAX_KEY_SIZE,
290 .ivsize = BF_BLOCK_SIZE,
291 .setkey = blowfish_setkey_skcipher,
292 .encrypt = cbc_encrypt,
293 .decrypt = cbc_decrypt,
297 static bool is_blacklisted_cpu(void)
299 if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL)
302 if (boot_cpu_data.x86 == 0x0f) {
304 * On Pentium 4, blowfish-x86_64 is slower than generic C
305 * implementation because use of 64bit rotates (which are really
306 * slow on P4). Therefore blacklist P4s.
315 module_param(force, int, 0);
316 MODULE_PARM_DESC(force, "Force module load, ignore CPU blacklist");
318 static int __init init(void)
322 if (!force && is_blacklisted_cpu()) {
324 "blowfish-x86_64: performance on this CPU "
325 "would be suboptimal: disabling "
326 "blowfish-x86_64.\n");
330 err = crypto_register_alg(&bf_cipher_alg);
334 err = crypto_register_skciphers(bf_skcipher_algs,
335 ARRAY_SIZE(bf_skcipher_algs));
337 crypto_unregister_alg(&bf_cipher_alg);
342 static void __exit fini(void)
344 crypto_unregister_alg(&bf_cipher_alg);
345 crypto_unregister_skciphers(bf_skcipher_algs,
346 ARRAY_SIZE(bf_skcipher_algs));
352 MODULE_LICENSE("GPL");
353 MODULE_DESCRIPTION("Blowfish Cipher Algorithm, asm optimized");
354 MODULE_ALIAS_CRYPTO("blowfish");
355 MODULE_ALIAS_CRYPTO("blowfish-asm");