1*404b540aSrobert /* md5.h - Declaration of functions and data types used for MD5 sum 2*404b540aSrobert computing library functions. 3*404b540aSrobert Copyright 1995, 1996, 2000 Free Software Foundation, Inc. 4*404b540aSrobert NOTE: The canonical source of this file is maintained with the GNU C 5*404b540aSrobert Library. Bugs can be reported to bug-glibc@prep.ai.mit.edu. 6*404b540aSrobert 7*404b540aSrobert This program is free software; you can redistribute it and/or modify it 8*404b540aSrobert under the terms of the GNU General Public License as published by the 9*404b540aSrobert Free Software Foundation; either version 2, or (at your option) any 10*404b540aSrobert later version. 11*404b540aSrobert 12*404b540aSrobert This program is distributed in the hope that it will be useful, 13*404b540aSrobert but WITHOUT ANY WARRANTY; without even the implied warranty of 14*404b540aSrobert MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15*404b540aSrobert GNU General Public License for more details. 16*404b540aSrobert 17*404b540aSrobert You should have received a copy of the GNU General Public License 18*404b540aSrobert along with this program; if not, write to the Free Software Foundation, 19*404b540aSrobert Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ 20*404b540aSrobert 21*404b540aSrobert #ifndef _MD5_H 22*404b540aSrobert #define _MD5_H 1 23*404b540aSrobert 24*404b540aSrobert #include <stdio.h> 25*404b540aSrobert 26*404b540aSrobert #if defined HAVE_LIMITS_H || _LIBC 27*404b540aSrobert # include <limits.h> 28*404b540aSrobert #endif 29*404b540aSrobert 30*404b540aSrobert #include "ansidecl.h" 31*404b540aSrobert 32*404b540aSrobert /* The following contortions are an attempt to use the C preprocessor 33*404b540aSrobert to determine an unsigned integral type that is 32 bits wide. An 34*404b540aSrobert alternative approach is to use autoconf's AC_CHECK_SIZEOF macro, but 35*404b540aSrobert doing that would require that the configure script compile and *run* 36*404b540aSrobert the resulting executable. Locally running cross-compiled executables 37*404b540aSrobert is usually not possible. */ 38*404b540aSrobert 39*404b540aSrobert #ifdef _LIBC 40*404b540aSrobert # include <sys/types.h> 41*404b540aSrobert typedef u_int32_t md5_uint32; 42*404b540aSrobert typedef uintptr_t md5_uintptr; 43*404b540aSrobert #else 44*404b540aSrobert # define INT_MAX_32_BITS 2147483647 45*404b540aSrobert 46*404b540aSrobert /* If UINT_MAX isn't defined, assume it's a 32-bit type. 47*404b540aSrobert This should be valid for all systems GNU cares about because 48*404b540aSrobert that doesn't include 16-bit systems, and only modern systems 49*404b540aSrobert (that certainly have <limits.h>) have 64+-bit integral types. */ 50*404b540aSrobert 51*404b540aSrobert # ifndef INT_MAX 52*404b540aSrobert # define INT_MAX INT_MAX_32_BITS 53*404b540aSrobert # endif 54*404b540aSrobert 55*404b540aSrobert # if INT_MAX == INT_MAX_32_BITS 56*404b540aSrobert typedef unsigned int md5_uint32; 57*404b540aSrobert # else 58*404b540aSrobert # if SHRT_MAX == INT_MAX_32_BITS 59*404b540aSrobert typedef unsigned short md5_uint32; 60*404b540aSrobert # else 61*404b540aSrobert # if LONG_MAX == INT_MAX_32_BITS 62*404b540aSrobert typedef unsigned long md5_uint32; 63*404b540aSrobert # else 64*404b540aSrobert /* The following line is intended to evoke an error. 65*404b540aSrobert Using #error is not portable enough. */ 66*404b540aSrobert "Cannot determine unsigned 32-bit data type." 67*404b540aSrobert # endif 68*404b540aSrobert # endif 69*404b540aSrobert # endif 70*404b540aSrobert /* We have to make a guess about the integer type equivalent in size 71*404b540aSrobert to pointers which should always be correct. */ 72*404b540aSrobert typedef unsigned long int md5_uintptr; 73*404b540aSrobert #endif 74*404b540aSrobert 75*404b540aSrobert /* Structure to save state of computation between the single steps. */ 76*404b540aSrobert struct md5_ctx 77*404b540aSrobert { 78*404b540aSrobert md5_uint32 A; 79*404b540aSrobert md5_uint32 B; 80*404b540aSrobert md5_uint32 C; 81*404b540aSrobert md5_uint32 D; 82*404b540aSrobert 83*404b540aSrobert md5_uint32 total[2]; 84*404b540aSrobert md5_uint32 buflen; 85*404b540aSrobert char buffer[128] ATTRIBUTE_ALIGNED_ALIGNOF(md5_uint32); 86*404b540aSrobert }; 87*404b540aSrobert 88*404b540aSrobert /* 89*404b540aSrobert * The following three functions are build up the low level used in 90*404b540aSrobert * the functions `md5_stream' and `md5_buffer'. 91*404b540aSrobert */ 92*404b540aSrobert 93*404b540aSrobert /* Initialize structure containing state of computation. 94*404b540aSrobert (RFC 1321, 3.3: Step 3) */ 95*404b540aSrobert extern void md5_init_ctx (struct md5_ctx *ctx); 96*404b540aSrobert 97*404b540aSrobert /* Starting with the result of former calls of this function (or the 98*404b540aSrobert initialization function update the context for the next LEN bytes 99*404b540aSrobert starting at BUFFER. 100*404b540aSrobert It is necessary that LEN is a multiple of 64!!! */ 101*404b540aSrobert extern void md5_process_block (const void *buffer, size_t len, 102*404b540aSrobert struct md5_ctx *ctx); 103*404b540aSrobert 104*404b540aSrobert /* Starting with the result of former calls of this function (or the 105*404b540aSrobert initialization function update the context for the next LEN bytes 106*404b540aSrobert starting at BUFFER. 107*404b540aSrobert It is NOT required that LEN is a multiple of 64. */ 108*404b540aSrobert extern void md5_process_bytes (const void *buffer, size_t len, 109*404b540aSrobert struct md5_ctx *ctx); 110*404b540aSrobert 111*404b540aSrobert /* Process the remaining bytes in the buffer and put result from CTX 112*404b540aSrobert in first 16 bytes following RESBUF. The result is always in little 113*404b540aSrobert endian byte order, so that a byte-wise output yields to the wanted 114*404b540aSrobert ASCII representation of the message digest. 115*404b540aSrobert 116*404b540aSrobert IMPORTANT: On some systems it is required that RESBUF is correctly 117*404b540aSrobert aligned for a 32 bits value. */ 118*404b540aSrobert extern void *md5_finish_ctx (struct md5_ctx *ctx, void *resbuf); 119*404b540aSrobert 120*404b540aSrobert 121*404b540aSrobert /* Put result from CTX in first 16 bytes following RESBUF. The result is 122*404b540aSrobert always in little endian byte order, so that a byte-wise output yields 123*404b540aSrobert to the wanted ASCII representation of the message digest. 124*404b540aSrobert 125*404b540aSrobert IMPORTANT: On some systems it is required that RESBUF is correctly 126*404b540aSrobert aligned for a 32 bits value. */ 127*404b540aSrobert extern void *md5_read_ctx (const struct md5_ctx *ctx, void *resbuf); 128*404b540aSrobert 129*404b540aSrobert 130*404b540aSrobert /* Compute MD5 message digest for bytes read from STREAM. The 131*404b540aSrobert resulting message digest number will be written into the 16 bytes 132*404b540aSrobert beginning at RESBLOCK. */ 133*404b540aSrobert extern int md5_stream (FILE *stream, void *resblock); 134*404b540aSrobert 135*404b540aSrobert /* Compute MD5 message digest for LEN bytes beginning at BUFFER. The 136*404b540aSrobert result is always in little endian byte order, so that a byte-wise 137*404b540aSrobert output yields to the wanted ASCII representation of the message 138*404b540aSrobert digest. */ 139*404b540aSrobert extern void *md5_buffer (const char *buffer, size_t len, void *resblock); 140*404b540aSrobert 141*404b540aSrobert #endif 142