1 #include <netinet/in.h>
12 /* Parse tag=value strings from .modinfo section */
13 static char *next_string(char *string, unsigned long *secsize)
15 /* Skip non-zero chars */
18 if ((*secsize)-- <= 1)
22 /* Skip any zero padding. */
25 if ((*secsize)-- <= 1)
31 static char *get_modinfo(void *modinfo, unsigned long modinfo_len,
35 unsigned int taglen = strlen(tag);
36 unsigned long size = modinfo_len;
38 for (p = modinfo; p; p = next_string(p, &size)) {
39 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
40 return p + taglen + 1;
46 * Stolen form Cryptographic API.
48 * MD4 Message Digest Algorithm (RFC1320).
50 * Implementation derived from Andrew Tridgell and Steve French's
51 * CIFS MD4 implementation, and the cryptoapi implementation
52 * originally based on the public domain implementation written
53 * by Colin Plumb in 1993.
55 * Copyright (c) Andrew Tridgell 1997-1998.
56 * Modified by Steve French (sfrench@us.ibm.com) 2002
57 * Copyright (c) Cryptoapi developers.
58 * Copyright (c) 2002 David S. Miller (davem@redhat.com)
59 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
61 * This program is free software; you can redistribute it and/or modify
62 * it under the terms of the GNU General Public License as published by
63 * the Free Software Foundation; either version 2 of the License, or
64 * (at your option) any later version.
67 #define MD4_DIGEST_SIZE 16
68 #define MD4_HMAC_BLOCK_SIZE 64
69 #define MD4_BLOCK_WORDS 16
70 #define MD4_HASH_WORDS 4
73 uint32_t hash[MD4_HASH_WORDS];
74 uint32_t block[MD4_BLOCK_WORDS];
78 static inline uint32_t lshift(uint32_t x, unsigned int s)
81 return ((x << s) & 0xFFFFFFFF) | (x >> (32 - s));
84 static inline uint32_t F(uint32_t x, uint32_t y, uint32_t z)
86 return (x & y) | ((~x) & z);
89 static inline uint32_t G(uint32_t x, uint32_t y, uint32_t z)
91 return (x & y) | (x & z) | (y & z);
94 static inline uint32_t H(uint32_t x, uint32_t y, uint32_t z)
99 #define ROUND1(a,b,c,d,k,s) (a = lshift(a + F(b,c,d) + k, s))
100 #define ROUND2(a,b,c,d,k,s) (a = lshift(a + G(b,c,d) + k + (uint32_t)0x5A827999,s))
101 #define ROUND3(a,b,c,d,k,s) (a = lshift(a + H(b,c,d) + k + (uint32_t)0x6ED9EBA1,s))
103 /* XXX: this stuff can be optimized */
104 static inline void le32_to_cpu_array(uint32_t *buf, unsigned int words)
112 static inline void cpu_to_le32_array(uint32_t *buf, unsigned int words)
120 static void md4_transform(uint32_t *hash, uint32_t const *in)
129 ROUND1(a, b, c, d, in[0], 3);
130 ROUND1(d, a, b, c, in[1], 7);
131 ROUND1(c, d, a, b, in[2], 11);
132 ROUND1(b, c, d, a, in[3], 19);
133 ROUND1(a, b, c, d, in[4], 3);
134 ROUND1(d, a, b, c, in[5], 7);
135 ROUND1(c, d, a, b, in[6], 11);
136 ROUND1(b, c, d, a, in[7], 19);
137 ROUND1(a, b, c, d, in[8], 3);
138 ROUND1(d, a, b, c, in[9], 7);
139 ROUND1(c, d, a, b, in[10], 11);
140 ROUND1(b, c, d, a, in[11], 19);
141 ROUND1(a, b, c, d, in[12], 3);
142 ROUND1(d, a, b, c, in[13], 7);
143 ROUND1(c, d, a, b, in[14], 11);
144 ROUND1(b, c, d, a, in[15], 19);
146 ROUND2(a, b, c, d,in[ 0], 3);
147 ROUND2(d, a, b, c, in[4], 5);
148 ROUND2(c, d, a, b, in[8], 9);
149 ROUND2(b, c, d, a, in[12], 13);
150 ROUND2(a, b, c, d, in[1], 3);
151 ROUND2(d, a, b, c, in[5], 5);
152 ROUND2(c, d, a, b, in[9], 9);
153 ROUND2(b, c, d, a, in[13], 13);
154 ROUND2(a, b, c, d, in[2], 3);
155 ROUND2(d, a, b, c, in[6], 5);
156 ROUND2(c, d, a, b, in[10], 9);
157 ROUND2(b, c, d, a, in[14], 13);
158 ROUND2(a, b, c, d, in[3], 3);
159 ROUND2(d, a, b, c, in[7], 5);
160 ROUND2(c, d, a, b, in[11], 9);
161 ROUND2(b, c, d, a, in[15], 13);
163 ROUND3(a, b, c, d,in[ 0], 3);
164 ROUND3(d, a, b, c, in[8], 9);
165 ROUND3(c, d, a, b, in[4], 11);
166 ROUND3(b, c, d, a, in[12], 15);
167 ROUND3(a, b, c, d, in[2], 3);
168 ROUND3(d, a, b, c, in[10], 9);
169 ROUND3(c, d, a, b, in[6], 11);
170 ROUND3(b, c, d, a, in[14], 15);
171 ROUND3(a, b, c, d, in[1], 3);
172 ROUND3(d, a, b, c, in[9], 9);
173 ROUND3(c, d, a, b, in[5], 11);
174 ROUND3(b, c, d, a, in[13], 15);
175 ROUND3(a, b, c, d, in[3], 3);
176 ROUND3(d, a, b, c, in[11], 9);
177 ROUND3(c, d, a, b, in[7], 11);
178 ROUND3(b, c, d, a, in[15], 15);
186 static inline void md4_transform_helper(struct md4_ctx *ctx)
188 le32_to_cpu_array(ctx->block, sizeof(ctx->block) / sizeof(uint32_t));
189 md4_transform(ctx->hash, ctx->block);
192 static void md4_init(struct md4_ctx *mctx)
194 mctx->hash[0] = 0x67452301;
195 mctx->hash[1] = 0xefcdab89;
196 mctx->hash[2] = 0x98badcfe;
197 mctx->hash[3] = 0x10325476;
198 mctx->byte_count = 0;
201 static void md4_update(struct md4_ctx *mctx,
202 const unsigned char *data, unsigned int len)
204 const uint32_t avail = sizeof(mctx->block) - (mctx->byte_count & 0x3f);
206 mctx->byte_count += len;
209 memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
214 memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
217 md4_transform_helper(mctx);
221 while (len >= sizeof(mctx->block)) {
222 memcpy(mctx->block, data, sizeof(mctx->block));
223 md4_transform_helper(mctx);
224 data += sizeof(mctx->block);
225 len -= sizeof(mctx->block);
228 memcpy(mctx->block, data, len);
231 static void md4_final_ascii(struct md4_ctx *mctx, char *out, unsigned int len)
233 const unsigned int offset = mctx->byte_count & 0x3f;
234 char *p = (char *)mctx->block + offset;
235 int padding = 56 - (offset + 1);
239 memset(p, 0x00, padding + sizeof (uint64_t));
240 md4_transform_helper(mctx);
241 p = (char *)mctx->block;
245 memset(p, 0, padding);
246 mctx->block[14] = mctx->byte_count << 3;
247 mctx->block[15] = mctx->byte_count >> 29;
248 le32_to_cpu_array(mctx->block, (sizeof(mctx->block) -
249 sizeof(uint64_t)) / sizeof(uint32_t));
250 md4_transform(mctx->hash, mctx->block);
251 cpu_to_le32_array(mctx->hash, sizeof(mctx->hash) / sizeof(uint32_t));
253 snprintf(out, len, "%08X%08X%08X%08X",
254 mctx->hash[0], mctx->hash[1], mctx->hash[2], mctx->hash[3]);
257 static inline void add_char(unsigned char c, struct md4_ctx *md)
259 md4_update(md, &c, 1);
262 static int parse_string(const char *file, unsigned long len,
267 add_char(file[0], md);
268 for (i = 1; i < len; i++) {
269 add_char(file[i], md);
270 if (file[i] == '"' && file[i-1] != '\\')
276 static int parse_comment(const char *file, unsigned long len)
280 for (i = 2; i < len; i++) {
281 if (file[i-1] == '*' && file[i] == '/')
287 /* FIXME: Handle .s files differently (eg. # starts comments) --RR */
288 static int parse_file(const char *fname, struct md4_ctx *md)
291 unsigned long i, len;
293 file = grab_file(fname, &len);
297 for (i = 0; i < len; i++) {
298 /* Collapse and ignore \ and CR. */
299 if (file[i] == '\\' && (i+1 < len) && file[i+1] == '\n') {
304 /* Ignore whitespace */
305 if (isspace(file[i]))
308 /* Handle strings as whole units */
309 if (file[i] == '"') {
310 i += parse_string(file+i, len - i, md);
314 /* Comments: ignore */
315 if (file[i] == '/' && file[i+1] == '*') {
316 i += parse_comment(file+i, len - i);
320 add_char(file[i], md);
322 release_file(file, len);
326 /* We have dir/file.o. Open dir/.file.o.cmd, look for deps_ line to
327 * figure out source file. */
328 static int parse_source_files(const char *objfile, struct md4_ctx *md)
330 char *cmd, *file, *line, *dir;
332 unsigned long flen, pos = 0;
333 int dirlen, ret = 0, check_files = 0;
335 cmd = NOFAIL(malloc(strlen(objfile) + sizeof("..cmd")));
337 base = strrchr(objfile, '/');
340 dirlen = base - objfile;
341 sprintf(cmd, "%.*s.%s.cmd", dirlen, objfile, base);
344 sprintf(cmd, ".%s.cmd", objfile);
346 dir = NOFAIL(malloc(dirlen + 1));
347 strncpy(dir, objfile, dirlen);
350 file = grab_file(cmd, &flen);
352 fprintf(stderr, "Warning: could not find %s for %s\n",
357 /* There will be a line like so:
358 deps_drivers/net/dummy.o := \
359 drivers/net/dummy.c \
360 $(wildcard include/config/net/fastroute.h) \
361 include/linux/config.h \
362 $(wildcard include/config/h.h) \
363 include/linux/module.h \
365 Sum all files in the same dir or subdirs.
367 while ((line = get_next_line(&pos, file, flen)) != NULL) {
369 if (strncmp(line, "deps_", sizeof("deps_")-1) == 0) {
376 /* Continue until line does not end with '\' */
377 if ( *(p + strlen(p)-1) != '\\')
379 /* Terminate line at first space, to get rid of final ' \' */
388 /* Check if this file is in same dir as objfile */
389 if ((strstr(line, dir)+strlen(dir)-1) == strrchr(line, '/')) {
390 if (!parse_file(line, md)) {
392 "Warning: could not open %s: %s\n",
393 line, strerror(errno));
401 /* Everyone parsed OK */
404 release_file(file, flen);
411 static int get_version(const char *modname, char sum[])
417 char *sources, *end, *fname;
418 const char *basename;
419 char filelist[sizeof(".tmp_versions/%s.mod") + strlen(modname)];
421 /* Source files for module are in .tmp_versions/modname.mod,
422 after the first line. */
423 if (strrchr(modname, '/'))
424 basename = strrchr(modname, '/') + 1;
427 sprintf(filelist, ".tmp_versions/%s", basename);
428 /* Truncate .o, add .mod */
429 strcpy(filelist + strlen(filelist)-2, ".mod");
431 file = grab_file(filelist, &len);
433 fprintf(stderr, "Warning: could not find versions for %s\n",
438 sources = strchr(file, '\n');
440 fprintf(stderr, "Warning: malformed versions file for %s\n",
446 end = strchr(sources, '\n');
448 fprintf(stderr, "Warning: bad ending versions file for %s\n",
455 for (fname = strtok(sources, " "); fname; fname = strtok(NULL, " ")) {
456 if (!parse_source_files(fname, &md))
460 /* sum is of form \0<padding>. */
461 md4_final_ascii(&md, sum, 1 + strlen(sum+1));
464 release_file(file, len);
468 static void write_version(const char *filename, const char *sum,
469 unsigned long offset)
473 fd = open(filename, O_RDWR);
475 fprintf(stderr, "Warning: changing sum in %s failed: %s\n",
476 filename, strerror(errno));
480 if (lseek(fd, offset, SEEK_SET) == (off_t)-1) {
481 fprintf(stderr, "Warning: changing sum in %s:%lu failed: %s\n",
482 filename, offset, strerror(errno));
486 if (write(fd, sum, strlen(sum)+1) != strlen(sum)+1) {
487 fprintf(stderr, "Warning: writing sum in %s failed: %s\n",
488 filename, strerror(errno));
495 void strip_rcs_crap(char *version)
497 unsigned int len, full_len;
499 if (strncmp(version, "$Revision", strlen("$Revision")) != 0)
502 /* Space for version string follows. */
503 full_len = strlen(version) + strlen(version + strlen(version) + 1) + 2;
505 /* Move string to start with version number: prefix will be
506 * $Revision$ or $Revision: */
507 len = strlen("$Revision");
508 if (version[len] == ':' || version[len] == '$')
510 while (isspace(version[len]))
512 memmove(version, version+len, full_len-len);
515 /* Preserve up to next whitespace. */
517 while (version[len] && !isspace(version[len]))
519 memmove(version + len, version + strlen(version),
520 full_len - strlen(version));
523 /* If the modinfo contains a "version" value, then set this. */
524 void maybe_frob_version(const char *modfilename,
526 unsigned long modinfo_len,
527 unsigned long modinfo_offset)
529 char *version, *csum;
531 version = get_modinfo(modinfo, modinfo_len, "version");
535 /* RCS $Revision gets stripped out. */
536 strip_rcs_crap(version);
538 /* Check against double sumversion */
539 if (strchr(version, ' '))
542 /* Version contains embedded NUL: second half has space for checksum */
543 csum = version + strlen(version);
545 if (get_version(modfilename, csum))
546 write_version(modfilename, version,
547 modinfo_offset + (version - (char *)modinfo));