2 * Glue Code for assembler optimized version of 3DES
4 * Copyright © 2014 Jussi Kivilinna <jussi.kivilinna@mbnet.fi>
6 * CBC & ECB parts based on code (crypto/cbc.c,ecb.c) by:
7 * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
8 * CTR part based on code (crypto/ctr.c) by:
9 * (C) Copyright IBM Corp. 2007 - Joy Latten <latten@us.ibm.com>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
23 #include <crypto/algapi.h>
24 #include <crypto/des.h>
25 #include <crypto/internal/skcipher.h>
26 #include <linux/crypto.h>
27 #include <linux/init.h>
28 #include <linux/module.h>
29 #include <linux/types.h>
31 struct des3_ede_x86_ctx {
32 u32 enc_expkey[DES3_EDE_EXPKEY_WORDS];
33 u32 dec_expkey[DES3_EDE_EXPKEY_WORDS];
36 /* regular block cipher functions */
37 asmlinkage void des3_ede_x86_64_crypt_blk(const u32 *expkey, u8 *dst,
40 /* 3-way parallel cipher functions */
41 asmlinkage void des3_ede_x86_64_crypt_blk_3way(const u32 *expkey, u8 *dst,
44 static inline void des3_ede_enc_blk(struct des3_ede_x86_ctx *ctx, u8 *dst,
47 u32 *enc_ctx = ctx->enc_expkey;
49 des3_ede_x86_64_crypt_blk(enc_ctx, dst, src);
52 static inline void des3_ede_dec_blk(struct des3_ede_x86_ctx *ctx, u8 *dst,
55 u32 *dec_ctx = ctx->dec_expkey;
57 des3_ede_x86_64_crypt_blk(dec_ctx, dst, src);
60 static inline void des3_ede_enc_blk_3way(struct des3_ede_x86_ctx *ctx, u8 *dst,
63 u32 *enc_ctx = ctx->enc_expkey;
65 des3_ede_x86_64_crypt_blk_3way(enc_ctx, dst, src);
68 static inline void des3_ede_dec_blk_3way(struct des3_ede_x86_ctx *ctx, u8 *dst,
71 u32 *dec_ctx = ctx->dec_expkey;
73 des3_ede_x86_64_crypt_blk_3way(dec_ctx, dst, src);
76 static void des3_ede_x86_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
78 des3_ede_enc_blk(crypto_tfm_ctx(tfm), dst, src);
81 static void des3_ede_x86_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
83 des3_ede_dec_blk(crypto_tfm_ctx(tfm), dst, src);
86 static int ecb_crypt(struct skcipher_request *req, const u32 *expkey)
88 const unsigned int bsize = DES3_EDE_BLOCK_SIZE;
89 struct skcipher_walk walk;
93 err = skcipher_walk_virt(&walk, req, false);
95 while ((nbytes = walk.nbytes)) {
96 u8 *wsrc = walk.src.virt.addr;
97 u8 *wdst = walk.dst.virt.addr;
99 /* Process four block batch */
100 if (nbytes >= bsize * 3) {
102 des3_ede_x86_64_crypt_blk_3way(expkey, wdst,
108 } while (nbytes >= bsize * 3);
114 /* Handle leftovers */
116 des3_ede_x86_64_crypt_blk(expkey, wdst, wsrc);
121 } while (nbytes >= bsize);
124 err = skcipher_walk_done(&walk, nbytes);
130 static int ecb_encrypt(struct skcipher_request *req)
132 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
133 struct des3_ede_x86_ctx *ctx = crypto_skcipher_ctx(tfm);
135 return ecb_crypt(req, ctx->enc_expkey);
138 static int ecb_decrypt(struct skcipher_request *req)
140 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
141 struct des3_ede_x86_ctx *ctx = crypto_skcipher_ctx(tfm);
143 return ecb_crypt(req, ctx->dec_expkey);
146 static unsigned int __cbc_encrypt(struct des3_ede_x86_ctx *ctx,
147 struct skcipher_walk *walk)
149 unsigned int bsize = DES3_EDE_BLOCK_SIZE;
150 unsigned int nbytes = walk->nbytes;
151 u64 *src = (u64 *)walk->src.virt.addr;
152 u64 *dst = (u64 *)walk->dst.virt.addr;
153 u64 *iv = (u64 *)walk->iv;
157 des3_ede_enc_blk(ctx, (u8 *)dst, (u8 *)dst);
163 } while (nbytes >= bsize);
165 *(u64 *)walk->iv = *iv;
169 static int cbc_encrypt(struct skcipher_request *req)
171 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
172 struct des3_ede_x86_ctx *ctx = crypto_skcipher_ctx(tfm);
173 struct skcipher_walk walk;
177 err = skcipher_walk_virt(&walk, req, false);
179 while ((nbytes = walk.nbytes)) {
180 nbytes = __cbc_encrypt(ctx, &walk);
181 err = skcipher_walk_done(&walk, nbytes);
187 static unsigned int __cbc_decrypt(struct des3_ede_x86_ctx *ctx,
188 struct skcipher_walk *walk)
190 unsigned int bsize = DES3_EDE_BLOCK_SIZE;
191 unsigned int nbytes = walk->nbytes;
192 u64 *src = (u64 *)walk->src.virt.addr;
193 u64 *dst = (u64 *)walk->dst.virt.addr;
197 /* Start of the last block. */
198 src += nbytes / bsize - 1;
199 dst += nbytes / bsize - 1;
203 /* Process four block batch */
204 if (nbytes >= bsize * 3) {
206 nbytes -= bsize * 3 - bsize;
213 des3_ede_dec_blk_3way(ctx, (u8 *)dst, (u8 *)src);
225 } while (nbytes >= bsize * 3);
228 /* Handle leftovers */
230 des3_ede_dec_blk(ctx, (u8 *)dst, (u8 *)src);
242 *dst ^= *(u64 *)walk->iv;
243 *(u64 *)walk->iv = last_iv;
248 static int cbc_decrypt(struct skcipher_request *req)
250 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
251 struct des3_ede_x86_ctx *ctx = crypto_skcipher_ctx(tfm);
252 struct skcipher_walk walk;
256 err = skcipher_walk_virt(&walk, req, false);
258 while ((nbytes = walk.nbytes)) {
259 nbytes = __cbc_decrypt(ctx, &walk);
260 err = skcipher_walk_done(&walk, nbytes);
266 static void ctr_crypt_final(struct des3_ede_x86_ctx *ctx,
267 struct skcipher_walk *walk)
269 u8 *ctrblk = walk->iv;
270 u8 keystream[DES3_EDE_BLOCK_SIZE];
271 u8 *src = walk->src.virt.addr;
272 u8 *dst = walk->dst.virt.addr;
273 unsigned int nbytes = walk->nbytes;
275 des3_ede_enc_blk(ctx, keystream, ctrblk);
276 crypto_xor_cpy(dst, keystream, src, nbytes);
278 crypto_inc(ctrblk, DES3_EDE_BLOCK_SIZE);
281 static unsigned int __ctr_crypt(struct des3_ede_x86_ctx *ctx,
282 struct skcipher_walk *walk)
284 unsigned int bsize = DES3_EDE_BLOCK_SIZE;
285 unsigned int nbytes = walk->nbytes;
286 __be64 *src = (__be64 *)walk->src.virt.addr;
287 __be64 *dst = (__be64 *)walk->dst.virt.addr;
288 u64 ctrblk = be64_to_cpu(*(__be64 *)walk->iv);
291 /* Process four block batch */
292 if (nbytes >= bsize * 3) {
294 /* create ctrblks for parallel encrypt */
295 ctrblocks[0] = cpu_to_be64(ctrblk++);
296 ctrblocks[1] = cpu_to_be64(ctrblk++);
297 ctrblocks[2] = cpu_to_be64(ctrblk++);
299 des3_ede_enc_blk_3way(ctx, (u8 *)ctrblocks,
302 dst[0] = src[0] ^ ctrblocks[0];
303 dst[1] = src[1] ^ ctrblocks[1];
304 dst[2] = src[2] ^ ctrblocks[2];
308 } while ((nbytes -= bsize * 3) >= bsize * 3);
314 /* Handle leftovers */
316 ctrblocks[0] = cpu_to_be64(ctrblk++);
318 des3_ede_enc_blk(ctx, (u8 *)ctrblocks, (u8 *)ctrblocks);
320 dst[0] = src[0] ^ ctrblocks[0];
324 } while ((nbytes -= bsize) >= bsize);
327 *(__be64 *)walk->iv = cpu_to_be64(ctrblk);
331 static int ctr_crypt(struct skcipher_request *req)
333 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
334 struct des3_ede_x86_ctx *ctx = crypto_skcipher_ctx(tfm);
335 struct skcipher_walk walk;
339 err = skcipher_walk_virt(&walk, req, false);
341 while ((nbytes = walk.nbytes) >= DES3_EDE_BLOCK_SIZE) {
342 nbytes = __ctr_crypt(ctx, &walk);
343 err = skcipher_walk_done(&walk, nbytes);
347 ctr_crypt_final(ctx, &walk);
348 err = skcipher_walk_done(&walk, 0);
354 static int des3_ede_x86_setkey(struct crypto_tfm *tfm, const u8 *key,
357 struct des3_ede_x86_ctx *ctx = crypto_tfm_ctx(tfm);
361 /* Generate encryption context using generic implementation. */
362 err = __des3_ede_setkey(ctx->enc_expkey, &tfm->crt_flags, key, keylen);
366 /* Fix encryption context for this implementation and form decryption
368 j = DES3_EDE_EXPKEY_WORDS - 2;
369 for (i = 0; i < DES3_EDE_EXPKEY_WORDS; i += 2, j -= 2) {
370 tmp = ror32(ctx->enc_expkey[i + 1], 4);
371 ctx->enc_expkey[i + 1] = tmp;
373 ctx->dec_expkey[j + 0] = ctx->enc_expkey[i + 0];
374 ctx->dec_expkey[j + 1] = tmp;
380 static int des3_ede_x86_setkey_skcipher(struct crypto_skcipher *tfm,
384 return des3_ede_x86_setkey(&tfm->base, key, keylen);
387 static struct crypto_alg des3_ede_cipher = {
388 .cra_name = "des3_ede",
389 .cra_driver_name = "des3_ede-asm",
391 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
392 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
393 .cra_ctxsize = sizeof(struct des3_ede_x86_ctx),
395 .cra_module = THIS_MODULE,
398 .cia_min_keysize = DES3_EDE_KEY_SIZE,
399 .cia_max_keysize = DES3_EDE_KEY_SIZE,
400 .cia_setkey = des3_ede_x86_setkey,
401 .cia_encrypt = des3_ede_x86_encrypt,
402 .cia_decrypt = des3_ede_x86_decrypt,
407 static struct skcipher_alg des3_ede_skciphers[] = {
409 .base.cra_name = "ecb(des3_ede)",
410 .base.cra_driver_name = "ecb-des3_ede-asm",
411 .base.cra_priority = 300,
412 .base.cra_blocksize = DES3_EDE_BLOCK_SIZE,
413 .base.cra_ctxsize = sizeof(struct des3_ede_x86_ctx),
414 .base.cra_module = THIS_MODULE,
415 .min_keysize = DES3_EDE_KEY_SIZE,
416 .max_keysize = DES3_EDE_KEY_SIZE,
417 .setkey = des3_ede_x86_setkey_skcipher,
418 .encrypt = ecb_encrypt,
419 .decrypt = ecb_decrypt,
421 .base.cra_name = "cbc(des3_ede)",
422 .base.cra_driver_name = "cbc-des3_ede-asm",
423 .base.cra_priority = 300,
424 .base.cra_blocksize = DES3_EDE_BLOCK_SIZE,
425 .base.cra_ctxsize = sizeof(struct des3_ede_x86_ctx),
426 .base.cra_module = THIS_MODULE,
427 .min_keysize = DES3_EDE_KEY_SIZE,
428 .max_keysize = DES3_EDE_KEY_SIZE,
429 .ivsize = DES3_EDE_BLOCK_SIZE,
430 .setkey = des3_ede_x86_setkey_skcipher,
431 .encrypt = cbc_encrypt,
432 .decrypt = cbc_decrypt,
434 .base.cra_name = "ctr(des3_ede)",
435 .base.cra_driver_name = "ctr-des3_ede-asm",
436 .base.cra_priority = 300,
437 .base.cra_blocksize = 1,
438 .base.cra_ctxsize = sizeof(struct des3_ede_x86_ctx),
439 .base.cra_module = THIS_MODULE,
440 .min_keysize = DES3_EDE_KEY_SIZE,
441 .max_keysize = DES3_EDE_KEY_SIZE,
442 .ivsize = DES3_EDE_BLOCK_SIZE,
443 .chunksize = DES3_EDE_BLOCK_SIZE,
444 .setkey = des3_ede_x86_setkey_skcipher,
445 .encrypt = ctr_crypt,
446 .decrypt = ctr_crypt,
450 static bool is_blacklisted_cpu(void)
452 if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL)
455 if (boot_cpu_data.x86 == 0x0f) {
457 * On Pentium 4, des3_ede-x86_64 is slower than generic C
458 * implementation because use of 64bit rotates (which are really
459 * slow on P4). Therefore blacklist P4s.
468 module_param(force, int, 0);
469 MODULE_PARM_DESC(force, "Force module load, ignore CPU blacklist");
471 static int __init des3_ede_x86_init(void)
475 if (!force && is_blacklisted_cpu()) {
476 pr_info("des3_ede-x86_64: performance on this CPU would be suboptimal: disabling des3_ede-x86_64.\n");
480 err = crypto_register_alg(&des3_ede_cipher);
484 err = crypto_register_skciphers(des3_ede_skciphers,
485 ARRAY_SIZE(des3_ede_skciphers));
487 crypto_unregister_alg(&des3_ede_cipher);
492 static void __exit des3_ede_x86_fini(void)
494 crypto_unregister_alg(&des3_ede_cipher);
495 crypto_unregister_skciphers(des3_ede_skciphers,
496 ARRAY_SIZE(des3_ede_skciphers));
499 module_init(des3_ede_x86_init);
500 module_exit(des3_ede_x86_fini);
502 MODULE_LICENSE("GPL");
503 MODULE_DESCRIPTION("Triple DES EDE Cipher Algorithm, asm optimized");
504 MODULE_ALIAS_CRYPTO("des3_ede");
505 MODULE_ALIAS_CRYPTO("des3_ede-asm");
506 MODULE_AUTHOR("Jussi Kivilinna <jussi.kivilinna@iki.fi>");