1 /** 2 * GHASH routines supporting VMX instructions on the Power 8 3 * 4 * Copyright (C) 2015 International Business Machines Inc. 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; version 2 only. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program; if not, write to the Free Software 17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 18 * 19 * Author: Marcelo Henrique Cerri <mhcerri@br.ibm.com> 20 */ 21 22 #include <linux/types.h> 23 #include <linux/err.h> 24 #include <linux/crypto.h> 25 #include <linux/delay.h> 26 #include <asm/simd.h> 27 #include <asm/switch_to.h> 28 #include <crypto/aes.h> 29 #include <crypto/ghash.h> 30 #include <crypto/scatterwalk.h> 31 #include <crypto/internal/hash.h> 32 #include <crypto/internal/simd.h> 33 #include <crypto/b128ops.h> 34 35 void gcm_init_p8(u128 htable[16], const u64 Xi[2]); 36 void gcm_gmult_p8(u64 Xi[2], const u128 htable[16]); 37 void gcm_ghash_p8(u64 Xi[2], const u128 htable[16], 38 const u8 *in, size_t len); 39 40 struct p8_ghash_ctx { 41 u128 htable[16]; 42 struct crypto_shash *fallback; 43 }; 44 45 struct p8_ghash_desc_ctx { 46 u64 shash[2]; 47 u8 buffer[GHASH_DIGEST_SIZE]; 48 int bytes; 49 struct shash_desc fallback_desc; 50 }; 51 52 static int p8_ghash_init_tfm(struct crypto_tfm *tfm) 53 { 54 const char *alg = "ghash-generic"; 55 struct crypto_shash *fallback; 56 struct crypto_shash *shash_tfm = __crypto_shash_cast(tfm); 57 struct p8_ghash_ctx *ctx = crypto_tfm_ctx(tfm); 58 59 fallback = crypto_alloc_shash(alg, 0, CRYPTO_ALG_NEED_FALLBACK); 60 if (IS_ERR(fallback)) { 61 printk(KERN_ERR 62 "Failed to allocate transformation for '%s': %ld\n", 63 alg, PTR_ERR(fallback)); 64 return PTR_ERR(fallback); 65 } 66 67 crypto_shash_set_flags(fallback, 68 crypto_shash_get_flags((struct crypto_shash 69 *) tfm)); 70 71 /* Check if the descsize defined in the algorithm is still enough. */ 72 if (shash_tfm->descsize < sizeof(struct p8_ghash_desc_ctx) 73 + crypto_shash_descsize(fallback)) { 74 printk(KERN_ERR 75 "Desc size of the fallback implementation (%s) does not match the expected value: %lu vs %u\n", 76 alg, 77 shash_tfm->descsize - sizeof(struct p8_ghash_desc_ctx), 78 crypto_shash_descsize(fallback)); 79 return -EINVAL; 80 } 81 ctx->fallback = fallback; 82 83 return 0; 84 } 85 86 static void p8_ghash_exit_tfm(struct crypto_tfm *tfm) 87 { 88 struct p8_ghash_ctx *ctx = crypto_tfm_ctx(tfm); 89 90 if (ctx->fallback) { 91 crypto_free_shash(ctx->fallback); 92 ctx->fallback = NULL; 93 } 94 } 95 96 static int p8_ghash_init(struct shash_desc *desc) 97 { 98 struct p8_ghash_ctx *ctx = crypto_tfm_ctx(crypto_shash_tfm(desc->tfm)); 99 struct p8_ghash_desc_ctx *dctx = shash_desc_ctx(desc); 100 101 dctx->bytes = 0; 102 memset(dctx->shash, 0, GHASH_DIGEST_SIZE); 103 dctx->fallback_desc.tfm = ctx->fallback; 104 return crypto_shash_init(&dctx->fallback_desc); 105 } 106 107 static int p8_ghash_setkey(struct crypto_shash *tfm, const u8 *key, 108 unsigned int keylen) 109 { 110 struct p8_ghash_ctx *ctx = crypto_tfm_ctx(crypto_shash_tfm(tfm)); 111 112 if (keylen != GHASH_BLOCK_SIZE) 113 return -EINVAL; 114 115 preempt_disable(); 116 pagefault_disable(); 117 enable_kernel_vsx(); 118 gcm_init_p8(ctx->htable, (const u64 *) key); 119 disable_kernel_vsx(); 120 pagefault_enable(); 121 preempt_enable(); 122 return crypto_shash_setkey(ctx->fallback, key, keylen); 123 } 124 125 static int p8_ghash_update(struct shash_desc *desc, 126 const u8 *src, unsigned int srclen) 127 { 128 unsigned int len; 129 struct p8_ghash_ctx *ctx = crypto_tfm_ctx(crypto_shash_tfm(desc->tfm)); 130 struct p8_ghash_desc_ctx *dctx = shash_desc_ctx(desc); 131 132 if (!crypto_simd_usable()) { 133 return crypto_shash_update(&dctx->fallback_desc, src, 134 srclen); 135 } else { 136 if (dctx->bytes) { 137 if (dctx->bytes + srclen < GHASH_DIGEST_SIZE) { 138 memcpy(dctx->buffer + dctx->bytes, src, 139 srclen); 140 dctx->bytes += srclen; 141 return 0; 142 } 143 memcpy(dctx->buffer + dctx->bytes, src, 144 GHASH_DIGEST_SIZE - dctx->bytes); 145 preempt_disable(); 146 pagefault_disable(); 147 enable_kernel_vsx(); 148 gcm_ghash_p8(dctx->shash, ctx->htable, 149 dctx->buffer, GHASH_DIGEST_SIZE); 150 disable_kernel_vsx(); 151 pagefault_enable(); 152 preempt_enable(); 153 src += GHASH_DIGEST_SIZE - dctx->bytes; 154 srclen -= GHASH_DIGEST_SIZE - dctx->bytes; 155 dctx->bytes = 0; 156 } 157 len = srclen & ~(GHASH_DIGEST_SIZE - 1); 158 if (len) { 159 preempt_disable(); 160 pagefault_disable(); 161 enable_kernel_vsx(); 162 gcm_ghash_p8(dctx->shash, ctx->htable, src, len); 163 disable_kernel_vsx(); 164 pagefault_enable(); 165 preempt_enable(); 166 src += len; 167 srclen -= len; 168 } 169 if (srclen) { 170 memcpy(dctx->buffer, src, srclen); 171 dctx->bytes = srclen; 172 } 173 return 0; 174 } 175 } 176 177 static int p8_ghash_final(struct shash_desc *desc, u8 *out) 178 { 179 int i; 180 struct p8_ghash_ctx *ctx = crypto_tfm_ctx(crypto_shash_tfm(desc->tfm)); 181 struct p8_ghash_desc_ctx *dctx = shash_desc_ctx(desc); 182 183 if (!crypto_simd_usable()) { 184 return crypto_shash_final(&dctx->fallback_desc, out); 185 } else { 186 if (dctx->bytes) { 187 for (i = dctx->bytes; i < GHASH_DIGEST_SIZE; i++) 188 dctx->buffer[i] = 0; 189 preempt_disable(); 190 pagefault_disable(); 191 enable_kernel_vsx(); 192 gcm_ghash_p8(dctx->shash, ctx->htable, 193 dctx->buffer, GHASH_DIGEST_SIZE); 194 disable_kernel_vsx(); 195 pagefault_enable(); 196 preempt_enable(); 197 dctx->bytes = 0; 198 } 199 memcpy(out, dctx->shash, GHASH_DIGEST_SIZE); 200 return 0; 201 } 202 } 203 204 struct shash_alg p8_ghash_alg = { 205 .digestsize = GHASH_DIGEST_SIZE, 206 .init = p8_ghash_init, 207 .update = p8_ghash_update, 208 .final = p8_ghash_final, 209 .setkey = p8_ghash_setkey, 210 .descsize = sizeof(struct p8_ghash_desc_ctx) 211 + sizeof(struct ghash_desc_ctx), 212 .base = { 213 .cra_name = "ghash", 214 .cra_driver_name = "p8_ghash", 215 .cra_priority = 1000, 216 .cra_flags = CRYPTO_ALG_NEED_FALLBACK, 217 .cra_blocksize = GHASH_BLOCK_SIZE, 218 .cra_ctxsize = sizeof(struct p8_ghash_ctx), 219 .cra_module = THIS_MODULE, 220 .cra_init = p8_ghash_init_tfm, 221 .cra_exit = p8_ghash_exit_tfm, 222 }, 223 }; 224