1169695Skan/* md5.h - Declaration of functions and data types used for MD5 sum
2169695Skan   computing library functions.
3169695Skan   Copyright 1995, 1996, 2000 Free Software Foundation, Inc.
4169695Skan   NOTE: The canonical source of this file is maintained with the GNU C
5169695Skan   Library.  Bugs can be reported to bug-glibc@prep.ai.mit.edu.
6169695Skan
7169695Skan   This program is free software; you can redistribute it and/or modify it
8169695Skan   under the terms of the GNU General Public License as published by the
9169695Skan   Free Software Foundation; either version 2, or (at your option) any
10169695Skan   later version.
11169695Skan
12169695Skan   This program is distributed in the hope that it will be useful,
13169695Skan   but WITHOUT ANY WARRANTY; without even the implied warranty of
14169695Skan   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15169695Skan   GNU General Public License for more details.
16169695Skan
17169695Skan   You should have received a copy of the GNU General Public License
18169695Skan   along with this program; if not, write to the Free Software Foundation,
19169695Skan   Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
20169695Skan
21169695Skan#ifndef _MD5_H
22169695Skan#define _MD5_H 1
23169695Skan
24169695Skan#include <stdio.h>
25169695Skan
26169695Skan#if defined HAVE_LIMITS_H || _LIBC
27169695Skan# include <limits.h>
28169695Skan#endif
29169695Skan
30169695Skan#include "ansidecl.h"
31169695Skan
32169695Skan/* The following contortions are an attempt to use the C preprocessor
33169695Skan   to determine an unsigned integral type that is 32 bits wide.  An
34169695Skan   alternative approach is to use autoconf's AC_CHECK_SIZEOF macro, but
35169695Skan   doing that would require that the configure script compile and *run*
36169695Skan   the resulting executable.  Locally running cross-compiled executables
37169695Skan   is usually not possible.  */
38169695Skan
39169695Skan#ifdef _LIBC
40169695Skan# include <sys/types.h>
41169695Skantypedef u_int32_t md5_uint32;
42169695Skantypedef uintptr_t md5_uintptr;
43169695Skan#else
44169695Skan#  define INT_MAX_32_BITS 2147483647
45169695Skan
46169695Skan/* If UINT_MAX isn't defined, assume it's a 32-bit type.
47169695Skan   This should be valid for all systems GNU cares about because
48169695Skan   that doesn't include 16-bit systems, and only modern systems
49169695Skan   (that certainly have <limits.h>) have 64+-bit integral types.  */
50169695Skan
51169695Skan# ifndef INT_MAX
52169695Skan#  define INT_MAX INT_MAX_32_BITS
53169695Skan# endif
54169695Skan
55169695Skan# if INT_MAX == INT_MAX_32_BITS
56169695Skan   typedef unsigned int md5_uint32;
57169695Skan# else
58169695Skan#  if SHRT_MAX == INT_MAX_32_BITS
59169695Skan    typedef unsigned short md5_uint32;
60169695Skan#  else
61169695Skan#   if LONG_MAX == INT_MAX_32_BITS
62169695Skan     typedef unsigned long md5_uint32;
63169695Skan#   else
64169695Skan     /* The following line is intended to evoke an error.
65169695Skan        Using #error is not portable enough.  */
66169695Skan     "Cannot determine unsigned 32-bit data type."
67169695Skan#   endif
68169695Skan#  endif
69169695Skan# endif
70169695Skan/* We have to make a guess about the integer type equivalent in size
71169695Skan   to pointers which should always be correct.  */
72169695Skantypedef unsigned long int md5_uintptr;
73169695Skan#endif
74169695Skan
75169695Skan/* Structure to save state of computation between the single steps.  */
76169695Skanstruct md5_ctx
77169695Skan{
78169695Skan  md5_uint32 A;
79169695Skan  md5_uint32 B;
80169695Skan  md5_uint32 C;
81169695Skan  md5_uint32 D;
82169695Skan
83169695Skan  md5_uint32 total[2];
84169695Skan  md5_uint32 buflen;
85169695Skan  char buffer[128] ATTRIBUTE_ALIGNED_ALIGNOF(md5_uint32);
86169695Skan};
87169695Skan
88169695Skan/*
89169695Skan * The following three functions are build up the low level used in
90169695Skan * the functions `md5_stream' and `md5_buffer'.
91169695Skan */
92169695Skan
93169695Skan/* Initialize structure containing state of computation.
94169695Skan   (RFC 1321, 3.3: Step 3)  */
95169695Skanextern void md5_init_ctx (struct md5_ctx *ctx);
96169695Skan
97169695Skan/* Starting with the result of former calls of this function (or the
98169695Skan   initialization function update the context for the next LEN bytes
99169695Skan   starting at BUFFER.
100169695Skan   It is necessary that LEN is a multiple of 64!!! */
101169695Skanextern void md5_process_block (const void *buffer, size_t len,
102169695Skan                               struct md5_ctx *ctx);
103169695Skan
104169695Skan/* Starting with the result of former calls of this function (or the
105169695Skan   initialization function update the context for the next LEN bytes
106169695Skan   starting at BUFFER.
107169695Skan   It is NOT required that LEN is a multiple of 64.  */
108169695Skanextern void md5_process_bytes (const void *buffer, size_t len,
109169695Skan                               struct md5_ctx *ctx);
110169695Skan
111169695Skan/* Process the remaining bytes in the buffer and put result from CTX
112169695Skan   in first 16 bytes following RESBUF.  The result is always in little
113169695Skan   endian byte order, so that a byte-wise output yields to the wanted
114169695Skan   ASCII representation of the message digest.
115169695Skan
116169695Skan   IMPORTANT: On some systems it is required that RESBUF is correctly
117169695Skan   aligned for a 32 bits value.  */
118169695Skanextern void *md5_finish_ctx (struct md5_ctx *ctx, void *resbuf);
119169695Skan
120169695Skan
121169695Skan/* Put result from CTX in first 16 bytes following RESBUF.  The result is
122169695Skan   always in little endian byte order, so that a byte-wise output yields
123169695Skan   to the wanted ASCII representation of the message digest.
124169695Skan
125169695Skan   IMPORTANT: On some systems it is required that RESBUF is correctly
126169695Skan   aligned for a 32 bits value.  */
127169695Skanextern void *md5_read_ctx (const struct md5_ctx *ctx, void *resbuf);
128169695Skan
129169695Skan
130169695Skan/* Compute MD5 message digest for bytes read from STREAM.  The
131169695Skan   resulting message digest number will be written into the 16 bytes
132169695Skan   beginning at RESBLOCK.  */
133169695Skanextern int md5_stream (FILE *stream, void *resblock);
134169695Skan
135169695Skan/* Compute MD5 message digest for LEN bytes beginning at BUFFER.  The
136169695Skan   result is always in little endian byte order, so that a byte-wise
137169695Skan   output yields to the wanted ASCII representation of the message
138169695Skan   digest.  */
139169695Skanextern void *md5_buffer (const char *buffer, size_t len, void *resblock);
140169695Skan
141169695Skan#endif
142