Thomas Gleixner | 1a59d1b8 | 2019-05-27 08:55:05 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Jussi Kivilinna | 8280daa | 2011-09-26 16:47:25 +0300 | [diff] [blame] | 2 | /* |
| 3 | * Glue Code for 3-way parallel assembler optimized version of Twofish |
| 4 | * |
| 5 | * Copyright (c) 2011 Jussi Kivilinna <jussi.kivilinna@mbnet.fi> |
Jussi Kivilinna | 8280daa | 2011-09-26 16:47:25 +0300 | [diff] [blame] | 6 | */ |
| 7 | |
Eric Biggers | 37992fa | 2018-02-19 23:48:09 -0800 | [diff] [blame] | 8 | #include <crypto/algapi.h> |
Eric Biggers | 37992fa | 2018-02-19 23:48:09 -0800 | [diff] [blame] | 9 | #include <crypto/twofish.h> |
Jussi Kivilinna | 8280daa | 2011-09-26 16:47:25 +0300 | [diff] [blame] | 10 | #include <linux/crypto.h> |
| 11 | #include <linux/init.h> |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/types.h> |
Jussi Kivilinna | 81559f9 | 2011-10-18 13:33:02 +0300 | [diff] [blame] | 14 | |
Ard Biesheuvel | a04ea6f | 2021-01-05 17:48:09 +0100 | [diff] [blame] | 15 | #include "twofish.h" |
Ard Biesheuvel | 165f357 | 2021-01-05 17:48:07 +0100 | [diff] [blame] | 16 | #include "ecb_cbc_helpers.h" |
| 17 | |
Johannes Goetzfried | 107778b5 | 2012-05-28 15:54:24 +0200 | [diff] [blame] | 18 | EXPORT_SYMBOL_GPL(__twofish_enc_blk_3way); |
Johannes Goetzfried | 107778b5 | 2012-05-28 15:54:24 +0200 | [diff] [blame] | 19 | EXPORT_SYMBOL_GPL(twofish_dec_blk_3way); |
Jussi Kivilinna | 8280daa | 2011-09-26 16:47:25 +0300 | [diff] [blame] | 20 | |
Eric Biggers | 37992fa | 2018-02-19 23:48:09 -0800 | [diff] [blame] | 21 | static int twofish_setkey_skcipher(struct crypto_skcipher *tfm, |
| 22 | const u8 *key, unsigned int keylen) |
| 23 | { |
| 24 | return twofish_setkey(&tfm->base, key, keylen); |
| 25 | } |
| 26 | |
Kees Cook | 9c1e883 | 2019-11-26 22:08:02 -0800 | [diff] [blame] | 27 | static inline void twofish_enc_blk_3way(const void *ctx, u8 *dst, const u8 *src) |
Jussi Kivilinna | 8280daa | 2011-09-26 16:47:25 +0300 | [diff] [blame] | 28 | { |
| 29 | __twofish_enc_blk_3way(ctx, dst, src, false); |
| 30 | } |
| 31 | |
Ard Biesheuvel | 165f357 | 2021-01-05 17:48:07 +0100 | [diff] [blame] | 32 | void twofish_dec_blk_cbc_3way(const void *ctx, u8 *dst, const u8 *src) |
Jussi Kivilinna | 8280daa | 2011-09-26 16:47:25 +0300 | [diff] [blame] | 33 | { |
Ard Biesheuvel | 165f357 | 2021-01-05 17:48:07 +0100 | [diff] [blame] | 34 | u8 buf[2][TF_BLOCK_SIZE]; |
| 35 | const u8 *s = src; |
Jussi Kivilinna | 8280daa | 2011-09-26 16:47:25 +0300 | [diff] [blame] | 36 | |
Ard Biesheuvel | 165f357 | 2021-01-05 17:48:07 +0100 | [diff] [blame] | 37 | if (dst == src) |
| 38 | s = memcpy(buf, src, sizeof(buf)); |
| 39 | twofish_dec_blk_3way(ctx, dst, src); |
| 40 | crypto_xor(dst + TF_BLOCK_SIZE, s, sizeof(buf)); |
Jussi Kivilinna | 8280daa | 2011-09-26 16:47:25 +0300 | [diff] [blame] | 41 | |
Jussi Kivilinna | 414cb5e | 2012-06-18 14:07:34 +0300 | [diff] [blame] | 42 | } |
Jussi Kivilinna | a7378d4 | 2012-06-18 14:07:39 +0300 | [diff] [blame] | 43 | EXPORT_SYMBOL_GPL(twofish_dec_blk_cbc_3way); |
Jussi Kivilinna | 8280daa | 2011-09-26 16:47:25 +0300 | [diff] [blame] | 44 | |
Eric Biggers | 37992fa | 2018-02-19 23:48:09 -0800 | [diff] [blame] | 45 | static int ecb_encrypt(struct skcipher_request *req) |
Jussi Kivilinna | 8280daa | 2011-09-26 16:47:25 +0300 | [diff] [blame] | 46 | { |
Ard Biesheuvel | 165f357 | 2021-01-05 17:48:07 +0100 | [diff] [blame] | 47 | ECB_WALK_START(req, TF_BLOCK_SIZE, -1); |
| 48 | ECB_BLOCK(3, twofish_enc_blk_3way); |
| 49 | ECB_BLOCK(1, twofish_enc_blk); |
| 50 | ECB_WALK_END(); |
Jussi Kivilinna | 8280daa | 2011-09-26 16:47:25 +0300 | [diff] [blame] | 51 | } |
| 52 | |
Eric Biggers | 37992fa | 2018-02-19 23:48:09 -0800 | [diff] [blame] | 53 | static int ecb_decrypt(struct skcipher_request *req) |
Jussi Kivilinna | 8280daa | 2011-09-26 16:47:25 +0300 | [diff] [blame] | 54 | { |
Ard Biesheuvel | 165f357 | 2021-01-05 17:48:07 +0100 | [diff] [blame] | 55 | ECB_WALK_START(req, TF_BLOCK_SIZE, -1); |
| 56 | ECB_BLOCK(3, twofish_dec_blk_3way); |
| 57 | ECB_BLOCK(1, twofish_dec_blk); |
| 58 | ECB_WALK_END(); |
Jussi Kivilinna | 8280daa | 2011-09-26 16:47:25 +0300 | [diff] [blame] | 59 | } |
| 60 | |
Eric Biggers | 37992fa | 2018-02-19 23:48:09 -0800 | [diff] [blame] | 61 | static int cbc_encrypt(struct skcipher_request *req) |
Jussi Kivilinna | 8280daa | 2011-09-26 16:47:25 +0300 | [diff] [blame] | 62 | { |
Ard Biesheuvel | 165f357 | 2021-01-05 17:48:07 +0100 | [diff] [blame] | 63 | CBC_WALK_START(req, TF_BLOCK_SIZE, -1); |
| 64 | CBC_ENC_BLOCK(twofish_enc_blk); |
| 65 | CBC_WALK_END(); |
Jussi Kivilinna | 8280daa | 2011-09-26 16:47:25 +0300 | [diff] [blame] | 66 | } |
| 67 | |
Eric Biggers | 37992fa | 2018-02-19 23:48:09 -0800 | [diff] [blame] | 68 | static int cbc_decrypt(struct skcipher_request *req) |
Jussi Kivilinna | 8280daa | 2011-09-26 16:47:25 +0300 | [diff] [blame] | 69 | { |
Ard Biesheuvel | 165f357 | 2021-01-05 17:48:07 +0100 | [diff] [blame] | 70 | CBC_WALK_START(req, TF_BLOCK_SIZE, -1); |
| 71 | CBC_DEC_BLOCK(3, twofish_dec_blk_cbc_3way); |
| 72 | CBC_DEC_BLOCK(1, twofish_dec_blk); |
| 73 | CBC_WALK_END(); |
Jussi Kivilinna | 8280daa | 2011-09-26 16:47:25 +0300 | [diff] [blame] | 74 | } |
| 75 | |
Eric Biggers | 37992fa | 2018-02-19 23:48:09 -0800 | [diff] [blame] | 76 | static struct skcipher_alg tf_skciphers[] = { |
| 77 | { |
| 78 | .base.cra_name = "ecb(twofish)", |
| 79 | .base.cra_driver_name = "ecb-twofish-3way", |
| 80 | .base.cra_priority = 300, |
| 81 | .base.cra_blocksize = TF_BLOCK_SIZE, |
| 82 | .base.cra_ctxsize = sizeof(struct twofish_ctx), |
| 83 | .base.cra_module = THIS_MODULE, |
| 84 | .min_keysize = TF_MIN_KEY_SIZE, |
| 85 | .max_keysize = TF_MAX_KEY_SIZE, |
| 86 | .setkey = twofish_setkey_skcipher, |
| 87 | .encrypt = ecb_encrypt, |
| 88 | .decrypt = ecb_decrypt, |
| 89 | }, { |
| 90 | .base.cra_name = "cbc(twofish)", |
| 91 | .base.cra_driver_name = "cbc-twofish-3way", |
| 92 | .base.cra_priority = 300, |
| 93 | .base.cra_blocksize = TF_BLOCK_SIZE, |
| 94 | .base.cra_ctxsize = sizeof(struct twofish_ctx), |
| 95 | .base.cra_module = THIS_MODULE, |
| 96 | .min_keysize = TF_MIN_KEY_SIZE, |
| 97 | .max_keysize = TF_MAX_KEY_SIZE, |
| 98 | .ivsize = TF_BLOCK_SIZE, |
| 99 | .setkey = twofish_setkey_skcipher, |
| 100 | .encrypt = cbc_encrypt, |
| 101 | .decrypt = cbc_decrypt, |
Jussi Kivilinna | 53709dd | 2012-02-17 22:48:43 +0200 | [diff] [blame] | 102 | }, |
Eric Biggers | 37992fa | 2018-02-19 23:48:09 -0800 | [diff] [blame] | 103 | }; |
Jussi Kivilinna | bae6d30 | 2011-10-18 13:33:43 +0300 | [diff] [blame] | 104 | |
Jussi Kivilinna | a522ee8 | 2011-12-20 12:20:16 +0200 | [diff] [blame] | 105 | static bool is_blacklisted_cpu(void) |
| 106 | { |
| 107 | if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL) |
| 108 | return false; |
| 109 | |
| 110 | if (boot_cpu_data.x86 == 0x06 && |
| 111 | (boot_cpu_data.x86_model == 0x1c || |
| 112 | boot_cpu_data.x86_model == 0x26 || |
| 113 | boot_cpu_data.x86_model == 0x36)) { |
| 114 | /* |
| 115 | * On Atom, twofish-3way is slower than original assembler |
| 116 | * implementation. Twofish-3way trades off some performance in |
| 117 | * storing blocks in 64bit registers to allow three blocks to |
| 118 | * be processed parallel. Parallel operation then allows gaining |
| 119 | * more performance than was trade off, on out-of-order CPUs. |
| 120 | * However Atom does not benefit from this parallellism and |
| 121 | * should be blacklisted. |
| 122 | */ |
| 123 | return true; |
| 124 | } |
| 125 | |
| 126 | if (boot_cpu_data.x86 == 0x0f) { |
| 127 | /* |
| 128 | * On Pentium 4, twofish-3way is slower than original assembler |
| 129 | * implementation because excessive uses of 64bit rotate and |
| 130 | * left-shifts (which are really slow on P4) needed to store and |
| 131 | * handle 128bit block in two 64bit registers. |
| 132 | */ |
| 133 | return true; |
| 134 | } |
| 135 | |
| 136 | return false; |
| 137 | } |
| 138 | |
| 139 | static int force; |
| 140 | module_param(force, int, 0); |
| 141 | MODULE_PARM_DESC(force, "Force module load, ignore CPU blacklist"); |
| 142 | |
Jussi Kivilinna | ff0a70f | 2012-03-15 22:11:57 +0200 | [diff] [blame] | 143 | static int __init init(void) |
Jussi Kivilinna | 8280daa | 2011-09-26 16:47:25 +0300 | [diff] [blame] | 144 | { |
Jussi Kivilinna | a522ee8 | 2011-12-20 12:20:16 +0200 | [diff] [blame] | 145 | if (!force && is_blacklisted_cpu()) { |
| 146 | printk(KERN_INFO |
| 147 | "twofish-x86_64-3way: performance on this CPU " |
| 148 | "would be suboptimal: disabling " |
| 149 | "twofish-x86_64-3way.\n"); |
| 150 | return -ENODEV; |
| 151 | } |
| 152 | |
Eric Biggers | 37992fa | 2018-02-19 23:48:09 -0800 | [diff] [blame] | 153 | return crypto_register_skciphers(tf_skciphers, |
| 154 | ARRAY_SIZE(tf_skciphers)); |
Jussi Kivilinna | 8280daa | 2011-09-26 16:47:25 +0300 | [diff] [blame] | 155 | } |
| 156 | |
Jussi Kivilinna | ff0a70f | 2012-03-15 22:11:57 +0200 | [diff] [blame] | 157 | static void __exit fini(void) |
Jussi Kivilinna | 8280daa | 2011-09-26 16:47:25 +0300 | [diff] [blame] | 158 | { |
Eric Biggers | 37992fa | 2018-02-19 23:48:09 -0800 | [diff] [blame] | 159 | crypto_unregister_skciphers(tf_skciphers, ARRAY_SIZE(tf_skciphers)); |
Jussi Kivilinna | 8280daa | 2011-09-26 16:47:25 +0300 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | module_init(init); |
| 163 | module_exit(fini); |
| 164 | |
| 165 | MODULE_LICENSE("GPL"); |
| 166 | MODULE_DESCRIPTION("Twofish Cipher Algorithm, 3-way parallel asm optimized"); |
Kees Cook | 5d26a10 | 2014-11-20 17:05:53 -0800 | [diff] [blame] | 167 | MODULE_ALIAS_CRYPTO("twofish"); |
| 168 | MODULE_ALIAS_CRYPTO("twofish-asm"); |