Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Ard Biesheuvel | cc477bf | 2017-01-11 16:41:54 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Bit sliced AES using NEON instructions |
| 4 | * |
| 5 | * Copyright (C) 2017 Linaro Ltd <ard.biesheuvel@linaro.org> |
Ard Biesheuvel | cc477bf | 2017-01-11 16:41:54 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <asm/neon.h> |
Ard Biesheuvel | e5f0504 | 2019-07-02 21:41:39 +0200 | [diff] [blame] | 9 | #include <asm/simd.h> |
Ard Biesheuvel | cc477bf | 2017-01-11 16:41:54 +0000 | [diff] [blame] | 10 | #include <crypto/aes.h> |
Ard Biesheuvel | e5f0504 | 2019-07-02 21:41:39 +0200 | [diff] [blame] | 11 | #include <crypto/ctr.h> |
Ard Biesheuvel | 0eb76ba | 2020-12-11 13:27:15 +0100 | [diff] [blame] | 12 | #include <crypto/internal/cipher.h> |
Ard Biesheuvel | cc477bf | 2017-01-11 16:41:54 +0000 | [diff] [blame] | 13 | #include <crypto/internal/simd.h> |
| 14 | #include <crypto/internal/skcipher.h> |
Ard Biesheuvel | 2ed8b79 | 2019-09-03 09:43:36 -0700 | [diff] [blame] | 15 | #include <crypto/scatterwalk.h> |
Ard Biesheuvel | cc477bf | 2017-01-11 16:41:54 +0000 | [diff] [blame] | 16 | #include <crypto/xts.h> |
| 17 | #include <linux/module.h> |
| 18 | |
| 19 | MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>"); |
| 20 | MODULE_LICENSE("GPL v2"); |
| 21 | |
| 22 | MODULE_ALIAS_CRYPTO("ecb(aes)"); |
Horia Geantă | a2715fb | 2020-10-28 11:03:20 +0200 | [diff] [blame] | 23 | MODULE_ALIAS_CRYPTO("cbc(aes)-all"); |
Ard Biesheuvel | cc477bf | 2017-01-11 16:41:54 +0000 | [diff] [blame] | 24 | MODULE_ALIAS_CRYPTO("ctr(aes)"); |
| 25 | MODULE_ALIAS_CRYPTO("xts(aes)"); |
| 26 | |
Ard Biesheuvel | 0eb76ba | 2020-12-11 13:27:15 +0100 | [diff] [blame] | 27 | MODULE_IMPORT_NS(CRYPTO_INTERNAL); |
| 28 | |
Ard Biesheuvel | cc477bf | 2017-01-11 16:41:54 +0000 | [diff] [blame] | 29 | asmlinkage void aesbs_convert_key(u8 out[], u32 const rk[], int rounds); |
| 30 | |
| 31 | asmlinkage void aesbs_ecb_encrypt(u8 out[], u8 const in[], u8 const rk[], |
| 32 | int rounds, int blocks); |
| 33 | asmlinkage void aesbs_ecb_decrypt(u8 out[], u8 const in[], u8 const rk[], |
| 34 | int rounds, int blocks); |
| 35 | |
| 36 | asmlinkage void aesbs_cbc_decrypt(u8 out[], u8 const in[], u8 const rk[], |
| 37 | int rounds, int blocks, u8 iv[]); |
| 38 | |
| 39 | asmlinkage void aesbs_ctr_encrypt(u8 out[], u8 const in[], u8 const rk[], |
Ard Biesheuvel | 1a20b96 | 2017-02-02 11:38:56 +0000 | [diff] [blame] | 40 | int rounds, int blocks, u8 ctr[], u8 final[]); |
Ard Biesheuvel | cc477bf | 2017-01-11 16:41:54 +0000 | [diff] [blame] | 41 | |
| 42 | asmlinkage void aesbs_xts_encrypt(u8 out[], u8 const in[], u8 const rk[], |
Ard Biesheuvel | 2ed8b79 | 2019-09-03 09:43:36 -0700 | [diff] [blame] | 43 | int rounds, int blocks, u8 iv[], int); |
Ard Biesheuvel | cc477bf | 2017-01-11 16:41:54 +0000 | [diff] [blame] | 44 | asmlinkage void aesbs_xts_decrypt(u8 out[], u8 const in[], u8 const rk[], |
Ard Biesheuvel | 2ed8b79 | 2019-09-03 09:43:36 -0700 | [diff] [blame] | 45 | int rounds, int blocks, u8 iv[], int); |
Ard Biesheuvel | cc477bf | 2017-01-11 16:41:54 +0000 | [diff] [blame] | 46 | |
Ard Biesheuvel | cc477bf | 2017-01-11 16:41:54 +0000 | [diff] [blame] | 47 | struct aesbs_ctx { |
| 48 | int rounds; |
| 49 | u8 rk[13 * (8 * AES_BLOCK_SIZE) + 32] __aligned(AES_BLOCK_SIZE); |
| 50 | }; |
| 51 | |
| 52 | struct aesbs_cbc_ctx { |
| 53 | struct aesbs_ctx key; |
Herbert Xu | 00b99ad2 | 2020-09-01 21:48:40 +1000 | [diff] [blame] | 54 | struct crypto_skcipher *enc_tfm; |
Ard Biesheuvel | cc477bf | 2017-01-11 16:41:54 +0000 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | struct aesbs_xts_ctx { |
| 58 | struct aesbs_ctx key; |
Ard Biesheuvel | 2ed8b79 | 2019-09-03 09:43:36 -0700 | [diff] [blame] | 59 | struct crypto_cipher *cts_tfm; |
Ard Biesheuvel | b56f5cb | 2017-02-14 21:51:01 +0000 | [diff] [blame] | 60 | struct crypto_cipher *tweak_tfm; |
Ard Biesheuvel | cc477bf | 2017-01-11 16:41:54 +0000 | [diff] [blame] | 61 | }; |
| 62 | |
Ard Biesheuvel | e5f0504 | 2019-07-02 21:41:39 +0200 | [diff] [blame] | 63 | struct aesbs_ctr_ctx { |
| 64 | struct aesbs_ctx key; /* must be first member */ |
| 65 | struct crypto_aes_ctx fallback; |
| 66 | }; |
| 67 | |
Ard Biesheuvel | cc477bf | 2017-01-11 16:41:54 +0000 | [diff] [blame] | 68 | static int aesbs_setkey(struct crypto_skcipher *tfm, const u8 *in_key, |
| 69 | unsigned int key_len) |
| 70 | { |
| 71 | struct aesbs_ctx *ctx = crypto_skcipher_ctx(tfm); |
| 72 | struct crypto_aes_ctx rk; |
| 73 | int err; |
| 74 | |
Ard Biesheuvel | aa6e2d2 | 2019-07-02 21:41:29 +0200 | [diff] [blame] | 75 | err = aes_expandkey(&rk, in_key, key_len); |
Ard Biesheuvel | cc477bf | 2017-01-11 16:41:54 +0000 | [diff] [blame] | 76 | if (err) |
| 77 | return err; |
| 78 | |
| 79 | ctx->rounds = 6 + key_len / 4; |
| 80 | |
| 81 | kernel_neon_begin(); |
| 82 | aesbs_convert_key(ctx->rk, rk.key_enc, ctx->rounds); |
| 83 | kernel_neon_end(); |
| 84 | |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | static int __ecb_crypt(struct skcipher_request *req, |
| 89 | void (*fn)(u8 out[], u8 const in[], u8 const rk[], |
| 90 | int rounds, int blocks)) |
| 91 | { |
| 92 | struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
| 93 | struct aesbs_ctx *ctx = crypto_skcipher_ctx(tfm); |
| 94 | struct skcipher_walk walk; |
| 95 | int err; |
| 96 | |
Ard Biesheuvel | 46a2277 | 2019-09-03 09:43:24 -0700 | [diff] [blame] | 97 | err = skcipher_walk_virt(&walk, req, false); |
Ard Biesheuvel | cc477bf | 2017-01-11 16:41:54 +0000 | [diff] [blame] | 98 | |
Ard Biesheuvel | cc477bf | 2017-01-11 16:41:54 +0000 | [diff] [blame] | 99 | while (walk.nbytes >= AES_BLOCK_SIZE) { |
| 100 | unsigned int blocks = walk.nbytes / AES_BLOCK_SIZE; |
| 101 | |
| 102 | if (walk.nbytes < walk.total) |
| 103 | blocks = round_down(blocks, |
| 104 | walk.stride / AES_BLOCK_SIZE); |
| 105 | |
Ard Biesheuvel | 46a2277 | 2019-09-03 09:43:24 -0700 | [diff] [blame] | 106 | kernel_neon_begin(); |
Ard Biesheuvel | cc477bf | 2017-01-11 16:41:54 +0000 | [diff] [blame] | 107 | fn(walk.dst.virt.addr, walk.src.virt.addr, ctx->rk, |
| 108 | ctx->rounds, blocks); |
Ard Biesheuvel | 46a2277 | 2019-09-03 09:43:24 -0700 | [diff] [blame] | 109 | kernel_neon_end(); |
Ard Biesheuvel | cc477bf | 2017-01-11 16:41:54 +0000 | [diff] [blame] | 110 | err = skcipher_walk_done(&walk, |
| 111 | walk.nbytes - blocks * AES_BLOCK_SIZE); |
| 112 | } |
Ard Biesheuvel | cc477bf | 2017-01-11 16:41:54 +0000 | [diff] [blame] | 113 | |
| 114 | return err; |
| 115 | } |
| 116 | |
| 117 | static int ecb_encrypt(struct skcipher_request *req) |
| 118 | { |
| 119 | return __ecb_crypt(req, aesbs_ecb_encrypt); |
| 120 | } |
| 121 | |
| 122 | static int ecb_decrypt(struct skcipher_request *req) |
| 123 | { |
| 124 | return __ecb_crypt(req, aesbs_ecb_decrypt); |
| 125 | } |
| 126 | |
| 127 | static int aesbs_cbc_setkey(struct crypto_skcipher *tfm, const u8 *in_key, |
| 128 | unsigned int key_len) |
| 129 | { |
| 130 | struct aesbs_cbc_ctx *ctx = crypto_skcipher_ctx(tfm); |
| 131 | struct crypto_aes_ctx rk; |
| 132 | int err; |
| 133 | |
Ard Biesheuvel | aa6e2d2 | 2019-07-02 21:41:29 +0200 | [diff] [blame] | 134 | err = aes_expandkey(&rk, in_key, key_len); |
Ard Biesheuvel | cc477bf | 2017-01-11 16:41:54 +0000 | [diff] [blame] | 135 | if (err) |
| 136 | return err; |
| 137 | |
| 138 | ctx->key.rounds = 6 + key_len / 4; |
| 139 | |
Ard Biesheuvel | cc477bf | 2017-01-11 16:41:54 +0000 | [diff] [blame] | 140 | kernel_neon_begin(); |
| 141 | aesbs_convert_key(ctx->key.rk, rk.key_enc, ctx->key.rounds); |
| 142 | kernel_neon_end(); |
Torsten Duwe | 82ff493 | 2020-03-13 12:02:58 +0100 | [diff] [blame] | 143 | memzero_explicit(&rk, sizeof(rk)); |
Ard Biesheuvel | cc477bf | 2017-01-11 16:41:54 +0000 | [diff] [blame] | 144 | |
Herbert Xu | 00b99ad2 | 2020-09-01 21:48:40 +1000 | [diff] [blame] | 145 | return crypto_skcipher_setkey(ctx->enc_tfm, in_key, key_len); |
Ard Biesheuvel | cc477bf | 2017-01-11 16:41:54 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | static int cbc_encrypt(struct skcipher_request *req) |
| 149 | { |
Herbert Xu | 00b99ad2 | 2020-09-01 21:48:40 +1000 | [diff] [blame] | 150 | struct skcipher_request *subreq = skcipher_request_ctx(req); |
| 151 | struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
| 152 | struct aesbs_cbc_ctx *ctx = crypto_skcipher_ctx(tfm); |
| 153 | |
| 154 | skcipher_request_set_tfm(subreq, ctx->enc_tfm); |
| 155 | skcipher_request_set_callback(subreq, |
| 156 | skcipher_request_flags(req), |
| 157 | NULL, NULL); |
| 158 | skcipher_request_set_crypt(subreq, req->src, req->dst, |
| 159 | req->cryptlen, req->iv); |
| 160 | |
| 161 | return crypto_skcipher_encrypt(subreq); |
Ard Biesheuvel | cc477bf | 2017-01-11 16:41:54 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | static int cbc_decrypt(struct skcipher_request *req) |
| 165 | { |
| 166 | struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
| 167 | struct aesbs_cbc_ctx *ctx = crypto_skcipher_ctx(tfm); |
| 168 | struct skcipher_walk walk; |
| 169 | int err; |
| 170 | |
Ard Biesheuvel | 46a2277 | 2019-09-03 09:43:24 -0700 | [diff] [blame] | 171 | err = skcipher_walk_virt(&walk, req, false); |
Ard Biesheuvel | cc477bf | 2017-01-11 16:41:54 +0000 | [diff] [blame] | 172 | |
Ard Biesheuvel | cc477bf | 2017-01-11 16:41:54 +0000 | [diff] [blame] | 173 | while (walk.nbytes >= AES_BLOCK_SIZE) { |
| 174 | unsigned int blocks = walk.nbytes / AES_BLOCK_SIZE; |
| 175 | |
| 176 | if (walk.nbytes < walk.total) |
| 177 | blocks = round_down(blocks, |
| 178 | walk.stride / AES_BLOCK_SIZE); |
| 179 | |
Ard Biesheuvel | 46a2277 | 2019-09-03 09:43:24 -0700 | [diff] [blame] | 180 | kernel_neon_begin(); |
Ard Biesheuvel | cc477bf | 2017-01-11 16:41:54 +0000 | [diff] [blame] | 181 | aesbs_cbc_decrypt(walk.dst.virt.addr, walk.src.virt.addr, |
| 182 | ctx->key.rk, ctx->key.rounds, blocks, |
| 183 | walk.iv); |
Ard Biesheuvel | 46a2277 | 2019-09-03 09:43:24 -0700 | [diff] [blame] | 184 | kernel_neon_end(); |
Ard Biesheuvel | cc477bf | 2017-01-11 16:41:54 +0000 | [diff] [blame] | 185 | err = skcipher_walk_done(&walk, |
| 186 | walk.nbytes - blocks * AES_BLOCK_SIZE); |
| 187 | } |
Ard Biesheuvel | cc477bf | 2017-01-11 16:41:54 +0000 | [diff] [blame] | 188 | |
| 189 | return err; |
| 190 | } |
| 191 | |
Herbert Xu | 00b99ad2 | 2020-09-01 21:48:40 +1000 | [diff] [blame] | 192 | static int cbc_init(struct crypto_skcipher *tfm) |
Ard Biesheuvel | b56f5cb | 2017-02-14 21:51:01 +0000 | [diff] [blame] | 193 | { |
Herbert Xu | 00b99ad2 | 2020-09-01 21:48:40 +1000 | [diff] [blame] | 194 | struct aesbs_cbc_ctx *ctx = crypto_skcipher_ctx(tfm); |
| 195 | unsigned int reqsize; |
Ard Biesheuvel | b56f5cb | 2017-02-14 21:51:01 +0000 | [diff] [blame] | 196 | |
Horia Geantă | a2715fb | 2020-10-28 11:03:20 +0200 | [diff] [blame] | 197 | ctx->enc_tfm = crypto_alloc_skcipher("cbc(aes)", 0, CRYPTO_ALG_ASYNC | |
| 198 | CRYPTO_ALG_NEED_FALLBACK); |
Herbert Xu | 00b99ad2 | 2020-09-01 21:48:40 +1000 | [diff] [blame] | 199 | if (IS_ERR(ctx->enc_tfm)) |
| 200 | return PTR_ERR(ctx->enc_tfm); |
Gomonovych, Vasyl | 26d85e5 | 2017-11-28 00:06:06 +0100 | [diff] [blame] | 201 | |
Herbert Xu | 00b99ad2 | 2020-09-01 21:48:40 +1000 | [diff] [blame] | 202 | reqsize = sizeof(struct skcipher_request); |
| 203 | reqsize += crypto_skcipher_reqsize(ctx->enc_tfm); |
| 204 | crypto_skcipher_set_reqsize(tfm, reqsize); |
| 205 | |
| 206 | return 0; |
Ard Biesheuvel | b56f5cb | 2017-02-14 21:51:01 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Herbert Xu | 00b99ad2 | 2020-09-01 21:48:40 +1000 | [diff] [blame] | 209 | static void cbc_exit(struct crypto_skcipher *tfm) |
Ard Biesheuvel | b56f5cb | 2017-02-14 21:51:01 +0000 | [diff] [blame] | 210 | { |
Herbert Xu | 00b99ad2 | 2020-09-01 21:48:40 +1000 | [diff] [blame] | 211 | struct aesbs_cbc_ctx *ctx = crypto_skcipher_ctx(tfm); |
Ard Biesheuvel | b56f5cb | 2017-02-14 21:51:01 +0000 | [diff] [blame] | 212 | |
Herbert Xu | 00b99ad2 | 2020-09-01 21:48:40 +1000 | [diff] [blame] | 213 | crypto_free_skcipher(ctx->enc_tfm); |
Ard Biesheuvel | b56f5cb | 2017-02-14 21:51:01 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Ard Biesheuvel | e5f0504 | 2019-07-02 21:41:39 +0200 | [diff] [blame] | 216 | static int aesbs_ctr_setkey_sync(struct crypto_skcipher *tfm, const u8 *in_key, |
| 217 | unsigned int key_len) |
| 218 | { |
| 219 | struct aesbs_ctr_ctx *ctx = crypto_skcipher_ctx(tfm); |
| 220 | int err; |
| 221 | |
| 222 | err = aes_expandkey(&ctx->fallback, in_key, key_len); |
| 223 | if (err) |
| 224 | return err; |
| 225 | |
| 226 | ctx->key.rounds = 6 + key_len / 4; |
| 227 | |
| 228 | kernel_neon_begin(); |
| 229 | aesbs_convert_key(ctx->key.rk, ctx->fallback.key_enc, ctx->key.rounds); |
| 230 | kernel_neon_end(); |
| 231 | |
| 232 | return 0; |
| 233 | } |
| 234 | |
Ard Biesheuvel | cc477bf | 2017-01-11 16:41:54 +0000 | [diff] [blame] | 235 | static int ctr_encrypt(struct skcipher_request *req) |
| 236 | { |
| 237 | struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
| 238 | struct aesbs_ctx *ctx = crypto_skcipher_ctx(tfm); |
| 239 | struct skcipher_walk walk; |
Ard Biesheuvel | 1a20b96 | 2017-02-02 11:38:56 +0000 | [diff] [blame] | 240 | u8 buf[AES_BLOCK_SIZE]; |
Ard Biesheuvel | cc477bf | 2017-01-11 16:41:54 +0000 | [diff] [blame] | 241 | int err; |
| 242 | |
Ard Biesheuvel | 46a2277 | 2019-09-03 09:43:24 -0700 | [diff] [blame] | 243 | err = skcipher_walk_virt(&walk, req, false); |
Ard Biesheuvel | cc477bf | 2017-01-11 16:41:54 +0000 | [diff] [blame] | 244 | |
Ard Biesheuvel | cc477bf | 2017-01-11 16:41:54 +0000 | [diff] [blame] | 245 | while (walk.nbytes > 0) { |
| 246 | unsigned int blocks = walk.nbytes / AES_BLOCK_SIZE; |
Ard Biesheuvel | 1a20b96 | 2017-02-02 11:38:56 +0000 | [diff] [blame] | 247 | u8 *final = (walk.total % AES_BLOCK_SIZE) ? buf : NULL; |
Ard Biesheuvel | cc477bf | 2017-01-11 16:41:54 +0000 | [diff] [blame] | 248 | |
| 249 | if (walk.nbytes < walk.total) { |
| 250 | blocks = round_down(blocks, |
| 251 | walk.stride / AES_BLOCK_SIZE); |
Ard Biesheuvel | 1a20b96 | 2017-02-02 11:38:56 +0000 | [diff] [blame] | 252 | final = NULL; |
Ard Biesheuvel | cc477bf | 2017-01-11 16:41:54 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Ard Biesheuvel | 46a2277 | 2019-09-03 09:43:24 -0700 | [diff] [blame] | 255 | kernel_neon_begin(); |
Ard Biesheuvel | cc477bf | 2017-01-11 16:41:54 +0000 | [diff] [blame] | 256 | aesbs_ctr_encrypt(walk.dst.virt.addr, walk.src.virt.addr, |
| 257 | ctx->rk, ctx->rounds, blocks, walk.iv, final); |
Ard Biesheuvel | 46a2277 | 2019-09-03 09:43:24 -0700 | [diff] [blame] | 258 | kernel_neon_end(); |
Ard Biesheuvel | cc477bf | 2017-01-11 16:41:54 +0000 | [diff] [blame] | 259 | |
| 260 | if (final) { |
| 261 | u8 *dst = walk.dst.virt.addr + blocks * AES_BLOCK_SIZE; |
| 262 | u8 *src = walk.src.virt.addr + blocks * AES_BLOCK_SIZE; |
| 263 | |
Ard Biesheuvel | 45fe93d | 2017-07-24 11:28:04 +0100 | [diff] [blame] | 264 | crypto_xor_cpy(dst, src, final, |
| 265 | walk.total % AES_BLOCK_SIZE); |
Ard Biesheuvel | cc477bf | 2017-01-11 16:41:54 +0000 | [diff] [blame] | 266 | |
| 267 | err = skcipher_walk_done(&walk, 0); |
| 268 | break; |
| 269 | } |
| 270 | err = skcipher_walk_done(&walk, |
| 271 | walk.nbytes - blocks * AES_BLOCK_SIZE); |
| 272 | } |
Ard Biesheuvel | cc477bf | 2017-01-11 16:41:54 +0000 | [diff] [blame] | 273 | |
| 274 | return err; |
| 275 | } |
| 276 | |
Ard Biesheuvel | e5f0504 | 2019-07-02 21:41:39 +0200 | [diff] [blame] | 277 | static void ctr_encrypt_one(struct crypto_skcipher *tfm, const u8 *src, u8 *dst) |
| 278 | { |
| 279 | struct aesbs_ctr_ctx *ctx = crypto_skcipher_ctx(tfm); |
| 280 | unsigned long flags; |
| 281 | |
| 282 | /* |
| 283 | * Temporarily disable interrupts to avoid races where |
| 284 | * cachelines are evicted when the CPU is interrupted |
| 285 | * to do something else. |
| 286 | */ |
| 287 | local_irq_save(flags); |
| 288 | aes_encrypt(&ctx->fallback, dst, src); |
| 289 | local_irq_restore(flags); |
| 290 | } |
| 291 | |
| 292 | static int ctr_encrypt_sync(struct skcipher_request *req) |
| 293 | { |
| 294 | if (!crypto_simd_usable()) |
| 295 | return crypto_ctr_encrypt_walk(req, ctr_encrypt_one); |
| 296 | |
| 297 | return ctr_encrypt(req); |
| 298 | } |
| 299 | |
Ard Biesheuvel | cc477bf | 2017-01-11 16:41:54 +0000 | [diff] [blame] | 300 | static int aesbs_xts_setkey(struct crypto_skcipher *tfm, const u8 *in_key, |
| 301 | unsigned int key_len) |
| 302 | { |
| 303 | struct aesbs_xts_ctx *ctx = crypto_skcipher_ctx(tfm); |
Ard Biesheuvel | cc477bf | 2017-01-11 16:41:54 +0000 | [diff] [blame] | 304 | int err; |
| 305 | |
| 306 | err = xts_verify_key(tfm, in_key, key_len); |
| 307 | if (err) |
| 308 | return err; |
| 309 | |
| 310 | key_len /= 2; |
Ard Biesheuvel | 2ed8b79 | 2019-09-03 09:43:36 -0700 | [diff] [blame] | 311 | err = crypto_cipher_setkey(ctx->cts_tfm, in_key, key_len); |
| 312 | if (err) |
| 313 | return err; |
Ard Biesheuvel | b56f5cb | 2017-02-14 21:51:01 +0000 | [diff] [blame] | 314 | err = crypto_cipher_setkey(ctx->tweak_tfm, in_key + key_len, key_len); |
Ard Biesheuvel | cc477bf | 2017-01-11 16:41:54 +0000 | [diff] [blame] | 315 | if (err) |
| 316 | return err; |
| 317 | |
Ard Biesheuvel | cc477bf | 2017-01-11 16:41:54 +0000 | [diff] [blame] | 318 | return aesbs_setkey(tfm, in_key, key_len); |
| 319 | } |
| 320 | |
Ard Biesheuvel | 3ebbc03 | 2020-09-16 15:36:42 +0300 | [diff] [blame] | 321 | static int xts_init(struct crypto_skcipher *tfm) |
Ard Biesheuvel | b56f5cb | 2017-02-14 21:51:01 +0000 | [diff] [blame] | 322 | { |
Ard Biesheuvel | 3ebbc03 | 2020-09-16 15:36:42 +0300 | [diff] [blame] | 323 | struct aesbs_xts_ctx *ctx = crypto_skcipher_ctx(tfm); |
Ard Biesheuvel | b56f5cb | 2017-02-14 21:51:01 +0000 | [diff] [blame] | 324 | |
Ard Biesheuvel | 2ed8b79 | 2019-09-03 09:43:36 -0700 | [diff] [blame] | 325 | ctx->cts_tfm = crypto_alloc_cipher("aes", 0, 0); |
| 326 | if (IS_ERR(ctx->cts_tfm)) |
| 327 | return PTR_ERR(ctx->cts_tfm); |
| 328 | |
Ard Biesheuvel | b56f5cb | 2017-02-14 21:51:01 +0000 | [diff] [blame] | 329 | ctx->tweak_tfm = crypto_alloc_cipher("aes", 0, 0); |
Ard Biesheuvel | 2ed8b79 | 2019-09-03 09:43:36 -0700 | [diff] [blame] | 330 | if (IS_ERR(ctx->tweak_tfm)) |
| 331 | crypto_free_cipher(ctx->cts_tfm); |
Gomonovych, Vasyl | 26d85e5 | 2017-11-28 00:06:06 +0100 | [diff] [blame] | 332 | |
| 333 | return PTR_ERR_OR_ZERO(ctx->tweak_tfm); |
Ard Biesheuvel | b56f5cb | 2017-02-14 21:51:01 +0000 | [diff] [blame] | 334 | } |
| 335 | |
Ard Biesheuvel | 3ebbc03 | 2020-09-16 15:36:42 +0300 | [diff] [blame] | 336 | static void xts_exit(struct crypto_skcipher *tfm) |
Ard Biesheuvel | b56f5cb | 2017-02-14 21:51:01 +0000 | [diff] [blame] | 337 | { |
Ard Biesheuvel | 3ebbc03 | 2020-09-16 15:36:42 +0300 | [diff] [blame] | 338 | struct aesbs_xts_ctx *ctx = crypto_skcipher_ctx(tfm); |
Ard Biesheuvel | b56f5cb | 2017-02-14 21:51:01 +0000 | [diff] [blame] | 339 | |
| 340 | crypto_free_cipher(ctx->tweak_tfm); |
Ard Biesheuvel | 2ed8b79 | 2019-09-03 09:43:36 -0700 | [diff] [blame] | 341 | crypto_free_cipher(ctx->cts_tfm); |
Ard Biesheuvel | b56f5cb | 2017-02-14 21:51:01 +0000 | [diff] [blame] | 342 | } |
| 343 | |
Ard Biesheuvel | 2ed8b79 | 2019-09-03 09:43:36 -0700 | [diff] [blame] | 344 | static int __xts_crypt(struct skcipher_request *req, bool encrypt, |
Ard Biesheuvel | cc477bf | 2017-01-11 16:41:54 +0000 | [diff] [blame] | 345 | void (*fn)(u8 out[], u8 const in[], u8 const rk[], |
Ard Biesheuvel | 2ed8b79 | 2019-09-03 09:43:36 -0700 | [diff] [blame] | 346 | int rounds, int blocks, u8 iv[], int)) |
Ard Biesheuvel | cc477bf | 2017-01-11 16:41:54 +0000 | [diff] [blame] | 347 | { |
| 348 | struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
| 349 | struct aesbs_xts_ctx *ctx = crypto_skcipher_ctx(tfm); |
Ard Biesheuvel | 2ed8b79 | 2019-09-03 09:43:36 -0700 | [diff] [blame] | 350 | int tail = req->cryptlen % AES_BLOCK_SIZE; |
| 351 | struct skcipher_request subreq; |
| 352 | u8 buf[2 * AES_BLOCK_SIZE]; |
Ard Biesheuvel | cc477bf | 2017-01-11 16:41:54 +0000 | [diff] [blame] | 353 | struct skcipher_walk walk; |
| 354 | int err; |
| 355 | |
Ard Biesheuvel | 2ed8b79 | 2019-09-03 09:43:36 -0700 | [diff] [blame] | 356 | if (req->cryptlen < AES_BLOCK_SIZE) |
| 357 | return -EINVAL; |
| 358 | |
| 359 | if (unlikely(tail)) { |
| 360 | skcipher_request_set_tfm(&subreq, tfm); |
| 361 | skcipher_request_set_callback(&subreq, |
| 362 | skcipher_request_flags(req), |
| 363 | NULL, NULL); |
| 364 | skcipher_request_set_crypt(&subreq, req->src, req->dst, |
| 365 | req->cryptlen - tail, req->iv); |
| 366 | req = &subreq; |
| 367 | } |
| 368 | |
Ard Biesheuvel | cc477bf | 2017-01-11 16:41:54 +0000 | [diff] [blame] | 369 | err = skcipher_walk_virt(&walk, req, true); |
Eric Biggers | 767f015 | 2019-04-09 23:46:31 -0700 | [diff] [blame] | 370 | if (err) |
| 371 | return err; |
Ard Biesheuvel | cc477bf | 2017-01-11 16:41:54 +0000 | [diff] [blame] | 372 | |
Ard Biesheuvel | b56f5cb | 2017-02-14 21:51:01 +0000 | [diff] [blame] | 373 | crypto_cipher_encrypt_one(ctx->tweak_tfm, walk.iv, walk.iv); |
Ard Biesheuvel | cc477bf | 2017-01-11 16:41:54 +0000 | [diff] [blame] | 374 | |
Ard Biesheuvel | cc477bf | 2017-01-11 16:41:54 +0000 | [diff] [blame] | 375 | while (walk.nbytes >= AES_BLOCK_SIZE) { |
| 376 | unsigned int blocks = walk.nbytes / AES_BLOCK_SIZE; |
Ard Biesheuvel | 2ed8b79 | 2019-09-03 09:43:36 -0700 | [diff] [blame] | 377 | int reorder_last_tweak = !encrypt && tail > 0; |
Ard Biesheuvel | cc477bf | 2017-01-11 16:41:54 +0000 | [diff] [blame] | 378 | |
Ard Biesheuvel | 2ed8b79 | 2019-09-03 09:43:36 -0700 | [diff] [blame] | 379 | if (walk.nbytes < walk.total) { |
Ard Biesheuvel | cc477bf | 2017-01-11 16:41:54 +0000 | [diff] [blame] | 380 | blocks = round_down(blocks, |
| 381 | walk.stride / AES_BLOCK_SIZE); |
Ard Biesheuvel | 2ed8b79 | 2019-09-03 09:43:36 -0700 | [diff] [blame] | 382 | reorder_last_tweak = 0; |
| 383 | } |
Ard Biesheuvel | cc477bf | 2017-01-11 16:41:54 +0000 | [diff] [blame] | 384 | |
Ard Biesheuvel | 46a2277 | 2019-09-03 09:43:24 -0700 | [diff] [blame] | 385 | kernel_neon_begin(); |
Ard Biesheuvel | cc477bf | 2017-01-11 16:41:54 +0000 | [diff] [blame] | 386 | fn(walk.dst.virt.addr, walk.src.virt.addr, ctx->key.rk, |
Ard Biesheuvel | 2ed8b79 | 2019-09-03 09:43:36 -0700 | [diff] [blame] | 387 | ctx->key.rounds, blocks, walk.iv, reorder_last_tweak); |
Ard Biesheuvel | 46a2277 | 2019-09-03 09:43:24 -0700 | [diff] [blame] | 388 | kernel_neon_end(); |
Ard Biesheuvel | cc477bf | 2017-01-11 16:41:54 +0000 | [diff] [blame] | 389 | err = skcipher_walk_done(&walk, |
| 390 | walk.nbytes - blocks * AES_BLOCK_SIZE); |
| 391 | } |
Ard Biesheuvel | cc477bf | 2017-01-11 16:41:54 +0000 | [diff] [blame] | 392 | |
Ard Biesheuvel | 2ed8b79 | 2019-09-03 09:43:36 -0700 | [diff] [blame] | 393 | if (err || likely(!tail)) |
| 394 | return err; |
| 395 | |
| 396 | /* handle ciphertext stealing */ |
| 397 | scatterwalk_map_and_copy(buf, req->dst, req->cryptlen - AES_BLOCK_SIZE, |
| 398 | AES_BLOCK_SIZE, 0); |
| 399 | memcpy(buf + AES_BLOCK_SIZE, buf, tail); |
| 400 | scatterwalk_map_and_copy(buf, req->src, req->cryptlen, tail, 0); |
| 401 | |
| 402 | crypto_xor(buf, req->iv, AES_BLOCK_SIZE); |
| 403 | |
| 404 | if (encrypt) |
| 405 | crypto_cipher_encrypt_one(ctx->cts_tfm, buf, buf); |
| 406 | else |
| 407 | crypto_cipher_decrypt_one(ctx->cts_tfm, buf, buf); |
| 408 | |
| 409 | crypto_xor(buf, req->iv, AES_BLOCK_SIZE); |
| 410 | |
| 411 | scatterwalk_map_and_copy(buf, req->dst, req->cryptlen - AES_BLOCK_SIZE, |
| 412 | AES_BLOCK_SIZE + tail, 1); |
| 413 | return 0; |
Ard Biesheuvel | cc477bf | 2017-01-11 16:41:54 +0000 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | static int xts_encrypt(struct skcipher_request *req) |
| 417 | { |
Ard Biesheuvel | 2ed8b79 | 2019-09-03 09:43:36 -0700 | [diff] [blame] | 418 | return __xts_crypt(req, true, aesbs_xts_encrypt); |
Ard Biesheuvel | cc477bf | 2017-01-11 16:41:54 +0000 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | static int xts_decrypt(struct skcipher_request *req) |
| 422 | { |
Ard Biesheuvel | 2ed8b79 | 2019-09-03 09:43:36 -0700 | [diff] [blame] | 423 | return __xts_crypt(req, false, aesbs_xts_decrypt); |
Ard Biesheuvel | cc477bf | 2017-01-11 16:41:54 +0000 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | static struct skcipher_alg aes_algs[] = { { |
| 427 | .base.cra_name = "__ecb(aes)", |
| 428 | .base.cra_driver_name = "__ecb-aes-neonbs", |
| 429 | .base.cra_priority = 250, |
| 430 | .base.cra_blocksize = AES_BLOCK_SIZE, |
| 431 | .base.cra_ctxsize = sizeof(struct aesbs_ctx), |
| 432 | .base.cra_module = THIS_MODULE, |
| 433 | .base.cra_flags = CRYPTO_ALG_INTERNAL, |
| 434 | |
| 435 | .min_keysize = AES_MIN_KEY_SIZE, |
| 436 | .max_keysize = AES_MAX_KEY_SIZE, |
| 437 | .walksize = 8 * AES_BLOCK_SIZE, |
| 438 | .setkey = aesbs_setkey, |
| 439 | .encrypt = ecb_encrypt, |
| 440 | .decrypt = ecb_decrypt, |
| 441 | }, { |
| 442 | .base.cra_name = "__cbc(aes)", |
| 443 | .base.cra_driver_name = "__cbc-aes-neonbs", |
| 444 | .base.cra_priority = 250, |
| 445 | .base.cra_blocksize = AES_BLOCK_SIZE, |
| 446 | .base.cra_ctxsize = sizeof(struct aesbs_cbc_ctx), |
| 447 | .base.cra_module = THIS_MODULE, |
Horia Geantă | a2715fb | 2020-10-28 11:03:20 +0200 | [diff] [blame] | 448 | .base.cra_flags = CRYPTO_ALG_INTERNAL | |
| 449 | CRYPTO_ALG_NEED_FALLBACK, |
Ard Biesheuvel | cc477bf | 2017-01-11 16:41:54 +0000 | [diff] [blame] | 450 | |
| 451 | .min_keysize = AES_MIN_KEY_SIZE, |
| 452 | .max_keysize = AES_MAX_KEY_SIZE, |
| 453 | .walksize = 8 * AES_BLOCK_SIZE, |
| 454 | .ivsize = AES_BLOCK_SIZE, |
| 455 | .setkey = aesbs_cbc_setkey, |
| 456 | .encrypt = cbc_encrypt, |
| 457 | .decrypt = cbc_decrypt, |
Herbert Xu | 00b99ad2 | 2020-09-01 21:48:40 +1000 | [diff] [blame] | 458 | .init = cbc_init, |
| 459 | .exit = cbc_exit, |
Ard Biesheuvel | cc477bf | 2017-01-11 16:41:54 +0000 | [diff] [blame] | 460 | }, { |
| 461 | .base.cra_name = "__ctr(aes)", |
| 462 | .base.cra_driver_name = "__ctr-aes-neonbs", |
| 463 | .base.cra_priority = 250, |
| 464 | .base.cra_blocksize = 1, |
| 465 | .base.cra_ctxsize = sizeof(struct aesbs_ctx), |
| 466 | .base.cra_module = THIS_MODULE, |
| 467 | .base.cra_flags = CRYPTO_ALG_INTERNAL, |
| 468 | |
| 469 | .min_keysize = AES_MIN_KEY_SIZE, |
| 470 | .max_keysize = AES_MAX_KEY_SIZE, |
| 471 | .chunksize = AES_BLOCK_SIZE, |
| 472 | .walksize = 8 * AES_BLOCK_SIZE, |
| 473 | .ivsize = AES_BLOCK_SIZE, |
| 474 | .setkey = aesbs_setkey, |
| 475 | .encrypt = ctr_encrypt, |
| 476 | .decrypt = ctr_encrypt, |
| 477 | }, { |
Ard Biesheuvel | e5f0504 | 2019-07-02 21:41:39 +0200 | [diff] [blame] | 478 | .base.cra_name = "ctr(aes)", |
| 479 | .base.cra_driver_name = "ctr-aes-neonbs-sync", |
| 480 | .base.cra_priority = 250 - 1, |
| 481 | .base.cra_blocksize = 1, |
| 482 | .base.cra_ctxsize = sizeof(struct aesbs_ctr_ctx), |
| 483 | .base.cra_module = THIS_MODULE, |
| 484 | |
| 485 | .min_keysize = AES_MIN_KEY_SIZE, |
| 486 | .max_keysize = AES_MAX_KEY_SIZE, |
| 487 | .chunksize = AES_BLOCK_SIZE, |
| 488 | .walksize = 8 * AES_BLOCK_SIZE, |
| 489 | .ivsize = AES_BLOCK_SIZE, |
| 490 | .setkey = aesbs_ctr_setkey_sync, |
| 491 | .encrypt = ctr_encrypt_sync, |
| 492 | .decrypt = ctr_encrypt_sync, |
| 493 | }, { |
Ard Biesheuvel | cc477bf | 2017-01-11 16:41:54 +0000 | [diff] [blame] | 494 | .base.cra_name = "__xts(aes)", |
| 495 | .base.cra_driver_name = "__xts-aes-neonbs", |
| 496 | .base.cra_priority = 250, |
| 497 | .base.cra_blocksize = AES_BLOCK_SIZE, |
| 498 | .base.cra_ctxsize = sizeof(struct aesbs_xts_ctx), |
| 499 | .base.cra_module = THIS_MODULE, |
| 500 | .base.cra_flags = CRYPTO_ALG_INTERNAL, |
| 501 | |
| 502 | .min_keysize = 2 * AES_MIN_KEY_SIZE, |
| 503 | .max_keysize = 2 * AES_MAX_KEY_SIZE, |
| 504 | .walksize = 8 * AES_BLOCK_SIZE, |
| 505 | .ivsize = AES_BLOCK_SIZE, |
| 506 | .setkey = aesbs_xts_setkey, |
| 507 | .encrypt = xts_encrypt, |
| 508 | .decrypt = xts_decrypt, |
Ard Biesheuvel | 3ebbc03 | 2020-09-16 15:36:42 +0300 | [diff] [blame] | 509 | .init = xts_init, |
| 510 | .exit = xts_exit, |
Ard Biesheuvel | cc477bf | 2017-01-11 16:41:54 +0000 | [diff] [blame] | 511 | } }; |
| 512 | |
| 513 | static struct simd_skcipher_alg *aes_simd_algs[ARRAY_SIZE(aes_algs)]; |
| 514 | |
| 515 | static void aes_exit(void) |
| 516 | { |
| 517 | int i; |
| 518 | |
| 519 | for (i = 0; i < ARRAY_SIZE(aes_simd_algs); i++) |
| 520 | if (aes_simd_algs[i]) |
| 521 | simd_skcipher_free(aes_simd_algs[i]); |
| 522 | |
| 523 | crypto_unregister_skciphers(aes_algs, ARRAY_SIZE(aes_algs)); |
| 524 | } |
| 525 | |
| 526 | static int __init aes_init(void) |
| 527 | { |
| 528 | struct simd_skcipher_alg *simd; |
| 529 | const char *basename; |
| 530 | const char *algname; |
| 531 | const char *drvname; |
| 532 | int err; |
| 533 | int i; |
| 534 | |
| 535 | if (!(elf_hwcap & HWCAP_NEON)) |
| 536 | return -ENODEV; |
| 537 | |
| 538 | err = crypto_register_skciphers(aes_algs, ARRAY_SIZE(aes_algs)); |
| 539 | if (err) |
| 540 | return err; |
| 541 | |
| 542 | for (i = 0; i < ARRAY_SIZE(aes_algs); i++) { |
| 543 | if (!(aes_algs[i].base.cra_flags & CRYPTO_ALG_INTERNAL)) |
| 544 | continue; |
| 545 | |
| 546 | algname = aes_algs[i].base.cra_name + 2; |
| 547 | drvname = aes_algs[i].base.cra_driver_name + 2; |
| 548 | basename = aes_algs[i].base.cra_driver_name; |
| 549 | simd = simd_skcipher_create_compat(algname, drvname, basename); |
| 550 | err = PTR_ERR(simd); |
| 551 | if (IS_ERR(simd)) |
| 552 | goto unregister_simds; |
| 553 | |
| 554 | aes_simd_algs[i] = simd; |
| 555 | } |
| 556 | return 0; |
| 557 | |
| 558 | unregister_simds: |
| 559 | aes_exit(); |
| 560 | return err; |
| 561 | } |
| 562 | |
Ard Biesheuvel | b56f5cb | 2017-02-14 21:51:01 +0000 | [diff] [blame] | 563 | late_initcall(aes_init); |
Ard Biesheuvel | cc477bf | 2017-01-11 16:41:54 +0000 | [diff] [blame] | 564 | module_exit(aes_exit); |