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 #include "ecb_cbc_helpers.h"
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
);
25 /* 4-way parallel cipher functions */
26 asmlinkage
void blowfish_enc_blk_4way(struct bf_ctx
*ctx
, u8
*dst
,
28 asmlinkage
void __blowfish_dec_blk_4way(struct bf_ctx
*ctx
, u8
*dst
,
29 const u8
*src
, bool cbc
);
31 static inline void blowfish_dec_ecb_4way(struct bf_ctx
*ctx
, u8
*dst
,
34 return __blowfish_dec_blk_4way(ctx
, dst
, src
, false);
37 static inline void blowfish_dec_cbc_4way(struct bf_ctx
*ctx
, u8
*dst
,
40 return __blowfish_dec_blk_4way(ctx
, dst
, src
, true);
43 static void blowfish_encrypt(struct crypto_tfm
*tfm
, u8
*dst
, const u8
*src
)
45 blowfish_enc_blk(crypto_tfm_ctx(tfm
), dst
, src
);
48 static void blowfish_decrypt(struct crypto_tfm
*tfm
, u8
*dst
, const u8
*src
)
50 blowfish_dec_blk(crypto_tfm_ctx(tfm
), dst
, src
);
53 static int blowfish_setkey_skcipher(struct crypto_skcipher
*tfm
,
54 const u8
*key
, unsigned int keylen
)
56 return blowfish_setkey(&tfm
->base
, key
, keylen
);
59 static int ecb_encrypt(struct skcipher_request
*req
)
61 ECB_WALK_START(req
, BF_BLOCK_SIZE
, -1);
62 ECB_BLOCK(4, blowfish_enc_blk_4way
);
63 ECB_BLOCK(1, blowfish_enc_blk
);
67 static int ecb_decrypt(struct skcipher_request
*req
)
69 ECB_WALK_START(req
, BF_BLOCK_SIZE
, -1);
70 ECB_BLOCK(4, blowfish_dec_ecb_4way
);
71 ECB_BLOCK(1, blowfish_dec_blk
);
75 static int cbc_encrypt(struct skcipher_request
*req
)
77 CBC_WALK_START(req
, BF_BLOCK_SIZE
, -1);
78 CBC_ENC_BLOCK(blowfish_enc_blk
);
82 static int cbc_decrypt(struct skcipher_request
*req
)
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
);
90 static struct crypto_alg bf_cipher_alg
= {
91 .cra_name
= "blowfish",
92 .cra_driver_name
= "blowfish-asm",
94 .cra_flags
= CRYPTO_ALG_TYPE_CIPHER
,
95 .cra_blocksize
= BF_BLOCK_SIZE
,
96 .cra_ctxsize
= sizeof(struct bf_ctx
),
98 .cra_module
= THIS_MODULE
,
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
,
110 static struct skcipher_alg bf_skcipher_algs
[] = {
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
,
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
,
139 static bool is_blacklisted_cpu(void)
141 if (boot_cpu_data
.x86_vendor
!= X86_VENDOR_INTEL
)
144 if (boot_cpu_data
.x86
== 0x0f) {
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.
157 module_param(force
, int, 0);
158 MODULE_PARM_DESC(force
, "Force module load, ignore CPU blacklist");
160 static int __init
blowfish_init(void)
164 if (!force
&& is_blacklisted_cpu()) {
166 "blowfish-x86_64: performance on this CPU "
167 "would be suboptimal: disabling "
168 "blowfish-x86_64.\n");
172 err
= crypto_register_alg(&bf_cipher_alg
);
176 err
= crypto_register_skciphers(bf_skcipher_algs
,
177 ARRAY_SIZE(bf_skcipher_algs
));
179 crypto_unregister_alg(&bf_cipher_alg
);
184 static void __exit
blowfish_fini(void)
186 crypto_unregister_alg(&bf_cipher_alg
);
187 crypto_unregister_skciphers(bf_skcipher_algs
,
188 ARRAY_SIZE(bf_skcipher_algs
));
191 module_init(blowfish_init
);
192 module_exit(blowfish_fini
);
194 MODULE_LICENSE("GPL");
195 MODULE_DESCRIPTION("Blowfish Cipher Algorithm, asm optimized");
196 MODULE_ALIAS_CRYPTO("blowfish");
197 MODULE_ALIAS_CRYPTO("blowfish-asm");