blob: 8a2875689e4da48202ddc87468a23e0e8db58c58 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#include <netinet/in.h>
2#ifdef __sun__
3#include <inttypes.h>
4#else
5#include <stdint.h>
6#endif
7#include <ctype.h>
8#include <errno.h>
9#include <string.h>
10#include "modpost.h"
11
12/*
13 * Stolen form Cryptographic API.
14 *
15 * MD4 Message Digest Algorithm (RFC1320).
16 *
17 * Implementation derived from Andrew Tridgell and Steve French's
18 * CIFS MD4 implementation, and the cryptoapi implementation
19 * originally based on the public domain implementation written
20 * by Colin Plumb in 1993.
21 *
22 * Copyright (c) Andrew Tridgell 1997-1998.
23 * Modified by Steve French (sfrench@us.ibm.com) 2002
24 * Copyright (c) Cryptoapi developers.
25 * Copyright (c) 2002 David S. Miller (davem@redhat.com)
26 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
27 *
28 * This program is free software; you can redistribute it and/or modify
29 * it under the terms of the GNU General Public License as published by
30 * the Free Software Foundation; either version 2 of the License, or
31 * (at your option) any later version.
32 *
33 */
34#define MD4_DIGEST_SIZE 16
35#define MD4_HMAC_BLOCK_SIZE 64
36#define MD4_BLOCK_WORDS 16
37#define MD4_HASH_WORDS 4
38
39struct md4_ctx {
40 uint32_t hash[MD4_HASH_WORDS];
41 uint32_t block[MD4_BLOCK_WORDS];
42 uint64_t byte_count;
43};
44
45static inline uint32_t lshift(uint32_t x, unsigned int s)
46{
47 x &= 0xFFFFFFFF;
48 return ((x << s) & 0xFFFFFFFF) | (x >> (32 - s));
49}
50
51static inline uint32_t F(uint32_t x, uint32_t y, uint32_t z)
52{
53 return (x & y) | ((~x) & z);
54}
55
56static inline uint32_t G(uint32_t x, uint32_t y, uint32_t z)
57{
58 return (x & y) | (x & z) | (y & z);
59}
60
61static inline uint32_t H(uint32_t x, uint32_t y, uint32_t z)
62{
63 return x ^ y ^ z;
64}
65
66#define ROUND1(a,b,c,d,k,s) (a = lshift(a + F(b,c,d) + k, s))
67#define ROUND2(a,b,c,d,k,s) (a = lshift(a + G(b,c,d) + k + (uint32_t)0x5A827999,s))
68#define ROUND3(a,b,c,d,k,s) (a = lshift(a + H(b,c,d) + k + (uint32_t)0x6ED9EBA1,s))
69
70/* XXX: this stuff can be optimized */
71static inline void le32_to_cpu_array(uint32_t *buf, unsigned int words)
72{
73 while (words--) {
74 *buf = ntohl(*buf);
75 buf++;
76 }
77}
78
79static inline void cpu_to_le32_array(uint32_t *buf, unsigned int words)
80{
81 while (words--) {
82 *buf = htonl(*buf);
83 buf++;
84 }
85}
86
87static void md4_transform(uint32_t *hash, uint32_t const *in)
88{
89 uint32_t a, b, c, d;
90
91 a = hash[0];
92 b = hash[1];
93 c = hash[2];
94 d = hash[3];
95
96 ROUND1(a, b, c, d, in[0], 3);
97 ROUND1(d, a, b, c, in[1], 7);
98 ROUND1(c, d, a, b, in[2], 11);
99 ROUND1(b, c, d, a, in[3], 19);
100 ROUND1(a, b, c, d, in[4], 3);
101 ROUND1(d, a, b, c, in[5], 7);
102 ROUND1(c, d, a, b, in[6], 11);
103 ROUND1(b, c, d, a, in[7], 19);
104 ROUND1(a, b, c, d, in[8], 3);
105 ROUND1(d, a, b, c, in[9], 7);
106 ROUND1(c, d, a, b, in[10], 11);
107 ROUND1(b, c, d, a, in[11], 19);
108 ROUND1(a, b, c, d, in[12], 3);
109 ROUND1(d, a, b, c, in[13], 7);
110 ROUND1(c, d, a, b, in[14], 11);
111 ROUND1(b, c, d, a, in[15], 19);
112
113 ROUND2(a, b, c, d,in[ 0], 3);
114 ROUND2(d, a, b, c, in[4], 5);
115 ROUND2(c, d, a, b, in[8], 9);
116 ROUND2(b, c, d, a, in[12], 13);
117 ROUND2(a, b, c, d, in[1], 3);
118 ROUND2(d, a, b, c, in[5], 5);
119 ROUND2(c, d, a, b, in[9], 9);
120 ROUND2(b, c, d, a, in[13], 13);
121 ROUND2(a, b, c, d, in[2], 3);
122 ROUND2(d, a, b, c, in[6], 5);
123 ROUND2(c, d, a, b, in[10], 9);
124 ROUND2(b, c, d, a, in[14], 13);
125 ROUND2(a, b, c, d, in[3], 3);
126 ROUND2(d, a, b, c, in[7], 5);
127 ROUND2(c, d, a, b, in[11], 9);
128 ROUND2(b, c, d, a, in[15], 13);
129
130 ROUND3(a, b, c, d,in[ 0], 3);
131 ROUND3(d, a, b, c, in[8], 9);
132 ROUND3(c, d, a, b, in[4], 11);
133 ROUND3(b, c, d, a, in[12], 15);
134 ROUND3(a, b, c, d, in[2], 3);
135 ROUND3(d, a, b, c, in[10], 9);
136 ROUND3(c, d, a, b, in[6], 11);
137 ROUND3(b, c, d, a, in[14], 15);
138 ROUND3(a, b, c, d, in[1], 3);
139 ROUND3(d, a, b, c, in[9], 9);
140 ROUND3(c, d, a, b, in[5], 11);
141 ROUND3(b, c, d, a, in[13], 15);
142 ROUND3(a, b, c, d, in[3], 3);
143 ROUND3(d, a, b, c, in[11], 9);
144 ROUND3(c, d, a, b, in[7], 11);
145 ROUND3(b, c, d, a, in[15], 15);
146
147 hash[0] += a;
148 hash[1] += b;
149 hash[2] += c;
150 hash[3] += d;
151}
152
153static inline void md4_transform_helper(struct md4_ctx *ctx)
154{
155 le32_to_cpu_array(ctx->block, sizeof(ctx->block) / sizeof(uint32_t));
156 md4_transform(ctx->hash, ctx->block);
157}
158
159static void md4_init(struct md4_ctx *mctx)
160{
161 mctx->hash[0] = 0x67452301;
162 mctx->hash[1] = 0xefcdab89;
163 mctx->hash[2] = 0x98badcfe;
164 mctx->hash[3] = 0x10325476;
165 mctx->byte_count = 0;
166}
167
168static void md4_update(struct md4_ctx *mctx,
169 const unsigned char *data, unsigned int len)
170{
171 const uint32_t 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;
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
198static void md4_final_ascii(struct md4_ctx *mctx, char *out, unsigned int len)
199{
200 const unsigned int offset = mctx->byte_count & 0x3f;
201 char *p = (char *)mctx->block + offset;
202 int padding = 56 - (offset + 1);
203
204 *p++ = 0x80;
205 if (padding < 0) {
206 memset(p, 0x00, padding + sizeof (uint64_t));
207 md4_transform_helper(mctx);
208 p = (char *)mctx->block;
209 padding = 56;
210 }
211
212 memset(p, 0, padding);
213 mctx->block[14] = mctx->byte_count << 3;
214 mctx->block[15] = mctx->byte_count >> 29;
215 le32_to_cpu_array(mctx->block, (sizeof(mctx->block) -
216 sizeof(uint64_t)) / sizeof(uint32_t));
217 md4_transform(mctx->hash, mctx->block);
218 cpu_to_le32_array(mctx->hash, sizeof(mctx->hash) / sizeof(uint32_t));
219
220 snprintf(out, len, "%08X%08X%08X%08X",
221 mctx->hash[0], mctx->hash[1], mctx->hash[2], mctx->hash[3]);
222}
223
224static inline void add_char(unsigned char c, struct md4_ctx *md)
225{
226 md4_update(md, &c, 1);
227}
228
229static int parse_string(const char *file, unsigned long len,
230 struct md4_ctx *md)
231{
232 unsigned long i;
233
234 add_char(file[0], md);
235 for (i = 1; i < len; i++) {
236 add_char(file[i], md);
237 if (file[i] == '"' && file[i-1] != '\\')
238 break;
239 }
240 return i;
241}
242
243static int parse_comment(const char *file, unsigned long len)
244{
245 unsigned long i;
246
247 for (i = 2; i < len; i++) {
248 if (file[i-1] == '*' && file[i] == '/')
249 break;
250 }
251 return i;
252}
253
254/* FIXME: Handle .s files differently (eg. # starts comments) --RR */
J.A. Magallon61d9cdf2005-07-15 22:14:43 +0000255static int parse_file(const char *fname, struct md4_ctx *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
J.A. Magallon61d9cdf2005-07-15 22:14:43 +0000257 char *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 unsigned long i, len;
259
260 file = grab_file(fname, &len);
261 if (!file)
262 return 0;
263
264 for (i = 0; i < len; i++) {
265 /* Collapse and ignore \ and CR. */
266 if (file[i] == '\\' && (i+1 < len) && file[i+1] == '\n') {
267 i++;
268 continue;
269 }
270
271 /* Ignore whitespace */
272 if (isspace(file[i]))
273 continue;
274
275 /* Handle strings as whole units */
276 if (file[i] == '"') {
277 i += parse_string(file+i, len - i, md);
278 continue;
279 }
280
281 /* Comments: ignore */
282 if (file[i] == '/' && file[i+1] == '*') {
283 i += parse_comment(file+i, len - i);
284 continue;
285 }
286
287 add_char(file[i], md);
288 }
289 release_file(file, len);
290 return 1;
291}
292
293/* We have dir/file.o. Open dir/.file.o.cmd, look for deps_ line to
294 * figure out source file. */
295static int parse_source_files(const char *objfile, struct md4_ctx *md)
296{
297 char *cmd, *file, *line, *dir;
298 const char *base;
299 unsigned long flen, pos = 0;
300 int dirlen, ret = 0, check_files = 0;
301
302 cmd = NOFAIL(malloc(strlen(objfile) + sizeof("..cmd")));
303
304 base = strrchr(objfile, '/');
305 if (base) {
306 base++;
307 dirlen = base - objfile;
308 sprintf(cmd, "%.*s.%s.cmd", dirlen, objfile, base);
309 } else {
310 dirlen = 0;
311 sprintf(cmd, ".%s.cmd", objfile);
312 }
313 dir = NOFAIL(malloc(dirlen + 1));
314 strncpy(dir, objfile, dirlen);
315 dir[dirlen] = '\0';
316
317 file = grab_file(cmd, &flen);
318 if (!file) {
Sam Ravnborgcb805142006-01-28 16:57:26 +0100319 warn("could not find %s for %s\n", cmd, objfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 goto out;
321 }
322
323 /* There will be a line like so:
324 deps_drivers/net/dummy.o := \
325 drivers/net/dummy.c \
326 $(wildcard include/config/net/fastroute.h) \
327 include/linux/config.h \
328 $(wildcard include/config/h.h) \
329 include/linux/module.h \
330
331 Sum all files in the same dir or subdirs.
332 */
333 while ((line = get_next_line(&pos, file, flen)) != NULL) {
J.A. Magallon61d9cdf2005-07-15 22:14:43 +0000334 char* p = line;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 if (strncmp(line, "deps_", sizeof("deps_")-1) == 0) {
336 check_files = 1;
337 continue;
338 }
339 if (!check_files)
340 continue;
341
342 /* Continue until line does not end with '\' */
343 if ( *(p + strlen(p)-1) != '\\')
344 break;
345 /* Terminate line at first space, to get rid of final ' \' */
346 while (*p) {
347 if (isspace(*p)) {
348 *p = '\0';
349 break;
350 }
351 p++;
352 }
353
354 /* Check if this file is in same dir as objfile */
355 if ((strstr(line, dir)+strlen(dir)-1) == strrchr(line, '/')) {
356 if (!parse_file(line, md)) {
Sam Ravnborgcb805142006-01-28 16:57:26 +0100357 warn("could not open %s: %s\n",
358 line, strerror(errno));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 goto out_file;
360 }
361
362 }
363
364 }
365
366 /* Everyone parsed OK */
367 ret = 1;
368out_file:
369 release_file(file, flen);
370out:
371 free(dir);
372 free(cmd);
373 return ret;
374}
375
376/* Calc and record src checksum. */
377void get_src_version(const char *modname, char sum[], unsigned sumlen)
378{
379 void *file;
380 unsigned long len;
381 struct md4_ctx md;
382 char *sources, *end, *fname;
383 const char *basename;
Sam Ravnborg41370d32006-02-19 00:49:37 +0100384 char filelist[PATH_MAX + 1];
385 char *modverdir = getenv("MODVERDIR");
386
387 if (!modverdir)
388 modverdir = ".";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390 /* Source files for module are in .tmp_versions/modname.mod,
391 after the first line. */
392 if (strrchr(modname, '/'))
393 basename = strrchr(modname, '/') + 1;
394 else
395 basename = modname;
Sam Ravnborg41370d32006-02-19 00:49:37 +0100396 sprintf(filelist, "%s/%.*s.mod", modverdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 (int) strlen(basename) - 2, basename);
398
399 file = grab_file(filelist, &len);
400 if (!file) {
Sam Ravnborgcb805142006-01-28 16:57:26 +0100401 warn("could not find versions for %s\n", filelist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 return;
403 }
404
405 sources = strchr(file, '\n');
406 if (!sources) {
Sam Ravnborgcb805142006-01-28 16:57:26 +0100407 warn("malformed versions file for %s\n", modname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 goto release;
409 }
410
411 sources++;
412 end = strchr(sources, '\n');
413 if (!end) {
Sam Ravnborgcb805142006-01-28 16:57:26 +0100414 warn("bad ending versions file for %s\n", modname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 goto release;
416 }
417 *end = '\0';
418
419 md4_init(&md);
420 while ((fname = strsep(&sources, " ")) != NULL) {
421 if (!*fname)
422 continue;
423 if (!parse_source_files(fname, &md))
424 goto release;
425 }
426
427 md4_final_ascii(&md, sum, sumlen);
428release:
429 release_file(file, len);
430}
431
432static void write_version(const char *filename, const char *sum,
433 unsigned long offset)
434{
435 int fd;
436
437 fd = open(filename, O_RDWR);
438 if (fd < 0) {
Sam Ravnborgcb805142006-01-28 16:57:26 +0100439 warn("changing sum in %s failed: %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 filename, strerror(errno));
441 return;
442 }
443
444 if (lseek(fd, offset, SEEK_SET) == (off_t)-1) {
Sam Ravnborgcb805142006-01-28 16:57:26 +0100445 warn("changing sum in %s:%lu failed: %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 filename, offset, strerror(errno));
447 goto out;
448 }
449
450 if (write(fd, sum, strlen(sum)+1) != strlen(sum)+1) {
Sam Ravnborgcb805142006-01-28 16:57:26 +0100451 warn("writing sum in %s failed: %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 filename, strerror(errno));
453 goto out;
454 }
455out:
456 close(fd);
457}
458
J.A. Magallon61d9cdf2005-07-15 22:14:43 +0000459static int strip_rcs_crap(char *version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
461 unsigned int len, full_len;
462
463 if (strncmp(version, "$Revision", strlen("$Revision")) != 0)
464 return 0;
465
466 /* Space for version string follows. */
467 full_len = strlen(version) + strlen(version + strlen(version) + 1) + 2;
468
469 /* Move string to start with version number: prefix will be
470 * $Revision$ or $Revision: */
471 len = strlen("$Revision");
472 if (version[len] == ':' || version[len] == '$')
473 len++;
474 while (isspace(version[len]))
475 len++;
476 memmove(version, version+len, full_len-len);
477 full_len -= len;
478
479 /* Preserve up to next whitespace. */
480 len = 0;
481 while (version[len] && !isspace(version[len]))
482 len++;
483 memmove(version + len, version + strlen(version),
484 full_len - strlen(version));
485 return 1;
486}
487
488/* Clean up RCS-style version numbers. */
489void maybe_frob_rcs_version(const char *modfilename,
490 char *version,
491 void *modinfo,
492 unsigned long version_offset)
493{
494 if (strip_rcs_crap(version))
495 write_version(modfilename, version, version_offset);
496}