From: Sascha Wildner Date: Thu, 11 Sep 2008 20:25:34 +0000 (+0000) Subject: Sync libmd with FreeBSD: X-Git-Tag: v2.1.1~437 X-Git-Url: https://gitweb.dragonflybsd.org/dragonfly.git/commitdiff_plain/3cd019119cb255decbc9a1724fd65c2cbba3dfdd Sync libmd with FreeBSD: * Some bugfixes * Addition of *_FileChunk() * Minor changes/cleanup --- diff --git a/lib/libmd/Makefile b/lib/libmd/Makefile index 30085df208..1e531c4279 100644 --- a/lib/libmd/Makefile +++ b/lib/libmd/Makefile @@ -1,5 +1,5 @@ -# $FreeBSD: src/lib/libmd/Makefile,v 1.34.2.2 2002/07/17 15:16:38 ru Exp $ -# $DragonFly: src/lib/libmd/Makefile,v 1.5 2008/04/12 08:38:40 swildner Exp $ +# $FreeBSD: src/lib/libmd/Makefile,v 1.42 2005/03/09 19:23:04 cperciva Exp $ +# $DragonFly: src/lib/libmd/Makefile,v 1.6 2008/09/11 20:25:34 swildner Exp $ LIB= md SRCS= md2c.c md4c.c md5c.c md2hl.c md4hl.c md5hl.c \ @@ -10,21 +10,27 @@ INCS= md2.h md4.h md5.h ripemd.h sha.h sha256.h MAN+= md2.3 md4.3 md5.3 ripemd.3 sha.3 sha256.3 MLINKS+=md2.3 MD2Init.3 md2.3 MD2Update.3 md2.3 MD2Pad.3 md2.3 MD2Final.3 -MLINKS+=md2.3 MD2End.3 md2.3 MD2File.3 md2.3 MD2Data.3 +MLINKS+=md2.3 MD2End.3 md2.3 MD2File.3 md2.3 MD2FileChunk.3 +MLINKS+=md2.3 MD2Data.3 MLINKS+=md4.3 MD4Init.3 md4.3 MD4Update.3 md4.3 MD4Pad.3 md4.3 MD4Final.3 -MLINKS+=md4.3 MD4End.3 md4.3 MD4File.3 md4.3 MD4Data.3 +MLINKS+=md4.3 MD4End.3 md4.3 MD4File.3 md4.3 MD4FileChunk.3 +MLINKS+=md4.3 MD4Data.3 MLINKS+=md5.3 MD5Init.3 md5.3 MD5Update.3 md5.3 MD5Pad.3 md5.3 MD5Final.3 -MLINKS+=md5.3 MD5End.3 md5.3 MD5File.3 md5.3 MD5Data.3 +MLINKS+=md5.3 MD5End.3 md5.3 MD5File.3 md5.3 MD5FileChunk.3 +MLINKS+=md5.3 MD5Data.3 MLINKS+=ripemd.3 RIPEMD160_Init.3 ripemd.3 RIPEMD160_Update.3 MLINKS+=ripemd.3 RIPEMD160_Final.3 ripemd.3 RIPEMD160_Data.3 MLINKS+=ripemd.3 RIPEMD160_End.3 ripemd.3 RIPEMD160_File.3 +MLINKS+=ripemd.3 RIPEMD160_FileChunk.3 MLINKS+=sha.3 SHA_Init.3 sha.3 SHA_Update.3 sha.3 SHA_Final.3 -MLINKS+=sha.3 SHA_End.3 sha.3 SHA_File.3 sha.3 SHA_Data.3 +MLINKS+=sha.3 SHA_End.3 sha.3 SHA_File.3 sha.3 SHA_FileChunk.3 +MLINKS+=sha.3 SHA_Data.3 MLINKS+=sha.3 SHA1_Init.3 sha.3 SHA1_Update.3 sha.3 SHA1_Final.3 -MLINKS+=sha.3 SHA1_End.3 sha.3 SHA1_File.3 sha.3 SHA1_Data.3 +MLINKS+=sha.3 SHA1_End.3 sha.3 SHA1_File.3 sha.3 SHA1_FileChunk.3 +MLINKS+=sha.3 SHA1_Data.3 MLINKS+=sha256.3 SHA256_Init.3 sha256.3 SHA256_Update.3 MLINKS+=sha256.3 SHA256_Final.3 sha256.3 SHA256_End.3 -MLINKS+=sha256.3 SHA256_File.3 sha256.3 SHA256_FileChunk.3 +MLINKS+=sha256.3 SHA256_File.3 sha256.3 SHA256_FileChunk.3 MLINKS+=sha256.3 SHA256_Data.3 CLEANFILES+= md[245]hl.c md[245].ref md[245].3 mddriver \ rmd160.ref rmd160hl.c rmddriver \ @@ -66,9 +72,9 @@ sha1hl.c: mdXhl.c sha256hl.c: mdXhl.c (echo '#define LENGTH 32'; \ - sed -e 's/mdX/sha256/g' -e 's/MDX/SHA256_/g' \ + sed -e 's/mdX/sha256/g' -e 's/MDX/SHA256_/g' \ -e 's/SHA256__/SHA256_/g' \ - ${.ALLSRC}) > ${.TARGET} + ${.ALLSRC}) > ${.TARGET} rmd160hl.c: mdXhl.c (echo '#define LENGTH 20'; \ @@ -113,22 +119,30 @@ md5.ref: @echo 'MD5 ("12345678901234567890123456789012345678901234567890123456789012345678901234567890") = 57edf4a22be3c955ac49da2e2107b67a' >> ${.TARGET} sha0.ref: - echo 'SHA-0 test suite:' > ${.TARGET} - @echo 'SHA-0 ("") = f96cea198ad1dd5617ac084a3d92c6107708c0ef' >> ${.TARGET} - @echo 'SHA-0 ("abc") = 0164b8a914cd2a5e74c4f7ff082c4d97f1edf880' >> ${.TARGET} - @echo 'SHA-0 ("message digest") = c1b0f222d150ebb9aa36a40cafdc8bcbed830b14' >> ${.TARGET} - @echo 'SHA-0 ("abcdefghijklmnopqrstuvwxyz") = b40ce07a430cfd3c033039b9fe9afec95dc1bdcd' >> ${.TARGET} - @echo 'SHA-0 ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") = 79e966f7a3a990df33e40e3d7f8f18d2caebadfa' >> ${.TARGET} - @echo 'SHA-0 ("12345678901234567890123456789012345678901234567890123456789012345678901234567890") = 4aa29d14d171522ece47bee8957e35a41f3e9cff' >> ${.TARGET} + (echo 'SHA-0 test suite:'; \ + echo 'SHA-0 ("") = f96cea198ad1dd5617ac084a3d92c6107708c0ef'; \ + echo 'SHA-0 ("abc") = 0164b8a914cd2a5e74c4f7ff082c4d97f1edf880'; \ + echo 'SHA-0 ("message digest") =' \ + 'c1b0f222d150ebb9aa36a40cafdc8bcbed830b14'; \ + echo 'SHA-0 ("abcdefghijklmnopqrstuvwxyz") =' \ + 'b40ce07a430cfd3c033039b9fe9afec95dc1bdcd'; \ + echo 'SHA-0 ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") =' \ + '79e966f7a3a990df33e40e3d7f8f18d2caebadfa'; \ + echo 'SHA-0 ("12345678901234567890123456789012345678901234567890123456789012345678901234567890") =' \ + '4aa29d14d171522ece47bee8957e35a41f3e9cff' ) > ${.TARGET} sha1.ref: - echo 'SHA-1 test suite:' > ${.TARGET} - @echo 'SHA-1 ("") = da39a3ee5e6b4b0d3255bfef95601890afd80709' >> ${.TARGET} - @echo 'SHA-1 ("abc") = a9993e364706816aba3e25717850c26c9cd0d89d' >> ${.TARGET} - @echo 'SHA-1 ("message digest") = c12252ceda8be8994d5fa0290a47231c1d16aae3' >> ${.TARGET} - @echo 'SHA-1 ("abcdefghijklmnopqrstuvwxyz") = 32d10c7b8cf96570ca04ce37f2a19d84240d3a89' >> ${.TARGET} - @echo 'SHA-1 ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") = 761c457bf73b14d27e9e9265c46f4b4dda11f940' >> ${.TARGET} - @echo 'SHA-1 ("12345678901234567890123456789012345678901234567890123456789012345678901234567890") = 50abf5706a150990a08b2c5ea40fa0e585554732' >> ${.TARGET} + (echo 'SHA-1 test suite:'; \ + echo 'SHA-1 ("") = da39a3ee5e6b4b0d3255bfef95601890afd80709'; \ + echo 'SHA-1 ("abc") = a9993e364706816aba3e25717850c26c9cd0d89d'; \ + echo 'SHA-1 ("message digest") =' \ + 'c12252ceda8be8994d5fa0290a47231c1d16aae3'; \ + echo 'SHA-1 ("abcdefghijklmnopqrstuvwxyz") =' \ + '32d10c7b8cf96570ca04ce37f2a19d84240d3a89'; \ + echo 'SHA-1 ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") =' \ + '761c457bf73b14d27e9e9265c46f4b4dda11f940'; \ + echo 'SHA-1 ("12345678901234567890123456789012345678901234567890123456789012345678901234567890") =' \ + '50abf5706a150990a08b2c5ea40fa0e585554732' ) > ${.TARGET} sha256.ref: echo 'SHA-256 test suite:' > ${.TARGET} @@ -140,42 +154,42 @@ sha256.ref: @echo 'SHA-256 ("12345678901234567890123456789012345678901234567890123456789012345678901234567890") = f371bc4a311f2b009eef952dd83ca80e2b60026c8e935592d0f9c308453c813e' >> ${.TARGET} rmd160.ref: - echo 'RIPEMD160 test suite:' > ${.TARGET} - @echo 'RIPEMD160 ("") = 9c1185a5c5e9fc54612808977ee8f548b2258d31' >> ${.TARGET} - @echo 'RIPEMD160 ("abc") = 8eb208f7e05d987a9b044a8e98c6b087f15a0bfc' >> ${.TARGET} - @echo 'RIPEMD160 ("message digest") = 5d0689ef49d2fae572b881b123a85ffa21595f36' >> ${.TARGET} - @echo 'RIPEMD160 ("abcdefghijklmnopqrstuvwxyz") = f71c27109c692c1b56bbdceb5b9d2865b3708dbc' >> ${.TARGET} - @echo 'RIPEMD160 ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") = b0e20b6e3116640286ed3a87a5713079b21f5189' >> ${.TARGET} - @echo 'RIPEMD160 ("12345678901234567890123456789012345678901234567890123456789012345678901234567890") = 9b752e45573d4b39f4dbd3323cab82bf63326bfb' >> ${.TARGET} + (echo 'RIPEMD160 test suite:'; \ + echo 'RIPEMD160 ("") = 9c1185a5c5e9fc54612808977ee8f548b2258d31'; \ + echo 'RIPEMD160 ("abc") = 8eb208f7e05d987a9b044a8e98c6b087f15a0bfc'; \ + echo 'RIPEMD160 ("message digest") =' \ + '5d0689ef49d2fae572b881b123a85ffa21595f36'; \ + echo 'RIPEMD160 ("abcdefghijklmnopqrstuvwxyz") =' \ + 'f71c27109c692c1b56bbdceb5b9d2865b3708dbc'; \ + echo 'RIPEMD160 ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") =' \ + 'b0e20b6e3116640286ed3a87a5713079b21f5189'; \ + echo 'RIPEMD160 ("12345678901234567890123456789012345678901234567890123456789012345678901234567890") =' \ + '9b752e45573d4b39f4dbd3323cab82bf63326bfb' ) > ${.TARGET} test: md2.ref md4.ref md5.ref sha0.ref rmd160.ref sha1.ref sha256.ref @${ECHO} if any of these test fail, the code produces wrong results @${ECHO} and should NOT be used. - ${CC} -static ${CFLAGS} ${LDFLAGS} -DMD=2 -o mddriver ${.CURDIR}/mddriver.c -L. -lmd + ${CC} ${CFLAGS} ${LDFLAGS} -DMD=2 -o mddriver ${.CURDIR}/mddriver.c -L. -lmd ./mddriver | cmp md2.ref - @${ECHO} MD2 passed test - -rm -f mddriver - ${CC} -static ${CFLAGS} ${LDFLAGS} -DMD=4 -o mddriver ${.CURDIR}/mddriver.c -L. -lmd + ${CC} ${CFLAGS} ${LDFLAGS} -DMD=4 -o mddriver ${.CURDIR}/mddriver.c -L. -lmd ./mddriver | cmp md4.ref - @${ECHO} MD4 passed test - -rm -f mddriver - ${CC} -static ${CFLAGS} ${LDFLAGS} -DMD=5 -o mddriver ${.CURDIR}/mddriver.c -L. -lmd + ${CC} ${CFLAGS} ${LDFLAGS} -DMD=5 -o mddriver ${.CURDIR}/mddriver.c -L. -lmd ./mddriver | cmp md5.ref - @${ECHO} MD5 passed test -rm -f mddriver - ${CC} -static ${CFLAGS} ${LDFLAGS} -o rmddriver ${.CURDIR}/rmddriver.c -L. -lmd + ${CC} ${CFLAGS} ${LDFLAGS} -o rmddriver ${.CURDIR}/rmddriver.c -L. -lmd ./rmddriver | cmp rmd160.ref - @${ECHO} RIPEMD160 passed test -rm -f rmddriver - ${CC} -static ${CFLAGS} ${LDFLAGS} -DSHA=0 -o shadriver ${.CURDIR}/shadriver.c -L. -lmd + ${CC} ${CFLAGS} ${LDFLAGS} -DSHA=0 -o shadriver ${.CURDIR}/shadriver.c -L. -lmd ./shadriver | cmp sha0.ref - @${ECHO} SHA-0 passed test - -rm -f shadriver - ${CC} -static ${CFLAGS} ${LDFLAGS} -DSHA=1 -o shadriver ${.CURDIR}/shadriver.c -L. -lmd + ${CC} ${CFLAGS} ${LDFLAGS} -DSHA=1 -o shadriver ${.CURDIR}/shadriver.c -L. -lmd ./shadriver | cmp sha1.ref - @${ECHO} SHA-1 passed test - -rm -f shadriver - ${CC} -static ${CFLAGS} ${LDFLAGS} -DSHA=256 -o shadriver ${.CURDIR}/shadriver.c -L. -lmd + ${CC} ${CFLAGS} ${LDFLAGS} -DSHA=256 -o shadriver ${.CURDIR}/shadriver.c -L. -lmd ./shadriver | cmp sha256.ref - @${ECHO} SHA-256 passed test -rm -f shadriver diff --git a/lib/libmd/i386/rmd160.S b/lib/libmd/i386/rmd160.S index 97193e9bad..b4d6acc585 100644 --- a/lib/libmd/i386/rmd160.S +++ b/lib/libmd/i386/rmd160.S @@ -1,3 +1,5 @@ +/* $FreeBSD: src/lib/libmd/i386/rmd160.S,v 1.4 2007/05/14 05:00:37 cperciva Exp $ */ +/* $DragonFly: src/lib/libmd/i386/rmd160.S,v 1.2 2008/09/11 20:25:34 swildner Exp $ */ /* Run the C pre-processor over this file with one of the following defined * ELF - elf object files, * OUT - a.out object files, @@ -2005,7 +2007,7 @@ ripemd160_block_x86: movl 112(%esp), %esi cmpl %esi, %edi movl 108(%esp), %edi - jge .L000start + jae .L000start addl $88, %esp popl %ebx popl %ebp diff --git a/lib/libmd/i386/sha.S b/lib/libmd/i386/sha.S index ae8f89ee8d..12d6a16cce 100644 --- a/lib/libmd/i386/sha.S +++ b/lib/libmd/i386/sha.S @@ -1,3 +1,5 @@ +/* $FreeBSD: src/lib/libmd/i386/sha.S,v 1.3 2007/05/14 05:00:37 cperciva Exp $ */ +/* $DragonFly: src/lib/libmd/i386/sha.S,v 1.2 2008/09/11 20:25:34 swildner Exp $ /* -*- Fundamental -*- Emacs' assembler mode hoses this file */ #ifndef PIC /* Run the C pre-processor over this file with one of the following defined @@ -1935,7 +1937,7 @@ sha1_block_x86: movl %edi, 16(%ebp) cmpl %esi, %eax movl %ebx, 4(%ebp) - jl .L001end + jb .L001end movl (%esi), %eax jmp .L000start .L001end: diff --git a/lib/libmd/md2.h b/lib/libmd/md2.h index fb0624919a..f75edf8c8e 100644 --- a/lib/libmd/md2.h +++ b/lib/libmd/md2.h @@ -1,6 +1,6 @@ /* MD2.H - header file for MD2C.C - * $FreeBSD: src/lib/libmd/md2.h,v 1.8 1999/08/28 00:05:04 peter Exp $ - * $DragonFly: src/lib/libmd/md2.h,v 1.2 2003/06/17 04:26:50 dillon Exp $ + * $FreeBSD: src/lib/libmd/md2.h,v 1.10 2006/01/17 15:35:56 phk Exp $ + * $DragonFly: src/lib/libmd/md2.h,v 1.3 2008/09/11 20:25:34 swildner Exp $ */ /* Copyright (C) 1990-2, RSA Data Security, Inc. Created 1990. All @@ -35,12 +35,13 @@ typedef struct MD2Context { __BEGIN_DECLS void MD2Init(MD2_CTX *); -void MD2Update(MD2_CTX *, const unsigned char *, unsigned int); +void MD2Update(MD2_CTX *, const void *, unsigned int); void MD2Pad(MD2_CTX *); void MD2Final(unsigned char [16], MD2_CTX *); char * MD2End(MD2_CTX *, char *); char * MD2File(const char *, char *); -char * MD2Data(const unsigned char *, unsigned int, char *); +char * MD2FileChunk(const char *, char *, off_t, off_t); +char * MD2Data(const void *, unsigned int, char *); __END_DECLS #endif /* _MD2_H_ */ diff --git a/lib/libmd/md2c.c b/lib/libmd/md2c.c index 01a4cbc585..42f815aba3 100644 --- a/lib/libmd/md2c.c +++ b/lib/libmd/md2c.c @@ -1,6 +1,6 @@ /* MD2C.C - RSA Data Security, Inc., MD2 message-digest algorithm - * $FreeBSD: src/lib/libmd/md2c.c,v 1.7 1999/08/28 00:05:05 peter Exp $ - * $DragonFly: src/lib/libmd/md2c.c,v 1.2 2003/06/17 04:26:50 dillon Exp $ + * $FreeBSD: src/lib/libmd/md2c.c,v 1.11 2006/01/17 15:35:56 phk Exp $ + * $DragonFly: src/lib/libmd/md2c.c,v 1.3 2008/09/11 20:25:34 swildner Exp $ */ /* Copyright (C) 1990-2, RSA Data Security, Inc. Created 1990. All @@ -21,9 +21,9 @@ documentation and/or software. */ -#include "md2.h" -#include #include +#include +#include "md2.h" typedef unsigned char *POINTER; @@ -98,37 +98,38 @@ MD2_CTX *context; /* context */ operation, processing another message block, and updating the context. */ -void MD2Update (context, input, inputLen) +void MD2Update (context, in, inputLen) MD2_CTX *context; /* context */ -const unsigned char *input; /* input block */ +const void *in; /* input block */ unsigned int inputLen; /* length of input block */ { - unsigned int i, index, partLen; + unsigned int i, idx, partLen; + const unsigned char *input = in; /* Update number of bytes mod 16 */ - index = context->count; - context->count = (index + inputLen) & 0xf; + idx = context->count; + context->count = (idx + inputLen) & 0xf; - partLen = 16 - index; + partLen = 16 - idx; /* Transform as many times as possible. */ if (inputLen >= partLen) { memcpy - ((POINTER)&context->buffer[index], (POINTER)input, partLen); + ((POINTER)&context->buffer[idx], (POINTER)input, partLen); MD2Transform (context->state, context->checksum, context->buffer); for (i = partLen; i + 15 < inputLen; i += 16) MD2Transform (context->state, context->checksum, &input[i]); - index = 0; + idx = 0; } else i = 0; /* Buffer remaining input */ memcpy - ((POINTER)&context->buffer[index], (POINTER)&input[i], + ((POINTER)&context->buffer[idx], (POINTER)&input[i], inputLen-i); } @@ -137,12 +138,12 @@ unsigned int inputLen; /* length of input block */ void MD2Pad (context) MD2_CTX *context; /* context */ { - unsigned int index, padLen; + unsigned int idx, padLen; /* Pad out to multiple of 16. */ - index = context->count; - padLen = 16 - index; + idx = context->count; + padLen = 16 - idx; MD2Update (context, PADDING[padLen], padLen); /* Extend with checksum */ diff --git a/lib/libmd/md4.h b/lib/libmd/md4.h index 05e6b8039e..bb8fd716fd 100644 --- a/lib/libmd/md4.h +++ b/lib/libmd/md4.h @@ -1,6 +1,6 @@ /* MD4.H - header file for MD4C.C - * $FreeBSD: src/lib/libmd/md4.h,v 1.9 1999/08/28 00:05:05 peter Exp $ - * $DragonFly: src/lib/libmd/md4.h,v 1.2 2003/06/17 04:26:50 dillon Exp $ + * $FreeBSD: src/lib/libmd/md4.h,v 1.11 2006/01/17 15:35:56 phk Exp $ + * $DragonFly: src/lib/libmd/md4.h,v 1.3 2008/09/11 20:25:34 swildner Exp $ */ /* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All @@ -37,12 +37,13 @@ typedef struct MD4Context { __BEGIN_DECLS void MD4Init(MD4_CTX *); -void MD4Update(MD4_CTX *, const unsigned char *, unsigned int); +void MD4Update(MD4_CTX *, const void *, unsigned int); void MD4Pad(MD4_CTX *); void MD4Final(unsigned char [16], MD4_CTX *); char * MD4End(MD4_CTX *, char *); char * MD4File(const char *, char *); -char * MD4Data(const unsigned char *, unsigned int, char *); +char * MD4FileChunk(const char *, char *, off_t, off_t); +char * MD4Data(const void *, unsigned int, char *); __END_DECLS #endif /* _MD4_H_ */ diff --git a/lib/libmd/md4c.c b/lib/libmd/md4c.c index 5b630a5fef..000a205a1b 100644 --- a/lib/libmd/md4c.c +++ b/lib/libmd/md4c.c @@ -1,6 +1,6 @@ /* MD4C.C - RSA Data Security, Inc., MD4 message-digest algorithm - * $FreeBSD: src/lib/libmd/md4c.c,v 1.7 1999/08/28 00:05:05 peter Exp $ - * $DragonFly: src/lib/libmd/md4c.c,v 1.2 2003/06/17 04:26:50 dillon Exp $ + * $FreeBSD: src/lib/libmd/md4c.c,v 1.10 2006/01/17 15:35:56 phk Exp $ + * $DragonFly: src/lib/libmd/md4c.c,v 1.3 2008/09/11 20:25:34 swildner Exp $ */ /* Copyright (C) 1990-2, RSA Data Security, Inc. All rights reserved. @@ -29,6 +29,7 @@ #include "md4.h" typedef unsigned char *POINTER; +typedef const unsigned char *CONST_POINTER; typedef u_int16_t UINT2; typedef u_int32_t UINT4; @@ -105,40 +106,41 @@ MD4_CTX *context; /* context */ operation, processing another message block, and updating the context. */ -void MD4Update (context, input, inputLen) +void MD4Update (context, in, inputLen) MD4_CTX *context; /* context */ -const unsigned char *input; /* input block */ +const void *in; /* input block */ unsigned int inputLen; /* length of input block */ { - unsigned int i, index, partLen; + unsigned int i, idx, partLen; + const unsigned char *input = in; /* Compute number of bytes mod 64 */ - index = (unsigned int)((context->count[0] >> 3) & 0x3F); + idx = (unsigned int)((context->count[0] >> 3) & 0x3F); /* Update number of bits */ if ((context->count[0] += ((UINT4)inputLen << 3)) < ((UINT4)inputLen << 3)) context->count[1]++; context->count[1] += ((UINT4)inputLen >> 29); - partLen = 64 - index; + partLen = 64 - idx; /* Transform as many times as possible. */ if (inputLen >= partLen) { memcpy - ((POINTER)&context->buffer[index], (POINTER)input, partLen); + ((POINTER)&context->buffer[idx], (CONST_POINTER)input, partLen); MD4Transform (context->state, context->buffer); for (i = partLen; i + 63 < inputLen; i += 64) MD4Transform (context->state, &input[i]); - index = 0; + idx = 0; } else i = 0; /* Buffer remaining input */ memcpy - ((POINTER)&context->buffer[index], (POINTER)&input[i], + ((POINTER)&context->buffer[idx], (CONST_POINTER)&input[i], inputLen-i); } @@ -147,15 +149,15 @@ void MD4Pad (context) MD4_CTX *context; /* context */ { unsigned char bits[8]; - unsigned int index, padLen; + unsigned int idx, padLen; /* Save number of bits */ Encode (bits, context->count, 8); /* Pad out to 56 mod 64. */ - index = (unsigned int)((context->count[0] >> 3) & 0x3f); - padLen = (index < 56) ? (56 - index) : (120 - index); + idx = (unsigned int)((context->count[0] >> 3) & 0x3f); + padLen = (idx < 56) ? (56 - idx) : (120 - idx); MD4Update (context, PADDING, padLen); /* Append length (before padding) */ diff --git a/lib/libmd/md5c.c b/lib/libmd/md5c.c index 136b18ffaa..1fd98f5114 100644 --- a/lib/libmd/md5c.c +++ b/lib/libmd/md5c.c @@ -22,8 +22,8 @@ * These notices must be retained in any copies of any part of this * documentation and/or software. * - * $FreeBSD: src/lib/libmd/md5c.c,v 1.11 1999/12/29 05:04:20 peter Exp $ - * $DragonFly: src/lib/libmd/md5c.c,v 1.3 2003/11/12 20:21:31 eirikn Exp $ + * $FreeBSD: src/lib/libmd/md5c.c,v 1.17 2006/01/17 15:35:56 phk Exp $ + * $DragonFly: src/lib/libmd/md5c.c,v 1.4 2008/09/11 20:25:34 swildner Exp $ * * This code is the same as the code published by RSA Inc. It has been * edited for clarity and style only. @@ -37,19 +37,21 @@ #include #endif +#include +#include #include -static void MD5Transform (u_int32_t [4], const unsigned char [64]); +static void MD5Transform(u_int32_t [4], const unsigned char [64]); #ifdef _KERNEL #define memset(x,y,z) bzero(x,z); #define memcpy(x,y,z) bcopy(y, x, z) #endif -#ifdef i386 +#if (BYTE_ORDER == LITTLE_ENDIAN) #define Encode memcpy #define Decode memcpy -#else /* i386 */ +#else /* * Encodes input (u_int32_t) into output (unsigned char). Assumes len is @@ -57,19 +59,13 @@ static void MD5Transform (u_int32_t [4], const unsigned char [64]); */ static void -Encode (output, input, len) - unsigned char *output; - u_int32_t *input; - unsigned int len; +Encode (unsigned char *output, u_int32_t *input, unsigned int len) { - unsigned int i, j; + unsigned int i; + u_int32_t *op = (u_int32_t *)output; - for (i = 0, j = 0; j < len; i++, j += 4) { - output[j] = (unsigned char)(input[i] & 0xff); - output[j+1] = (unsigned char)((input[i] >> 8) & 0xff); - output[j+2] = (unsigned char)((input[i] >> 16) & 0xff); - output[j+3] = (unsigned char)((input[i] >> 24) & 0xff); - } + for (i = 0; i < len / 4; i++) + op[i] = htole32(input[i]); } /* @@ -78,18 +74,15 @@ Encode (output, input, len) */ static void -Decode (output, input, len) - u_int32_t *output; - const unsigned char *input; - unsigned int len; +Decode (u_int32_t *output, const unsigned char *input, unsigned int len) { - unsigned int i, j; + unsigned int i; + const u_int32_t *ip = (const u_int32_t *)input; - for (i = 0, j = 0; j < len; i++, j += 4) - output[i] = ((u_int32_t)input[j]) | (((u_int32_t)input[j+1]) << 8) | - (((u_int32_t)input[j+2]) << 16) | (((u_int32_t)input[j+3]) << 24); + for (i = 0; i < len / 4; i++) + output[i] = le32toh(ip[i]); } -#endif /* i386 */ +#endif static unsigned char PADDING[64] = { 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, @@ -154,15 +147,16 @@ MD5Init (context) */ void -MD5Update (context, input, inputLen) +MD5Update (context, in, inputLen) MD5_CTX *context; - const unsigned char *input; + const void *in; unsigned int inputLen; { - unsigned int i, index, partLen; + unsigned int i, idx, partLen; + const unsigned char *input = in; /* Compute number of bytes mod 64 */ - index = (unsigned int)((context->count[0] >> 3) & 0x3F); + idx = (unsigned int)((context->count[0] >> 3) & 0x3F); /* Update number of bits */ if ((context->count[0] += ((u_int32_t)inputLen << 3)) @@ -170,24 +164,24 @@ MD5Update (context, input, inputLen) context->count[1]++; context->count[1] += ((u_int32_t)inputLen >> 29); - partLen = 64 - index; + partLen = 64 - idx; /* Transform as many times as possible. */ if (inputLen >= partLen) { - memcpy((void *)&context->buffer[index], (const void *)input, + memcpy((void *)&context->buffer[idx], (const void *)input, partLen); MD5Transform (context->state, context->buffer); for (i = partLen; i + 63 < inputLen; i += 64) MD5Transform (context->state, &input[i]); - index = 0; + idx = 0; } else i = 0; /* Buffer remaining input */ - memcpy ((void *)&context->buffer[index], (const void *)&input[i], + memcpy ((void *)&context->buffer[idx], (const void *)&input[i], inputLen-i); } @@ -200,14 +194,14 @@ MD5Pad (context) MD5_CTX *context; { unsigned char bits[8]; - unsigned int index, padLen; + unsigned int idx, padLen; /* Save number of bits */ Encode (bits, context->count, 8); /* Pad out to 56 mod 64. */ - index = (unsigned int)((context->count[0] >> 3) & 0x3f); - padLen = (index < 56) ? (56 - index) : (120 - index); + idx = (unsigned int)((context->count[0] >> 3) & 0x3f); + padLen = (idx < 56) ? (56 - idx) : (120 - idx); MD5Update (context, PADDING, padLen); /* Append length (before padding) */ diff --git a/lib/libmd/mdX.3 b/lib/libmd/mdX.3 index 719c89e1c8..f2b5ef4729 100644 --- a/lib/libmd/mdX.3 +++ b/lib/libmd/mdX.3 @@ -1,13 +1,13 @@ .\" .\" ---------------------------------------------------------------------------- .\" "THE BEER-WARE LICENSE" (Revision 42): -.\" wrote this file. As long as you retain this notice you +.\" wrote this file. As long as you retain this notice you .\" can do whatever you want with this stuff. If we meet some day, and you think .\" this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp .\" ---------------------------------------------------------------------------- .\" -.\" $FreeBSD: src/lib/libmd/mdX.3,v 1.16.2.5 2001/12/17 10:08:31 ru Exp $ -.\" $DragonFly: src/lib/libmd/mdX.3,v 1.3 2006/02/17 19:35:07 swildner Exp $ +.\" $FreeBSD: src/lib/libmd/mdX.3,v 1.30 2006/01/17 15:35:56 phk Exp $ +.\" $DragonFly: src/lib/libmd/mdX.3,v 1.4 2008/09/11 20:25:34 swildner Exp $ .\" .Dd February 11, 1999 .Dt MDX 3 @@ -19,6 +19,7 @@ .Nm MDXFinal , .Nm MDXEnd , .Nm MDXFile , +.Nm MDXFileChunk , .Nm MDXData .Nd calculate the RSA Data Security, Inc., ``MDX'' message digest .Sh LIBRARY @@ -29,7 +30,7 @@ .Ft void .Fn MDXInit "MDX_CTX *context" .Ft void -.Fn MDXUpdate "MDX_CTX *context" "const unsigned char *data" "unsigned int len" +.Fn MDXUpdate "MDX_CTX *context" "const void *data" "unsigned int len" .Ft void .Fn MDXPad "MDX_CTX *context" .Ft void @@ -39,22 +40,30 @@ .Ft "char *" .Fn MDXFile "const char *filename" "char *buf" .Ft "char *" -.Fn MDXData "const unsigned char *data" "unsigned int len" "char *buf" +.Fn MDXFileChunk "const char *filename" "char *buf" "off_t offset" "off_t length" +.Ft "char *" +.Fn MDXData "const void *data" "unsigned int len" "char *buf" .Sh DESCRIPTION The MDX functions calculate a 128-bit cryptographic checksum (digest) -for any number of input bytes. A cryptographic checksum is a one-way +for any number of input bytes. +A cryptographic checksum is a one-way hash-function, that is, you cannot find (except by exhaustive search) -the input corresponding to a particular output. This net result is -a ``fingerprint'' of the input-data, which doesn't disclose the actual -input. +the input corresponding to a particular output. +This net result is a +.Dq fingerprint +of the input-data, which does not disclose the actual input. .Pp MD2 is the slowest, MD4 is the fastest and MD5 is somewhere in the middle. MD2 can only be used for Privacy-Enhanced Mail. MD4 has now been broken; it should only be used where necessary for backward compatibility. MD5 has not yet (1999-02-11) been broken, but sufficient attacks have been -made that its security is in some doubt. The attacks on both MD4 and MD5 -are both in the nature of finding ``collisions'' \- that is, multiple +made that its security is in some doubt. +The attacks on both MD4 and MD5 +are both in the nature of finding +.Dq collisions +\[en] +that is, multiple inputs which hash to the same value; it is still unlikely for an attacker to be able to determine the exact original input given a hash value. .Pp @@ -63,34 +72,59 @@ The .Fn MDXUpdate , and .Fn MDXFinal -functions are the core functions. Allocate an MDX_CTX, initialize it with +functions are the core functions. +Allocate an +.Vt MDX_CTX , +initialize it with .Fn MDXInit , run over the data with .Fn MDXUpdate , and finally extract the result using .Fn MDXFinal . .Pp +The .Fn MDXPad -can be used to pad message data in same way +function can be used to pad message data in same way as done by .Fn MDXFinal without terminating calculation. .Pp +The .Fn MDXEnd -is a wrapper for +function is a wrapper for .Fn MDXFinal which converts the return value to a 33-character (including the terminating '\e0') .Tn ASCII string which represents the 128 bits in hexadecimal. .Pp +The .Fn MDXFile -calculates the digest of a file, and uses +function calculates the digest of a file, and uses .Fn MDXEnd to return the result. If the file cannot be opened, a null pointer is returned. +The +.Fn MDXFileChunk +function is similar to +.Fn MDXFile , +but it only calculates the digest over a byte-range of the file specified, +starting at +.Fa offset +and spanning +.Fa length +bytes. +If the +.Fa length +parameter is specified as 0, or more than the length of the remaining part +of the file, +.Fn MDXFileChunk +calculates the digest from +.Fa offset +to the end of file. +The .Fn MDXData -calculates the digest of a chunk of data in memory, and uses +function calculates the digest of a chunk of data in memory, and uses .Fn MDXEnd to return the result. .Pp @@ -157,13 +191,13 @@ The original MDX routines were developed by .Tn RSA Data Security, Inc., and published in the above references. This code is derived directly from these implementations by -.An Poul-Henning Kamp Aq phk@login.dkuug.dk +.An Poul-Henning Kamp Aq phk@FreeBSD.org .Pp Phk ristede runen. .Sh BUGS No method is known to exist which finds two files having the same hash value, nor to find a file with a specific hash value. -There is on the other hand no guarantee that such a method doesn't exist. +There is on the other hand no guarantee that such a method does not exist. .Pp MD2 has only been licensed for use in Privacy Enhanced Mail. -Use MD4 or MD5 if that isn't what you're doing. +Use MD4 or MD5 if that is not what you are doing. diff --git a/lib/libmd/mdXhl.c b/lib/libmd/mdXhl.c index cf07d23e7c..96b71caeb3 100644 --- a/lib/libmd/mdXhl.c +++ b/lib/libmd/mdXhl.c @@ -1,17 +1,17 @@ -/* mdXhl.c - * ---------------------------------------------------------------------------- +/* mdXhl.c * ---------------------------------------------------------------------------- * "THE BEER-WARE LICENSE" (Revision 42): - * wrote this file. As long as you retain this notice you + * wrote this file. As long as you retain this notice you * can do whatever you want with this stuff. If we meet some day, and you think * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp * ---------------------------------------------------------------------------- * - * $FreeBSD: src/lib/libmd/mdXhl.c,v 1.13 1999/08/28 00:05:07 peter Exp $ - * $DragonFly: src/lib/libmd/mdXhl.c,v 1.2 2003/06/17 04:26:50 dillon Exp $ + * $FreeBSD: src/lib/libmd/mdXhl.c,v 1.19 2006/01/17 15:35:56 phk Exp $ + * $DragonFly: src/lib/libmd/mdXhl.c,v 1.3 2008/09/11 20:25:34 swildner Exp $ * */ #include +#include #include #include @@ -24,49 +24,76 @@ char * MDXEnd(MDX_CTX *ctx, char *buf) { - int i; - unsigned char digest[LENGTH]; - static const char hex[]="0123456789abcdef"; + int i; + unsigned char digest[LENGTH]; + static const char hex[]="0123456789abcdef"; - if (!buf) - buf = malloc(2*LENGTH + 1); - if (!buf) - return 0; - MDXFinal(digest, ctx); - for (i = 0; i < LENGTH; i++) { - buf[i+i] = hex[digest[i] >> 4]; - buf[i+i+1] = hex[digest[i] & 0x0f]; - } - buf[i+i] = '\0'; - return buf; + if (!buf) + buf = malloc(2*LENGTH + 1); + if (!buf) + return 0; + MDXFinal(digest, ctx); + for (i = 0; i < LENGTH; i++) { + buf[i+i] = hex[digest[i] >> 4]; + buf[i+i+1] = hex[digest[i] & 0x0f]; + } + buf[i+i] = '\0'; + return buf; } char * MDXFile(const char *filename, char *buf) { - unsigned char buffer[BUFSIZ]; - MDX_CTX ctx; - int f,i,j; + return (MDXFileChunk(filename, buf, 0, 0)); +} + +char * +MDXFileChunk(const char *filename, char *buf, off_t ofs, off_t len) +{ + unsigned char buffer[BUFSIZ]; + MDX_CTX ctx; + struct stat stbuf; + int f, i, e; + off_t n; - MDXInit(&ctx); - f = open(filename,O_RDONLY); - if (f < 0) return 0; - while ((i = read(f,buffer,sizeof buffer)) > 0) { - MDXUpdate(&ctx,buffer,i); - } - j = errno; - close(f); - errno = j; - if (i < 0) return 0; - return MDXEnd(&ctx, buf); + MDXInit(&ctx); + f = open(filename, O_RDONLY); + if (f < 0) + return 0; + if (fstat(f, &stbuf) < 0) + return 0; + if (ofs > stbuf.st_size) + ofs = stbuf.st_size; + if ((len == 0) || (len > stbuf.st_size - ofs)) + len = stbuf.st_size - ofs; + if (lseek(f, ofs, SEEK_SET) < 0) + return 0; + n = len; + i = 0; + while (n > 0) { + if (n > sizeof(buffer)) + i = read(f, buffer, sizeof(buffer)); + else + i = read(f, buffer, n); + if (i < 0) + break; + MDXUpdate(&ctx, buffer, i); + n -= i; + } + e = errno; + close(f); + errno = e; + if (i < 0) + return 0; + return (MDXEnd(&ctx, buf)); } char * -MDXData (const unsigned char *data, unsigned int len, char *buf) +MDXData (const void *data, unsigned int len, char *buf) { - MDX_CTX ctx; + MDX_CTX ctx; - MDXInit(&ctx); - MDXUpdate(&ctx,data,len); - return MDXEnd(&ctx, buf); + MDXInit(&ctx); + MDXUpdate(&ctx,data,len); + return (MDXEnd(&ctx, buf)); } diff --git a/lib/libmd/mddriver.c b/lib/libmd/mddriver.c index aedd9bd841..52f7ee8af2 100644 --- a/lib/libmd/mddriver.c +++ b/lib/libmd/mddriver.c @@ -1,6 +1,6 @@ /* MDDRIVER.C - test driver for MD2, MD4 and MD5 - * $FreeBSD: src/lib/libmd/mddriver.c,v 1.6 1999/08/28 00:05:07 peter Exp $ - * $DragonFly: src/lib/libmd/mddriver.c,v 1.2 2003/06/17 04:26:50 dillon Exp $ + * $FreeBSD: src/lib/libmd/mddriver.c,v 1.7 2001/09/30 21:56:22 dillon Exp $ + * $DragonFly: src/lib/libmd/mddriver.c,v 1.3 2008/09/11 20:25:34 swildner Exp $ */ /* Copyright (C) 1990-2, RSA Data Security, Inc. Created 1990. All diff --git a/lib/libmd/ripemd.3 b/lib/libmd/ripemd.3 index c9fb13974b..1cd473e1e2 100644 --- a/lib/libmd/ripemd.3 +++ b/lib/libmd/ripemd.3 @@ -1,14 +1,14 @@ .\" .\" ---------------------------------------------------------------------------- .\" "THE BEER-WARE LICENSE" (Revision 42): -.\" wrote this file. As long as you retain this notice you +.\" wrote this file. As long as you retain this notice you .\" can do whatever you want with this stuff. If we meet some day, and you think .\" this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp .\" ---------------------------------------------------------------------------- .\" .\" From: Id: mdX.3,v 1.14 1999/02/11 20:31:49 wollman Exp -.\" $FreeBSD: src/lib/libmd/ripemd.3,v 1.2.2.5 2001/12/17 10:08:31 ru Exp $ -.\" $DragonFly: src/lib/libmd/ripemd.3,v 1.3 2006/02/17 19:35:07 swildner Exp $ +.\" $FreeBSD: src/lib/libmd/ripemd.3,v 1.15 2005/02/13 22:25:13 ru Exp $ +.\" $DragonFly: src/lib/libmd/ripemd.3,v 1.4 2008/09/11 20:25:34 swildner Exp $ .\" .Dd February 26, 1999 .Dt RIPEMD 3 @@ -19,6 +19,7 @@ .Nm RIPEMD160_Final , .Nm RIPEMD160_End , .Nm RIPEMD160_File , +.Nm RIPEMD160_FileChunk , .Nm RIPEMD160_Data .Nd calculate the RIPEMD160 message digest .Sh LIBRARY @@ -37,44 +38,72 @@ .Ft "char *" .Fn RIPEMD160_File "const char *filename" "char *buf" .Ft "char *" +.Fn RIPEMD160_FileChunk "const char *filename" "char *buf" "off_t offset" "off_t length" +.Ft "char *" .Fn RIPEMD160_Data "const unsigned char *data" "unsigned int len" "char *buf" .Sh DESCRIPTION The .Li RIPEMD160_ functions calculate a 160-bit cryptographic checksum (digest) -for any number of input bytes. A cryptographic checksum is a one-way +for any number of input bytes. +A cryptographic checksum is a one-way hash function; that is, it is computationally impractical to find -the input corresponding to a particular output. This net result is -a ``fingerprint'' of the input-data, which doesn't disclose the actual -input. +the input corresponding to a particular output. +This net result is a +.Dq fingerprint +of the input-data, which does not disclose the actual input. .Pp The -.Fn RIPEMD160_Init , +.Fn RIPEMD160_Init , .Fn RIPEMD160_Update , and .Fn RIPEMD160_Final -functions are the core functions. Allocate an RIPEMD160_CTX, initialize it with +functions are the core functions. +Allocate an +.Vt RIPEMD160_CTX , +initialize it with .Fn RIPEMD160_Init , run over the data with .Fn RIPEMD160_Update , and finally extract the result using .Fn RIPEMD160_Final . .Pp +The .Fn RIPEMD160_End -is a wrapper for +function is a wrapper for .Fn RIPEMD160_Final which converts the return value to a 41-character (including the terminating '\e0') .Tn ASCII string which represents the 160 bits in hexadecimal. .Pp +The .Fn RIPEMD160_File -calculates the digest of a file, and uses +function calculates the digest of a file, and uses .Fn RIPEMD160_End to return the result. If the file cannot be opened, a null pointer is returned. +The +.Fn RIPEMD160_FileChunk +function is similar to +.Fn RIPEMD160_File , +but it only calculates the digest over a byte-range of the file specified, +starting at +.Fa offset +and spanning +.Fa length +bytes. +If the +.Fa length +parameter is specified as 0, or more than the length of the remaining part +of the file, +.Fn RIPEMD160_FileChunk +calculates the digest from +.Fa offset +to the end of file. +The .Fn RIPEMD160_Data -calculates the digest of a chunk of data in memory, and uses +function calculates the digest of a chunk of data in memory, and uses .Fn RIPEMD160_End to return the result. .Pp @@ -110,4 +139,4 @@ specification. .Sh BUGS No method is known to exist which finds two files having the same hash value, nor to find a file with a specific hash value. -There is on the other hand no guarantee that such a method doesn't exist. +There is on the other hand no guarantee that such a method does not exist. diff --git a/lib/libmd/ripemd.h b/lib/libmd/ripemd.h index f73744a75c..9d252b5835 100644 --- a/lib/libmd/ripemd.h +++ b/lib/libmd/ripemd.h @@ -54,8 +54,11 @@ * derivative of this code cannot be changed. i.e. this code cannot simply be * copied and put under another distribution licence * [including the GNU Public Licence.] - * - * $DragonFly: src/lib/libmd/ripemd.h,v 1.2 2003/11/09 02:34:03 dillon Exp $ + */ + +/* + * $FreeBSD: src/lib/libmd/ripemd.h,v 1.3 2006/01/17 15:35:56 phk Exp $ + * $DragonFly: src/lib/libmd/ripemd.h,v 1.3 2008/09/11 20:25:34 swildner Exp $ */ #ifndef HEADER_RIPEMD_H @@ -80,12 +83,13 @@ typedef struct RIPEMD160state_st { __BEGIN_DECLS void RIPEMD160_Init(RIPEMD160_CTX *c); -void RIPEMD160_Update(RIPEMD160_CTX *c, const unsigned char *data, +void RIPEMD160_Update(RIPEMD160_CTX *c, const void *data, size_t len); void RIPEMD160_Final(unsigned char *md, RIPEMD160_CTX *c); char *RIPEMD160_End(RIPEMD160_CTX *, char *); char *RIPEMD160_File(const char *, char *); -char *RIPEMD160_Data(const unsigned char *, unsigned int, char *); +char *RIPEMD160_FileChunk(const char *, char *, off_t, off_t); +char *RIPEMD160_Data(const void *, unsigned int, char *); __END_DECLS #endif diff --git a/lib/libmd/rmd160c.c b/lib/libmd/rmd160c.c index 6b4996ea98..c7aece5d08 100644 --- a/lib/libmd/rmd160c.c +++ b/lib/libmd/rmd160c.c @@ -55,8 +55,8 @@ * copied and put under another distribution licence * [including the GNU Public Licence.] * - * $FreeBSD: src/lib/libmd/rmd160c.c,v 1.4 1999/08/28 00:05:07 peter Exp $ - * $DragonFly: src/lib/libmd/rmd160c.c,v 1.4 2004/10/25 19:38:45 drhodus Exp $ + * $FreeBSD: src/lib/libmd/rmd160c.c,v 1.7 2006/01/17 15:35:56 phk Exp $ + * $DragonFly: src/lib/libmd/rmd160c.c,v 1.5 2008/09/11 20:25:34 swildner Exp $ */ #include @@ -98,14 +98,15 @@ RIPEMD160_CTX *c; c->num=0; } -void RIPEMD160_Update(c, data, len) +void RIPEMD160_Update(c, in, len) RIPEMD160_CTX *c; -const unsigned char *data; +const void *in; size_t len; { u_int32_t *p; int sw,sc; u_int32_t l; + const unsigned char *data = in; if (len == 0) return; diff --git a/lib/libmd/rmddriver.c b/lib/libmd/rmddriver.c index 05613e4198..665c86fc9d 100644 --- a/lib/libmd/rmddriver.c +++ b/lib/libmd/rmddriver.c @@ -1,6 +1,6 @@ /* RIPEMD160DRIVER.C - test driver for RIPEMD160 - * $FreeBSD: src/lib/libmd/rmddriver.c,v 1.2 1999/08/28 00:05:08 peter Exp $ - * $DragonFly: src/lib/libmd/rmddriver.c,v 1.2 2003/06/17 04:26:50 dillon Exp $ + * $FreeBSD: src/lib/libmd/rmddriver.c,v 1.3 2001/09/30 21:56:22 dillon Exp $ + * $DragonFly: src/lib/libmd/rmddriver.c,v 1.3 2008/09/11 20:25:34 swildner Exp $ */ /* Copyright (C) 1990-2, RSA Data Security, Inc. Created 1990. All diff --git a/lib/libmd/sha.3 b/lib/libmd/sha.3 index d06d316a76..011ef40270 100644 --- a/lib/libmd/sha.3 +++ b/lib/libmd/sha.3 @@ -1,14 +1,14 @@ .\" .\" ---------------------------------------------------------------------------- .\" "THE BEER-WARE LICENSE" (Revision 42): -.\" wrote this file. As long as you retain this notice you +.\" wrote this file. As long as you retain this notice you .\" can do whatever you want with this stuff. If we meet some day, and you think .\" this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp .\" ---------------------------------------------------------------------------- .\" .\" From: Id: mdX.3,v 1.14 1999/02/11 20:31:49 wollman Exp -.\" $FreeBSD: src/lib/libmd/sha.3,v 1.4.2.5 2001/12/17 10:08:31 ru Exp $ -.\" $DragonFly: src/lib/libmd/sha.3,v 1.3 2006/02/17 19:35:07 swildner Exp $ +.\" $FreeBSD: src/lib/libmd/sha.3,v 1.19 2005/06/16 19:01:06 ru Exp $ +.\" $DragonFly: src/lib/libmd/sha.3,v 1.4 2008/09/11 20:25:34 swildner Exp $ .\" .Dd February 25, 1999 .Dt SHA 3 @@ -19,12 +19,14 @@ .Nm SHA_Final , .Nm SHA_End , .Nm SHA_File , +.Nm SHA_FileChunk , .Nm SHA_Data , .Nm SHA1_Init , .Nm SHA1_Update , .Nm SHA1_Final , .Nm SHA1_End , .Nm SHA1_File , +.Nm SHA1_FileChunk , .Nm SHA1_Data .Nd calculate the FIPS 160 and 160-1 ``SHA'' message digests .Sh LIBRARY @@ -35,7 +37,7 @@ .Ft void .Fn SHA_Init "SHA_CTX *context" .Ft void -.Fn SHA_Update "SHA_CTX *context" "const unsigned char *data" "unsigned int len" +.Fn SHA_Update "SHA_CTX *context" "const unsigned char *data" "size_t len" .Ft void .Fn SHA_Final "unsigned char digest[20]" "SHA_CTX *context" .Ft "char *" @@ -43,11 +45,13 @@ .Ft "char *" .Fn SHA_File "const char *filename" "char *buf" .Ft "char *" +.Fn SHA_FileChunk "const char *filename" "char *buf" "off_t offset" "off_t length" +.Ft "char *" .Fn SHA_Data "const unsigned char *data" "unsigned int len" "char *buf" .Ft void .Fn SHA1_Init "SHA_CTX *context" .Ft void -.Fn SHA1_Update "SHA_CTX *context" "const unsigned char *data" "unsigned int len" +.Fn SHA1_Update "SHA_CTX *context" "const unsigned char *data" "size_t len" .Ft void .Fn SHA1_Final "unsigned char digest[20]" "SHA_CTX *context" .Ft "char *" @@ -55,6 +59,8 @@ .Ft "char *" .Fn SHA1_File "const char *filename" "char *buf" .Ft "char *" +.Fn SHA1_FileChunk "const char *filename" "char *buf" "off_t offset" "off_t length" +.Ft "char *" .Fn SHA1_Data "const unsigned char *data" "unsigned int len" "char *buf" .Sh DESCRIPTION The @@ -62,28 +68,35 @@ The and .Li SHA1_ functions calculate a 160-bit cryptographic checksum (digest) -for any number of input bytes. A cryptographic checksum is a one-way +for any number of input bytes. +A cryptographic checksum is a one-way hash function; that is, it is computationally impractical to find -the input corresponding to a particular output. This net result is -a ``fingerprint'' of the input-data, which doesn't disclose the actual -input. +the input corresponding to a particular output. +This net result is +a +.Dq fingerprint +of the input-data, which does not disclose the actual input. .Pp .Tn SHA (or .Tn SHA-0 ) is the original Secure Hash Algorithm specified in .Tn FIPS -160. It was quickly proven insecure, and has been superseded by +160. +It was quickly proven insecure, and has been superseded by .Tn SHA-1 . .Tn SHA-0 is included for compatibility purposes only. .Pp The -.Fn SHA1_Init , +.Fn SHA1_Init , .Fn SHA1_Update , and .Fn SHA1_Final -functions are the core functions. Allocate an SHA_CTX, initialize it with +functions are the core functions. +Allocate an +.Vt SHA_CTX , +initialize it with .Fn SHA1_Init , run over the data with .Fn SHA1_Update , @@ -103,6 +116,23 @@ calculates the digest of a file, and uses .Fn SHA1_End to return the result. If the file cannot be opened, a null pointer is returned. +.Fn SHA1_FileChunk +is similar to +.Fn SHA1_File , +but it only calculates the digest over a byte-range of the file specified, +starting at +.Fa offset +and spanning +.Fa length +bytes. +If the +.Fa length +parameter is specified as 0, or more than the length of the remaining part +of the file, +.Fn SHA1_FileChunk +calculates the digest from +.Fa offset +to the end of file. .Fn SHA1_Data calculates the digest of a chunk of data in memory, and uses .Fn SHA1_End @@ -128,7 +158,8 @@ argument is non-null it must point to at least 41 characters of buffer space. .Xr md2 3 , .Xr md4 3 , .Xr md5 3 , -.Xr ripemd 3 +.Xr ripemd 3 , +.Xr sha256 3 .Sh HISTORY These functions appeared in .Fx 4.0 . @@ -140,7 +171,7 @@ standards. .Sh BUGS No method is known to exist which finds two files having the same hash value, nor to find a file with a specific hash value. -There is on the other hand no guarantee that such a method doesn't exist. +There is on the other hand no guarantee that such a method does not exist. .Pp The .Tn IA32 @@ -148,8 +179,8 @@ The .Tn SHA-1 makes heavy use of the .Ql bswapl -instruction, which is not present on the original 80386. Attempts -to use +instruction, which is not present on the original 80386. +Attempts to use .Tn SHA-1 on those processors will cause an illegal instruction trap. (Arguably, the kernel should simply emulate this instruction.) diff --git a/lib/libmd/sha.h b/lib/libmd/sha.h index 8cd082e2b0..8451b41ce2 100644 --- a/lib/libmd/sha.h +++ b/lib/libmd/sha.h @@ -54,8 +54,8 @@ * copied and put under another distribution licence * [including the GNU Public Licence.] * - * $FreeBSD: src/lib/libmd/sha.h,v 1.3 1999/08/28 00:05:08 peter Exp $ - * $DragonFly: src/lib/libmd/sha.h,v 1.3 2003/11/09 02:34:03 dillon Exp $ + * $FreeBSD: src/lib/libmd/sha.h,v 1.5 2006/01/17 15:35:56 phk Exp $ + * $DragonFly: src/lib/libmd/sha.h,v 1.4 2008/09/11 20:25:34 swildner Exp $ */ #ifndef _SHA_H_ @@ -81,17 +81,19 @@ typedef struct SHAstate_st { __BEGIN_DECLS void SHA_Init(SHA_CTX *c); -void SHA_Update(SHA_CTX *c, const unsigned char *data, size_t len); +void SHA_Update(SHA_CTX *c, const void *data, size_t len); void SHA_Final(unsigned char *md, SHA_CTX *c); char *SHA_End(SHA_CTX *, char *); char *SHA_File(const char *, char *); -char *SHA_Data(const unsigned char *, unsigned int, char *); +char *SHA_FileChunk(const char *, char *, off_t, off_t); +char *SHA_Data(const void *, unsigned int, char *); void SHA1_Init(SHA_CTX *c); -void SHA1_Update(SHA_CTX *c, const unsigned char *data, size_t len); +void SHA1_Update(SHA_CTX *c, const void *data, size_t len); void SHA1_Final(unsigned char *md, SHA_CTX *c); char *SHA1_End(SHA_CTX *, char *); char *SHA1_File(const char *, char *); -char *SHA1_Data(const unsigned char *, unsigned int, char *); +char *SHA1_FileChunk(const char *, char *, off_t, off_t); +char *SHA1_Data(const void *, unsigned int, char *); __END_DECLS #endif /* !_SHA_H_ */ diff --git a/lib/libmd/sha0c.c b/lib/libmd/sha0c.c index 6c279fccc2..fa5b3762f8 100644 --- a/lib/libmd/sha0c.c +++ b/lib/libmd/sha0c.c @@ -55,8 +55,8 @@ * copied and put under another distribution licence * [including the GNU Public Licence.] * - * $FreeBSD: src/lib/libmd/sha0c.c,v 1.3 1999/08/28 00:05:09 peter Exp $ - * $DragonFly: src/lib/libmd/sha0c.c,v 1.4 2004/10/25 19:38:45 drhodus Exp $ + * $FreeBSD: src/lib/libmd/sha0c.c,v 1.6 2006/01/17 15:35:56 phk Exp $ + * $DragonFly: src/lib/libmd/sha0c.c,v 1.5 2008/09/11 20:25:34 swildner Exp $ */ #include @@ -112,14 +112,15 @@ SHA_CTX *c; c->num=0; } -void SHA_Update(c, data, len) +void SHA_Update(c, in, len) SHA_CTX *c; -const unsigned char *data; +const void *in; size_t len; { u_int32_t *p; int ew,ec,sw,sc; u_int32_t l; + const unsigned char *data = in; if (len == 0) return; diff --git a/lib/libmd/sha1c.c b/lib/libmd/sha1c.c index 64f9f22641..9a8043361b 100644 --- a/lib/libmd/sha1c.c +++ b/lib/libmd/sha1c.c @@ -55,7 +55,8 @@ * copied and put under another distribution licence * [including the GNU Public Licence.] * - * $DragonFly: src/lib/libmd/sha1c.c,v 1.3 2004/10/25 19:38:45 drhodus Exp $ + * $FreeBSD: src/lib/libmd/sha1c.c,v 1.5 2006/01/17 15:35:56 phk Exp $ + * $DragonFly: src/lib/libmd/sha1c.c,v 1.4 2008/09/11 20:25:34 swildner Exp $ */ #include @@ -139,14 +140,15 @@ SHA_CTX *c; } void -SHA1_Update(c, data, len) +SHA1_Update(c, in, len) SHA_CTX *c; - const unsigned char *data; + const void *in; size_t len; { u_int32_t *p; int ew,ec,sw,sc; u_int32_t l; + const unsigned char *data = in; if (len == 0) return; diff --git a/lib/libmd/sha256.3 b/lib/libmd/sha256.3 index 05618e852e..7ca08315c5 100644 --- a/lib/libmd/sha256.3 +++ b/lib/libmd/sha256.3 @@ -7,10 +7,10 @@ .\" ---------------------------------------------------------------------------- .\" .\" From: Id: mdX.3,v 1.14 1999/02/11 20:31:49 wollman Exp -.\" $FreeBSD: src/lib/libmd/sha256.3,v 1.2.4.1 2005/10/08 21:46:23 pjd Exp $ -.\" $DragonFly: src/lib/libmd/sha256.3,v 1.1 2006/04/29 22:19:26 dillon Exp $ +.\" $FreeBSD: src/lib/libmd/sha256.3,v 1.4 2005/11/17 13:00:00 ru Exp $ +.\" $DragonFly: src/lib/libmd/sha256.3,v 1.2 2008/09/11 20:25:34 swildner Exp $ .\" -.Dd March 9, 2005 +.Dd April 29, 2006 .Dt SHA256 3 .Os .Sh NAME diff --git a/lib/libmd/sha256.h b/lib/libmd/sha256.h index e7c1c4b1b7..770e2b80cf 100644 --- a/lib/libmd/sha256.h +++ b/lib/libmd/sha256.h @@ -23,8 +23,8 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $FreeBSD: src/lib/libmd/sha256.h,v 1.1 2005/03/09 19:23:04 cperciva Exp $ - * $DragonFly: src/lib/libmd/sha256.h,v 1.1 2006/04/29 22:19:26 dillon Exp $ + * $FreeBSD: src/lib/libmd/sha256.h,v 1.2 2006/01/17 15:35:56 phk Exp $ + * $DragonFly: src/lib/libmd/sha256.h,v 1.2 2008/09/11 20:25:34 swildner Exp $ */ #ifndef _SHA256_H_ @@ -40,12 +40,12 @@ typedef struct SHA256Context { __BEGIN_DECLS void SHA256_Init(SHA256_CTX *); -void SHA256_Update(SHA256_CTX *, const unsigned char *, size_t); +void SHA256_Update(SHA256_CTX *, const void *, size_t); void SHA256_Final(unsigned char [32], SHA256_CTX *); char *SHA256_End(SHA256_CTX *, char *); char *SHA256_File(const char *, char *); char *SHA256_FileChunk(const char *, char *, off_t, off_t); -char *SHA256_Data(const unsigned char *, unsigned int, char *); +char *SHA256_Data(const void *, unsigned int, char *); __END_DECLS #endif /* !_SHA256_H_ */ diff --git a/lib/libmd/sha256c.c b/lib/libmd/sha256c.c index 8f9be1dd0e..34868775b0 100644 --- a/lib/libmd/sha256c.c +++ b/lib/libmd/sha256c.c @@ -23,8 +23,8 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $FreeBSD: src/lib/libmd/sha256c.c,v 1.1 2005/03/09 19:23:04 cperciva Exp $ - * $DragonFly: src/lib/libmd/sha256c.c,v 1.1 2006/04/29 22:19:26 dillon Exp $ + * $FreeBSD: src/lib/libmd/sha256c.c,v 1.2 2006/01/17 15:35:56 phk Exp $ + * $DragonFly: src/lib/libmd/sha256c.c,v 1.2 2008/09/11 20:25:34 swildner Exp $ */ #include @@ -241,10 +241,11 @@ SHA256_Init(SHA256_CTX * ctx) /* Add bytes into the hash */ void -SHA256_Update(SHA256_CTX * ctx, const unsigned char *src, size_t len) +SHA256_Update(SHA256_CTX * ctx, const void *in, size_t len) { uint32_t bitlen[2]; uint32_t r; + const unsigned char *src = in; /* Number of bytes left in the buffer from previous updates */ r = (ctx->count[1] >> 3) & 0x3f; diff --git a/lib/libmd/shadriver.c b/lib/libmd/shadriver.c index 1f0ecb076a..281d0a92d4 100644 --- a/lib/libmd/shadriver.c +++ b/lib/libmd/shadriver.c @@ -1,6 +1,6 @@ /* SHADRIVER.C - test driver for SHA-1 (and SHA-0) - * $FreeBSD: src/lib/libmd/shadriver.c,v 1.2 1999/08/28 00:05:09 peter Exp $ - * $DragonFly: src/lib/libmd/shadriver.c,v 1.3 2006/04/29 22:19:26 dillon Exp $ + * $FreeBSD: src/lib/libmd/shadriver.c,v 1.4 2005/03/09 19:23:04 cperciva Exp $ + * $DragonFly: src/lib/libmd/shadriver.c,v 1.4 2008/09/11 20:25:34 swildner Exp $ */ /* Copyright (C) 1990-2, RSA Data Security, Inc. Created 1990. All diff --git a/sys/kern/md5c.c b/sys/kern/md5c.c index 706cae0567..27e14ff807 100644 --- a/sys/kern/md5c.c +++ b/sys/kern/md5c.c @@ -22,8 +22,8 @@ * These notices must be retained in any copies of any part of this * documentation and/or software. * - * $FreeBSD: src/sys/kern/md5c.c,v 1.17 1999/12/29 04:54:39 peter Exp $ - * $DragonFly: src/sys/kern/md5c.c,v 1.4 2005/04/30 23:04:21 swildner Exp $ + * $FreeBSD: src/sys/kern/md5c.c,v 1.27 2006/03/30 18:45:50 pjd Exp $ + * $DragonFly: src/sys/kern/md5c.c,v 1.5 2008/09/11 20:25:34 swildner Exp $ * * This code is the same as the code published by RSA Inc. It has been * edited for clarity and style only. @@ -37,35 +37,32 @@ #include #endif +#include +#include #include - -#ifdef _KERNEL -#define memset(x,y,z) bzero(x,z); -#define memcpy(x,y,z) bcopy(y, x, z) -#endif - -#if defined(__i386__) +#if (BYTE_ORDER == LITTLE_ENDIAN) #define Encode memcpy #define Decode memcpy -#else /* __i386__ */ +#else /* * Encodes input (u_int32_t) into output (unsigned char). Assumes len is * a multiple of 4. */ -/* XXX not prototyped, and not compatible with memcpy(). */ static void Encode (unsigned char *output, u_int32_t *input, unsigned int len) { - unsigned int i, j; - - for (i = 0, j = 0; j < len; i++, j += 4) { - output[j] = (unsigned char)(input[i] & 0xff); - output[j+1] = (unsigned char)((input[i] >> 8) & 0xff); - output[j+2] = (unsigned char)((input[i] >> 16) & 0xff); - output[j+3] = (unsigned char)((input[i] >> 24) & 0xff); + unsigned int i; + uint32_t ip; + + for (i = 0; i < len / 4; i++) { + ip = input[i]; + *output++ = ip; + *output++ = ip >> 8; + *output++ = ip >> 16; + *output++ = ip >> 24; } } @@ -77,13 +74,14 @@ Encode (unsigned char *output, u_int32_t *input, unsigned int len) static void Decode (u_int32_t *output, const unsigned char *input, unsigned int len) { - unsigned int i, j; + unsigned int i; - for (i = 0, j = 0; j < len; i++, j += 4) - output[i] = ((u_int32_t)input[j]) | (((u_int32_t)input[j+1]) << 8) | - (((u_int32_t)input[j+2]) << 16) | (((u_int32_t)input[j+3]) << 24); + for (i = 0; i < len; i += 4) { + *output++ = input[i] | (input[i+1] << 8) | (input[i+2] << 16) | + (input[i+3] << 24); + } } -#endif /* i386 */ +#endif static unsigned char PADDING[64] = { 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, @@ -147,9 +145,10 @@ MD5Init (MD5_CTX *context) */ void -MD5Update (MD5_CTX *context, const unsigned char *input, unsigned int inputLen) +MD5Update (MD5_CTX *context, const void *in, unsigned int inputLen) { unsigned int i, index, partLen; + const unsigned char *input = in; /* Compute number of bytes mod 64 */ index = (unsigned int)((context->count[0] >> 3) & 0x3F); diff --git a/sys/sys/md4.h b/sys/sys/md4.h index cdd34bb657..8a4c9695ba 100644 --- a/sys/sys/md4.h +++ b/sys/sys/md4.h @@ -1,9 +1,10 @@ /* MD4.H - header file for MD4C.C - * $FreeBSD: src/sys/sys/md4.h,v 1.1.2.1 2001/05/22 08:32:34 bp Exp $ - * $DragonFly: src/sys/sys/md4.h,v 1.3 2006/05/20 02:42:13 dillon Exp $ + * $FreeBSD: src/sys/sys/md4.h,v 1.2 2005/01/07 02:29:23 imp Exp $ + * $DragonFly: src/sys/sys/md4.h,v 1.4 2008/09/11 20:25:34 swildner Exp $ */ -/* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All +/*- + Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All rights reserved. License to copy and use this software is granted provided that it diff --git a/sys/sys/md5.h b/sys/sys/md5.h index cafc5542b3..ab1f6f9ed1 100644 --- a/sys/sys/md5.h +++ b/sys/sys/md5.h @@ -1,9 +1,10 @@ /* MD5.H - header file for MD5C.C - * $FreeBSD: src/sys/sys/md5.h,v 1.13 1999/12/29 04:24:44 peter Exp $ - * $DragonFly: src/sys/sys/md5.h,v 1.4 2006/05/20 02:42:13 dillon Exp $ + * $FreeBSD: src/sys/sys/md5.h,v 1.20 2006/03/15 19:47:12 andre Exp $ + * $DragonFly: src/sys/sys/md5.h,v 1.5 2008/09/11 20:25:34 swildner Exp $ */ -/* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All +/*- + Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All rights reserved. License to copy and use this software is granted provided that it @@ -32,6 +33,10 @@ documentation and/or software. #include #endif +#define MD5_BLOCK_LENGTH 64 +#define MD5_DIGEST_LENGTH 16 +#define MD5_DIGEST_STRING_LENGTH (MD5_DIGEST_LENGTH * 2 + 1) + /* MD5 context. */ typedef struct MD5Context { u_int32_t state[4]; /* state (ABCD) */ @@ -43,12 +48,13 @@ typedef struct MD5Context { __BEGIN_DECLS void MD5Init (MD5_CTX *); -void MD5Update (MD5_CTX *, const unsigned char *, unsigned int); +void MD5Update (MD5_CTX *, const void *, unsigned int); void MD5Pad (MD5_CTX *); void MD5Final (unsigned char [16], MD5_CTX *); char * MD5End(MD5_CTX *, char *); char * MD5File(const char *, char *); -char * MD5Data(const unsigned char *, unsigned int, char *); +char * MD5FileChunk(const char *, char *, off_t, off_t); +char * MD5Data(const void *, unsigned int, char *); #ifdef _KERNEL void MD5Transform (u_int32_t [4], const unsigned char [64]); #endif