blob: d587f40f11177ddb81f724757809c633b77195e5 [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>
Mike Frysingerfc31c772007-05-17 14:57:20 -040010#include <limits.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include "modpost.h"
12
13/*
14 * Stolen form Cryptographic API.
15 *
16 * MD4 Message Digest Algorithm (RFC1320).
17 *
18 * Implementation derived from Andrew Tridgell and Steve French's
19 * CIFS MD4 implementation, and the cryptoapi implementation
20 * originally based on the public domain implementation written
21 * by Colin Plumb in 1993.
22 *
23 * Copyright (c) Andrew Tridgell 1997-1998.
24 * Modified by Steve French (sfrench@us.ibm.com) 2002
25 * Copyright (c) Cryptoapi developers.
26 * Copyright (c) 2002 David S. Miller (davem@redhat.com)
27 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
28 *
29 * This program is free software; you can redistribute it and/or modify
30 * it under the terms of the GNU General Public License as published by
31 * the Free Software Foundation; either version 2 of the License, or
32 * (at your option) any later version.
33 *
34 */
35#define MD4_DIGEST_SIZE 16
36#define MD4_HMAC_BLOCK_SIZE 64
37#define MD4_BLOCK_WORDS 16
38#define MD4_HASH_WORDS 4
39
40struct md4_ctx {
41 uint32_t hash[MD4_HASH_WORDS];
42 uint32_t block[MD4_BLOCK_WORDS];
43 uint64_t byte_count;
44};
45
46static inline uint32_t lshift(uint32_t x, unsigned int s)
47{
48 x &= 0xFFFFFFFF;
49 return ((x << s) & 0xFFFFFFFF) | (x >> (32 - s));
50}
51
52static inline uint32_t F(uint32_t x, uint32_t y, uint32_t z)
53{
54 return (x & y) | ((~x) & z);
55}
56
57static inline uint32_t G(uint32_t x, uint32_t y, uint32_t z)
58{
59 return (x & y) | (x & z) | (y & z);
60}
61
62static inline uint32_t H(uint32_t x, uint32_t y, uint32_t z)
63{
64 return x ^ y ^ z;
65}
66
67#define ROUND1(a,b,c,d,k,s) (a = lshift(a + F(b,c,d) + k, s))
68#define ROUND2(a,b,c,d,k,s) (a = lshift(a + G(b,c,d) + k + (uint32_t)0x5A827999,s))
69#define ROUND3(a,b,c,d,k,s) (a = lshift(a + H(b,c,d) + k + (uint32_t)0x6ED9EBA1,s))
70
71/* XXX: this stuff can be optimized */
72static inline void le32_to_cpu_array(uint32_t *buf, unsigned int words)
73{
74 while (words--) {
75 *buf = ntohl(*buf);
76 buf++;
77 }
78}
79
80static inline void cpu_to_le32_array(uint32_t *buf, unsigned int words)
81{
82 while (words--) {
83 *buf = htonl(*buf);
84 buf++;
85 }
86}
87
88static void md4_transform(uint32_t *hash, uint32_t const *in)
89{
90 uint32_t a, b, c, d;
91
92 a = hash[0];
93 b = hash[1];
94 c = hash[2];
95 d = hash[3];
96
97 ROUND1(a, b, c, d, in[0], 3);
98 ROUND1(d, a, b, c, in[1], 7);
99 ROUND1(c, d, a, b, in[2], 11);
100 ROUND1(b, c, d, a, in[3], 19);
101 ROUND1(a, b, c, d, in[4], 3);
102 ROUND1(d, a, b, c, in[5], 7);
103 ROUND1(c, d, a, b, in[6], 11);
104 ROUND1(b, c, d, a, in[7], 19);
105 ROUND1(a, b, c, d, in[8], 3);
106 ROUND1(d, a, b, c, in[9], 7);
107 ROUND1(c, d, a, b, in[10], 11);
108 ROUND1(b, c, d, a, in[11], 19);
109 ROUND1(a, b, c, d, in[12], 3);
110 ROUND1(d, a, b, c, in[13], 7);
111 ROUND1(c, d, a, b, in[14], 11);
112 ROUND1(b, c, d, a, in[15], 19);
113
114 ROUND2(a, b, c, d,in[ 0], 3);
115 ROUND2(d, a, b, c, in[4], 5);
116 ROUND2(c, d, a, b, in[8], 9);
117 ROUND2(b, c, d, a, in[12], 13);
118 ROUND2(a, b, c, d, in[1], 3);
119 ROUND2(d, a, b, c, in[5], 5);
120 ROUND2(c, d, a, b, in[9], 9);
121 ROUND2(b, c, d, a, in[13], 13);
122 ROUND2(a, b, c, d, in[2], 3);
123 ROUND2(d, a, b, c, in[6], 5);
124 ROUND2(c, d, a, b, in[10], 9);
125 ROUND2(b, c, d, a, in[14], 13);
126 ROUND2(a, b, c, d, in[3], 3);
127 ROUND2(d, a, b, c, in[7], 5);
128 ROUND2(c, d, a, b, in[11], 9);
129 ROUND2(b, c, d, a, in[15], 13);
130
131 ROUND3(a, b, c, d,in[ 0], 3);
132 ROUND3(d, a, b, c, in[8], 9);
133 ROUND3(c, d, a, b, in[4], 11);
134 ROUND3(b, c, d, a, in[12], 15);
135 ROUND3(a, b, c, d, in[2], 3);
136 ROUND3(d, a, b, c, in[10], 9);
137 ROUND3(c, d, a, b, in[6], 11);
138 ROUND3(b, c, d, a, in[14], 15);
139 ROUND3(a, b, c, d, in[1], 3);
140 ROUND3(d, a, b, c, in[9], 9);
141 ROUND3(c, d, a, b, in[5], 11);
142 ROUND3(b, c, d, a, in[13], 15);
143 ROUND3(a, b, c, d, in[3], 3);
144 ROUND3(d, a, b, c, in[11], 9);
145 ROUND3(c, d, a, b, in[7], 11);
146 ROUND3(b, c, d, a, in[15], 15);
147
148 hash[0] += a;
149 hash[1] += b;
150 hash[2] += c;
151 hash[3] += d;
152}
153
154static inline void md4_transform_helper(struct md4_ctx *ctx)
155{
156 le32_to_cpu_array(ctx->block, sizeof(ctx->block) / sizeof(uint32_t));
157 md4_transform(ctx->hash, ctx->block);
158}
159
160static void md4_init(struct md4_ctx *mctx)
161{
162 mctx->hash[0] = 0x67452301;
163 mctx->hash[1] = 0xefcdab89;
164 mctx->hash[2] = 0x98badcfe;
165 mctx->hash[3] = 0x10325476;
166 mctx->byte_count = 0;
167}
168
169static void md4_update(struct md4_ctx *mctx,
170 const unsigned char *data, unsigned int len)
171{
172 const uint32_t avail = sizeof(mctx->block) - (mctx->byte_count & 0x3f);
173
174 mctx->byte_count += len;
175
176 if (avail > len) {
177 memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
178 data, len);
179 return;
180 }
181
182 memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
183 data, avail);
184
185 md4_transform_helper(mctx);
186 data += avail;
187 len -= avail;
188
189 while (len >= sizeof(mctx->block)) {
190 memcpy(mctx->block, data, sizeof(mctx->block));
191 md4_transform_helper(mctx);
192 data += sizeof(mctx->block);
193 len -= sizeof(mctx->block);
194 }
195
196 memcpy(mctx->block, data, len);
197}
198
199static void md4_final_ascii(struct md4_ctx *mctx, char *out, unsigned int len)
200{
201 const unsigned int offset = mctx->byte_count & 0x3f;
202 char *p = (char *)mctx->block + offset;
203 int padding = 56 - (offset + 1);
204
205 *p++ = 0x80;
206 if (padding < 0) {
207 memset(p, 0x00, padding + sizeof (uint64_t));
208 md4_transform_helper(mctx);
209 p = (char *)mctx->block;
210 padding = 56;
211 }
212
213 memset(p, 0, padding);
214 mctx->block[14] = mctx->byte_count << 3;
215 mctx->block[15] = mctx->byte_count >> 29;
216 le32_to_cpu_array(mctx->block, (sizeof(mctx->block) -
Masahiro Yamadabb66fc62014-06-10 19:08:13 +0900217 sizeof(uint64_t)) / sizeof(uint32_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 md4_transform(mctx->hash, mctx->block);
219 cpu_to_le32_array(mctx->hash, sizeof(mctx->hash) / sizeof(uint32_t));
220
221 snprintf(out, len, "%08X%08X%08X%08X",
222 mctx->hash[0], mctx->hash[1], mctx->hash[2], mctx->hash[3]);
223}
224
225static inline void add_char(unsigned char c, struct md4_ctx *md)
226{
227 md4_update(md, &c, 1);
228}
229
230static int parse_string(const char *file, unsigned long len,
231 struct md4_ctx *md)
232{
233 unsigned long i;
234
235 add_char(file[0], md);
236 for (i = 1; i < len; i++) {
237 add_char(file[i], md);
238 if (file[i] == '"' && file[i-1] != '\\')
239 break;
240 }
241 return i;
242}
243
244static int parse_comment(const char *file, unsigned long len)
245{
246 unsigned long i;
247
248 for (i = 2; i < len; i++) {
249 if (file[i-1] == '*' && file[i] == '/')
250 break;
251 }
252 return i;
253}
254
255/* FIXME: Handle .s files differently (eg. # starts comments) --RR */
J.A. Magallon61d9cdf2005-07-15 22:14:43 +0000256static int parse_file(const char *fname, struct md4_ctx *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
J.A. Magallon61d9cdf2005-07-15 22:14:43 +0000258 char *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 unsigned long i, len;
260
Masahiro Yamada7c8f5662020-06-01 14:57:19 +0900261 file = read_text_file(fname);
262 len = strlen(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
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 }
Masahiro Yamada7c8f5662020-06-01 14:57:19 +0900289 free(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 return 1;
291}
Ashutosh Naike6e67332008-10-25 15:02:53 -0700292/* Check whether the file is a static library or not */
293static int is_static_library(const char *objfile)
294{
295 int len = strlen(objfile);
296 if (objfile[len - 2] == '.' && objfile[len - 1] == 'a')
297 return 1;
298 else
299 return 0;
300}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Michal Marek7840fea2011-03-11 22:34:47 +0100302/* We have dir/file.o. Open dir/.file.o.cmd, look for source_ and deps_ line
303 * to figure out source files. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304static int parse_source_files(const char *objfile, struct md4_ctx *md)
305{
Masahiro Yamada70f30cf2020-06-01 14:57:20 +0900306 char *cmd, *file, *line, *dir, *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 const char *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 int dirlen, ret = 0, check_files = 0;
309
310 cmd = NOFAIL(malloc(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 = NOFAIL(malloc(dirlen + 1));
322 strncpy(dir, objfile, dirlen);
323 dir[dirlen] = '\0';
324
Masahiro Yamada70f30cf2020-06-01 14:57:20 +0900325 file = read_text_file(cmd);
326
327 pos = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Rasmus Villemoes9fc34762018-03-22 22:05:23 +0100329 /* Sum all files in the same dir or subdirs. */
Masahiro Yamada70f30cf2020-06-01 14:57:20 +0900330 while ((line = get_line(&pos))) {
J.A. Magallon61d9cdf2005-07-15 22:14:43 +0000331 char* p = line;
Michal Marek7840fea2011-03-11 22:34:47 +0100332
333 if (strncmp(line, "source_", sizeof("source_")-1) == 0) {
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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 if (strncmp(line, "deps_", sizeof("deps_")-1) == 0) {
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) {
Masahiro Yamadabb66fc62014-06-10 19:08:13 +0900359 if (isspace(*p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 *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)) {
Sam Ravnborgcb805142006-01-28 16:57:26 +0100369 warn("could not open %s: %s\n",
370 line, strerror(errno));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 goto out_file;
372 }
373
374 }
375
376 }
377
378 /* Everyone parsed OK */
379 ret = 1;
380out_file:
Masahiro Yamada70f30cf2020-06-01 14:57:20 +0900381 free(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 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{
Masahiro Yamadaf531c1b2020-06-01 14:57:18 +0900390 char *buf, *pos, *firstline;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 struct md4_ctx md;
Masahiro Yamadaf531c1b2020-06-01 14:57:18 +0900392 char *fname;
Sam Ravnborg41370d32006-02-19 00:49:37 +0100393 char filelist[PATH_MAX + 1];
Sam Ravnborg41370d32006-02-19 00:49:37 +0100394
Masahiro Yamada60ae1b12019-07-17 15:17:58 +0900395 /* objects for a module are listed in the first line of *.mod file. */
Masahiro Yamadab7dca6d2019-07-17 15:17:57 +0900396 snprintf(filelist, sizeof(filelist), "%.*smod",
397 (int)strlen(modname) - 1, modname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Masahiro Yamadaf531c1b2020-06-01 14:57:18 +0900399 buf = read_text_file(filelist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Masahiro Yamadaf531c1b2020-06-01 14:57:18 +0900401 pos = buf;
402 firstline = get_line(&pos);
403 if (!firstline) {
Sam Ravnborgcb805142006-01-28 16:57:26 +0100404 warn("bad ending versions file for %s\n", modname);
Masahiro Yamadaf531c1b2020-06-01 14:57:18 +0900405 goto free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408 md4_init(&md);
Masahiro Yamadaf531c1b2020-06-01 14:57:18 +0900409 while ((fname = strsep(&firstline, " "))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 if (!*fname)
411 continue;
Ashutosh Naike6e67332008-10-25 15:02:53 -0700412 if (!(is_static_library(fname)) &&
413 !parse_source_files(fname, &md))
Masahiro Yamadaf531c1b2020-06-01 14:57:18 +0900414 goto free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 }
416
417 md4_final_ascii(&md, sum, sumlen);
Masahiro Yamadaf531c1b2020-06-01 14:57:18 +0900418free:
419 free(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420}