hugetlb: introduce generic version of hugetlb_free_pgd_range
[linux/fpc-iii.git] / arch / x86 / crypto / des3_ede_glue.c
blob5c610d4ef9fc1a22f423469cce7cd88a607ca21d
1 /*
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,
38 const u8 *src);
40 /* 3-way parallel cipher functions */
41 asmlinkage void des3_ede_x86_64_crypt_blk_3way(const u32 *expkey, u8 *dst,
42 const u8 *src);
44 static inline void des3_ede_enc_blk(struct des3_ede_x86_ctx *ctx, u8 *dst,
45 const u8 *src)
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,
53 const u8 *src)
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,
61 const u8 *src)
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,
69 const u8 *src)
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;
90 unsigned int nbytes;
91 int err;
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) {
101 do {
102 des3_ede_x86_64_crypt_blk_3way(expkey, wdst,
103 wsrc);
105 wsrc += bsize * 3;
106 wdst += bsize * 3;
107 nbytes -= bsize * 3;
108 } while (nbytes >= bsize * 3);
110 if (nbytes < bsize)
111 goto done;
114 /* Handle leftovers */
115 do {
116 des3_ede_x86_64_crypt_blk(expkey, wdst, wsrc);
118 wsrc += bsize;
119 wdst += bsize;
120 nbytes -= bsize;
121 } while (nbytes >= bsize);
123 done:
124 err = skcipher_walk_done(&walk, nbytes);
127 return err;
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;
155 do {
156 *dst = *src ^ *iv;
157 des3_ede_enc_blk(ctx, (u8 *)dst, (u8 *)dst);
158 iv = dst;
160 src += 1;
161 dst += 1;
162 nbytes -= bsize;
163 } while (nbytes >= bsize);
165 *(u64 *)walk->iv = *iv;
166 return nbytes;
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;
174 unsigned int nbytes;
175 int err;
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);
184 return err;
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;
194 u64 ivs[3 - 1];
195 u64 last_iv;
197 /* Start of the last block. */
198 src += nbytes / bsize - 1;
199 dst += nbytes / bsize - 1;
201 last_iv = *src;
203 /* Process four block batch */
204 if (nbytes >= bsize * 3) {
205 do {
206 nbytes -= bsize * 3 - bsize;
207 src -= 3 - 1;
208 dst -= 3 - 1;
210 ivs[0] = src[0];
211 ivs[1] = src[1];
213 des3_ede_dec_blk_3way(ctx, (u8 *)dst, (u8 *)src);
215 dst[1] ^= ivs[0];
216 dst[2] ^= ivs[1];
218 nbytes -= bsize;
219 if (nbytes < bsize)
220 goto done;
222 *dst ^= *(src - 1);
223 src -= 1;
224 dst -= 1;
225 } while (nbytes >= bsize * 3);
228 /* Handle leftovers */
229 for (;;) {
230 des3_ede_dec_blk(ctx, (u8 *)dst, (u8 *)src);
232 nbytes -= bsize;
233 if (nbytes < bsize)
234 break;
236 *dst ^= *(src - 1);
237 src -= 1;
238 dst -= 1;
241 done:
242 *dst ^= *(u64 *)walk->iv;
243 *(u64 *)walk->iv = last_iv;
245 return nbytes;
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;
253 unsigned int nbytes;
254 int err;
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);
263 return err;
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);
289 __be64 ctrblocks[3];
291 /* Process four block batch */
292 if (nbytes >= bsize * 3) {
293 do {
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,
300 (u8 *)ctrblocks);
302 dst[0] = src[0] ^ ctrblocks[0];
303 dst[1] = src[1] ^ ctrblocks[1];
304 dst[2] = src[2] ^ ctrblocks[2];
306 src += 3;
307 dst += 3;
308 } while ((nbytes -= bsize * 3) >= bsize * 3);
310 if (nbytes < bsize)
311 goto done;
314 /* Handle leftovers */
315 do {
316 ctrblocks[0] = cpu_to_be64(ctrblk++);
318 des3_ede_enc_blk(ctx, (u8 *)ctrblocks, (u8 *)ctrblocks);
320 dst[0] = src[0] ^ ctrblocks[0];
322 src += 1;
323 dst += 1;
324 } while ((nbytes -= bsize) >= bsize);
326 done:
327 *(__be64 *)walk->iv = cpu_to_be64(ctrblk);
328 return nbytes;
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;
336 unsigned int nbytes;
337 int err;
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);
346 if (nbytes) {
347 ctr_crypt_final(ctx, &walk);
348 err = skcipher_walk_done(&walk, 0);
351 return err;
354 static int des3_ede_x86_setkey(struct crypto_tfm *tfm, const u8 *key,
355 unsigned int keylen)
357 struct des3_ede_x86_ctx *ctx = crypto_tfm_ctx(tfm);
358 u32 i, j, tmp;
359 int err;
361 /* Generate encryption context using generic implementation. */
362 err = __des3_ede_setkey(ctx->enc_expkey, &tfm->crt_flags, key, keylen);
363 if (err < 0)
364 return err;
366 /* Fix encryption context for this implementation and form decryption
367 * context. */
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;
377 return 0;
380 static int des3_ede_x86_setkey_skcipher(struct crypto_skcipher *tfm,
381 const u8 *key,
382 unsigned int keylen)
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",
390 .cra_priority = 200,
391 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
392 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
393 .cra_ctxsize = sizeof(struct des3_ede_x86_ctx),
394 .cra_alignmask = 0,
395 .cra_module = THIS_MODULE,
396 .cra_u = {
397 .cipher = {
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,
420 }, {
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,
433 }, {
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)
453 return false;
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.
461 return true;
464 return false;
467 static int force;
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)
473 int err;
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");
477 return -ENODEV;
480 err = crypto_register_alg(&des3_ede_cipher);
481 if (err)
482 return err;
484 err = crypto_register_skciphers(des3_ede_skciphers,
485 ARRAY_SIZE(des3_ede_skciphers));
486 if (err)
487 crypto_unregister_alg(&des3_ede_cipher);
489 return err;
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>");