Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Ard Biesheuvel | 1abee99 | 2017-01-11 16:41:55 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Bit sliced AES using NEON instructions |
| 4 | * |
Ard Biesheuvel | ec808bb | 2017-07-24 11:28:15 +0100 | [diff] [blame] | 5 | * Copyright (C) 2016 - 2017 Linaro Ltd <ard.biesheuvel@linaro.org> |
Ard Biesheuvel | 1abee99 | 2017-01-11 16:41:55 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <asm/neon.h> |
Ard Biesheuvel | ec808bb | 2017-07-24 11:28:15 +0100 | [diff] [blame] | 9 | #include <asm/simd.h> |
Ard Biesheuvel | 1abee99 | 2017-01-11 16:41:55 +0000 | [diff] [blame] | 10 | #include <crypto/aes.h> |
Ard Biesheuvel | ff6f411 | 2019-07-02 21:41:35 +0200 | [diff] [blame] | 11 | #include <crypto/ctr.h> |
Ard Biesheuvel | 1abee99 | 2017-01-11 16:41:55 +0000 | [diff] [blame] | 12 | #include <crypto/internal/simd.h> |
| 13 | #include <crypto/internal/skcipher.h> |
Ard Biesheuvel | 67cfa5d | 2019-09-03 09:43:34 -0700 | [diff] [blame] | 14 | #include <crypto/scatterwalk.h> |
Ard Biesheuvel | 1abee99 | 2017-01-11 16:41:55 +0000 | [diff] [blame] | 15 | #include <crypto/xts.h> |
| 16 | #include <linux/module.h> |
| 17 | |
| 18 | MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>"); |
| 19 | MODULE_LICENSE("GPL v2"); |
| 20 | |
| 21 | MODULE_ALIAS_CRYPTO("ecb(aes)"); |
| 22 | MODULE_ALIAS_CRYPTO("cbc(aes)"); |
| 23 | MODULE_ALIAS_CRYPTO("ctr(aes)"); |
| 24 | MODULE_ALIAS_CRYPTO("xts(aes)"); |
| 25 | |
| 26 | asmlinkage void aesbs_convert_key(u8 out[], u32 const rk[], int rounds); |
| 27 | |
| 28 | asmlinkage void aesbs_ecb_encrypt(u8 out[], u8 const in[], u8 const rk[], |
| 29 | int rounds, int blocks); |
| 30 | asmlinkage void aesbs_ecb_decrypt(u8 out[], u8 const in[], u8 const rk[], |
| 31 | int rounds, int blocks); |
| 32 | |
| 33 | asmlinkage void aesbs_cbc_decrypt(u8 out[], u8 const in[], u8 const rk[], |
| 34 | int rounds, int blocks, u8 iv[]); |
| 35 | |
| 36 | asmlinkage void aesbs_ctr_encrypt(u8 out[], u8 const in[], u8 const rk[], |
Ard Biesheuvel | fc074e1 | 2022-01-27 12:35:44 +0100 | [diff] [blame] | 37 | int rounds, int blocks, u8 iv[]); |
Ard Biesheuvel | 1abee99 | 2017-01-11 16:41:55 +0000 | [diff] [blame] | 38 | |
| 39 | asmlinkage void aesbs_xts_encrypt(u8 out[], u8 const in[], u8 const rk[], |
| 40 | int rounds, int blocks, u8 iv[]); |
| 41 | asmlinkage void aesbs_xts_decrypt(u8 out[], u8 const in[], u8 const rk[], |
| 42 | int rounds, int blocks, u8 iv[]); |
| 43 | |
Ard Biesheuvel | 12fcd92 | 2017-01-28 23:25:39 +0000 | [diff] [blame] | 44 | /* borrowed from aes-neon-blk.ko */ |
| 45 | asmlinkage void neon_aes_ecb_encrypt(u8 out[], u8 const in[], u32 const rk[], |
Ard Biesheuvel | 6833817 | 2018-03-10 15:21:48 +0000 | [diff] [blame] | 46 | int rounds, int blocks); |
Ard Biesheuvel | 12fcd92 | 2017-01-28 23:25:39 +0000 | [diff] [blame] | 47 | asmlinkage void neon_aes_cbc_encrypt(u8 out[], u8 const in[], u32 const rk[], |
Ard Biesheuvel | 6833817 | 2018-03-10 15:21:48 +0000 | [diff] [blame] | 48 | int rounds, int blocks, u8 iv[]); |
Ard Biesheuvel | fc074e1 | 2022-01-27 12:35:44 +0100 | [diff] [blame] | 49 | asmlinkage void neon_aes_ctr_encrypt(u8 out[], u8 const in[], u32 const rk[], |
| 50 | int rounds, int bytes, u8 ctr[]); |
Ard Biesheuvel | 67cfa5d | 2019-09-03 09:43:34 -0700 | [diff] [blame] | 51 | asmlinkage void neon_aes_xts_encrypt(u8 out[], u8 const in[], |
| 52 | u32 const rk1[], int rounds, int bytes, |
| 53 | u32 const rk2[], u8 iv[], int first); |
| 54 | asmlinkage void neon_aes_xts_decrypt(u8 out[], u8 const in[], |
| 55 | u32 const rk1[], int rounds, int bytes, |
| 56 | u32 const rk2[], u8 iv[], int first); |
Ard Biesheuvel | 1abee99 | 2017-01-11 16:41:55 +0000 | [diff] [blame] | 57 | |
| 58 | struct aesbs_ctx { |
| 59 | u8 rk[13 * (8 * AES_BLOCK_SIZE) + 32]; |
| 60 | int rounds; |
| 61 | } __aligned(AES_BLOCK_SIZE); |
| 62 | |
Ard Biesheuvel | fc074e1 | 2022-01-27 12:35:44 +0100 | [diff] [blame] | 63 | struct aesbs_cbc_ctr_ctx { |
Ard Biesheuvel | 1abee99 | 2017-01-11 16:41:55 +0000 | [diff] [blame] | 64 | struct aesbs_ctx key; |
| 65 | u32 enc[AES_MAX_KEYLENGTH_U32]; |
| 66 | }; |
| 67 | |
| 68 | struct aesbs_xts_ctx { |
| 69 | struct aesbs_ctx key; |
| 70 | u32 twkey[AES_MAX_KEYLENGTH_U32]; |
Ard Biesheuvel | 67cfa5d | 2019-09-03 09:43:34 -0700 | [diff] [blame] | 71 | struct crypto_aes_ctx cts; |
Ard Biesheuvel | 1abee99 | 2017-01-11 16:41:55 +0000 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | static int aesbs_setkey(struct crypto_skcipher *tfm, const u8 *in_key, |
| 75 | unsigned int key_len) |
| 76 | { |
| 77 | struct aesbs_ctx *ctx = crypto_skcipher_ctx(tfm); |
| 78 | struct crypto_aes_ctx rk; |
| 79 | int err; |
| 80 | |
Ard Biesheuvel | f68df54 | 2019-07-02 21:41:31 +0200 | [diff] [blame] | 81 | err = aes_expandkey(&rk, in_key, key_len); |
Ard Biesheuvel | 1abee99 | 2017-01-11 16:41:55 +0000 | [diff] [blame] | 82 | if (err) |
| 83 | return err; |
| 84 | |
| 85 | ctx->rounds = 6 + key_len / 4; |
| 86 | |
| 87 | kernel_neon_begin(); |
| 88 | aesbs_convert_key(ctx->rk, rk.key_enc, ctx->rounds); |
| 89 | kernel_neon_end(); |
| 90 | |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | static int __ecb_crypt(struct skcipher_request *req, |
| 95 | void (*fn)(u8 out[], u8 const in[], u8 const rk[], |
| 96 | int rounds, int blocks)) |
| 97 | { |
| 98 | struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
| 99 | struct aesbs_ctx *ctx = crypto_skcipher_ctx(tfm); |
| 100 | struct skcipher_walk walk; |
| 101 | int err; |
| 102 | |
Ard Biesheuvel | 78ad7b0 | 2018-03-10 15:21:49 +0000 | [diff] [blame] | 103 | err = skcipher_walk_virt(&walk, req, false); |
Ard Biesheuvel | 1abee99 | 2017-01-11 16:41:55 +0000 | [diff] [blame] | 104 | |
Ard Biesheuvel | 1abee99 | 2017-01-11 16:41:55 +0000 | [diff] [blame] | 105 | while (walk.nbytes >= AES_BLOCK_SIZE) { |
| 106 | unsigned int blocks = walk.nbytes / AES_BLOCK_SIZE; |
| 107 | |
| 108 | if (walk.nbytes < walk.total) |
| 109 | blocks = round_down(blocks, |
| 110 | walk.stride / AES_BLOCK_SIZE); |
| 111 | |
Ard Biesheuvel | 78ad7b0 | 2018-03-10 15:21:49 +0000 | [diff] [blame] | 112 | kernel_neon_begin(); |
Ard Biesheuvel | 1abee99 | 2017-01-11 16:41:55 +0000 | [diff] [blame] | 113 | fn(walk.dst.virt.addr, walk.src.virt.addr, ctx->rk, |
| 114 | ctx->rounds, blocks); |
Ard Biesheuvel | 78ad7b0 | 2018-03-10 15:21:49 +0000 | [diff] [blame] | 115 | kernel_neon_end(); |
Ard Biesheuvel | 1abee99 | 2017-01-11 16:41:55 +0000 | [diff] [blame] | 116 | err = skcipher_walk_done(&walk, |
| 117 | walk.nbytes - blocks * AES_BLOCK_SIZE); |
| 118 | } |
Ard Biesheuvel | 1abee99 | 2017-01-11 16:41:55 +0000 | [diff] [blame] | 119 | |
| 120 | return err; |
| 121 | } |
| 122 | |
| 123 | static int ecb_encrypt(struct skcipher_request *req) |
| 124 | { |
| 125 | return __ecb_crypt(req, aesbs_ecb_encrypt); |
| 126 | } |
| 127 | |
| 128 | static int ecb_decrypt(struct skcipher_request *req) |
| 129 | { |
| 130 | return __ecb_crypt(req, aesbs_ecb_decrypt); |
| 131 | } |
| 132 | |
Ard Biesheuvel | fc074e1 | 2022-01-27 12:35:44 +0100 | [diff] [blame] | 133 | static int aesbs_cbc_ctr_setkey(struct crypto_skcipher *tfm, const u8 *in_key, |
Ard Biesheuvel | 1abee99 | 2017-01-11 16:41:55 +0000 | [diff] [blame] | 134 | unsigned int key_len) |
| 135 | { |
Ard Biesheuvel | fc074e1 | 2022-01-27 12:35:44 +0100 | [diff] [blame] | 136 | struct aesbs_cbc_ctr_ctx *ctx = crypto_skcipher_ctx(tfm); |
Ard Biesheuvel | 1abee99 | 2017-01-11 16:41:55 +0000 | [diff] [blame] | 137 | struct crypto_aes_ctx rk; |
| 138 | int err; |
| 139 | |
Ard Biesheuvel | f68df54 | 2019-07-02 21:41:31 +0200 | [diff] [blame] | 140 | err = aes_expandkey(&rk, in_key, key_len); |
Ard Biesheuvel | 1abee99 | 2017-01-11 16:41:55 +0000 | [diff] [blame] | 141 | if (err) |
| 142 | return err; |
| 143 | |
| 144 | ctx->key.rounds = 6 + key_len / 4; |
| 145 | |
| 146 | memcpy(ctx->enc, rk.key_enc, sizeof(ctx->enc)); |
| 147 | |
| 148 | kernel_neon_begin(); |
| 149 | aesbs_convert_key(ctx->key.rk, rk.key_enc, ctx->key.rounds); |
| 150 | kernel_neon_end(); |
Torsten Duwe | 82ff493 | 2020-03-13 12:02:58 +0100 | [diff] [blame] | 151 | memzero_explicit(&rk, sizeof(rk)); |
Ard Biesheuvel | 1abee99 | 2017-01-11 16:41:55 +0000 | [diff] [blame] | 152 | |
| 153 | return 0; |
| 154 | } |
| 155 | |
Ard Biesheuvel | 1abee99 | 2017-01-11 16:41:55 +0000 | [diff] [blame] | 156 | static int cbc_encrypt(struct skcipher_request *req) |
| 157 | { |
Ard Biesheuvel | 12fcd92 | 2017-01-28 23:25:39 +0000 | [diff] [blame] | 158 | struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
Ard Biesheuvel | fc074e1 | 2022-01-27 12:35:44 +0100 | [diff] [blame] | 159 | struct aesbs_cbc_ctr_ctx *ctx = crypto_skcipher_ctx(tfm); |
Ard Biesheuvel | 12fcd92 | 2017-01-28 23:25:39 +0000 | [diff] [blame] | 160 | struct skcipher_walk walk; |
Ard Biesheuvel | 6833817 | 2018-03-10 15:21:48 +0000 | [diff] [blame] | 161 | int err; |
Ard Biesheuvel | 12fcd92 | 2017-01-28 23:25:39 +0000 | [diff] [blame] | 162 | |
Ard Biesheuvel | 78ad7b0 | 2018-03-10 15:21:49 +0000 | [diff] [blame] | 163 | err = skcipher_walk_virt(&walk, req, false); |
Ard Biesheuvel | 12fcd92 | 2017-01-28 23:25:39 +0000 | [diff] [blame] | 164 | |
Ard Biesheuvel | 12fcd92 | 2017-01-28 23:25:39 +0000 | [diff] [blame] | 165 | while (walk.nbytes >= AES_BLOCK_SIZE) { |
| 166 | unsigned int blocks = walk.nbytes / AES_BLOCK_SIZE; |
| 167 | |
| 168 | /* fall back to the non-bitsliced NEON implementation */ |
Ard Biesheuvel | 78ad7b0 | 2018-03-10 15:21:49 +0000 | [diff] [blame] | 169 | kernel_neon_begin(); |
Ard Biesheuvel | 12fcd92 | 2017-01-28 23:25:39 +0000 | [diff] [blame] | 170 | neon_aes_cbc_encrypt(walk.dst.virt.addr, walk.src.virt.addr, |
Ard Biesheuvel | 6833817 | 2018-03-10 15:21:48 +0000 | [diff] [blame] | 171 | ctx->enc, ctx->key.rounds, blocks, |
| 172 | walk.iv); |
Ard Biesheuvel | 78ad7b0 | 2018-03-10 15:21:49 +0000 | [diff] [blame] | 173 | kernel_neon_end(); |
Ard Biesheuvel | 12fcd92 | 2017-01-28 23:25:39 +0000 | [diff] [blame] | 174 | err = skcipher_walk_done(&walk, walk.nbytes % AES_BLOCK_SIZE); |
Ard Biesheuvel | 12fcd92 | 2017-01-28 23:25:39 +0000 | [diff] [blame] | 175 | } |
Ard Biesheuvel | 12fcd92 | 2017-01-28 23:25:39 +0000 | [diff] [blame] | 176 | return err; |
Ard Biesheuvel | 1abee99 | 2017-01-11 16:41:55 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | static int cbc_decrypt(struct skcipher_request *req) |
| 180 | { |
| 181 | struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
Ard Biesheuvel | fc074e1 | 2022-01-27 12:35:44 +0100 | [diff] [blame] | 182 | struct aesbs_cbc_ctr_ctx *ctx = crypto_skcipher_ctx(tfm); |
Ard Biesheuvel | 1abee99 | 2017-01-11 16:41:55 +0000 | [diff] [blame] | 183 | struct skcipher_walk walk; |
| 184 | int err; |
| 185 | |
Ard Biesheuvel | 78ad7b0 | 2018-03-10 15:21:49 +0000 | [diff] [blame] | 186 | err = skcipher_walk_virt(&walk, req, false); |
Ard Biesheuvel | 1abee99 | 2017-01-11 16:41:55 +0000 | [diff] [blame] | 187 | |
Ard Biesheuvel | 1abee99 | 2017-01-11 16:41:55 +0000 | [diff] [blame] | 188 | while (walk.nbytes >= AES_BLOCK_SIZE) { |
| 189 | unsigned int blocks = walk.nbytes / AES_BLOCK_SIZE; |
| 190 | |
| 191 | if (walk.nbytes < walk.total) |
| 192 | blocks = round_down(blocks, |
| 193 | walk.stride / AES_BLOCK_SIZE); |
| 194 | |
Ard Biesheuvel | 78ad7b0 | 2018-03-10 15:21:49 +0000 | [diff] [blame] | 195 | kernel_neon_begin(); |
Ard Biesheuvel | 1abee99 | 2017-01-11 16:41:55 +0000 | [diff] [blame] | 196 | aesbs_cbc_decrypt(walk.dst.virt.addr, walk.src.virt.addr, |
| 197 | ctx->key.rk, ctx->key.rounds, blocks, |
| 198 | walk.iv); |
Ard Biesheuvel | 78ad7b0 | 2018-03-10 15:21:49 +0000 | [diff] [blame] | 199 | kernel_neon_end(); |
Ard Biesheuvel | 1abee99 | 2017-01-11 16:41:55 +0000 | [diff] [blame] | 200 | err = skcipher_walk_done(&walk, |
| 201 | walk.nbytes - blocks * AES_BLOCK_SIZE); |
| 202 | } |
Ard Biesheuvel | 1abee99 | 2017-01-11 16:41:55 +0000 | [diff] [blame] | 203 | |
| 204 | return err; |
| 205 | } |
| 206 | |
| 207 | static int ctr_encrypt(struct skcipher_request *req) |
| 208 | { |
| 209 | struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
Ard Biesheuvel | fc074e1 | 2022-01-27 12:35:44 +0100 | [diff] [blame] | 210 | struct aesbs_cbc_ctr_ctx *ctx = crypto_skcipher_ctx(tfm); |
Ard Biesheuvel | 1abee99 | 2017-01-11 16:41:55 +0000 | [diff] [blame] | 211 | struct skcipher_walk walk; |
| 212 | int err; |
| 213 | |
Ard Biesheuvel | 78ad7b0 | 2018-03-10 15:21:49 +0000 | [diff] [blame] | 214 | err = skcipher_walk_virt(&walk, req, false); |
Ard Biesheuvel | 1abee99 | 2017-01-11 16:41:55 +0000 | [diff] [blame] | 215 | |
Ard Biesheuvel | 1abee99 | 2017-01-11 16:41:55 +0000 | [diff] [blame] | 216 | while (walk.nbytes > 0) { |
Ard Biesheuvel | fc074e1 | 2022-01-27 12:35:44 +0100 | [diff] [blame] | 217 | int blocks = (walk.nbytes / AES_BLOCK_SIZE) & ~7; |
| 218 | int nbytes = walk.nbytes % (8 * AES_BLOCK_SIZE); |
| 219 | const u8 *src = walk.src.virt.addr; |
| 220 | u8 *dst = walk.dst.virt.addr; |
Ard Biesheuvel | 1abee99 | 2017-01-11 16:41:55 +0000 | [diff] [blame] | 221 | |
Ard Biesheuvel | 78ad7b0 | 2018-03-10 15:21:49 +0000 | [diff] [blame] | 222 | kernel_neon_begin(); |
Ard Biesheuvel | fc074e1 | 2022-01-27 12:35:44 +0100 | [diff] [blame] | 223 | if (blocks >= 8) { |
| 224 | aesbs_ctr_encrypt(dst, src, ctx->key.rk, ctx->key.rounds, |
| 225 | blocks, walk.iv); |
| 226 | dst += blocks * AES_BLOCK_SIZE; |
| 227 | src += blocks * AES_BLOCK_SIZE; |
Ard Biesheuvel | 1abee99 | 2017-01-11 16:41:55 +0000 | [diff] [blame] | 228 | } |
Ard Biesheuvel | fc074e1 | 2022-01-27 12:35:44 +0100 | [diff] [blame] | 229 | if (nbytes && walk.nbytes == walk.total) { |
Ard Biesheuvel | 1c0cf6d | 2024-02-23 14:20:35 +0100 | [diff] [blame] | 230 | u8 buf[AES_BLOCK_SIZE]; |
| 231 | u8 *d = dst; |
| 232 | |
| 233 | if (unlikely(nbytes < AES_BLOCK_SIZE)) |
| 234 | src = dst = memcpy(buf + sizeof(buf) - nbytes, |
| 235 | src, nbytes); |
| 236 | |
Ard Biesheuvel | fc074e1 | 2022-01-27 12:35:44 +0100 | [diff] [blame] | 237 | neon_aes_ctr_encrypt(dst, src, ctx->enc, ctx->key.rounds, |
| 238 | nbytes, walk.iv); |
Ard Biesheuvel | 1c0cf6d | 2024-02-23 14:20:35 +0100 | [diff] [blame] | 239 | |
| 240 | if (unlikely(nbytes < AES_BLOCK_SIZE)) |
| 241 | memcpy(d, dst, nbytes); |
| 242 | |
Ard Biesheuvel | fc074e1 | 2022-01-27 12:35:44 +0100 | [diff] [blame] | 243 | nbytes = 0; |
| 244 | } |
| 245 | kernel_neon_end(); |
| 246 | err = skcipher_walk_done(&walk, nbytes); |
Ard Biesheuvel | 1abee99 | 2017-01-11 16:41:55 +0000 | [diff] [blame] | 247 | } |
Ard Biesheuvel | 1abee99 | 2017-01-11 16:41:55 +0000 | [diff] [blame] | 248 | return err; |
| 249 | } |
| 250 | |
| 251 | static int aesbs_xts_setkey(struct crypto_skcipher *tfm, const u8 *in_key, |
| 252 | unsigned int key_len) |
| 253 | { |
| 254 | struct aesbs_xts_ctx *ctx = crypto_skcipher_ctx(tfm); |
| 255 | struct crypto_aes_ctx rk; |
| 256 | int err; |
| 257 | |
| 258 | err = xts_verify_key(tfm, in_key, key_len); |
| 259 | if (err) |
| 260 | return err; |
| 261 | |
| 262 | key_len /= 2; |
Ard Biesheuvel | 67cfa5d | 2019-09-03 09:43:34 -0700 | [diff] [blame] | 263 | err = aes_expandkey(&ctx->cts, in_key, key_len); |
| 264 | if (err) |
| 265 | return err; |
| 266 | |
Ard Biesheuvel | f68df54 | 2019-07-02 21:41:31 +0200 | [diff] [blame] | 267 | err = aes_expandkey(&rk, in_key + key_len, key_len); |
Ard Biesheuvel | 1abee99 | 2017-01-11 16:41:55 +0000 | [diff] [blame] | 268 | if (err) |
| 269 | return err; |
| 270 | |
| 271 | memcpy(ctx->twkey, rk.key_enc, sizeof(ctx->twkey)); |
| 272 | |
| 273 | return aesbs_setkey(tfm, in_key, key_len); |
| 274 | } |
| 275 | |
Ard Biesheuvel | 67cfa5d | 2019-09-03 09:43:34 -0700 | [diff] [blame] | 276 | static int __xts_crypt(struct skcipher_request *req, bool encrypt, |
Ard Biesheuvel | 1abee99 | 2017-01-11 16:41:55 +0000 | [diff] [blame] | 277 | void (*fn)(u8 out[], u8 const in[], u8 const rk[], |
| 278 | int rounds, int blocks, u8 iv[])) |
| 279 | { |
| 280 | struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
| 281 | struct aesbs_xts_ctx *ctx = crypto_skcipher_ctx(tfm); |
Ard Biesheuvel | 67cfa5d | 2019-09-03 09:43:34 -0700 | [diff] [blame] | 282 | int tail = req->cryptlen % (8 * AES_BLOCK_SIZE); |
| 283 | struct scatterlist sg_src[2], sg_dst[2]; |
| 284 | struct skcipher_request subreq; |
| 285 | struct scatterlist *src, *dst; |
Ard Biesheuvel | 1abee99 | 2017-01-11 16:41:55 +0000 | [diff] [blame] | 286 | struct skcipher_walk walk; |
Ard Biesheuvel | 67cfa5d | 2019-09-03 09:43:34 -0700 | [diff] [blame] | 287 | int nbytes, err; |
| 288 | int first = 1; |
| 289 | u8 *out, *in; |
| 290 | |
| 291 | if (req->cryptlen < AES_BLOCK_SIZE) |
| 292 | return -EINVAL; |
| 293 | |
| 294 | /* ensure that the cts tail is covered by a single step */ |
| 295 | if (unlikely(tail > 0 && tail < AES_BLOCK_SIZE)) { |
| 296 | int xts_blocks = DIV_ROUND_UP(req->cryptlen, |
| 297 | AES_BLOCK_SIZE) - 2; |
| 298 | |
| 299 | skcipher_request_set_tfm(&subreq, tfm); |
| 300 | skcipher_request_set_callback(&subreq, |
| 301 | skcipher_request_flags(req), |
| 302 | NULL, NULL); |
| 303 | skcipher_request_set_crypt(&subreq, req->src, req->dst, |
| 304 | xts_blocks * AES_BLOCK_SIZE, |
| 305 | req->iv); |
| 306 | req = &subreq; |
| 307 | } else { |
| 308 | tail = 0; |
| 309 | } |
Ard Biesheuvel | 1abee99 | 2017-01-11 16:41:55 +0000 | [diff] [blame] | 310 | |
Ard Biesheuvel | 78ad7b0 | 2018-03-10 15:21:49 +0000 | [diff] [blame] | 311 | err = skcipher_walk_virt(&walk, req, false); |
Eric Biggers | 4a8108b | 2019-04-09 23:46:32 -0700 | [diff] [blame] | 312 | if (err) |
| 313 | return err; |
Ard Biesheuvel | 1abee99 | 2017-01-11 16:41:55 +0000 | [diff] [blame] | 314 | |
Ard Biesheuvel | 1abee99 | 2017-01-11 16:41:55 +0000 | [diff] [blame] | 315 | while (walk.nbytes >= AES_BLOCK_SIZE) { |
Ard Biesheuvel | dfc6031 | 2022-01-27 12:35:45 +0100 | [diff] [blame] | 316 | int blocks = (walk.nbytes / AES_BLOCK_SIZE) & ~7; |
Ard Biesheuvel | 67cfa5d | 2019-09-03 09:43:34 -0700 | [diff] [blame] | 317 | out = walk.dst.virt.addr; |
| 318 | in = walk.src.virt.addr; |
| 319 | nbytes = walk.nbytes; |
| 320 | |
Ard Biesheuvel | 78ad7b0 | 2018-03-10 15:21:49 +0000 | [diff] [blame] | 321 | kernel_neon_begin(); |
Ard Biesheuvel | dfc6031 | 2022-01-27 12:35:45 +0100 | [diff] [blame] | 322 | if (blocks >= 8) { |
| 323 | if (first == 1) |
Ard Biesheuvel | 67cfa5d | 2019-09-03 09:43:34 -0700 | [diff] [blame] | 324 | neon_aes_ecb_encrypt(walk.iv, walk.iv, |
| 325 | ctx->twkey, |
| 326 | ctx->key.rounds, 1); |
Ard Biesheuvel | dfc6031 | 2022-01-27 12:35:45 +0100 | [diff] [blame] | 327 | first = 2; |
Ard Biesheuvel | 67cfa5d | 2019-09-03 09:43:34 -0700 | [diff] [blame] | 328 | |
| 329 | fn(out, in, ctx->key.rk, ctx->key.rounds, blocks, |
| 330 | walk.iv); |
| 331 | |
| 332 | out += blocks * AES_BLOCK_SIZE; |
| 333 | in += blocks * AES_BLOCK_SIZE; |
| 334 | nbytes -= blocks * AES_BLOCK_SIZE; |
| 335 | } |
Ard Biesheuvel | dfc6031 | 2022-01-27 12:35:45 +0100 | [diff] [blame] | 336 | if (walk.nbytes == walk.total && nbytes > 0) { |
| 337 | if (encrypt) |
| 338 | neon_aes_xts_encrypt(out, in, ctx->cts.key_enc, |
| 339 | ctx->key.rounds, nbytes, |
| 340 | ctx->twkey, walk.iv, first); |
| 341 | else |
| 342 | neon_aes_xts_decrypt(out, in, ctx->cts.key_dec, |
| 343 | ctx->key.rounds, nbytes, |
| 344 | ctx->twkey, walk.iv, first); |
| 345 | nbytes = first = 0; |
| 346 | } |
Ard Biesheuvel | 78ad7b0 | 2018-03-10 15:21:49 +0000 | [diff] [blame] | 347 | kernel_neon_end(); |
Yunfeng Ye | 9b53799 | 2019-10-22 16:11:18 +0800 | [diff] [blame] | 348 | err = skcipher_walk_done(&walk, nbytes); |
Ard Biesheuvel | 1abee99 | 2017-01-11 16:41:55 +0000 | [diff] [blame] | 349 | } |
Ard Biesheuvel | 67cfa5d | 2019-09-03 09:43:34 -0700 | [diff] [blame] | 350 | |
| 351 | if (err || likely(!tail)) |
| 352 | return err; |
| 353 | |
| 354 | /* handle ciphertext stealing */ |
| 355 | dst = src = scatterwalk_ffwd(sg_src, req->src, req->cryptlen); |
| 356 | if (req->dst != req->src) |
| 357 | dst = scatterwalk_ffwd(sg_dst, req->dst, req->cryptlen); |
| 358 | |
| 359 | skcipher_request_set_crypt(req, src, dst, AES_BLOCK_SIZE + tail, |
| 360 | req->iv); |
| 361 | |
| 362 | err = skcipher_walk_virt(&walk, req, false); |
| 363 | if (err) |
| 364 | return err; |
| 365 | |
| 366 | out = walk.dst.virt.addr; |
| 367 | in = walk.src.virt.addr; |
| 368 | nbytes = walk.nbytes; |
| 369 | |
| 370 | kernel_neon_begin(); |
Ard Biesheuvel | 67cfa5d | 2019-09-03 09:43:34 -0700 | [diff] [blame] | 371 | if (encrypt) |
| 372 | neon_aes_xts_encrypt(out, in, ctx->cts.key_enc, ctx->key.rounds, |
Ard Biesheuvel | dfc6031 | 2022-01-27 12:35:45 +0100 | [diff] [blame] | 373 | nbytes, ctx->twkey, walk.iv, first); |
Ard Biesheuvel | 67cfa5d | 2019-09-03 09:43:34 -0700 | [diff] [blame] | 374 | else |
| 375 | neon_aes_xts_decrypt(out, in, ctx->cts.key_dec, ctx->key.rounds, |
Ard Biesheuvel | dfc6031 | 2022-01-27 12:35:45 +0100 | [diff] [blame] | 376 | nbytes, ctx->twkey, walk.iv, first); |
Ard Biesheuvel | 67cfa5d | 2019-09-03 09:43:34 -0700 | [diff] [blame] | 377 | kernel_neon_end(); |
| 378 | |
| 379 | return skcipher_walk_done(&walk, 0); |
Ard Biesheuvel | 1abee99 | 2017-01-11 16:41:55 +0000 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | static int xts_encrypt(struct skcipher_request *req) |
| 383 | { |
Ard Biesheuvel | 67cfa5d | 2019-09-03 09:43:34 -0700 | [diff] [blame] | 384 | return __xts_crypt(req, true, aesbs_xts_encrypt); |
Ard Biesheuvel | 1abee99 | 2017-01-11 16:41:55 +0000 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | static int xts_decrypt(struct skcipher_request *req) |
| 388 | { |
Ard Biesheuvel | 67cfa5d | 2019-09-03 09:43:34 -0700 | [diff] [blame] | 389 | return __xts_crypt(req, false, aesbs_xts_decrypt); |
Ard Biesheuvel | 1abee99 | 2017-01-11 16:41:55 +0000 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | static struct skcipher_alg aes_algs[] = { { |
Ard Biesheuvel | 96c34e1 | 2021-08-27 09:03:37 +0200 | [diff] [blame] | 393 | .base.cra_name = "ecb(aes)", |
| 394 | .base.cra_driver_name = "ecb-aes-neonbs", |
Ard Biesheuvel | 1abee99 | 2017-01-11 16:41:55 +0000 | [diff] [blame] | 395 | .base.cra_priority = 250, |
| 396 | .base.cra_blocksize = AES_BLOCK_SIZE, |
| 397 | .base.cra_ctxsize = sizeof(struct aesbs_ctx), |
| 398 | .base.cra_module = THIS_MODULE, |
Ard Biesheuvel | 1abee99 | 2017-01-11 16:41:55 +0000 | [diff] [blame] | 399 | |
| 400 | .min_keysize = AES_MIN_KEY_SIZE, |
| 401 | .max_keysize = AES_MAX_KEY_SIZE, |
| 402 | .walksize = 8 * AES_BLOCK_SIZE, |
| 403 | .setkey = aesbs_setkey, |
| 404 | .encrypt = ecb_encrypt, |
| 405 | .decrypt = ecb_decrypt, |
| 406 | }, { |
Ard Biesheuvel | 96c34e1 | 2021-08-27 09:03:37 +0200 | [diff] [blame] | 407 | .base.cra_name = "cbc(aes)", |
| 408 | .base.cra_driver_name = "cbc-aes-neonbs", |
Ard Biesheuvel | 1abee99 | 2017-01-11 16:41:55 +0000 | [diff] [blame] | 409 | .base.cra_priority = 250, |
| 410 | .base.cra_blocksize = AES_BLOCK_SIZE, |
Ard Biesheuvel | fc074e1 | 2022-01-27 12:35:44 +0100 | [diff] [blame] | 411 | .base.cra_ctxsize = sizeof(struct aesbs_cbc_ctr_ctx), |
Ard Biesheuvel | 1abee99 | 2017-01-11 16:41:55 +0000 | [diff] [blame] | 412 | .base.cra_module = THIS_MODULE, |
Ard Biesheuvel | 1abee99 | 2017-01-11 16:41:55 +0000 | [diff] [blame] | 413 | |
| 414 | .min_keysize = AES_MIN_KEY_SIZE, |
| 415 | .max_keysize = AES_MAX_KEY_SIZE, |
| 416 | .walksize = 8 * AES_BLOCK_SIZE, |
| 417 | .ivsize = AES_BLOCK_SIZE, |
Ard Biesheuvel | fc074e1 | 2022-01-27 12:35:44 +0100 | [diff] [blame] | 418 | .setkey = aesbs_cbc_ctr_setkey, |
Ard Biesheuvel | 1abee99 | 2017-01-11 16:41:55 +0000 | [diff] [blame] | 419 | .encrypt = cbc_encrypt, |
| 420 | .decrypt = cbc_decrypt, |
| 421 | }, { |
Ard Biesheuvel | 96c34e1 | 2021-08-27 09:03:37 +0200 | [diff] [blame] | 422 | .base.cra_name = "ctr(aes)", |
| 423 | .base.cra_driver_name = "ctr-aes-neonbs", |
Ard Biesheuvel | 1abee99 | 2017-01-11 16:41:55 +0000 | [diff] [blame] | 424 | .base.cra_priority = 250, |
| 425 | .base.cra_blocksize = 1, |
Ard Biesheuvel | fc074e1 | 2022-01-27 12:35:44 +0100 | [diff] [blame] | 426 | .base.cra_ctxsize = sizeof(struct aesbs_cbc_ctr_ctx), |
Ard Biesheuvel | 1abee99 | 2017-01-11 16:41:55 +0000 | [diff] [blame] | 427 | .base.cra_module = THIS_MODULE, |
Ard Biesheuvel | 1abee99 | 2017-01-11 16:41:55 +0000 | [diff] [blame] | 428 | |
| 429 | .min_keysize = AES_MIN_KEY_SIZE, |
| 430 | .max_keysize = AES_MAX_KEY_SIZE, |
| 431 | .chunksize = AES_BLOCK_SIZE, |
| 432 | .walksize = 8 * AES_BLOCK_SIZE, |
| 433 | .ivsize = AES_BLOCK_SIZE, |
Ard Biesheuvel | fc074e1 | 2022-01-27 12:35:44 +0100 | [diff] [blame] | 434 | .setkey = aesbs_cbc_ctr_setkey, |
Ard Biesheuvel | 1abee99 | 2017-01-11 16:41:55 +0000 | [diff] [blame] | 435 | .encrypt = ctr_encrypt, |
| 436 | .decrypt = ctr_encrypt, |
| 437 | }, { |
Ard Biesheuvel | 96c34e1 | 2021-08-27 09:03:37 +0200 | [diff] [blame] | 438 | .base.cra_name = "xts(aes)", |
| 439 | .base.cra_driver_name = "xts-aes-neonbs", |
Ard Biesheuvel | 1abee99 | 2017-01-11 16:41:55 +0000 | [diff] [blame] | 440 | .base.cra_priority = 250, |
| 441 | .base.cra_blocksize = AES_BLOCK_SIZE, |
| 442 | .base.cra_ctxsize = sizeof(struct aesbs_xts_ctx), |
| 443 | .base.cra_module = THIS_MODULE, |
Ard Biesheuvel | 1abee99 | 2017-01-11 16:41:55 +0000 | [diff] [blame] | 444 | |
| 445 | .min_keysize = 2 * AES_MIN_KEY_SIZE, |
| 446 | .max_keysize = 2 * AES_MAX_KEY_SIZE, |
| 447 | .walksize = 8 * AES_BLOCK_SIZE, |
| 448 | .ivsize = AES_BLOCK_SIZE, |
| 449 | .setkey = aesbs_xts_setkey, |
| 450 | .encrypt = xts_encrypt, |
| 451 | .decrypt = xts_decrypt, |
| 452 | } }; |
| 453 | |
Ard Biesheuvel | 1abee99 | 2017-01-11 16:41:55 +0000 | [diff] [blame] | 454 | static void aes_exit(void) |
| 455 | { |
Ard Biesheuvel | 1abee99 | 2017-01-11 16:41:55 +0000 | [diff] [blame] | 456 | crypto_unregister_skciphers(aes_algs, ARRAY_SIZE(aes_algs)); |
| 457 | } |
| 458 | |
| 459 | static int __init aes_init(void) |
| 460 | { |
Andrew Murray | aaba098 | 2019-04-09 10:52:40 +0100 | [diff] [blame] | 461 | if (!cpu_have_named_feature(ASIMD)) |
Ard Biesheuvel | 1abee99 | 2017-01-11 16:41:55 +0000 | [diff] [blame] | 462 | return -ENODEV; |
| 463 | |
Ard Biesheuvel | 96c34e1 | 2021-08-27 09:03:37 +0200 | [diff] [blame] | 464 | return crypto_register_skciphers(aes_algs, ARRAY_SIZE(aes_algs)); |
Ard Biesheuvel | 1abee99 | 2017-01-11 16:41:55 +0000 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | module_init(aes_init); |
| 468 | module_exit(aes_exit); |