summaryrefslogtreecommitdiffstats
path: root/arch/riscv/crypto/ghash-riscv64-glue.c
blob: 312e7891fd0a36ceee68de31d90f6b1918e684f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// SPDX-License-Identifier: GPL-2.0-only
/*
 * GHASH using the RISC-V vector crypto extensions
 *
 * Copyright (C) 2023 VRULL GmbH
 * Author: Heiko Stuebner <heiko.stuebner@vrull.eu>
 *
 * Copyright (C) 2023 SiFive, Inc.
 * Author: Jerry Shih <jerry.shih@sifive.com>
 */

#include <asm/simd.h>
#include <asm/vector.h>
#include <crypto/ghash.h>
#include <crypto/internal/hash.h>
#include <crypto/internal/simd.h>
#include <linux/linkage.h>
#include <linux/module.h>

asmlinkage void ghash_zvkg(be128 *accumulator, const be128 *key, const u8 *data,
			   size_t len);

struct riscv64_ghash_tfm_ctx {
	be128 key;
};

struct riscv64_ghash_desc_ctx {
	be128 accumulator;
	u8 buffer[GHASH_BLOCK_SIZE];
	u32 bytes;
};

static int riscv64_ghash_setkey(struct crypto_shash *tfm, const u8 *key,
				unsigned int keylen)
{
	struct riscv64_ghash_tfm_ctx *tctx = crypto_shash_ctx(tfm);

	if (keylen != GHASH_BLOCK_SIZE)
		return -EINVAL;

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

	return 0;
}

static int riscv64_ghash_init(struct shash_desc *desc)
{
	struct riscv64_ghash_desc_ctx *dctx = shash_desc_ctx(desc);

	*dctx = (struct riscv64_ghash_desc_ctx){};

	return 0;
}

static inline void
riscv64_ghash_blocks(const struct riscv64_ghash_tfm_ctx *tctx,
		     struct riscv64_ghash_desc_ctx *dctx,
		     const u8 *src, size_t srclen)
{
	/* The srclen is nonzero and a multiple of 16. */
	if (crypto_simd_usable()) {
		kernel_vector_begin();
		ghash_zvkg(&dctx->accumulator, &tctx->key, src, srclen);
		kernel_vector_end();
	} else {
		do {
			crypto_xor((u8 *)&dctx->accumulator, src,
				   GHASH_BLOCK_SIZE);
			gf128mul_lle(&dctx->accumulator, &tctx->key);
			src += GHASH_BLOCK_SIZE;
			srclen -= GHASH_BLOCK_SIZE;
		} while (srclen);
	}
}

static int riscv64_ghash_update(struct shash_desc *desc, const u8 *src,
				unsigned int srclen)
{
	const struct riscv64_ghash_tfm_ctx *tctx = crypto_shash_ctx(desc->tfm);
	struct riscv64_ghash_desc_ctx *dctx = shash_desc_ctx(desc);
	unsigned int len;

	if (dctx->bytes) {
		if (dctx->bytes + srclen < GHASH_BLOCK_SIZE) {
			memcpy(dctx->buffer + dctx->bytes, src, srclen);
			dctx->bytes += srclen;
			return 0;
		}
		memcpy(dctx->buffer + dctx->bytes, src,
		       GHASH_BLOCK_SIZE - dctx->bytes);
		riscv64_ghash_blocks(tctx, dctx, dctx->buffer,
				     GHASH_BLOCK_SIZE);
		src += GHASH_BLOCK_SIZE - dctx->bytes;
		srclen -= GHASH_BLOCK_SIZE - dctx->bytes;
		dctx->bytes = 0;
	}

	len = round_down(srclen, GHASH_BLOCK_SIZE);
	if (len) {
		riscv64_ghash_blocks(tctx, dctx, src, len);
		src += len;
		srclen -= len;
	}

	if (srclen) {
		memcpy(dctx->buffer, src, srclen);
		dctx->bytes = srclen;
	}

	return 0;
}

static int riscv64_ghash_final(struct shash_desc *desc, u8 *out)
{
	const struct riscv64_ghash_tfm_ctx *tctx = crypto_shash_ctx(desc->tfm);
	struct riscv64_ghash_desc_ctx *dctx = shash_desc_ctx(desc);
	int i;

	if (dctx->bytes) {
		for (i = dctx->bytes; i < GHASH_BLOCK_SIZE; i++)
			dctx->buffer[i] = 0;

		riscv64_ghash_blocks(tctx, dctx, dctx->buffer,
				     GHASH_BLOCK_SIZE);
	}

	memcpy(out, &dctx->accumulator, GHASH_DIGEST_SIZE);
	return 0;
}

static struct shash_alg riscv64_ghash_alg = {
	.init = riscv64_ghash_init,
	.update = riscv64_ghash_update,
	.final = riscv64_ghash_final,
	.setkey = riscv64_ghash_setkey,
	.descsize = sizeof(struct riscv64_ghash_desc_ctx),
	.digestsize = GHASH_DIGEST_SIZE,
	.base = {
		.cra_blocksize = GHASH_BLOCK_SIZE,
		.cra_ctxsize = sizeof(struct riscv64_ghash_tfm_ctx),
		.cra_priority = 300,
		.cra_name = "ghash",
		.cra_driver_name = "ghash-riscv64-zvkg",
		.cra_module = THIS_MODULE,
	},
};

static int __init riscv64_ghash_mod_init(void)
{
	if (riscv_isa_extension_available(NULL, ZVKG) &&
	    riscv_vector_vlen() >= 128)
		return crypto_register_shash(&riscv64_ghash_alg);

	return -ENODEV;
}

static void __exit riscv64_ghash_mod_exit(void)
{
	crypto_unregister_shash(&riscv64_ghash_alg);
}

module_init(riscv64_ghash_mod_init);
module_exit(riscv64_ghash_mod_exit);

MODULE_DESCRIPTION("GHASH (RISC-V accelerated)");
MODULE_AUTHOR("Heiko Stuebner <heiko.stuebner@vrull.eu>");
MODULE_LICENSE("GPL");
MODULE_ALIAS_CRYPTO("ghash");