1 #include <netinet/in.h>
8 /* Parse tag=value strings from .modinfo section */
9 static char *next_string(char *string
, unsigned long *secsize
)
11 /* Skip non-zero chars */
14 if ((*secsize
)-- <= 1)
18 /* Skip any zero padding. */
21 if ((*secsize
)-- <= 1)
27 static char *get_modinfo(void *modinfo
, unsigned long modinfo_len
,
31 unsigned int taglen
= strlen(tag
);
32 unsigned long size
= modinfo_len
;
34 for (p
= modinfo
; p
; p
= next_string(p
, &size
)) {
35 if (strncmp(p
, tag
, taglen
) == 0 && p
[taglen
] == '=')
36 return p
+ taglen
+ 1;
42 * Stolen form Cryptographic API.
44 * MD4 Message Digest Algorithm (RFC1320).
46 * Implementation derived from Andrew Tridgell and Steve French's
47 * CIFS MD4 implementation, and the cryptoapi implementation
48 * originally based on the public domain implementation written
49 * by Colin Plumb in 1993.
51 * Copyright (c) Andrew Tridgell 1997-1998.
52 * Modified by Steve French (sfrench@us.ibm.com) 2002
53 * Copyright (c) Cryptoapi developers.
54 * Copyright (c) 2002 David S. Miller (davem@redhat.com)
55 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
57 * This program is free software; you can redistribute it and/or modify
58 * it under the terms of the GNU General Public License as published by
59 * the Free Software Foundation; either version 2 of the License, or
60 * (at your option) any later version.
63 #define MD4_DIGEST_SIZE 16
64 #define MD4_HMAC_BLOCK_SIZE 64
65 #define MD4_BLOCK_WORDS 16
66 #define MD4_HASH_WORDS 4
69 uint32_t hash
[MD4_HASH_WORDS
];
70 uint32_t block
[MD4_BLOCK_WORDS
];
74 static inline uint32_t lshift(uint32_t x
, unsigned int s
)
77 return ((x
<< s
) & 0xFFFFFFFF) | (x
>> (32 - s
));
80 static inline uint32_t F(uint32_t x
, uint32_t y
, uint32_t z
)
82 return (x
& y
) | ((~x
) & z
);
85 static inline uint32_t G(uint32_t x
, uint32_t y
, uint32_t z
)
87 return (x
& y
) | (x
& z
) | (y
& z
);
90 static inline uint32_t H(uint32_t x
, uint32_t y
, uint32_t z
)
95 #define ROUND1(a,b,c,d,k,s) (a = lshift(a + F(b,c,d) + k, s))
96 #define ROUND2(a,b,c,d,k,s) (a = lshift(a + G(b,c,d) + k + (uint32_t)0x5A827999,s))
97 #define ROUND3(a,b,c,d,k,s) (a = lshift(a + H(b,c,d) + k + (uint32_t)0x6ED9EBA1,s))
99 /* XXX: this stuff can be optimized */
100 static inline void le32_to_cpu_array(uint32_t *buf
, unsigned int words
)
108 static inline void cpu_to_le32_array(uint32_t *buf
, unsigned int words
)
116 static void md4_transform(uint32_t *hash
, uint32_t const *in
)
125 ROUND1(a
, b
, c
, d
, in
[0], 3);
126 ROUND1(d
, a
, b
, c
, in
[1], 7);
127 ROUND1(c
, d
, a
, b
, in
[2], 11);
128 ROUND1(b
, c
, d
, a
, in
[3], 19);
129 ROUND1(a
, b
, c
, d
, in
[4], 3);
130 ROUND1(d
, a
, b
, c
, in
[5], 7);
131 ROUND1(c
, d
, a
, b
, in
[6], 11);
132 ROUND1(b
, c
, d
, a
, in
[7], 19);
133 ROUND1(a
, b
, c
, d
, in
[8], 3);
134 ROUND1(d
, a
, b
, c
, in
[9], 7);
135 ROUND1(c
, d
, a
, b
, in
[10], 11);
136 ROUND1(b
, c
, d
, a
, in
[11], 19);
137 ROUND1(a
, b
, c
, d
, in
[12], 3);
138 ROUND1(d
, a
, b
, c
, in
[13], 7);
139 ROUND1(c
, d
, a
, b
, in
[14], 11);
140 ROUND1(b
, c
, d
, a
, in
[15], 19);
142 ROUND2(a
, b
, c
, d
,in
[ 0], 3);
143 ROUND2(d
, a
, b
, c
, in
[4], 5);
144 ROUND2(c
, d
, a
, b
, in
[8], 9);
145 ROUND2(b
, c
, d
, a
, in
[12], 13);
146 ROUND2(a
, b
, c
, d
, in
[1], 3);
147 ROUND2(d
, a
, b
, c
, in
[5], 5);
148 ROUND2(c
, d
, a
, b
, in
[9], 9);
149 ROUND2(b
, c
, d
, a
, in
[13], 13);
150 ROUND2(a
, b
, c
, d
, in
[2], 3);
151 ROUND2(d
, a
, b
, c
, in
[6], 5);
152 ROUND2(c
, d
, a
, b
, in
[10], 9);
153 ROUND2(b
, c
, d
, a
, in
[14], 13);
154 ROUND2(a
, b
, c
, d
, in
[3], 3);
155 ROUND2(d
, a
, b
, c
, in
[7], 5);
156 ROUND2(c
, d
, a
, b
, in
[11], 9);
157 ROUND2(b
, c
, d
, a
, in
[15], 13);
159 ROUND3(a
, b
, c
, d
,in
[ 0], 3);
160 ROUND3(d
, a
, b
, c
, in
[8], 9);
161 ROUND3(c
, d
, a
, b
, in
[4], 11);
162 ROUND3(b
, c
, d
, a
, in
[12], 15);
163 ROUND3(a
, b
, c
, d
, in
[2], 3);
164 ROUND3(d
, a
, b
, c
, in
[10], 9);
165 ROUND3(c
, d
, a
, b
, in
[6], 11);
166 ROUND3(b
, c
, d
, a
, in
[14], 15);
167 ROUND3(a
, b
, c
, d
, in
[1], 3);
168 ROUND3(d
, a
, b
, c
, in
[9], 9);
169 ROUND3(c
, d
, a
, b
, in
[5], 11);
170 ROUND3(b
, c
, d
, a
, in
[13], 15);
171 ROUND3(a
, b
, c
, d
, in
[3], 3);
172 ROUND3(d
, a
, b
, c
, in
[11], 9);
173 ROUND3(c
, d
, a
, b
, in
[7], 11);
174 ROUND3(b
, c
, d
, a
, in
[15], 15);
182 static inline void md4_transform_helper(struct md4_ctx
*ctx
)
184 le32_to_cpu_array(ctx
->block
, sizeof(ctx
->block
) / sizeof(uint32_t));
185 md4_transform(ctx
->hash
, ctx
->block
);
188 static void md4_init(struct md4_ctx
*mctx
)
190 mctx
->hash
[0] = 0x67452301;
191 mctx
->hash
[1] = 0xefcdab89;
192 mctx
->hash
[2] = 0x98badcfe;
193 mctx
->hash
[3] = 0x10325476;
194 mctx
->byte_count
= 0;
197 static void md4_update(struct md4_ctx
*mctx
,
198 const unsigned char *data
, unsigned int len
)
200 const uint32_t avail
= sizeof(mctx
->block
) - (mctx
->byte_count
& 0x3f);
202 mctx
->byte_count
+= len
;
205 memcpy((char *)mctx
->block
+ (sizeof(mctx
->block
) - avail
),
210 memcpy((char *)mctx
->block
+ (sizeof(mctx
->block
) - avail
),
213 md4_transform_helper(mctx
);
217 while (len
>= sizeof(mctx
->block
)) {
218 memcpy(mctx
->block
, data
, sizeof(mctx
->block
));
219 md4_transform_helper(mctx
);
220 data
+= sizeof(mctx
->block
);
221 len
-= sizeof(mctx
->block
);
224 memcpy(mctx
->block
, data
, len
);
227 static void md4_final_ascii(struct md4_ctx
*mctx
, char *out
, unsigned int len
)
229 const unsigned int offset
= mctx
->byte_count
& 0x3f;
230 char *p
= (char *)mctx
->block
+ offset
;
231 int padding
= 56 - (offset
+ 1);
235 memset(p
, 0x00, padding
+ sizeof (uint64_t));
236 md4_transform_helper(mctx
);
237 p
= (char *)mctx
->block
;
241 memset(p
, 0, padding
);
242 mctx
->block
[14] = mctx
->byte_count
<< 3;
243 mctx
->block
[15] = mctx
->byte_count
>> 29;
244 le32_to_cpu_array(mctx
->block
, (sizeof(mctx
->block
) -
245 sizeof(uint64_t)) / sizeof(uint32_t));
246 md4_transform(mctx
->hash
, mctx
->block
);
247 cpu_to_le32_array(mctx
->hash
, sizeof(mctx
->hash
) / sizeof(uint32_t));
249 snprintf(out
, len
, "%08X%08X%08X%08X",
250 mctx
->hash
[0], mctx
->hash
[1], mctx
->hash
[2], mctx
->hash
[3]);
253 static inline void add_char(unsigned char c
, struct md4_ctx
*md
)
255 md4_update(md
, &c
, 1);
258 static int parse_string(const char *file
, unsigned long len
,
263 add_char(file
[0], md
);
264 for (i
= 1; i
< len
; i
++) {
265 add_char(file
[i
], md
);
266 if (file
[i
] == '"' && file
[i
-1] != '\\')
272 static int parse_comment(const char *file
, unsigned long len
)
276 for (i
= 2; i
< len
; i
++) {
277 if (file
[i
-1] == '*' && file
[i
] == '/')
283 /* FIXME: Handle .s files differently (eg. # starts comments) --RR */
284 static int parse_file(const char *fname
, struct md4_ctx
*md
)
287 unsigned long i
, len
;
289 file
= grab_file(fname
, &len
);
293 for (i
= 0; i
< len
; i
++) {
294 /* Collapse and ignore \ and CR. */
295 if (file
[i
] == '\\' && (i
+1 < len
) && file
[i
+1] == '\n') {
300 /* Ignore whitespace */
301 if (isspace(file
[i
]))
304 /* Handle strings as whole units */
305 if (file
[i
] == '"') {
306 i
+= parse_string(file
+i
, len
- i
, md
);
310 /* Comments: ignore */
311 if (file
[i
] == '/' && file
[i
+1] == '*') {
312 i
+= parse_comment(file
+i
, len
- i
);
316 add_char(file
[i
], md
);
318 release_file(file
, len
);
322 /* We have dir/file.o. Open dir/.file.o.cmd, look for deps_ line to
323 * figure out source file. */
324 static int parse_source_files(const char *objfile
, struct md4_ctx
*md
)
326 char *cmd
, *file
, *line
, *dir
;
328 unsigned long flen
, pos
= 0;
329 int dirlen
, ret
= 0, check_files
= 0;
331 cmd
= NOFAIL(malloc(strlen(objfile
) + sizeof("..cmd")));
333 base
= strrchr(objfile
, '/');
336 dirlen
= base
- objfile
;
337 sprintf(cmd
, "%.*s.%s.cmd", dirlen
, objfile
, base
);
340 sprintf(cmd
, ".%s.cmd", objfile
);
342 dir
= NOFAIL(malloc(dirlen
+ 1));
343 strncpy(dir
, objfile
, dirlen
);
346 file
= grab_file(cmd
, &flen
);
348 fprintf(stderr
, "Warning: could not find %s for %s\n",
353 /* There will be a line like so:
354 deps_drivers/net/dummy.o := \
355 drivers/net/dummy.c \
356 $(wildcard include/config/net/fastroute.h) \
357 include/linux/config.h \
358 $(wildcard include/config/h.h) \
359 include/linux/module.h \
361 Sum all files in the same dir or subdirs.
363 while ((line
= get_next_line(&pos
, file
, flen
)) != NULL
) {
365 if (strncmp(line
, "deps_", sizeof("deps_")-1) == 0) {
372 /* Continue until line does not end with '\' */
373 if ( *(p
+ strlen(p
)-1) != '\\')
375 /* Terminate line at first space, to get rid of final ' \' */
384 /* Check if this file is in same dir as objfile */
385 if ((strstr(line
, dir
)+strlen(dir
)-1) == strrchr(line
, '/')) {
386 if (!parse_file(line
, md
)) {
388 "Warning: could not open %s: %s\n",
389 line
, strerror(errno
));
397 /* Everyone parsed OK */
400 release_file(file
, flen
);
407 static int get_version(const char *modname
, char sum
[])
413 char *sources
, *end
, *fname
;
414 const char *basename
;
415 char filelist
[sizeof(".tmp_versions/%s.mod") + strlen(modname
)];
417 /* Source files for module are in .tmp_versions/modname.mod,
418 after the first line. */
419 if (strrchr(modname
, '/'))
420 basename
= strrchr(modname
, '/') + 1;
423 sprintf(filelist
, ".tmp_versions/%s", basename
);
424 /* Truncate .o, add .mod */
425 strcpy(filelist
+ strlen(filelist
)-2, ".mod");
427 file
= grab_file(filelist
, &len
);
429 fprintf(stderr
, "Warning: could not find versions for %s\n",
434 sources
= strchr(file
, '\n');
436 fprintf(stderr
, "Warning: malformed versions file for %s\n",
442 end
= strchr(sources
, '\n');
444 fprintf(stderr
, "Warning: bad ending versions file for %s\n",
451 for (fname
= strtok(sources
, " "); fname
; fname
= strtok(NULL
, " ")) {
452 if (!parse_source_files(fname
, &md
))
456 /* sum is of form \0<padding>. */
457 md4_final_ascii(&md
, sum
, 1 + strlen(sum
+1));
460 release_file(file
, len
);
464 static void write_version(const char *filename
, const char *sum
,
465 unsigned long offset
)
469 fd
= open(filename
, O_RDWR
);
471 fprintf(stderr
, "Warning: changing sum in %s failed: %s\n",
472 filename
, strerror(errno
));
476 if (lseek(fd
, offset
, SEEK_SET
) == (off_t
)-1) {
477 fprintf(stderr
, "Warning: changing sum in %s:%lu failed: %s\n",
478 filename
, offset
, strerror(errno
));
482 if (write(fd
, sum
, strlen(sum
)+1) != strlen(sum
)+1) {
483 fprintf(stderr
, "Warning: writing sum in %s failed: %s\n",
484 filename
, strerror(errno
));
491 void strip_rcs_crap(char *version
)
493 unsigned int len
, full_len
;
495 if (strncmp(version
, "$Revision", strlen("$Revision")) != 0)
498 /* Space for version string follows. */
499 full_len
= strlen(version
) + strlen(version
+ strlen(version
) + 1) + 2;
501 /* Move string to start with version number: prefix will be
502 * $Revision$ or $Revision: */
503 len
= strlen("$Revision");
504 if (version
[len
] == ':' || version
[len
] == '$')
506 while (isspace(version
[len
]))
508 memmove(version
, version
+len
, full_len
-len
);
511 /* Preserve up to next whitespace. */
513 while (version
[len
] && !isspace(version
[len
]))
515 memmove(version
+ len
, version
+ strlen(version
),
516 full_len
- strlen(version
));
519 /* If the modinfo contains a "version" value, then set this. */
520 void maybe_frob_version(const char *modfilename
,
522 unsigned long modinfo_len
,
523 unsigned long modinfo_offset
)
525 char *version
, *csum
;
527 version
= get_modinfo(modinfo
, modinfo_len
, "version");
531 /* RCS $Revision gets stripped out. */
532 strip_rcs_crap(version
);
534 /* Check against double sumversion */
535 if (strchr(version
, ' '))
538 /* Version contains embedded NUL: second half has space for checksum */
539 csum
= version
+ strlen(version
);
541 if (get_version(modfilename
, csum
))
542 write_version(modfilename
, version
,
543 modinfo_offset
+ (version
- (char *)modinfo
));