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