xref: /openbmc/linux/crypto/md5.c (revision 14b75ba70da925a9f040a7575cb46ad7d394b117)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * Cryptographic API.
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * MD5 Message Digest Algorithm (RFC1321).
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  * Derived from cryptoapi implementation, originally based on the
71da177e4SLinus Torvalds  * public domain implementation written by Colin Plumb in 1993.
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  * Copyright (c) Cryptoapi developers.
101da177e4SLinus Torvalds  * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
111da177e4SLinus Torvalds  *
121da177e4SLinus Torvalds  * This program is free software; you can redistribute it and/or modify it
131da177e4SLinus Torvalds  * under the terms of the GNU General Public License as published by the Free
141da177e4SLinus Torvalds  * Software Foundation; either version 2 of the License, or (at your option)
151da177e4SLinus Torvalds  * any later version.
161da177e4SLinus Torvalds  *
171da177e4SLinus Torvalds  */
18*14b75ba7SAdrian-Ken Rueegsegger #include <crypto/internal/hash.h>
191da177e4SLinus Torvalds #include <linux/init.h>
201da177e4SLinus Torvalds #include <linux/module.h>
211da177e4SLinus Torvalds #include <linux/string.h>
2206ace7a9SHerbert Xu #include <linux/types.h>
231da177e4SLinus Torvalds #include <asm/byteorder.h>
241da177e4SLinus Torvalds 
251da177e4SLinus Torvalds #define MD5_DIGEST_SIZE		16
261da177e4SLinus Torvalds #define MD5_HMAC_BLOCK_SIZE	64
271da177e4SLinus Torvalds #define MD5_BLOCK_WORDS		16
281da177e4SLinus Torvalds #define MD5_HASH_WORDS		4
291da177e4SLinus Torvalds 
301da177e4SLinus Torvalds #define F1(x, y, z)	(z ^ (x & (y ^ z)))
311da177e4SLinus Torvalds #define F2(x, y, z)	F1(z, x, y)
321da177e4SLinus Torvalds #define F3(x, y, z)	(x ^ y ^ z)
331da177e4SLinus Torvalds #define F4(x, y, z)	(y ^ (x | ~z))
341da177e4SLinus Torvalds 
351da177e4SLinus Torvalds #define MD5STEP(f, w, x, y, z, in, s) \
361da177e4SLinus Torvalds 	(w += f(x, y, z) + in, w = (w<<s | w>>(32-s)) + x)
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds struct md5_ctx {
391da177e4SLinus Torvalds 	u32 hash[MD5_HASH_WORDS];
401da177e4SLinus Torvalds 	u32 block[MD5_BLOCK_WORDS];
411da177e4SLinus Torvalds 	u64 byte_count;
421da177e4SLinus Torvalds };
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds static void md5_transform(u32 *hash, u32 const *in)
451da177e4SLinus Torvalds {
461da177e4SLinus Torvalds 	u32 a, b, c, d;
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds 	a = hash[0];
491da177e4SLinus Torvalds 	b = hash[1];
501da177e4SLinus Torvalds 	c = hash[2];
511da177e4SLinus Torvalds 	d = hash[3];
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds 	MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
541da177e4SLinus Torvalds 	MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
551da177e4SLinus Torvalds 	MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
561da177e4SLinus Torvalds 	MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
571da177e4SLinus Torvalds 	MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
581da177e4SLinus Torvalds 	MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
591da177e4SLinus Torvalds 	MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
601da177e4SLinus Torvalds 	MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
611da177e4SLinus Torvalds 	MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
621da177e4SLinus Torvalds 	MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
631da177e4SLinus Torvalds 	MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
641da177e4SLinus Torvalds 	MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
651da177e4SLinus Torvalds 	MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
661da177e4SLinus Torvalds 	MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
671da177e4SLinus Torvalds 	MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
681da177e4SLinus Torvalds 	MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds 	MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
711da177e4SLinus Torvalds 	MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
721da177e4SLinus Torvalds 	MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
731da177e4SLinus Torvalds 	MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
741da177e4SLinus Torvalds 	MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
751da177e4SLinus Torvalds 	MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
761da177e4SLinus Torvalds 	MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
771da177e4SLinus Torvalds 	MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
781da177e4SLinus Torvalds 	MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
791da177e4SLinus Torvalds 	MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
801da177e4SLinus Torvalds 	MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
811da177e4SLinus Torvalds 	MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
821da177e4SLinus Torvalds 	MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
831da177e4SLinus Torvalds 	MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
841da177e4SLinus Torvalds 	MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
851da177e4SLinus Torvalds 	MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds 	MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
881da177e4SLinus Torvalds 	MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
891da177e4SLinus Torvalds 	MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
901da177e4SLinus Torvalds 	MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
911da177e4SLinus Torvalds 	MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
921da177e4SLinus Torvalds 	MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
931da177e4SLinus Torvalds 	MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
941da177e4SLinus Torvalds 	MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
951da177e4SLinus Torvalds 	MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
961da177e4SLinus Torvalds 	MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
971da177e4SLinus Torvalds 	MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
981da177e4SLinus Torvalds 	MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
991da177e4SLinus Torvalds 	MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
1001da177e4SLinus Torvalds 	MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
1011da177e4SLinus Torvalds 	MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
1021da177e4SLinus Torvalds 	MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
1031da177e4SLinus Torvalds 
1041da177e4SLinus Torvalds 	MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
1051da177e4SLinus Torvalds 	MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
1061da177e4SLinus Torvalds 	MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
1071da177e4SLinus Torvalds 	MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
1081da177e4SLinus Torvalds 	MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
1091da177e4SLinus Torvalds 	MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
1101da177e4SLinus Torvalds 	MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
1111da177e4SLinus Torvalds 	MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
1121da177e4SLinus Torvalds 	MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
1131da177e4SLinus Torvalds 	MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
1141da177e4SLinus Torvalds 	MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
1151da177e4SLinus Torvalds 	MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
1161da177e4SLinus Torvalds 	MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
1171da177e4SLinus Torvalds 	MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
1181da177e4SLinus Torvalds 	MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
1191da177e4SLinus Torvalds 	MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
1201da177e4SLinus Torvalds 
1211da177e4SLinus Torvalds 	hash[0] += a;
1221da177e4SLinus Torvalds 	hash[1] += b;
1231da177e4SLinus Torvalds 	hash[2] += c;
1241da177e4SLinus Torvalds 	hash[3] += d;
1251da177e4SLinus Torvalds }
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds /* XXX: this stuff can be optimized */
1281da177e4SLinus Torvalds static inline void le32_to_cpu_array(u32 *buf, unsigned int words)
1291da177e4SLinus Torvalds {
1301da177e4SLinus Torvalds 	while (words--) {
1311da177e4SLinus Torvalds 		__le32_to_cpus(buf);
1321da177e4SLinus Torvalds 		buf++;
1331da177e4SLinus Torvalds 	}
1341da177e4SLinus Torvalds }
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds static inline void cpu_to_le32_array(u32 *buf, unsigned int words)
1371da177e4SLinus Torvalds {
1381da177e4SLinus Torvalds 	while (words--) {
1391da177e4SLinus Torvalds 		__cpu_to_le32s(buf);
1401da177e4SLinus Torvalds 		buf++;
1411da177e4SLinus Torvalds 	}
1421da177e4SLinus Torvalds }
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds static inline void md5_transform_helper(struct md5_ctx *ctx)
1451da177e4SLinus Torvalds {
1461da177e4SLinus Torvalds 	le32_to_cpu_array(ctx->block, sizeof(ctx->block) / sizeof(u32));
1471da177e4SLinus Torvalds 	md5_transform(ctx->hash, ctx->block);
1481da177e4SLinus Torvalds }
1491da177e4SLinus Torvalds 
150*14b75ba7SAdrian-Ken Rueegsegger static int md5_init(struct shash_desc *desc)
1511da177e4SLinus Torvalds {
152*14b75ba7SAdrian-Ken Rueegsegger 	struct md5_ctx *mctx = shash_desc_ctx(desc);
1531da177e4SLinus Torvalds 
1541da177e4SLinus Torvalds 	mctx->hash[0] = 0x67452301;
1551da177e4SLinus Torvalds 	mctx->hash[1] = 0xefcdab89;
1561da177e4SLinus Torvalds 	mctx->hash[2] = 0x98badcfe;
1571da177e4SLinus Torvalds 	mctx->hash[3] = 0x10325476;
1581da177e4SLinus Torvalds 	mctx->byte_count = 0;
159*14b75ba7SAdrian-Ken Rueegsegger 
160*14b75ba7SAdrian-Ken Rueegsegger 	return 0;
1611da177e4SLinus Torvalds }
1621da177e4SLinus Torvalds 
163*14b75ba7SAdrian-Ken Rueegsegger static int md5_update(struct shash_desc *desc, const u8 *data, unsigned int len)
1641da177e4SLinus Torvalds {
165*14b75ba7SAdrian-Ken Rueegsegger 	struct md5_ctx *mctx = shash_desc_ctx(desc);
1661da177e4SLinus Torvalds 	const u32 avail = sizeof(mctx->block) - (mctx->byte_count & 0x3f);
1671da177e4SLinus Torvalds 
1681da177e4SLinus Torvalds 	mctx->byte_count += len;
1691da177e4SLinus Torvalds 
1701da177e4SLinus Torvalds 	if (avail > len) {
1711da177e4SLinus Torvalds 		memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
1721da177e4SLinus Torvalds 		       data, len);
173*14b75ba7SAdrian-Ken Rueegsegger 		return 0;
1741da177e4SLinus Torvalds 	}
1751da177e4SLinus Torvalds 
1761da177e4SLinus Torvalds 	memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
1771da177e4SLinus Torvalds 	       data, avail);
1781da177e4SLinus Torvalds 
1791da177e4SLinus Torvalds 	md5_transform_helper(mctx);
1801da177e4SLinus Torvalds 	data += avail;
1811da177e4SLinus Torvalds 	len -= avail;
1821da177e4SLinus Torvalds 
1831da177e4SLinus Torvalds 	while (len >= sizeof(mctx->block)) {
1841da177e4SLinus Torvalds 		memcpy(mctx->block, data, sizeof(mctx->block));
1851da177e4SLinus Torvalds 		md5_transform_helper(mctx);
1861da177e4SLinus Torvalds 		data += sizeof(mctx->block);
1871da177e4SLinus Torvalds 		len -= sizeof(mctx->block);
1881da177e4SLinus Torvalds 	}
1891da177e4SLinus Torvalds 
1901da177e4SLinus Torvalds 	memcpy(mctx->block, data, len);
191*14b75ba7SAdrian-Ken Rueegsegger 
192*14b75ba7SAdrian-Ken Rueegsegger 	return 0;
1931da177e4SLinus Torvalds }
1941da177e4SLinus Torvalds 
195*14b75ba7SAdrian-Ken Rueegsegger static int md5_final(struct shash_desc *desc, u8 *out)
1961da177e4SLinus Torvalds {
197*14b75ba7SAdrian-Ken Rueegsegger 	struct md5_ctx *mctx = shash_desc_ctx(desc);
1981da177e4SLinus Torvalds 	const unsigned int offset = mctx->byte_count & 0x3f;
1991da177e4SLinus Torvalds 	char *p = (char *)mctx->block + offset;
2001da177e4SLinus Torvalds 	int padding = 56 - (offset + 1);
2011da177e4SLinus Torvalds 
2021da177e4SLinus Torvalds 	*p++ = 0x80;
2031da177e4SLinus Torvalds 	if (padding < 0) {
2041da177e4SLinus Torvalds 		memset(p, 0x00, padding + sizeof (u64));
2051da177e4SLinus Torvalds 		md5_transform_helper(mctx);
2061da177e4SLinus Torvalds 		p = (char *)mctx->block;
2071da177e4SLinus Torvalds 		padding = 56;
2081da177e4SLinus Torvalds 	}
2091da177e4SLinus Torvalds 
2101da177e4SLinus Torvalds 	memset(p, 0, padding);
2111da177e4SLinus Torvalds 	mctx->block[14] = mctx->byte_count << 3;
2121da177e4SLinus Torvalds 	mctx->block[15] = mctx->byte_count >> 29;
2131da177e4SLinus Torvalds 	le32_to_cpu_array(mctx->block, (sizeof(mctx->block) -
2141da177e4SLinus Torvalds 	                  sizeof(u64)) / sizeof(u32));
2151da177e4SLinus Torvalds 	md5_transform(mctx->hash, mctx->block);
2161da177e4SLinus Torvalds 	cpu_to_le32_array(mctx->hash, sizeof(mctx->hash) / sizeof(u32));
2171da177e4SLinus Torvalds 	memcpy(out, mctx->hash, sizeof(mctx->hash));
2181da177e4SLinus Torvalds 	memset(mctx, 0, sizeof(*mctx));
219*14b75ba7SAdrian-Ken Rueegsegger 
220*14b75ba7SAdrian-Ken Rueegsegger 	return 0;
2211da177e4SLinus Torvalds }
2221da177e4SLinus Torvalds 
223*14b75ba7SAdrian-Ken Rueegsegger static struct shash_alg alg = {
224*14b75ba7SAdrian-Ken Rueegsegger 	.digestsize	=	MD5_DIGEST_SIZE,
225*14b75ba7SAdrian-Ken Rueegsegger 	.init		=	md5_init,
226*14b75ba7SAdrian-Ken Rueegsegger 	.update		=	md5_update,
227*14b75ba7SAdrian-Ken Rueegsegger 	.final		=	md5_final,
228*14b75ba7SAdrian-Ken Rueegsegger 	.descsize	=	sizeof(struct md5_ctx),
229*14b75ba7SAdrian-Ken Rueegsegger 	.base		=	{
2301da177e4SLinus Torvalds 		.cra_name	=	"md5",
231*14b75ba7SAdrian-Ken Rueegsegger 		.cra_flags	=	CRYPTO_ALG_TYPE_SHASH,
2321da177e4SLinus Torvalds 		.cra_blocksize	=	MD5_HMAC_BLOCK_SIZE,
2331da177e4SLinus Torvalds 		.cra_module	=	THIS_MODULE,
234*14b75ba7SAdrian-Ken Rueegsegger 	}
2351da177e4SLinus Torvalds };
2361da177e4SLinus Torvalds 
2373af5b90bSKamalesh Babulal static int __init md5_mod_init(void)
2381da177e4SLinus Torvalds {
239*14b75ba7SAdrian-Ken Rueegsegger 	return crypto_register_shash(&alg);
2401da177e4SLinus Torvalds }
2411da177e4SLinus Torvalds 
2423af5b90bSKamalesh Babulal static void __exit md5_mod_fini(void)
2431da177e4SLinus Torvalds {
244*14b75ba7SAdrian-Ken Rueegsegger 	crypto_unregister_shash(&alg);
2451da177e4SLinus Torvalds }
2461da177e4SLinus Torvalds 
2473af5b90bSKamalesh Babulal module_init(md5_mod_init);
2483af5b90bSKamalesh Babulal module_exit(md5_mod_fini);
2491da177e4SLinus Torvalds 
2501da177e4SLinus Torvalds MODULE_LICENSE("GPL");
2511da177e4SLinus Torvalds MODULE_DESCRIPTION("MD5 Message Digest Algorithm");
252