md4.c (31a61bfc6e415fbd871317cbee7b8a4158d8ac5b) md4.c (808a1763cef93bf0f740d7e10dd9a2dfc4065b1a)
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

--- 6 unchanged lines hidden (view full) ---

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 */
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

--- 6 unchanged lines hidden (view full) ---

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>
23#include <linux/init.h>
24#include <linux/init.h>
24#include <linux/crypto.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

--- 20 unchanged lines hidden (view full) ---

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}
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

--- 20 unchanged lines hidden (view full) ---

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
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--) {

--- 77 unchanged lines hidden (view full) ---

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
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--) {

--- 77 unchanged lines hidden (view full) ---

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 void md4_init(struct crypto_tfm *tfm)
155static int md4_init(struct shash_desc *desc)
156{
156{
157 struct md4_ctx *mctx = crypto_tfm_ctx(tfm);
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;
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;
164}
165
166}
167
166static void md4_update(struct crypto_tfm *tfm, const u8 *data, unsigned int len)
168static int md4_update(struct shash_desc *desc, const u8 *data, unsigned int len)
167{
169{
168 struct md4_ctx *mctx = crypto_tfm_ctx(tfm);
170 struct md4_ctx *mctx = shash_desc_ctx(desc);
169 const u32 avail = sizeof(mctx->block) - (mctx->byte_count & 0x3f);
170
171 mctx->byte_count += len;
172
173 if (avail > len) {
174 memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
175 data, len);
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);
176 return;
178 return 0;
177 }
178
179 memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
180 data, avail);
181
182 md4_transform_helper(mctx);
183 data += avail;
184 len -= avail;
185
186 while (len >= sizeof(mctx->block)) {
187 memcpy(mctx->block, data, sizeof(mctx->block));
188 md4_transform_helper(mctx);
189 data += sizeof(mctx->block);
190 len -= sizeof(mctx->block);
191 }
192
193 memcpy(mctx->block, data, len);
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;
194}
195
198}
199
196static void md4_final(struct crypto_tfm *tfm, u8 *out)
200static int md4_final(struct shash_desc *desc, u8 *out)
197{
201{
198 struct md4_ctx *mctx = crypto_tfm_ctx(tfm);
202 struct md4_ctx *mctx = shash_desc_ctx(desc);
199 const unsigned int offset = mctx->byte_count & 0x3f;
200 char *p = (char *)mctx->block + offset;
201 int padding = 56 - (offset + 1);
202
203 *p++ = 0x80;
204 if (padding < 0) {
205 memset(p, 0x00, padding + sizeof (u64));
206 md4_transform_helper(mctx);

--- 5 unchanged lines hidden (view full) ---

212 mctx->block[14] = mctx->byte_count << 3;
213 mctx->block[15] = mctx->byte_count >> 29;
214 le32_to_cpu_array(mctx->block, (sizeof(mctx->block) -
215 sizeof(u64)) / sizeof(u32));
216 md4_transform(mctx->hash, mctx->block);
217 cpu_to_le32_array(mctx->hash, ARRAY_SIZE(mctx->hash));
218 memcpy(out, mctx->hash, sizeof(mctx->hash));
219 memset(mctx, 0, sizeof(*mctx));
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);

--- 5 unchanged lines hidden (view full) ---

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;
220}
221
226}
227
222static struct crypto_alg alg = {
223 .cra_name = "md4",
224 .cra_flags = CRYPTO_ALG_TYPE_DIGEST,
225 .cra_blocksize = MD4_HMAC_BLOCK_SIZE,
226 .cra_ctxsize = sizeof(struct md4_ctx),
227 .cra_module = THIS_MODULE,
228 .cra_list = LIST_HEAD_INIT(alg.cra_list),
229 .cra_u = { .digest = {
230 .dia_digestsize = MD4_DIGEST_SIZE,
231 .dia_init = md4_init,
232 .dia_update = md4_update,
233 .dia_final = md4_final } }
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 }
234};
235
236static int __init md4_mod_init(void)
237{
240};
241
242static int __init md4_mod_init(void)
243{
238 return crypto_register_alg(&alg);
244 return crypto_register_shash(&alg);
239}
240
241static void __exit md4_mod_fini(void)
242{
245}
246
247static void __exit md4_mod_fini(void)
248{
243 crypto_unregister_alg(&alg);
249 crypto_unregister_shash(&alg);
244}
245
246module_init(md4_mod_init);
247module_exit(md4_mod_fini);
248
249MODULE_LICENSE("GPL");
250MODULE_DESCRIPTION("MD4 Message Digest Algorithm");
251
250}
251
252module_init(md4_mod_init);
253module_exit(md4_mod_fini);
254
255MODULE_LICENSE("GPL");
256MODULE_DESCRIPTION("MD4 Message Digest Algorithm");
257