Daniel Axtens | 357d065 | 2019-05-17 01:40:02 +1000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Marcelo H. Cerri | cc333cd | 2015-02-06 14:59:05 -0200 | [diff] [blame] | 2 | /** |
| 3 | * GHASH routines supporting VMX instructions on the Power 8 |
| 4 | * |
Daniel Axtens | 357d065 | 2019-05-17 01:40:02 +1000 | [diff] [blame] | 5 | * Copyright (C) 2015, 2019 International Business Machines Inc. |
Marcelo H. Cerri | cc333cd | 2015-02-06 14:59:05 -0200 | [diff] [blame] | 6 | * |
| 7 | * Author: Marcelo Henrique Cerri <mhcerri@br.ibm.com> |
Daniel Axtens | 357d065 | 2019-05-17 01:40:02 +1000 | [diff] [blame] | 8 | * |
| 9 | * Extended by Daniel Axtens <dja@axtens.net> to replace the fallback |
| 10 | * mechanism. The new approach is based on arm64 code, which is: |
| 11 | * Copyright (C) 2014 - 2018 Linaro Ltd. <ard.biesheuvel@linaro.org> |
Marcelo H. Cerri | cc333cd | 2015-02-06 14:59:05 -0200 | [diff] [blame] | 12 | */ |
| 13 | |
| 14 | #include <linux/types.h> |
| 15 | #include <linux/err.h> |
| 16 | #include <linux/crypto.h> |
| 17 | #include <linux/delay.h> |
Eric Biggers | 626ddb2 | 2019-04-12 22:33:12 -0700 | [diff] [blame] | 18 | #include <asm/simd.h> |
Marcelo H. Cerri | cc333cd | 2015-02-06 14:59:05 -0200 | [diff] [blame] | 19 | #include <asm/switch_to.h> |
| 20 | #include <crypto/aes.h> |
Marcelo Cerri | 80da44c | 2016-09-28 13:42:10 -0300 | [diff] [blame] | 21 | #include <crypto/ghash.h> |
Marcelo H. Cerri | cc333cd | 2015-02-06 14:59:05 -0200 | [diff] [blame] | 22 | #include <crypto/scatterwalk.h> |
| 23 | #include <crypto/internal/hash.h> |
Eric Biggers | 626ddb2 | 2019-04-12 22:33:12 -0700 | [diff] [blame] | 24 | #include <crypto/internal/simd.h> |
Marcelo H. Cerri | cc333cd | 2015-02-06 14:59:05 -0200 | [diff] [blame] | 25 | #include <crypto/b128ops.h> |
| 26 | |
Marcelo H. Cerri | cc333cd | 2015-02-06 14:59:05 -0200 | [diff] [blame] | 27 | void gcm_init_p8(u128 htable[16], const u64 Xi[2]); |
| 28 | void gcm_gmult_p8(u64 Xi[2], const u128 htable[16]); |
| 29 | void gcm_ghash_p8(u64 Xi[2], const u128 htable[16], |
Herbert Xu | 4beb106 | 2015-06-15 16:55:46 +0800 | [diff] [blame] | 30 | const u8 *in, size_t len); |
Marcelo H. Cerri | cc333cd | 2015-02-06 14:59:05 -0200 | [diff] [blame] | 31 | |
| 32 | struct p8_ghash_ctx { |
Daniel Axtens | 357d065 | 2019-05-17 01:40:02 +1000 | [diff] [blame] | 33 | /* key used by vector asm */ |
Herbert Xu | 4beb106 | 2015-06-15 16:55:46 +0800 | [diff] [blame] | 34 | u128 htable[16]; |
Daniel Axtens | 357d065 | 2019-05-17 01:40:02 +1000 | [diff] [blame] | 35 | /* key used by software fallback */ |
| 36 | be128 key; |
Marcelo H. Cerri | cc333cd | 2015-02-06 14:59:05 -0200 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | struct p8_ghash_desc_ctx { |
Herbert Xu | 4beb106 | 2015-06-15 16:55:46 +0800 | [diff] [blame] | 40 | u64 shash[2]; |
| 41 | u8 buffer[GHASH_DIGEST_SIZE]; |
| 42 | int bytes; |
Marcelo H. Cerri | cc333cd | 2015-02-06 14:59:05 -0200 | [diff] [blame] | 43 | }; |
| 44 | |
Marcelo H. Cerri | cc333cd | 2015-02-06 14:59:05 -0200 | [diff] [blame] | 45 | static int p8_ghash_init(struct shash_desc *desc) |
| 46 | { |
Herbert Xu | 4beb106 | 2015-06-15 16:55:46 +0800 | [diff] [blame] | 47 | struct p8_ghash_desc_ctx *dctx = shash_desc_ctx(desc); |
Marcelo H. Cerri | cc333cd | 2015-02-06 14:59:05 -0200 | [diff] [blame] | 48 | |
Herbert Xu | 4beb106 | 2015-06-15 16:55:46 +0800 | [diff] [blame] | 49 | dctx->bytes = 0; |
| 50 | memset(dctx->shash, 0, GHASH_DIGEST_SIZE); |
Daniel Axtens | 357d065 | 2019-05-17 01:40:02 +1000 | [diff] [blame] | 51 | return 0; |
Marcelo H. Cerri | cc333cd | 2015-02-06 14:59:05 -0200 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | static int p8_ghash_setkey(struct crypto_shash *tfm, const u8 *key, |
Herbert Xu | 4beb106 | 2015-06-15 16:55:46 +0800 | [diff] [blame] | 55 | unsigned int keylen) |
Marcelo H. Cerri | cc333cd | 2015-02-06 14:59:05 -0200 | [diff] [blame] | 56 | { |
Herbert Xu | 4beb106 | 2015-06-15 16:55:46 +0800 | [diff] [blame] | 57 | struct p8_ghash_ctx *ctx = crypto_tfm_ctx(crypto_shash_tfm(tfm)); |
Marcelo H. Cerri | cc333cd | 2015-02-06 14:59:05 -0200 | [diff] [blame] | 58 | |
Marcelo Cerri | 80da44c | 2016-09-28 13:42:10 -0300 | [diff] [blame] | 59 | if (keylen != GHASH_BLOCK_SIZE) |
Herbert Xu | 4beb106 | 2015-06-15 16:55:46 +0800 | [diff] [blame] | 60 | return -EINVAL; |
Marcelo H. Cerri | cc333cd | 2015-02-06 14:59:05 -0200 | [diff] [blame] | 61 | |
Linus Torvalds | 44d21c3 | 2015-06-22 21:04:48 -0700 | [diff] [blame] | 62 | preempt_disable(); |
Herbert Xu | 4beb106 | 2015-06-15 16:55:46 +0800 | [diff] [blame] | 63 | pagefault_disable(); |
Leonidas Da Silva Barbosa | 2d6f060 | 2015-07-13 13:51:39 -0300 | [diff] [blame] | 64 | enable_kernel_vsx(); |
Herbert Xu | 4beb106 | 2015-06-15 16:55:46 +0800 | [diff] [blame] | 65 | gcm_init_p8(ctx->htable, (const u64 *) key); |
Anton Blanchard | dc4fbba | 2015-10-29 11:44:05 +1100 | [diff] [blame] | 66 | disable_kernel_vsx(); |
Herbert Xu | 4beb106 | 2015-06-15 16:55:46 +0800 | [diff] [blame] | 67 | pagefault_enable(); |
Linus Torvalds | 44d21c3 | 2015-06-22 21:04:48 -0700 | [diff] [blame] | 68 | preempt_enable(); |
Daniel Axtens | 357d065 | 2019-05-17 01:40:02 +1000 | [diff] [blame] | 69 | |
| 70 | memcpy(&ctx->key, key, GHASH_BLOCK_SIZE); |
| 71 | |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | static inline void __ghash_block(struct p8_ghash_ctx *ctx, |
| 76 | struct p8_ghash_desc_ctx *dctx) |
| 77 | { |
| 78 | if (crypto_simd_usable()) { |
| 79 | preempt_disable(); |
| 80 | pagefault_disable(); |
| 81 | enable_kernel_vsx(); |
| 82 | gcm_ghash_p8(dctx->shash, ctx->htable, |
| 83 | dctx->buffer, GHASH_DIGEST_SIZE); |
| 84 | disable_kernel_vsx(); |
| 85 | pagefault_enable(); |
| 86 | preempt_enable(); |
| 87 | } else { |
| 88 | crypto_xor((u8 *)dctx->shash, dctx->buffer, GHASH_BLOCK_SIZE); |
| 89 | gf128mul_lle((be128 *)dctx->shash, &ctx->key); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | static inline void __ghash_blocks(struct p8_ghash_ctx *ctx, |
| 94 | struct p8_ghash_desc_ctx *dctx, |
| 95 | const u8 *src, unsigned int srclen) |
| 96 | { |
| 97 | if (crypto_simd_usable()) { |
| 98 | preempt_disable(); |
| 99 | pagefault_disable(); |
| 100 | enable_kernel_vsx(); |
| 101 | gcm_ghash_p8(dctx->shash, ctx->htable, |
| 102 | src, srclen); |
| 103 | disable_kernel_vsx(); |
| 104 | pagefault_enable(); |
| 105 | preempt_enable(); |
| 106 | } else { |
| 107 | while (srclen >= GHASH_BLOCK_SIZE) { |
| 108 | crypto_xor((u8 *)dctx->shash, src, GHASH_BLOCK_SIZE); |
| 109 | gf128mul_lle((be128 *)dctx->shash, &ctx->key); |
| 110 | srclen -= GHASH_BLOCK_SIZE; |
| 111 | src += GHASH_BLOCK_SIZE; |
| 112 | } |
| 113 | } |
Marcelo H. Cerri | cc333cd | 2015-02-06 14:59:05 -0200 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | static int p8_ghash_update(struct shash_desc *desc, |
Herbert Xu | 4beb106 | 2015-06-15 16:55:46 +0800 | [diff] [blame] | 117 | const u8 *src, unsigned int srclen) |
Marcelo H. Cerri | cc333cd | 2015-02-06 14:59:05 -0200 | [diff] [blame] | 118 | { |
Herbert Xu | 4beb106 | 2015-06-15 16:55:46 +0800 | [diff] [blame] | 119 | unsigned int len; |
| 120 | struct p8_ghash_ctx *ctx = crypto_tfm_ctx(crypto_shash_tfm(desc->tfm)); |
| 121 | struct p8_ghash_desc_ctx *dctx = shash_desc_ctx(desc); |
Marcelo H. Cerri | cc333cd | 2015-02-06 14:59:05 -0200 | [diff] [blame] | 122 | |
Daniel Axtens | 357d065 | 2019-05-17 01:40:02 +1000 | [diff] [blame] | 123 | if (dctx->bytes) { |
| 124 | if (dctx->bytes + srclen < GHASH_DIGEST_SIZE) { |
Herbert Xu | 4beb106 | 2015-06-15 16:55:46 +0800 | [diff] [blame] | 125 | memcpy(dctx->buffer + dctx->bytes, src, |
Daniel Axtens | 357d065 | 2019-05-17 01:40:02 +1000 | [diff] [blame] | 126 | srclen); |
| 127 | dctx->bytes += srclen; |
| 128 | return 0; |
Herbert Xu | 4beb106 | 2015-06-15 16:55:46 +0800 | [diff] [blame] | 129 | } |
Daniel Axtens | 357d065 | 2019-05-17 01:40:02 +1000 | [diff] [blame] | 130 | memcpy(dctx->buffer + dctx->bytes, src, |
| 131 | GHASH_DIGEST_SIZE - dctx->bytes); |
| 132 | |
| 133 | __ghash_block(ctx, dctx); |
| 134 | |
| 135 | src += GHASH_DIGEST_SIZE - dctx->bytes; |
| 136 | srclen -= GHASH_DIGEST_SIZE - dctx->bytes; |
| 137 | dctx->bytes = 0; |
Herbert Xu | 4beb106 | 2015-06-15 16:55:46 +0800 | [diff] [blame] | 138 | } |
Daniel Axtens | 357d065 | 2019-05-17 01:40:02 +1000 | [diff] [blame] | 139 | len = srclen & ~(GHASH_DIGEST_SIZE - 1); |
| 140 | if (len) { |
| 141 | __ghash_blocks(ctx, dctx, src, len); |
| 142 | src += len; |
| 143 | srclen -= len; |
| 144 | } |
| 145 | if (srclen) { |
| 146 | memcpy(dctx->buffer, src, srclen); |
| 147 | dctx->bytes = srclen; |
| 148 | } |
| 149 | return 0; |
Marcelo H. Cerri | cc333cd | 2015-02-06 14:59:05 -0200 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | static int p8_ghash_final(struct shash_desc *desc, u8 *out) |
| 153 | { |
Herbert Xu | 4beb106 | 2015-06-15 16:55:46 +0800 | [diff] [blame] | 154 | int i; |
| 155 | struct p8_ghash_ctx *ctx = crypto_tfm_ctx(crypto_shash_tfm(desc->tfm)); |
| 156 | struct p8_ghash_desc_ctx *dctx = shash_desc_ctx(desc); |
Marcelo H. Cerri | cc333cd | 2015-02-06 14:59:05 -0200 | [diff] [blame] | 157 | |
Daniel Axtens | 357d065 | 2019-05-17 01:40:02 +1000 | [diff] [blame] | 158 | if (dctx->bytes) { |
| 159 | for (i = dctx->bytes; i < GHASH_DIGEST_SIZE; i++) |
| 160 | dctx->buffer[i] = 0; |
| 161 | __ghash_block(ctx, dctx); |
| 162 | dctx->bytes = 0; |
Herbert Xu | 4beb106 | 2015-06-15 16:55:46 +0800 | [diff] [blame] | 163 | } |
Daniel Axtens | 357d065 | 2019-05-17 01:40:02 +1000 | [diff] [blame] | 164 | memcpy(out, dctx->shash, GHASH_DIGEST_SIZE); |
| 165 | return 0; |
Marcelo H. Cerri | cc333cd | 2015-02-06 14:59:05 -0200 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | struct shash_alg p8_ghash_alg = { |
Herbert Xu | 4beb106 | 2015-06-15 16:55:46 +0800 | [diff] [blame] | 169 | .digestsize = GHASH_DIGEST_SIZE, |
| 170 | .init = p8_ghash_init, |
| 171 | .update = p8_ghash_update, |
| 172 | .final = p8_ghash_final, |
| 173 | .setkey = p8_ghash_setkey, |
Marcelo Cerri | 80da44c | 2016-09-28 13:42:10 -0300 | [diff] [blame] | 174 | .descsize = sizeof(struct p8_ghash_desc_ctx) |
| 175 | + sizeof(struct ghash_desc_ctx), |
Herbert Xu | 4beb106 | 2015-06-15 16:55:46 +0800 | [diff] [blame] | 176 | .base = { |
| 177 | .cra_name = "ghash", |
| 178 | .cra_driver_name = "p8_ghash", |
| 179 | .cra_priority = 1000, |
Herbert Xu | 4beb106 | 2015-06-15 16:55:46 +0800 | [diff] [blame] | 180 | .cra_blocksize = GHASH_BLOCK_SIZE, |
| 181 | .cra_ctxsize = sizeof(struct p8_ghash_ctx), |
| 182 | .cra_module = THIS_MODULE, |
Herbert Xu | 4beb106 | 2015-06-15 16:55:46 +0800 | [diff] [blame] | 183 | }, |
Marcelo H. Cerri | cc333cd | 2015-02-06 14:59:05 -0200 | [diff] [blame] | 184 | }; |