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