Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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
42struct md4_ctx {
43 uint32_t hash[MD4_HASH_WORDS];
44 uint32_t block[MD4_BLOCK_WORDS];
45 uint64_t byte_count;
46};
47
48static inline uint32_t lshift(uint32_t x, unsigned int s)
49{
50 x &= 0xFFFFFFFF;
51 return ((x << s) & 0xFFFFFFFF) | (x >> (32 - s));
52}
53
54static inline uint32_t F(uint32_t x, uint32_t y, uint32_t z)
55{
56 return (x & y) | ((~x) & z);
57}
58
59static inline uint32_t G(uint32_t x, uint32_t y, uint32_t z)
60{
61 return (x & y) | (x & z) | (y & z);
62}
63
64static 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 */
74static 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
82static 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
90static 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
156static 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
162static 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
171static 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
201static 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
227static inline void add_char(unsigned char c, struct md4_ctx *md)
228{
229 md4_update(md, &c, 1);
230}
231
232static 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
246static 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 */
258static 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 */
295static 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. */
304static 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 = get_basename(objfile);
313 dirlen = base - objfile;
314 sprintf(cmd, "%.*s.%s.cmd", dirlen, objfile, base);
315
316 dir = xmalloc(dirlen + 1);
317 strncpy(dir, objfile, dirlen);
318 dir[dirlen] = '\0';
319
320 file = read_text_file(cmd);
321
322 pos = file;
323
324 /* Sum all files in the same dir or subdirs. */
325 while ((line = get_line(&pos))) {
326 char* p;
327
328 /* trim the leading spaces away */
329 while (isspace(*line))
330 line++;
331 p = line;
332
333 if (strstarts(line, "source_")) {
334 p = strrchr(line, ' ');
335 if (!p) {
336 warn("malformed line: %s\n", line);
337 goto out_file;
338 }
339 p++;
340 if (!parse_file(p, md)) {
341 warn("could not open %s: %s\n",
342 p, strerror(errno));
343 goto out_file;
344 }
345 continue;
346 }
347 if (strstarts(line, "deps_")) {
348 check_files = 1;
349 continue;
350 }
351 if (!check_files)
352 continue;
353
354 /* Continue until line does not end with '\' */
355 if ( *(p + strlen(p)-1) != '\\')
356 break;
357 /* Terminate line at first space, to get rid of final ' \' */
358 while (*p) {
359 if (isspace(*p)) {
360 *p = '\0';
361 break;
362 }
363 p++;
364 }
365
366 /* Check if this file is in same dir as objfile */
367 if ((strstr(line, dir)+strlen(dir)-1) == strrchr(line, '/')) {
368 if (!parse_file(line, md)) {
369 warn("could not open %s: %s\n",
370 line, strerror(errno));
371 goto out_file;
372 }
373
374 }
375
376 }
377
378 /* Everyone parsed OK */
379 ret = 1;
380out_file:
381 free(file);
382 free(dir);
383 free(cmd);
384 return ret;
385}
386
387/* Calc and record src checksum. */
388void get_src_version(const char *modname, char sum[], unsigned sumlen)
389{
390 char *buf, *pos;
391 struct md4_ctx md;
392 char *fname;
393 char filelist[PATH_MAX + 1];
394
395 /* objects for a module are listed in the first line of *.mod file. */
396 snprintf(filelist, sizeof(filelist), "%s.mod", modname);
397
398 buf = read_text_file(filelist);
399 pos = buf;
400
401 md4_init(&md);
402 while ((fname = strsep(&pos, "\n"))) {
403 if (!*fname)
404 continue;
405 if (!(is_static_library(fname)) &&
406 !parse_source_files(fname, &md))
407 goto free;
408 }
409
410 md4_final_ascii(&md, sum, sumlen);
411free:
412 free(buf);
413}