xref: /netbsd-src/external/gpl2/xcvs/dist/lib/md5.h (revision a7c918477dd5f12c1da816ba05caf44eab2d06d6)
1*a7c91847Schristos /* Declaration of functions and data types used for MD5 sum computing
2*a7c91847Schristos    library functions.
3*a7c91847Schristos    Copyright (C) 1995-1997,1999-2005 Free Software Foundation, Inc.
4*a7c91847Schristos 
5*a7c91847Schristos    NOTE: The canonical source of this file is maintained with the GNU C
6*a7c91847Schristos    Library.  Bugs can be reported to bug-glibc@prep.ai.mit.edu.
7*a7c91847Schristos 
8*a7c91847Schristos    This program is free software; you can redistribute it and/or modify it
9*a7c91847Schristos    under the terms of the GNU General Public License as published by the
10*a7c91847Schristos    Free Software Foundation; either version 2, or (at your option) any
11*a7c91847Schristos    later version.
12*a7c91847Schristos 
13*a7c91847Schristos    This program is distributed in the hope that it will be useful,
14*a7c91847Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
15*a7c91847Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16*a7c91847Schristos    GNU General Public License for more details.
17*a7c91847Schristos 
18*a7c91847Schristos    You should have received a copy of the GNU General Public License
19*a7c91847Schristos    along with this program; if not, write to the Free Software Foundation,
20*a7c91847Schristos    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
21*a7c91847Schristos 
22*a7c91847Schristos #ifndef _MD5_H
23*a7c91847Schristos #define _MD5_H 1
24*a7c91847Schristos 
25*a7c91847Schristos #include <stdio.h>
26*a7c91847Schristos 
27*a7c91847Schristos #if HAVE_INTTYPES_H
28*a7c91847Schristos # include <inttypes.h>
29*a7c91847Schristos #endif
30*a7c91847Schristos #if HAVE_STDINT_H || _LIBC
31*a7c91847Schristos # include <stdint.h>
32*a7c91847Schristos #endif
33*a7c91847Schristos 
34*a7c91847Schristos #ifndef __GNUC_PREREQ
35*a7c91847Schristos # if defined __GNUC__ && defined __GNUC_MINOR__
36*a7c91847Schristos #  define __GNUC_PREREQ(maj, min) \
37*a7c91847Schristos 	((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
38*a7c91847Schristos # else
39*a7c91847Schristos #  define __GNUC_PREREQ(maj, min) 0
40*a7c91847Schristos # endif
41*a7c91847Schristos #endif
42*a7c91847Schristos 
43*a7c91847Schristos #ifndef __THROW
44*a7c91847Schristos # if defined __cplusplus && __GNUC_PREREQ (2,8)
45*a7c91847Schristos #  define __THROW	throw ()
46*a7c91847Schristos # else
47*a7c91847Schristos #  define __THROW
48*a7c91847Schristos # endif
49*a7c91847Schristos #endif
50*a7c91847Schristos 
51*a7c91847Schristos #ifndef __attribute__
52*a7c91847Schristos # if ! __GNUC_PREREQ (2,8) || __STRICT_ANSI__
53*a7c91847Schristos #  define __attribute__(x)
54*a7c91847Schristos # endif
55*a7c91847Schristos #endif
56*a7c91847Schristos 
57*a7c91847Schristos #ifndef _LIBC
58*a7c91847Schristos # define __md5_buffer md5_buffer
59*a7c91847Schristos # define __md5_finish_ctx md5_finish_ctx
60*a7c91847Schristos # define __md5_init_ctx md5_init_ctx
61*a7c91847Schristos # define __md5_process_block md5_process_block
62*a7c91847Schristos # define __md5_process_bytes md5_process_bytes
63*a7c91847Schristos # define __md5_read_ctx md5_read_ctx
64*a7c91847Schristos # define __md5_stream md5_stream
65*a7c91847Schristos #endif
66*a7c91847Schristos 
67*a7c91847Schristos typedef uint32_t md5_uint32;
68*a7c91847Schristos 
69*a7c91847Schristos /* Structure to save state of computation between the single steps.  */
70*a7c91847Schristos struct md5_ctx
71*a7c91847Schristos {
72*a7c91847Schristos   md5_uint32 A;
73*a7c91847Schristos   md5_uint32 B;
74*a7c91847Schristos   md5_uint32 C;
75*a7c91847Schristos   md5_uint32 D;
76*a7c91847Schristos 
77*a7c91847Schristos   md5_uint32 total[2];
78*a7c91847Schristos   md5_uint32 buflen;
79*a7c91847Schristos   char buffer[128] __attribute__ ((__aligned__ (__alignof__ (md5_uint32))));
80*a7c91847Schristos };
81*a7c91847Schristos 
82*a7c91847Schristos /*
83*a7c91847Schristos  * The following three functions are build up the low level used in
84*a7c91847Schristos  * the functions `md5_stream' and `md5_buffer'.
85*a7c91847Schristos  */
86*a7c91847Schristos 
87*a7c91847Schristos /* Initialize structure containing state of computation.
88*a7c91847Schristos    (RFC 1321, 3.3: Step 3)  */
89*a7c91847Schristos extern void __md5_init_ctx (struct md5_ctx *ctx) __THROW;
90*a7c91847Schristos 
91*a7c91847Schristos /* Starting with the result of former calls of this function (or the
92*a7c91847Schristos    initialization function update the context for the next LEN bytes
93*a7c91847Schristos    starting at BUFFER.
94*a7c91847Schristos    It is necessary that LEN is a multiple of 64!!! */
95*a7c91847Schristos extern void __md5_process_block (const void *buffer, size_t len,
96*a7c91847Schristos 				 struct md5_ctx *ctx) __THROW;
97*a7c91847Schristos 
98*a7c91847Schristos /* Starting with the result of former calls of this function (or the
99*a7c91847Schristos    initialization function update the context for the next LEN bytes
100*a7c91847Schristos    starting at BUFFER.
101*a7c91847Schristos    It is NOT required that LEN is a multiple of 64.  */
102*a7c91847Schristos extern void __md5_process_bytes (const void *buffer, size_t len,
103*a7c91847Schristos 				 struct md5_ctx *ctx) __THROW;
104*a7c91847Schristos 
105*a7c91847Schristos /* Process the remaining bytes in the buffer and put result from CTX
106*a7c91847Schristos    in first 16 bytes following RESBUF.  The result is always in little
107*a7c91847Schristos    endian byte order, so that a byte-wise output yields to the wanted
108*a7c91847Schristos    ASCII representation of the message digest.
109*a7c91847Schristos 
110*a7c91847Schristos    IMPORTANT: On some systems it is required that RESBUF be correctly
111*a7c91847Schristos    aligned for a 32 bits value.  */
112*a7c91847Schristos extern void *__md5_finish_ctx (struct md5_ctx *ctx, void *resbuf) __THROW;
113*a7c91847Schristos 
114*a7c91847Schristos 
115*a7c91847Schristos /* Put result from CTX in first 16 bytes following RESBUF.  The result is
116*a7c91847Schristos    always in little endian byte order, so that a byte-wise output yields
117*a7c91847Schristos    to the wanted ASCII representation of the message digest.
118*a7c91847Schristos 
119*a7c91847Schristos    IMPORTANT: On some systems it is required that RESBUF is correctly
120*a7c91847Schristos    aligned for a 32 bits value.  */
121*a7c91847Schristos extern void *__md5_read_ctx (const struct md5_ctx *ctx, void *resbuf) __THROW;
122*a7c91847Schristos 
123*a7c91847Schristos 
124*a7c91847Schristos /* Compute MD5 message digest for bytes read from STREAM.  The
125*a7c91847Schristos    resulting message digest number will be written into the 16 bytes
126*a7c91847Schristos    beginning at RESBLOCK.  */
127*a7c91847Schristos extern int __md5_stream (FILE *stream, void *resblock) __THROW;
128*a7c91847Schristos 
129*a7c91847Schristos /* Compute MD5 message digest for LEN bytes beginning at BUFFER.  The
130*a7c91847Schristos    result is always in little endian byte order, so that a byte-wise
131*a7c91847Schristos    output yields to the wanted ASCII representation of the message
132*a7c91847Schristos    digest.  */
133*a7c91847Schristos extern void *__md5_buffer (const char *buffer, size_t len,
134*a7c91847Schristos 			   void *resblock) __THROW;
135*a7c91847Schristos 
136*a7c91847Schristos #endif /* md5.h */
137