Root/crypto/md4.c

1/*
2 * Cryptographic API.
3 *
4 * MD4 Message Digest Algorithm (RFC1320).
5 *
6 * Implementation derived from Andrew Tridgell and Steve French's
7 * CIFS MD4 implementation, and the cryptoapi implementation
8 * originally based on the public domain implementation written
9 * by Colin Plumb in 1993.
10 *
11 * Copyright (c) Andrew Tridgell 1997-1998.
12 * Modified by Steve French (sfrench@us.ibm.com) 2002
13 * Copyright (c) Cryptoapi developers.
14 * Copyright (c) 2002 David S. Miller (davem@redhat.com)
15 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
16 *
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
21 *
22 */
23#include <crypto/internal/hash.h>
24#include <linux/init.h>
25#include <linux/kernel.h>
26#include <linux/string.h>
27#include <linux/types.h>
28#include <asm/byteorder.h>
29
30#define MD4_DIGEST_SIZE 16
31#define MD4_HMAC_BLOCK_SIZE 64
32#define MD4_BLOCK_WORDS 16
33#define MD4_HASH_WORDS 4
34
35struct md4_ctx {
36    u32 hash[MD4_HASH_WORDS];
37    u32 block[MD4_BLOCK_WORDS];
38    u64 byte_count;
39};
40
41static inline u32 lshift(u32 x, unsigned int s)
42{
43    x &= 0xFFFFFFFF;
44    return ((x << s) & 0xFFFFFFFF) | (x >> (32 - s));
45}
46
47static inline u32 F(u32 x, u32 y, u32 z)
48{
49    return (x & y) | ((~x) & z);
50}
51
52static inline u32 G(u32 x, u32 y, u32 z)
53{
54    return (x & y) | (x & z) | (y & z);
55}
56
57static inline u32 H(u32 x, u32 y, u32 z)
58{
59    return x ^ y ^ z;
60}
61
62#define ROUND1(a,b,c,d,k,s) (a = lshift(a + F(b,c,d) + k, s))
63#define ROUND2(a,b,c,d,k,s) (a = lshift(a + G(b,c,d) + k + (u32)0x5A827999,s))
64#define ROUND3(a,b,c,d,k,s) (a = lshift(a + H(b,c,d) + k + (u32)0x6ED9EBA1,s))
65
66/* XXX: this stuff can be optimized */
67static inline void le32_to_cpu_array(u32 *buf, unsigned int words)
68{
69    while (words--) {
70        __le32_to_cpus(buf);
71        buf++;
72    }
73}
74
75static inline void cpu_to_le32_array(u32 *buf, unsigned int words)
76{
77    while (words--) {
78        __cpu_to_le32s(buf);
79        buf++;
80    }
81}
82
83static void md4_transform(u32 *hash, u32 const *in)
84{
85    u32 a, b, c, d;
86
87    a = hash[0];
88    b = hash[1];
89    c = hash[2];
90    d = hash[3];
91
92    ROUND1(a, b, c, d, in[0], 3);
93    ROUND1(d, a, b, c, in[1], 7);
94    ROUND1(c, d, a, b, in[2], 11);
95    ROUND1(b, c, d, a, in[3], 19);
96    ROUND1(a, b, c, d, in[4], 3);
97    ROUND1(d, a, b, c, in[5], 7);
98    ROUND1(c, d, a, b, in[6], 11);
99    ROUND1(b, c, d, a, in[7], 19);
100    ROUND1(a, b, c, d, in[8], 3);
101    ROUND1(d, a, b, c, in[9], 7);
102    ROUND1(c, d, a, b, in[10], 11);
103    ROUND1(b, c, d, a, in[11], 19);
104    ROUND1(a, b, c, d, in[12], 3);
105    ROUND1(d, a, b, c, in[13], 7);
106    ROUND1(c, d, a, b, in[14], 11);
107    ROUND1(b, c, d, a, in[15], 19);
108
109    ROUND2(a, b, c, d,in[ 0], 3);
110    ROUND2(d, a, b, c, in[4], 5);
111    ROUND2(c, d, a, b, in[8], 9);
112    ROUND2(b, c, d, a, in[12], 13);
113    ROUND2(a, b, c, d, in[1], 3);
114    ROUND2(d, a, b, c, in[5], 5);
115    ROUND2(c, d, a, b, in[9], 9);
116    ROUND2(b, c, d, a, in[13], 13);
117    ROUND2(a, b, c, d, in[2], 3);
118    ROUND2(d, a, b, c, in[6], 5);
119    ROUND2(c, d, a, b, in[10], 9);
120    ROUND2(b, c, d, a, in[14], 13);
121    ROUND2(a, b, c, d, in[3], 3);
122    ROUND2(d, a, b, c, in[7], 5);
123    ROUND2(c, d, a, b, in[11], 9);
124    ROUND2(b, c, d, a, in[15], 13);
125
126    ROUND3(a, b, c, d,in[ 0], 3);
127    ROUND3(d, a, b, c, in[8], 9);
128    ROUND3(c, d, a, b, in[4], 11);
129    ROUND3(b, c, d, a, in[12], 15);
130    ROUND3(a, b, c, d, in[2], 3);
131    ROUND3(d, a, b, c, in[10], 9);
132    ROUND3(c, d, a, b, in[6], 11);
133    ROUND3(b, c, d, a, in[14], 15);
134    ROUND3(a, b, c, d, in[1], 3);
135    ROUND3(d, a, b, c, in[9], 9);
136    ROUND3(c, d, a, b, in[5], 11);
137    ROUND3(b, c, d, a, in[13], 15);
138    ROUND3(a, b, c, d, in[3], 3);
139    ROUND3(d, a, b, c, in[11], 9);
140    ROUND3(c, d, a, b, in[7], 11);
141    ROUND3(b, c, d, a, in[15], 15);
142
143    hash[0] += a;
144    hash[1] += b;
145    hash[2] += c;
146    hash[3] += d;
147}
148
149static inline void md4_transform_helper(struct md4_ctx *ctx)
150{
151    le32_to_cpu_array(ctx->block, ARRAY_SIZE(ctx->block));
152    md4_transform(ctx->hash, ctx->block);
153}
154
155static int md4_init(struct shash_desc *desc)
156{
157    struct md4_ctx *mctx = shash_desc_ctx(desc);
158
159    mctx->hash[0] = 0x67452301;
160    mctx->hash[1] = 0xefcdab89;
161    mctx->hash[2] = 0x98badcfe;
162    mctx->hash[3] = 0x10325476;
163    mctx->byte_count = 0;
164
165    return 0;
166}
167
168static int md4_update(struct shash_desc *desc, const u8 *data, unsigned int len)
169{
170    struct md4_ctx *mctx = shash_desc_ctx(desc);
171    const u32 avail = sizeof(mctx->block) - (mctx->byte_count & 0x3f);
172
173    mctx->byte_count += len;
174
175    if (avail > len) {
176        memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
177               data, len);
178        return 0;
179    }
180
181    memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
182           data, avail);
183
184    md4_transform_helper(mctx);
185    data += avail;
186    len -= avail;
187
188    while (len >= sizeof(mctx->block)) {
189        memcpy(mctx->block, data, sizeof(mctx->block));
190        md4_transform_helper(mctx);
191        data += sizeof(mctx->block);
192        len -= sizeof(mctx->block);
193    }
194
195    memcpy(mctx->block, data, len);
196
197    return 0;
198}
199
200static int md4_final(struct shash_desc *desc, u8 *out)
201{
202    struct md4_ctx *mctx = shash_desc_ctx(desc);
203    const unsigned int offset = mctx->byte_count & 0x3f;
204    char *p = (char *)mctx->block + offset;
205    int padding = 56 - (offset + 1);
206
207    *p++ = 0x80;
208    if (padding < 0) {
209        memset(p, 0x00, padding + sizeof (u64));
210        md4_transform_helper(mctx);
211        p = (char *)mctx->block;
212        padding = 56;
213    }
214
215    memset(p, 0, padding);
216    mctx->block[14] = mctx->byte_count << 3;
217    mctx->block[15] = mctx->byte_count >> 29;
218    le32_to_cpu_array(mctx->block, (sizeof(mctx->block) -
219                      sizeof(u64)) / sizeof(u32));
220    md4_transform(mctx->hash, mctx->block);
221    cpu_to_le32_array(mctx->hash, ARRAY_SIZE(mctx->hash));
222    memcpy(out, mctx->hash, sizeof(mctx->hash));
223    memset(mctx, 0, sizeof(*mctx));
224
225    return 0;
226}
227
228static struct shash_alg alg = {
229    .digestsize = MD4_DIGEST_SIZE,
230    .init = md4_init,
231    .update = md4_update,
232    .final = md4_final,
233    .descsize = sizeof(struct md4_ctx),
234    .base = {
235        .cra_name = "md4",
236        .cra_flags = CRYPTO_ALG_TYPE_SHASH,
237        .cra_blocksize = MD4_HMAC_BLOCK_SIZE,
238        .cra_module = THIS_MODULE,
239    }
240};
241
242static int __init md4_mod_init(void)
243{
244    return crypto_register_shash(&alg);
245}
246
247static void __exit md4_mod_fini(void)
248{
249    crypto_unregister_shash(&alg);
250}
251
252module_init(md4_mod_init);
253module_exit(md4_mod_fini);
254
255MODULE_LICENSE("GPL");
256MODULE_DESCRIPTION("MD4 Message Digest Algorithm");
257
258

Archive Download this file



interactive