12a6b7db3Sskrll /* md5.h - Declaration of functions and data types used for MD5 sum 22a6b7db3Sskrll computing library functions. 3*cb63e24eSchristos Copyright (C) 1995-2024 Free Software Foundation, Inc. 42a6b7db3Sskrll NOTE: The canonical source of this file is maintained with the GNU C 52a6b7db3Sskrll Library. Bugs can be reported to bug-glibc@prep.ai.mit.edu. 62a6b7db3Sskrll 72a6b7db3Sskrll This program is free software; you can redistribute it and/or modify it 82a6b7db3Sskrll under the terms of the GNU General Public License as published by the 92a6b7db3Sskrll Free Software Foundation; either version 2, or (at your option) any 102a6b7db3Sskrll later version. 112a6b7db3Sskrll 122a6b7db3Sskrll This program is distributed in the hope that it will be useful, 132a6b7db3Sskrll but WITHOUT ANY WARRANTY; without even the implied warranty of 142a6b7db3Sskrll MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 152a6b7db3Sskrll GNU General Public License for more details. 162a6b7db3Sskrll 172a6b7db3Sskrll You should have received a copy of the GNU General Public License 182a6b7db3Sskrll along with this program; if not, write to the Free Software Foundation, 192a6b7db3Sskrll Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ 202a6b7db3Sskrll 212a6b7db3Sskrll #ifndef _MD5_H 222a6b7db3Sskrll #define _MD5_H 1 232a6b7db3Sskrll 244f645668Schristos #ifdef USE_SYSTEM_MD5 254f645668Schristos #include_next <md5.h> 264f645668Schristos #else 274f645668Schristos 282a6b7db3Sskrll #include <stdio.h> 292a6b7db3Sskrll 302a6b7db3Sskrll #if defined HAVE_LIMITS_H || _LIBC 312a6b7db3Sskrll # include <limits.h> 322a6b7db3Sskrll #endif 332a6b7db3Sskrll 342a6b7db3Sskrll #include "ansidecl.h" 352a6b7db3Sskrll 362a6b7db3Sskrll /* The following contortions are an attempt to use the C preprocessor 372a6b7db3Sskrll to determine an unsigned integral type that is 32 bits wide. An 382a6b7db3Sskrll alternative approach is to use autoconf's AC_CHECK_SIZEOF macro, but 392a6b7db3Sskrll doing that would require that the configure script compile and *run* 402a6b7db3Sskrll the resulting executable. Locally running cross-compiled executables 412a6b7db3Sskrll is usually not possible. */ 422a6b7db3Sskrll 432a6b7db3Sskrll #ifdef _LIBC 442a6b7db3Sskrll # include <sys/types.h> 452a6b7db3Sskrll typedef u_int32_t md5_uint32; 462a6b7db3Sskrll typedef uintptr_t md5_uintptr; 479573673dSchristos #elif defined (HAVE_SYS_TYPES_H) && defined (HAVE_STDINT_H) 489573673dSchristos #include <stdint.h> 499573673dSchristos #include <sys/types.h> 509573673dSchristos typedef uint32_t md5_uint32; 519573673dSchristos typedef uintptr_t md5_uintptr; 522a6b7db3Sskrll #else 532a6b7db3Sskrll # define INT_MAX_32_BITS 2147483647 542a6b7db3Sskrll 552a6b7db3Sskrll /* If UINT_MAX isn't defined, assume it's a 32-bit type. 562a6b7db3Sskrll This should be valid for all systems GNU cares about because 572a6b7db3Sskrll that doesn't include 16-bit systems, and only modern systems 582a6b7db3Sskrll (that certainly have <limits.h>) have 64+-bit integral types. */ 592a6b7db3Sskrll 602a6b7db3Sskrll # ifndef INT_MAX 612a6b7db3Sskrll # define INT_MAX INT_MAX_32_BITS 622a6b7db3Sskrll # endif 632a6b7db3Sskrll 642a6b7db3Sskrll # if INT_MAX == INT_MAX_32_BITS 652a6b7db3Sskrll typedef unsigned int md5_uint32; 662a6b7db3Sskrll # else 672a6b7db3Sskrll # if SHRT_MAX == INT_MAX_32_BITS 682a6b7db3Sskrll typedef unsigned short md5_uint32; 692a6b7db3Sskrll # else 702a6b7db3Sskrll # if LONG_MAX == INT_MAX_32_BITS 712a6b7db3Sskrll typedef unsigned long md5_uint32; 722a6b7db3Sskrll # else 732a6b7db3Sskrll /* The following line is intended to evoke an error. 742a6b7db3Sskrll Using #error is not portable enough. */ 752a6b7db3Sskrll "Cannot determine unsigned 32-bit data type." 762a6b7db3Sskrll # endif 772a6b7db3Sskrll # endif 782a6b7db3Sskrll # endif 792a6b7db3Sskrll /* We have to make a guess about the integer type equivalent in size 802a6b7db3Sskrll to pointers which should always be correct. */ 812a6b7db3Sskrll typedef unsigned long int md5_uintptr; 822a6b7db3Sskrll #endif 832a6b7db3Sskrll 842a6b7db3Sskrll #ifdef __cplusplus 852a6b7db3Sskrll extern "C" { 862a6b7db3Sskrll #endif 872a6b7db3Sskrll 882a6b7db3Sskrll /* Structure to save state of computation between the single steps. */ 892a6b7db3Sskrll struct md5_ctx 902a6b7db3Sskrll { 912a6b7db3Sskrll md5_uint32 A; 922a6b7db3Sskrll md5_uint32 B; 932a6b7db3Sskrll md5_uint32 C; 942a6b7db3Sskrll md5_uint32 D; 952a6b7db3Sskrll 962a6b7db3Sskrll md5_uint32 total[2]; 972a6b7db3Sskrll md5_uint32 buflen; 982a6b7db3Sskrll char buffer[128] ATTRIBUTE_ALIGNED_ALIGNOF(md5_uint32); 992a6b7db3Sskrll }; 1002a6b7db3Sskrll 1012a6b7db3Sskrll /* 1022a6b7db3Sskrll * The following three functions are build up the low level used in 1032a6b7db3Sskrll * the functions `md5_stream' and `md5_buffer'. 1042a6b7db3Sskrll */ 1052a6b7db3Sskrll 1062a6b7db3Sskrll /* Initialize structure containing state of computation. 1072a6b7db3Sskrll (RFC 1321, 3.3: Step 3) */ 1082a6b7db3Sskrll extern void md5_init_ctx (struct md5_ctx *ctx); 1092a6b7db3Sskrll 1102a6b7db3Sskrll /* Starting with the result of former calls of this function (or the 1112a6b7db3Sskrll initialization function update the context for the next LEN bytes 1122a6b7db3Sskrll starting at BUFFER. 1132a6b7db3Sskrll It is necessary that LEN is a multiple of 64!!! */ 1142a6b7db3Sskrll extern void md5_process_block (const void *buffer, size_t len, 1152a6b7db3Sskrll struct md5_ctx *ctx); 1162a6b7db3Sskrll 1172a6b7db3Sskrll /* Starting with the result of former calls of this function (or the 1182a6b7db3Sskrll initialization function update the context for the next LEN bytes 1192a6b7db3Sskrll starting at BUFFER. 1202a6b7db3Sskrll It is NOT required that LEN is a multiple of 64. */ 1212a6b7db3Sskrll extern void md5_process_bytes (const void *buffer, size_t len, 1222a6b7db3Sskrll struct md5_ctx *ctx); 1232a6b7db3Sskrll 1242a6b7db3Sskrll /* Process the remaining bytes in the buffer and put result from CTX 1252a6b7db3Sskrll in first 16 bytes following RESBUF. The result is always in little 1262a6b7db3Sskrll endian byte order, so that a byte-wise output yields to the wanted 1272a6b7db3Sskrll ASCII representation of the message digest. 1282a6b7db3Sskrll 1292a6b7db3Sskrll IMPORTANT: On some systems it is required that RESBUF is correctly 1302a6b7db3Sskrll aligned for a 32 bits value. */ 1312a6b7db3Sskrll extern void *md5_finish_ctx (struct md5_ctx *ctx, void *resbuf); 1322a6b7db3Sskrll 1332a6b7db3Sskrll 1342a6b7db3Sskrll /* Put result from CTX in first 16 bytes following RESBUF. The result is 1352a6b7db3Sskrll always in little endian byte order, so that a byte-wise output yields 1362a6b7db3Sskrll to the wanted ASCII representation of the message digest. 1372a6b7db3Sskrll 1382a6b7db3Sskrll IMPORTANT: On some systems it is required that RESBUF is correctly 1392a6b7db3Sskrll aligned for a 32 bits value. */ 1402a6b7db3Sskrll extern void *md5_read_ctx (const struct md5_ctx *ctx, void *resbuf); 1412a6b7db3Sskrll 1422a6b7db3Sskrll 1432a6b7db3Sskrll /* Compute MD5 message digest for bytes read from STREAM. The 1442a6b7db3Sskrll resulting message digest number will be written into the 16 bytes 1452a6b7db3Sskrll beginning at RESBLOCK. */ 1462a6b7db3Sskrll extern int md5_stream (FILE *stream, void *resblock); 1472a6b7db3Sskrll 1482a6b7db3Sskrll /* Compute MD5 message digest for LEN bytes beginning at BUFFER. The 1492a6b7db3Sskrll result is always in little endian byte order, so that a byte-wise 1502a6b7db3Sskrll output yields to the wanted ASCII representation of the message 1512a6b7db3Sskrll digest. */ 1522a6b7db3Sskrll extern void *md5_buffer (const char *buffer, size_t len, void *resblock); 1532a6b7db3Sskrll 1542a6b7db3Sskrll #ifdef __cplusplus 1552a6b7db3Sskrll } 1562a6b7db3Sskrll #endif 1572a6b7db3Sskrll 1584f645668Schristos #endif // USE_SYSTEM_MD5 1594f645668Schristos 1602a6b7db3Sskrll #endif 161