Back to index

glibc  2.9
md5.h
Go to the documentation of this file.
00001 /* Declaration of functions and data types used for MD5 sum computing
00002    library functions.
00003    Copyright (C) 1995-1997,1999,2000,2001,2004,2005
00004       Free Software Foundation, Inc.
00005    This file is part of the GNU C Library.
00006 
00007    The GNU C Library is free software; you can redistribute it and/or
00008    modify it under the terms of the GNU Lesser General Public
00009    License as published by the Free Software Foundation; either
00010    version 2.1 of the License, or (at your option) any later version.
00011 
00012    The GNU C Library is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015    Lesser General Public License for more details.
00016 
00017    You should have received a copy of the GNU Lesser General Public
00018    License along with the GNU C Library; if not, write to the Free
00019    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00020    02111-1307 USA.  */
00021 
00022 #ifndef _MD5_H
00023 #define _MD5_H 1
00024 
00025 #include <stdio.h>
00026 
00027 #if defined HAVE_LIMITS_H || _LIBC
00028 # include <limits.h>
00029 #endif
00030 
00031 #define MD5_DIGEST_SIZE 16
00032 #define MD5_BLOCK_SIZE 64
00033 
00034 /* The following contortions are an attempt to use the C preprocessor
00035    to determine an unsigned integral type that is 32 bits wide.  An
00036    alternative approach is to use autoconf's AC_CHECK_SIZEOF macro, but
00037    doing that would require that the configure script compile and *run*
00038    the resulting executable.  Locally running cross-compiled executables
00039    is usually not possible.  */
00040 
00041 #ifdef _LIBC
00042 # include <stdint.h>
00043 typedef uint32_t md5_uint32;
00044 typedef uintptr_t md5_uintptr;
00045 #else
00046 # if defined __STDC__ && __STDC__
00047 #  define UINT_MAX_32_BITS 4294967295U
00048 # else
00049 #  define UINT_MAX_32_BITS 0xFFFFFFFF
00050 # endif
00051 
00052 /* If UINT_MAX isn't defined, assume it's a 32-bit type.
00053    This should be valid for all systems GNU cares about because
00054    that doesn't include 16-bit systems, and only modern systems
00055    (that certainly have <limits.h>) have 64+-bit integral types.  */
00056 
00057 # ifndef UINT_MAX
00058 #  define UINT_MAX UINT_MAX_32_BITS
00059 # endif
00060 
00061 # if UINT_MAX == UINT_MAX_32_BITS
00062    typedef unsigned int md5_uint32;
00063 # else
00064 #  if USHRT_MAX == UINT_MAX_32_BITS
00065     typedef unsigned short md5_uint32;
00066 #  else
00067 #   if ULONG_MAX == UINT_MAX_32_BITS
00068      typedef unsigned long md5_uint32;
00069 #   else
00070      /* The following line is intended to evoke an error.
00071         Using #error is not portable enough.  */
00072      "Cannot determine unsigned 32-bit data type."
00073 #   endif
00074 #  endif
00075 # endif
00076 /* We have to make a guess about the integer type equivalent in size
00077    to pointers which should always be correct.  */
00078 typedef unsigned long int md5_uintptr;
00079 #endif
00080 
00081 /* Structure to save state of computation between the single steps.  */
00082 struct md5_ctx
00083 {
00084   md5_uint32 A;
00085   md5_uint32 B;
00086   md5_uint32 C;
00087   md5_uint32 D;
00088 
00089   md5_uint32 total[2];
00090   md5_uint32 buflen;
00091   char buffer[128] __attribute__ ((__aligned__ (__alignof__ (md5_uint32))));
00092 };
00093 
00094 /*
00095  * The following three functions are build up the low level used in
00096  * the functions `md5_stream' and `md5_buffer'.
00097  */
00098 
00099 /* Initialize structure containing state of computation.
00100    (RFC 1321, 3.3: Step 3)  */
00101 extern void __md5_init_ctx (struct md5_ctx *ctx) __THROW;
00102 
00103 /* Starting with the result of former calls of this function (or the
00104    initialization function update the context for the next LEN bytes
00105    starting at BUFFER.
00106    It is necessary that LEN is a multiple of 64!!! */
00107 extern void __md5_process_block (const void *buffer, size_t len,
00108                              struct md5_ctx *ctx) __THROW;
00109 
00110 /* Starting with the result of former calls of this function (or the
00111    initialization function update the context for the next LEN bytes
00112    starting at BUFFER.
00113    It is NOT required that LEN is a multiple of 64.  */
00114 extern void __md5_process_bytes (const void *buffer, size_t len,
00115                              struct md5_ctx *ctx) __THROW;
00116 
00117 /* Process the remaining bytes in the buffer and put result from CTX
00118    in first 16 bytes following RESBUF.  The result is always in little
00119    endian byte order, so that a byte-wise output yields to the wanted
00120    ASCII representation of the message digest.
00121 
00122    IMPORTANT: On some systems it is required that RESBUF is correctly
00123    aligned for a 32 bits value.  */
00124 extern void *__md5_finish_ctx (struct md5_ctx *ctx, void *resbuf) __THROW;
00125 
00126 
00127 /* Put result from CTX in first 16 bytes following RESBUF.  The result is
00128    always in little endian byte order, so that a byte-wise output yields
00129    to the wanted ASCII representation of the message digest.
00130 
00131    IMPORTANT: On some systems it is required that RESBUF is correctly
00132    aligned for a 32 bits value.  */
00133 extern void *__md5_read_ctx (const struct md5_ctx *ctx, void *resbuf) __THROW;
00134 
00135 
00136 /* Compute MD5 message digest for bytes read from STREAM.  The
00137    resulting message digest number will be written into the 16 bytes
00138    beginning at RESBLOCK.  */
00139 extern int __md5_stream (FILE *stream, void *resblock) __THROW;
00140 
00141 /* Compute MD5 message digest for LEN bytes beginning at BUFFER.  The
00142    result is always in little endian byte order, so that a byte-wise
00143    output yields to the wanted ASCII representation of the message
00144    digest.  */
00145 extern void *__md5_buffer (const char *buffer, size_t len,
00146                         void *resblock) __THROW;
00147 
00148 #endif /* md5.h */