1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Glue Code for assembler optimized version of 3DES
7 * CBC & ECB parts based on code (crypto/cbc.c,ecb.c) by:
11 #include <crypto/algapi.h>
12 #include <crypto/des.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 struct des3_ede_x86_ctx {
20 struct des3_ede_ctx enc;
21 struct des3_ede_ctx dec;
24 /* regular block cipher functions */
25 asmlinkage void des3_ede_x86_64_crypt_blk(const u32 *expkey, u8 *dst,
28 /* 3-way parallel cipher functions */
29 asmlinkage void des3_ede_x86_64_crypt_blk_3way(const u32 *expkey, u8 *dst,
32 static inline void des3_ede_enc_blk(struct des3_ede_x86_ctx *ctx, u8 *dst,
35 u32 *enc_ctx = ctx->enc.expkey;
37 des3_ede_x86_64_crypt_blk(enc_ctx, dst, src);
40 static inline void des3_ede_dec_blk(struct des3_ede_x86_ctx *ctx, u8 *dst,
43 u32 *dec_ctx = ctx->dec.expkey;
45 des3_ede_x86_64_crypt_blk(dec_ctx, dst, src);
48 static inline void des3_ede_dec_blk_3way(struct des3_ede_x86_ctx *ctx, u8 *dst,
51 u32 *dec_ctx = ctx->dec.expkey;
53 des3_ede_x86_64_crypt_blk_3way(dec_ctx, dst, src);
56 static void des3_ede_x86_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
58 des3_ede_enc_blk(crypto_tfm_ctx(tfm), dst, src);
61 static void des3_ede_x86_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
63 des3_ede_dec_blk(crypto_tfm_ctx(tfm), dst, src);
66 static int ecb_crypt(struct skcipher_request *req, const u32 *expkey)
68 const unsigned int bsize = DES3_EDE_BLOCK_SIZE;
69 struct skcipher_walk walk;
73 err = skcipher_walk_virt(&walk, req, false);
75 while ((nbytes = walk.nbytes)) {
76 u8 *wsrc = walk.src.virt.addr;
77 u8 *wdst = walk.dst.virt.addr;
79 /* Process four block batch */
80 if (nbytes >= bsize * 3) {
82 des3_ede_x86_64_crypt_blk_3way(expkey, wdst,
88 } while (nbytes >= bsize * 3);
94 /* Handle leftovers */
96 des3_ede_x86_64_crypt_blk(expkey, wdst, wsrc);
101 } while (nbytes >= bsize);
104 err = skcipher_walk_done(&walk, nbytes);
110 static int ecb_encrypt(struct skcipher_request *req)
112 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
113 struct des3_ede_x86_ctx *ctx = crypto_skcipher_ctx(tfm);
115 return ecb_crypt(req, ctx->enc.expkey);
118 static int ecb_decrypt(struct skcipher_request *req)
120 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
121 struct des3_ede_x86_ctx *ctx = crypto_skcipher_ctx(tfm);
123 return ecb_crypt(req, ctx->dec.expkey);
126 static unsigned int __cbc_encrypt(struct des3_ede_x86_ctx *ctx,
127 struct skcipher_walk *walk)
129 unsigned int bsize = DES3_EDE_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 des3_ede_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 des3_ede_x86_ctx *ctx = crypto_skcipher_ctx(tfm);
153 struct skcipher_walk walk;
157 err = skcipher_walk_virt(&walk, req, false);
159 while (walk.nbytes) {
160 nbytes = __cbc_encrypt(ctx, &walk);
161 err = skcipher_walk_done(&walk, nbytes);
167 static unsigned int __cbc_decrypt(struct des3_ede_x86_ctx *ctx,
168 struct skcipher_walk *walk)
170 unsigned int bsize = DES3_EDE_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 * 3) {
186 nbytes -= bsize * 3 - bsize;
193 des3_ede_dec_blk_3way(ctx, (u8 *)dst, (u8 *)src);
205 } while (nbytes >= bsize * 3);
208 /* Handle leftovers */
210 des3_ede_dec_blk(ctx, (u8 *)dst, (u8 *)src);
222 *dst ^= *(u64 *)walk->iv;
223 *(u64 *)walk->iv = last_iv;
228 static int cbc_decrypt(struct skcipher_request *req)
230 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
231 struct des3_ede_x86_ctx *ctx = crypto_skcipher_ctx(tfm);
232 struct skcipher_walk walk;
236 err = skcipher_walk_virt(&walk, req, false);
238 while (walk.nbytes) {
239 nbytes = __cbc_decrypt(ctx, &walk);
240 err = skcipher_walk_done(&walk, nbytes);
246 static int des3_ede_x86_setkey(struct crypto_tfm *tfm, const u8 *key,
249 struct des3_ede_x86_ctx *ctx = crypto_tfm_ctx(tfm);
253 err = des3_ede_expand_key(&ctx->enc, key, keylen);
254 if (err == -ENOKEY) {
255 if (crypto_tfm_get_flags(tfm) & CRYPTO_TFM_REQ_FORBID_WEAK_KEYS)
262 memset(ctx, 0, sizeof(*ctx));
266 /* Fix encryption context for this implementation and form decryption
268 j = DES3_EDE_EXPKEY_WORDS - 2;
269 for (i = 0; i < DES3_EDE_EXPKEY_WORDS; i += 2, j -= 2) {
270 tmp = ror32(ctx->enc.expkey[i + 1], 4);
271 ctx->enc.expkey[i + 1] = tmp;
273 ctx->dec.expkey[j + 0] = ctx->enc.expkey[i + 0];
274 ctx->dec.expkey[j + 1] = tmp;
280 static int des3_ede_x86_setkey_skcipher(struct crypto_skcipher *tfm,
284 return des3_ede_x86_setkey(&tfm->base, key, keylen);
287 static struct crypto_alg des3_ede_cipher = {
288 .cra_name = "des3_ede",
289 .cra_driver_name = "des3_ede-asm",
291 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
292 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
293 .cra_ctxsize = sizeof(struct des3_ede_x86_ctx),
295 .cra_module = THIS_MODULE,
298 .cia_min_keysize = DES3_EDE_KEY_SIZE,
299 .cia_max_keysize = DES3_EDE_KEY_SIZE,
300 .cia_setkey = des3_ede_x86_setkey,
301 .cia_encrypt = des3_ede_x86_encrypt,
302 .cia_decrypt = des3_ede_x86_decrypt,
307 static struct skcipher_alg des3_ede_skciphers[] = {
309 .base.cra_name = "ecb(des3_ede)",
310 .base.cra_driver_name = "ecb-des3_ede-asm",
311 .base.cra_priority = 300,
312 .base.cra_blocksize = DES3_EDE_BLOCK_SIZE,
313 .base.cra_ctxsize = sizeof(struct des3_ede_x86_ctx),
314 .base.cra_module = THIS_MODULE,
315 .min_keysize = DES3_EDE_KEY_SIZE,
316 .max_keysize = DES3_EDE_KEY_SIZE,
317 .setkey = des3_ede_x86_setkey_skcipher,
318 .encrypt = ecb_encrypt,
319 .decrypt = ecb_decrypt,
321 .base.cra_name = "cbc(des3_ede)",
322 .base.cra_driver_name = "cbc-des3_ede-asm",
323 .base.cra_priority = 300,
324 .base.cra_blocksize = DES3_EDE_BLOCK_SIZE,
325 .base.cra_ctxsize = sizeof(struct des3_ede_x86_ctx),
326 .base.cra_module = THIS_MODULE,
327 .min_keysize = DES3_EDE_KEY_SIZE,
328 .max_keysize = DES3_EDE_KEY_SIZE,
329 .ivsize = DES3_EDE_BLOCK_SIZE,
330 .setkey = des3_ede_x86_setkey_skcipher,
331 .encrypt = cbc_encrypt,
332 .decrypt = cbc_decrypt,
336 static bool is_blacklisted_cpu(void)
338 if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL)
341 if (boot_cpu_data.x86 == 0x0f) {
343 * On Pentium 4, des3_ede-x86_64 is slower than generic C
344 * implementation because use of 64bit rotates (which are really
345 * slow on P4). Therefore blacklist P4s.
354 module_param(force, int, 0);
355 MODULE_PARM_DESC(force, "Force module load, ignore CPU blacklist");
357 static int __init des3_ede_x86_init(void)
361 if (!force && is_blacklisted_cpu()) {
362 pr_info("des3_ede-x86_64: performance on this CPU would be suboptimal: disabling des3_ede-x86_64.\n");
366 err = crypto_register_alg(&des3_ede_cipher);
370 err = crypto_register_skciphers(des3_ede_skciphers,
371 ARRAY_SIZE(des3_ede_skciphers));
373 crypto_unregister_alg(&des3_ede_cipher);
378 static void __exit des3_ede_x86_fini(void)
380 crypto_unregister_alg(&des3_ede_cipher);
381 crypto_unregister_skciphers(des3_ede_skciphers,
382 ARRAY_SIZE(des3_ede_skciphers));
385 module_init(des3_ede_x86_init);
386 module_exit(des3_ede_x86_fini);
388 MODULE_LICENSE("GPL");
389 MODULE_DESCRIPTION("Triple DES EDE Cipher Algorithm, asm optimized");
390 MODULE_ALIAS_CRYPTO("des3_ede");
391 MODULE_ALIAS_CRYPTO("des3_ede-asm");