1 #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 <limits.h>
11
12 #include <xalloc.h>
13 #include "modpost.h"
14
15 /*
16 * Stolen form Cryptographic API.
17 *
18 * MD4 Message Digest Algorithm (RFC1320).
19 *
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.
24 *
25 * Copyright (c) Andrew Tridgell 1997-1998.
26 * Modified by Steve French (sfrench@us.ibm.com) 2002
27 * Copyright (c) Cryptoapi developers.
28 * Copyright (c) 2002 David S. Miller (davem@redhat.com)
29 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
30 *
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.
35 *
36 */
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
41
42 struct md4_ctx {
43 uint32_t hash[MD4_HASH_WORDS];
44 uint32_t block[MD4_BLOCK_WORDS];
45 uint64_t byte_count;
46 };
47
lshift(uint32_t x,unsigned int s)48 static inline uint32_t lshift(uint32_t x, unsigned int s)
49 {
50 x &= 0xFFFFFFFF;
51 return ((x << s) & 0xFFFFFFFF) | (x >> (32 - s));
52 }
53
F(uint32_t x,uint32_t y,uint32_t z)54 static inline uint32_t F(uint32_t x, uint32_t y, uint32_t z)
55 {
56 return (x & y) | ((~x) & z);
57 }
58
G(uint32_t x,uint32_t y,uint32_t z)59 static inline uint32_t G(uint32_t x, uint32_t y, uint32_t z)
60 {
61 return (x & y) | (x & z) | (y & z);
62 }
63
H(uint32_t x,uint32_t y,uint32_t z)64 static inline uint32_t H(uint32_t x, uint32_t y, uint32_t z)
65 {
66 return x ^ y ^ z;
67 }
68
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))
72
73 /* XXX: this stuff can be optimized */
le32_to_cpu_array(uint32_t * buf,unsigned int words)74 static inline void le32_to_cpu_array(uint32_t *buf, unsigned int words)
75 {
76 while (words--) {
77 *buf = ntohl(*buf);
78 buf++;
79 }
80 }
81
cpu_to_le32_array(uint32_t * buf,unsigned int words)82 static inline void cpu_to_le32_array(uint32_t *buf, unsigned int words)
83 {
84 while (words--) {
85 *buf = htonl(*buf);
86 buf++;
87 }
88 }
89
md4_transform(uint32_t * hash,uint32_t const * in)90 static void md4_transform(uint32_t *hash, uint32_t const *in)
91 {
92 uint32_t a, b, c, d;
93
94 a = hash[0];
95 b = hash[1];
96 c = hash[2];
97 d = hash[3];
98
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);
115
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);
132
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);
149
150 hash[0] += a;
151 hash[1] += b;
152 hash[2] += c;
153 hash[3] += d;
154 }
155
md4_transform_helper(struct md4_ctx * ctx)156 static inline void md4_transform_helper(struct md4_ctx *ctx)
157 {
158 le32_to_cpu_array(ctx->block, ARRAY_SIZE(ctx->block));
159 md4_transform(ctx->hash, ctx->block);
160 }
161
md4_init(struct md4_ctx * mctx)162 static void md4_init(struct md4_ctx *mctx)
163 {
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;
169 }
170
md4_update(struct md4_ctx * mctx,const unsigned char * data,unsigned int len)171 static void md4_update(struct md4_ctx *mctx,
172 const unsigned char *data, unsigned int len)
173 {
174 const uint32_t avail = sizeof(mctx->block) - (mctx->byte_count & 0x3f);
175
176 mctx->byte_count += len;
177
178 if (avail > len) {
179 memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
180 data, len);
181 return;
182 }
183
184 memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
185 data, avail);
186
187 md4_transform_helper(mctx);
188 data += avail;
189 len -= avail;
190
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);
196 }
197
198 memcpy(mctx->block, data, len);
199 }
200
md4_final_ascii(struct md4_ctx * mctx,char * out,unsigned int len)201 static void md4_final_ascii(struct md4_ctx *mctx, char *out, unsigned int len)
202 {
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 (uint64_t));
210 md4_transform_helper(mctx);
211 p = (char *)mctx->block;
212 padding = 56;
213 }
214
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));
222
223 snprintf(out, len, "%08X%08X%08X%08X",
224 mctx->hash[0], mctx->hash[1], mctx->hash[2], mctx->hash[3]);
225 }
226
add_char(unsigned char c,struct md4_ctx * md)227 static inline void add_char(unsigned char c, struct md4_ctx *md)
228 {
229 md4_update(md, &c, 1);
230 }
231
parse_string(const char * file,unsigned long len,struct md4_ctx * md)232 static int parse_string(const char *file, unsigned long len,
233 struct md4_ctx *md)
234 {
235 unsigned long i;
236
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] != '\\')
241 break;
242 }
243 return i;
244 }
245
parse_comment(const char * file,unsigned long len)246 static int parse_comment(const char *file, unsigned long len)
247 {
248 unsigned long i;
249
250 for (i = 2; i < len; i++) {
251 if (file[i-1] == '*' && file[i] == '/')
252 break;
253 }
254 return i;
255 }
256
257 /* FIXME: Handle .s files differently (eg. # starts comments) --RR */
parse_file(const char * fname,struct md4_ctx * md)258 static int parse_file(const char *fname, struct md4_ctx *md)
259 {
260 char *file;
261 unsigned long i, len;
262
263 file = read_text_file(fname);
264 len = strlen(file);
265
266 for (i = 0; i < len; i++) {
267 /* Collapse and ignore \ and CR. */
268 if (file[i] == '\\' && (i+1 < len) && file[i+1] == '\n') {
269 i++;
270 continue;
271 }
272
273 /* Ignore whitespace */
274 if (isspace(file[i]))
275 continue;
276
277 /* Handle strings as whole units */
278 if (file[i] == '"') {
279 i += parse_string(file+i, len - i, md);
280 continue;
281 }
282
283 /* Comments: ignore */
284 if (file[i] == '/' && file[i+1] == '*') {
285 i += parse_comment(file+i, len - i);
286 continue;
287 }
288
289 add_char(file[i], md);
290 }
291 free(file);
292 return 1;
293 }
294 /* Check whether the file is a static library or not */
is_static_library(const char * objfile)295 static bool is_static_library(const char *objfile)
296 {
297 int len = strlen(objfile);
298
299 return objfile[len - 2] == '.' && objfile[len - 1] == 'a';
300 }
301
302 /* We have dir/file.o. Open dir/.file.o.cmd, look for source_ and deps_ line
303 * to figure out source files. */
parse_source_files(const char * objfile,struct md4_ctx * md)304 static int parse_source_files(const char *objfile, struct md4_ctx *md)
305 {
306 char *cmd, *file, *line, *dir, *pos;
307 const char *base;
308 int dirlen, ret = 0, check_files = 0;
309
310 cmd = xmalloc(strlen(objfile) + sizeof("..cmd"));
311
312 base = strrchr(objfile, '/');
313 if (base) {
314 base++;
315 dirlen = base - objfile;
316 sprintf(cmd, "%.*s.%s.cmd", dirlen, objfile, base);
317 } else {
318 dirlen = 0;
319 sprintf(cmd, ".%s.cmd", objfile);
320 }
321 dir = xmalloc(dirlen + 1);
322 strncpy(dir, objfile, dirlen);
323 dir[dirlen] = '\0';
324
325 file = read_text_file(cmd);
326
327 pos = file;
328
329 /* Sum all files in the same dir or subdirs. */
330 while ((line = get_line(&pos))) {
331 char* p;
332
333 /* trim the leading spaces away */
334 while (isspace(*line))
335 line++;
336 p = line;
337
338 if (strncmp(line, "source_", sizeof("source_")-1) == 0) {
339 p = strrchr(line, ' ');
340 if (!p) {
341 warn("malformed line: %s\n", line);
342 goto out_file;
343 }
344 p++;
345 if (!parse_file(p, md)) {
346 warn("could not open %s: %s\n",
347 p, strerror(errno));
348 goto out_file;
349 }
350 continue;
351 }
352 if (strncmp(line, "deps_", sizeof("deps_")-1) == 0) {
353 check_files = 1;
354 continue;
355 }
356 if (!check_files)
357 continue;
358
359 /* Continue until line does not end with '\' */
360 if ( *(p + strlen(p)-1) != '\\')
361 break;
362 /* Terminate line at first space, to get rid of final ' \' */
363 while (*p) {
364 if (isspace(*p)) {
365 *p = '\0';
366 break;
367 }
368 p++;
369 }
370
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));
376 goto out_file;
377 }
378
379 }
380
381 }
382
383 /* Everyone parsed OK */
384 ret = 1;
385 out_file:
386 free(file);
387 free(dir);
388 free(cmd);
389 return ret;
390 }
391
392 /* Calc and record src checksum. */
get_src_version(const char * modname,char sum[],unsigned sumlen)393 void get_src_version(const char *modname, char sum[], unsigned sumlen)
394 {
395 char *buf, *pos;
396 struct md4_ctx md;
397 char *fname;
398 char filelist[PATH_MAX + 1];
399
400 /* objects for a module are listed in the first line of *.mod file. */
401 snprintf(filelist, sizeof(filelist), "%s.mod", modname);
402
403 buf = read_text_file(filelist);
404 pos = buf;
405
406 md4_init(&md);
407 while ((fname = strsep(&pos, "\n"))) {
408 if (!*fname)
409 continue;
410 if (!(is_static_library(fname)) &&
411 !parse_source_files(fname, &md))
412 goto free;
413 }
414
415 md4_final_ascii(&md, sum, sumlen);
416 free:
417 free(buf);
418 }
419