wok-current rev 12210

syslinux: add md5sum.c32
author Pascal Bellard <pascal.bellard@slitaz.org>
date Sat Mar 31 14:27:35 2012 +0200 (2012-03-31)
parents fa389f5b7eb9
children 7219c33850c6
files syslinux/receipt syslinux/stuff/extra/md5sum.c syslinux/stuff/isolinux.cfg
line diff
     1.1 --- a/syslinux/receipt	Fri Mar 30 14:26:56 2012 +0200
     1.2 +++ b/syslinux/receipt	Sat Mar 31 14:27:35 2012 +0200
     1.3 @@ -19,8 +19,9 @@
     1.4      cp $stuff/tools/isohybrid.sh .
     1.5      cp $stuff/tools/keytab-lilo.pl .
     1.6      cp $stuff/extra/ifmem.c com32/modules
     1.7 +    cp $stuff/extra/md5sum.c com32/modules
     1.8      grep -q ifmem.c32 com32/modules/Makefile ||
     1.9 -	sed -i 's/ifcpu64.c32/ifcpu64.c32 ifmem.c32/' com32/modules/Makefile
    1.10 +	sed -i 's/ifcpu64.c32/ifcpu64.c32 ifmem.c32 md5sum.c32/' com32/modules/Makefile
    1.11      make -C com32
    1.12      ./isohybrid.sh --build
    1.13      for i in /usr/share/kbd/keymaps/i386/*/*.map.gz; do
    1.14 @@ -38,6 +39,7 @@
    1.15      cp -a $src/core/isolinux.bin $fs/boot/isolinux
    1.16      cp -a $src/com32/modules/reboot.c32 $fs/boot/isolinux
    1.17      cp -a $src/com32/modules/ifmem.c32 $fs/boot/isolinux
    1.18 +    cp -a $src/com32/modules/md5sum.c32 $fs/boot/isolinux
    1.19      cp -a $src/com32/menu/vesamenu.c32 $fs/boot/isolinux
    1.20      cp -a $src/modules/poweroff.com $fs/boot/isolinux
    1.21      # $stuff/isolinux.msg is the old way the have a splash image.
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/syslinux/stuff/extra/md5sum.c	Sat Mar 31 14:27:35 2012 +0200
     2.3 @@ -0,0 +1,363 @@
     2.4 +/* vi: set sw=4 ts=4: */
     2.5 +/*
     2.6 + * Based on busybox code
     2.7 + *
     2.8 + * Utility routines.
     2.9 + *
    2.10 + * Copyright (C) 2010 Denys Vlasenko
    2.11 + *
    2.12 + * Licensed under GPLv2 or later, see file LICENSE in this source tree.
    2.13 + */
    2.14 +
    2.15 +#include <stdio.h>
    2.16 +#include <stdlib.h>
    2.17 +#include <string.h>
    2.18 +#include <unistd.h>
    2.19 +#include <fcntl.h>
    2.20 +#include <console.h>
    2.21 +#include <com32.h>
    2.22 +
    2.23 +#define ALIGN1
    2.24 +const char bb_hexdigits_upcase[] ALIGN1 = "0123456789ABCDEF";
    2.25 +
    2.26 +/* Emit a string of hex representation of bytes */
    2.27 +char* bin2hex(char *p, const char *cp, int count)
    2.28 +{
    2.29 +	while (count) {
    2.30 +		unsigned char c = *cp++;
    2.31 +		/* put lowercase hex digits */
    2.32 +		*p++ = 0x20 | bb_hexdigits_upcase[c >> 4];
    2.33 +		*p++ = 0x20 | bb_hexdigits_upcase[c & 0xf];
    2.34 +		count--;
    2.35 +	}
    2.36 +	return p;
    2.37 +}
    2.38 +
    2.39 +//#define rotl32(x,n) (((x) << (n)) | ((x) >> (32 - (n))))
    2.40 +static uint32_t rotl32(uint32_t x, unsigned n)
    2.41 +{
    2.42 +	return (x << n) | (x >> (32 - n));
    2.43 +}
    2.44 +
    2.45 +typedef struct md5_ctx_t {
    2.46 +	uint8_t wbuffer[64]; /* always correctly aligned for uint64_t */
    2.47 +	uint64_t total64;    /* must be directly before hash[] */
    2.48 +	uint32_t hash[8];    /* 4 elements for md5, 5 for sha1, 8 for sha256 */
    2.49 +} md5_ctx_t;
    2.50 +
    2.51 +static void md5_process_block64(md5_ctx_t *ctx);
    2.52 +
    2.53 +/* Feed data through a temporary buffer.
    2.54 + * The internal buffer remembers previous data until it has 64
    2.55 + * bytes worth to pass on.
    2.56 + */
    2.57 +static void common64_hash(md5_ctx_t *ctx, const void *buffer, size_t len)
    2.58 +{
    2.59 +	unsigned bufpos = ctx->total64 & 63;
    2.60 +
    2.61 +	ctx->total64 += len;
    2.62 +
    2.63 +	while (1) {
    2.64 +		unsigned remaining = 64 - bufpos;
    2.65 +		if (remaining > len)
    2.66 +			remaining = len;
    2.67 +		/* Copy data into aligned buffer */
    2.68 +		memcpy(ctx->wbuffer + bufpos, buffer, remaining);
    2.69 +		len -= remaining;
    2.70 +		buffer = (const char *)buffer + remaining;
    2.71 +		bufpos += remaining;
    2.72 +		/* clever way to do "if (bufpos != 64) break; ... ; bufpos = 0;" */
    2.73 +		bufpos -= 64;
    2.74 +		if (bufpos != 0)
    2.75 +			break;
    2.76 +		/* Buffer is filled up, process it */
    2.77 +		md5_process_block64(ctx);
    2.78 +		/*bufpos = 0; - already is */
    2.79 +	}
    2.80 +}
    2.81 +
    2.82 +/* Process the remaining bytes in the buffer */
    2.83 +static void common64_end(md5_ctx_t *ctx)
    2.84 +{
    2.85 +	unsigned bufpos = ctx->total64 & 63;
    2.86 +	/* Pad the buffer to the next 64-byte boundary with 0x80,0,0,0... */
    2.87 +	ctx->wbuffer[bufpos++] = 0x80;
    2.88 +
    2.89 +	/* This loop iterates either once or twice, no more, no less */
    2.90 +	while (1) {
    2.91 +		unsigned remaining = 64 - bufpos;
    2.92 +		memset(ctx->wbuffer + bufpos, 0, remaining);
    2.93 +		/* Do we have enough space for the length count? */
    2.94 +		if (remaining >= 8) {
    2.95 +			/* Store the 64-bit counter of bits in the buffer */
    2.96 +			uint64_t t = ctx->total64 << 3;
    2.97 +			/* wbuffer is suitably aligned for this */
    2.98 +			*(uint64_t *) (&ctx->wbuffer[64 - 8]) = t;
    2.99 +		}
   2.100 +		md5_process_block64(ctx);
   2.101 +		if (remaining >= 8)
   2.102 +			break;
   2.103 +		bufpos = 0;
   2.104 +	}
   2.105 +}
   2.106 +
   2.107 +
   2.108 +/*
   2.109 + * Compute MD5 checksum of strings according to the
   2.110 + * definition of MD5 in RFC 1321 from April 1992.
   2.111 + *
   2.112 + * Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.
   2.113 + *
   2.114 + * Copyright (C) 1995-1999 Free Software Foundation, Inc.
   2.115 + * Copyright (C) 2001 Manuel Novoa III
   2.116 + * Copyright (C) 2003 Glenn L. McGrath
   2.117 + * Copyright (C) 2003 Erik Andersen
   2.118 + *
   2.119 + * Licensed under GPLv2 or later, see file LICENSE in this source tree.
   2.120 + */
   2.121 +
   2.122 +/* These are the four functions used in the four steps of the MD5 algorithm
   2.123 + * and defined in the RFC 1321.  The first function is a little bit optimized
   2.124 + * (as found in Colin Plumbs public domain implementation).
   2.125 + * #define FF(b, c, d) ((b & c) | (~b & d))
   2.126 + */
   2.127 +#undef FF
   2.128 +#undef FG
   2.129 +#undef FH
   2.130 +#undef FI
   2.131 +#define FF(b, c, d) (d ^ (b & (c ^ d)))
   2.132 +#define FG(b, c, d) FF(d, b, c)
   2.133 +#define FH(b, c, d) (b ^ c ^ d)
   2.134 +#define FI(b, c, d) (c ^ (b | ~d))
   2.135 +
   2.136 +/* Hash a single block, 64 bytes long and 4-byte aligned */
   2.137 +static void md5_process_block64(md5_ctx_t *ctx)
   2.138 +{
   2.139 +	/* Before we start, one word to the strange constants.
   2.140 +	   They are defined in RFC 1321 as
   2.141 +	   T[i] = (int)(4294967296.0 * fabs(sin(i))), i=1..64
   2.142 +	 */
   2.143 +	static const uint32_t C_array[] = {
   2.144 +		/* round 1 */
   2.145 +		0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee,
   2.146 +		0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501,
   2.147 +		0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be,
   2.148 +		0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821,
   2.149 +		/* round 2 */
   2.150 +		0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa,
   2.151 +		0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8,
   2.152 +		0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed,
   2.153 +		0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a,
   2.154 +		/* round 3 */
   2.155 +		0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c,
   2.156 +		0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70,
   2.157 +		0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x4881d05,
   2.158 +		0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,
   2.159 +		/* round 4 */
   2.160 +		0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039,
   2.161 +		0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1,
   2.162 +		0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1,
   2.163 +		0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391
   2.164 +	};
   2.165 +	static const char P_array[] ALIGN1 = {
   2.166 +		0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, /* 1 */
   2.167 +		1, 6, 11, 0, 5, 10, 15, 4, 9, 14, 3, 8, 13, 2, 7, 12, /* 2 */
   2.168 +		5, 8, 11, 14, 1, 4, 7, 10, 13, 0, 3, 6, 9, 12, 15, 2, /* 3 */
   2.169 +		0, 7, 14, 5, 12, 3, 10, 1, 8, 15, 6, 13, 4, 11, 2, 9  /* 4 */
   2.170 +	};
   2.171 +	uint32_t *words = (void*) ctx->wbuffer;
   2.172 +	uint32_t A = ctx->hash[0];
   2.173 +	uint32_t B = ctx->hash[1];
   2.174 +	uint32_t C = ctx->hash[2];
   2.175 +	uint32_t D = ctx->hash[3];
   2.176 +
   2.177 +	static const char S_array[] ALIGN1 = {
   2.178 +		7, 12, 17, 22,
   2.179 +		5, 9, 14, 20,
   2.180 +		4, 11, 16, 23,
   2.181 +		6, 10, 15, 21
   2.182 +	};
   2.183 +	const uint32_t *pc;
   2.184 +	const char *pp;
   2.185 +	const char *ps;
   2.186 +	int i;
   2.187 +	uint32_t temp;
   2.188 +
   2.189 +
   2.190 +	pc = C_array;
   2.191 +	pp = P_array;
   2.192 +	ps = S_array - 4;
   2.193 +
   2.194 +	for (i = 0; i < 64; i++) {
   2.195 +		if ((i & 0x0f) == 0)
   2.196 +			ps += 4;
   2.197 +		temp = A;
   2.198 +		switch (i >> 4) {
   2.199 +		case 0:
   2.200 +			temp += FF(B, C, D);
   2.201 +			break;
   2.202 +		case 1:
   2.203 +			temp += FG(B, C, D);
   2.204 +			break;
   2.205 +		case 2:
   2.206 +			temp += FH(B, C, D);
   2.207 +			break;
   2.208 +		case 3:
   2.209 +			temp += FI(B, C, D);
   2.210 +		}
   2.211 +		temp += words[(int) (*pp++)] + *pc++;
   2.212 +		temp = rotl32(temp, ps[i & 3]);
   2.213 +		temp += B;
   2.214 +		A = D;
   2.215 +		D = C;
   2.216 +		C = B;
   2.217 +		B = temp;
   2.218 +	}
   2.219 +	/* Add checksum to the starting values */
   2.220 +	ctx->hash[0] += A;
   2.221 +	ctx->hash[1] += B;
   2.222 +	ctx->hash[2] += C;
   2.223 +	ctx->hash[3] += D;
   2.224 +
   2.225 +}
   2.226 +#undef FF
   2.227 +#undef FG
   2.228 +#undef FH
   2.229 +#undef FI
   2.230 +
   2.231 +/* Initialize structure containing state of computation.
   2.232 + * (RFC 1321, 3.3: Step 3)
   2.233 + */
   2.234 +void md5_begin(md5_ctx_t *ctx)
   2.235 +{
   2.236 +	ctx->hash[0] = 0x67452301;
   2.237 +	ctx->hash[1] = 0xefcdab89;
   2.238 +	ctx->hash[2] = 0x98badcfe;
   2.239 +	ctx->hash[3] = 0x10325476;
   2.240 +	ctx->total64 = 0;
   2.241 +}
   2.242 +
   2.243 +/* Used also for sha1 and sha256 */
   2.244 +void md5_hash(md5_ctx_t *ctx, const void *buffer, size_t len)
   2.245 +{
   2.246 +	common64_hash(ctx, buffer, len);
   2.247 +}
   2.248 +
   2.249 +/* Process the remaining bytes in the buffer and put result from CTX
   2.250 + * in first 16 bytes following RESBUF.  The result is always in little
   2.251 + * endian byte order, so that a byte-wise output yields to the wanted
   2.252 + * ASCII representation of the message digest.
   2.253 + */
   2.254 +void md5_end(md5_ctx_t *ctx, void *resbuf)
   2.255 +{
   2.256 +	/* MD5 stores total in LE, need to swap on BE arches: */
   2.257 +	common64_end(ctx);
   2.258 +
   2.259 +	/* The MD5 result is in little endian byte order */
   2.260 +	memcpy(resbuf, ctx->hash, sizeof(ctx->hash[0]) * 4);
   2.261 +}
   2.262 +
   2.263 +/*
   2.264 + *  Copyright (C) 2003 Glenn L. McGrath
   2.265 + *  Copyright (C) 2003-2004 Erik Andersen
   2.266 + *
   2.267 + * Licensed under GPLv2 or later, see file LICENSE in this source tree.
   2.268 + */
   2.269 +
   2.270 +/* This might be useful elsewhere */
   2.271 +static unsigned char *hash_bin_to_hex(unsigned char *hash_value,
   2.272 +				unsigned hash_length)
   2.273 +{
   2.274 +	static char hex_value[16*2+1];
   2.275 +	bin2hex(hex_value, (char*)hash_value, hash_length);
   2.276 +	return (unsigned char *)hex_value;
   2.277 +}
   2.278 +
   2.279 +static uint8_t *hash_file(const char *filename)
   2.280 +{
   2.281 +	int src_fd, count;
   2.282 +	md5_ctx_t context;
   2.283 +	uint8_t *hash_value, in_buf[4096];
   2.284 +
   2.285 +	src_fd = open(filename, O_RDONLY);
   2.286 +	if (src_fd < 0) {
   2.287 +		return NULL;
   2.288 +	}
   2.289 +
   2.290 +	md5_begin(&context);
   2.291 +	while ((count = read(src_fd, in_buf, 4096)) > 0) {
   2.292 +		md5_hash(&context, in_buf, count);
   2.293 +	}
   2.294 +	hash_value = NULL;
   2.295 +	if (count == 0) {
   2.296 +		md5_end(&context, in_buf);
   2.297 +		hash_value = hash_bin_to_hex(in_buf, 16);
   2.298 +	}
   2.299 +
   2.300 +	close(src_fd);
   2.301 +
   2.302 +	return hash_value;
   2.303 +}
   2.304 +
   2.305 +static int valid_name(char *name)
   2.306 +{
   2.307 +	int dots, suffix;
   2.308 +	for (dots = 0, suffix = 0; *name; name++) {
   2.309 +		if (dots) suffix++;
   2.310 +		if (*name == '.') dots++;
   2.311 +	}
   2.312 +	return dots < 2 && suffix <= 3;
   2.313 +}
   2.314 +
   2.315 +int main(int argc, char **argv)
   2.316 +{
   2.317 +	int return_value = EXIT_SUCCESS;
   2.318 +
   2.319 +	(void) argc;
   2.320 +	/* -c implied */
   2.321 +	openconsole(&dev_rawcon_r, &dev_stdcon_w);
   2.322 +
   2.323 +	do {
   2.324 +		FILE *fp;
   2.325 +		char *line, buffer[256];
   2.326 +		fp = fopen(*argv,"r");
   2.327 +
   2.328 +		while ((line = fgets(buffer,256,fp)) != NULL) {
   2.329 +			uint8_t *hash_value;
   2.330 +			char *filename_ptr, *status;
   2.331 +			int len = strlen(line);
   2.332 +#define BLANK "                                "
   2.333 +
   2.334 +			if (line[0] < '0')
   2.335 +				continue;
   2.336 +			if (line[len-1] == '\n')
   2.337 +				line[len-1] = 0;
   2.338 +			filename_ptr = strstr(line, "  ");
   2.339 +			/* handle format for binary checksums */
   2.340 +			if (filename_ptr == NULL) {
   2.341 +				filename_ptr = strstr(line, " *");
   2.342 +			}
   2.343 +			if (filename_ptr == NULL) {
   2.344 +				return_value = EXIT_FAILURE;
   2.345 +				continue;
   2.346 +			}
   2.347 +			*filename_ptr = '\0';
   2.348 +			*++filename_ptr = '/';
   2.349 +
   2.350 +			status = "SKIPPED" BLANK;
   2.351 +			if (valid_name(filename_ptr)) {
   2.352 +				hash_value = hash_file(filename_ptr);
   2.353 +				status = "OK" BLANK;
   2.354 +				if (hash_value == NULL || strcmp((char*)hash_value, line)) {
   2.355 +					return_value = EXIT_FAILURE;
   2.356 +					status = "FAILED" BLANK "\n";
   2.357 +				}
   2.358 +			}
   2.359 +			printf("\r%s: %s", filename_ptr, status);
   2.360 +		}
   2.361 +		fclose(fp);
   2.362 +	} while (*++argv);
   2.363 +	printf("\r" BLANK "\r");
   2.364 +
   2.365 +	return return_value;
   2.366 +}
     3.1 --- a/syslinux/stuff/isolinux.cfg	Fri Mar 30 14:26:56 2012 +0200
     3.2 +++ b/syslinux/stuff/isolinux.cfg	Sat Mar 31 14:27:35 2012 +0200
     3.3 @@ -38,6 +38,11 @@
     3.4  
     3.5  INCLUDE i18n.cfg
     3.6  
     3.7 +LABEL md5sum
     3.8 +	MENU LABEL Check media
     3.9 +	COM32 md5sum.c32
    3.10 +	append /md5sum
    3.11 +
    3.12  LABEL cmdline
    3.13  	MENU LABEL Command Line
    3.14  	MENU QUIT