Back to home page

OSCL-LXR

 
 

    


0001 /* 
0002  * Cryptographic API.
0003  *
0004  * MD4 Message Digest Algorithm (RFC1320).
0005  *
0006  * Implementation derived from Andrew Tridgell and Steve French's
0007  * CIFS MD4 implementation, and the cryptoapi implementation
0008  * originally based on the public domain implementation written
0009  * by Colin Plumb in 1993.
0010  *
0011  * Copyright (c) Andrew Tridgell 1997-1998.
0012  * Modified by Steve French (sfrench@us.ibm.com) 2002
0013  * Copyright (c) Cryptoapi developers.
0014  * Copyright (c) 2002 David S. Miller (davem@redhat.com)
0015  * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
0016  *
0017  * This program is free software; you can redistribute it and/or modify
0018  * it under the terms of the GNU General Public License as published by
0019  * the Free Software Foundation; either version 2 of the License, or
0020  * (at your option) any later version.
0021  *
0022  */
0023 #include <crypto/internal/hash.h>
0024 #include <linux/init.h>
0025 #include <linux/kernel.h>
0026 #include <linux/module.h>
0027 #include <linux/string.h>
0028 #include <linux/types.h>
0029 #include <asm/byteorder.h>
0030 
0031 #define MD4_DIGEST_SIZE     16
0032 #define MD4_HMAC_BLOCK_SIZE 64
0033 #define MD4_BLOCK_WORDS     16
0034 #define MD4_HASH_WORDS      4
0035 
0036 struct md4_ctx {
0037     u32 hash[MD4_HASH_WORDS];
0038     u32 block[MD4_BLOCK_WORDS];
0039     u64 byte_count;
0040 };
0041 
0042 static inline u32 lshift(u32 x, unsigned int s)
0043 {
0044     x &= 0xFFFFFFFF;
0045     return ((x << s) & 0xFFFFFFFF) | (x >> (32 - s));
0046 }
0047 
0048 static inline u32 F(u32 x, u32 y, u32 z)
0049 {
0050     return (x & y) | ((~x) & z);
0051 }
0052 
0053 static inline u32 G(u32 x, u32 y, u32 z)
0054 {
0055     return (x & y) | (x & z) | (y & z);
0056 }
0057 
0058 static inline u32 H(u32 x, u32 y, u32 z)
0059 {
0060     return x ^ y ^ z;
0061 }
0062 
0063 #define ROUND1(a,b,c,d,k,s) (a = lshift(a + F(b,c,d) + k, s))
0064 #define ROUND2(a,b,c,d,k,s) (a = lshift(a + G(b,c,d) + k + (u32)0x5A827999,s))
0065 #define ROUND3(a,b,c,d,k,s) (a = lshift(a + H(b,c,d) + k + (u32)0x6ED9EBA1,s))
0066 
0067 static void md4_transform(u32 *hash, u32 const *in)
0068 {
0069     u32 a, b, c, d;
0070 
0071     a = hash[0];
0072     b = hash[1];
0073     c = hash[2];
0074     d = hash[3];
0075 
0076     ROUND1(a, b, c, d, in[0], 3);
0077     ROUND1(d, a, b, c, in[1], 7);
0078     ROUND1(c, d, a, b, in[2], 11);
0079     ROUND1(b, c, d, a, in[3], 19);
0080     ROUND1(a, b, c, d, in[4], 3);
0081     ROUND1(d, a, b, c, in[5], 7);
0082     ROUND1(c, d, a, b, in[6], 11);
0083     ROUND1(b, c, d, a, in[7], 19);
0084     ROUND1(a, b, c, d, in[8], 3);
0085     ROUND1(d, a, b, c, in[9], 7);
0086     ROUND1(c, d, a, b, in[10], 11);
0087     ROUND1(b, c, d, a, in[11], 19);
0088     ROUND1(a, b, c, d, in[12], 3);
0089     ROUND1(d, a, b, c, in[13], 7);
0090     ROUND1(c, d, a, b, in[14], 11);
0091     ROUND1(b, c, d, a, in[15], 19);
0092 
0093     ROUND2(a, b, c, d,in[ 0], 3);
0094     ROUND2(d, a, b, c, in[4], 5);
0095     ROUND2(c, d, a, b, in[8], 9);
0096     ROUND2(b, c, d, a, in[12], 13);
0097     ROUND2(a, b, c, d, in[1], 3);
0098     ROUND2(d, a, b, c, in[5], 5);
0099     ROUND2(c, d, a, b, in[9], 9);
0100     ROUND2(b, c, d, a, in[13], 13);
0101     ROUND2(a, b, c, d, in[2], 3);
0102     ROUND2(d, a, b, c, in[6], 5);
0103     ROUND2(c, d, a, b, in[10], 9);
0104     ROUND2(b, c, d, a, in[14], 13);
0105     ROUND2(a, b, c, d, in[3], 3);
0106     ROUND2(d, a, b, c, in[7], 5);
0107     ROUND2(c, d, a, b, in[11], 9);
0108     ROUND2(b, c, d, a, in[15], 13);
0109 
0110     ROUND3(a, b, c, d,in[ 0], 3);
0111     ROUND3(d, a, b, c, in[8], 9);
0112     ROUND3(c, d, a, b, in[4], 11);
0113     ROUND3(b, c, d, a, in[12], 15);
0114     ROUND3(a, b, c, d, in[2], 3);
0115     ROUND3(d, a, b, c, in[10], 9);
0116     ROUND3(c, d, a, b, in[6], 11);
0117     ROUND3(b, c, d, a, in[14], 15);
0118     ROUND3(a, b, c, d, in[1], 3);
0119     ROUND3(d, a, b, c, in[9], 9);
0120     ROUND3(c, d, a, b, in[5], 11);
0121     ROUND3(b, c, d, a, in[13], 15);
0122     ROUND3(a, b, c, d, in[3], 3);
0123     ROUND3(d, a, b, c, in[11], 9);
0124     ROUND3(c, d, a, b, in[7], 11);
0125     ROUND3(b, c, d, a, in[15], 15);
0126 
0127     hash[0] += a;
0128     hash[1] += b;
0129     hash[2] += c;
0130     hash[3] += d;
0131 }
0132 
0133 static inline void md4_transform_helper(struct md4_ctx *ctx)
0134 {
0135     le32_to_cpu_array(ctx->block, ARRAY_SIZE(ctx->block));
0136     md4_transform(ctx->hash, ctx->block);
0137 }
0138 
0139 static int md4_init(struct shash_desc *desc)
0140 {
0141     struct md4_ctx *mctx = shash_desc_ctx(desc);
0142 
0143     mctx->hash[0] = 0x67452301;
0144     mctx->hash[1] = 0xefcdab89;
0145     mctx->hash[2] = 0x98badcfe;
0146     mctx->hash[3] = 0x10325476;
0147     mctx->byte_count = 0;
0148 
0149     return 0;
0150 }
0151 
0152 static int md4_update(struct shash_desc *desc, const u8 *data, unsigned int len)
0153 {
0154     struct md4_ctx *mctx = shash_desc_ctx(desc);
0155     const u32 avail = sizeof(mctx->block) - (mctx->byte_count & 0x3f);
0156 
0157     mctx->byte_count += len;
0158 
0159     if (avail > len) {
0160         memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
0161                data, len);
0162         return 0;
0163     }
0164 
0165     memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
0166            data, avail);
0167 
0168     md4_transform_helper(mctx);
0169     data += avail;
0170     len -= avail;
0171 
0172     while (len >= sizeof(mctx->block)) {
0173         memcpy(mctx->block, data, sizeof(mctx->block));
0174         md4_transform_helper(mctx);
0175         data += sizeof(mctx->block);
0176         len -= sizeof(mctx->block);
0177     }
0178 
0179     memcpy(mctx->block, data, len);
0180 
0181     return 0;
0182 }
0183 
0184 static int md4_final(struct shash_desc *desc, u8 *out)
0185 {
0186     struct md4_ctx *mctx = shash_desc_ctx(desc);
0187     const unsigned int offset = mctx->byte_count & 0x3f;
0188     char *p = (char *)mctx->block + offset;
0189     int padding = 56 - (offset + 1);
0190 
0191     *p++ = 0x80;
0192     if (padding < 0) {
0193         memset(p, 0x00, padding + sizeof (u64));
0194         md4_transform_helper(mctx);
0195         p = (char *)mctx->block;
0196         padding = 56;
0197     }
0198 
0199     memset(p, 0, padding);
0200     mctx->block[14] = mctx->byte_count << 3;
0201     mctx->block[15] = mctx->byte_count >> 29;
0202     le32_to_cpu_array(mctx->block, (sizeof(mctx->block) -
0203                       sizeof(u64)) / sizeof(u32));
0204     md4_transform(mctx->hash, mctx->block);
0205     cpu_to_le32_array(mctx->hash, ARRAY_SIZE(mctx->hash));
0206     memcpy(out, mctx->hash, sizeof(mctx->hash));
0207     memset(mctx, 0, sizeof(*mctx));
0208 
0209     return 0;
0210 }
0211 
0212 static struct shash_alg alg = {
0213     .digestsize =   MD4_DIGEST_SIZE,
0214     .init       =   md4_init,
0215     .update     =   md4_update,
0216     .final      =   md4_final,
0217     .descsize   =   sizeof(struct md4_ctx),
0218     .base       =   {
0219         .cra_name    =  "md4",
0220         .cra_driver_name =  "md4-generic",
0221         .cra_blocksize   =  MD4_HMAC_BLOCK_SIZE,
0222         .cra_module  =  THIS_MODULE,
0223     }
0224 };
0225 
0226 static int __init md4_mod_init(void)
0227 {
0228     return crypto_register_shash(&alg);
0229 }
0230 
0231 static void __exit md4_mod_fini(void)
0232 {
0233     crypto_unregister_shash(&alg);
0234 }
0235 
0236 subsys_initcall(md4_mod_init);
0237 module_exit(md4_mod_fini);
0238 
0239 MODULE_LICENSE("GPL");
0240 MODULE_DESCRIPTION("MD4 Message Digest Algorithm");
0241 MODULE_ALIAS_CRYPTO("md4");