Back to home page

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 <linux/cryptohash.h>
0025 #include <asm/byteorder.h>
0026 
0027 const u8 md5_zero_message_hash[MD5_DIGEST_SIZE] = {
0028     0xd4, 0x1d, 0x8c, 0xd9, 0x8f, 0x00, 0xb2, 0x04,
0029     0xe9, 0x80, 0x09, 0x98, 0xec, 0xf8, 0x42, 0x7e,
0030 };
0031 EXPORT_SYMBOL_GPL(md5_zero_message_hash);
0032 
0033 /* XXX: this stuff can be optimized */
0034 static inline void le32_to_cpu_array(u32 *buf, unsigned int words)
0035 {
0036     while (words--) {
0037         __le32_to_cpus(buf);
0038         buf++;
0039     }
0040 }
0041 
0042 static inline void cpu_to_le32_array(u32 *buf, unsigned int words)
0043 {
0044     while (words--) {
0045         __cpu_to_le32s(buf);
0046         buf++;
0047     }
0048 }
0049 
0050 static inline void md5_transform_helper(struct md5_state *ctx)
0051 {
0052     le32_to_cpu_array(ctx->block, sizeof(ctx->block) / sizeof(u32));
0053     md5_transform(ctx->hash, ctx->block);
0054 }
0055 
0056 static int md5_init(struct shash_desc *desc)
0057 {
0058     struct md5_state *mctx = shash_desc_ctx(desc);
0059 
0060     mctx->hash[0] = MD5_H0;
0061     mctx->hash[1] = MD5_H1;
0062     mctx->hash[2] = MD5_H2;
0063     mctx->hash[3] = MD5_H3;
0064     mctx->byte_count = 0;
0065 
0066     return 0;
0067 }
0068 
0069 static int md5_update(struct shash_desc *desc, const u8 *data, unsigned int len)
0070 {
0071     struct md5_state *mctx = shash_desc_ctx(desc);
0072     const u32 avail = sizeof(mctx->block) - (mctx->byte_count & 0x3f);
0073 
0074     mctx->byte_count += len;
0075 
0076     if (avail > len) {
0077         memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
0078                data, len);
0079         return 0;
0080     }
0081 
0082     memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
0083            data, avail);
0084 
0085     md5_transform_helper(mctx);
0086     data += avail;
0087     len -= avail;
0088 
0089     while (len >= sizeof(mctx->block)) {
0090         memcpy(mctx->block, data, sizeof(mctx->block));
0091         md5_transform_helper(mctx);
0092         data += sizeof(mctx->block);
0093         len -= sizeof(mctx->block);
0094     }
0095 
0096     memcpy(mctx->block, data, len);
0097 
0098     return 0;
0099 }
0100 
0101 static int md5_final(struct shash_desc *desc, u8 *out)
0102 {
0103     struct md5_state *mctx = shash_desc_ctx(desc);
0104     const unsigned int offset = mctx->byte_count & 0x3f;
0105     char *p = (char *)mctx->block + offset;
0106     int padding = 56 - (offset + 1);
0107 
0108     *p++ = 0x80;
0109     if (padding < 0) {
0110         memset(p, 0x00, padding + sizeof (u64));
0111         md5_transform_helper(mctx);
0112         p = (char *)mctx->block;
0113         padding = 56;
0114     }
0115 
0116     memset(p, 0, padding);
0117     mctx->block[14] = mctx->byte_count << 3;
0118     mctx->block[15] = mctx->byte_count >> 29;
0119     le32_to_cpu_array(mctx->block, (sizeof(mctx->block) -
0120                       sizeof(u64)) / sizeof(u32));
0121     md5_transform(mctx->hash, mctx->block);
0122     cpu_to_le32_array(mctx->hash, sizeof(mctx->hash) / sizeof(u32));
0123     memcpy(out, mctx->hash, sizeof(mctx->hash));
0124     memset(mctx, 0, sizeof(*mctx));
0125 
0126     return 0;
0127 }
0128 
0129 static int md5_export(struct shash_desc *desc, void *out)
0130 {
0131     struct md5_state *ctx = shash_desc_ctx(desc);
0132 
0133     memcpy(out, ctx, sizeof(*ctx));
0134     return 0;
0135 }
0136 
0137 static int md5_import(struct shash_desc *desc, const void *in)
0138 {
0139     struct md5_state *ctx = shash_desc_ctx(desc);
0140 
0141     memcpy(ctx, in, sizeof(*ctx));
0142     return 0;
0143 }
0144 
0145 static struct shash_alg alg = {
0146     .digestsize =   MD5_DIGEST_SIZE,
0147     .init       =   md5_init,
0148     .update     =   md5_update,
0149     .final      =   md5_final,
0150     .export     =   md5_export,
0151     .import     =   md5_import,
0152     .descsize   =   sizeof(struct md5_state),
0153     .statesize  =   sizeof(struct md5_state),
0154     .base       =   {
0155         .cra_name   =   "md5",
0156         .cra_flags  =   CRYPTO_ALG_TYPE_SHASH,
0157         .cra_blocksize  =   MD5_HMAC_BLOCK_SIZE,
0158         .cra_module =   THIS_MODULE,
0159     }
0160 };
0161 
0162 static int __init md5_mod_init(void)
0163 {
0164     return crypto_register_shash(&alg);
0165 }
0166 
0167 static void __exit md5_mod_fini(void)
0168 {
0169     crypto_unregister_shash(&alg);
0170 }
0171 
0172 module_init(md5_mod_init);
0173 module_exit(md5_mod_fini);
0174 
0175 MODULE_LICENSE("GPL");
0176 MODULE_DESCRIPTION("MD5 Message Digest Algorithm");
0177 MODULE_ALIAS_CRYPTO("md5");