sumversion.c 11 KB

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