xref: /netbsd-src/external/gpl3/gcc/dist/include/sha1.h (revision b1e838363e3c6fc78a55519254d99869742dd33c)
14fee23f9Smrg /* Declarations of functions and data types used for SHA1 sum
24fee23f9Smrg    library functions.
3*b1e83836Smrg    Copyright (C) 2000-2022 Free Software Foundation, Inc.
44fee23f9Smrg 
54fee23f9Smrg    This program is free software; you can redistribute it and/or modify it
64fee23f9Smrg    under the terms of the GNU General Public License as published by the
748fb7bfaSmrg    Free Software Foundation; either version 3, or (at your option) any
84fee23f9Smrg    later version.
94fee23f9Smrg 
104fee23f9Smrg    This program is distributed in the hope that it will be useful,
114fee23f9Smrg    but WITHOUT ANY WARRANTY; without even the implied warranty of
124fee23f9Smrg    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
134fee23f9Smrg    GNU General Public License for more details.
144fee23f9Smrg 
154fee23f9Smrg    You should have received a copy of the GNU General Public License
164fee23f9Smrg    along with this program; if not, write to the Free Software Foundation,
174fee23f9Smrg    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
184fee23f9Smrg 
194fee23f9Smrg #ifndef SHA1_H
204fee23f9Smrg # define SHA1_H 1
214fee23f9Smrg 
224fee23f9Smrg #include <stdio.h>
234fee23f9Smrg 
244fee23f9Smrg #if defined HAVE_LIMITS_H || _LIBC
254fee23f9Smrg # include <limits.h>
264fee23f9Smrg #endif
274fee23f9Smrg 
284fee23f9Smrg #include "ansidecl.h"
294fee23f9Smrg 
304fee23f9Smrg /* The following contortions are an attempt to use the C preprocessor
314fee23f9Smrg    to determine an unsigned integral type that is 32 bits wide.  An
324fee23f9Smrg    alternative approach is to use autoconf's AC_CHECK_SIZEOF macro, but
334fee23f9Smrg    doing that would require that the configure script compile and *run*
344fee23f9Smrg    the resulting executable.  Locally running cross-compiled executables
354fee23f9Smrg    is usually not possible.  */
364fee23f9Smrg 
374fee23f9Smrg #ifdef _LIBC
384fee23f9Smrg # include <sys/types.h>
394fee23f9Smrg typedef u_int32_t sha1_uint32;
404fee23f9Smrg typedef uintptr_t sha1_uintptr;
4148fb7bfaSmrg #elif defined (HAVE_SYS_TYPES_H) && defined (HAVE_STDINT_H)
4248fb7bfaSmrg #include <stdint.h>
4348fb7bfaSmrg #include <sys/types.h>
4448fb7bfaSmrg typedef uint32_t sha1_uint32;
4548fb7bfaSmrg typedef uintptr_t sha1_uintptr;
464fee23f9Smrg #else
474fee23f9Smrg #  define INT_MAX_32_BITS 2147483647
484fee23f9Smrg 
494fee23f9Smrg /* If UINT_MAX isn't defined, assume it's a 32-bit type.
504fee23f9Smrg    This should be valid for all systems GNU cares about because
514fee23f9Smrg    that doesn't include 16-bit systems, and only modern systems
524fee23f9Smrg    (that certainly have <limits.h>) have 64+-bit integral types.  */
534fee23f9Smrg 
544fee23f9Smrg # ifndef INT_MAX
554fee23f9Smrg #  define INT_MAX INT_MAX_32_BITS
564fee23f9Smrg # endif
574fee23f9Smrg 
584fee23f9Smrg # if INT_MAX == INT_MAX_32_BITS
594fee23f9Smrg    typedef unsigned int sha1_uint32;
604fee23f9Smrg # else
614fee23f9Smrg #  if SHRT_MAX == INT_MAX_32_BITS
624fee23f9Smrg     typedef unsigned short sha1_uint32;
634fee23f9Smrg #  else
644fee23f9Smrg #   if LONG_MAX == INT_MAX_32_BITS
654fee23f9Smrg      typedef unsigned long sha1_uint32;
664fee23f9Smrg #   else
674fee23f9Smrg      /* The following line is intended to evoke an error.
684fee23f9Smrg         Using #error is not portable enough.  */
694fee23f9Smrg      "Cannot determine unsigned 32-bit data type."
704fee23f9Smrg #   endif
714fee23f9Smrg #  endif
724fee23f9Smrg # endif
734fee23f9Smrg #endif
744fee23f9Smrg 
754fee23f9Smrg #ifdef __cplusplus
764fee23f9Smrg extern "C" {
774fee23f9Smrg #endif
784fee23f9Smrg 
794fee23f9Smrg /* Structure to save state of computation between the single steps.  */
804fee23f9Smrg struct sha1_ctx
814fee23f9Smrg {
824fee23f9Smrg   sha1_uint32 A;
834fee23f9Smrg   sha1_uint32 B;
844fee23f9Smrg   sha1_uint32 C;
854fee23f9Smrg   sha1_uint32 D;
864fee23f9Smrg   sha1_uint32 E;
874fee23f9Smrg 
884fee23f9Smrg   sha1_uint32 total[2];
894fee23f9Smrg   sha1_uint32 buflen;
904fee23f9Smrg   sha1_uint32 buffer[32];
914fee23f9Smrg };
924fee23f9Smrg 
934fee23f9Smrg 
944fee23f9Smrg /* Initialize structure containing state of computation. */
954fee23f9Smrg extern void sha1_init_ctx (struct sha1_ctx *ctx);
964fee23f9Smrg 
974fee23f9Smrg /* Starting with the result of former calls of this function (or the
984fee23f9Smrg    initialization function update the context for the next LEN bytes
994fee23f9Smrg    starting at BUFFER.
1004fee23f9Smrg    It is necessary that LEN is a multiple of 64!!! */
1014fee23f9Smrg extern void sha1_process_block (const void *buffer, size_t len,
1024fee23f9Smrg 				struct sha1_ctx *ctx);
1034fee23f9Smrg 
1044fee23f9Smrg /* Starting with the result of former calls of this function (or the
1054fee23f9Smrg    initialization function update the context for the next LEN bytes
1064fee23f9Smrg    starting at BUFFER.
1074fee23f9Smrg    It is NOT required that LEN is a multiple of 64.  */
1084fee23f9Smrg extern void sha1_process_bytes (const void *buffer, size_t len,
1094fee23f9Smrg 				struct sha1_ctx *ctx);
1104fee23f9Smrg 
1114fee23f9Smrg /* Process the remaining bytes in the buffer and put result from CTX
1124fee23f9Smrg    in first 20 bytes following RESBUF.  The result is always in little
1134fee23f9Smrg    endian byte order, so that a byte-wise output yields to the wanted
1144fee23f9Smrg    ASCII representation of the message digest.
1154fee23f9Smrg 
1164fee23f9Smrg    IMPORTANT: On some systems it is required that RESBUF be correctly
1174fee23f9Smrg    aligned for a 32 bits value.  */
1184fee23f9Smrg extern void *sha1_finish_ctx (struct sha1_ctx *ctx, void *resbuf);
1194fee23f9Smrg 
1204fee23f9Smrg 
1214fee23f9Smrg /* Put result from CTX in first 20 bytes following RESBUF.  The result is
1224fee23f9Smrg    always in little endian byte order, so that a byte-wise output yields
1234fee23f9Smrg    to the wanted ASCII representation of the message digest.
1244fee23f9Smrg 
1254fee23f9Smrg    IMPORTANT: On some systems it is required that RESBUF is correctly
1264fee23f9Smrg    aligned for a 32 bits value.  */
1274fee23f9Smrg extern void *sha1_read_ctx (const struct sha1_ctx *ctx, void *resbuf);
1284fee23f9Smrg 
1294fee23f9Smrg 
1304fee23f9Smrg /* Compute SHA1 message digest for bytes read from STREAM.  The
1314fee23f9Smrg    resulting message digest number will be written into the 20 bytes
1324fee23f9Smrg    beginning at RESBLOCK.  */
1334fee23f9Smrg extern int sha1_stream (FILE *stream, void *resblock);
1344fee23f9Smrg 
1354fee23f9Smrg /* Compute SHA1 message digest for LEN bytes beginning at BUFFER.  The
1364fee23f9Smrg    result is always in little endian byte order, so that a byte-wise
1374fee23f9Smrg    output yields to the wanted ASCII representation of the message
1384fee23f9Smrg    digest.  */
1394fee23f9Smrg extern void *sha1_buffer (const char *buffer, size_t len, void *resblock);
1404fee23f9Smrg 
1414fee23f9Smrg #ifdef __cplusplus
1424fee23f9Smrg }
1434fee23f9Smrg #endif
1444fee23f9Smrg 
1454fee23f9Smrg #endif
146