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