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 271da177e4SLinus Torvalds /* XXX: this stuff can be optimized */ 281da177e4SLinus Torvalds static inline void le32_to_cpu_array(u32 *buf, unsigned int words) 291da177e4SLinus Torvalds { 301da177e4SLinus Torvalds while (words--) { 311da177e4SLinus Torvalds __le32_to_cpus(buf); 321da177e4SLinus Torvalds buf++; 331da177e4SLinus Torvalds } 341da177e4SLinus Torvalds } 351da177e4SLinus Torvalds 361da177e4SLinus Torvalds static inline void cpu_to_le32_array(u32 *buf, unsigned int words) 371da177e4SLinus Torvalds { 381da177e4SLinus Torvalds while (words--) { 391da177e4SLinus Torvalds __cpu_to_le32s(buf); 401da177e4SLinus Torvalds buf++; 411da177e4SLinus Torvalds } 421da177e4SLinus Torvalds } 431da177e4SLinus Torvalds 447d6f75ebSMax Vozeler static inline void md5_transform_helper(struct md5_state *ctx) 451da177e4SLinus Torvalds { 461da177e4SLinus Torvalds le32_to_cpu_array(ctx->block, sizeof(ctx->block) / sizeof(u32)); 471da177e4SLinus Torvalds md5_transform(ctx->hash, ctx->block); 481da177e4SLinus Torvalds } 491da177e4SLinus Torvalds 5014b75ba7SAdrian-Ken Rueegsegger static int md5_init(struct shash_desc *desc) 511da177e4SLinus Torvalds { 527d6f75ebSMax Vozeler struct md5_state *mctx = shash_desc_ctx(desc); 531da177e4SLinus Torvalds 541da177e4SLinus Torvalds mctx->hash[0] = 0x67452301; 551da177e4SLinus Torvalds mctx->hash[1] = 0xefcdab89; 561da177e4SLinus Torvalds mctx->hash[2] = 0x98badcfe; 571da177e4SLinus Torvalds mctx->hash[3] = 0x10325476; 581da177e4SLinus Torvalds mctx->byte_count = 0; 5914b75ba7SAdrian-Ken Rueegsegger 6014b75ba7SAdrian-Ken Rueegsegger return 0; 611da177e4SLinus Torvalds } 621da177e4SLinus Torvalds 6314b75ba7SAdrian-Ken Rueegsegger static int md5_update(struct shash_desc *desc, const u8 *data, unsigned int len) 641da177e4SLinus Torvalds { 657d6f75ebSMax Vozeler struct md5_state *mctx = shash_desc_ctx(desc); 661da177e4SLinus Torvalds const u32 avail = sizeof(mctx->block) - (mctx->byte_count & 0x3f); 671da177e4SLinus Torvalds 681da177e4SLinus Torvalds mctx->byte_count += len; 691da177e4SLinus Torvalds 701da177e4SLinus Torvalds if (avail > len) { 711da177e4SLinus Torvalds memcpy((char *)mctx->block + (sizeof(mctx->block) - avail), 721da177e4SLinus Torvalds data, len); 7314b75ba7SAdrian-Ken Rueegsegger return 0; 741da177e4SLinus Torvalds } 751da177e4SLinus Torvalds 761da177e4SLinus Torvalds memcpy((char *)mctx->block + (sizeof(mctx->block) - avail), 771da177e4SLinus Torvalds data, avail); 781da177e4SLinus Torvalds 791da177e4SLinus Torvalds md5_transform_helper(mctx); 801da177e4SLinus Torvalds data += avail; 811da177e4SLinus Torvalds len -= avail; 821da177e4SLinus Torvalds 831da177e4SLinus Torvalds while (len >= sizeof(mctx->block)) { 841da177e4SLinus Torvalds memcpy(mctx->block, data, sizeof(mctx->block)); 851da177e4SLinus Torvalds md5_transform_helper(mctx); 861da177e4SLinus Torvalds data += sizeof(mctx->block); 871da177e4SLinus Torvalds len -= sizeof(mctx->block); 881da177e4SLinus Torvalds } 891da177e4SLinus Torvalds 901da177e4SLinus Torvalds memcpy(mctx->block, data, len); 9114b75ba7SAdrian-Ken Rueegsegger 9214b75ba7SAdrian-Ken Rueegsegger return 0; 931da177e4SLinus Torvalds } 941da177e4SLinus Torvalds 9514b75ba7SAdrian-Ken Rueegsegger static int md5_final(struct shash_desc *desc, u8 *out) 961da177e4SLinus Torvalds { 977d6f75ebSMax Vozeler struct md5_state *mctx = shash_desc_ctx(desc); 981da177e4SLinus Torvalds const unsigned int offset = mctx->byte_count & 0x3f; 991da177e4SLinus Torvalds char *p = (char *)mctx->block + offset; 1001da177e4SLinus Torvalds int padding = 56 - (offset + 1); 1011da177e4SLinus Torvalds 1021da177e4SLinus Torvalds *p++ = 0x80; 1031da177e4SLinus Torvalds if (padding < 0) { 1041da177e4SLinus Torvalds memset(p, 0x00, padding + sizeof (u64)); 1051da177e4SLinus Torvalds md5_transform_helper(mctx); 1061da177e4SLinus Torvalds p = (char *)mctx->block; 1071da177e4SLinus Torvalds padding = 56; 1081da177e4SLinus Torvalds } 1091da177e4SLinus Torvalds 1101da177e4SLinus Torvalds memset(p, 0, padding); 1111da177e4SLinus Torvalds mctx->block[14] = mctx->byte_count << 3; 1121da177e4SLinus Torvalds mctx->block[15] = mctx->byte_count >> 29; 1131da177e4SLinus Torvalds le32_to_cpu_array(mctx->block, (sizeof(mctx->block) - 1141da177e4SLinus Torvalds sizeof(u64)) / sizeof(u32)); 1151da177e4SLinus Torvalds md5_transform(mctx->hash, mctx->block); 1161da177e4SLinus Torvalds cpu_to_le32_array(mctx->hash, sizeof(mctx->hash) / sizeof(u32)); 1171da177e4SLinus Torvalds memcpy(out, mctx->hash, sizeof(mctx->hash)); 1181da177e4SLinus Torvalds memset(mctx, 0, sizeof(*mctx)); 11914b75ba7SAdrian-Ken Rueegsegger 12014b75ba7SAdrian-Ken Rueegsegger return 0; 1211da177e4SLinus Torvalds } 1221da177e4SLinus Torvalds 1237d6f75ebSMax Vozeler static int md5_export(struct shash_desc *desc, void *out) 1247d6f75ebSMax Vozeler { 1257d6f75ebSMax Vozeler struct md5_state *ctx = shash_desc_ctx(desc); 1267d6f75ebSMax Vozeler 1277d6f75ebSMax Vozeler memcpy(out, ctx, sizeof(*ctx)); 1287d6f75ebSMax Vozeler return 0; 1297d6f75ebSMax Vozeler } 1307d6f75ebSMax Vozeler 1317d6f75ebSMax Vozeler static int md5_import(struct shash_desc *desc, const void *in) 1327d6f75ebSMax Vozeler { 1337d6f75ebSMax Vozeler struct md5_state *ctx = shash_desc_ctx(desc); 1347d6f75ebSMax Vozeler 1357d6f75ebSMax Vozeler memcpy(ctx, in, sizeof(*ctx)); 1367d6f75ebSMax Vozeler return 0; 1377d6f75ebSMax Vozeler } 1387d6f75ebSMax Vozeler 13914b75ba7SAdrian-Ken Rueegsegger static struct shash_alg alg = { 14014b75ba7SAdrian-Ken Rueegsegger .digestsize = MD5_DIGEST_SIZE, 14114b75ba7SAdrian-Ken Rueegsegger .init = md5_init, 14214b75ba7SAdrian-Ken Rueegsegger .update = md5_update, 14314b75ba7SAdrian-Ken Rueegsegger .final = md5_final, 1447d6f75ebSMax Vozeler .export = md5_export, 1457d6f75ebSMax Vozeler .import = md5_import, 1467d6f75ebSMax Vozeler .descsize = sizeof(struct md5_state), 147eebb111fSHerbert Xu .statesize = sizeof(struct md5_state), 14814b75ba7SAdrian-Ken Rueegsegger .base = { 1491da177e4SLinus Torvalds .cra_name = "md5", 15014b75ba7SAdrian-Ken Rueegsegger .cra_flags = CRYPTO_ALG_TYPE_SHASH, 1511da177e4SLinus Torvalds .cra_blocksize = MD5_HMAC_BLOCK_SIZE, 1521da177e4SLinus Torvalds .cra_module = THIS_MODULE, 15314b75ba7SAdrian-Ken Rueegsegger } 1541da177e4SLinus Torvalds }; 1551da177e4SLinus Torvalds 1563af5b90bSKamalesh Babulal static int __init md5_mod_init(void) 1571da177e4SLinus Torvalds { 15814b75ba7SAdrian-Ken Rueegsegger return crypto_register_shash(&alg); 1591da177e4SLinus Torvalds } 1601da177e4SLinus Torvalds 1613af5b90bSKamalesh Babulal static void __exit md5_mod_fini(void) 1621da177e4SLinus Torvalds { 16314b75ba7SAdrian-Ken Rueegsegger crypto_unregister_shash(&alg); 1641da177e4SLinus Torvalds } 1651da177e4SLinus Torvalds 1663af5b90bSKamalesh Babulal module_init(md5_mod_init); 1673af5b90bSKamalesh Babulal module_exit(md5_mod_fini); 1681da177e4SLinus Torvalds 1691da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 1701da177e4SLinus Torvalds MODULE_DESCRIPTION("MD5 Message Digest Algorithm"); 171*5d26a105SKees Cook MODULE_ALIAS_CRYPTO("md5"); 172