1139804Simp/* md5.h - Declaration of functions and data types used for MD5 sum
21541Srgrimes   computing library functions.
31541Srgrimes   Copyright 1995, 1996, 2000 Free Software Foundation, Inc.
41541Srgrimes   NOTE: The canonical source of this file is maintained with the GNU C
51541Srgrimes   Library.  Bugs can be reported to bug-glibc@prep.ai.mit.edu.
61541Srgrimes
71541Srgrimes   This program is free software; you can redistribute it and/or modify it
81541Srgrimes   under the terms of the GNU General Public License as published by the
91541Srgrimes   Free Software Foundation; either version 2, or (at your option) any
101541Srgrimes   later version.
111541Srgrimes
121541Srgrimes   This program is distributed in the hope that it will be useful,
131541Srgrimes   but WITHOUT ANY WARRANTY; without even the implied warranty of
141541Srgrimes   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
151541Srgrimes   GNU General Public License for more details.
161541Srgrimes
171541Srgrimes   You should have received a copy of the GNU General Public License
181541Srgrimes   along with this program; if not, write to the Free Software Foundation,
191541Srgrimes   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
201541Srgrimes
211541Srgrimes#ifndef _MD5_H
221541Srgrimes#define _MD5_H 1
231541Srgrimes
241541Srgrimes#include <stdio.h>
251541Srgrimes
261541Srgrimes#if defined HAVE_LIMITS_H || _LIBC
271541Srgrimes# include <limits.h>
281541Srgrimes#endif
291541Srgrimes
301541Srgrimes/* The following contortions are an attempt to use the C preprocessor
311541Srgrimes   to determine an unsigned integral type that is 32 bits wide.  An
321541Srgrimes   alternative approach is to use autoconf's AC_CHECK_SIZEOF macro, but
331541Srgrimes   doing that would require that the configure script compile and *run*
341541Srgrimes   the resulting executable.  Locally running cross-compiled executables
351541Srgrimes   is usually not possible.  */
361541Srgrimes
37116182Sobrien#ifdef _LIBC
38116182Sobrien# include <sys/types.h>
39116182Sobrientypedef u_int32_t md5_uint32;
40224778Srwatson#else
41190759Srwatson#  define INT_MAX_32_BITS 2147483647
4213203Swollman
4313203Swollman/* If UINT_MAX isn't defined, assume it's a 32-bit type.
441541Srgrimes   This should be valid for all systems GNU cares about because
452112Swollman   that doesn't include 16-bit systems, and only modern systems
4669664Speter   (that certainly have <limits.h>) have 64+-bit integral types.  */
47224778Srwatson
48177785Skib# ifndef INT_MAX
49192895Sjamie#  define INT_MAX INT_MAX_32_BITS
5076166Smarkm# endif
5189316Salfred
521541Srgrimes# if INT_MAX == INT_MAX_32_BITS
531541Srgrimes   typedef unsigned int md5_uint32;
541541Srgrimes# else
551541Srgrimes#  if SHRT_MAX == INT_MAX_32_BITS
561541Srgrimes    typedef unsigned short md5_uint32;
57190759Srwatson#  else
58141471Sjhb#   if LONG_MAX == INT_MAX_32_BITS
59144613Sjeff     typedef unsigned long md5_uint32;
601541Srgrimes#   else
611541Srgrimes     /* The following line is intended to evoke an error.
621541Srgrimes        Using #error is not portable enough.  */
631541Srgrimes     "Cannot determine unsigned 32-bit data type."
64155334Srwatson#   endif
65163606Srwatson#  endif
66155334Srwatson# endif
6792751Sjeff#endif
6832011Sbde
69155168Sjeff#undef __P
70138345Sphk#if defined (__STDC__) && __STDC__
71138345Sphk#define	__P(x) x
72190759Srwatson#else
73211616Srpaulo#define	__P(x) ()
74190759Srwatson#endif
75211616Srpaulo
76190759Srwatson/* Structure to save state of computation between the single steps.  */
771541Srgrimesstruct md5_ctx
7869664Speter{
7969664Speter  md5_uint32 A;
8092751Sjeff  md5_uint32 B;
81166167Skib  md5_uint32 C;
82166167Skib  md5_uint32 D;
83166167Skib
84166167Skib  md5_uint32 total[2];
8569664Speter  md5_uint32 buflen;
8669664Speter  char buffer[128];
8769664Speter};
8869664Speter
89168138Srwatson/*
9092654Sjeff * The following three functions are build up the low level used in
9192654Sjeff * the functions `md5_stream' and `md5_buffer'.
92211531Sjhb */
93211531Sjhb
94176519Sattilio/* Initialize structure containing state of computation.
95211531Sjhb   (RFC 1321, 3.3: Step 3)  */
9669664Speterextern void md5_init_ctx __P ((struct md5_ctx *ctx));
97177253Srwatson
9869664Speter/* Starting with the result of former calls of this function (or the
99183520Sjhb   initialization function update the context for the next LEN bytes
100144613Sjeff   starting at BUFFER.
101144613Sjeff   It is necessary that LEN is a multiple of 64!!! */
102183519Sjhbextern void md5_process_block __P ((const void *buffer, size_t len,
103144613Sjeff				    struct md5_ctx *ctx));
10469664Speter
105161010Srwatson/* Starting with the result of former calls of this function (or the
1061541Srgrimes   initialization function update the context for the next LEN bytes
1071541Srgrimes   starting at BUFFER.
1081541Srgrimes   It is NOT required that LEN is a multiple of 64.  */
1091541Srgrimesextern void md5_process_bytes __P ((const void *buffer, size_t len,
1101541Srgrimes				    struct md5_ctx *ctx));
1111541Srgrimes
1121541Srgrimes/* Process the remaining bytes in the buffer and put result from CTX
1131541Srgrimes   in first 16 bytes following RESBUF.  The result is always in little
1141541Srgrimes   endian byte order, so that a byte-wise output yields to the wanted
1151541Srgrimes   ASCII representation of the message digest.
1161541Srgrimes
1171541Srgrimes   IMPORTANT: On some systems it is required that RESBUF is correctly
1181541Srgrimes   aligned for a 32 bits value.  */
1191541Srgrimesextern void *md5_finish_ctx __P ((struct md5_ctx *ctx, void *resbuf));
1201541Srgrimes
1211541Srgrimes
1221541Srgrimes/* Put result from CTX in first 16 bytes following RESBUF.  The result is
1231541Srgrimes   always in little endian byte order, so that a byte-wise output yields
1241541Srgrimes   to the wanted ASCII representation of the message digest.
125161011Srwatson
1261541Srgrimes   IMPORTANT: On some systems it is required that RESBUF is correctly
127161011Srwatson   aligned for a 32 bits value.  */
128161011Srwatsonextern void *md5_read_ctx __P ((const struct md5_ctx *ctx, void *resbuf));
129161011Srwatson
1301541Srgrimes
1311541Srgrimes/* Compute MD5 message digest for bytes read from STREAM.  The
1321541Srgrimes   resulting message digest number will be written into the 16 bytes
1331541Srgrimes   beginning at RESBLOCK.  */
13483366Sjulianextern int md5_stream __P ((FILE *stream, void *resblock));
13583366Sjulian
1361541Srgrimes/* Compute MD5 message digest for LEN bytes beginning at BUFFER.  The
13791419Sjhb   result is always in little endian byte order, so that a byte-wise
13883366Sjulian   output yields to the wanted ASCII representation of the message
13942408Seivind   digest.  */
14042453Seivindextern void *md5_buffer __P ((const char *buffer, size_t len, void *resblock));
14142408Seivind
14242453Seivind#endif
143144613Sjeff