1130803Smarcel/* md5.h - Declaration of functions and data types used for MD5 sum
2130803Smarcel   computing library functions.
3130803Smarcel   Copyright 1995, 1996, 2000 Free Software Foundation, Inc.
4130803Smarcel   NOTE: The canonical source of this file is maintained with the GNU C
5130803Smarcel   Library.  Bugs can be reported to bug-glibc@prep.ai.mit.edu.
6130803Smarcel
7130803Smarcel   This program is free software; you can redistribute it and/or modify it
8130803Smarcel   under the terms of the GNU General Public License as published by the
9130803Smarcel   Free Software Foundation; either version 2, or (at your option) any
10130803Smarcel   later version.
11130803Smarcel
12130803Smarcel   This program is distributed in the hope that it will be useful,
13130803Smarcel   but WITHOUT ANY WARRANTY; without even the implied warranty of
14130803Smarcel   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15130803Smarcel   GNU General Public License for more details.
16130803Smarcel
17130803Smarcel   You should have received a copy of the GNU General Public License
18130803Smarcel   along with this program; if not, write to the Free Software Foundation,
19130803Smarcel   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
20130803Smarcel
21130803Smarcel#ifndef _MD5_H
22130803Smarcel#define _MD5_H 1
23130803Smarcel
24130803Smarcel#include <stdio.h>
25130803Smarcel
26130803Smarcel#if defined HAVE_LIMITS_H || _LIBC
27130803Smarcel# include <limits.h>
28130803Smarcel#endif
29130803Smarcel
30130803Smarcel/* The following contortions are an attempt to use the C preprocessor
31130803Smarcel   to determine an unsigned integral type that is 32 bits wide.  An
32130803Smarcel   alternative approach is to use autoconf's AC_CHECK_SIZEOF macro, but
33130803Smarcel   doing that would require that the configure script compile and *run*
34130803Smarcel   the resulting executable.  Locally running cross-compiled executables
35130803Smarcel   is usually not possible.  */
36130803Smarcel
37130803Smarcel#ifdef _LIBC
38130803Smarcel# include <sys/types.h>
39130803Smarceltypedef u_int32_t md5_uint32;
40130803Smarcel#else
41130803Smarcel#  define INT_MAX_32_BITS 2147483647
42130803Smarcel
43130803Smarcel/* If UINT_MAX isn't defined, assume it's a 32-bit type.
44130803Smarcel   This should be valid for all systems GNU cares about because
45130803Smarcel   that doesn't include 16-bit systems, and only modern systems
46130803Smarcel   (that certainly have <limits.h>) have 64+-bit integral types.  */
47130803Smarcel
48130803Smarcel# ifndef INT_MAX
49130803Smarcel#  define INT_MAX INT_MAX_32_BITS
50130803Smarcel# endif
51130803Smarcel
52130803Smarcel# if INT_MAX == INT_MAX_32_BITS
53130803Smarcel   typedef unsigned int md5_uint32;
54130803Smarcel# else
55130803Smarcel#  if SHRT_MAX == INT_MAX_32_BITS
56130803Smarcel    typedef unsigned short md5_uint32;
57130803Smarcel#  else
58130803Smarcel#   if LONG_MAX == INT_MAX_32_BITS
59130803Smarcel     typedef unsigned long md5_uint32;
60130803Smarcel#   else
61130803Smarcel     /* The following line is intended to evoke an error.
62130803Smarcel        Using #error is not portable enough.  */
63130803Smarcel     "Cannot determine unsigned 32-bit data type."
64130803Smarcel#   endif
65130803Smarcel#  endif
66130803Smarcel# endif
67130803Smarcel#endif
68130803Smarcel
69130803Smarcel#undef __P
70130803Smarcel#if defined (__STDC__) && __STDC__
71130803Smarcel#define	__P(x) x
72130803Smarcel#else
73130803Smarcel#define	__P(x) ()
74130803Smarcel#endif
75130803Smarcel
76130803Smarcel/* Structure to save state of computation between the single steps.  */
77130803Smarcelstruct md5_ctx
78130803Smarcel{
79130803Smarcel  md5_uint32 A;
80130803Smarcel  md5_uint32 B;
81130803Smarcel  md5_uint32 C;
82130803Smarcel  md5_uint32 D;
83130803Smarcel
84130803Smarcel  md5_uint32 total[2];
85130803Smarcel  md5_uint32 buflen;
86130803Smarcel  char buffer[128];
87130803Smarcel};
88130803Smarcel
89130803Smarcel/*
90130803Smarcel * The following three functions are build up the low level used in
91130803Smarcel * the functions `md5_stream' and `md5_buffer'.
92130803Smarcel */
93130803Smarcel
94130803Smarcel/* Initialize structure containing state of computation.
95130803Smarcel   (RFC 1321, 3.3: Step 3)  */
96130803Smarcelextern void md5_init_ctx __P ((struct md5_ctx *ctx));
97130803Smarcel
98130803Smarcel/* Starting with the result of former calls of this function (or the
99130803Smarcel   initialization function update the context for the next LEN bytes
100130803Smarcel   starting at BUFFER.
101130803Smarcel   It is necessary that LEN is a multiple of 64!!! */
102130803Smarcelextern void md5_process_block __P ((const void *buffer, size_t len,
103130803Smarcel				    struct md5_ctx *ctx));
104130803Smarcel
105130803Smarcel/* Starting with the result of former calls of this function (or the
106130803Smarcel   initialization function update the context for the next LEN bytes
107130803Smarcel   starting at BUFFER.
108130803Smarcel   It is NOT required that LEN is a multiple of 64.  */
109130803Smarcelextern void md5_process_bytes __P ((const void *buffer, size_t len,
110130803Smarcel				    struct md5_ctx *ctx));
111130803Smarcel
112130803Smarcel/* Process the remaining bytes in the buffer and put result from CTX
113130803Smarcel   in first 16 bytes following RESBUF.  The result is always in little
114130803Smarcel   endian byte order, so that a byte-wise output yields to the wanted
115130803Smarcel   ASCII representation of the message digest.
116130803Smarcel
117130803Smarcel   IMPORTANT: On some systems it is required that RESBUF is correctly
118130803Smarcel   aligned for a 32 bits value.  */
119130803Smarcelextern void *md5_finish_ctx __P ((struct md5_ctx *ctx, void *resbuf));
120130803Smarcel
121130803Smarcel
122130803Smarcel/* Put result from CTX in first 16 bytes following RESBUF.  The result is
123130803Smarcel   always in little endian byte order, so that a byte-wise output yields
124130803Smarcel   to the wanted ASCII representation of the message digest.
125130803Smarcel
126130803Smarcel   IMPORTANT: On some systems it is required that RESBUF is correctly
127130803Smarcel   aligned for a 32 bits value.  */
128130803Smarcelextern void *md5_read_ctx __P ((const struct md5_ctx *ctx, void *resbuf));
129130803Smarcel
130130803Smarcel
131130803Smarcel/* Compute MD5 message digest for bytes read from STREAM.  The
132130803Smarcel   resulting message digest number will be written into the 16 bytes
133130803Smarcel   beginning at RESBLOCK.  */
134130803Smarcelextern int md5_stream __P ((FILE *stream, void *resblock));
135130803Smarcel
136130803Smarcel/* Compute MD5 message digest for LEN bytes beginning at BUFFER.  The
137130803Smarcel   result is always in little endian byte order, so that a byte-wise
138130803Smarcel   output yields to the wanted ASCII representation of the message
139130803Smarcel   digest.  */
140130803Smarcelextern void *md5_buffer __P ((const char *buffer, size_t len, void *resblock));
141130803Smarcel
142130803Smarcel#endif
143