ghash.c 4.5 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2 3 4
/**
 * GHASH routines supporting VMX instructions on the Power 8
 *
5
 * Copyright (C) 2015, 2019 International Business Machines Inc.
6 7
 *
 * Author: Marcelo Henrique Cerri <mhcerri@br.ibm.com>
8 9 10 11
 *
 * Extended by Daniel Axtens <dja@axtens.net> to replace the fallback
 * mechanism. The new approach is based on arm64 code, which is:
 *   Copyright (C) 2014 - 2018 Linaro Ltd. <ard.biesheuvel@linaro.org>
12 13 14 15 16 17 18 19 20
 */

#include <linux/types.h>
#include <linux/err.h>
#include <linux/crypto.h>
#include <linux/delay.h>
#include <linux/hardirq.h>
#include <asm/switch_to.h>
#include <crypto/aes.h>
21
#include <crypto/ghash.h>
22 23 24 25 26 27 28 29 30
#include <crypto/scatterwalk.h>
#include <crypto/internal/hash.h>
#include <crypto/b128ops.h>

#define IN_INTERRUPT in_interrupt()

void gcm_init_p8(u128 htable[16], const u64 Xi[2]);
void gcm_gmult_p8(u64 Xi[2], const u128 htable[16]);
void gcm_ghash_p8(u64 Xi[2], const u128 htable[16],
31
		  const u8 *in, size_t len);
32 33

struct p8_ghash_ctx {
34
	/* key used by vector asm */
35
	u128 htable[16];
36 37
	/* key used by software fallback */
	be128 key;
38 39 40
};

struct p8_ghash_desc_ctx {
41 42 43
	u64 shash[2];
	u8 buffer[GHASH_DIGEST_SIZE];
	int bytes;
44 45 46 47
};

static int p8_ghash_init(struct shash_desc *desc)
{
48 49 50 51
	struct p8_ghash_desc_ctx *dctx = shash_desc_ctx(desc);

	dctx->bytes = 0;
	memset(dctx->shash, 0, GHASH_DIGEST_SIZE);
52
	return 0;
53 54 55
}

static int p8_ghash_setkey(struct crypto_shash *tfm, const u8 *key,
56
			   unsigned int keylen)
57
{
58
	struct p8_ghash_ctx *ctx = crypto_tfm_ctx(crypto_shash_tfm(tfm));
59

60
	if (keylen != GHASH_BLOCK_SIZE)
61
		return -EINVAL;
62

63
	preempt_disable();
64
	pagefault_disable();
65
	enable_kernel_vsx();
66
	gcm_init_p8(ctx->htable, (const u64 *) key);
67
	disable_kernel_vsx();
68
	pagefault_enable();
69
	preempt_enable();
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114

	memcpy(&ctx->key, key, GHASH_BLOCK_SIZE);

	return 0;
}

static inline void __ghash_block(struct p8_ghash_ctx *ctx,
				 struct p8_ghash_desc_ctx *dctx)
{
	if (!IN_INTERRUPT) {
		preempt_disable();
		pagefault_disable();
		enable_kernel_vsx();
		gcm_ghash_p8(dctx->shash, ctx->htable,
				dctx->buffer, GHASH_DIGEST_SIZE);
		disable_kernel_vsx();
		pagefault_enable();
		preempt_enable();
	} else {
		crypto_xor((u8 *)dctx->shash, dctx->buffer, GHASH_BLOCK_SIZE);
		gf128mul_lle((be128 *)dctx->shash, &ctx->key);
	}
}

static inline void __ghash_blocks(struct p8_ghash_ctx *ctx,
				  struct p8_ghash_desc_ctx *dctx,
				  const u8 *src, unsigned int srclen)
{
	if (!IN_INTERRUPT) {
		preempt_disable();
		pagefault_disable();
		enable_kernel_vsx();
		gcm_ghash_p8(dctx->shash, ctx->htable,
				src, srclen);
		disable_kernel_vsx();
		pagefault_enable();
		preempt_enable();
	} else {
		while (srclen >= GHASH_BLOCK_SIZE) {
			crypto_xor((u8 *)dctx->shash, src, GHASH_BLOCK_SIZE);
			gf128mul_lle((be128 *)dctx->shash, &ctx->key);
			srclen -= GHASH_BLOCK_SIZE;
			src += GHASH_BLOCK_SIZE;
		}
	}
115 116 117
}

static int p8_ghash_update(struct shash_desc *desc,
118
			   const u8 *src, unsigned int srclen)
119
{
120 121 122 123
	unsigned int len;
	struct p8_ghash_ctx *ctx = crypto_tfm_ctx(crypto_shash_tfm(desc->tfm));
	struct p8_ghash_desc_ctx *dctx = shash_desc_ctx(desc);

124 125
	if (dctx->bytes) {
		if (dctx->bytes + srclen < GHASH_DIGEST_SIZE) {
126
			memcpy(dctx->buffer + dctx->bytes, src,
127 128 129
				srclen);
			dctx->bytes += srclen;
			return 0;
130
		}
131 132 133 134 135 136 137 138 139 140 141 142 143 144
		memcpy(dctx->buffer + dctx->bytes, src,
			GHASH_DIGEST_SIZE - dctx->bytes);

		__ghash_block(ctx, dctx);

		src += GHASH_DIGEST_SIZE - dctx->bytes;
		srclen -= GHASH_DIGEST_SIZE - dctx->bytes;
		dctx->bytes = 0;
	}
	len = srclen & ~(GHASH_DIGEST_SIZE - 1);
	if (len) {
		__ghash_blocks(ctx, dctx, src, len);
		src += len;
		srclen -= len;
145
	}
146 147 148 149 150
	if (srclen) {
		memcpy(dctx->buffer, src, srclen);
		dctx->bytes = srclen;
	}
	return 0;
151 152 153 154
}

static int p8_ghash_final(struct shash_desc *desc, u8 *out)
{
155 156 157 158
	int i;
	struct p8_ghash_ctx *ctx = crypto_tfm_ctx(crypto_shash_tfm(desc->tfm));
	struct p8_ghash_desc_ctx *dctx = shash_desc_ctx(desc);

159 160 161 162 163
	if (dctx->bytes) {
		for (i = dctx->bytes; i < GHASH_DIGEST_SIZE; i++)
			dctx->buffer[i] = 0;
		__ghash_block(ctx, dctx);
		dctx->bytes = 0;
164
	}
165 166
	memcpy(out, dctx->shash, GHASH_DIGEST_SIZE);
	return 0;
167 168 169
}

struct shash_alg p8_ghash_alg = {
170 171 172 173 174
	.digestsize = GHASH_DIGEST_SIZE,
	.init = p8_ghash_init,
	.update = p8_ghash_update,
	.final = p8_ghash_final,
	.setkey = p8_ghash_setkey,
175 176
	.descsize = sizeof(struct p8_ghash_desc_ctx)
		+ sizeof(struct ghash_desc_ctx),
177 178 179 180 181 182 183 184
	.base = {
		 .cra_name = "ghash",
		 .cra_driver_name = "p8_ghash",
		 .cra_priority = 1000,
		 .cra_blocksize = GHASH_BLOCK_SIZE,
		 .cra_ctxsize = sizeof(struct p8_ghash_ctx),
		 .cra_module = THIS_MODULE,
	},
185
};