Back to home page

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 /* XXX: this stuff can be optimized */
0068 static inline void le32_to_cpu_array(u32 *buf, unsigned int words)
0069 {
0070     while (words--) {
0071         __le32_to_cpus(buf);
0072         buf++;
0073     }
0074 }
0075 
0076 static inline void cpu_to_le32_array(u32 *buf, unsigned int words)
0077 {
0078     while (words--) {
0079         __cpu_to_le32s(buf);
0080         buf++;
0081     }
0082 }
0083 
0084 static void md4_transform(u32 *hash, u32 const *in)
0085 {
0086     u32 a, b, c, d;
0087 
0088     a = hash[0];
0089     b = hash[1];
0090     c = hash[2];
0091     d = hash[3];
0092 
0093     ROUND1(a, b, c, d, in[0], 3);
0094     ROUND1(d, a, b, c, in[1], 7);
0095     ROUND1(c, d, a, b, in[2], 11);
0096     ROUND1(b, c, d, a, in[3], 19);
0097     ROUND1(a, b, c, d, in[4], 3);
0098     ROUND1(d, a, b, c, in[5], 7);
0099     ROUND1(c, d, a, b, in[6], 11);
0100     ROUND1(b, c, d, a, in[7], 19);
0101     ROUND1(a, b, c, d, in[8], 3);
0102     ROUND1(d, a, b, c, in[9], 7);
0103     ROUND1(c, d, a, b, in[10], 11);
0104     ROUND1(b, c, d, a, in[11], 19);
0105     ROUND1(a, b, c, d, in[12], 3);
0106     ROUND1(d, a, b, c, in[13], 7);
0107     ROUND1(c, d, a, b, in[14], 11);
0108     ROUND1(b, c, d, a, in[15], 19);
0109 
0110     ROUND2(a, b, c, d,in[ 0], 3);
0111     ROUND2(d, a, b, c, in[4], 5);
0112     ROUND2(c, d, a, b, in[8], 9);
0113     ROUND2(b, c, d, a, in[12], 13);
0114     ROUND2(a, b, c, d, in[1], 3);
0115     ROUND2(d, a, b, c, in[5], 5);
0116     ROUND2(c, d, a, b, in[9], 9);
0117     ROUND2(b, c, d, a, in[13], 13);
0118     ROUND2(a, b, c, d, in[2], 3);
0119     ROUND2(d, a, b, c, in[6], 5);
0120     ROUND2(c, d, a, b, in[10], 9);
0121     ROUND2(b, c, d, a, in[14], 13);
0122     ROUND2(a, b, c, d, in[3], 3);
0123     ROUND2(d, a, b, c, in[7], 5);
0124     ROUND2(c, d, a, b, in[11], 9);
0125     ROUND2(b, c, d, a, in[15], 13);
0126 
0127     ROUND3(a, b, c, d,in[ 0], 3);
0128     ROUND3(d, a, b, c, in[8], 9);
0129     ROUND3(c, d, a, b, in[4], 11);
0130     ROUND3(b, c, d, a, in[12], 15);
0131     ROUND3(a, b, c, d, in[2], 3);
0132     ROUND3(d, a, b, c, in[10], 9);
0133     ROUND3(c, d, a, b, in[6], 11);
0134     ROUND3(b, c, d, a, in[14], 15);
0135     ROUND3(a, b, c, d, in[1], 3);
0136     ROUND3(d, a, b, c, in[9], 9);
0137     ROUND3(c, d, a, b, in[5], 11);
0138     ROUND3(b, c, d, a, in[13], 15);
0139     ROUND3(a, b, c, d, in[3], 3);
0140     ROUND3(d, a, b, c, in[11], 9);
0141     ROUND3(c, d, a, b, in[7], 11);
0142     ROUND3(b, c, d, a, in[15], 15);
0143 
0144     hash[0] += a;
0145     hash[1] += b;
0146     hash[2] += c;
0147     hash[3] += d;
0148 }
0149 
0150 static inline void md4_transform_helper(struct md4_ctx *ctx)
0151 {
0152     le32_to_cpu_array(ctx->block, ARRAY_SIZE(ctx->block));
0153     md4_transform(ctx->hash, ctx->block);
0154 }
0155 
0156 static int md4_init(struct shash_desc *desc)
0157 {
0158     struct md4_ctx *mctx = shash_desc_ctx(desc);
0159 
0160     mctx->hash[0] = 0x67452301;
0161     mctx->hash[1] = 0xefcdab89;
0162     mctx->hash[2] = 0x98badcfe;
0163     mctx->hash[3] = 0x10325476;
0164     mctx->byte_count = 0;
0165 
0166     return 0;
0167 }
0168 
0169 static int md4_update(struct shash_desc *desc, const u8 *data, unsigned int len)
0170 {
0171     struct md4_ctx *mctx = shash_desc_ctx(desc);
0172     const u32 avail = sizeof(mctx->block) - (mctx->byte_count & 0x3f);
0173 
0174     mctx->byte_count += len;
0175 
0176     if (avail > len) {
0177         memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
0178                data, len);
0179         return 0;
0180     }
0181 
0182     memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
0183            data, avail);
0184 
0185     md4_transform_helper(mctx);
0186     data += avail;
0187     len -= avail;
0188 
0189     while (len >= sizeof(mctx->block)) {
0190         memcpy(mctx->block, data, sizeof(mctx->block));
0191         md4_transform_helper(mctx);
0192         data += sizeof(mctx->block);
0193         len -= sizeof(mctx->block);
0194     }
0195 
0196     memcpy(mctx->block, data, len);
0197 
0198     return 0;
0199 }
0200 
0201 static int md4_final(struct shash_desc *desc, u8 *out)
0202 {
0203     struct md4_ctx *mctx = shash_desc_ctx(desc);
0204     const unsigned int offset = mctx->byte_count & 0x3f;
0205     char *p = (char *)mctx->block + offset;
0206     int padding = 56 - (offset + 1);
0207 
0208     *p++ = 0x80;
0209     if (padding < 0) {
0210         memset(p, 0x00, padding + sizeof (u64));
0211         md4_transform_helper(mctx);
0212         p = (char *)mctx->block;
0213         padding = 56;
0214     }
0215 
0216     memset(p, 0, padding);
0217     mctx->block[14] = mctx->byte_count << 3;
0218     mctx->block[15] = mctx->byte_count >> 29;
0219     le32_to_cpu_array(mctx->block, (sizeof(mctx->block) -
0220                       sizeof(u64)) / sizeof(u32));
0221     md4_transform(mctx->hash, mctx->block);
0222     cpu_to_le32_array(mctx->hash, ARRAY_SIZE(mctx->hash));
0223     memcpy(out, mctx->hash, sizeof(mctx->hash));
0224     memset(mctx, 0, sizeof(*mctx));
0225 
0226     return 0;
0227 }
0228 
0229 static struct shash_alg alg = {
0230     .digestsize =   MD4_DIGEST_SIZE,
0231     .init       =   md4_init,
0232     .update     =   md4_update,
0233     .final      =   md4_final,
0234     .descsize   =   sizeof(struct md4_ctx),
0235     .base       =   {
0236         .cra_name   =   "md4",
0237         .cra_flags  =   CRYPTO_ALG_TYPE_SHASH,
0238         .cra_blocksize  =   MD4_HMAC_BLOCK_SIZE,
0239         .cra_module =   THIS_MODULE,
0240     }
0241 };
0242 
0243 static int __init md4_mod_init(void)
0244 {
0245     return crypto_register_shash(&alg);
0246 }
0247 
0248 static void __exit md4_mod_fini(void)
0249 {
0250     crypto_unregister_shash(&alg);
0251 }
0252 
0253 module_init(md4_mod_init);
0254 module_exit(md4_mod_fini);
0255 
0256 MODULE_LICENSE("GPL");
0257 MODULE_DESCRIPTION("MD4 Message Digest Algorithm");
0258 MODULE_ALIAS_CRYPTO("md4");