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>
9 * CTR part based on code (crypto/ctr.c) by:
10 * (C) Copyright IBM Corp. 2007 - Joy Latten <latten@us.ibm.com>
13 #include <crypto/algapi.h>
14 #include <crypto/blowfish.h>
15 #include <crypto/internal/skcipher.h>
16 #include <linux/crypto.h>
17 #include <linux/init.h>
18 #include <linux/module.h>
19 #include <linux/types.h>
21 /* regular block cipher functions */
22 asmlinkage
void __blowfish_enc_blk(struct bf_ctx
*ctx
, u8
*dst
, const u8
*src
,
24 asmlinkage
void blowfish_dec_blk(struct bf_ctx
*ctx
, u8
*dst
, const u8
*src
);
26 /* 4-way parallel cipher functions */
27 asmlinkage
void __blowfish_enc_blk_4way(struct bf_ctx
*ctx
, u8
*dst
,
28 const u8
*src
, bool xor);
29 asmlinkage
void blowfish_dec_blk_4way(struct bf_ctx
*ctx
, u8
*dst
,
32 static inline void blowfish_enc_blk(struct bf_ctx
*ctx
, u8
*dst
, const u8
*src
)
34 __blowfish_enc_blk(ctx
, dst
, src
, false);
37 static inline void blowfish_enc_blk_xor(struct bf_ctx
*ctx
, u8
*dst
,
40 __blowfish_enc_blk(ctx
, dst
, src
, true);
43 static inline void blowfish_enc_blk_4way(struct bf_ctx
*ctx
, u8
*dst
,
46 __blowfish_enc_blk_4way(ctx
, dst
, src
, false);
49 static inline void blowfish_enc_blk_xor_4way(struct bf_ctx
*ctx
, u8
*dst
,
52 __blowfish_enc_blk_4way(ctx
, dst
, src
, true);
55 static void blowfish_encrypt(struct crypto_tfm
*tfm
, u8
*dst
, const u8
*src
)
57 blowfish_enc_blk(crypto_tfm_ctx(tfm
), dst
, src
);
60 static void blowfish_decrypt(struct crypto_tfm
*tfm
, u8
*dst
, const u8
*src
)
62 blowfish_dec_blk(crypto_tfm_ctx(tfm
), dst
, src
);
65 static int blowfish_setkey_skcipher(struct crypto_skcipher
*tfm
,
66 const u8
*key
, unsigned int keylen
)
68 return blowfish_setkey(&tfm
->base
, key
, keylen
);
71 static int ecb_crypt(struct skcipher_request
*req
,
72 void (*fn
)(struct bf_ctx
*, u8
*, const u8
*),
73 void (*fn_4way
)(struct bf_ctx
*, u8
*, const u8
*))
75 unsigned int bsize
= BF_BLOCK_SIZE
;
76 struct crypto_skcipher
*tfm
= crypto_skcipher_reqtfm(req
);
77 struct bf_ctx
*ctx
= crypto_skcipher_ctx(tfm
);
78 struct skcipher_walk walk
;
82 err
= skcipher_walk_virt(&walk
, req
, false);
84 while ((nbytes
= walk
.nbytes
)) {
85 u8
*wsrc
= walk
.src
.virt
.addr
;
86 u8
*wdst
= walk
.dst
.virt
.addr
;
88 /* Process four block batch */
89 if (nbytes
>= bsize
* 4) {
91 fn_4way(ctx
, wdst
, wsrc
);
96 } while (nbytes
>= bsize
* 4);
102 /* Handle leftovers */
109 } while (nbytes
>= bsize
);
112 err
= skcipher_walk_done(&walk
, nbytes
);
118 static int ecb_encrypt(struct skcipher_request
*req
)
120 return ecb_crypt(req
, blowfish_enc_blk
, blowfish_enc_blk_4way
);
123 static int ecb_decrypt(struct skcipher_request
*req
)
125 return ecb_crypt(req
, blowfish_dec_blk
, blowfish_dec_blk_4way
);
128 static unsigned int __cbc_encrypt(struct bf_ctx
*ctx
,
129 struct skcipher_walk
*walk
)
131 unsigned int bsize
= BF_BLOCK_SIZE
;
132 unsigned int nbytes
= walk
->nbytes
;
133 u64
*src
= (u64
*)walk
->src
.virt
.addr
;
134 u64
*dst
= (u64
*)walk
->dst
.virt
.addr
;
135 u64
*iv
= (u64
*)walk
->iv
;
139 blowfish_enc_blk(ctx
, (u8
*)dst
, (u8
*)dst
);
145 } while (nbytes
>= bsize
);
147 *(u64
*)walk
->iv
= *iv
;
151 static int cbc_encrypt(struct skcipher_request
*req
)
153 struct crypto_skcipher
*tfm
= crypto_skcipher_reqtfm(req
);
154 struct bf_ctx
*ctx
= crypto_skcipher_ctx(tfm
);
155 struct skcipher_walk walk
;
159 err
= skcipher_walk_virt(&walk
, req
, false);
161 while ((nbytes
= walk
.nbytes
)) {
162 nbytes
= __cbc_encrypt(ctx
, &walk
);
163 err
= skcipher_walk_done(&walk
, nbytes
);
169 static unsigned int __cbc_decrypt(struct bf_ctx
*ctx
,
170 struct skcipher_walk
*walk
)
172 unsigned int bsize
= BF_BLOCK_SIZE
;
173 unsigned int nbytes
= walk
->nbytes
;
174 u64
*src
= (u64
*)walk
->src
.virt
.addr
;
175 u64
*dst
= (u64
*)walk
->dst
.virt
.addr
;
179 /* Start of the last block. */
180 src
+= nbytes
/ bsize
- 1;
181 dst
+= nbytes
/ bsize
- 1;
185 /* Process four block batch */
186 if (nbytes
>= bsize
* 4) {
188 nbytes
-= bsize
* 4 - bsize
;
196 blowfish_dec_blk_4way(ctx
, (u8
*)dst
, (u8
*)src
);
209 } while (nbytes
>= bsize
* 4);
212 /* Handle leftovers */
214 blowfish_dec_blk(ctx
, (u8
*)dst
, (u8
*)src
);
226 *dst
^= *(u64
*)walk
->iv
;
227 *(u64
*)walk
->iv
= last_iv
;
232 static int cbc_decrypt(struct skcipher_request
*req
)
234 struct crypto_skcipher
*tfm
= crypto_skcipher_reqtfm(req
);
235 struct bf_ctx
*ctx
= crypto_skcipher_ctx(tfm
);
236 struct skcipher_walk walk
;
240 err
= skcipher_walk_virt(&walk
, req
, false);
242 while ((nbytes
= walk
.nbytes
)) {
243 nbytes
= __cbc_decrypt(ctx
, &walk
);
244 err
= skcipher_walk_done(&walk
, nbytes
);
250 static void ctr_crypt_final(struct bf_ctx
*ctx
, struct skcipher_walk
*walk
)
252 u8
*ctrblk
= walk
->iv
;
253 u8 keystream
[BF_BLOCK_SIZE
];
254 u8
*src
= walk
->src
.virt
.addr
;
255 u8
*dst
= walk
->dst
.virt
.addr
;
256 unsigned int nbytes
= walk
->nbytes
;
258 blowfish_enc_blk(ctx
, keystream
, ctrblk
);
259 crypto_xor_cpy(dst
, keystream
, src
, nbytes
);
261 crypto_inc(ctrblk
, BF_BLOCK_SIZE
);
264 static unsigned int __ctr_crypt(struct bf_ctx
*ctx
, struct skcipher_walk
*walk
)
266 unsigned int bsize
= BF_BLOCK_SIZE
;
267 unsigned int nbytes
= walk
->nbytes
;
268 u64
*src
= (u64
*)walk
->src
.virt
.addr
;
269 u64
*dst
= (u64
*)walk
->dst
.virt
.addr
;
270 u64 ctrblk
= be64_to_cpu(*(__be64
*)walk
->iv
);
273 /* Process four block batch */
274 if (nbytes
>= bsize
* 4) {
283 /* create ctrblks for parallel encrypt */
284 ctrblocks
[0] = cpu_to_be64(ctrblk
++);
285 ctrblocks
[1] = cpu_to_be64(ctrblk
++);
286 ctrblocks
[2] = cpu_to_be64(ctrblk
++);
287 ctrblocks
[3] = cpu_to_be64(ctrblk
++);
289 blowfish_enc_blk_xor_4way(ctx
, (u8
*)dst
,
294 } while ((nbytes
-= bsize
* 4) >= bsize
* 4);
300 /* Handle leftovers */
305 ctrblocks
[0] = cpu_to_be64(ctrblk
++);
307 blowfish_enc_blk_xor(ctx
, (u8
*)dst
, (u8
*)ctrblocks
);
311 } while ((nbytes
-= bsize
) >= bsize
);
314 *(__be64
*)walk
->iv
= cpu_to_be64(ctrblk
);
318 static int ctr_crypt(struct skcipher_request
*req
)
320 struct crypto_skcipher
*tfm
= crypto_skcipher_reqtfm(req
);
321 struct bf_ctx
*ctx
= crypto_skcipher_ctx(tfm
);
322 struct skcipher_walk walk
;
326 err
= skcipher_walk_virt(&walk
, req
, false);
328 while ((nbytes
= walk
.nbytes
) >= BF_BLOCK_SIZE
) {
329 nbytes
= __ctr_crypt(ctx
, &walk
);
330 err
= skcipher_walk_done(&walk
, nbytes
);
334 ctr_crypt_final(ctx
, &walk
);
335 err
= skcipher_walk_done(&walk
, 0);
341 static struct crypto_alg bf_cipher_alg
= {
342 .cra_name
= "blowfish",
343 .cra_driver_name
= "blowfish-asm",
345 .cra_flags
= CRYPTO_ALG_TYPE_CIPHER
,
346 .cra_blocksize
= BF_BLOCK_SIZE
,
347 .cra_ctxsize
= sizeof(struct bf_ctx
),
349 .cra_module
= THIS_MODULE
,
352 .cia_min_keysize
= BF_MIN_KEY_SIZE
,
353 .cia_max_keysize
= BF_MAX_KEY_SIZE
,
354 .cia_setkey
= blowfish_setkey
,
355 .cia_encrypt
= blowfish_encrypt
,
356 .cia_decrypt
= blowfish_decrypt
,
361 static struct skcipher_alg bf_skcipher_algs
[] = {
363 .base
.cra_name
= "ecb(blowfish)",
364 .base
.cra_driver_name
= "ecb-blowfish-asm",
365 .base
.cra_priority
= 300,
366 .base
.cra_blocksize
= BF_BLOCK_SIZE
,
367 .base
.cra_ctxsize
= sizeof(struct bf_ctx
),
368 .base
.cra_module
= THIS_MODULE
,
369 .min_keysize
= BF_MIN_KEY_SIZE
,
370 .max_keysize
= BF_MAX_KEY_SIZE
,
371 .setkey
= blowfish_setkey_skcipher
,
372 .encrypt
= ecb_encrypt
,
373 .decrypt
= ecb_decrypt
,
375 .base
.cra_name
= "cbc(blowfish)",
376 .base
.cra_driver_name
= "cbc-blowfish-asm",
377 .base
.cra_priority
= 300,
378 .base
.cra_blocksize
= BF_BLOCK_SIZE
,
379 .base
.cra_ctxsize
= sizeof(struct bf_ctx
),
380 .base
.cra_module
= THIS_MODULE
,
381 .min_keysize
= BF_MIN_KEY_SIZE
,
382 .max_keysize
= BF_MAX_KEY_SIZE
,
383 .ivsize
= BF_BLOCK_SIZE
,
384 .setkey
= blowfish_setkey_skcipher
,
385 .encrypt
= cbc_encrypt
,
386 .decrypt
= cbc_decrypt
,
388 .base
.cra_name
= "ctr(blowfish)",
389 .base
.cra_driver_name
= "ctr-blowfish-asm",
390 .base
.cra_priority
= 300,
391 .base
.cra_blocksize
= 1,
392 .base
.cra_ctxsize
= sizeof(struct bf_ctx
),
393 .base
.cra_module
= THIS_MODULE
,
394 .min_keysize
= BF_MIN_KEY_SIZE
,
395 .max_keysize
= BF_MAX_KEY_SIZE
,
396 .ivsize
= BF_BLOCK_SIZE
,
397 .chunksize
= BF_BLOCK_SIZE
,
398 .setkey
= blowfish_setkey_skcipher
,
399 .encrypt
= ctr_crypt
,
400 .decrypt
= ctr_crypt
,
404 static bool is_blacklisted_cpu(void)
406 if (boot_cpu_data
.x86_vendor
!= X86_VENDOR_INTEL
)
409 if (boot_cpu_data
.x86
== 0x0f) {
411 * On Pentium 4, blowfish-x86_64 is slower than generic C
412 * implementation because use of 64bit rotates (which are really
413 * slow on P4). Therefore blacklist P4s.
422 module_param(force
, int, 0);
423 MODULE_PARM_DESC(force
, "Force module load, ignore CPU blacklist");
425 static int __init
init(void)
429 if (!force
&& is_blacklisted_cpu()) {
431 "blowfish-x86_64: performance on this CPU "
432 "would be suboptimal: disabling "
433 "blowfish-x86_64.\n");
437 err
= crypto_register_alg(&bf_cipher_alg
);
441 err
= crypto_register_skciphers(bf_skcipher_algs
,
442 ARRAY_SIZE(bf_skcipher_algs
));
444 crypto_unregister_alg(&bf_cipher_alg
);
449 static void __exit
fini(void)
451 crypto_unregister_alg(&bf_cipher_alg
);
452 crypto_unregister_skciphers(bf_skcipher_algs
,
453 ARRAY_SIZE(bf_skcipher_algs
));
459 MODULE_LICENSE("GPL");
460 MODULE_DESCRIPTION("Blowfish Cipher Algorithm, asm optimized");
461 MODULE_ALIAS_CRYPTO("blowfish");
462 MODULE_ALIAS_CRYPTO("blowfish-asm");