1 #include <netinet/in.h>
16 * Stolen form Cryptographic API.
18 * MD4 Message Digest Algorithm (RFC1320).
20 * Implementation derived from Andrew Tridgell and Steve French's
21 * CIFS MD4 implementation, and the cryptoapi implementation
22 * originally based on the public domain implementation written
23 * by Colin Plumb in 1993.
25 * Copyright (c) Andrew Tridgell 1997-1998.
27 * Copyright (c) Cryptoapi developers.
31 * This program is free software; you can redistribute it and/or modify
32 * it under the terms of the GNU General Public License as published by
33 * the Free Software Foundation; either version 2 of the License, or
34 * (at your option) any later version.
37 #define MD4_DIGEST_SIZE 16
38 #define MD4_HMAC_BLOCK_SIZE 64
39 #define MD4_BLOCK_WORDS 16
40 #define MD4_HASH_WORDS 4
43 uint32_t hash[MD4_HASH_WORDS];
44 uint32_t block[MD4_BLOCK_WORDS];
48 static inline uint32_t lshift(uint32_t x, unsigned int s)
51 return ((x << s) & 0xFFFFFFFF) | (x >> (32 - s));
54 static inline uint32_t F(uint32_t x, uint32_t y, uint32_t z)
56 return (x & y) | ((~x) & z);
59 static inline uint32_t G(uint32_t x, uint32_t y, uint32_t z)
61 return (x & y) | (x & z) | (y & z);
64 static inline uint32_t H(uint32_t x, uint32_t y, uint32_t z)
69 #define ROUND1(a,b,c,d,k,s) (a = lshift(a + F(b,c,d) + k, s))
70 #define ROUND2(a,b,c,d,k,s) (a = lshift(a + G(b,c,d) + k + (uint32_t)0x5A827999,s))
71 #define ROUND3(a,b,c,d,k,s) (a = lshift(a + H(b,c,d) + k + (uint32_t)0x6ED9EBA1,s))
73 /* XXX: this stuff can be optimized */
74 static inline void le32_to_cpu_array(uint32_t *buf, unsigned int words)
82 static inline void cpu_to_le32_array(uint32_t *buf, unsigned int words)
90 static void md4_transform(uint32_t *hash, uint32_t const *in)
99 ROUND1(a, b, c, d, in[0], 3);
100 ROUND1(d, a, b, c, in[1], 7);
101 ROUND1(c, d, a, b, in[2], 11);
102 ROUND1(b, c, d, a, in[3], 19);
103 ROUND1(a, b, c, d, in[4], 3);
104 ROUND1(d, a, b, c, in[5], 7);
105 ROUND1(c, d, a, b, in[6], 11);
106 ROUND1(b, c, d, a, in[7], 19);
107 ROUND1(a, b, c, d, in[8], 3);
108 ROUND1(d, a, b, c, in[9], 7);
109 ROUND1(c, d, a, b, in[10], 11);
110 ROUND1(b, c, d, a, in[11], 19);
111 ROUND1(a, b, c, d, in[12], 3);
112 ROUND1(d, a, b, c, in[13], 7);
113 ROUND1(c, d, a, b, in[14], 11);
114 ROUND1(b, c, d, a, in[15], 19);
116 ROUND2(a, b, c, d,in[ 0], 3);
117 ROUND2(d, a, b, c, in[4], 5);
118 ROUND2(c, d, a, b, in[8], 9);
119 ROUND2(b, c, d, a, in[12], 13);
120 ROUND2(a, b, c, d, in[1], 3);
121 ROUND2(d, a, b, c, in[5], 5);
122 ROUND2(c, d, a, b, in[9], 9);
123 ROUND2(b, c, d, a, in[13], 13);
124 ROUND2(a, b, c, d, in[2], 3);
125 ROUND2(d, a, b, c, in[6], 5);
126 ROUND2(c, d, a, b, in[10], 9);
127 ROUND2(b, c, d, a, in[14], 13);
128 ROUND2(a, b, c, d, in[3], 3);
129 ROUND2(d, a, b, c, in[7], 5);
130 ROUND2(c, d, a, b, in[11], 9);
131 ROUND2(b, c, d, a, in[15], 13);
133 ROUND3(a, b, c, d,in[ 0], 3);
134 ROUND3(d, a, b, c, in[8], 9);
135 ROUND3(c, d, a, b, in[4], 11);
136 ROUND3(b, c, d, a, in[12], 15);
137 ROUND3(a, b, c, d, in[2], 3);
138 ROUND3(d, a, b, c, in[10], 9);
139 ROUND3(c, d, a, b, in[6], 11);
140 ROUND3(b, c, d, a, in[14], 15);
141 ROUND3(a, b, c, d, in[1], 3);
142 ROUND3(d, a, b, c, in[9], 9);
143 ROUND3(c, d, a, b, in[5], 11);
144 ROUND3(b, c, d, a, in[13], 15);
145 ROUND3(a, b, c, d, in[3], 3);
146 ROUND3(d, a, b, c, in[11], 9);
147 ROUND3(c, d, a, b, in[7], 11);
148 ROUND3(b, c, d, a, in[15], 15);
156 static inline void md4_transform_helper(struct md4_ctx *ctx)
158 le32_to_cpu_array(ctx->block, ARRAY_SIZE(ctx->block));
159 md4_transform(ctx->hash, ctx->block);
162 static void md4_init(struct md4_ctx *mctx)
164 mctx->hash[0] = 0x67452301;
165 mctx->hash[1] = 0xefcdab89;
166 mctx->hash[2] = 0x98badcfe;
167 mctx->hash[3] = 0x10325476;
168 mctx->byte_count = 0;
171 static void md4_update(struct md4_ctx *mctx,
172 const unsigned char *data, unsigned int len)
174 const uint32_t avail = sizeof(mctx->block) - (mctx->byte_count & 0x3f);
176 mctx->byte_count += len;
179 memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
184 memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
187 md4_transform_helper(mctx);
191 while (len >= sizeof(mctx->block)) {
192 memcpy(mctx->block, data, sizeof(mctx->block));
193 md4_transform_helper(mctx);
194 data += sizeof(mctx->block);
195 len -= sizeof(mctx->block);
198 memcpy(mctx->block, data, len);
201 static void md4_final_ascii(struct md4_ctx *mctx, char *out, unsigned int len)
203 const unsigned int offset = mctx->byte_count & 0x3f;
204 char *p = (char *)mctx->block + offset;
205 int padding = 56 - (offset + 1);
209 memset(p, 0x00, padding + sizeof (uint64_t));
210 md4_transform_helper(mctx);
211 p = (char *)mctx->block;
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(uint64_t)) / sizeof(uint32_t));
220 md4_transform(mctx->hash, mctx->block);
221 cpu_to_le32_array(mctx->hash, ARRAY_SIZE(mctx->hash));
223 snprintf(out, len, "%08X%08X%08X%08X",
224 mctx->hash[0], mctx->hash[1], mctx->hash[2], mctx->hash[3]);
227 static inline void add_char(unsigned char c, struct md4_ctx *md)
229 md4_update(md, &c, 1);
232 static int parse_string(const char *file, unsigned long len,
237 add_char(file[0], md);
238 for (i = 1; i < len; i++) {
239 add_char(file[i], md);
240 if (file[i] == '"' && file[i-1] != '\\')
246 static int parse_comment(const char *file, unsigned long len)
250 for (i = 2; i < len; i++) {
251 if (file[i-1] == '*' && file[i] == '/')
257 /* FIXME: Handle .s files differently (eg. # starts comments) --RR */
258 static int parse_file(const char *fname, struct md4_ctx *md)
261 unsigned long i, len;
263 file = read_text_file(fname);
266 for (i = 0; i < len; i++) {
267 /* Collapse and ignore \ and CR. */
268 if (file[i] == '\\' && (i+1 < len) && file[i+1] == '\n') {
273 /* Ignore whitespace */
274 if (isspace(file[i]))
277 /* Handle strings as whole units */
278 if (file[i] == '"') {
279 i += parse_string(file+i, len - i, md);
283 /* Comments: ignore */
284 if (file[i] == '/' && file[i+1] == '*') {
285 i += parse_comment(file+i, len - i);
289 add_char(file[i], md);
294 /* Check whether the file is a static library or not */
295 static bool is_static_library(const char *objfile)
297 int len = strlen(objfile);
299 return objfile[len - 2] == '.' && objfile[len - 1] == 'a';
302 /* We have dir/file.o. Open dir/.file.o.cmd, look for source_ and deps_ line
303 * to figure out source files. */
304 static int parse_source_files(const char *objfile, struct md4_ctx *md)
306 char *cmd, *file, *line, *dir, *pos;
308 int dirlen, ret = 0, check_files = 0;
310 cmd = xmalloc(strlen(objfile) + sizeof("..cmd"));
312 base = strrchr(objfile, '/');
315 dirlen = base - objfile;
316 sprintf(cmd, "%.*s.%s.cmd", dirlen, objfile, base);
319 sprintf(cmd, ".%s.cmd", objfile);
321 dir = xmalloc(dirlen + 1);
322 strncpy(dir, objfile, dirlen);
325 file = read_text_file(cmd);
329 /* Sum all files in the same dir or subdirs. */
330 while ((line = get_line(&pos))) {
333 /* trim the leading spaces away */
334 while (isspace(*line))
338 if (strncmp(line, "source_", sizeof("source_")-1) == 0) {
339 p = strrchr(line, ' ');
341 warn("malformed line: %s\n", line);
345 if (!parse_file(p, md)) {
346 warn("could not open %s: %s\n",
352 if (strncmp(line, "deps_", sizeof("deps_")-1) == 0) {
359 /* Continue until line does not end with '\' */
360 if ( *(p + strlen(p)-1) != '\\')
362 /* Terminate line at first space, to get rid of final ' \' */
371 /* Check if this file is in same dir as objfile */
372 if ((strstr(line, dir)+strlen(dir)-1) == strrchr(line, '/')) {
373 if (!parse_file(line, md)) {
374 warn("could not open %s: %s\n",
375 line, strerror(errno));
383 /* Everyone parsed OK */
392 /* Calc and record src checksum. */
393 void get_src_version(const char *modname, char sum[], unsigned sumlen)
398 char filelist[PATH_MAX + 1];
400 /* objects for a module are listed in the first line of *.mod file. */
401 snprintf(filelist, sizeof(filelist), "%s.mod", modname);
403 buf = read_text_file(filelist);
407 while ((fname = strsep(&pos, "\n"))) {
410 if (!(is_static_library(fname)) &&
411 !parse_source_files(fname, &md))
415 md4_final_ascii(&md, sum, sumlen);