Back to home page

OSCL-LXR

 
 

    


0001 #include <netinet/in.h>
0002 #ifdef __sun__
0003 #include <inttypes.h>
0004 #else
0005 #include <stdint.h>
0006 #endif
0007 #include <ctype.h>
0008 #include <errno.h>
0009 #include <string.h>
0010 #include <limits.h>
0011 #include "modpost.h"
0012 
0013 /*
0014  * Stolen form Cryptographic API.
0015  *
0016  * MD4 Message Digest Algorithm (RFC1320).
0017  *
0018  * Implementation derived from Andrew Tridgell and Steve French's
0019  * CIFS MD4 implementation, and the cryptoapi implementation
0020  * originally based on the public domain implementation written
0021  * by Colin Plumb in 1993.
0022  *
0023  * Copyright (c) Andrew Tridgell 1997-1998.
0024  * Modified by Steve French (sfrench@us.ibm.com) 2002
0025  * Copyright (c) Cryptoapi developers.
0026  * Copyright (c) 2002 David S. Miller (davem@redhat.com)
0027  * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
0028  *
0029  * This program is free software; you can redistribute it and/or modify
0030  * it under the terms of the GNU General Public License as published by
0031  * the Free Software Foundation; either version 2 of the License, or
0032  * (at your option) any later version.
0033  *
0034  */
0035 #define MD4_DIGEST_SIZE     16
0036 #define MD4_HMAC_BLOCK_SIZE 64
0037 #define MD4_BLOCK_WORDS     16
0038 #define MD4_HASH_WORDS      4
0039 
0040 struct md4_ctx {
0041     uint32_t hash[MD4_HASH_WORDS];
0042     uint32_t block[MD4_BLOCK_WORDS];
0043     uint64_t byte_count;
0044 };
0045 
0046 static inline uint32_t lshift(uint32_t x, unsigned int s)
0047 {
0048     x &= 0xFFFFFFFF;
0049     return ((x << s) & 0xFFFFFFFF) | (x >> (32 - s));
0050 }
0051 
0052 static inline uint32_t F(uint32_t x, uint32_t y, uint32_t z)
0053 {
0054     return (x & y) | ((~x) & z);
0055 }
0056 
0057 static inline uint32_t G(uint32_t x, uint32_t y, uint32_t z)
0058 {
0059     return (x & y) | (x & z) | (y & z);
0060 }
0061 
0062 static inline uint32_t H(uint32_t x, uint32_t y, uint32_t z)
0063 {
0064     return x ^ y ^ z;
0065 }
0066 
0067 #define ROUND1(a,b,c,d,k,s) (a = lshift(a + F(b,c,d) + k, s))
0068 #define ROUND2(a,b,c,d,k,s) (a = lshift(a + G(b,c,d) + k + (uint32_t)0x5A827999,s))
0069 #define ROUND3(a,b,c,d,k,s) (a = lshift(a + H(b,c,d) + k + (uint32_t)0x6ED9EBA1,s))
0070 
0071 /* XXX: this stuff can be optimized */
0072 static inline void le32_to_cpu_array(uint32_t *buf, unsigned int words)
0073 {
0074     while (words--) {
0075         *buf = ntohl(*buf);
0076         buf++;
0077     }
0078 }
0079 
0080 static inline void cpu_to_le32_array(uint32_t *buf, unsigned int words)
0081 {
0082     while (words--) {
0083         *buf = htonl(*buf);
0084         buf++;
0085     }
0086 }
0087 
0088 static void md4_transform(uint32_t *hash, uint32_t const *in)
0089 {
0090     uint32_t a, b, c, d;
0091 
0092     a = hash[0];
0093     b = hash[1];
0094     c = hash[2];
0095     d = hash[3];
0096 
0097     ROUND1(a, b, c, d, in[0], 3);
0098     ROUND1(d, a, b, c, in[1], 7);
0099     ROUND1(c, d, a, b, in[2], 11);
0100     ROUND1(b, c, d, a, in[3], 19);
0101     ROUND1(a, b, c, d, in[4], 3);
0102     ROUND1(d, a, b, c, in[5], 7);
0103     ROUND1(c, d, a, b, in[6], 11);
0104     ROUND1(b, c, d, a, in[7], 19);
0105     ROUND1(a, b, c, d, in[8], 3);
0106     ROUND1(d, a, b, c, in[9], 7);
0107     ROUND1(c, d, a, b, in[10], 11);
0108     ROUND1(b, c, d, a, in[11], 19);
0109     ROUND1(a, b, c, d, in[12], 3);
0110     ROUND1(d, a, b, c, in[13], 7);
0111     ROUND1(c, d, a, b, in[14], 11);
0112     ROUND1(b, c, d, a, in[15], 19);
0113 
0114     ROUND2(a, b, c, d,in[ 0], 3);
0115     ROUND2(d, a, b, c, in[4], 5);
0116     ROUND2(c, d, a, b, in[8], 9);
0117     ROUND2(b, c, d, a, in[12], 13);
0118     ROUND2(a, b, c, d, in[1], 3);
0119     ROUND2(d, a, b, c, in[5], 5);
0120     ROUND2(c, d, a, b, in[9], 9);
0121     ROUND2(b, c, d, a, in[13], 13);
0122     ROUND2(a, b, c, d, in[2], 3);
0123     ROUND2(d, a, b, c, in[6], 5);
0124     ROUND2(c, d, a, b, in[10], 9);
0125     ROUND2(b, c, d, a, in[14], 13);
0126     ROUND2(a, b, c, d, in[3], 3);
0127     ROUND2(d, a, b, c, in[7], 5);
0128     ROUND2(c, d, a, b, in[11], 9);
0129     ROUND2(b, c, d, a, in[15], 13);
0130 
0131     ROUND3(a, b, c, d,in[ 0], 3);
0132     ROUND3(d, a, b, c, in[8], 9);
0133     ROUND3(c, d, a, b, in[4], 11);
0134     ROUND3(b, c, d, a, in[12], 15);
0135     ROUND3(a, b, c, d, in[2], 3);
0136     ROUND3(d, a, b, c, in[10], 9);
0137     ROUND3(c, d, a, b, in[6], 11);
0138     ROUND3(b, c, d, a, in[14], 15);
0139     ROUND3(a, b, c, d, in[1], 3);
0140     ROUND3(d, a, b, c, in[9], 9);
0141     ROUND3(c, d, a, b, in[5], 11);
0142     ROUND3(b, c, d, a, in[13], 15);
0143     ROUND3(a, b, c, d, in[3], 3);
0144     ROUND3(d, a, b, c, in[11], 9);
0145     ROUND3(c, d, a, b, in[7], 11);
0146     ROUND3(b, c, d, a, in[15], 15);
0147 
0148     hash[0] += a;
0149     hash[1] += b;
0150     hash[2] += c;
0151     hash[3] += d;
0152 }
0153 
0154 static inline void md4_transform_helper(struct md4_ctx *ctx)
0155 {
0156     le32_to_cpu_array(ctx->block, sizeof(ctx->block) / sizeof(uint32_t));
0157     md4_transform(ctx->hash, ctx->block);
0158 }
0159 
0160 static void md4_init(struct md4_ctx *mctx)
0161 {
0162     mctx->hash[0] = 0x67452301;
0163     mctx->hash[1] = 0xefcdab89;
0164     mctx->hash[2] = 0x98badcfe;
0165     mctx->hash[3] = 0x10325476;
0166     mctx->byte_count = 0;
0167 }
0168 
0169 static void md4_update(struct md4_ctx *mctx,
0170                const unsigned char *data, unsigned int len)
0171 {
0172     const uint32_t avail = sizeof(mctx->block) - (mctx->byte_count & 0x3f);
0173 
0174     mctx->byte_count += len;
0175 
0176     if (avail > len) {
0177         memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
0178                data, len);
0179         return;
0180     }
0181 
0182     memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
0183            data, avail);
0184 
0185     md4_transform_helper(mctx);
0186     data += avail;
0187     len -= avail;
0188 
0189     while (len >= sizeof(mctx->block)) {
0190         memcpy(mctx->block, data, sizeof(mctx->block));
0191         md4_transform_helper(mctx);
0192         data += sizeof(mctx->block);
0193         len -= sizeof(mctx->block);
0194     }
0195 
0196     memcpy(mctx->block, data, len);
0197 }
0198 
0199 static void md4_final_ascii(struct md4_ctx *mctx, char *out, unsigned int len)
0200 {
0201     const unsigned int offset = mctx->byte_count & 0x3f;
0202     char *p = (char *)mctx->block + offset;
0203     int padding = 56 - (offset + 1);
0204 
0205     *p++ = 0x80;
0206     if (padding < 0) {
0207         memset(p, 0x00, padding + sizeof (uint64_t));
0208         md4_transform_helper(mctx);
0209         p = (char *)mctx->block;
0210         padding = 56;
0211     }
0212 
0213     memset(p, 0, padding);
0214     mctx->block[14] = mctx->byte_count << 3;
0215     mctx->block[15] = mctx->byte_count >> 29;
0216     le32_to_cpu_array(mctx->block, (sizeof(mctx->block) -
0217               sizeof(uint64_t)) / sizeof(uint32_t));
0218     md4_transform(mctx->hash, mctx->block);
0219     cpu_to_le32_array(mctx->hash, sizeof(mctx->hash) / sizeof(uint32_t));
0220 
0221     snprintf(out, len, "%08X%08X%08X%08X",
0222          mctx->hash[0], mctx->hash[1], mctx->hash[2], mctx->hash[3]);
0223 }
0224 
0225 static inline void add_char(unsigned char c, struct md4_ctx *md)
0226 {
0227     md4_update(md, &c, 1);
0228 }
0229 
0230 static int parse_string(const char *file, unsigned long len,
0231             struct md4_ctx *md)
0232 {
0233     unsigned long i;
0234 
0235     add_char(file[0], md);
0236     for (i = 1; i < len; i++) {
0237         add_char(file[i], md);
0238         if (file[i] == '"' && file[i-1] != '\\')
0239             break;
0240     }
0241     return i;
0242 }
0243 
0244 static int parse_comment(const char *file, unsigned long len)
0245 {
0246     unsigned long i;
0247 
0248     for (i = 2; i < len; i++) {
0249         if (file[i-1] == '*' && file[i] == '/')
0250             break;
0251     }
0252     return i;
0253 }
0254 
0255 /* FIXME: Handle .s files differently (eg. # starts comments) --RR */
0256 static int parse_file(const char *fname, struct md4_ctx *md)
0257 {
0258     char *file;
0259     unsigned long i, len;
0260 
0261     file = read_text_file(fname);
0262     len = strlen(file);
0263 
0264     for (i = 0; i < len; i++) {
0265         /* Collapse and ignore \ and CR. */
0266         if (file[i] == '\\' && (i+1 < len) && file[i+1] == '\n') {
0267             i++;
0268             continue;
0269         }
0270 
0271         /* Ignore whitespace */
0272         if (isspace(file[i]))
0273             continue;
0274 
0275         /* Handle strings as whole units */
0276         if (file[i] == '"') {
0277             i += parse_string(file+i, len - i, md);
0278             continue;
0279         }
0280 
0281         /* Comments: ignore */
0282         if (file[i] == '/' && file[i+1] == '*') {
0283             i += parse_comment(file+i, len - i);
0284             continue;
0285         }
0286 
0287         add_char(file[i], md);
0288     }
0289     free(file);
0290     return 1;
0291 }
0292 /* Check whether the file is a static library or not */
0293 static bool is_static_library(const char *objfile)
0294 {
0295     int len = strlen(objfile);
0296 
0297     return objfile[len - 2] == '.' && objfile[len - 1] == 'a';
0298 }
0299 
0300 /* We have dir/file.o.  Open dir/.file.o.cmd, look for source_ and deps_ line
0301  * to figure out source files. */
0302 static int parse_source_files(const char *objfile, struct md4_ctx *md)
0303 {
0304     char *cmd, *file, *line, *dir, *pos;
0305     const char *base;
0306     int dirlen, ret = 0, check_files = 0;
0307 
0308     cmd = NOFAIL(malloc(strlen(objfile) + sizeof("..cmd")));
0309 
0310     base = strrchr(objfile, '/');
0311     if (base) {
0312         base++;
0313         dirlen = base - objfile;
0314         sprintf(cmd, "%.*s.%s.cmd", dirlen, objfile, base);
0315     } else {
0316         dirlen = 0;
0317         sprintf(cmd, ".%s.cmd", objfile);
0318     }
0319     dir = NOFAIL(malloc(dirlen + 1));
0320     strncpy(dir, objfile, dirlen);
0321     dir[dirlen] = '\0';
0322 
0323     file = read_text_file(cmd);
0324 
0325     pos = file;
0326 
0327     /* Sum all files in the same dir or subdirs. */
0328     while ((line = get_line(&pos))) {
0329         char* p = line;
0330 
0331         if (strncmp(line, "source_", sizeof("source_")-1) == 0) {
0332             p = strrchr(line, ' ');
0333             if (!p) {
0334                 warn("malformed line: %s\n", line);
0335                 goto out_file;
0336             }
0337             p++;
0338             if (!parse_file(p, md)) {
0339                 warn("could not open %s: %s\n",
0340                      p, strerror(errno));
0341                 goto out_file;
0342             }
0343             continue;
0344         }
0345         if (strncmp(line, "deps_", sizeof("deps_")-1) == 0) {
0346             check_files = 1;
0347             continue;
0348         }
0349         if (!check_files)
0350             continue;
0351 
0352         /* Continue until line does not end with '\' */
0353         if ( *(p + strlen(p)-1) != '\\')
0354             break;
0355         /* Terminate line at first space, to get rid of final ' \' */
0356         while (*p) {
0357             if (isspace(*p)) {
0358                 *p = '\0';
0359                 break;
0360             }
0361             p++;
0362         }
0363 
0364         /* Check if this file is in same dir as objfile */
0365         if ((strstr(line, dir)+strlen(dir)-1) == strrchr(line, '/')) {
0366             if (!parse_file(line, md)) {
0367                 warn("could not open %s: %s\n",
0368                      line, strerror(errno));
0369                 goto out_file;
0370             }
0371 
0372         }
0373 
0374     }
0375 
0376     /* Everyone parsed OK */
0377     ret = 1;
0378 out_file:
0379     free(file);
0380     free(dir);
0381     free(cmd);
0382     return ret;
0383 }
0384 
0385 /* Calc and record src checksum. */
0386 void get_src_version(const char *modname, char sum[], unsigned sumlen)
0387 {
0388     char *buf;
0389     struct md4_ctx md;
0390     char *fname;
0391     char filelist[PATH_MAX + 1];
0392 
0393     /* objects for a module are listed in the first line of *.mod file. */
0394     snprintf(filelist, sizeof(filelist), "%s.mod", modname);
0395 
0396     buf = read_text_file(filelist);
0397 
0398     md4_init(&md);
0399     while ((fname = strsep(&buf, "\n"))) {
0400         if (!*fname)
0401             continue;
0402         if (!(is_static_library(fname)) &&
0403                 !parse_source_files(fname, &md))
0404             goto free;
0405     }
0406 
0407     md4_final_ascii(&md, sum, sumlen);
0408 free:
0409     free(buf);
0410 }