1 #include <netinet/in.h>
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
49 return ((x << s) & 0xFFFFFFFF) | (x >> (32 -
s));
54 return (x & y) | ((~x) & z);
59 return (x & y) | (x & z) | (y & z);
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))
72 static inline void le32_to_cpu_array(
uint32_t *
buf,
unsigned int words)
80 static inline void cpu_to_le32_array(
uint32_t *
buf,
unsigned int words)
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);
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);
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);
154 static inline void md4_transform_helper(
struct md4_ctx *
ctx)
160 static void md4_init(
struct md4_ctx *mctx)
162 mctx->
hash[0] = 0x67452301;
163 mctx->
hash[1] = 0xefcdab89;
164 mctx->
hash[2] = 0x98badcfe;
165 mctx->
hash[3] = 0x10325476;
169 static void md4_update(
struct md4_ctx *mctx,
170 const unsigned char *
data,
unsigned int len)
185 md4_transform_helper(mctx);
189 while (len >=
sizeof(mctx->
block)) {
191 md4_transform_helper(mctx);
192 data +=
sizeof(mctx->
block);
193 len -=
sizeof(mctx->
block);
199 static void md4_final_ascii(
struct md4_ctx *mctx,
char *
out,
unsigned int len)
202 char *
p = (
char *)mctx->
block + offset;
203 int padding = 56 - (offset + 1);
208 md4_transform_helper(mctx);
209 p = (
char *)mctx->
block;
216 le32_to_cpu_array(mctx->
block, (
sizeof(mctx->
block) -
221 snprintf(out, len,
"%08X%08X%08X%08X",
225 static inline void add_char(
unsigned char c,
struct md4_ctx *
md)
227 md4_update(md, &c, 1);
230 static int parse_string(
const char *
file,
unsigned long len,
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] !=
'\\')
244 static int parse_comment(
const char *file,
unsigned long len)
248 for (i = 2; i < len; i++) {
249 if (file[i-1] ==
'*' && file[i] ==
'/')
256 static int parse_file(
const char *
fname,
struct md4_ctx *md)
259 unsigned long i, len;
265 for (i = 0; i < len; i++) {
267 if (file[i] ==
'\\' && (i+1 < len) && file[i+1] ==
'\n') {
277 if (file[i] ==
'"') {
278 i += parse_string(file+i, len - i, md);
283 if (file[i] ==
'/' && file[i+1] ==
'*') {
284 i += parse_comment(file+i, len - i);
288 add_char(file[i], md);
294 static int is_static_library(
const char *objfile)
296 int len =
strlen(objfile);
297 if (objfile[len - 2] ==
'.' && objfile[len - 1] ==
'a')
305 static int parse_source_files(
const char *objfile,
struct md4_ctx *md)
309 unsigned long flen,
pos = 0;
310 int dirlen,
ret = 0, check_files = 0;
317 dirlen = base - objfile;
318 sprintf(cmd,
"%.*s.%s.cmd", dirlen, objfile, base);
321 sprintf(cmd,
".%s.cmd", objfile);
329 warn(
"could not find %s for %s\n", cmd, objfile);
344 if (
strncmp(line,
"source_",
sizeof(
"source_")-1) == 0) {
347 warn(
"malformed line: %s\n", line);
351 if (!parse_file(p, md)) {
352 warn(
"could not open %s: %s\n",
358 if (
strncmp(line,
"deps_",
sizeof(
"deps_")-1) == 0) {
366 if ( *(p +
strlen(p)-1) !=
'\\')
379 if (!parse_file(line, md)) {
380 warn(
"could not open %s: %s\n",
406 const char *basename;
408 char *modverdir = getenv(
"MODVERDIR");
416 basename =
strrchr(modname,
'/') + 1;
419 sprintf(filelist,
"%s/%.*s.mod", modverdir,
420 (
int)
strlen(basename) - 2, basename);
427 sources =
strchr(file,
'\n');
429 warn(
"malformed versions file for %s\n", modname);
434 end =
strchr(sources,
'\n');
436 warn(
"bad ending versions file for %s\n", modname);
442 while ((fname =
strsep(&sources,
" ")) !=
NULL) {
445 if (!(is_static_library(fname)) &&
446 !parse_source_files(fname, &md))
450 md4_final_ascii(&md, sum, sumlen);
455 static void write_version(
const char *
filename,
const char *
sum,
456 unsigned long offset)
462 warn(
"changing sum in %s failed: %s\n",
468 warn(
"changing sum in %s:%lu failed: %s\n",
474 warn(
"writing sum in %s failed: %s\n",
482 static int strip_rcs_crap(
char *
version)
484 unsigned int len, full_len;
494 len =
strlen(
"$Revision");
495 if (version[len] ==
':' || version[len] ==
'$')
499 memmove(version, version+len, full_len-len);
504 while (version[len] && !
isspace(version[len]))
507 full_len -
strlen(version));
515 unsigned long version_offset)
517 if (strip_rcs_crap(version))
518 write_version(modfilename, version, version_offset);