1 /*
2  * Cryptographic API.
3  *
4  * MD5 Message Digest Algorithm (RFC1321).
5  *
6  * Adapted for OCTEON by Aaro Koskinen <aaro.koskinen@iki.fi>.
7  *
8  * Based on crypto/md5.c, which is:
9  *
10  * Derived from cryptoapi implementation, originally based on the
11  * public domain implementation written by Colin Plumb in 1993.
12  *
13  * Copyright (c) Cryptoapi developers.
14  * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
15  *
16  * This program is free software; you can redistribute it and/or modify it
17  * under the terms of the GNU General Public License as published by the Free
18  * Software Foundation; either version 2 of the License, or (at your option)
19  * any later version.
20  */
21 
22 #include <crypto/md5.h>
23 #include <linux/init.h>
24 #include <linux/types.h>
25 #include <linux/module.h>
26 #include <linux/string.h>
27 #include <asm/byteorder.h>
28 #include <asm/octeon/octeon.h>
29 #include <crypto/internal/hash.h>
30 
31 #include "octeon-crypto.h"
32 
33 /*
34  * We pass everything as 64-bit. OCTEON can handle misaligned data.
35  */
36 
37 static void octeon_md5_store_hash(struct md5_state *ctx)
38 {
39 	u64 *hash = (u64 *)ctx->hash;
40 
41 	write_octeon_64bit_hash_dword(hash[0], 0);
42 	write_octeon_64bit_hash_dword(hash[1], 1);
43 }
44 
45 static void octeon_md5_read_hash(struct md5_state *ctx)
46 {
47 	u64 *hash = (u64 *)ctx->hash;
48 
49 	hash[0] = read_octeon_64bit_hash_dword(0);
50 	hash[1] = read_octeon_64bit_hash_dword(1);
51 }
52 
53 static void octeon_md5_transform(const void *_block)
54 {
55 	const u64 *block = _block;
56 
57 	write_octeon_64bit_block_dword(block[0], 0);
58 	write_octeon_64bit_block_dword(block[1], 1);
59 	write_octeon_64bit_block_dword(block[2], 2);
60 	write_octeon_64bit_block_dword(block[3], 3);
61 	write_octeon_64bit_block_dword(block[4], 4);
62 	write_octeon_64bit_block_dword(block[5], 5);
63 	write_octeon_64bit_block_dword(block[6], 6);
64 	octeon_md5_start(block[7]);
65 }
66 
67 static int octeon_md5_init(struct shash_desc *desc)
68 {
69 	struct md5_state *mctx = shash_desc_ctx(desc);
70 
71 	mctx->hash[0] = cpu_to_le32(MD5_H0);
72 	mctx->hash[1] = cpu_to_le32(MD5_H1);
73 	mctx->hash[2] = cpu_to_le32(MD5_H2);
74 	mctx->hash[3] = cpu_to_le32(MD5_H3);
75 	mctx->byte_count = 0;
76 
77 	return 0;
78 }
79 
80 static int octeon_md5_update(struct shash_desc *desc, const u8 *data,
81 			     unsigned int len)
82 {
83 	struct md5_state *mctx = shash_desc_ctx(desc);
84 	const u32 avail = sizeof(mctx->block) - (mctx->byte_count & 0x3f);
85 	struct octeon_cop2_state state;
86 	unsigned long flags;
87 
88 	mctx->byte_count += len;
89 
90 	if (avail > len) {
91 		memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
92 		       data, len);
93 		return 0;
94 	}
95 
96 	memcpy((char *)mctx->block + (sizeof(mctx->block) - avail), data,
97 	       avail);
98 
99 	flags = octeon_crypto_enable(&state);
100 	octeon_md5_store_hash(mctx);
101 
102 	octeon_md5_transform(mctx->block);
103 	data += avail;
104 	len -= avail;
105 
106 	while (len >= sizeof(mctx->block)) {
107 		octeon_md5_transform(data);
108 		data += sizeof(mctx->block);
109 		len -= sizeof(mctx->block);
110 	}
111 
112 	octeon_md5_read_hash(mctx);
113 	octeon_crypto_disable(&state, flags);
114 
115 	memcpy(mctx->block, data, len);
116 
117 	return 0;
118 }
119 
120 static int octeon_md5_final(struct shash_desc *desc, u8 *out)
121 {
122 	struct md5_state *mctx = shash_desc_ctx(desc);
123 	const unsigned int offset = mctx->byte_count & 0x3f;
124 	char *p = (char *)mctx->block + offset;
125 	int padding = 56 - (offset + 1);
126 	struct octeon_cop2_state state;
127 	unsigned long flags;
128 
129 	*p++ = 0x80;
130 
131 	flags = octeon_crypto_enable(&state);
132 	octeon_md5_store_hash(mctx);
133 
134 	if (padding < 0) {
135 		memset(p, 0x00, padding + sizeof(u64));
136 		octeon_md5_transform(mctx->block);
137 		p = (char *)mctx->block;
138 		padding = 56;
139 	}
140 
141 	memset(p, 0, padding);
142 	mctx->block[14] = cpu_to_le32(mctx->byte_count << 3);
143 	mctx->block[15] = cpu_to_le32(mctx->byte_count >> 29);
144 	octeon_md5_transform(mctx->block);
145 
146 	octeon_md5_read_hash(mctx);
147 	octeon_crypto_disable(&state, flags);
148 
149 	memcpy(out, mctx->hash, sizeof(mctx->hash));
150 	memset(mctx, 0, sizeof(*mctx));
151 
152 	return 0;
153 }
154 
155 static int octeon_md5_export(struct shash_desc *desc, void *out)
156 {
157 	struct md5_state *ctx = shash_desc_ctx(desc);
158 
159 	memcpy(out, ctx, sizeof(*ctx));
160 	return 0;
161 }
162 
163 static int octeon_md5_import(struct shash_desc *desc, const void *in)
164 {
165 	struct md5_state *ctx = shash_desc_ctx(desc);
166 
167 	memcpy(ctx, in, sizeof(*ctx));
168 	return 0;
169 }
170 
171 static struct shash_alg alg = {
172 	.digestsize	=	MD5_DIGEST_SIZE,
173 	.init		=	octeon_md5_init,
174 	.update		=	octeon_md5_update,
175 	.final		=	octeon_md5_final,
176 	.export		=	octeon_md5_export,
177 	.import		=	octeon_md5_import,
178 	.descsize	=	sizeof(struct md5_state),
179 	.statesize	=	sizeof(struct md5_state),
180 	.base		=	{
181 		.cra_name	=	"md5",
182 		.cra_driver_name=	"octeon-md5",
183 		.cra_priority	=	OCTEON_CR_OPCODE_PRIORITY,
184 		.cra_blocksize	=	MD5_HMAC_BLOCK_SIZE,
185 		.cra_module	=	THIS_MODULE,
186 	}
187 };
188 
189 static int __init md5_mod_init(void)
190 {
191 	if (!octeon_has_crypto())
192 		return -ENOTSUPP;
193 	return crypto_register_shash(&alg);
194 }
195 
196 static void __exit md5_mod_fini(void)
197 {
198 	crypto_unregister_shash(&alg);
199 }
200 
201 module_init(md5_mod_init);
202 module_exit(md5_mod_fini);
203 
204 MODULE_LICENSE("GPL");
205 MODULE_DESCRIPTION("MD5 Message Digest Algorithm (OCTEON)");
206 MODULE_AUTHOR("Aaro Koskinen <aaro.koskinen@iki.fi>");
207