| Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
| Ard Biesheuvel | 8646485 | 2015-03-10 09:47:47 +0100 | [diff] [blame] | 2 | /* |
| 3 | * aes-ce-glue.c - wrapper code for ARMv8 AES |
| 4 | * |
| 5 | * Copyright (C) 2015 Linaro Ltd <ard.biesheuvel@linaro.org> |
| Ard Biesheuvel | 8646485 | 2015-03-10 09:47:47 +0100 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <asm/hwcap.h> |
| 9 | #include <asm/neon.h> |
| Ard Biesheuvel | 8646485 | 2015-03-10 09:47:47 +0100 | [diff] [blame] | 10 | #include <crypto/aes.h> |
| Herbert Xu | da40e7a | 2016-11-22 20:08:37 +0800 | [diff] [blame] | 11 | #include <crypto/internal/simd.h> |
| 12 | #include <crypto/internal/skcipher.h> |
| Ard Biesheuvel | 4d8061a | 2017-05-21 10:23:36 +0000 | [diff] [blame] | 13 | #include <linux/cpufeature.h> |
| Ard Biesheuvel | 8646485 | 2015-03-10 09:47:47 +0100 | [diff] [blame] | 14 | #include <linux/module.h> |
| Stephan Mueller | 49abc0d | 2016-02-17 07:00:01 +0100 | [diff] [blame] | 15 | #include <crypto/xts.h> |
| Ard Biesheuvel | 8646485 | 2015-03-10 09:47:47 +0100 | [diff] [blame] | 16 | |
| 17 | MODULE_DESCRIPTION("AES-ECB/CBC/CTR/XTS using ARMv8 Crypto Extensions"); |
| 18 | MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>"); |
| 19 | MODULE_LICENSE("GPL v2"); |
| 20 | |
| 21 | /* defined in aes-ce-core.S */ |
| 22 | asmlinkage u32 ce_aes_sub(u32 input); |
| 23 | asmlinkage void ce_aes_invert(void *dst, void *src); |
| 24 | |
| 25 | asmlinkage void ce_aes_ecb_encrypt(u8 out[], u8 const in[], u8 const rk[], |
| 26 | int rounds, int blocks); |
| 27 | asmlinkage void ce_aes_ecb_decrypt(u8 out[], u8 const in[], u8 const rk[], |
| 28 | int rounds, int blocks); |
| 29 | |
| 30 | asmlinkage void ce_aes_cbc_encrypt(u8 out[], u8 const in[], u8 const rk[], |
| 31 | int rounds, int blocks, u8 iv[]); |
| 32 | asmlinkage void ce_aes_cbc_decrypt(u8 out[], u8 const in[], u8 const rk[], |
| 33 | int rounds, int blocks, u8 iv[]); |
| 34 | |
| 35 | asmlinkage void ce_aes_ctr_encrypt(u8 out[], u8 const in[], u8 const rk[], |
| 36 | int rounds, int blocks, u8 ctr[]); |
| 37 | |
| 38 | asmlinkage void ce_aes_xts_encrypt(u8 out[], u8 const in[], u8 const rk1[], |
| 39 | int rounds, int blocks, u8 iv[], |
| 40 | u8 const rk2[], int first); |
| 41 | asmlinkage void ce_aes_xts_decrypt(u8 out[], u8 const in[], u8 const rk1[], |
| 42 | int rounds, int blocks, u8 iv[], |
| 43 | u8 const rk2[], int first); |
| 44 | |
| 45 | struct aes_block { |
| 46 | u8 b[AES_BLOCK_SIZE]; |
| 47 | }; |
| 48 | |
| 49 | static int num_rounds(struct crypto_aes_ctx *ctx) |
| 50 | { |
| 51 | /* |
| 52 | * # of rounds specified by AES: |
| 53 | * 128 bit key 10 rounds |
| 54 | * 192 bit key 12 rounds |
| 55 | * 256 bit key 14 rounds |
| 56 | * => n byte key => 6 + (n/4) rounds |
| 57 | */ |
| 58 | return 6 + ctx->key_length / 4; |
| 59 | } |
| 60 | |
| 61 | static int ce_aes_expandkey(struct crypto_aes_ctx *ctx, const u8 *in_key, |
| 62 | unsigned int key_len) |
| 63 | { |
| 64 | /* |
| 65 | * The AES key schedule round constants |
| 66 | */ |
| 67 | static u8 const rcon[] = { |
| 68 | 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, |
| 69 | }; |
| 70 | |
| 71 | u32 kwords = key_len / sizeof(u32); |
| 72 | struct aes_block *key_enc, *key_dec; |
| 73 | int i, j; |
| 74 | |
| 75 | if (key_len != AES_KEYSIZE_128 && |
| 76 | key_len != AES_KEYSIZE_192 && |
| 77 | key_len != AES_KEYSIZE_256) |
| 78 | return -EINVAL; |
| 79 | |
| 80 | memcpy(ctx->key_enc, in_key, key_len); |
| 81 | ctx->key_length = key_len; |
| 82 | |
| 83 | kernel_neon_begin(); |
| 84 | for (i = 0; i < sizeof(rcon); i++) { |
| 85 | u32 *rki = ctx->key_enc + (i * kwords); |
| 86 | u32 *rko = rki + kwords; |
| 87 | |
| Ard Biesheuvel | 58010fa | 2016-10-11 19:15:20 +0100 | [diff] [blame] | 88 | #ifndef CONFIG_CPU_BIG_ENDIAN |
| Ard Biesheuvel | 8646485 | 2015-03-10 09:47:47 +0100 | [diff] [blame] | 89 | rko[0] = ror32(ce_aes_sub(rki[kwords - 1]), 8); |
| 90 | rko[0] = rko[0] ^ rki[0] ^ rcon[i]; |
| Ard Biesheuvel | 58010fa | 2016-10-11 19:15:20 +0100 | [diff] [blame] | 91 | #else |
| 92 | rko[0] = rol32(ce_aes_sub(rki[kwords - 1]), 8); |
| 93 | rko[0] = rko[0] ^ rki[0] ^ (rcon[i] << 24); |
| 94 | #endif |
| Ard Biesheuvel | 8646485 | 2015-03-10 09:47:47 +0100 | [diff] [blame] | 95 | rko[1] = rko[0] ^ rki[1]; |
| 96 | rko[2] = rko[1] ^ rki[2]; |
| 97 | rko[3] = rko[2] ^ rki[3]; |
| 98 | |
| 99 | if (key_len == AES_KEYSIZE_192) { |
| 100 | if (i >= 7) |
| 101 | break; |
| 102 | rko[4] = rko[3] ^ rki[4]; |
| 103 | rko[5] = rko[4] ^ rki[5]; |
| 104 | } else if (key_len == AES_KEYSIZE_256) { |
| 105 | if (i >= 6) |
| 106 | break; |
| 107 | rko[4] = ce_aes_sub(rko[3]) ^ rki[4]; |
| 108 | rko[5] = rko[4] ^ rki[5]; |
| 109 | rko[6] = rko[5] ^ rki[6]; |
| 110 | rko[7] = rko[6] ^ rki[7]; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | /* |
| 115 | * Generate the decryption keys for the Equivalent Inverse Cipher. |
| 116 | * This involves reversing the order of the round keys, and applying |
| 117 | * the Inverse Mix Columns transformation on all but the first and |
| 118 | * the last one. |
| 119 | */ |
| 120 | key_enc = (struct aes_block *)ctx->key_enc; |
| 121 | key_dec = (struct aes_block *)ctx->key_dec; |
| 122 | j = num_rounds(ctx); |
| 123 | |
| 124 | key_dec[0] = key_enc[j]; |
| 125 | for (i = 1, j--; j > 0; i++, j--) |
| 126 | ce_aes_invert(key_dec + i, key_enc + j); |
| 127 | key_dec[i] = key_enc[0]; |
| 128 | |
| 129 | kernel_neon_end(); |
| 130 | return 0; |
| 131 | } |
| 132 | |
| Herbert Xu | da40e7a | 2016-11-22 20:08:37 +0800 | [diff] [blame] | 133 | static int ce_aes_setkey(struct crypto_skcipher *tfm, const u8 *in_key, |
| Ard Biesheuvel | 8646485 | 2015-03-10 09:47:47 +0100 | [diff] [blame] | 134 | unsigned int key_len) |
| 135 | { |
| Herbert Xu | da40e7a | 2016-11-22 20:08:37 +0800 | [diff] [blame] | 136 | struct crypto_aes_ctx *ctx = crypto_skcipher_ctx(tfm); |
| Ard Biesheuvel | 8646485 | 2015-03-10 09:47:47 +0100 | [diff] [blame] | 137 | int ret; |
| 138 | |
| 139 | ret = ce_aes_expandkey(ctx, in_key, key_len); |
| 140 | if (!ret) |
| 141 | return 0; |
| 142 | |
| Herbert Xu | da40e7a | 2016-11-22 20:08:37 +0800 | [diff] [blame] | 143 | crypto_skcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN); |
| Ard Biesheuvel | 8646485 | 2015-03-10 09:47:47 +0100 | [diff] [blame] | 144 | return -EINVAL; |
| 145 | } |
| 146 | |
| 147 | struct crypto_aes_xts_ctx { |
| 148 | struct crypto_aes_ctx key1; |
| 149 | struct crypto_aes_ctx __aligned(8) key2; |
| 150 | }; |
| 151 | |
| Herbert Xu | da40e7a | 2016-11-22 20:08:37 +0800 | [diff] [blame] | 152 | static int xts_set_key(struct crypto_skcipher *tfm, const u8 *in_key, |
| Ard Biesheuvel | 8646485 | 2015-03-10 09:47:47 +0100 | [diff] [blame] | 153 | unsigned int key_len) |
| 154 | { |
| Herbert Xu | da40e7a | 2016-11-22 20:08:37 +0800 | [diff] [blame] | 155 | struct crypto_aes_xts_ctx *ctx = crypto_skcipher_ctx(tfm); |
| Ard Biesheuvel | 8646485 | 2015-03-10 09:47:47 +0100 | [diff] [blame] | 156 | int ret; |
| 157 | |
| Herbert Xu | da40e7a | 2016-11-22 20:08:37 +0800 | [diff] [blame] | 158 | ret = xts_verify_key(tfm, in_key, key_len); |
| Stephan Mueller | 28856a9 | 2016-02-09 15:37:47 +0100 | [diff] [blame] | 159 | if (ret) |
| 160 | return ret; |
| 161 | |
| Ard Biesheuvel | 8646485 | 2015-03-10 09:47:47 +0100 | [diff] [blame] | 162 | ret = ce_aes_expandkey(&ctx->key1, in_key, key_len / 2); |
| 163 | if (!ret) |
| 164 | ret = ce_aes_expandkey(&ctx->key2, &in_key[key_len / 2], |
| 165 | key_len / 2); |
| 166 | if (!ret) |
| 167 | return 0; |
| 168 | |
| Herbert Xu | da40e7a | 2016-11-22 20:08:37 +0800 | [diff] [blame] | 169 | crypto_skcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN); |
| Ard Biesheuvel | 8646485 | 2015-03-10 09:47:47 +0100 | [diff] [blame] | 170 | return -EINVAL; |
| 171 | } |
| 172 | |
| Herbert Xu | da40e7a | 2016-11-22 20:08:37 +0800 | [diff] [blame] | 173 | static int ecb_encrypt(struct skcipher_request *req) |
| Ard Biesheuvel | 8646485 | 2015-03-10 09:47:47 +0100 | [diff] [blame] | 174 | { |
| Herbert Xu | da40e7a | 2016-11-22 20:08:37 +0800 | [diff] [blame] | 175 | struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
| 176 | struct crypto_aes_ctx *ctx = crypto_skcipher_ctx(tfm); |
| 177 | struct skcipher_walk walk; |
| Ard Biesheuvel | 8646485 | 2015-03-10 09:47:47 +0100 | [diff] [blame] | 178 | unsigned int blocks; |
| 179 | int err; |
| 180 | |
| Herbert Xu | da40e7a | 2016-11-22 20:08:37 +0800 | [diff] [blame] | 181 | err = skcipher_walk_virt(&walk, req, true); |
| Ard Biesheuvel | 8646485 | 2015-03-10 09:47:47 +0100 | [diff] [blame] | 182 | |
| 183 | kernel_neon_begin(); |
| 184 | while ((blocks = (walk.nbytes / AES_BLOCK_SIZE))) { |
| 185 | ce_aes_ecb_encrypt(walk.dst.virt.addr, walk.src.virt.addr, |
| 186 | (u8 *)ctx->key_enc, num_rounds(ctx), blocks); |
| Herbert Xu | da40e7a | 2016-11-22 20:08:37 +0800 | [diff] [blame] | 187 | err = skcipher_walk_done(&walk, walk.nbytes % AES_BLOCK_SIZE); |
| Ard Biesheuvel | 8646485 | 2015-03-10 09:47:47 +0100 | [diff] [blame] | 188 | } |
| 189 | kernel_neon_end(); |
| 190 | return err; |
| 191 | } |
| 192 | |
| Herbert Xu | da40e7a | 2016-11-22 20:08:37 +0800 | [diff] [blame] | 193 | static int ecb_decrypt(struct skcipher_request *req) |
| Ard Biesheuvel | 8646485 | 2015-03-10 09:47:47 +0100 | [diff] [blame] | 194 | { |
| Herbert Xu | da40e7a | 2016-11-22 20:08:37 +0800 | [diff] [blame] | 195 | struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
| 196 | struct crypto_aes_ctx *ctx = crypto_skcipher_ctx(tfm); |
| 197 | struct skcipher_walk walk; |
| Ard Biesheuvel | 8646485 | 2015-03-10 09:47:47 +0100 | [diff] [blame] | 198 | unsigned int blocks; |
| 199 | int err; |
| 200 | |
| Herbert Xu | da40e7a | 2016-11-22 20:08:37 +0800 | [diff] [blame] | 201 | err = skcipher_walk_virt(&walk, req, true); |
| Ard Biesheuvel | 8646485 | 2015-03-10 09:47:47 +0100 | [diff] [blame] | 202 | |
| 203 | kernel_neon_begin(); |
| 204 | while ((blocks = (walk.nbytes / AES_BLOCK_SIZE))) { |
| 205 | ce_aes_ecb_decrypt(walk.dst.virt.addr, walk.src.virt.addr, |
| 206 | (u8 *)ctx->key_dec, num_rounds(ctx), blocks); |
| Herbert Xu | da40e7a | 2016-11-22 20:08:37 +0800 | [diff] [blame] | 207 | err = skcipher_walk_done(&walk, walk.nbytes % AES_BLOCK_SIZE); |
| Ard Biesheuvel | 8646485 | 2015-03-10 09:47:47 +0100 | [diff] [blame] | 208 | } |
| 209 | kernel_neon_end(); |
| 210 | return err; |
| 211 | } |
| 212 | |
| Herbert Xu | da40e7a | 2016-11-22 20:08:37 +0800 | [diff] [blame] | 213 | static int cbc_encrypt(struct skcipher_request *req) |
| Ard Biesheuvel | 8646485 | 2015-03-10 09:47:47 +0100 | [diff] [blame] | 214 | { |
| Herbert Xu | da40e7a | 2016-11-22 20:08:37 +0800 | [diff] [blame] | 215 | struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
| 216 | struct crypto_aes_ctx *ctx = crypto_skcipher_ctx(tfm); |
| 217 | struct skcipher_walk walk; |
| Ard Biesheuvel | 8646485 | 2015-03-10 09:47:47 +0100 | [diff] [blame] | 218 | unsigned int blocks; |
| 219 | int err; |
| 220 | |
| Herbert Xu | da40e7a | 2016-11-22 20:08:37 +0800 | [diff] [blame] | 221 | err = skcipher_walk_virt(&walk, req, true); |
| Ard Biesheuvel | 8646485 | 2015-03-10 09:47:47 +0100 | [diff] [blame] | 222 | |
| 223 | kernel_neon_begin(); |
| 224 | while ((blocks = (walk.nbytes / AES_BLOCK_SIZE))) { |
| 225 | ce_aes_cbc_encrypt(walk.dst.virt.addr, walk.src.virt.addr, |
| 226 | (u8 *)ctx->key_enc, num_rounds(ctx), blocks, |
| 227 | walk.iv); |
| Herbert Xu | da40e7a | 2016-11-22 20:08:37 +0800 | [diff] [blame] | 228 | err = skcipher_walk_done(&walk, walk.nbytes % AES_BLOCK_SIZE); |
| Ard Biesheuvel | 8646485 | 2015-03-10 09:47:47 +0100 | [diff] [blame] | 229 | } |
| 230 | kernel_neon_end(); |
| 231 | return err; |
| 232 | } |
| 233 | |
| Herbert Xu | da40e7a | 2016-11-22 20:08:37 +0800 | [diff] [blame] | 234 | static int cbc_decrypt(struct skcipher_request *req) |
| Ard Biesheuvel | 8646485 | 2015-03-10 09:47:47 +0100 | [diff] [blame] | 235 | { |
| Herbert Xu | da40e7a | 2016-11-22 20:08:37 +0800 | [diff] [blame] | 236 | struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
| 237 | struct crypto_aes_ctx *ctx = crypto_skcipher_ctx(tfm); |
| 238 | struct skcipher_walk walk; |
| Ard Biesheuvel | 8646485 | 2015-03-10 09:47:47 +0100 | [diff] [blame] | 239 | unsigned int blocks; |
| 240 | int err; |
| 241 | |
| Herbert Xu | da40e7a | 2016-11-22 20:08:37 +0800 | [diff] [blame] | 242 | err = skcipher_walk_virt(&walk, req, true); |
| Ard Biesheuvel | 8646485 | 2015-03-10 09:47:47 +0100 | [diff] [blame] | 243 | |
| 244 | kernel_neon_begin(); |
| 245 | while ((blocks = (walk.nbytes / AES_BLOCK_SIZE))) { |
| 246 | ce_aes_cbc_decrypt(walk.dst.virt.addr, walk.src.virt.addr, |
| 247 | (u8 *)ctx->key_dec, num_rounds(ctx), blocks, |
| 248 | walk.iv); |
| Herbert Xu | da40e7a | 2016-11-22 20:08:37 +0800 | [diff] [blame] | 249 | err = skcipher_walk_done(&walk, walk.nbytes % AES_BLOCK_SIZE); |
| Ard Biesheuvel | 8646485 | 2015-03-10 09:47:47 +0100 | [diff] [blame] | 250 | } |
| 251 | kernel_neon_end(); |
| 252 | return err; |
| 253 | } |
| 254 | |
| Herbert Xu | da40e7a | 2016-11-22 20:08:37 +0800 | [diff] [blame] | 255 | static int ctr_encrypt(struct skcipher_request *req) |
| Ard Biesheuvel | 8646485 | 2015-03-10 09:47:47 +0100 | [diff] [blame] | 256 | { |
| Herbert Xu | da40e7a | 2016-11-22 20:08:37 +0800 | [diff] [blame] | 257 | struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
| 258 | struct crypto_aes_ctx *ctx = crypto_skcipher_ctx(tfm); |
| 259 | struct skcipher_walk walk; |
| Ard Biesheuvel | 8646485 | 2015-03-10 09:47:47 +0100 | [diff] [blame] | 260 | int err, blocks; |
| 261 | |
| Herbert Xu | da40e7a | 2016-11-22 20:08:37 +0800 | [diff] [blame] | 262 | err = skcipher_walk_virt(&walk, req, true); |
| Ard Biesheuvel | 8646485 | 2015-03-10 09:47:47 +0100 | [diff] [blame] | 263 | |
| 264 | kernel_neon_begin(); |
| 265 | while ((blocks = (walk.nbytes / AES_BLOCK_SIZE))) { |
| 266 | ce_aes_ctr_encrypt(walk.dst.virt.addr, walk.src.virt.addr, |
| 267 | (u8 *)ctx->key_enc, num_rounds(ctx), blocks, |
| 268 | walk.iv); |
| Herbert Xu | da40e7a | 2016-11-22 20:08:37 +0800 | [diff] [blame] | 269 | err = skcipher_walk_done(&walk, walk.nbytes % AES_BLOCK_SIZE); |
| Ard Biesheuvel | 8646485 | 2015-03-10 09:47:47 +0100 | [diff] [blame] | 270 | } |
| Herbert Xu | da40e7a | 2016-11-22 20:08:37 +0800 | [diff] [blame] | 271 | if (walk.nbytes) { |
| Ard Biesheuvel | 8646485 | 2015-03-10 09:47:47 +0100 | [diff] [blame] | 272 | u8 __aligned(8) tail[AES_BLOCK_SIZE]; |
| Herbert Xu | da40e7a | 2016-11-22 20:08:37 +0800 | [diff] [blame] | 273 | unsigned int nbytes = walk.nbytes; |
| 274 | u8 *tdst = walk.dst.virt.addr; |
| 275 | u8 *tsrc = walk.src.virt.addr; |
| Ard Biesheuvel | 8646485 | 2015-03-10 09:47:47 +0100 | [diff] [blame] | 276 | |
| 277 | /* |
| Ard Biesheuvel | 1465fb1 | 2017-01-28 23:25:31 +0000 | [diff] [blame] | 278 | * Tell aes_ctr_encrypt() to process a tail block. |
| Ard Biesheuvel | 8646485 | 2015-03-10 09:47:47 +0100 | [diff] [blame] | 279 | */ |
| Ard Biesheuvel | 1465fb1 | 2017-01-28 23:25:31 +0000 | [diff] [blame] | 280 | blocks = -1; |
| Ard Biesheuvel | 8646485 | 2015-03-10 09:47:47 +0100 | [diff] [blame] | 281 | |
| Ard Biesheuvel | 1465fb1 | 2017-01-28 23:25:31 +0000 | [diff] [blame] | 282 | ce_aes_ctr_encrypt(tail, NULL, (u8 *)ctx->key_enc, |
| Ard Biesheuvel | 8646485 | 2015-03-10 09:47:47 +0100 | [diff] [blame] | 283 | num_rounds(ctx), blocks, walk.iv); |
| Ard Biesheuvel | 45fe93d | 2017-07-24 11:28:04 +0100 | [diff] [blame] | 284 | crypto_xor_cpy(tdst, tsrc, tail, nbytes); |
| Herbert Xu | da40e7a | 2016-11-22 20:08:37 +0800 | [diff] [blame] | 285 | err = skcipher_walk_done(&walk, 0); |
| Ard Biesheuvel | 8646485 | 2015-03-10 09:47:47 +0100 | [diff] [blame] | 286 | } |
| 287 | kernel_neon_end(); |
| 288 | |
| 289 | return err; |
| 290 | } |
| 291 | |
| Herbert Xu | da40e7a | 2016-11-22 20:08:37 +0800 | [diff] [blame] | 292 | static int xts_encrypt(struct skcipher_request *req) |
| Ard Biesheuvel | 8646485 | 2015-03-10 09:47:47 +0100 | [diff] [blame] | 293 | { |
| Herbert Xu | da40e7a | 2016-11-22 20:08:37 +0800 | [diff] [blame] | 294 | struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
| 295 | struct crypto_aes_xts_ctx *ctx = crypto_skcipher_ctx(tfm); |
| Ard Biesheuvel | 8646485 | 2015-03-10 09:47:47 +0100 | [diff] [blame] | 296 | int err, first, rounds = num_rounds(&ctx->key1); |
| Herbert Xu | da40e7a | 2016-11-22 20:08:37 +0800 | [diff] [blame] | 297 | struct skcipher_walk walk; |
| Ard Biesheuvel | 8646485 | 2015-03-10 09:47:47 +0100 | [diff] [blame] | 298 | unsigned int blocks; |
| 299 | |
| Herbert Xu | da40e7a | 2016-11-22 20:08:37 +0800 | [diff] [blame] | 300 | err = skcipher_walk_virt(&walk, req, true); |
| Ard Biesheuvel | 8646485 | 2015-03-10 09:47:47 +0100 | [diff] [blame] | 301 | |
| 302 | kernel_neon_begin(); |
| 303 | for (first = 1; (blocks = (walk.nbytes / AES_BLOCK_SIZE)); first = 0) { |
| 304 | ce_aes_xts_encrypt(walk.dst.virt.addr, walk.src.virt.addr, |
| 305 | (u8 *)ctx->key1.key_enc, rounds, blocks, |
| 306 | walk.iv, (u8 *)ctx->key2.key_enc, first); |
| Herbert Xu | da40e7a | 2016-11-22 20:08:37 +0800 | [diff] [blame] | 307 | err = skcipher_walk_done(&walk, walk.nbytes % AES_BLOCK_SIZE); |
| Ard Biesheuvel | 8646485 | 2015-03-10 09:47:47 +0100 | [diff] [blame] | 308 | } |
| 309 | kernel_neon_end(); |
| 310 | |
| 311 | return err; |
| 312 | } |
| 313 | |
| Herbert Xu | da40e7a | 2016-11-22 20:08:37 +0800 | [diff] [blame] | 314 | static int xts_decrypt(struct skcipher_request *req) |
| Ard Biesheuvel | 8646485 | 2015-03-10 09:47:47 +0100 | [diff] [blame] | 315 | { |
| Herbert Xu | da40e7a | 2016-11-22 20:08:37 +0800 | [diff] [blame] | 316 | struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
| 317 | struct crypto_aes_xts_ctx *ctx = crypto_skcipher_ctx(tfm); |
| Ard Biesheuvel | 8646485 | 2015-03-10 09:47:47 +0100 | [diff] [blame] | 318 | int err, first, rounds = num_rounds(&ctx->key1); |
| Herbert Xu | da40e7a | 2016-11-22 20:08:37 +0800 | [diff] [blame] | 319 | struct skcipher_walk walk; |
| Ard Biesheuvel | 8646485 | 2015-03-10 09:47:47 +0100 | [diff] [blame] | 320 | unsigned int blocks; |
| 321 | |
| Herbert Xu | da40e7a | 2016-11-22 20:08:37 +0800 | [diff] [blame] | 322 | err = skcipher_walk_virt(&walk, req, true); |
| Ard Biesheuvel | 8646485 | 2015-03-10 09:47:47 +0100 | [diff] [blame] | 323 | |
| 324 | kernel_neon_begin(); |
| 325 | for (first = 1; (blocks = (walk.nbytes / AES_BLOCK_SIZE)); first = 0) { |
| 326 | ce_aes_xts_decrypt(walk.dst.virt.addr, walk.src.virt.addr, |
| 327 | (u8 *)ctx->key1.key_dec, rounds, blocks, |
| 328 | walk.iv, (u8 *)ctx->key2.key_enc, first); |
| Herbert Xu | da40e7a | 2016-11-22 20:08:37 +0800 | [diff] [blame] | 329 | err = skcipher_walk_done(&walk, walk.nbytes % AES_BLOCK_SIZE); |
| Ard Biesheuvel | 8646485 | 2015-03-10 09:47:47 +0100 | [diff] [blame] | 330 | } |
| 331 | kernel_neon_end(); |
| 332 | |
| 333 | return err; |
| 334 | } |
| 335 | |
| Herbert Xu | da40e7a | 2016-11-22 20:08:37 +0800 | [diff] [blame] | 336 | static struct skcipher_alg aes_algs[] = { { |
| 337 | .base = { |
| 338 | .cra_name = "__ecb(aes)", |
| 339 | .cra_driver_name = "__ecb-aes-ce", |
| 340 | .cra_priority = 300, |
| 341 | .cra_flags = CRYPTO_ALG_INTERNAL, |
| 342 | .cra_blocksize = AES_BLOCK_SIZE, |
| 343 | .cra_ctxsize = sizeof(struct crypto_aes_ctx), |
| Herbert Xu | da40e7a | 2016-11-22 20:08:37 +0800 | [diff] [blame] | 344 | .cra_module = THIS_MODULE, |
| Ard Biesheuvel | 8646485 | 2015-03-10 09:47:47 +0100 | [diff] [blame] | 345 | }, |
| Herbert Xu | da40e7a | 2016-11-22 20:08:37 +0800 | [diff] [blame] | 346 | .min_keysize = AES_MIN_KEY_SIZE, |
| 347 | .max_keysize = AES_MAX_KEY_SIZE, |
| 348 | .setkey = ce_aes_setkey, |
| 349 | .encrypt = ecb_encrypt, |
| 350 | .decrypt = ecb_decrypt, |
| Ard Biesheuvel | 8646485 | 2015-03-10 09:47:47 +0100 | [diff] [blame] | 351 | }, { |
| Herbert Xu | da40e7a | 2016-11-22 20:08:37 +0800 | [diff] [blame] | 352 | .base = { |
| 353 | .cra_name = "__cbc(aes)", |
| 354 | .cra_driver_name = "__cbc-aes-ce", |
| 355 | .cra_priority = 300, |
| 356 | .cra_flags = CRYPTO_ALG_INTERNAL, |
| 357 | .cra_blocksize = AES_BLOCK_SIZE, |
| 358 | .cra_ctxsize = sizeof(struct crypto_aes_ctx), |
| Herbert Xu | da40e7a | 2016-11-22 20:08:37 +0800 | [diff] [blame] | 359 | .cra_module = THIS_MODULE, |
| Ard Biesheuvel | 8646485 | 2015-03-10 09:47:47 +0100 | [diff] [blame] | 360 | }, |
| Herbert Xu | da40e7a | 2016-11-22 20:08:37 +0800 | [diff] [blame] | 361 | .min_keysize = AES_MIN_KEY_SIZE, |
| 362 | .max_keysize = AES_MAX_KEY_SIZE, |
| 363 | .ivsize = AES_BLOCK_SIZE, |
| 364 | .setkey = ce_aes_setkey, |
| 365 | .encrypt = cbc_encrypt, |
| 366 | .decrypt = cbc_decrypt, |
| Ard Biesheuvel | 8646485 | 2015-03-10 09:47:47 +0100 | [diff] [blame] | 367 | }, { |
| Herbert Xu | da40e7a | 2016-11-22 20:08:37 +0800 | [diff] [blame] | 368 | .base = { |
| 369 | .cra_name = "__ctr(aes)", |
| 370 | .cra_driver_name = "__ctr-aes-ce", |
| 371 | .cra_priority = 300, |
| 372 | .cra_flags = CRYPTO_ALG_INTERNAL, |
| 373 | .cra_blocksize = 1, |
| 374 | .cra_ctxsize = sizeof(struct crypto_aes_ctx), |
| Herbert Xu | da40e7a | 2016-11-22 20:08:37 +0800 | [diff] [blame] | 375 | .cra_module = THIS_MODULE, |
| Ard Biesheuvel | 8646485 | 2015-03-10 09:47:47 +0100 | [diff] [blame] | 376 | }, |
| Herbert Xu | da40e7a | 2016-11-22 20:08:37 +0800 | [diff] [blame] | 377 | .min_keysize = AES_MIN_KEY_SIZE, |
| 378 | .max_keysize = AES_MAX_KEY_SIZE, |
| 379 | .ivsize = AES_BLOCK_SIZE, |
| 380 | .chunksize = AES_BLOCK_SIZE, |
| 381 | .setkey = ce_aes_setkey, |
| 382 | .encrypt = ctr_encrypt, |
| 383 | .decrypt = ctr_encrypt, |
| Ard Biesheuvel | 8646485 | 2015-03-10 09:47:47 +0100 | [diff] [blame] | 384 | }, { |
| Herbert Xu | da40e7a | 2016-11-22 20:08:37 +0800 | [diff] [blame] | 385 | .base = { |
| 386 | .cra_name = "__xts(aes)", |
| 387 | .cra_driver_name = "__xts-aes-ce", |
| 388 | .cra_priority = 300, |
| 389 | .cra_flags = CRYPTO_ALG_INTERNAL, |
| 390 | .cra_blocksize = AES_BLOCK_SIZE, |
| 391 | .cra_ctxsize = sizeof(struct crypto_aes_xts_ctx), |
| Herbert Xu | da40e7a | 2016-11-22 20:08:37 +0800 | [diff] [blame] | 392 | .cra_module = THIS_MODULE, |
| Ard Biesheuvel | 8646485 | 2015-03-10 09:47:47 +0100 | [diff] [blame] | 393 | }, |
| Herbert Xu | da40e7a | 2016-11-22 20:08:37 +0800 | [diff] [blame] | 394 | .min_keysize = 2 * AES_MIN_KEY_SIZE, |
| 395 | .max_keysize = 2 * AES_MAX_KEY_SIZE, |
| 396 | .ivsize = AES_BLOCK_SIZE, |
| 397 | .setkey = xts_set_key, |
| 398 | .encrypt = xts_encrypt, |
| 399 | .decrypt = xts_decrypt, |
| Ard Biesheuvel | 8646485 | 2015-03-10 09:47:47 +0100 | [diff] [blame] | 400 | } }; |
| 401 | |
| Herbert Xu | efad2b6 | 2016-12-01 13:45:05 +0800 | [diff] [blame] | 402 | static struct simd_skcipher_alg *aes_simd_algs[ARRAY_SIZE(aes_algs)]; |
| Herbert Xu | da40e7a | 2016-11-22 20:08:37 +0800 | [diff] [blame] | 403 | |
| 404 | static void aes_exit(void) |
| 405 | { |
| 406 | int i; |
| 407 | |
| 408 | for (i = 0; i < ARRAY_SIZE(aes_simd_algs) && aes_simd_algs[i]; i++) |
| 409 | simd_skcipher_free(aes_simd_algs[i]); |
| 410 | |
| 411 | crypto_unregister_skciphers(aes_algs, ARRAY_SIZE(aes_algs)); |
| 412 | } |
| 413 | |
| Ard Biesheuvel | 8646485 | 2015-03-10 09:47:47 +0100 | [diff] [blame] | 414 | static int __init aes_init(void) |
| 415 | { |
| Herbert Xu | da40e7a | 2016-11-22 20:08:37 +0800 | [diff] [blame] | 416 | struct simd_skcipher_alg *simd; |
| 417 | const char *basename; |
| 418 | const char *algname; |
| 419 | const char *drvname; |
| 420 | int err; |
| 421 | int i; |
| 422 | |
| Herbert Xu | da40e7a | 2016-11-22 20:08:37 +0800 | [diff] [blame] | 423 | err = crypto_register_skciphers(aes_algs, ARRAY_SIZE(aes_algs)); |
| 424 | if (err) |
| 425 | return err; |
| 426 | |
| 427 | for (i = 0; i < ARRAY_SIZE(aes_algs); i++) { |
| 428 | algname = aes_algs[i].base.cra_name + 2; |
| 429 | drvname = aes_algs[i].base.cra_driver_name + 2; |
| 430 | basename = aes_algs[i].base.cra_driver_name; |
| 431 | simd = simd_skcipher_create_compat(algname, drvname, basename); |
| 432 | err = PTR_ERR(simd); |
| 433 | if (IS_ERR(simd)) |
| 434 | goto unregister_simds; |
| 435 | |
| 436 | aes_simd_algs[i] = simd; |
| 437 | } |
| 438 | |
| 439 | return 0; |
| 440 | |
| 441 | unregister_simds: |
| 442 | aes_exit(); |
| 443 | return err; |
| Ard Biesheuvel | 8646485 | 2015-03-10 09:47:47 +0100 | [diff] [blame] | 444 | } |
| 445 | |
| Ard Biesheuvel | 4d8061a | 2017-05-21 10:23:36 +0000 | [diff] [blame] | 446 | module_cpu_feature_match(AES, aes_init); |
| Ard Biesheuvel | 8646485 | 2015-03-10 09:47:47 +0100 | [diff] [blame] | 447 | module_exit(aes_exit); |