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