sumversion.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. #include "modpost.h"
  12. /*
  13. * Stolen form Cryptographic API.
  14. *
  15. * MD4 Message Digest Algorithm (RFC1320).
  16. *
  17. * Implementation derived from Andrew Tridgell and Steve French's
  18. * CIFS MD4 implementation, and the cryptoapi implementation
  19. * originally based on the public domain implementation written
  20. * by Colin Plumb in 1993.
  21. *
  22. * Copyright (c) Andrew Tridgell 1997-1998.
  23. * Modified by Steve French ([email protected]) 2002
  24. * Copyright (c) Cryptoapi developers.
  25. * Copyright (c) 2002 David S. Miller ([email protected])
  26. * Copyright (c) 2002 James Morris <[email protected]>
  27. *
  28. * This program is free software; you can redistribute it and/or modify
  29. * it under the terms of the GNU General Public License as published by
  30. * the Free Software Foundation; either version 2 of the License, or
  31. * (at your option) any later version.
  32. *
  33. */
  34. #define MD4_DIGEST_SIZE 16
  35. #define MD4_HMAC_BLOCK_SIZE 64
  36. #define MD4_BLOCK_WORDS 16
  37. #define MD4_HASH_WORDS 4
  38. struct md4_ctx {
  39. uint32_t hash[MD4_HASH_WORDS];
  40. uint32_t block[MD4_BLOCK_WORDS];
  41. uint64_t byte_count;
  42. };
  43. static inline uint32_t lshift(uint32_t x, unsigned int s)
  44. {
  45. x &= 0xFFFFFFFF;
  46. return ((x << s) & 0xFFFFFFFF) | (x >> (32 - s));
  47. }
  48. static inline uint32_t F(uint32_t x, uint32_t y, uint32_t z)
  49. {
  50. return (x & y) | ((~x) & z);
  51. }
  52. static inline uint32_t G(uint32_t x, uint32_t y, uint32_t z)
  53. {
  54. return (x & y) | (x & z) | (y & z);
  55. }
  56. static inline uint32_t H(uint32_t x, uint32_t y, uint32_t z)
  57. {
  58. return x ^ y ^ z;
  59. }
  60. #define ROUND1(a,b,c,d,k,s) (a = lshift(a + F(b,c,d) + k, s))
  61. #define ROUND2(a,b,c,d,k,s) (a = lshift(a + G(b,c,d) + k + (uint32_t)0x5A827999,s))
  62. #define ROUND3(a,b,c,d,k,s) (a = lshift(a + H(b,c,d) + k + (uint32_t)0x6ED9EBA1,s))
  63. /* XXX: this stuff can be optimized */
  64. static inline void le32_to_cpu_array(uint32_t *buf, unsigned int words)
  65. {
  66. while (words--) {
  67. *buf = ntohl(*buf);
  68. buf++;
  69. }
  70. }
  71. static inline void cpu_to_le32_array(uint32_t *buf, unsigned int words)
  72. {
  73. while (words--) {
  74. *buf = htonl(*buf);
  75. buf++;
  76. }
  77. }
  78. static void md4_transform(uint32_t *hash, uint32_t const *in)
  79. {
  80. uint32_t a, b, c, d;
  81. a = hash[0];
  82. b = hash[1];
  83. c = hash[2];
  84. d = hash[3];
  85. ROUND1(a, b, c, d, in[0], 3);
  86. ROUND1(d, a, b, c, in[1], 7);
  87. ROUND1(c, d, a, b, in[2], 11);
  88. ROUND1(b, c, d, a, in[3], 19);
  89. ROUND1(a, b, c, d, in[4], 3);
  90. ROUND1(d, a, b, c, in[5], 7);
  91. ROUND1(c, d, a, b, in[6], 11);
  92. ROUND1(b, c, d, a, in[7], 19);
  93. ROUND1(a, b, c, d, in[8], 3);
  94. ROUND1(d, a, b, c, in[9], 7);
  95. ROUND1(c, d, a, b, in[10], 11);
  96. ROUND1(b, c, d, a, in[11], 19);
  97. ROUND1(a, b, c, d, in[12], 3);
  98. ROUND1(d, a, b, c, in[13], 7);
  99. ROUND1(c, d, a, b, in[14], 11);
  100. ROUND1(b, c, d, a, in[15], 19);
  101. ROUND2(a, b, c, d,in[ 0], 3);
  102. ROUND2(d, a, b, c, in[4], 5);
  103. ROUND2(c, d, a, b, in[8], 9);
  104. ROUND2(b, c, d, a, in[12], 13);
  105. ROUND2(a, b, c, d, in[1], 3);
  106. ROUND2(d, a, b, c, in[5], 5);
  107. ROUND2(c, d, a, b, in[9], 9);
  108. ROUND2(b, c, d, a, in[13], 13);
  109. ROUND2(a, b, c, d, in[2], 3);
  110. ROUND2(d, a, b, c, in[6], 5);
  111. ROUND2(c, d, a, b, in[10], 9);
  112. ROUND2(b, c, d, a, in[14], 13);
  113. ROUND2(a, b, c, d, in[3], 3);
  114. ROUND2(d, a, b, c, in[7], 5);
  115. ROUND2(c, d, a, b, in[11], 9);
  116. ROUND2(b, c, d, a, in[15], 13);
  117. ROUND3(a, b, c, d,in[ 0], 3);
  118. ROUND3(d, a, b, c, in[8], 9);
  119. ROUND3(c, d, a, b, in[4], 11);
  120. ROUND3(b, c, d, a, in[12], 15);
  121. ROUND3(a, b, c, d, in[2], 3);
  122. ROUND3(d, a, b, c, in[10], 9);
  123. ROUND3(c, d, a, b, in[6], 11);
  124. ROUND3(b, c, d, a, in[14], 15);
  125. ROUND3(a, b, c, d, in[1], 3);
  126. ROUND3(d, a, b, c, in[9], 9);
  127. ROUND3(c, d, a, b, in[5], 11);
  128. ROUND3(b, c, d, a, in[13], 15);
  129. ROUND3(a, b, c, d, in[3], 3);
  130. ROUND3(d, a, b, c, in[11], 9);
  131. ROUND3(c, d, a, b, in[7], 11);
  132. ROUND3(b, c, d, a, in[15], 15);
  133. hash[0] += a;
  134. hash[1] += b;
  135. hash[2] += c;
  136. hash[3] += d;
  137. }
  138. static inline void md4_transform_helper(struct md4_ctx *ctx)
  139. {
  140. le32_to_cpu_array(ctx->block, sizeof(ctx->block) / sizeof(uint32_t));
  141. md4_transform(ctx->hash, ctx->block);
  142. }
  143. static void md4_init(struct md4_ctx *mctx)
  144. {
  145. mctx->hash[0] = 0x67452301;
  146. mctx->hash[1] = 0xefcdab89;
  147. mctx->hash[2] = 0x98badcfe;
  148. mctx->hash[3] = 0x10325476;
  149. mctx->byte_count = 0;
  150. }
  151. static void md4_update(struct md4_ctx *mctx,
  152. const unsigned char *data, unsigned int len)
  153. {
  154. const uint32_t avail = sizeof(mctx->block) - (mctx->byte_count & 0x3f);
  155. mctx->byte_count += len;
  156. if (avail > len) {
  157. memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
  158. data, len);
  159. return;
  160. }
  161. memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
  162. data, avail);
  163. md4_transform_helper(mctx);
  164. data += avail;
  165. len -= avail;
  166. while (len >= sizeof(mctx->block)) {
  167. memcpy(mctx->block, data, sizeof(mctx->block));
  168. md4_transform_helper(mctx);
  169. data += sizeof(mctx->block);
  170. len -= sizeof(mctx->block);
  171. }
  172. memcpy(mctx->block, data, len);
  173. }
  174. static void md4_final_ascii(struct md4_ctx *mctx, char *out, unsigned int len)
  175. {
  176. const unsigned int offset = mctx->byte_count & 0x3f;
  177. char *p = (char *)mctx->block + offset;
  178. int padding = 56 - (offset + 1);
  179. *p++ = 0x80;
  180. if (padding < 0) {
  181. memset(p, 0x00, padding + sizeof (uint64_t));
  182. md4_transform_helper(mctx);
  183. p = (char *)mctx->block;
  184. padding = 56;
  185. }
  186. memset(p, 0, padding);
  187. mctx->block[14] = mctx->byte_count << 3;
  188. mctx->block[15] = mctx->byte_count >> 29;
  189. le32_to_cpu_array(mctx->block, (sizeof(mctx->block) -
  190. sizeof(uint64_t)) / sizeof(uint32_t));
  191. md4_transform(mctx->hash, mctx->block);
  192. cpu_to_le32_array(mctx->hash, sizeof(mctx->hash) / sizeof(uint32_t));
  193. snprintf(out, len, "%08X%08X%08X%08X",
  194. mctx->hash[0], mctx->hash[1], mctx->hash[2], mctx->hash[3]);
  195. }
  196. static inline void add_char(unsigned char c, struct md4_ctx *md)
  197. {
  198. md4_update(md, &c, 1);
  199. }
  200. static int parse_string(const char *file, unsigned long len,
  201. struct md4_ctx *md)
  202. {
  203. unsigned long i;
  204. add_char(file[0], md);
  205. for (i = 1; i < len; i++) {
  206. add_char(file[i], md);
  207. if (file[i] == '"' && file[i-1] != '\\')
  208. break;
  209. }
  210. return i;
  211. }
  212. static int parse_comment(const char *file, unsigned long len)
  213. {
  214. unsigned long i;
  215. for (i = 2; i < len; i++) {
  216. if (file[i-1] == '*' && file[i] == '/')
  217. break;
  218. }
  219. return i;
  220. }
  221. /* FIXME: Handle .s files differently (eg. # starts comments) --RR */
  222. static int parse_file(const char *fname, struct md4_ctx *md)
  223. {
  224. char *file;
  225. unsigned long i, len;
  226. file = read_text_file(fname);
  227. len = strlen(file);
  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. free(file);
  250. return 1;
  251. }
  252. /* Check whether the file is a static library or not */
  253. static bool is_static_library(const char *objfile)
  254. {
  255. int len = strlen(objfile);
  256. return objfile[len - 2] == '.' && objfile[len - 1] == 'a';
  257. }
  258. /* We have dir/file.o. Open dir/.file.o.cmd, look for source_ and deps_ line
  259. * to figure out source files. */
  260. static int parse_source_files(const char *objfile, struct md4_ctx *md)
  261. {
  262. char *cmd, *file, *line, *dir, *pos;
  263. const char *base;
  264. int dirlen, ret = 0, check_files = 0;
  265. cmd = NOFAIL(malloc(strlen(objfile) + sizeof("..cmd")));
  266. base = strrchr(objfile, '/');
  267. if (base) {
  268. base++;
  269. dirlen = base - objfile;
  270. sprintf(cmd, "%.*s.%s.cmd", dirlen, objfile, base);
  271. } else {
  272. dirlen = 0;
  273. sprintf(cmd, ".%s.cmd", objfile);
  274. }
  275. dir = NOFAIL(malloc(dirlen + 1));
  276. strncpy(dir, objfile, dirlen);
  277. dir[dirlen] = '\0';
  278. file = read_text_file(cmd);
  279. pos = file;
  280. /* Sum all files in the same dir or subdirs. */
  281. while ((line = get_line(&pos))) {
  282. char* p = line;
  283. if (strncmp(line, "source_", sizeof("source_")-1) == 0) {
  284. p = strrchr(line, ' ');
  285. if (!p) {
  286. warn("malformed line: %s\n", line);
  287. goto out_file;
  288. }
  289. p++;
  290. if (!parse_file(p, md)) {
  291. warn("could not open %s: %s\n",
  292. p, strerror(errno));
  293. goto out_file;
  294. }
  295. continue;
  296. }
  297. if (strncmp(line, "deps_", sizeof("deps_")-1) == 0) {
  298. check_files = 1;
  299. continue;
  300. }
  301. if (!check_files)
  302. continue;
  303. /* Continue until line does not end with '\' */
  304. if ( *(p + strlen(p)-1) != '\\')
  305. break;
  306. /* Terminate line at first space, to get rid of final ' \' */
  307. while (*p) {
  308. if (isspace(*p)) {
  309. *p = '\0';
  310. break;
  311. }
  312. p++;
  313. }
  314. /* Check if this file is in same dir as objfile */
  315. if ((strstr(line, dir)+strlen(dir)-1) == strrchr(line, '/')) {
  316. if (!parse_file(line, md)) {
  317. warn("could not open %s: %s\n",
  318. line, strerror(errno));
  319. goto out_file;
  320. }
  321. }
  322. }
  323. /* Everyone parsed OK */
  324. ret = 1;
  325. out_file:
  326. free(file);
  327. free(dir);
  328. free(cmd);
  329. return ret;
  330. }
  331. /* Calc and record src checksum. */
  332. void get_src_version(const char *modname, char sum[], unsigned sumlen)
  333. {
  334. char *buf;
  335. struct md4_ctx md;
  336. char *fname;
  337. char filelist[PATH_MAX + 1];
  338. /* objects for a module are listed in the first line of *.mod file. */
  339. snprintf(filelist, sizeof(filelist), "%s.mod", modname);
  340. buf = read_text_file(filelist);
  341. md4_init(&md);
  342. while ((fname = strsep(&buf, "\n"))) {
  343. if (!*fname)
  344. continue;
  345. if (!(is_static_library(fname)) &&
  346. !parse_source_files(fname, &md))
  347. goto free;
  348. }
  349. md4_final_ascii(&md, sum, sumlen);
  350. free:
  351. free(buf);
  352. }