xref: /openbmc/linux/arch/sparc/crypto/sha512_glue.c (revision 03ab8e6297acd1bc0eedaa050e2a1635c576fd11)
109c434b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2775e0c69SDavid S. Miller /* Glue code for SHA512 hashing optimized for sparc64 crypto opcodes.
3775e0c69SDavid S. Miller  *
4775e0c69SDavid S. Miller  * This is based largely upon crypto/sha512_generic.c
5775e0c69SDavid S. Miller  *
6775e0c69SDavid S. Miller  * Copyright (c) Jean-Luc Cooke <jlcooke@certainkey.com>
7775e0c69SDavid S. Miller  * Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk>
8775e0c69SDavid S. Miller  * Copyright (c) 2003 Kyle McMartin <kyle@debian.org>
9775e0c69SDavid S. Miller  */
10775e0c69SDavid S. Miller 
11775e0c69SDavid S. Miller #define pr_fmt(fmt)	KBUILD_MODNAME ": " fmt
12775e0c69SDavid S. Miller 
13775e0c69SDavid S. Miller #include <crypto/internal/hash.h>
14775e0c69SDavid S. Miller #include <linux/init.h>
15775e0c69SDavid S. Miller #include <linux/module.h>
16775e0c69SDavid S. Miller #include <linux/mm.h>
17775e0c69SDavid S. Miller #include <linux/types.h>
18a24d22b2SEric Biggers #include <crypto/sha2.h>
19*e0583b6aSTianjia Zhang #include <crypto/sha512_base.h>
20775e0c69SDavid S. Miller 
21775e0c69SDavid S. Miller #include <asm/pstate.h>
22775e0c69SDavid S. Miller #include <asm/elf.h>
23775e0c69SDavid S. Miller 
2410803624SDavid S. Miller #include "opcodes.h"
2510803624SDavid S. Miller 
26775e0c69SDavid S. Miller asmlinkage void sha512_sparc64_transform(u64 *digest, const char *data,
27775e0c69SDavid S. Miller 					 unsigned int rounds);
28775e0c69SDavid S. Miller 
__sha512_sparc64_update(struct sha512_state * sctx,const u8 * data,unsigned int len,unsigned int partial)29775e0c69SDavid S. Miller static void __sha512_sparc64_update(struct sha512_state *sctx, const u8 *data,
30775e0c69SDavid S. Miller 				    unsigned int len, unsigned int partial)
31775e0c69SDavid S. Miller {
32775e0c69SDavid S. Miller 	unsigned int done = 0;
33775e0c69SDavid S. Miller 
34775e0c69SDavid S. Miller 	if ((sctx->count[0] += len) < len)
35775e0c69SDavid S. Miller 		sctx->count[1]++;
36775e0c69SDavid S. Miller 	if (partial) {
37775e0c69SDavid S. Miller 		done = SHA512_BLOCK_SIZE - partial;
38775e0c69SDavid S. Miller 		memcpy(sctx->buf + partial, data, done);
39775e0c69SDavid S. Miller 		sha512_sparc64_transform(sctx->state, sctx->buf, 1);
40775e0c69SDavid S. Miller 	}
41775e0c69SDavid S. Miller 	if (len - done >= SHA512_BLOCK_SIZE) {
42775e0c69SDavid S. Miller 		const unsigned int rounds = (len - done) / SHA512_BLOCK_SIZE;
43775e0c69SDavid S. Miller 
44775e0c69SDavid S. Miller 		sha512_sparc64_transform(sctx->state, data + done, rounds);
45775e0c69SDavid S. Miller 		done += rounds * SHA512_BLOCK_SIZE;
46775e0c69SDavid S. Miller 	}
47775e0c69SDavid S. Miller 
48775e0c69SDavid S. Miller 	memcpy(sctx->buf, data + done, len - done);
49775e0c69SDavid S. Miller }
50775e0c69SDavid S. Miller 
sha512_sparc64_update(struct shash_desc * desc,const u8 * data,unsigned int len)51775e0c69SDavid S. Miller static int sha512_sparc64_update(struct shash_desc *desc, const u8 *data,
52775e0c69SDavid S. Miller 				 unsigned int len)
53775e0c69SDavid S. Miller {
54775e0c69SDavid S. Miller 	struct sha512_state *sctx = shash_desc_ctx(desc);
55775e0c69SDavid S. Miller 	unsigned int partial = sctx->count[0] % SHA512_BLOCK_SIZE;
56775e0c69SDavid S. Miller 
57775e0c69SDavid S. Miller 	/* Handle the fast case right here */
58775e0c69SDavid S. Miller 	if (partial + len < SHA512_BLOCK_SIZE) {
59775e0c69SDavid S. Miller 		if ((sctx->count[0] += len) < len)
60775e0c69SDavid S. Miller 			sctx->count[1]++;
61775e0c69SDavid S. Miller 		memcpy(sctx->buf + partial, data, len);
62775e0c69SDavid S. Miller 	} else
63775e0c69SDavid S. Miller 		__sha512_sparc64_update(sctx, data, len, partial);
64775e0c69SDavid S. Miller 
65775e0c69SDavid S. Miller 	return 0;
66775e0c69SDavid S. Miller }
67775e0c69SDavid S. Miller 
sha512_sparc64_final(struct shash_desc * desc,u8 * out)68775e0c69SDavid S. Miller static int sha512_sparc64_final(struct shash_desc *desc, u8 *out)
69775e0c69SDavid S. Miller {
70775e0c69SDavid S. Miller 	struct sha512_state *sctx = shash_desc_ctx(desc);
71775e0c69SDavid S. Miller 	unsigned int i, index, padlen;
72775e0c69SDavid S. Miller 	__be64 *dst = (__be64 *)out;
73775e0c69SDavid S. Miller 	__be64 bits[2];
74775e0c69SDavid S. Miller 	static const u8 padding[SHA512_BLOCK_SIZE] = { 0x80, };
75775e0c69SDavid S. Miller 
76775e0c69SDavid S. Miller 	/* Save number of bits */
77775e0c69SDavid S. Miller 	bits[1] = cpu_to_be64(sctx->count[0] << 3);
78775e0c69SDavid S. Miller 	bits[0] = cpu_to_be64(sctx->count[1] << 3 | sctx->count[0] >> 61);
79775e0c69SDavid S. Miller 
80775e0c69SDavid S. Miller 	/* Pad out to 112 mod 128 and append length */
81775e0c69SDavid S. Miller 	index = sctx->count[0] % SHA512_BLOCK_SIZE;
82775e0c69SDavid S. Miller 	padlen = (index < 112) ? (112 - index) : ((SHA512_BLOCK_SIZE+112) - index);
83775e0c69SDavid S. Miller 
84775e0c69SDavid S. Miller 	/* We need to fill a whole block for __sha512_sparc64_update() */
85775e0c69SDavid S. Miller 	if (padlen <= 112) {
86775e0c69SDavid S. Miller 		if ((sctx->count[0] += padlen) < padlen)
87775e0c69SDavid S. Miller 			sctx->count[1]++;
88775e0c69SDavid S. Miller 		memcpy(sctx->buf + index, padding, padlen);
89775e0c69SDavid S. Miller 	} else {
90775e0c69SDavid S. Miller 		__sha512_sparc64_update(sctx, padding, padlen, index);
91775e0c69SDavid S. Miller 	}
92775e0c69SDavid S. Miller 	__sha512_sparc64_update(sctx, (const u8 *)&bits, sizeof(bits), 112);
93775e0c69SDavid S. Miller 
94775e0c69SDavid S. Miller 	/* Store state in digest */
95775e0c69SDavid S. Miller 	for (i = 0; i < 8; i++)
96775e0c69SDavid S. Miller 		dst[i] = cpu_to_be64(sctx->state[i]);
97775e0c69SDavid S. Miller 
98775e0c69SDavid S. Miller 	/* Wipe context */
99775e0c69SDavid S. Miller 	memset(sctx, 0, sizeof(*sctx));
100775e0c69SDavid S. Miller 
101775e0c69SDavid S. Miller 	return 0;
102775e0c69SDavid S. Miller }
103775e0c69SDavid S. Miller 
sha384_sparc64_final(struct shash_desc * desc,u8 * hash)104775e0c69SDavid S. Miller static int sha384_sparc64_final(struct shash_desc *desc, u8 *hash)
105775e0c69SDavid S. Miller {
106775e0c69SDavid S. Miller 	u8 D[64];
107775e0c69SDavid S. Miller 
108775e0c69SDavid S. Miller 	sha512_sparc64_final(desc, D);
109775e0c69SDavid S. Miller 
110775e0c69SDavid S. Miller 	memcpy(hash, D, 48);
1118202cd72SJulia Lawall 	memzero_explicit(D, 64);
112775e0c69SDavid S. Miller 
113775e0c69SDavid S. Miller 	return 0;
114775e0c69SDavid S. Miller }
115775e0c69SDavid S. Miller 
116775e0c69SDavid S. Miller static struct shash_alg sha512 = {
117775e0c69SDavid S. Miller 	.digestsize	=	SHA512_DIGEST_SIZE,
118*e0583b6aSTianjia Zhang 	.init		=	sha512_base_init,
119775e0c69SDavid S. Miller 	.update		=	sha512_sparc64_update,
120775e0c69SDavid S. Miller 	.final		=	sha512_sparc64_final,
121775e0c69SDavid S. Miller 	.descsize	=	sizeof(struct sha512_state),
122775e0c69SDavid S. Miller 	.base		=	{
123775e0c69SDavid S. Miller 		.cra_name	=	"sha512",
124775e0c69SDavid S. Miller 		.cra_driver_name=	"sha512-sparc64",
12510803624SDavid S. Miller 		.cra_priority	=	SPARC_CR_OPCODE_PRIORITY,
126775e0c69SDavid S. Miller 		.cra_blocksize	=	SHA512_BLOCK_SIZE,
127775e0c69SDavid S. Miller 		.cra_module	=	THIS_MODULE,
128775e0c69SDavid S. Miller 	}
129775e0c69SDavid S. Miller };
130775e0c69SDavid S. Miller 
131775e0c69SDavid S. Miller static struct shash_alg sha384 = {
132775e0c69SDavid S. Miller 	.digestsize	=	SHA384_DIGEST_SIZE,
133*e0583b6aSTianjia Zhang 	.init		=	sha384_base_init,
134775e0c69SDavid S. Miller 	.update		=	sha512_sparc64_update,
135775e0c69SDavid S. Miller 	.final		=	sha384_sparc64_final,
136775e0c69SDavid S. Miller 	.descsize	=	sizeof(struct sha512_state),
137775e0c69SDavid S. Miller 	.base		=	{
138775e0c69SDavid S. Miller 		.cra_name	=	"sha384",
139775e0c69SDavid S. Miller 		.cra_driver_name=	"sha384-sparc64",
14010803624SDavid S. Miller 		.cra_priority	=	SPARC_CR_OPCODE_PRIORITY,
141775e0c69SDavid S. Miller 		.cra_blocksize	=	SHA384_BLOCK_SIZE,
142775e0c69SDavid S. Miller 		.cra_module	=	THIS_MODULE,
143775e0c69SDavid S. Miller 	}
144775e0c69SDavid S. Miller };
145775e0c69SDavid S. Miller 
sparc64_has_sha512_opcode(void)146775e0c69SDavid S. Miller static bool __init sparc64_has_sha512_opcode(void)
147775e0c69SDavid S. Miller {
148775e0c69SDavid S. Miller 	unsigned long cfr;
149775e0c69SDavid S. Miller 
150775e0c69SDavid S. Miller 	if (!(sparc64_elf_hwcap & HWCAP_SPARC_CRYPTO))
151775e0c69SDavid S. Miller 		return false;
152775e0c69SDavid S. Miller 
153775e0c69SDavid S. Miller 	__asm__ __volatile__("rd %%asr26, %0" : "=r" (cfr));
154775e0c69SDavid S. Miller 	if (!(cfr & CFR_SHA512))
155775e0c69SDavid S. Miller 		return false;
156775e0c69SDavid S. Miller 
157775e0c69SDavid S. Miller 	return true;
158775e0c69SDavid S. Miller }
159775e0c69SDavid S. Miller 
sha512_sparc64_mod_init(void)160775e0c69SDavid S. Miller static int __init sha512_sparc64_mod_init(void)
161775e0c69SDavid S. Miller {
162775e0c69SDavid S. Miller 	if (sparc64_has_sha512_opcode()) {
163775e0c69SDavid S. Miller 		int ret = crypto_register_shash(&sha384);
164775e0c69SDavid S. Miller 		if (ret < 0)
165775e0c69SDavid S. Miller 			return ret;
166775e0c69SDavid S. Miller 
167775e0c69SDavid S. Miller 		ret = crypto_register_shash(&sha512);
168775e0c69SDavid S. Miller 		if (ret < 0) {
169775e0c69SDavid S. Miller 			crypto_unregister_shash(&sha384);
170775e0c69SDavid S. Miller 			return ret;
171775e0c69SDavid S. Miller 		}
172775e0c69SDavid S. Miller 
173775e0c69SDavid S. Miller 		pr_info("Using sparc64 sha512 opcode optimized SHA-512/SHA-384 implementation\n");
174775e0c69SDavid S. Miller 		return 0;
175775e0c69SDavid S. Miller 	}
176775e0c69SDavid S. Miller 	pr_info("sparc64 sha512 opcode not available.\n");
177775e0c69SDavid S. Miller 	return -ENODEV;
178775e0c69SDavid S. Miller }
179775e0c69SDavid S. Miller 
sha512_sparc64_mod_fini(void)180775e0c69SDavid S. Miller static void __exit sha512_sparc64_mod_fini(void)
181775e0c69SDavid S. Miller {
182775e0c69SDavid S. Miller 	crypto_unregister_shash(&sha384);
183775e0c69SDavid S. Miller 	crypto_unregister_shash(&sha512);
184775e0c69SDavid S. Miller }
185775e0c69SDavid S. Miller 
186775e0c69SDavid S. Miller module_init(sha512_sparc64_mod_init);
187775e0c69SDavid S. Miller module_exit(sha512_sparc64_mod_fini);
188775e0c69SDavid S. Miller 
189775e0c69SDavid S. Miller MODULE_LICENSE("GPL");
190775e0c69SDavid S. Miller MODULE_DESCRIPTION("SHA-384 and SHA-512 Secure Hash Algorithm, sparc64 sha512 opcode accelerated");
191775e0c69SDavid S. Miller 
1925d26a105SKees Cook MODULE_ALIAS_CRYPTO("sha384");
1935d26a105SKees Cook MODULE_ALIAS_CRYPTO("sha512");
194226f7ceaSDavid S. Miller 
195226f7ceaSDavid S. Miller #include "crop_devid.c"
196