198b9484cSchristos /* Declarations of functions and data types used for SHA1 sum 298b9484cSchristos library functions. 3*e663ba6eSchristos Copyright (C) 2000-2024 Free Software Foundation, Inc. 498b9484cSchristos 598b9484cSchristos This program is free software; you can redistribute it and/or modify it 698b9484cSchristos under the terms of the GNU General Public License as published by the 798b9484cSchristos Free Software Foundation; either version 3, or (at your option) any 898b9484cSchristos later version. 998b9484cSchristos 1098b9484cSchristos This program is distributed in the hope that it will be useful, 1198b9484cSchristos but WITHOUT ANY WARRANTY; without even the implied warranty of 1298b9484cSchristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1398b9484cSchristos GNU General Public License for more details. 1498b9484cSchristos 1598b9484cSchristos You should have received a copy of the GNU General Public License 1698b9484cSchristos along with this program; if not, write to the Free Software Foundation, 1798b9484cSchristos Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ 1898b9484cSchristos 1998b9484cSchristos #ifndef SHA1_H 2098b9484cSchristos # define SHA1_H 1 2198b9484cSchristos 2298b9484cSchristos #include <stdio.h> 2398b9484cSchristos 2498b9484cSchristos #if defined HAVE_LIMITS_H || _LIBC 2598b9484cSchristos # include <limits.h> 2698b9484cSchristos #endif 2798b9484cSchristos 2898b9484cSchristos #include "ansidecl.h" 2998b9484cSchristos 3098b9484cSchristos /* The following contortions are an attempt to use the C preprocessor 3198b9484cSchristos to determine an unsigned integral type that is 32 bits wide. An 3298b9484cSchristos alternative approach is to use autoconf's AC_CHECK_SIZEOF macro, but 3398b9484cSchristos doing that would require that the configure script compile and *run* 3498b9484cSchristos the resulting executable. Locally running cross-compiled executables 3598b9484cSchristos is usually not possible. */ 3698b9484cSchristos 3798b9484cSchristos #ifdef _LIBC 3898b9484cSchristos # include <sys/types.h> 3998b9484cSchristos typedef u_int32_t sha1_uint32; 4098b9484cSchristos typedef uintptr_t sha1_uintptr; 41a2e2270fSchristos #elif defined (HAVE_SYS_TYPES_H) && defined (HAVE_STDINT_H) 42a2e2270fSchristos #include <stdint.h> 43a2e2270fSchristos #include <sys/types.h> 44a2e2270fSchristos typedef uint32_t sha1_uint32; 45a2e2270fSchristos typedef uintptr_t sha1_uintptr; 4698b9484cSchristos #else 4798b9484cSchristos # define INT_MAX_32_BITS 2147483647 4898b9484cSchristos 4998b9484cSchristos /* If UINT_MAX isn't defined, assume it's a 32-bit type. 5098b9484cSchristos This should be valid for all systems GNU cares about because 5198b9484cSchristos that doesn't include 16-bit systems, and only modern systems 5298b9484cSchristos (that certainly have <limits.h>) have 64+-bit integral types. */ 5398b9484cSchristos 5498b9484cSchristos # ifndef INT_MAX 5598b9484cSchristos # define INT_MAX INT_MAX_32_BITS 5698b9484cSchristos # endif 5798b9484cSchristos 5898b9484cSchristos # if INT_MAX == INT_MAX_32_BITS 5998b9484cSchristos typedef unsigned int sha1_uint32; 6098b9484cSchristos # else 6198b9484cSchristos # if SHRT_MAX == INT_MAX_32_BITS 6298b9484cSchristos typedef unsigned short sha1_uint32; 6398b9484cSchristos # else 6498b9484cSchristos # if LONG_MAX == INT_MAX_32_BITS 6598b9484cSchristos typedef unsigned long sha1_uint32; 6698b9484cSchristos # else 6798b9484cSchristos /* The following line is intended to evoke an error. 6898b9484cSchristos Using #error is not portable enough. */ 6998b9484cSchristos "Cannot determine unsigned 32-bit data type." 7098b9484cSchristos # endif 7198b9484cSchristos # endif 7298b9484cSchristos # endif 7398b9484cSchristos #endif 7498b9484cSchristos 7598b9484cSchristos #ifdef __cplusplus 7698b9484cSchristos extern "C" { 7798b9484cSchristos #endif 7898b9484cSchristos 7998b9484cSchristos /* Structure to save state of computation between the single steps. */ 8098b9484cSchristos struct sha1_ctx 8198b9484cSchristos { 8298b9484cSchristos sha1_uint32 A; 8398b9484cSchristos sha1_uint32 B; 8498b9484cSchristos sha1_uint32 C; 8598b9484cSchristos sha1_uint32 D; 8698b9484cSchristos sha1_uint32 E; 8798b9484cSchristos 8898b9484cSchristos sha1_uint32 total[2]; 8998b9484cSchristos sha1_uint32 buflen; 9098b9484cSchristos sha1_uint32 buffer[32]; 9198b9484cSchristos }; 9298b9484cSchristos 9398b9484cSchristos 9498b9484cSchristos /* Initialize structure containing state of computation. */ 9598b9484cSchristos extern void sha1_init_ctx (struct sha1_ctx *ctx); 9698b9484cSchristos 9798b9484cSchristos /* Starting with the result of former calls of this function (or the 9898b9484cSchristos initialization function update the context for the next LEN bytes 9998b9484cSchristos starting at BUFFER. 10098b9484cSchristos It is necessary that LEN is a multiple of 64!!! */ 10198b9484cSchristos extern void sha1_process_block (const void *buffer, size_t len, 10298b9484cSchristos struct sha1_ctx *ctx); 10398b9484cSchristos 10498b9484cSchristos /* Starting with the result of former calls of this function (or the 10598b9484cSchristos initialization function update the context for the next LEN bytes 10698b9484cSchristos starting at BUFFER. 10798b9484cSchristos It is NOT required that LEN is a multiple of 64. */ 10898b9484cSchristos extern void sha1_process_bytes (const void *buffer, size_t len, 10998b9484cSchristos struct sha1_ctx *ctx); 11098b9484cSchristos 111*e663ba6eSchristos typedef void (*sha1_process_bytes_fn) (const void *, size_t, 112*e663ba6eSchristos struct sha1_ctx *); 113*e663ba6eSchristos 114*e663ba6eSchristos /* Return sha1_process_bytes or some hardware optimized version thereof 115*e663ba6eSchristos depending on current CPU. */ 116*e663ba6eSchristos extern sha1_process_bytes_fn sha1_choose_process_bytes (void); 117*e663ba6eSchristos 11898b9484cSchristos /* Process the remaining bytes in the buffer and put result from CTX 11998b9484cSchristos in first 20 bytes following RESBUF. The result is always in little 12098b9484cSchristos endian byte order, so that a byte-wise output yields to the wanted 12198b9484cSchristos ASCII representation of the message digest. 12298b9484cSchristos 12398b9484cSchristos IMPORTANT: On some systems it is required that RESBUF be correctly 12498b9484cSchristos aligned for a 32 bits value. */ 12598b9484cSchristos extern void *sha1_finish_ctx (struct sha1_ctx *ctx, void *resbuf); 12698b9484cSchristos 12798b9484cSchristos 12898b9484cSchristos /* Put result from CTX in first 20 bytes following RESBUF. The result is 12998b9484cSchristos always in little endian byte order, so that a byte-wise output yields 13098b9484cSchristos to the wanted ASCII representation of the message digest. 13198b9484cSchristos 13298b9484cSchristos IMPORTANT: On some systems it is required that RESBUF is correctly 13398b9484cSchristos aligned for a 32 bits value. */ 13498b9484cSchristos extern void *sha1_read_ctx (const struct sha1_ctx *ctx, void *resbuf); 13598b9484cSchristos 13698b9484cSchristos 13798b9484cSchristos /* Compute SHA1 message digest for bytes read from STREAM. The 13898b9484cSchristos resulting message digest number will be written into the 20 bytes 13998b9484cSchristos beginning at RESBLOCK. */ 14098b9484cSchristos extern int sha1_stream (FILE *stream, void *resblock); 14198b9484cSchristos 14298b9484cSchristos /* Compute SHA1 message digest for LEN bytes beginning at BUFFER. The 14398b9484cSchristos result is always in little endian byte order, so that a byte-wise 14498b9484cSchristos output yields to the wanted ASCII representation of the message 14598b9484cSchristos digest. */ 14698b9484cSchristos extern void *sha1_buffer (const char *buffer, size_t len, void *resblock); 14798b9484cSchristos 14898b9484cSchristos #ifdef __cplusplus 14998b9484cSchristos } 15098b9484cSchristos #endif 15198b9484cSchristos 15298b9484cSchristos #endif 153