| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * Glue Code for AVX assembler version of Twofish Cipher |
|---|
| 3 | 4 | * |
|---|
| .. | .. |
|---|
| 5 | 6 | * <Johannes.Goetzfried@informatik.stud.uni-erlangen.de> |
|---|
| 6 | 7 | * |
|---|
| 7 | 8 | * Copyright © 2013 Jussi Kivilinna <jussi.kivilinna@iki.fi> |
|---|
| 8 | | - * |
|---|
| 9 | | - * This program is free software; you can redistribute it and/or modify |
|---|
| 10 | | - * it under the terms of the GNU General Public License as published by |
|---|
| 11 | | - * the Free Software Foundation; either version 2 of the License, or |
|---|
| 12 | | - * (at your option) any later version. |
|---|
| 13 | | - * |
|---|
| 14 | | - * This program is distributed in the hope that it will be useful, |
|---|
| 15 | | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 16 | | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 17 | | - * GNU General Public License for more details. |
|---|
| 18 | | - * |
|---|
| 19 | | - * You should have received a copy of the GNU General Public License |
|---|
| 20 | | - * along with this program; if not, write to the Free Software |
|---|
| 21 | | - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
|---|
| 22 | | - * USA |
|---|
| 23 | | - * |
|---|
| 24 | 9 | */ |
|---|
| 25 | 10 | |
|---|
| 26 | 11 | #include <linux/module.h> |
|---|
| .. | .. |
|---|
| 37 | 22 | #define TWOFISH_PARALLEL_BLOCKS 8 |
|---|
| 38 | 23 | |
|---|
| 39 | 24 | /* 8-way parallel cipher functions */ |
|---|
| 40 | | -asmlinkage void twofish_ecb_enc_8way(struct twofish_ctx *ctx, u8 *dst, |
|---|
| 41 | | - const u8 *src); |
|---|
| 42 | | -asmlinkage void twofish_ecb_dec_8way(struct twofish_ctx *ctx, u8 *dst, |
|---|
| 43 | | - const u8 *src); |
|---|
| 25 | +asmlinkage void twofish_ecb_enc_8way(const void *ctx, u8 *dst, const u8 *src); |
|---|
| 26 | +asmlinkage void twofish_ecb_dec_8way(const void *ctx, u8 *dst, const u8 *src); |
|---|
| 44 | 27 | |
|---|
| 45 | | -asmlinkage void twofish_cbc_dec_8way(struct twofish_ctx *ctx, u8 *dst, |
|---|
| 46 | | - const u8 *src); |
|---|
| 47 | | -asmlinkage void twofish_ctr_8way(struct twofish_ctx *ctx, u8 *dst, |
|---|
| 48 | | - const u8 *src, le128 *iv); |
|---|
| 28 | +asmlinkage void twofish_cbc_dec_8way(const void *ctx, u8 *dst, const u8 *src); |
|---|
| 29 | +asmlinkage void twofish_ctr_8way(const void *ctx, u8 *dst, const u8 *src, |
|---|
| 30 | + le128 *iv); |
|---|
| 49 | 31 | |
|---|
| 50 | | -asmlinkage void twofish_xts_enc_8way(struct twofish_ctx *ctx, u8 *dst, |
|---|
| 51 | | - const u8 *src, le128 *iv); |
|---|
| 52 | | -asmlinkage void twofish_xts_dec_8way(struct twofish_ctx *ctx, u8 *dst, |
|---|
| 53 | | - const u8 *src, le128 *iv); |
|---|
| 32 | +asmlinkage void twofish_xts_enc_8way(const void *ctx, u8 *dst, const u8 *src, |
|---|
| 33 | + le128 *iv); |
|---|
| 34 | +asmlinkage void twofish_xts_dec_8way(const void *ctx, u8 *dst, const u8 *src, |
|---|
| 35 | + le128 *iv); |
|---|
| 54 | 36 | |
|---|
| 55 | 37 | static int twofish_setkey_skcipher(struct crypto_skcipher *tfm, |
|---|
| 56 | 38 | const u8 *key, unsigned int keylen) |
|---|
| .. | .. |
|---|
| 58 | 40 | return twofish_setkey(&tfm->base, key, keylen); |
|---|
| 59 | 41 | } |
|---|
| 60 | 42 | |
|---|
| 61 | | -static inline void twofish_enc_blk_3way(struct twofish_ctx *ctx, u8 *dst, |
|---|
| 62 | | - const u8 *src) |
|---|
| 43 | +static inline void twofish_enc_blk_3way(const void *ctx, u8 *dst, const u8 *src) |
|---|
| 63 | 44 | { |
|---|
| 64 | 45 | __twofish_enc_blk_3way(ctx, dst, src, false); |
|---|
| 65 | 46 | } |
|---|
| 66 | 47 | |
|---|
| 67 | | -static void twofish_xts_enc(void *ctx, u128 *dst, const u128 *src, le128 *iv) |
|---|
| 48 | +static void twofish_xts_enc(const void *ctx, u8 *dst, const u8 *src, le128 *iv) |
|---|
| 68 | 49 | { |
|---|
| 69 | | - glue_xts_crypt_128bit_one(ctx, dst, src, iv, |
|---|
| 70 | | - GLUE_FUNC_CAST(twofish_enc_blk)); |
|---|
| 50 | + glue_xts_crypt_128bit_one(ctx, dst, src, iv, twofish_enc_blk); |
|---|
| 71 | 51 | } |
|---|
| 72 | 52 | |
|---|
| 73 | | -static void twofish_xts_dec(void *ctx, u128 *dst, const u128 *src, le128 *iv) |
|---|
| 53 | +static void twofish_xts_dec(const void *ctx, u8 *dst, const u8 *src, le128 *iv) |
|---|
| 74 | 54 | { |
|---|
| 75 | | - glue_xts_crypt_128bit_one(ctx, dst, src, iv, |
|---|
| 76 | | - GLUE_FUNC_CAST(twofish_dec_blk)); |
|---|
| 55 | + glue_xts_crypt_128bit_one(ctx, dst, src, iv, twofish_dec_blk); |
|---|
| 77 | 56 | } |
|---|
| 78 | 57 | |
|---|
| 79 | 58 | struct twofish_xts_ctx { |
|---|
| .. | .. |
|---|
| 85 | 64 | unsigned int keylen) |
|---|
| 86 | 65 | { |
|---|
| 87 | 66 | struct twofish_xts_ctx *ctx = crypto_skcipher_ctx(tfm); |
|---|
| 88 | | - u32 *flags = &tfm->base.crt_flags; |
|---|
| 89 | 67 | int err; |
|---|
| 90 | 68 | |
|---|
| 91 | 69 | err = xts_verify_key(tfm, key, keylen); |
|---|
| .. | .. |
|---|
| 93 | 71 | return err; |
|---|
| 94 | 72 | |
|---|
| 95 | 73 | /* first half of xts-key is for crypt */ |
|---|
| 96 | | - err = __twofish_setkey(&ctx->crypt_ctx, key, keylen / 2, flags); |
|---|
| 74 | + err = __twofish_setkey(&ctx->crypt_ctx, key, keylen / 2); |
|---|
| 97 | 75 | if (err) |
|---|
| 98 | 76 | return err; |
|---|
| 99 | 77 | |
|---|
| 100 | 78 | /* second half of xts-key is for tweak */ |
|---|
| 101 | | - return __twofish_setkey(&ctx->tweak_ctx, key + keylen / 2, keylen / 2, |
|---|
| 102 | | - flags); |
|---|
| 79 | + return __twofish_setkey(&ctx->tweak_ctx, key + keylen / 2, keylen / 2); |
|---|
| 103 | 80 | } |
|---|
| 104 | 81 | |
|---|
| 105 | 82 | static const struct common_glue_ctx twofish_enc = { |
|---|
| .. | .. |
|---|
| 108 | 85 | |
|---|
| 109 | 86 | .funcs = { { |
|---|
| 110 | 87 | .num_blocks = TWOFISH_PARALLEL_BLOCKS, |
|---|
| 111 | | - .fn_u = { .ecb = GLUE_FUNC_CAST(twofish_ecb_enc_8way) } |
|---|
| 88 | + .fn_u = { .ecb = twofish_ecb_enc_8way } |
|---|
| 112 | 89 | }, { |
|---|
| 113 | 90 | .num_blocks = 3, |
|---|
| 114 | | - .fn_u = { .ecb = GLUE_FUNC_CAST(twofish_enc_blk_3way) } |
|---|
| 91 | + .fn_u = { .ecb = twofish_enc_blk_3way } |
|---|
| 115 | 92 | }, { |
|---|
| 116 | 93 | .num_blocks = 1, |
|---|
| 117 | | - .fn_u = { .ecb = GLUE_FUNC_CAST(twofish_enc_blk) } |
|---|
| 94 | + .fn_u = { .ecb = twofish_enc_blk } |
|---|
| 118 | 95 | } } |
|---|
| 119 | 96 | }; |
|---|
| 120 | 97 | |
|---|
| .. | .. |
|---|
| 124 | 101 | |
|---|
| 125 | 102 | .funcs = { { |
|---|
| 126 | 103 | .num_blocks = TWOFISH_PARALLEL_BLOCKS, |
|---|
| 127 | | - .fn_u = { .ctr = GLUE_CTR_FUNC_CAST(twofish_ctr_8way) } |
|---|
| 104 | + .fn_u = { .ctr = twofish_ctr_8way } |
|---|
| 128 | 105 | }, { |
|---|
| 129 | 106 | .num_blocks = 3, |
|---|
| 130 | | - .fn_u = { .ctr = GLUE_CTR_FUNC_CAST(twofish_enc_blk_ctr_3way) } |
|---|
| 107 | + .fn_u = { .ctr = twofish_enc_blk_ctr_3way } |
|---|
| 131 | 108 | }, { |
|---|
| 132 | 109 | .num_blocks = 1, |
|---|
| 133 | | - .fn_u = { .ctr = GLUE_CTR_FUNC_CAST(twofish_enc_blk_ctr) } |
|---|
| 110 | + .fn_u = { .ctr = twofish_enc_blk_ctr } |
|---|
| 134 | 111 | } } |
|---|
| 135 | 112 | }; |
|---|
| 136 | 113 | |
|---|
| .. | .. |
|---|
| 140 | 117 | |
|---|
| 141 | 118 | .funcs = { { |
|---|
| 142 | 119 | .num_blocks = TWOFISH_PARALLEL_BLOCKS, |
|---|
| 143 | | - .fn_u = { .xts = GLUE_XTS_FUNC_CAST(twofish_xts_enc_8way) } |
|---|
| 120 | + .fn_u = { .xts = twofish_xts_enc_8way } |
|---|
| 144 | 121 | }, { |
|---|
| 145 | 122 | .num_blocks = 1, |
|---|
| 146 | | - .fn_u = { .xts = GLUE_XTS_FUNC_CAST(twofish_xts_enc) } |
|---|
| 123 | + .fn_u = { .xts = twofish_xts_enc } |
|---|
| 147 | 124 | } } |
|---|
| 148 | 125 | }; |
|---|
| 149 | 126 | |
|---|
| .. | .. |
|---|
| 153 | 130 | |
|---|
| 154 | 131 | .funcs = { { |
|---|
| 155 | 132 | .num_blocks = TWOFISH_PARALLEL_BLOCKS, |
|---|
| 156 | | - .fn_u = { .ecb = GLUE_FUNC_CAST(twofish_ecb_dec_8way) } |
|---|
| 133 | + .fn_u = { .ecb = twofish_ecb_dec_8way } |
|---|
| 157 | 134 | }, { |
|---|
| 158 | 135 | .num_blocks = 3, |
|---|
| 159 | | - .fn_u = { .ecb = GLUE_FUNC_CAST(twofish_dec_blk_3way) } |
|---|
| 136 | + .fn_u = { .ecb = twofish_dec_blk_3way } |
|---|
| 160 | 137 | }, { |
|---|
| 161 | 138 | .num_blocks = 1, |
|---|
| 162 | | - .fn_u = { .ecb = GLUE_FUNC_CAST(twofish_dec_blk) } |
|---|
| 139 | + .fn_u = { .ecb = twofish_dec_blk } |
|---|
| 163 | 140 | } } |
|---|
| 164 | 141 | }; |
|---|
| 165 | 142 | |
|---|
| .. | .. |
|---|
| 169 | 146 | |
|---|
| 170 | 147 | .funcs = { { |
|---|
| 171 | 148 | .num_blocks = TWOFISH_PARALLEL_BLOCKS, |
|---|
| 172 | | - .fn_u = { .cbc = GLUE_CBC_FUNC_CAST(twofish_cbc_dec_8way) } |
|---|
| 149 | + .fn_u = { .cbc = twofish_cbc_dec_8way } |
|---|
| 173 | 150 | }, { |
|---|
| 174 | 151 | .num_blocks = 3, |
|---|
| 175 | | - .fn_u = { .cbc = GLUE_CBC_FUNC_CAST(twofish_dec_blk_cbc_3way) } |
|---|
| 152 | + .fn_u = { .cbc = twofish_dec_blk_cbc_3way } |
|---|
| 176 | 153 | }, { |
|---|
| 177 | 154 | .num_blocks = 1, |
|---|
| 178 | | - .fn_u = { .cbc = GLUE_CBC_FUNC_CAST(twofish_dec_blk) } |
|---|
| 155 | + .fn_u = { .cbc = twofish_dec_blk } |
|---|
| 179 | 156 | } } |
|---|
| 180 | 157 | }; |
|---|
| 181 | 158 | |
|---|
| .. | .. |
|---|
| 185 | 162 | |
|---|
| 186 | 163 | .funcs = { { |
|---|
| 187 | 164 | .num_blocks = TWOFISH_PARALLEL_BLOCKS, |
|---|
| 188 | | - .fn_u = { .xts = GLUE_XTS_FUNC_CAST(twofish_xts_dec_8way) } |
|---|
| 165 | + .fn_u = { .xts = twofish_xts_dec_8way } |
|---|
| 189 | 166 | }, { |
|---|
| 190 | 167 | .num_blocks = 1, |
|---|
| 191 | | - .fn_u = { .xts = GLUE_XTS_FUNC_CAST(twofish_xts_dec) } |
|---|
| 168 | + .fn_u = { .xts = twofish_xts_dec } |
|---|
| 192 | 169 | } } |
|---|
| 193 | 170 | }; |
|---|
| 194 | 171 | |
|---|
| .. | .. |
|---|
| 204 | 181 | |
|---|
| 205 | 182 | static int cbc_encrypt(struct skcipher_request *req) |
|---|
| 206 | 183 | { |
|---|
| 207 | | - return glue_cbc_encrypt_req_128bit(GLUE_FUNC_CAST(twofish_enc_blk), |
|---|
| 208 | | - req); |
|---|
| 184 | + return glue_cbc_encrypt_req_128bit(twofish_enc_blk, req); |
|---|
| 209 | 185 | } |
|---|
| 210 | 186 | |
|---|
| 211 | 187 | static int cbc_decrypt(struct skcipher_request *req) |
|---|
| .. | .. |
|---|
| 223 | 199 | struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
|---|
| 224 | 200 | struct twofish_xts_ctx *ctx = crypto_skcipher_ctx(tfm); |
|---|
| 225 | 201 | |
|---|
| 226 | | - return glue_xts_req_128bit(&twofish_enc_xts, req, |
|---|
| 227 | | - XTS_TWEAK_CAST(twofish_enc_blk), |
|---|
| 228 | | - &ctx->tweak_ctx, &ctx->crypt_ctx); |
|---|
| 202 | + return glue_xts_req_128bit(&twofish_enc_xts, req, twofish_enc_blk, |
|---|
| 203 | + &ctx->tweak_ctx, &ctx->crypt_ctx, false); |
|---|
| 229 | 204 | } |
|---|
| 230 | 205 | |
|---|
| 231 | 206 | static int xts_decrypt(struct skcipher_request *req) |
|---|
| .. | .. |
|---|
| 233 | 208 | struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
|---|
| 234 | 209 | struct twofish_xts_ctx *ctx = crypto_skcipher_ctx(tfm); |
|---|
| 235 | 210 | |
|---|
| 236 | | - return glue_xts_req_128bit(&twofish_dec_xts, req, |
|---|
| 237 | | - XTS_TWEAK_CAST(twofish_enc_blk), |
|---|
| 238 | | - &ctx->tweak_ctx, &ctx->crypt_ctx); |
|---|
| 211 | + return glue_xts_req_128bit(&twofish_dec_xts, req, twofish_enc_blk, |
|---|
| 212 | + &ctx->tweak_ctx, &ctx->crypt_ctx, true); |
|---|
| 239 | 213 | } |
|---|
| 240 | 214 | |
|---|
| 241 | 215 | static struct skcipher_alg twofish_algs[] = { |
|---|