1 /* md5.h - Declaration of functions and data types used for MD5 sum 2 computing library functions. 3 Copyright (C) 1995, 1996, 1999, 2000, 2003 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@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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 20 21 #ifndef _MD5_H 22 #define _MD5_H 1 23 24 #include <stdio.h> 25 #include <limits.h> 26 27 /* The following contortions are an attempt to use the C preprocessor 28 to determine an unsigned integral type that is 32 bits wide. An 29 alternative approach is to use autoconf's AC_CHECK_SIZEOF macro, but 30 doing that would require that the configure script compile and *run* 31 the resulting executable. Locally running cross-compiled executables 32 is usually not possible. */ 33 34 #ifdef _LIBC 35 # include <stdint.h> 36 typedef uint32_t md5_uint32; 37 typedef uintptr_t md5_uintptr; 38 #else 39 # define UINT_MAX_32_BITS 4294967295U 40 41 # if UINT_MAX == UINT_MAX_32_BITS 42 typedef unsigned int md5_uint32; 43 # else 44 # if USHRT_MAX == UINT_MAX_32_BITS 45 typedef unsigned short md5_uint32; 46 # else 47 # if ULONG_MAX == UINT_MAX_32_BITS 48 typedef unsigned long md5_uint32; 49 # else 50 /* The following line is intended to evoke an error. 51 Using #error is not portable enough. */ 52 "Cannot determine unsigned 32-bit data type." 53 # endif 54 # endif 55 # endif 56 /* We have to make a guess about the integer type equivalent in size 57 to pointers which should always be correct. */ 58 typedef unsigned long int md5_uintptr; 59 #endif 60 61 /* Structure to save state of computation between the single steps. */ 62 struct md5_ctx 63 { 64 md5_uint32 A; 65 md5_uint32 B; 66 md5_uint32 C; 67 md5_uint32 D; 68 69 md5_uint32 total[2]; 70 md5_uint32 buflen; 71 char buffer[128]; 72 }; 73 74 /* 75 * The following three functions are build up the low level used in 76 * the functions `md5_stream' and `md5_buffer'. 77 */ 78 79 /* Initialize structure containing state of computation. 80 (RFC 1321, 3.3: Step 3) */ 81 extern void md5_init_ctx (struct md5_ctx *ctx); 82 83 /* Starting with the result of former calls of this function (or the 84 initialization function update the context for the next LEN bytes 85 starting at BUFFER. 86 It is necessary that LEN is a multiple of 64!!! */ 87 extern void md5_process_block (const void *buffer, size_t len, 88 struct md5_ctx *ctx); 89 90 /* Starting with the result of former calls of this function (or the 91 initialization function update the context for the next LEN bytes 92 starting at BUFFER. 93 It is NOT required that LEN is a multiple of 64. */ 94 extern void md5_process_bytes (const void *buffer, size_t len, 95 struct md5_ctx *ctx); 96 97 /* Process the remaining bytes in the buffer and put result from CTX 98 in first 16 bytes following RESBUF. The result is always in little 99 endian byte order, so that a byte-wise output yields to the wanted 100 ASCII representation of the message digest. 101 102 IMPORTANT: On some systems it is required that RESBUF be correctly 103 aligned for a 32 bits value. */ 104 extern void *md5_finish_ctx (struct md5_ctx *ctx, void *resbuf); 105 106 107 /* Put result from CTX in first 16 bytes following RESBUF. The result is 108 always in little endian byte order, so that a byte-wise output yields 109 to the wanted ASCII representation of the message digest. 110 111 IMPORTANT: On some systems it is required that RESBUF is correctly 112 aligned for a 32 bits value. */ 113 extern void *md5_read_ctx (const struct md5_ctx *ctx, void *resbuf); 114 115 116 /* Compute MD5 message digest for LEN bytes beginning at BUFFER. The 117 result is always in little endian byte order, so that a byte-wise 118 output yields to the wanted ASCII representation of the message 119 digest. */ 120 extern void *md5_buffer (const char *buffer, size_t len, void *resblock); 121 122 #define rol(x,n) ( ((x) << (n)) | ((x) >> (32-(n))) ) 123 124 #endif 125