1/* Glue code for MD5 hashing optimized for sparc64 crypto opcodes.
2 *
3 * This is based largely upon arch/x86/crypto/sha1_ssse3_glue.c
4 * and crypto/md5.c which are:
5 *
6 * Copyright (c) Alan Smithee.
7 * Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk>
8 * Copyright (c) Jean-Francois Dive <jef@linuxbe.org>
9 * Copyright (c) Mathias Krause <minipli@googlemail.com>
10 * Copyright (c) Cryptoapi developers.
11 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
12 */
13
14#define pr_fmt(fmt)	KBUILD_MODNAME ": " fmt
15
16#include <crypto/internal/hash.h>
17#include <linux/init.h>
18#include <linux/module.h>
19#include <linux/mm.h>
20#include <linux/cryptohash.h>
21#include <linux/types.h>
22#include <crypto/md5.h>
23
24#include <asm/pstate.h>
25#include <asm/elf.h>
26
27#include "opcodes.h"
28
29asmlinkage void md5_sparc64_transform(u32 *digest, const char *data,
30				      unsigned int rounds);
31
32static int md5_sparc64_init(struct shash_desc *desc)
33{
34	struct md5_state *mctx = shash_desc_ctx(desc);
35
36	mctx->hash[0] = cpu_to_le32(0x67452301);
37	mctx->hash[1] = cpu_to_le32(0xefcdab89);
38	mctx->hash[2] = cpu_to_le32(0x98badcfe);
39	mctx->hash[3] = cpu_to_le32(0x10325476);
40	mctx->byte_count = 0;
41
42	return 0;
43}
44
45static void __md5_sparc64_update(struct md5_state *sctx, const u8 *data,
46				 unsigned int len, unsigned int partial)
47{
48	unsigned int done = 0;
49
50	sctx->byte_count += len;
51	if (partial) {
52		done = MD5_HMAC_BLOCK_SIZE - partial;
53		memcpy((u8 *)sctx->block + partial, data, done);
54		md5_sparc64_transform(sctx->hash, (u8 *)sctx->block, 1);
55	}
56	if (len - done >= MD5_HMAC_BLOCK_SIZE) {
57		const unsigned int rounds = (len - done) / MD5_HMAC_BLOCK_SIZE;
58
59		md5_sparc64_transform(sctx->hash, data + done, rounds);
60		done += rounds * MD5_HMAC_BLOCK_SIZE;
61	}
62
63	memcpy(sctx->block, data + done, len - done);
64}
65
66static int md5_sparc64_update(struct shash_desc *desc, const u8 *data,
67			      unsigned int len)
68{
69	struct md5_state *sctx = shash_desc_ctx(desc);
70	unsigned int partial = sctx->byte_count % MD5_HMAC_BLOCK_SIZE;
71
72	/* Handle the fast case right here */
73	if (partial + len < MD5_HMAC_BLOCK_SIZE) {
74		sctx->byte_count += len;
75		memcpy((u8 *)sctx->block + partial, data, len);
76	} else
77		__md5_sparc64_update(sctx, data, len, partial);
78
79	return 0;
80}
81
82/* Add padding and return the message digest. */
83static int md5_sparc64_final(struct shash_desc *desc, u8 *out)
84{
85	struct md5_state *sctx = shash_desc_ctx(desc);
86	unsigned int i, index, padlen;
87	u32 *dst = (u32 *)out;
88	__le64 bits;
89	static const u8 padding[MD5_HMAC_BLOCK_SIZE] = { 0x80, };
90
91	bits = cpu_to_le64(sctx->byte_count << 3);
92
93	/* Pad out to 56 mod 64 and append length */
94	index = sctx->byte_count % MD5_HMAC_BLOCK_SIZE;
95	padlen = (index < 56) ? (56 - index) : ((MD5_HMAC_BLOCK_SIZE+56) - index);
96
97	/* We need to fill a whole block for __md5_sparc64_update() */
98	if (padlen <= 56) {
99		sctx->byte_count += padlen;
100		memcpy((u8 *)sctx->block + index, padding, padlen);
101	} else {
102		__md5_sparc64_update(sctx, padding, padlen, index);
103	}
104	__md5_sparc64_update(sctx, (const u8 *)&bits, sizeof(bits), 56);
105
106	/* Store state in digest */
107	for (i = 0; i < MD5_HASH_WORDS; i++)
108		dst[i] = sctx->hash[i];
109
110	/* Wipe context */
111	memset(sctx, 0, sizeof(*sctx));
112
113	return 0;
114}
115
116static int md5_sparc64_export(struct shash_desc *desc, void *out)
117{
118	struct md5_state *sctx = shash_desc_ctx(desc);
119
120	memcpy(out, sctx, sizeof(*sctx));
121
122	return 0;
123}
124
125static int md5_sparc64_import(struct shash_desc *desc, const void *in)
126{
127	struct md5_state *sctx = shash_desc_ctx(desc);
128
129	memcpy(sctx, in, sizeof(*sctx));
130
131	return 0;
132}
133
134static struct shash_alg alg = {
135	.digestsize	=	MD5_DIGEST_SIZE,
136	.init		=	md5_sparc64_init,
137	.update		=	md5_sparc64_update,
138	.final		=	md5_sparc64_final,
139	.export		=	md5_sparc64_export,
140	.import		=	md5_sparc64_import,
141	.descsize	=	sizeof(struct md5_state),
142	.statesize	=	sizeof(struct md5_state),
143	.base		=	{
144		.cra_name	=	"md5",
145		.cra_driver_name=	"md5-sparc64",
146		.cra_priority	=	SPARC_CR_OPCODE_PRIORITY,
147		.cra_flags	=	CRYPTO_ALG_TYPE_SHASH,
148		.cra_blocksize	=	MD5_HMAC_BLOCK_SIZE,
149		.cra_module	=	THIS_MODULE,
150	}
151};
152
153static bool __init sparc64_has_md5_opcode(void)
154{
155	unsigned long cfr;
156
157	if (!(sparc64_elf_hwcap & HWCAP_SPARC_CRYPTO))
158		return false;
159
160	__asm__ __volatile__("rd %%asr26, %0" : "=r" (cfr));
161	if (!(cfr & CFR_MD5))
162		return false;
163
164	return true;
165}
166
167static int __init md5_sparc64_mod_init(void)
168{
169	if (sparc64_has_md5_opcode()) {
170		pr_info("Using sparc64 md5 opcode optimized MD5 implementation\n");
171		return crypto_register_shash(&alg);
172	}
173	pr_info("sparc64 md5 opcode not available.\n");
174	return -ENODEV;
175}
176
177static void __exit md5_sparc64_mod_fini(void)
178{
179	crypto_unregister_shash(&alg);
180}
181
182module_init(md5_sparc64_mod_init);
183module_exit(md5_sparc64_mod_fini);
184
185MODULE_LICENSE("GPL");
186MODULE_DESCRIPTION("MD5 Message Digest Algorithm, sparc64 md5 opcode accelerated");
187
188MODULE_ALIAS_CRYPTO("md5");
189
190#include "crop_devid.c"
191