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> 24bc0b96b5SDavid S. Miller #include <linux/cryptohash.h> 251da177e4SLinus Torvalds #include <asm/byteorder.h> 261da177e4SLinus Torvalds 27*0c4c78deSLABBE Corentin const u8 md5_zero_message_hash[MD5_DIGEST_SIZE] = { 28*0c4c78deSLABBE Corentin 0xd4, 0x1d, 0x8c, 0xd9, 0x8f, 0x00, 0xb2, 0x04, 29*0c4c78deSLABBE Corentin 0xe9, 0x80, 0x09, 0x98, 0xec, 0xf8, 0x42, 0x7e, 30*0c4c78deSLABBE Corentin }; 31*0c4c78deSLABBE Corentin EXPORT_SYMBOL_GPL(md5_zero_message_hash); 32*0c4c78deSLABBE Corentin 331da177e4SLinus Torvalds /* XXX: this stuff can be optimized */ 341da177e4SLinus Torvalds static inline void le32_to_cpu_array(u32 *buf, unsigned int words) 351da177e4SLinus Torvalds { 361da177e4SLinus Torvalds while (words--) { 371da177e4SLinus Torvalds __le32_to_cpus(buf); 381da177e4SLinus Torvalds buf++; 391da177e4SLinus Torvalds } 401da177e4SLinus Torvalds } 411da177e4SLinus Torvalds 421da177e4SLinus Torvalds static inline void cpu_to_le32_array(u32 *buf, unsigned int words) 431da177e4SLinus Torvalds { 441da177e4SLinus Torvalds while (words--) { 451da177e4SLinus Torvalds __cpu_to_le32s(buf); 461da177e4SLinus Torvalds buf++; 471da177e4SLinus Torvalds } 481da177e4SLinus Torvalds } 491da177e4SLinus Torvalds 507d6f75ebSMax Vozeler static inline void md5_transform_helper(struct md5_state *ctx) 511da177e4SLinus Torvalds { 521da177e4SLinus Torvalds le32_to_cpu_array(ctx->block, sizeof(ctx->block) / sizeof(u32)); 531da177e4SLinus Torvalds md5_transform(ctx->hash, ctx->block); 541da177e4SLinus Torvalds } 551da177e4SLinus Torvalds 5614b75ba7SAdrian-Ken Rueegsegger static int md5_init(struct shash_desc *desc) 571da177e4SLinus Torvalds { 587d6f75ebSMax Vozeler struct md5_state *mctx = shash_desc_ctx(desc); 591da177e4SLinus Torvalds 606a185016SLABBE Corentin mctx->hash[0] = MD5_H0; 616a185016SLABBE Corentin mctx->hash[1] = MD5_H1; 626a185016SLABBE Corentin mctx->hash[2] = MD5_H2; 636a185016SLABBE Corentin mctx->hash[3] = MD5_H3; 641da177e4SLinus Torvalds mctx->byte_count = 0; 6514b75ba7SAdrian-Ken Rueegsegger 6614b75ba7SAdrian-Ken Rueegsegger return 0; 671da177e4SLinus Torvalds } 681da177e4SLinus Torvalds 6914b75ba7SAdrian-Ken Rueegsegger static int md5_update(struct shash_desc *desc, const u8 *data, unsigned int len) 701da177e4SLinus Torvalds { 717d6f75ebSMax Vozeler struct md5_state *mctx = shash_desc_ctx(desc); 721da177e4SLinus Torvalds const u32 avail = sizeof(mctx->block) - (mctx->byte_count & 0x3f); 731da177e4SLinus Torvalds 741da177e4SLinus Torvalds mctx->byte_count += len; 751da177e4SLinus Torvalds 761da177e4SLinus Torvalds if (avail > len) { 771da177e4SLinus Torvalds memcpy((char *)mctx->block + (sizeof(mctx->block) - avail), 781da177e4SLinus Torvalds data, len); 7914b75ba7SAdrian-Ken Rueegsegger return 0; 801da177e4SLinus Torvalds } 811da177e4SLinus Torvalds 821da177e4SLinus Torvalds memcpy((char *)mctx->block + (sizeof(mctx->block) - avail), 831da177e4SLinus Torvalds data, avail); 841da177e4SLinus Torvalds 851da177e4SLinus Torvalds md5_transform_helper(mctx); 861da177e4SLinus Torvalds data += avail; 871da177e4SLinus Torvalds len -= avail; 881da177e4SLinus Torvalds 891da177e4SLinus Torvalds while (len >= sizeof(mctx->block)) { 901da177e4SLinus Torvalds memcpy(mctx->block, data, sizeof(mctx->block)); 911da177e4SLinus Torvalds md5_transform_helper(mctx); 921da177e4SLinus Torvalds data += sizeof(mctx->block); 931da177e4SLinus Torvalds len -= sizeof(mctx->block); 941da177e4SLinus Torvalds } 951da177e4SLinus Torvalds 961da177e4SLinus Torvalds memcpy(mctx->block, data, len); 9714b75ba7SAdrian-Ken Rueegsegger 9814b75ba7SAdrian-Ken Rueegsegger return 0; 991da177e4SLinus Torvalds } 1001da177e4SLinus Torvalds 10114b75ba7SAdrian-Ken Rueegsegger static int md5_final(struct shash_desc *desc, u8 *out) 1021da177e4SLinus Torvalds { 1037d6f75ebSMax Vozeler struct md5_state *mctx = shash_desc_ctx(desc); 1041da177e4SLinus Torvalds const unsigned int offset = mctx->byte_count & 0x3f; 1051da177e4SLinus Torvalds char *p = (char *)mctx->block + offset; 1061da177e4SLinus Torvalds int padding = 56 - (offset + 1); 1071da177e4SLinus Torvalds 1081da177e4SLinus Torvalds *p++ = 0x80; 1091da177e4SLinus Torvalds if (padding < 0) { 1101da177e4SLinus Torvalds memset(p, 0x00, padding + sizeof (u64)); 1111da177e4SLinus Torvalds md5_transform_helper(mctx); 1121da177e4SLinus Torvalds p = (char *)mctx->block; 1131da177e4SLinus Torvalds padding = 56; 1141da177e4SLinus Torvalds } 1151da177e4SLinus Torvalds 1161da177e4SLinus Torvalds memset(p, 0, padding); 1171da177e4SLinus Torvalds mctx->block[14] = mctx->byte_count << 3; 1181da177e4SLinus Torvalds mctx->block[15] = mctx->byte_count >> 29; 1191da177e4SLinus Torvalds le32_to_cpu_array(mctx->block, (sizeof(mctx->block) - 1201da177e4SLinus Torvalds sizeof(u64)) / sizeof(u32)); 1211da177e4SLinus Torvalds md5_transform(mctx->hash, mctx->block); 1221da177e4SLinus Torvalds cpu_to_le32_array(mctx->hash, sizeof(mctx->hash) / sizeof(u32)); 1231da177e4SLinus Torvalds memcpy(out, mctx->hash, sizeof(mctx->hash)); 1241da177e4SLinus Torvalds memset(mctx, 0, sizeof(*mctx)); 12514b75ba7SAdrian-Ken Rueegsegger 12614b75ba7SAdrian-Ken Rueegsegger return 0; 1271da177e4SLinus Torvalds } 1281da177e4SLinus Torvalds 1297d6f75ebSMax Vozeler static int md5_export(struct shash_desc *desc, void *out) 1307d6f75ebSMax Vozeler { 1317d6f75ebSMax Vozeler struct md5_state *ctx = shash_desc_ctx(desc); 1327d6f75ebSMax Vozeler 1337d6f75ebSMax Vozeler memcpy(out, ctx, sizeof(*ctx)); 1347d6f75ebSMax Vozeler return 0; 1357d6f75ebSMax Vozeler } 1367d6f75ebSMax Vozeler 1377d6f75ebSMax Vozeler static int md5_import(struct shash_desc *desc, const void *in) 1387d6f75ebSMax Vozeler { 1397d6f75ebSMax Vozeler struct md5_state *ctx = shash_desc_ctx(desc); 1407d6f75ebSMax Vozeler 1417d6f75ebSMax Vozeler memcpy(ctx, in, sizeof(*ctx)); 1427d6f75ebSMax Vozeler return 0; 1437d6f75ebSMax Vozeler } 1447d6f75ebSMax Vozeler 14514b75ba7SAdrian-Ken Rueegsegger static struct shash_alg alg = { 14614b75ba7SAdrian-Ken Rueegsegger .digestsize = MD5_DIGEST_SIZE, 14714b75ba7SAdrian-Ken Rueegsegger .init = md5_init, 14814b75ba7SAdrian-Ken Rueegsegger .update = md5_update, 14914b75ba7SAdrian-Ken Rueegsegger .final = md5_final, 1507d6f75ebSMax Vozeler .export = md5_export, 1517d6f75ebSMax Vozeler .import = md5_import, 1527d6f75ebSMax Vozeler .descsize = sizeof(struct md5_state), 153eebb111fSHerbert Xu .statesize = sizeof(struct md5_state), 15414b75ba7SAdrian-Ken Rueegsegger .base = { 1551da177e4SLinus Torvalds .cra_name = "md5", 15614b75ba7SAdrian-Ken Rueegsegger .cra_flags = CRYPTO_ALG_TYPE_SHASH, 1571da177e4SLinus Torvalds .cra_blocksize = MD5_HMAC_BLOCK_SIZE, 1581da177e4SLinus Torvalds .cra_module = THIS_MODULE, 15914b75ba7SAdrian-Ken Rueegsegger } 1601da177e4SLinus Torvalds }; 1611da177e4SLinus Torvalds 1623af5b90bSKamalesh Babulal static int __init md5_mod_init(void) 1631da177e4SLinus Torvalds { 16414b75ba7SAdrian-Ken Rueegsegger return crypto_register_shash(&alg); 1651da177e4SLinus Torvalds } 1661da177e4SLinus Torvalds 1673af5b90bSKamalesh Babulal static void __exit md5_mod_fini(void) 1681da177e4SLinus Torvalds { 16914b75ba7SAdrian-Ken Rueegsegger crypto_unregister_shash(&alg); 1701da177e4SLinus Torvalds } 1711da177e4SLinus Torvalds 1723af5b90bSKamalesh Babulal module_init(md5_mod_init); 1733af5b90bSKamalesh Babulal module_exit(md5_mod_fini); 1741da177e4SLinus Torvalds 1751da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 1761da177e4SLinus Torvalds MODULE_DESCRIPTION("MD5 Message Digest Algorithm"); 1775d26a105SKees Cook MODULE_ALIAS_CRYPTO("md5"); 178