Back to home page

OSCL-LXR

 
 

    


0001 /* 
0002  * Cryptographic API.
0003  *
0004  * MD5 Message Digest Algorithm (RFC1321).
0005  *
0006  * Derived from cryptoapi implementation, originally based on the
0007  * public domain implementation written by Colin Plumb in 1993.
0008  *
0009  * Copyright (c) Cryptoapi developers.
0010  * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
0011  * 
0012  * This program is free software; you can redistribute it and/or modify it
0013  * under the terms of the GNU General Public License as published by the Free
0014  * Software Foundation; either version 2 of the License, or (at your option) 
0015  * any later version.
0016  *
0017  */
0018 #include <crypto/internal/hash.h>
0019 #include <crypto/md5.h>
0020 #include <linux/init.h>
0021 #include <linux/module.h>
0022 #include <linux/string.h>
0023 #include <linux/types.h>
0024 #include <asm/byteorder.h>
0025 
0026 const u8 md5_zero_message_hash[MD5_DIGEST_SIZE] = {
0027     0xd4, 0x1d, 0x8c, 0xd9, 0x8f, 0x00, 0xb2, 0x04,
0028     0xe9, 0x80, 0x09, 0x98, 0xec, 0xf8, 0x42, 0x7e,
0029 };
0030 EXPORT_SYMBOL_GPL(md5_zero_message_hash);
0031 
0032 #define F1(x, y, z) (z ^ (x & (y ^ z)))
0033 #define F2(x, y, z) F1(z, x, y)
0034 #define F3(x, y, z) (x ^ y ^ z)
0035 #define F4(x, y, z) (y ^ (x | ~z))
0036 
0037 #define MD5STEP(f, w, x, y, z, in, s) \
0038     (w += f(x, y, z) + in, w = (w<<s | w>>(32-s)) + x)
0039 
0040 static void md5_transform(__u32 *hash, __u32 const *in)
0041 {
0042     u32 a, b, c, d;
0043 
0044     a = hash[0];
0045     b = hash[1];
0046     c = hash[2];
0047     d = hash[3];
0048 
0049     MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
0050     MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
0051     MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
0052     MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
0053     MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
0054     MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
0055     MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
0056     MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
0057     MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
0058     MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
0059     MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
0060     MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
0061     MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
0062     MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
0063     MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
0064     MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
0065 
0066     MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
0067     MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
0068     MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
0069     MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
0070     MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
0071     MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
0072     MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
0073     MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
0074     MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
0075     MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
0076     MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
0077     MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
0078     MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
0079     MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
0080     MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
0081     MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
0082 
0083     MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
0084     MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
0085     MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
0086     MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
0087     MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
0088     MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
0089     MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
0090     MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
0091     MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
0092     MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
0093     MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
0094     MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
0095     MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
0096     MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
0097     MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
0098     MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
0099 
0100     MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
0101     MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
0102     MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
0103     MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
0104     MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
0105     MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
0106     MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
0107     MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
0108     MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
0109     MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
0110     MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
0111     MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
0112     MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
0113     MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
0114     MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
0115     MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
0116 
0117     hash[0] += a;
0118     hash[1] += b;
0119     hash[2] += c;
0120     hash[3] += d;
0121 }
0122 
0123 static inline void md5_transform_helper(struct md5_state *ctx)
0124 {
0125     le32_to_cpu_array(ctx->block, sizeof(ctx->block) / sizeof(u32));
0126     md5_transform(ctx->hash, ctx->block);
0127 }
0128 
0129 static int md5_init(struct shash_desc *desc)
0130 {
0131     struct md5_state *mctx = shash_desc_ctx(desc);
0132 
0133     mctx->hash[0] = MD5_H0;
0134     mctx->hash[1] = MD5_H1;
0135     mctx->hash[2] = MD5_H2;
0136     mctx->hash[3] = MD5_H3;
0137     mctx->byte_count = 0;
0138 
0139     return 0;
0140 }
0141 
0142 static int md5_update(struct shash_desc *desc, const u8 *data, unsigned int len)
0143 {
0144     struct md5_state *mctx = shash_desc_ctx(desc);
0145     const u32 avail = sizeof(mctx->block) - (mctx->byte_count & 0x3f);
0146 
0147     mctx->byte_count += len;
0148 
0149     if (avail > len) {
0150         memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
0151                data, len);
0152         return 0;
0153     }
0154 
0155     memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
0156            data, avail);
0157 
0158     md5_transform_helper(mctx);
0159     data += avail;
0160     len -= avail;
0161 
0162     while (len >= sizeof(mctx->block)) {
0163         memcpy(mctx->block, data, sizeof(mctx->block));
0164         md5_transform_helper(mctx);
0165         data += sizeof(mctx->block);
0166         len -= sizeof(mctx->block);
0167     }
0168 
0169     memcpy(mctx->block, data, len);
0170 
0171     return 0;
0172 }
0173 
0174 static int md5_final(struct shash_desc *desc, u8 *out)
0175 {
0176     struct md5_state *mctx = shash_desc_ctx(desc);
0177     const unsigned int offset = mctx->byte_count & 0x3f;
0178     char *p = (char *)mctx->block + offset;
0179     int padding = 56 - (offset + 1);
0180 
0181     *p++ = 0x80;
0182     if (padding < 0) {
0183         memset(p, 0x00, padding + sizeof (u64));
0184         md5_transform_helper(mctx);
0185         p = (char *)mctx->block;
0186         padding = 56;
0187     }
0188 
0189     memset(p, 0, padding);
0190     mctx->block[14] = mctx->byte_count << 3;
0191     mctx->block[15] = mctx->byte_count >> 29;
0192     le32_to_cpu_array(mctx->block, (sizeof(mctx->block) -
0193                       sizeof(u64)) / sizeof(u32));
0194     md5_transform(mctx->hash, mctx->block);
0195     cpu_to_le32_array(mctx->hash, sizeof(mctx->hash) / sizeof(u32));
0196     memcpy(out, mctx->hash, sizeof(mctx->hash));
0197     memset(mctx, 0, sizeof(*mctx));
0198 
0199     return 0;
0200 }
0201 
0202 static int md5_export(struct shash_desc *desc, void *out)
0203 {
0204     struct md5_state *ctx = shash_desc_ctx(desc);
0205 
0206     memcpy(out, ctx, sizeof(*ctx));
0207     return 0;
0208 }
0209 
0210 static int md5_import(struct shash_desc *desc, const void *in)
0211 {
0212     struct md5_state *ctx = shash_desc_ctx(desc);
0213 
0214     memcpy(ctx, in, sizeof(*ctx));
0215     return 0;
0216 }
0217 
0218 static struct shash_alg alg = {
0219     .digestsize =   MD5_DIGEST_SIZE,
0220     .init       =   md5_init,
0221     .update     =   md5_update,
0222     .final      =   md5_final,
0223     .export     =   md5_export,
0224     .import     =   md5_import,
0225     .descsize   =   sizeof(struct md5_state),
0226     .statesize  =   sizeof(struct md5_state),
0227     .base       =   {
0228         .cra_name    =  "md5",
0229         .cra_driver_name =  "md5-generic",
0230         .cra_blocksize   =  MD5_HMAC_BLOCK_SIZE,
0231         .cra_module  =  THIS_MODULE,
0232     }
0233 };
0234 
0235 static int __init md5_mod_init(void)
0236 {
0237     return crypto_register_shash(&alg);
0238 }
0239 
0240 static void __exit md5_mod_fini(void)
0241 {
0242     crypto_unregister_shash(&alg);
0243 }
0244 
0245 subsys_initcall(md5_mod_init);
0246 module_exit(md5_mod_fini);
0247 
0248 MODULE_LICENSE("GPL");
0249 MODULE_DESCRIPTION("MD5 Message Digest Algorithm");
0250 MODULE_ALIAS_CRYPTO("md5");