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 */ 1814b75ba7SAdrian-Ken Rueegsegger #include <crypto/internal/hash.h> 197d6f75ebSMax Vozeler #include <crypto/md5.h> 201da177e4SLinus Torvalds #include <linux/init.h> 211da177e4SLinus Torvalds #include <linux/module.h> 221da177e4SLinus Torvalds #include <linux/string.h> 2306ace7a9SHerbert Xu #include <linux/types.h> 241da177e4SLinus Torvalds #include <asm/byteorder.h> 251da177e4SLinus Torvalds 26*3c7eb3ccSJason A. Donenfeld #define MD5_DIGEST_WORDS 4 27*3c7eb3ccSJason A. Donenfeld #define MD5_MESSAGE_BYTES 64 28*3c7eb3ccSJason A. Donenfeld 290c4c78deSLABBE Corentin const u8 md5_zero_message_hash[MD5_DIGEST_SIZE] = { 300c4c78deSLABBE Corentin 0xd4, 0x1d, 0x8c, 0xd9, 0x8f, 0x00, 0xb2, 0x04, 310c4c78deSLABBE Corentin 0xe9, 0x80, 0x09, 0x98, 0xec, 0xf8, 0x42, 0x7e, 320c4c78deSLABBE Corentin }; 330c4c78deSLABBE Corentin EXPORT_SYMBOL_GPL(md5_zero_message_hash); 340c4c78deSLABBE Corentin 351da177e4SLinus Torvalds /* XXX: this stuff can be optimized */ 361da177e4SLinus Torvalds static inline void le32_to_cpu_array(u32 *buf, unsigned int words) 371da177e4SLinus Torvalds { 381da177e4SLinus Torvalds while (words--) { 391da177e4SLinus Torvalds __le32_to_cpus(buf); 401da177e4SLinus Torvalds buf++; 411da177e4SLinus Torvalds } 421da177e4SLinus Torvalds } 431da177e4SLinus Torvalds 441da177e4SLinus Torvalds static inline void cpu_to_le32_array(u32 *buf, unsigned int words) 451da177e4SLinus Torvalds { 461da177e4SLinus Torvalds while (words--) { 471da177e4SLinus Torvalds __cpu_to_le32s(buf); 481da177e4SLinus Torvalds buf++; 491da177e4SLinus Torvalds } 501da177e4SLinus Torvalds } 511da177e4SLinus Torvalds 52*3c7eb3ccSJason A. Donenfeld #define F1(x, y, z) (z ^ (x & (y ^ z))) 53*3c7eb3ccSJason A. Donenfeld #define F2(x, y, z) F1(z, x, y) 54*3c7eb3ccSJason A. Donenfeld #define F3(x, y, z) (x ^ y ^ z) 55*3c7eb3ccSJason A. Donenfeld #define F4(x, y, z) (y ^ (x | ~z)) 56*3c7eb3ccSJason A. Donenfeld 57*3c7eb3ccSJason A. Donenfeld #define MD5STEP(f, w, x, y, z, in, s) \ 58*3c7eb3ccSJason A. Donenfeld (w += f(x, y, z) + in, w = (w<<s | w>>(32-s)) + x) 59*3c7eb3ccSJason A. Donenfeld 60*3c7eb3ccSJason A. Donenfeld static void md5_transform(__u32 *hash, __u32 const *in) 61*3c7eb3ccSJason A. Donenfeld { 62*3c7eb3ccSJason A. Donenfeld u32 a, b, c, d; 63*3c7eb3ccSJason A. Donenfeld 64*3c7eb3ccSJason A. Donenfeld a = hash[0]; 65*3c7eb3ccSJason A. Donenfeld b = hash[1]; 66*3c7eb3ccSJason A. Donenfeld c = hash[2]; 67*3c7eb3ccSJason A. Donenfeld d = hash[3]; 68*3c7eb3ccSJason A. Donenfeld 69*3c7eb3ccSJason A. Donenfeld MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7); 70*3c7eb3ccSJason A. Donenfeld MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12); 71*3c7eb3ccSJason A. Donenfeld MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17); 72*3c7eb3ccSJason A. Donenfeld MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22); 73*3c7eb3ccSJason A. Donenfeld MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7); 74*3c7eb3ccSJason A. Donenfeld MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12); 75*3c7eb3ccSJason A. Donenfeld MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17); 76*3c7eb3ccSJason A. Donenfeld MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22); 77*3c7eb3ccSJason A. Donenfeld MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7); 78*3c7eb3ccSJason A. Donenfeld MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12); 79*3c7eb3ccSJason A. Donenfeld MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17); 80*3c7eb3ccSJason A. Donenfeld MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22); 81*3c7eb3ccSJason A. Donenfeld MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7); 82*3c7eb3ccSJason A. Donenfeld MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12); 83*3c7eb3ccSJason A. Donenfeld MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17); 84*3c7eb3ccSJason A. Donenfeld MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22); 85*3c7eb3ccSJason A. Donenfeld 86*3c7eb3ccSJason A. Donenfeld MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5); 87*3c7eb3ccSJason A. Donenfeld MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9); 88*3c7eb3ccSJason A. Donenfeld MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14); 89*3c7eb3ccSJason A. Donenfeld MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20); 90*3c7eb3ccSJason A. Donenfeld MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5); 91*3c7eb3ccSJason A. Donenfeld MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9); 92*3c7eb3ccSJason A. Donenfeld MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14); 93*3c7eb3ccSJason A. Donenfeld MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20); 94*3c7eb3ccSJason A. Donenfeld MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5); 95*3c7eb3ccSJason A. Donenfeld MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9); 96*3c7eb3ccSJason A. Donenfeld MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14); 97*3c7eb3ccSJason A. Donenfeld MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20); 98*3c7eb3ccSJason A. Donenfeld MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5); 99*3c7eb3ccSJason A. Donenfeld MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9); 100*3c7eb3ccSJason A. Donenfeld MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14); 101*3c7eb3ccSJason A. Donenfeld MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20); 102*3c7eb3ccSJason A. Donenfeld 103*3c7eb3ccSJason A. Donenfeld MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4); 104*3c7eb3ccSJason A. Donenfeld MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11); 105*3c7eb3ccSJason A. Donenfeld MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16); 106*3c7eb3ccSJason A. Donenfeld MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23); 107*3c7eb3ccSJason A. Donenfeld MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4); 108*3c7eb3ccSJason A. Donenfeld MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11); 109*3c7eb3ccSJason A. Donenfeld MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16); 110*3c7eb3ccSJason A. Donenfeld MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23); 111*3c7eb3ccSJason A. Donenfeld MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4); 112*3c7eb3ccSJason A. Donenfeld MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11); 113*3c7eb3ccSJason A. Donenfeld MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16); 114*3c7eb3ccSJason A. Donenfeld MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23); 115*3c7eb3ccSJason A. Donenfeld MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4); 116*3c7eb3ccSJason A. Donenfeld MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11); 117*3c7eb3ccSJason A. Donenfeld MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16); 118*3c7eb3ccSJason A. Donenfeld MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23); 119*3c7eb3ccSJason A. Donenfeld 120*3c7eb3ccSJason A. Donenfeld MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6); 121*3c7eb3ccSJason A. Donenfeld MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10); 122*3c7eb3ccSJason A. Donenfeld MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15); 123*3c7eb3ccSJason A. Donenfeld MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21); 124*3c7eb3ccSJason A. Donenfeld MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6); 125*3c7eb3ccSJason A. Donenfeld MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10); 126*3c7eb3ccSJason A. Donenfeld MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15); 127*3c7eb3ccSJason A. Donenfeld MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21); 128*3c7eb3ccSJason A. Donenfeld MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6); 129*3c7eb3ccSJason A. Donenfeld MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10); 130*3c7eb3ccSJason A. Donenfeld MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15); 131*3c7eb3ccSJason A. Donenfeld MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21); 132*3c7eb3ccSJason A. Donenfeld MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6); 133*3c7eb3ccSJason A. Donenfeld MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10); 134*3c7eb3ccSJason A. Donenfeld MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15); 135*3c7eb3ccSJason A. Donenfeld MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21); 136*3c7eb3ccSJason A. Donenfeld 137*3c7eb3ccSJason A. Donenfeld hash[0] += a; 138*3c7eb3ccSJason A. Donenfeld hash[1] += b; 139*3c7eb3ccSJason A. Donenfeld hash[2] += c; 140*3c7eb3ccSJason A. Donenfeld hash[3] += d; 141*3c7eb3ccSJason A. Donenfeld } 142*3c7eb3ccSJason A. Donenfeld 1437d6f75ebSMax Vozeler static inline void md5_transform_helper(struct md5_state *ctx) 1441da177e4SLinus Torvalds { 1451da177e4SLinus Torvalds le32_to_cpu_array(ctx->block, sizeof(ctx->block) / sizeof(u32)); 1461da177e4SLinus Torvalds md5_transform(ctx->hash, ctx->block); 1471da177e4SLinus Torvalds } 1481da177e4SLinus Torvalds 14914b75ba7SAdrian-Ken Rueegsegger static int md5_init(struct shash_desc *desc) 1501da177e4SLinus Torvalds { 1517d6f75ebSMax Vozeler struct md5_state *mctx = shash_desc_ctx(desc); 1521da177e4SLinus Torvalds 1536a185016SLABBE Corentin mctx->hash[0] = MD5_H0; 1546a185016SLABBE Corentin mctx->hash[1] = MD5_H1; 1556a185016SLABBE Corentin mctx->hash[2] = MD5_H2; 1566a185016SLABBE Corentin mctx->hash[3] = MD5_H3; 1571da177e4SLinus Torvalds mctx->byte_count = 0; 15814b75ba7SAdrian-Ken Rueegsegger 15914b75ba7SAdrian-Ken Rueegsegger return 0; 1601da177e4SLinus Torvalds } 1611da177e4SLinus Torvalds 16214b75ba7SAdrian-Ken Rueegsegger static int md5_update(struct shash_desc *desc, const u8 *data, unsigned int len) 1631da177e4SLinus Torvalds { 1647d6f75ebSMax Vozeler struct md5_state *mctx = shash_desc_ctx(desc); 1651da177e4SLinus Torvalds const u32 avail = sizeof(mctx->block) - (mctx->byte_count & 0x3f); 1661da177e4SLinus Torvalds 1671da177e4SLinus Torvalds mctx->byte_count += len; 1681da177e4SLinus Torvalds 1691da177e4SLinus Torvalds if (avail > len) { 1701da177e4SLinus Torvalds memcpy((char *)mctx->block + (sizeof(mctx->block) - avail), 1711da177e4SLinus Torvalds data, len); 17214b75ba7SAdrian-Ken Rueegsegger return 0; 1731da177e4SLinus Torvalds } 1741da177e4SLinus Torvalds 1751da177e4SLinus Torvalds memcpy((char *)mctx->block + (sizeof(mctx->block) - avail), 1761da177e4SLinus Torvalds data, avail); 1771da177e4SLinus Torvalds 1781da177e4SLinus Torvalds md5_transform_helper(mctx); 1791da177e4SLinus Torvalds data += avail; 1801da177e4SLinus Torvalds len -= avail; 1811da177e4SLinus Torvalds 1821da177e4SLinus Torvalds while (len >= sizeof(mctx->block)) { 1831da177e4SLinus Torvalds memcpy(mctx->block, data, sizeof(mctx->block)); 1841da177e4SLinus Torvalds md5_transform_helper(mctx); 1851da177e4SLinus Torvalds data += sizeof(mctx->block); 1861da177e4SLinus Torvalds len -= sizeof(mctx->block); 1871da177e4SLinus Torvalds } 1881da177e4SLinus Torvalds 1891da177e4SLinus Torvalds memcpy(mctx->block, data, len); 19014b75ba7SAdrian-Ken Rueegsegger 19114b75ba7SAdrian-Ken Rueegsegger return 0; 1921da177e4SLinus Torvalds } 1931da177e4SLinus Torvalds 19414b75ba7SAdrian-Ken Rueegsegger static int md5_final(struct shash_desc *desc, u8 *out) 1951da177e4SLinus Torvalds { 1967d6f75ebSMax Vozeler struct md5_state *mctx = shash_desc_ctx(desc); 1971da177e4SLinus Torvalds const unsigned int offset = mctx->byte_count & 0x3f; 1981da177e4SLinus Torvalds char *p = (char *)mctx->block + offset; 1991da177e4SLinus Torvalds int padding = 56 - (offset + 1); 2001da177e4SLinus Torvalds 2011da177e4SLinus Torvalds *p++ = 0x80; 2021da177e4SLinus Torvalds if (padding < 0) { 2031da177e4SLinus Torvalds memset(p, 0x00, padding + sizeof (u64)); 2041da177e4SLinus Torvalds md5_transform_helper(mctx); 2051da177e4SLinus Torvalds p = (char *)mctx->block; 2061da177e4SLinus Torvalds padding = 56; 2071da177e4SLinus Torvalds } 2081da177e4SLinus Torvalds 2091da177e4SLinus Torvalds memset(p, 0, padding); 2101da177e4SLinus Torvalds mctx->block[14] = mctx->byte_count << 3; 2111da177e4SLinus Torvalds mctx->block[15] = mctx->byte_count >> 29; 2121da177e4SLinus Torvalds le32_to_cpu_array(mctx->block, (sizeof(mctx->block) - 2131da177e4SLinus Torvalds sizeof(u64)) / sizeof(u32)); 2141da177e4SLinus Torvalds md5_transform(mctx->hash, mctx->block); 2151da177e4SLinus Torvalds cpu_to_le32_array(mctx->hash, sizeof(mctx->hash) / sizeof(u32)); 2161da177e4SLinus Torvalds memcpy(out, mctx->hash, sizeof(mctx->hash)); 2171da177e4SLinus Torvalds memset(mctx, 0, sizeof(*mctx)); 21814b75ba7SAdrian-Ken Rueegsegger 21914b75ba7SAdrian-Ken Rueegsegger return 0; 2201da177e4SLinus Torvalds } 2211da177e4SLinus Torvalds 2227d6f75ebSMax Vozeler static int md5_export(struct shash_desc *desc, void *out) 2237d6f75ebSMax Vozeler { 2247d6f75ebSMax Vozeler struct md5_state *ctx = shash_desc_ctx(desc); 2257d6f75ebSMax Vozeler 2267d6f75ebSMax Vozeler memcpy(out, ctx, sizeof(*ctx)); 2277d6f75ebSMax Vozeler return 0; 2287d6f75ebSMax Vozeler } 2297d6f75ebSMax Vozeler 2307d6f75ebSMax Vozeler static int md5_import(struct shash_desc *desc, const void *in) 2317d6f75ebSMax Vozeler { 2327d6f75ebSMax Vozeler struct md5_state *ctx = shash_desc_ctx(desc); 2337d6f75ebSMax Vozeler 2347d6f75ebSMax Vozeler memcpy(ctx, in, sizeof(*ctx)); 2357d6f75ebSMax Vozeler return 0; 2367d6f75ebSMax Vozeler } 2377d6f75ebSMax Vozeler 23814b75ba7SAdrian-Ken Rueegsegger static struct shash_alg alg = { 23914b75ba7SAdrian-Ken Rueegsegger .digestsize = MD5_DIGEST_SIZE, 24014b75ba7SAdrian-Ken Rueegsegger .init = md5_init, 24114b75ba7SAdrian-Ken Rueegsegger .update = md5_update, 24214b75ba7SAdrian-Ken Rueegsegger .final = md5_final, 2437d6f75ebSMax Vozeler .export = md5_export, 2447d6f75ebSMax Vozeler .import = md5_import, 2457d6f75ebSMax Vozeler .descsize = sizeof(struct md5_state), 246eebb111fSHerbert Xu .statesize = sizeof(struct md5_state), 24714b75ba7SAdrian-Ken Rueegsegger .base = { 2481da177e4SLinus Torvalds .cra_name = "md5", 24914b75ba7SAdrian-Ken Rueegsegger .cra_flags = CRYPTO_ALG_TYPE_SHASH, 2501da177e4SLinus Torvalds .cra_blocksize = MD5_HMAC_BLOCK_SIZE, 2511da177e4SLinus Torvalds .cra_module = THIS_MODULE, 25214b75ba7SAdrian-Ken Rueegsegger } 2531da177e4SLinus Torvalds }; 2541da177e4SLinus Torvalds 2553af5b90bSKamalesh Babulal static int __init md5_mod_init(void) 2561da177e4SLinus Torvalds { 25714b75ba7SAdrian-Ken Rueegsegger return crypto_register_shash(&alg); 2581da177e4SLinus Torvalds } 2591da177e4SLinus Torvalds 2603af5b90bSKamalesh Babulal static void __exit md5_mod_fini(void) 2611da177e4SLinus Torvalds { 26214b75ba7SAdrian-Ken Rueegsegger crypto_unregister_shash(&alg); 2631da177e4SLinus Torvalds } 2641da177e4SLinus Torvalds 2653af5b90bSKamalesh Babulal module_init(md5_mod_init); 2663af5b90bSKamalesh Babulal module_exit(md5_mod_fini); 2671da177e4SLinus Torvalds 2681da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 2691da177e4SLinus Torvalds MODULE_DESCRIPTION("MD5 Message Digest Algorithm"); 2705d26a105SKees Cook MODULE_ALIAS_CRYPTO("md5"); 271