2000-07-04 18:24:11 +00:00
|
|
|
/* Functions to compute MD5 message digest of files or memory blocks.
|
1996-12-15 02:15:29 +00:00
|
|
|
according to the definition of MD5 in RFC 1321 from April 1992.
|
2015-01-02 16:28:19 +00:00
|
|
|
Copyright (C) 1995-2015 Free Software Foundation, Inc.
|
1996-12-15 02:15:29 +00:00
|
|
|
This file is part of the GNU C Library.
|
|
|
|
|
|
|
|
The GNU C Library is free software; you can redistribute it and/or
|
2001-07-06 04:58:11 +00:00
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
1996-12-15 02:15:29 +00:00
|
|
|
|
|
|
|
The GNU C Library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
2001-07-06 04:58:11 +00:00
|
|
|
Lesser General Public License for more details.
|
1996-12-15 02:15:29 +00:00
|
|
|
|
2001-07-06 04:58:11 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public
|
2012-02-09 23:18:22 +00:00
|
|
|
License along with the GNU C Library; if not, see
|
|
|
|
<http://www.gnu.org/licenses/>. */
|
1996-12-15 02:15:29 +00:00
|
|
|
|
|
|
|
/* Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995. */
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include <config.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#if STDC_HEADERS || defined _LIBC
|
|
|
|
# include <stdlib.h>
|
|
|
|
# include <string.h>
|
|
|
|
#else
|
|
|
|
# ifndef HAVE_MEMCPY
|
2005-05-02 20:15:34 +00:00
|
|
|
# define memcpy(d, s, n) (bcopy ((s), (d), (n)), (d))
|
1996-12-15 02:15:29 +00:00
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "md5.h"
|
|
|
|
|
|
|
|
#ifdef _LIBC
|
|
|
|
# include <endian.h>
|
|
|
|
# if __BYTE_ORDER == __BIG_ENDIAN
|
|
|
|
# define WORDS_BIGENDIAN 1
|
|
|
|
# endif
|
Update.
1997-06-03 23:42 Ulrich Drepper <drepper@cygnus.com>
* elf/dl-support.c: Define and initialize _dl_verbose used in
dl-machine.h.
* io/ftw.c: Expand stat/lstat calls.
* manual/intro.texi: Also refer to ISO 9945.
Update info about SVID.
Add description for XPG.
* md5-crypt/md5-crypt.c: Namespace cleanups.
* md5-crypt/md5.c: Likewise.
* md5-crypt/md5.h: Likewise.
* sysdeps/generic/crypt-entry.c: Likewise.
* posix/unistd.h (_POSIX2_C_VERSION): Set to 199209L.
* stdlib/fmtmsg.h: Declare addseverity only if __USE_SVID is
defined.
* sunrpc/rpc_scan.c (findkind): Declare `token' as const.
* sunrpc/rpc_util.c (toktostr): Declare `token' as const.
* time/Makefile: Make tzselect dependen of config.make.
1997-06-01 15:01 Miles Bader <miles@gnu.ai.mit.edu>
* manual/string.texi (String and Array Utilities): Add `Argz and
Envz Vectors' to the menu.
(Argz and Envz Vectors, Argz Functions, Envz Functions): New nodes.
1997-05-31 20:59 Andreas Schwab <schwab@issan.informatik.uni-dortmund.de>
* login/Makefile (utmpd-routines): Add utmpd.
(extra-objs): Add utmpd objects to get dependencies.
(distribute): Add sources for utmpd.
(subdir-dirs): Define.
1997-06-02 16:28 Ulrich Drepper <drepper@cygnus.com>
* sysdeps/wordsize-32/inttypes.h: Include features.h and use
__CONCAT instead of defined __CONCAT__ ourself.
* sysdeps/wordsize-64/inttypes.h: Likewise.
1997-06-01 19:11 Andreas Schwab <schwab@issan.informatik.uni-dortmund.de>
* sysdeps/wordsize-64/inttypes.h (INTMAX_C): Use `l' suffix, not
`ll'.
(PRIdFAST, PRIoFAST, PRIxFAST, PRIuFAST, SCNdFAST, SCNiFAST,
SCNoFAST, SCNxFAST): Correct format specifiers.
1997-06-02 04:23 Ulrich Drepper <drepper@cygnus.com>
* sysdeps/unix/syscalls.list: Make fchdir weak alias for __fchdir.
1997-06-01 19:17 Ulrich Drepper <drepper@cygnus.com>
* md5-crypt/Makefile: Correct libmd5crypt file to really generate
DES free libcrypt.
* md5-crypt/onlymd5-entry.c: New file. Wrapper around
sysdeps/generic/crypt-entry.c.
1997-06-01 12:48 Ulrich Drepper <drepper@cygnus.com>
* sysdeps/unix/sysv/linux/alpha/gnu/types.h (__fd_mask): Change
type to `unsigned long int'.
Patch by Richard Henderson <richard@twiddle.rth.home>.
1997-05-30 17:34 Andreas Jaeger <aj@arthur.rhein-neckar.de>
* sysdeps/generic/bb_init_func.c (__bb_init_func): Use ISO C
declaration style.
* nss/nss_files/files-hosts.c: Delete inclusion of
"../resolv/mapv4v6hostent.h".
1997-06-03 21:53:09 +00:00
|
|
|
/* We need to keep the namespace clean so define the MD5 function
|
1999-10-25 20:18:27 +00:00
|
|
|
protected using leading __ . */
|
Update.
1997-06-03 23:42 Ulrich Drepper <drepper@cygnus.com>
* elf/dl-support.c: Define and initialize _dl_verbose used in
dl-machine.h.
* io/ftw.c: Expand stat/lstat calls.
* manual/intro.texi: Also refer to ISO 9945.
Update info about SVID.
Add description for XPG.
* md5-crypt/md5-crypt.c: Namespace cleanups.
* md5-crypt/md5.c: Likewise.
* md5-crypt/md5.h: Likewise.
* sysdeps/generic/crypt-entry.c: Likewise.
* posix/unistd.h (_POSIX2_C_VERSION): Set to 199209L.
* stdlib/fmtmsg.h: Declare addseverity only if __USE_SVID is
defined.
* sunrpc/rpc_scan.c (findkind): Declare `token' as const.
* sunrpc/rpc_util.c (toktostr): Declare `token' as const.
* time/Makefile: Make tzselect dependen of config.make.
1997-06-01 15:01 Miles Bader <miles@gnu.ai.mit.edu>
* manual/string.texi (String and Array Utilities): Add `Argz and
Envz Vectors' to the menu.
(Argz and Envz Vectors, Argz Functions, Envz Functions): New nodes.
1997-05-31 20:59 Andreas Schwab <schwab@issan.informatik.uni-dortmund.de>
* login/Makefile (utmpd-routines): Add utmpd.
(extra-objs): Add utmpd objects to get dependencies.
(distribute): Add sources for utmpd.
(subdir-dirs): Define.
1997-06-02 16:28 Ulrich Drepper <drepper@cygnus.com>
* sysdeps/wordsize-32/inttypes.h: Include features.h and use
__CONCAT instead of defined __CONCAT__ ourself.
* sysdeps/wordsize-64/inttypes.h: Likewise.
1997-06-01 19:11 Andreas Schwab <schwab@issan.informatik.uni-dortmund.de>
* sysdeps/wordsize-64/inttypes.h (INTMAX_C): Use `l' suffix, not
`ll'.
(PRIdFAST, PRIoFAST, PRIxFAST, PRIuFAST, SCNdFAST, SCNiFAST,
SCNoFAST, SCNxFAST): Correct format specifiers.
1997-06-02 04:23 Ulrich Drepper <drepper@cygnus.com>
* sysdeps/unix/syscalls.list: Make fchdir weak alias for __fchdir.
1997-06-01 19:17 Ulrich Drepper <drepper@cygnus.com>
* md5-crypt/Makefile: Correct libmd5crypt file to really generate
DES free libcrypt.
* md5-crypt/onlymd5-entry.c: New file. Wrapper around
sysdeps/generic/crypt-entry.c.
1997-06-01 12:48 Ulrich Drepper <drepper@cygnus.com>
* sysdeps/unix/sysv/linux/alpha/gnu/types.h (__fd_mask): Change
type to `unsigned long int'.
Patch by Richard Henderson <richard@twiddle.rth.home>.
1997-05-30 17:34 Andreas Jaeger <aj@arthur.rhein-neckar.de>
* sysdeps/generic/bb_init_func.c (__bb_init_func): Use ISO C
declaration style.
* nss/nss_files/files-hosts.c: Delete inclusion of
"../resolv/mapv4v6hostent.h".
1997-06-03 21:53:09 +00:00
|
|
|
# define md5_init_ctx __md5_init_ctx
|
|
|
|
# define md5_process_bytes __md5_process_bytes
|
|
|
|
# define md5_finish_ctx __md5_finish_ctx
|
|
|
|
# define md5_read_ctx __md5_read_ctx
|
|
|
|
# define md5_stream __md5_stream
|
|
|
|
# define md5_buffer __md5_buffer
|
1996-12-15 02:15:29 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef WORDS_BIGENDIAN
|
|
|
|
# define SWAP(n) \
|
|
|
|
(((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24))
|
|
|
|
#else
|
|
|
|
# define SWAP(n) (n)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/* This array contains the bytes used to pad the buffer to the next
|
|
|
|
64-byte boundary. (RFC 1321, 3.1: Step 1) */
|
|
|
|
static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ... */ };
|
|
|
|
|
|
|
|
|
|
|
|
/* Initialize structure containing state of computation.
|
|
|
|
(RFC 1321, 3.3: Step 3) */
|
|
|
|
void
|
2015-10-16 20:21:49 +00:00
|
|
|
md5_init_ctx (struct md5_ctx *ctx)
|
1996-12-15 02:15:29 +00:00
|
|
|
{
|
|
|
|
ctx->A = 0x67452301;
|
|
|
|
ctx->B = 0xefcdab89;
|
|
|
|
ctx->C = 0x98badcfe;
|
|
|
|
ctx->D = 0x10325476;
|
|
|
|
|
|
|
|
ctx->total[0] = ctx->total[1] = 0;
|
|
|
|
ctx->buflen = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Put result from CTX in first 16 bytes following RESBUF. The result
|
|
|
|
must be in little endian byte order.
|
|
|
|
|
|
|
|
IMPORTANT: On some systems it is required that RESBUF is correctly
|
|
|
|
aligned for a 32 bits value. */
|
|
|
|
void *
|
2015-10-16 20:21:49 +00:00
|
|
|
md5_read_ctx (const struct md5_ctx *ctx, void *resbuf)
|
1996-12-15 02:15:29 +00:00
|
|
|
{
|
|
|
|
((md5_uint32 *) resbuf)[0] = SWAP (ctx->A);
|
|
|
|
((md5_uint32 *) resbuf)[1] = SWAP (ctx->B);
|
|
|
|
((md5_uint32 *) resbuf)[2] = SWAP (ctx->C);
|
|
|
|
((md5_uint32 *) resbuf)[3] = SWAP (ctx->D);
|
|
|
|
|
|
|
|
return resbuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Process the remaining bytes in the internal buffer and the usual
|
|
|
|
prolog according to the standard and write the result to RESBUF.
|
|
|
|
|
|
|
|
IMPORTANT: On some systems it is required that RESBUF is correctly
|
|
|
|
aligned for a 32 bits value. */
|
|
|
|
void *
|
2015-10-16 20:21:49 +00:00
|
|
|
md5_finish_ctx (struct md5_ctx *ctx, void *resbuf)
|
1996-12-15 02:15:29 +00:00
|
|
|
{
|
|
|
|
/* Take yet unprocessed bytes into account. */
|
|
|
|
md5_uint32 bytes = ctx->buflen;
|
|
|
|
size_t pad;
|
|
|
|
|
|
|
|
/* Now count remaining bytes. */
|
|
|
|
ctx->total[0] += bytes;
|
|
|
|
if (ctx->total[0] < bytes)
|
|
|
|
++ctx->total[1];
|
|
|
|
|
|
|
|
pad = bytes >= 56 ? 64 + 56 - bytes : 56 - bytes;
|
|
|
|
memcpy (&ctx->buffer[bytes], fillbuf, pad);
|
|
|
|
|
|
|
|
/* Put the 64-bit file length in *bits* at the end of the buffer. */
|
2011-07-19 20:53:43 +00:00
|
|
|
ctx->buffer32[(bytes + pad) / 4] = SWAP (ctx->total[0] << 3);
|
|
|
|
ctx->buffer32[(bytes + pad + 4) / 4] = SWAP ((ctx->total[1] << 3) |
|
|
|
|
(ctx->total[0] >> 29));
|
1996-12-15 02:15:29 +00:00
|
|
|
|
|
|
|
/* Process last bytes. */
|
2012-11-12 23:17:31 +00:00
|
|
|
__md5_process_block (ctx->buffer, bytes + pad + 8, ctx);
|
1996-12-15 02:15:29 +00:00
|
|
|
|
|
|
|
return md5_read_ctx (ctx, resbuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Compute MD5 message digest for bytes read from STREAM. The
|
|
|
|
resulting message digest number will be written into the 16 bytes
|
|
|
|
beginning at RESBLOCK. */
|
|
|
|
int
|
2015-10-16 20:21:49 +00:00
|
|
|
md5_stream (FILE *stream, void *resblock)
|
1996-12-15 02:15:29 +00:00
|
|
|
{
|
|
|
|
/* Important: BLOCKSIZE must be a multiple of 64. */
|
|
|
|
#define BLOCKSIZE 4096
|
|
|
|
struct md5_ctx ctx;
|
|
|
|
char buffer[BLOCKSIZE + 72];
|
|
|
|
size_t sum;
|
|
|
|
|
|
|
|
/* Initialize the computation context. */
|
|
|
|
md5_init_ctx (&ctx);
|
|
|
|
|
|
|
|
/* Iterate over full file contents. */
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
/* We read the file in blocks of BLOCKSIZE bytes. One call of the
|
|
|
|
computation function processes the whole buffer so that with the
|
|
|
|
next round of the loop another block can be read. */
|
|
|
|
size_t n;
|
|
|
|
sum = 0;
|
|
|
|
|
|
|
|
/* Read block. Take care for partial reads. */
|
|
|
|
do
|
|
|
|
{
|
|
|
|
n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream);
|
|
|
|
|
|
|
|
sum += n;
|
|
|
|
}
|
|
|
|
while (sum < BLOCKSIZE && n != 0);
|
|
|
|
if (n == 0 && ferror (stream))
|
2011-07-19 20:53:43 +00:00
|
|
|
return 1;
|
1996-12-15 02:15:29 +00:00
|
|
|
|
|
|
|
/* If end of file is reached, end the loop. */
|
|
|
|
if (n == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Process buffer with BLOCKSIZE bytes. Note that
|
|
|
|
BLOCKSIZE % 64 == 0
|
|
|
|
*/
|
2012-11-12 23:17:31 +00:00
|
|
|
__md5_process_block (buffer, BLOCKSIZE, &ctx);
|
1996-12-15 02:15:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Add the last bytes if necessary. */
|
|
|
|
if (sum > 0)
|
|
|
|
md5_process_bytes (buffer, sum, &ctx);
|
|
|
|
|
|
|
|
/* Construct result in desired memory. */
|
|
|
|
md5_finish_ctx (&ctx, resblock);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Compute MD5 message digest for LEN bytes beginning at BUFFER. The
|
|
|
|
result is always in little endian byte order, so that a byte-wise
|
|
|
|
output yields to the wanted ASCII representation of the message
|
|
|
|
digest. */
|
|
|
|
void *
|
2015-10-16 20:21:49 +00:00
|
|
|
md5_buffer (const char *buffer, size_t len, void *resblock)
|
1996-12-15 02:15:29 +00:00
|
|
|
{
|
|
|
|
struct md5_ctx ctx;
|
|
|
|
|
|
|
|
/* Initialize the computation context. */
|
|
|
|
md5_init_ctx (&ctx);
|
|
|
|
|
|
|
|
/* Process whole buffer but last len % 64 bytes. */
|
|
|
|
md5_process_bytes (buffer, len, &ctx);
|
|
|
|
|
|
|
|
/* Put result in desired memory area. */
|
|
|
|
return md5_finish_ctx (&ctx, resblock);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2015-10-16 20:21:49 +00:00
|
|
|
md5_process_bytes (const void *buffer, size_t len, struct md5_ctx *ctx)
|
1996-12-15 02:15:29 +00:00
|
|
|
{
|
|
|
|
/* When we already have some bits in our internal buffer concatenate
|
|
|
|
both inputs first. */
|
|
|
|
if (ctx->buflen != 0)
|
|
|
|
{
|
|
|
|
size_t left_over = ctx->buflen;
|
|
|
|
size_t add = 128 - left_over > len ? len : 128 - left_over;
|
|
|
|
|
|
|
|
memcpy (&ctx->buffer[left_over], buffer, add);
|
|
|
|
ctx->buflen += add;
|
|
|
|
|
2000-07-04 18:24:11 +00:00
|
|
|
if (ctx->buflen > 64)
|
1996-12-15 02:15:29 +00:00
|
|
|
{
|
2012-11-12 23:17:31 +00:00
|
|
|
__md5_process_block (ctx->buffer, ctx->buflen & ~63, ctx);
|
2000-07-04 18:24:11 +00:00
|
|
|
|
|
|
|
ctx->buflen &= 63;
|
1996-12-15 02:15:29 +00:00
|
|
|
/* The regions in the following copy operation cannot overlap. */
|
|
|
|
memcpy (ctx->buffer, &ctx->buffer[(left_over + add) & ~63],
|
2000-07-04 18:24:11 +00:00
|
|
|
ctx->buflen);
|
1996-12-15 02:15:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
buffer = (const char *) buffer + add;
|
|
|
|
len -= add;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Process available complete blocks. */
|
2001-12-14 06:33:57 +00:00
|
|
|
if (len >= 64)
|
1996-12-15 02:15:29 +00:00
|
|
|
{
|
2001-12-14 06:33:57 +00:00
|
|
|
#if !_STRING_ARCH_unaligned
|
|
|
|
/* To check alignment gcc has an appropriate operator. Other
|
|
|
|
compilers don't. */
|
|
|
|
# if __GNUC__ >= 2
|
|
|
|
# define UNALIGNED_P(p) (((md5_uintptr) p) % __alignof__ (md5_uint32) != 0)
|
|
|
|
# else
|
|
|
|
# define UNALIGNED_P(p) (((md5_uintptr) p) % sizeof (md5_uint32) != 0)
|
|
|
|
# endif
|
|
|
|
if (UNALIGNED_P (buffer))
|
|
|
|
while (len > 64)
|
|
|
|
{
|
2012-11-12 23:17:31 +00:00
|
|
|
__md5_process_block (memcpy (ctx->buffer, buffer, 64), 64, ctx);
|
2001-12-14 06:33:57 +00:00
|
|
|
buffer = (const char *) buffer + 64;
|
|
|
|
len -= 64;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
2012-11-12 23:17:31 +00:00
|
|
|
__md5_process_block (buffer, len & ~63, ctx);
|
2001-12-14 06:33:57 +00:00
|
|
|
buffer = (const char *) buffer + (len & ~63);
|
|
|
|
len &= 63;
|
|
|
|
}
|
1996-12-15 02:15:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Move remaining bytes in internal buffer. */
|
|
|
|
if (len > 0)
|
|
|
|
{
|
2000-07-04 18:24:11 +00:00
|
|
|
size_t left_over = ctx->buflen;
|
|
|
|
|
|
|
|
memcpy (&ctx->buffer[left_over], buffer, len);
|
|
|
|
left_over += len;
|
|
|
|
if (left_over >= 64)
|
|
|
|
{
|
2012-11-12 23:17:31 +00:00
|
|
|
__md5_process_block (ctx->buffer, 64, ctx);
|
2000-07-04 18:24:11 +00:00
|
|
|
left_over -= 64;
|
|
|
|
memcpy (ctx->buffer, &ctx->buffer[64], left_over);
|
|
|
|
}
|
|
|
|
ctx->buflen = left_over;
|
1996-12-15 02:15:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-12 23:17:31 +00:00
|
|
|
#include <md5-block.c>
|