1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Glue Code for assembler optimized version of 3DES
5 * Copyright © 2014 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/des.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 struct des3_ede_x86_ctx
{
22 struct des3_ede_ctx enc
;
23 struct des3_ede_ctx dec
;
26 /* regular block cipher functions */
27 asmlinkage
void des3_ede_x86_64_crypt_blk(const u32
*expkey
, u8
*dst
,
30 /* 3-way parallel cipher functions */
31 asmlinkage
void des3_ede_x86_64_crypt_blk_3way(const u32
*expkey
, u8
*dst
,
34 static inline void des3_ede_enc_blk(struct des3_ede_x86_ctx
*ctx
, u8
*dst
,
37 u32
*enc_ctx
= ctx
->enc
.expkey
;
39 des3_ede_x86_64_crypt_blk(enc_ctx
, dst
, src
);
42 static inline void des3_ede_dec_blk(struct des3_ede_x86_ctx
*ctx
, u8
*dst
,
45 u32
*dec_ctx
= ctx
->dec
.expkey
;
47 des3_ede_x86_64_crypt_blk(dec_ctx
, dst
, src
);
50 static inline void des3_ede_enc_blk_3way(struct des3_ede_x86_ctx
*ctx
, u8
*dst
,
53 u32
*enc_ctx
= ctx
->enc
.expkey
;
55 des3_ede_x86_64_crypt_blk_3way(enc_ctx
, dst
, src
);
58 static inline void des3_ede_dec_blk_3way(struct des3_ede_x86_ctx
*ctx
, u8
*dst
,
61 u32
*dec_ctx
= ctx
->dec
.expkey
;
63 des3_ede_x86_64_crypt_blk_3way(dec_ctx
, dst
, src
);
66 static void des3_ede_x86_encrypt(struct crypto_tfm
*tfm
, u8
*dst
, const u8
*src
)
68 des3_ede_enc_blk(crypto_tfm_ctx(tfm
), dst
, src
);
71 static void des3_ede_x86_decrypt(struct crypto_tfm
*tfm
, u8
*dst
, const u8
*src
)
73 des3_ede_dec_blk(crypto_tfm_ctx(tfm
), dst
, src
);
76 static int ecb_crypt(struct skcipher_request
*req
, const u32
*expkey
)
78 const unsigned int bsize
= DES3_EDE_BLOCK_SIZE
;
79 struct skcipher_walk walk
;
83 err
= skcipher_walk_virt(&walk
, req
, false);
85 while ((nbytes
= walk
.nbytes
)) {
86 u8
*wsrc
= walk
.src
.virt
.addr
;
87 u8
*wdst
= walk
.dst
.virt
.addr
;
89 /* Process four block batch */
90 if (nbytes
>= bsize
* 3) {
92 des3_ede_x86_64_crypt_blk_3way(expkey
, wdst
,
98 } while (nbytes
>= bsize
* 3);
104 /* Handle leftovers */
106 des3_ede_x86_64_crypt_blk(expkey
, wdst
, wsrc
);
111 } while (nbytes
>= bsize
);
114 err
= skcipher_walk_done(&walk
, nbytes
);
120 static int ecb_encrypt(struct skcipher_request
*req
)
122 struct crypto_skcipher
*tfm
= crypto_skcipher_reqtfm(req
);
123 struct des3_ede_x86_ctx
*ctx
= crypto_skcipher_ctx(tfm
);
125 return ecb_crypt(req
, ctx
->enc
.expkey
);
128 static int ecb_decrypt(struct skcipher_request
*req
)
130 struct crypto_skcipher
*tfm
= crypto_skcipher_reqtfm(req
);
131 struct des3_ede_x86_ctx
*ctx
= crypto_skcipher_ctx(tfm
);
133 return ecb_crypt(req
, ctx
->dec
.expkey
);
136 static unsigned int __cbc_encrypt(struct des3_ede_x86_ctx
*ctx
,
137 struct skcipher_walk
*walk
)
139 unsigned int bsize
= DES3_EDE_BLOCK_SIZE
;
140 unsigned int nbytes
= walk
->nbytes
;
141 u64
*src
= (u64
*)walk
->src
.virt
.addr
;
142 u64
*dst
= (u64
*)walk
->dst
.virt
.addr
;
143 u64
*iv
= (u64
*)walk
->iv
;
147 des3_ede_enc_blk(ctx
, (u8
*)dst
, (u8
*)dst
);
153 } while (nbytes
>= bsize
);
155 *(u64
*)walk
->iv
= *iv
;
159 static int cbc_encrypt(struct skcipher_request
*req
)
161 struct crypto_skcipher
*tfm
= crypto_skcipher_reqtfm(req
);
162 struct des3_ede_x86_ctx
*ctx
= crypto_skcipher_ctx(tfm
);
163 struct skcipher_walk walk
;
167 err
= skcipher_walk_virt(&walk
, req
, false);
169 while ((nbytes
= walk
.nbytes
)) {
170 nbytes
= __cbc_encrypt(ctx
, &walk
);
171 err
= skcipher_walk_done(&walk
, nbytes
);
177 static unsigned int __cbc_decrypt(struct des3_ede_x86_ctx
*ctx
,
178 struct skcipher_walk
*walk
)
180 unsigned int bsize
= DES3_EDE_BLOCK_SIZE
;
181 unsigned int nbytes
= walk
->nbytes
;
182 u64
*src
= (u64
*)walk
->src
.virt
.addr
;
183 u64
*dst
= (u64
*)walk
->dst
.virt
.addr
;
187 /* Start of the last block. */
188 src
+= nbytes
/ bsize
- 1;
189 dst
+= nbytes
/ bsize
- 1;
193 /* Process four block batch */
194 if (nbytes
>= bsize
* 3) {
196 nbytes
-= bsize
* 3 - bsize
;
203 des3_ede_dec_blk_3way(ctx
, (u8
*)dst
, (u8
*)src
);
215 } while (nbytes
>= bsize
* 3);
218 /* Handle leftovers */
220 des3_ede_dec_blk(ctx
, (u8
*)dst
, (u8
*)src
);
232 *dst
^= *(u64
*)walk
->iv
;
233 *(u64
*)walk
->iv
= last_iv
;
238 static int cbc_decrypt(struct skcipher_request
*req
)
240 struct crypto_skcipher
*tfm
= crypto_skcipher_reqtfm(req
);
241 struct des3_ede_x86_ctx
*ctx
= crypto_skcipher_ctx(tfm
);
242 struct skcipher_walk walk
;
246 err
= skcipher_walk_virt(&walk
, req
, false);
248 while ((nbytes
= walk
.nbytes
)) {
249 nbytes
= __cbc_decrypt(ctx
, &walk
);
250 err
= skcipher_walk_done(&walk
, nbytes
);
256 static void ctr_crypt_final(struct des3_ede_x86_ctx
*ctx
,
257 struct skcipher_walk
*walk
)
259 u8
*ctrblk
= walk
->iv
;
260 u8 keystream
[DES3_EDE_BLOCK_SIZE
];
261 u8
*src
= walk
->src
.virt
.addr
;
262 u8
*dst
= walk
->dst
.virt
.addr
;
263 unsigned int nbytes
= walk
->nbytes
;
265 des3_ede_enc_blk(ctx
, keystream
, ctrblk
);
266 crypto_xor_cpy(dst
, keystream
, src
, nbytes
);
268 crypto_inc(ctrblk
, DES3_EDE_BLOCK_SIZE
);
271 static unsigned int __ctr_crypt(struct des3_ede_x86_ctx
*ctx
,
272 struct skcipher_walk
*walk
)
274 unsigned int bsize
= DES3_EDE_BLOCK_SIZE
;
275 unsigned int nbytes
= walk
->nbytes
;
276 __be64
*src
= (__be64
*)walk
->src
.virt
.addr
;
277 __be64
*dst
= (__be64
*)walk
->dst
.virt
.addr
;
278 u64 ctrblk
= be64_to_cpu(*(__be64
*)walk
->iv
);
281 /* Process four block batch */
282 if (nbytes
>= bsize
* 3) {
284 /* create ctrblks for parallel encrypt */
285 ctrblocks
[0] = cpu_to_be64(ctrblk
++);
286 ctrblocks
[1] = cpu_to_be64(ctrblk
++);
287 ctrblocks
[2] = cpu_to_be64(ctrblk
++);
289 des3_ede_enc_blk_3way(ctx
, (u8
*)ctrblocks
,
292 dst
[0] = src
[0] ^ ctrblocks
[0];
293 dst
[1] = src
[1] ^ ctrblocks
[1];
294 dst
[2] = src
[2] ^ ctrblocks
[2];
298 } while ((nbytes
-= bsize
* 3) >= bsize
* 3);
304 /* Handle leftovers */
306 ctrblocks
[0] = cpu_to_be64(ctrblk
++);
308 des3_ede_enc_blk(ctx
, (u8
*)ctrblocks
, (u8
*)ctrblocks
);
310 dst
[0] = src
[0] ^ ctrblocks
[0];
314 } while ((nbytes
-= bsize
) >= bsize
);
317 *(__be64
*)walk
->iv
= cpu_to_be64(ctrblk
);
321 static int ctr_crypt(struct skcipher_request
*req
)
323 struct crypto_skcipher
*tfm
= crypto_skcipher_reqtfm(req
);
324 struct des3_ede_x86_ctx
*ctx
= crypto_skcipher_ctx(tfm
);
325 struct skcipher_walk walk
;
329 err
= skcipher_walk_virt(&walk
, req
, false);
331 while ((nbytes
= walk
.nbytes
) >= DES3_EDE_BLOCK_SIZE
) {
332 nbytes
= __ctr_crypt(ctx
, &walk
);
333 err
= skcipher_walk_done(&walk
, nbytes
);
337 ctr_crypt_final(ctx
, &walk
);
338 err
= skcipher_walk_done(&walk
, 0);
344 static int des3_ede_x86_setkey(struct crypto_tfm
*tfm
, const u8
*key
,
347 struct des3_ede_x86_ctx
*ctx
= crypto_tfm_ctx(tfm
);
351 err
= des3_ede_expand_key(&ctx
->enc
, key
, keylen
);
352 if (err
== -ENOKEY
) {
353 if (crypto_tfm_get_flags(tfm
) & CRYPTO_TFM_REQ_FORBID_WEAK_KEYS
)
360 memset(ctx
, 0, sizeof(*ctx
));
364 /* Fix encryption context for this implementation and form decryption
366 j
= DES3_EDE_EXPKEY_WORDS
- 2;
367 for (i
= 0; i
< DES3_EDE_EXPKEY_WORDS
; i
+= 2, j
-= 2) {
368 tmp
= ror32(ctx
->enc
.expkey
[i
+ 1], 4);
369 ctx
->enc
.expkey
[i
+ 1] = tmp
;
371 ctx
->dec
.expkey
[j
+ 0] = ctx
->enc
.expkey
[i
+ 0];
372 ctx
->dec
.expkey
[j
+ 1] = tmp
;
378 static int des3_ede_x86_setkey_skcipher(struct crypto_skcipher
*tfm
,
382 return des3_ede_x86_setkey(&tfm
->base
, key
, keylen
);
385 static struct crypto_alg des3_ede_cipher
= {
386 .cra_name
= "des3_ede",
387 .cra_driver_name
= "des3_ede-asm",
389 .cra_flags
= CRYPTO_ALG_TYPE_CIPHER
,
390 .cra_blocksize
= DES3_EDE_BLOCK_SIZE
,
391 .cra_ctxsize
= sizeof(struct des3_ede_x86_ctx
),
393 .cra_module
= THIS_MODULE
,
396 .cia_min_keysize
= DES3_EDE_KEY_SIZE
,
397 .cia_max_keysize
= DES3_EDE_KEY_SIZE
,
398 .cia_setkey
= des3_ede_x86_setkey
,
399 .cia_encrypt
= des3_ede_x86_encrypt
,
400 .cia_decrypt
= des3_ede_x86_decrypt
,
405 static struct skcipher_alg des3_ede_skciphers
[] = {
407 .base
.cra_name
= "ecb(des3_ede)",
408 .base
.cra_driver_name
= "ecb-des3_ede-asm",
409 .base
.cra_priority
= 300,
410 .base
.cra_blocksize
= DES3_EDE_BLOCK_SIZE
,
411 .base
.cra_ctxsize
= sizeof(struct des3_ede_x86_ctx
),
412 .base
.cra_module
= THIS_MODULE
,
413 .min_keysize
= DES3_EDE_KEY_SIZE
,
414 .max_keysize
= DES3_EDE_KEY_SIZE
,
415 .setkey
= des3_ede_x86_setkey_skcipher
,
416 .encrypt
= ecb_encrypt
,
417 .decrypt
= ecb_decrypt
,
419 .base
.cra_name
= "cbc(des3_ede)",
420 .base
.cra_driver_name
= "cbc-des3_ede-asm",
421 .base
.cra_priority
= 300,
422 .base
.cra_blocksize
= DES3_EDE_BLOCK_SIZE
,
423 .base
.cra_ctxsize
= sizeof(struct des3_ede_x86_ctx
),
424 .base
.cra_module
= THIS_MODULE
,
425 .min_keysize
= DES3_EDE_KEY_SIZE
,
426 .max_keysize
= DES3_EDE_KEY_SIZE
,
427 .ivsize
= DES3_EDE_BLOCK_SIZE
,
428 .setkey
= des3_ede_x86_setkey_skcipher
,
429 .encrypt
= cbc_encrypt
,
430 .decrypt
= cbc_decrypt
,
432 .base
.cra_name
= "ctr(des3_ede)",
433 .base
.cra_driver_name
= "ctr-des3_ede-asm",
434 .base
.cra_priority
= 300,
435 .base
.cra_blocksize
= 1,
436 .base
.cra_ctxsize
= sizeof(struct des3_ede_x86_ctx
),
437 .base
.cra_module
= THIS_MODULE
,
438 .min_keysize
= DES3_EDE_KEY_SIZE
,
439 .max_keysize
= DES3_EDE_KEY_SIZE
,
440 .ivsize
= DES3_EDE_BLOCK_SIZE
,
441 .chunksize
= DES3_EDE_BLOCK_SIZE
,
442 .setkey
= des3_ede_x86_setkey_skcipher
,
443 .encrypt
= ctr_crypt
,
444 .decrypt
= ctr_crypt
,
448 static bool is_blacklisted_cpu(void)
450 if (boot_cpu_data
.x86_vendor
!= X86_VENDOR_INTEL
)
453 if (boot_cpu_data
.x86
== 0x0f) {
455 * On Pentium 4, des3_ede-x86_64 is slower than generic C
456 * implementation because use of 64bit rotates (which are really
457 * slow on P4). Therefore blacklist P4s.
466 module_param(force
, int, 0);
467 MODULE_PARM_DESC(force
, "Force module load, ignore CPU blacklist");
469 static int __init
des3_ede_x86_init(void)
473 if (!force
&& is_blacklisted_cpu()) {
474 pr_info("des3_ede-x86_64: performance on this CPU would be suboptimal: disabling des3_ede-x86_64.\n");
478 err
= crypto_register_alg(&des3_ede_cipher
);
482 err
= crypto_register_skciphers(des3_ede_skciphers
,
483 ARRAY_SIZE(des3_ede_skciphers
));
485 crypto_unregister_alg(&des3_ede_cipher
);
490 static void __exit
des3_ede_x86_fini(void)
492 crypto_unregister_alg(&des3_ede_cipher
);
493 crypto_unregister_skciphers(des3_ede_skciphers
,
494 ARRAY_SIZE(des3_ede_skciphers
));
497 module_init(des3_ede_x86_init
);
498 module_exit(des3_ede_x86_fini
);
500 MODULE_LICENSE("GPL");
501 MODULE_DESCRIPTION("Triple DES EDE Cipher Algorithm, asm optimized");
502 MODULE_ALIAS_CRYPTO("des3_ede");
503 MODULE_ALIAS_CRYPTO("des3_ede-asm");
504 MODULE_AUTHOR("Jussi Kivilinna <jussi.kivilinna@iki.fi>");