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