xref: /OK3568_Linux_fs/kernel/crypto/md5.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Cryptographic API.
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * MD5 Message Digest Algorithm (RFC1321).
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * Derived from cryptoapi implementation, originally based on the
7*4882a593Smuzhiyun  * public domain implementation written by Colin Plumb in 1993.
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * Copyright (c) Cryptoapi developers.
10*4882a593Smuzhiyun  * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
11*4882a593Smuzhiyun  *
12*4882a593Smuzhiyun  * This program is free software; you can redistribute it and/or modify it
13*4882a593Smuzhiyun  * under the terms of the GNU General Public License as published by the Free
14*4882a593Smuzhiyun  * Software Foundation; either version 2 of the License, or (at your option)
15*4882a593Smuzhiyun  * any later version.
16*4882a593Smuzhiyun  *
17*4882a593Smuzhiyun  */
18*4882a593Smuzhiyun #include <crypto/internal/hash.h>
19*4882a593Smuzhiyun #include <crypto/md5.h>
20*4882a593Smuzhiyun #include <linux/init.h>
21*4882a593Smuzhiyun #include <linux/module.h>
22*4882a593Smuzhiyun #include <linux/string.h>
23*4882a593Smuzhiyun #include <linux/types.h>
24*4882a593Smuzhiyun #include <asm/byteorder.h>
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun const u8 md5_zero_message_hash[MD5_DIGEST_SIZE] = {
27*4882a593Smuzhiyun 	0xd4, 0x1d, 0x8c, 0xd9, 0x8f, 0x00, 0xb2, 0x04,
28*4882a593Smuzhiyun 	0xe9, 0x80, 0x09, 0x98, 0xec, 0xf8, 0x42, 0x7e,
29*4882a593Smuzhiyun };
30*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(md5_zero_message_hash);
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun #define F1(x, y, z)	(z ^ (x & (y ^ z)))
33*4882a593Smuzhiyun #define F2(x, y, z)	F1(z, x, y)
34*4882a593Smuzhiyun #define F3(x, y, z)	(x ^ y ^ z)
35*4882a593Smuzhiyun #define F4(x, y, z)	(y ^ (x | ~z))
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun #define MD5STEP(f, w, x, y, z, in, s) \
38*4882a593Smuzhiyun 	(w += f(x, y, z) + in, w = (w<<s | w>>(32-s)) + x)
39*4882a593Smuzhiyun 
md5_transform(__u32 * hash,__u32 const * in)40*4882a593Smuzhiyun static void md5_transform(__u32 *hash, __u32 const *in)
41*4882a593Smuzhiyun {
42*4882a593Smuzhiyun 	u32 a, b, c, d;
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun 	a = hash[0];
45*4882a593Smuzhiyun 	b = hash[1];
46*4882a593Smuzhiyun 	c = hash[2];
47*4882a593Smuzhiyun 	d = hash[3];
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun 	MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
50*4882a593Smuzhiyun 	MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
51*4882a593Smuzhiyun 	MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
52*4882a593Smuzhiyun 	MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
53*4882a593Smuzhiyun 	MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
54*4882a593Smuzhiyun 	MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
55*4882a593Smuzhiyun 	MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
56*4882a593Smuzhiyun 	MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
57*4882a593Smuzhiyun 	MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
58*4882a593Smuzhiyun 	MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
59*4882a593Smuzhiyun 	MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
60*4882a593Smuzhiyun 	MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
61*4882a593Smuzhiyun 	MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
62*4882a593Smuzhiyun 	MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
63*4882a593Smuzhiyun 	MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
64*4882a593Smuzhiyun 	MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun 	MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
67*4882a593Smuzhiyun 	MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
68*4882a593Smuzhiyun 	MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
69*4882a593Smuzhiyun 	MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
70*4882a593Smuzhiyun 	MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
71*4882a593Smuzhiyun 	MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
72*4882a593Smuzhiyun 	MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
73*4882a593Smuzhiyun 	MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
74*4882a593Smuzhiyun 	MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
75*4882a593Smuzhiyun 	MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
76*4882a593Smuzhiyun 	MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
77*4882a593Smuzhiyun 	MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
78*4882a593Smuzhiyun 	MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
79*4882a593Smuzhiyun 	MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
80*4882a593Smuzhiyun 	MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
81*4882a593Smuzhiyun 	MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun 	MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
84*4882a593Smuzhiyun 	MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
85*4882a593Smuzhiyun 	MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
86*4882a593Smuzhiyun 	MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
87*4882a593Smuzhiyun 	MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
88*4882a593Smuzhiyun 	MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
89*4882a593Smuzhiyun 	MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
90*4882a593Smuzhiyun 	MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
91*4882a593Smuzhiyun 	MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
92*4882a593Smuzhiyun 	MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
93*4882a593Smuzhiyun 	MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
94*4882a593Smuzhiyun 	MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
95*4882a593Smuzhiyun 	MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
96*4882a593Smuzhiyun 	MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
97*4882a593Smuzhiyun 	MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
98*4882a593Smuzhiyun 	MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun 	MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
101*4882a593Smuzhiyun 	MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
102*4882a593Smuzhiyun 	MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
103*4882a593Smuzhiyun 	MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
104*4882a593Smuzhiyun 	MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
105*4882a593Smuzhiyun 	MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
106*4882a593Smuzhiyun 	MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
107*4882a593Smuzhiyun 	MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
108*4882a593Smuzhiyun 	MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
109*4882a593Smuzhiyun 	MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
110*4882a593Smuzhiyun 	MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
111*4882a593Smuzhiyun 	MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
112*4882a593Smuzhiyun 	MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
113*4882a593Smuzhiyun 	MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
114*4882a593Smuzhiyun 	MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
115*4882a593Smuzhiyun 	MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun 	hash[0] += a;
118*4882a593Smuzhiyun 	hash[1] += b;
119*4882a593Smuzhiyun 	hash[2] += c;
120*4882a593Smuzhiyun 	hash[3] += d;
121*4882a593Smuzhiyun }
122*4882a593Smuzhiyun 
md5_transform_helper(struct md5_state * ctx)123*4882a593Smuzhiyun static inline void md5_transform_helper(struct md5_state *ctx)
124*4882a593Smuzhiyun {
125*4882a593Smuzhiyun 	le32_to_cpu_array(ctx->block, sizeof(ctx->block) / sizeof(u32));
126*4882a593Smuzhiyun 	md5_transform(ctx->hash, ctx->block);
127*4882a593Smuzhiyun }
128*4882a593Smuzhiyun 
md5_init(struct shash_desc * desc)129*4882a593Smuzhiyun static int md5_init(struct shash_desc *desc)
130*4882a593Smuzhiyun {
131*4882a593Smuzhiyun 	struct md5_state *mctx = shash_desc_ctx(desc);
132*4882a593Smuzhiyun 
133*4882a593Smuzhiyun 	mctx->hash[0] = MD5_H0;
134*4882a593Smuzhiyun 	mctx->hash[1] = MD5_H1;
135*4882a593Smuzhiyun 	mctx->hash[2] = MD5_H2;
136*4882a593Smuzhiyun 	mctx->hash[3] = MD5_H3;
137*4882a593Smuzhiyun 	mctx->byte_count = 0;
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun 	return 0;
140*4882a593Smuzhiyun }
141*4882a593Smuzhiyun 
md5_update(struct shash_desc * desc,const u8 * data,unsigned int len)142*4882a593Smuzhiyun static int md5_update(struct shash_desc *desc, const u8 *data, unsigned int len)
143*4882a593Smuzhiyun {
144*4882a593Smuzhiyun 	struct md5_state *mctx = shash_desc_ctx(desc);
145*4882a593Smuzhiyun 	const u32 avail = sizeof(mctx->block) - (mctx->byte_count & 0x3f);
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun 	mctx->byte_count += len;
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun 	if (avail > len) {
150*4882a593Smuzhiyun 		memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
151*4882a593Smuzhiyun 		       data, len);
152*4882a593Smuzhiyun 		return 0;
153*4882a593Smuzhiyun 	}
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun 	memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
156*4882a593Smuzhiyun 	       data, avail);
157*4882a593Smuzhiyun 
158*4882a593Smuzhiyun 	md5_transform_helper(mctx);
159*4882a593Smuzhiyun 	data += avail;
160*4882a593Smuzhiyun 	len -= avail;
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun 	while (len >= sizeof(mctx->block)) {
163*4882a593Smuzhiyun 		memcpy(mctx->block, data, sizeof(mctx->block));
164*4882a593Smuzhiyun 		md5_transform_helper(mctx);
165*4882a593Smuzhiyun 		data += sizeof(mctx->block);
166*4882a593Smuzhiyun 		len -= sizeof(mctx->block);
167*4882a593Smuzhiyun 	}
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun 	memcpy(mctx->block, data, len);
170*4882a593Smuzhiyun 
171*4882a593Smuzhiyun 	return 0;
172*4882a593Smuzhiyun }
173*4882a593Smuzhiyun 
md5_final(struct shash_desc * desc,u8 * out)174*4882a593Smuzhiyun static int md5_final(struct shash_desc *desc, u8 *out)
175*4882a593Smuzhiyun {
176*4882a593Smuzhiyun 	struct md5_state *mctx = shash_desc_ctx(desc);
177*4882a593Smuzhiyun 	const unsigned int offset = mctx->byte_count & 0x3f;
178*4882a593Smuzhiyun 	char *p = (char *)mctx->block + offset;
179*4882a593Smuzhiyun 	int padding = 56 - (offset + 1);
180*4882a593Smuzhiyun 
181*4882a593Smuzhiyun 	*p++ = 0x80;
182*4882a593Smuzhiyun 	if (padding < 0) {
183*4882a593Smuzhiyun 		memset(p, 0x00, padding + sizeof (u64));
184*4882a593Smuzhiyun 		md5_transform_helper(mctx);
185*4882a593Smuzhiyun 		p = (char *)mctx->block;
186*4882a593Smuzhiyun 		padding = 56;
187*4882a593Smuzhiyun 	}
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun 	memset(p, 0, padding);
190*4882a593Smuzhiyun 	mctx->block[14] = mctx->byte_count << 3;
191*4882a593Smuzhiyun 	mctx->block[15] = mctx->byte_count >> 29;
192*4882a593Smuzhiyun 	le32_to_cpu_array(mctx->block, (sizeof(mctx->block) -
193*4882a593Smuzhiyun 	                  sizeof(u64)) / sizeof(u32));
194*4882a593Smuzhiyun 	md5_transform(mctx->hash, mctx->block);
195*4882a593Smuzhiyun 	cpu_to_le32_array(mctx->hash, sizeof(mctx->hash) / sizeof(u32));
196*4882a593Smuzhiyun 	memcpy(out, mctx->hash, sizeof(mctx->hash));
197*4882a593Smuzhiyun 	memset(mctx, 0, sizeof(*mctx));
198*4882a593Smuzhiyun 
199*4882a593Smuzhiyun 	return 0;
200*4882a593Smuzhiyun }
201*4882a593Smuzhiyun 
md5_export(struct shash_desc * desc,void * out)202*4882a593Smuzhiyun static int md5_export(struct shash_desc *desc, void *out)
203*4882a593Smuzhiyun {
204*4882a593Smuzhiyun 	struct md5_state *ctx = shash_desc_ctx(desc);
205*4882a593Smuzhiyun 
206*4882a593Smuzhiyun 	memcpy(out, ctx, sizeof(*ctx));
207*4882a593Smuzhiyun 	return 0;
208*4882a593Smuzhiyun }
209*4882a593Smuzhiyun 
md5_import(struct shash_desc * desc,const void * in)210*4882a593Smuzhiyun static int md5_import(struct shash_desc *desc, const void *in)
211*4882a593Smuzhiyun {
212*4882a593Smuzhiyun 	struct md5_state *ctx = shash_desc_ctx(desc);
213*4882a593Smuzhiyun 
214*4882a593Smuzhiyun 	memcpy(ctx, in, sizeof(*ctx));
215*4882a593Smuzhiyun 	return 0;
216*4882a593Smuzhiyun }
217*4882a593Smuzhiyun 
218*4882a593Smuzhiyun static struct shash_alg alg = {
219*4882a593Smuzhiyun 	.digestsize	=	MD5_DIGEST_SIZE,
220*4882a593Smuzhiyun 	.init		=	md5_init,
221*4882a593Smuzhiyun 	.update		=	md5_update,
222*4882a593Smuzhiyun 	.final		=	md5_final,
223*4882a593Smuzhiyun 	.export		=	md5_export,
224*4882a593Smuzhiyun 	.import		=	md5_import,
225*4882a593Smuzhiyun 	.descsize	=	sizeof(struct md5_state),
226*4882a593Smuzhiyun 	.statesize	=	sizeof(struct md5_state),
227*4882a593Smuzhiyun 	.base		=	{
228*4882a593Smuzhiyun 		.cra_name	 =	"md5",
229*4882a593Smuzhiyun 		.cra_driver_name =	"md5-generic",
230*4882a593Smuzhiyun 		.cra_blocksize	 =	MD5_HMAC_BLOCK_SIZE,
231*4882a593Smuzhiyun 		.cra_module	 =	THIS_MODULE,
232*4882a593Smuzhiyun 	}
233*4882a593Smuzhiyun };
234*4882a593Smuzhiyun 
md5_mod_init(void)235*4882a593Smuzhiyun static int __init md5_mod_init(void)
236*4882a593Smuzhiyun {
237*4882a593Smuzhiyun 	return crypto_register_shash(&alg);
238*4882a593Smuzhiyun }
239*4882a593Smuzhiyun 
md5_mod_fini(void)240*4882a593Smuzhiyun static void __exit md5_mod_fini(void)
241*4882a593Smuzhiyun {
242*4882a593Smuzhiyun 	crypto_unregister_shash(&alg);
243*4882a593Smuzhiyun }
244*4882a593Smuzhiyun 
245*4882a593Smuzhiyun subsys_initcall(md5_mod_init);
246*4882a593Smuzhiyun module_exit(md5_mod_fini);
247*4882a593Smuzhiyun 
248*4882a593Smuzhiyun MODULE_LICENSE("GPL");
249*4882a593Smuzhiyun MODULE_DESCRIPTION("MD5 Message Digest Algorithm");
250*4882a593Smuzhiyun MODULE_ALIAS_CRYPTO("md5");
251