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