xref: /netbsd-src/external/gpl3/binutils.old/dist/include/md5.h (revision e992f068c547fd6e84b3f104dc2340adcc955732)
175fd0b74Schristos /* md5.h - Declaration of functions and data types used for MD5 sum
275fd0b74Schristos    computing library functions.
3*e992f068Schristos    Copyright (C) 1995-2022 Free Software Foundation, Inc.
475fd0b74Schristos    NOTE: The canonical source of this file is maintained with the GNU C
575fd0b74Schristos    Library.  Bugs can be reported to bug-glibc@prep.ai.mit.edu.
675fd0b74Schristos 
775fd0b74Schristos    This program is free software; you can redistribute it and/or modify it
875fd0b74Schristos    under the terms of the GNU General Public License as published by the
975fd0b74Schristos    Free Software Foundation; either version 2, or (at your option) any
1075fd0b74Schristos    later version.
1175fd0b74Schristos 
1275fd0b74Schristos    This program is distributed in the hope that it will be useful,
1375fd0b74Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
1475fd0b74Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1575fd0b74Schristos    GNU General Public License for more details.
1675fd0b74Schristos 
1775fd0b74Schristos    You should have received a copy of the GNU General Public License
1875fd0b74Schristos    along with this program; if not, write to the Free Software Foundation,
1975fd0b74Schristos    Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
2075fd0b74Schristos 
2175fd0b74Schristos #ifndef _MD5_H
2275fd0b74Schristos #define _MD5_H 1
2375fd0b74Schristos 
24*e992f068Schristos #ifdef USE_SYSTEM_MD5
25*e992f068Schristos #include_next <md5.h>
26*e992f068Schristos #else
27*e992f068Schristos 
2875fd0b74Schristos #include <stdio.h>
2975fd0b74Schristos 
3075fd0b74Schristos #if defined HAVE_LIMITS_H || _LIBC
3175fd0b74Schristos # include <limits.h>
3275fd0b74Schristos #endif
3375fd0b74Schristos 
3475fd0b74Schristos #include "ansidecl.h"
3575fd0b74Schristos 
3675fd0b74Schristos /* The following contortions are an attempt to use the C preprocessor
3775fd0b74Schristos    to determine an unsigned integral type that is 32 bits wide.  An
3875fd0b74Schristos    alternative approach is to use autoconf's AC_CHECK_SIZEOF macro, but
3975fd0b74Schristos    doing that would require that the configure script compile and *run*
4075fd0b74Schristos    the resulting executable.  Locally running cross-compiled executables
4175fd0b74Schristos    is usually not possible.  */
4275fd0b74Schristos 
4375fd0b74Schristos #ifdef _LIBC
4475fd0b74Schristos # include <sys/types.h>
4575fd0b74Schristos typedef u_int32_t md5_uint32;
4675fd0b74Schristos typedef uintptr_t md5_uintptr;
4775fd0b74Schristos #elif defined (HAVE_SYS_TYPES_H) && defined (HAVE_STDINT_H)
4875fd0b74Schristos #include <stdint.h>
4975fd0b74Schristos #include <sys/types.h>
5075fd0b74Schristos typedef uint32_t md5_uint32;
5175fd0b74Schristos typedef uintptr_t md5_uintptr;
5275fd0b74Schristos #else
5375fd0b74Schristos #  define INT_MAX_32_BITS 2147483647
5475fd0b74Schristos 
5575fd0b74Schristos /* If UINT_MAX isn't defined, assume it's a 32-bit type.
5675fd0b74Schristos    This should be valid for all systems GNU cares about because
5775fd0b74Schristos    that doesn't include 16-bit systems, and only modern systems
5875fd0b74Schristos    (that certainly have <limits.h>) have 64+-bit integral types.  */
5975fd0b74Schristos 
6075fd0b74Schristos # ifndef INT_MAX
6175fd0b74Schristos #  define INT_MAX INT_MAX_32_BITS
6275fd0b74Schristos # endif
6375fd0b74Schristos 
6475fd0b74Schristos # if INT_MAX == INT_MAX_32_BITS
6575fd0b74Schristos    typedef unsigned int md5_uint32;
6675fd0b74Schristos # else
6775fd0b74Schristos #  if SHRT_MAX == INT_MAX_32_BITS
6875fd0b74Schristos     typedef unsigned short md5_uint32;
6975fd0b74Schristos #  else
7075fd0b74Schristos #   if LONG_MAX == INT_MAX_32_BITS
7175fd0b74Schristos      typedef unsigned long md5_uint32;
7275fd0b74Schristos #   else
7375fd0b74Schristos      /* The following line is intended to evoke an error.
7475fd0b74Schristos         Using #error is not portable enough.  */
7575fd0b74Schristos      "Cannot determine unsigned 32-bit data type."
7675fd0b74Schristos #   endif
7775fd0b74Schristos #  endif
7875fd0b74Schristos # endif
7975fd0b74Schristos /* We have to make a guess about the integer type equivalent in size
8075fd0b74Schristos    to pointers which should always be correct.  */
8175fd0b74Schristos typedef unsigned long int md5_uintptr;
8275fd0b74Schristos #endif
8375fd0b74Schristos 
8475fd0b74Schristos #ifdef __cplusplus
8575fd0b74Schristos extern "C" {
8675fd0b74Schristos #endif
8775fd0b74Schristos 
8875fd0b74Schristos /* Structure to save state of computation between the single steps.  */
8975fd0b74Schristos struct md5_ctx
9075fd0b74Schristos {
9175fd0b74Schristos   md5_uint32 A;
9275fd0b74Schristos   md5_uint32 B;
9375fd0b74Schristos   md5_uint32 C;
9475fd0b74Schristos   md5_uint32 D;
9575fd0b74Schristos 
9675fd0b74Schristos   md5_uint32 total[2];
9775fd0b74Schristos   md5_uint32 buflen;
9875fd0b74Schristos   char buffer[128] ATTRIBUTE_ALIGNED_ALIGNOF(md5_uint32);
9975fd0b74Schristos };
10075fd0b74Schristos 
10175fd0b74Schristos /*
10275fd0b74Schristos  * The following three functions are build up the low level used in
10375fd0b74Schristos  * the functions `md5_stream' and `md5_buffer'.
10475fd0b74Schristos  */
10575fd0b74Schristos 
10675fd0b74Schristos /* Initialize structure containing state of computation.
10775fd0b74Schristos    (RFC 1321, 3.3: Step 3)  */
10875fd0b74Schristos extern void md5_init_ctx (struct md5_ctx *ctx);
10975fd0b74Schristos 
11075fd0b74Schristos /* Starting with the result of former calls of this function (or the
11175fd0b74Schristos    initialization function update the context for the next LEN bytes
11275fd0b74Schristos    starting at BUFFER.
11375fd0b74Schristos    It is necessary that LEN is a multiple of 64!!! */
11475fd0b74Schristos extern void md5_process_block (const void *buffer, size_t len,
11575fd0b74Schristos                                struct md5_ctx *ctx);
11675fd0b74Schristos 
11775fd0b74Schristos /* Starting with the result of former calls of this function (or the
11875fd0b74Schristos    initialization function update the context for the next LEN bytes
11975fd0b74Schristos    starting at BUFFER.
12075fd0b74Schristos    It is NOT required that LEN is a multiple of 64.  */
12175fd0b74Schristos extern void md5_process_bytes (const void *buffer, size_t len,
12275fd0b74Schristos                                struct md5_ctx *ctx);
12375fd0b74Schristos 
12475fd0b74Schristos /* Process the remaining bytes in the buffer and put result from CTX
12575fd0b74Schristos    in first 16 bytes following RESBUF.  The result is always in little
12675fd0b74Schristos    endian byte order, so that a byte-wise output yields to the wanted
12775fd0b74Schristos    ASCII representation of the message digest.
12875fd0b74Schristos 
12975fd0b74Schristos    IMPORTANT: On some systems it is required that RESBUF is correctly
13075fd0b74Schristos    aligned for a 32 bits value.  */
13175fd0b74Schristos extern void *md5_finish_ctx (struct md5_ctx *ctx, void *resbuf);
13275fd0b74Schristos 
13375fd0b74Schristos 
13475fd0b74Schristos /* Put result from CTX in first 16 bytes following RESBUF.  The result is
13575fd0b74Schristos    always in little endian byte order, so that a byte-wise output yields
13675fd0b74Schristos    to the wanted ASCII representation of the message digest.
13775fd0b74Schristos 
13875fd0b74Schristos    IMPORTANT: On some systems it is required that RESBUF is correctly
13975fd0b74Schristos    aligned for a 32 bits value.  */
14075fd0b74Schristos extern void *md5_read_ctx (const struct md5_ctx *ctx, void *resbuf);
14175fd0b74Schristos 
14275fd0b74Schristos 
14375fd0b74Schristos /* Compute MD5 message digest for bytes read from STREAM.  The
14475fd0b74Schristos    resulting message digest number will be written into the 16 bytes
14575fd0b74Schristos    beginning at RESBLOCK.  */
14675fd0b74Schristos extern int md5_stream (FILE *stream, void *resblock);
14775fd0b74Schristos 
14875fd0b74Schristos /* Compute MD5 message digest for LEN bytes beginning at BUFFER.  The
14975fd0b74Schristos    result is always in little endian byte order, so that a byte-wise
15075fd0b74Schristos    output yields to the wanted ASCII representation of the message
15175fd0b74Schristos    digest.  */
15275fd0b74Schristos extern void *md5_buffer (const char *buffer, size_t len, void *resblock);
15375fd0b74Schristos 
15475fd0b74Schristos #ifdef __cplusplus
15575fd0b74Schristos }
15675fd0b74Schristos #endif
15775fd0b74Schristos 
158*e992f068Schristos #endif // USE_SYSTEM_MD5
159*e992f068Schristos 
16075fd0b74Schristos #endif
161