177298Sobrien/* md5.h - Declaration of functions and data types used for MD5 sum 277298Sobrien computing library functions. 378828Sobrien Copyright 1995, 1996, 2000 Free Software Foundation, Inc. 477298Sobrien NOTE: The canonical source of this file is maintained with the GNU C 577298Sobrien Library. Bugs can be reported to bug-glibc@prep.ai.mit.edu. 677298Sobrien 777298Sobrien This program is free software; you can redistribute it and/or modify it 877298Sobrien under the terms of the GNU General Public License as published by the 977298Sobrien Free Software Foundation; either version 2, or (at your option) any 1077298Sobrien later version. 1177298Sobrien 1277298Sobrien This program is distributed in the hope that it will be useful, 1377298Sobrien but WITHOUT ANY WARRANTY; without even the implied warranty of 1477298Sobrien MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1577298Sobrien GNU General Public License for more details. 1677298Sobrien 1777298Sobrien You should have received a copy of the GNU General Public License 1877298Sobrien along with this program; if not, write to the Free Software Foundation, 19218822Sdim Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ 2077298Sobrien 2177298Sobrien#ifndef _MD5_H 2277298Sobrien#define _MD5_H 1 2377298Sobrien 2477298Sobrien#include <stdio.h> 2577298Sobrien 2677298Sobrien#if defined HAVE_LIMITS_H || _LIBC 2777298Sobrien# include <limits.h> 2877298Sobrien#endif 2977298Sobrien 30218822Sdim#include "ansidecl.h" 31218822Sdim 3277298Sobrien/* The following contortions are an attempt to use the C preprocessor 3377298Sobrien to determine an unsigned integral type that is 32 bits wide. An 3477298Sobrien alternative approach is to use autoconf's AC_CHECK_SIZEOF macro, but 3577298Sobrien doing that would require that the configure script compile and *run* 3677298Sobrien the resulting executable. Locally running cross-compiled executables 3777298Sobrien is usually not possible. */ 3877298Sobrien 3977298Sobrien#ifdef _LIBC 4077298Sobrien# include <sys/types.h> 4177298Sobrientypedef u_int32_t md5_uint32; 42218822Sdimtypedef uintptr_t md5_uintptr; 4377298Sobrien#else 4477298Sobrien# define INT_MAX_32_BITS 2147483647 4577298Sobrien 4677298Sobrien/* If UINT_MAX isn't defined, assume it's a 32-bit type. 4777298Sobrien This should be valid for all systems GNU cares about because 4877298Sobrien that doesn't include 16-bit systems, and only modern systems 4977298Sobrien (that certainly have <limits.h>) have 64+-bit integral types. */ 5077298Sobrien 5177298Sobrien# ifndef INT_MAX 5277298Sobrien# define INT_MAX INT_MAX_32_BITS 5377298Sobrien# endif 5477298Sobrien 5577298Sobrien# if INT_MAX == INT_MAX_32_BITS 5677298Sobrien typedef unsigned int md5_uint32; 5777298Sobrien# else 5877298Sobrien# if SHRT_MAX == INT_MAX_32_BITS 5977298Sobrien typedef unsigned short md5_uint32; 6077298Sobrien# else 6177298Sobrien# if LONG_MAX == INT_MAX_32_BITS 6277298Sobrien typedef unsigned long md5_uint32; 6377298Sobrien# else 6477298Sobrien /* The following line is intended to evoke an error. 6577298Sobrien Using #error is not portable enough. */ 6677298Sobrien "Cannot determine unsigned 32-bit data type." 6777298Sobrien# endif 6877298Sobrien# endif 6977298Sobrien# endif 70218822Sdim/* We have to make a guess about the integer type equivalent in size 71218822Sdim to pointers which should always be correct. */ 72218822Sdimtypedef unsigned long int md5_uintptr; 7377298Sobrien#endif 7477298Sobrien 7577298Sobrien/* Structure to save state of computation between the single steps. */ 7677298Sobrienstruct md5_ctx 7777298Sobrien{ 7877298Sobrien md5_uint32 A; 7977298Sobrien md5_uint32 B; 8077298Sobrien md5_uint32 C; 8177298Sobrien md5_uint32 D; 8277298Sobrien 8377298Sobrien md5_uint32 total[2]; 8477298Sobrien md5_uint32 buflen; 85218822Sdim char buffer[128] ATTRIBUTE_ALIGNED_ALIGNOF(md5_uint32); 8677298Sobrien}; 8777298Sobrien 8877298Sobrien/* 8977298Sobrien * The following three functions are build up the low level used in 9077298Sobrien * the functions `md5_stream' and `md5_buffer'. 9177298Sobrien */ 9277298Sobrien 9377298Sobrien/* Initialize structure containing state of computation. 9477298Sobrien (RFC 1321, 3.3: Step 3) */ 95218822Sdimextern void md5_init_ctx (struct md5_ctx *ctx); 9677298Sobrien 9777298Sobrien/* Starting with the result of former calls of this function (or the 9877298Sobrien initialization function update the context for the next LEN bytes 9977298Sobrien starting at BUFFER. 10077298Sobrien It is necessary that LEN is a multiple of 64!!! */ 101218822Sdimextern void md5_process_block (const void *buffer, size_t len, 102218822Sdim struct md5_ctx *ctx); 10377298Sobrien 10477298Sobrien/* Starting with the result of former calls of this function (or the 10577298Sobrien initialization function update the context for the next LEN bytes 10677298Sobrien starting at BUFFER. 10777298Sobrien It is NOT required that LEN is a multiple of 64. */ 108218822Sdimextern void md5_process_bytes (const void *buffer, size_t len, 109218822Sdim struct md5_ctx *ctx); 11077298Sobrien 11177298Sobrien/* Process the remaining bytes in the buffer and put result from CTX 11277298Sobrien in first 16 bytes following RESBUF. The result is always in little 11377298Sobrien endian byte order, so that a byte-wise output yields to the wanted 11477298Sobrien ASCII representation of the message digest. 11577298Sobrien 11677298Sobrien IMPORTANT: On some systems it is required that RESBUF is correctly 11777298Sobrien aligned for a 32 bits value. */ 118218822Sdimextern void *md5_finish_ctx (struct md5_ctx *ctx, void *resbuf); 11977298Sobrien 12077298Sobrien 12177298Sobrien/* Put result from CTX in first 16 bytes following RESBUF. The result is 12277298Sobrien always in little endian byte order, so that a byte-wise output yields 12377298Sobrien to the wanted ASCII representation of the message digest. 12477298Sobrien 12577298Sobrien IMPORTANT: On some systems it is required that RESBUF is correctly 12677298Sobrien aligned for a 32 bits value. */ 127218822Sdimextern void *md5_read_ctx (const struct md5_ctx *ctx, void *resbuf); 12877298Sobrien 12977298Sobrien 13077298Sobrien/* Compute MD5 message digest for bytes read from STREAM. The 13177298Sobrien resulting message digest number will be written into the 16 bytes 13277298Sobrien beginning at RESBLOCK. */ 133218822Sdimextern int md5_stream (FILE *stream, void *resblock); 13477298Sobrien 13577298Sobrien/* Compute MD5 message digest for LEN bytes beginning at BUFFER. The 13677298Sobrien result is always in little endian byte order, so that a byte-wise 13777298Sobrien output yields to the wanted ASCII representation of the message 13877298Sobrien digest. */ 139218822Sdimextern void *md5_buffer (const char *buffer, size_t len, void *resblock); 14077298Sobrien 14177298Sobrien#endif 142