xref: /netbsd-src/external/gpl3/binutils/dist/include/sha1.h (revision cb63e24e8d6aae7ddac1859a9015f48b1d8bd90e)
12a6b7db3Sskrll /* Declarations of functions and data types used for SHA1 sum
22a6b7db3Sskrll    library functions.
3*cb63e24eSchristos    Copyright (C) 2000-2024 Free Software Foundation, Inc.
42a6b7db3Sskrll 
52a6b7db3Sskrll    This program is free software; you can redistribute it and/or modify it
62a6b7db3Sskrll    under the terms of the GNU General Public License as published by the
745548106Schristos    Free Software Foundation; either version 3, or (at your option) any
82a6b7db3Sskrll    later version.
92a6b7db3Sskrll 
102a6b7db3Sskrll    This program is distributed in the hope that it will be useful,
112a6b7db3Sskrll    but WITHOUT ANY WARRANTY; without even the implied warranty of
122a6b7db3Sskrll    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
132a6b7db3Sskrll    GNU General Public License for more details.
142a6b7db3Sskrll 
152a6b7db3Sskrll    You should have received a copy of the GNU General Public License
162a6b7db3Sskrll    along with this program; if not, write to the Free Software Foundation,
172a6b7db3Sskrll    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
182a6b7db3Sskrll 
192a6b7db3Sskrll #ifndef SHA1_H
202a6b7db3Sskrll # define SHA1_H 1
212a6b7db3Sskrll 
222a6b7db3Sskrll #include <stdio.h>
232a6b7db3Sskrll 
242a6b7db3Sskrll #if defined HAVE_LIMITS_H || _LIBC
252a6b7db3Sskrll # include <limits.h>
262a6b7db3Sskrll #endif
272a6b7db3Sskrll 
282a6b7db3Sskrll #include "ansidecl.h"
292a6b7db3Sskrll 
302a6b7db3Sskrll /* The following contortions are an attempt to use the C preprocessor
312a6b7db3Sskrll    to determine an unsigned integral type that is 32 bits wide.  An
322a6b7db3Sskrll    alternative approach is to use autoconf's AC_CHECK_SIZEOF macro, but
332a6b7db3Sskrll    doing that would require that the configure script compile and *run*
342a6b7db3Sskrll    the resulting executable.  Locally running cross-compiled executables
352a6b7db3Sskrll    is usually not possible.  */
362a6b7db3Sskrll 
372a6b7db3Sskrll #ifdef _LIBC
382a6b7db3Sskrll # include <sys/types.h>
392a6b7db3Sskrll typedef u_int32_t sha1_uint32;
402a6b7db3Sskrll typedef uintptr_t sha1_uintptr;
419573673dSchristos #elif defined (HAVE_SYS_TYPES_H) && defined (HAVE_STDINT_H)
429573673dSchristos #include <stdint.h>
439573673dSchristos #include <sys/types.h>
449573673dSchristos typedef uint32_t sha1_uint32;
459573673dSchristos typedef uintptr_t sha1_uintptr;
462a6b7db3Sskrll #else
472a6b7db3Sskrll #  define INT_MAX_32_BITS 2147483647
482a6b7db3Sskrll 
492a6b7db3Sskrll /* If UINT_MAX isn't defined, assume it's a 32-bit type.
502a6b7db3Sskrll    This should be valid for all systems GNU cares about because
512a6b7db3Sskrll    that doesn't include 16-bit systems, and only modern systems
522a6b7db3Sskrll    (that certainly have <limits.h>) have 64+-bit integral types.  */
532a6b7db3Sskrll 
542a6b7db3Sskrll # ifndef INT_MAX
552a6b7db3Sskrll #  define INT_MAX INT_MAX_32_BITS
562a6b7db3Sskrll # endif
572a6b7db3Sskrll 
582a6b7db3Sskrll # if INT_MAX == INT_MAX_32_BITS
592a6b7db3Sskrll    typedef unsigned int sha1_uint32;
602a6b7db3Sskrll # else
612a6b7db3Sskrll #  if SHRT_MAX == INT_MAX_32_BITS
622a6b7db3Sskrll     typedef unsigned short sha1_uint32;
632a6b7db3Sskrll #  else
642a6b7db3Sskrll #   if LONG_MAX == INT_MAX_32_BITS
652a6b7db3Sskrll      typedef unsigned long sha1_uint32;
662a6b7db3Sskrll #   else
672a6b7db3Sskrll      /* The following line is intended to evoke an error.
682a6b7db3Sskrll         Using #error is not portable enough.  */
692a6b7db3Sskrll      "Cannot determine unsigned 32-bit data type."
702a6b7db3Sskrll #   endif
712a6b7db3Sskrll #  endif
722a6b7db3Sskrll # endif
732a6b7db3Sskrll #endif
742a6b7db3Sskrll 
752a6b7db3Sskrll #ifdef __cplusplus
762a6b7db3Sskrll extern "C" {
772a6b7db3Sskrll #endif
782a6b7db3Sskrll 
792a6b7db3Sskrll /* Structure to save state of computation between the single steps.  */
802a6b7db3Sskrll struct sha1_ctx
812a6b7db3Sskrll {
822a6b7db3Sskrll   sha1_uint32 A;
832a6b7db3Sskrll   sha1_uint32 B;
842a6b7db3Sskrll   sha1_uint32 C;
852a6b7db3Sskrll   sha1_uint32 D;
862a6b7db3Sskrll   sha1_uint32 E;
872a6b7db3Sskrll 
882a6b7db3Sskrll   sha1_uint32 total[2];
892a6b7db3Sskrll   sha1_uint32 buflen;
902a6b7db3Sskrll   sha1_uint32 buffer[32];
912a6b7db3Sskrll };
922a6b7db3Sskrll 
932a6b7db3Sskrll 
942a6b7db3Sskrll /* Initialize structure containing state of computation. */
952a6b7db3Sskrll extern void sha1_init_ctx (struct sha1_ctx *ctx);
962a6b7db3Sskrll 
972a6b7db3Sskrll /* Starting with the result of former calls of this function (or the
982a6b7db3Sskrll    initialization function update the context for the next LEN bytes
992a6b7db3Sskrll    starting at BUFFER.
1002a6b7db3Sskrll    It is necessary that LEN is a multiple of 64!!! */
1012a6b7db3Sskrll extern void sha1_process_block (const void *buffer, size_t len,
1022a6b7db3Sskrll 				struct sha1_ctx *ctx);
1032a6b7db3Sskrll 
1042a6b7db3Sskrll /* Starting with the result of former calls of this function (or the
1052a6b7db3Sskrll    initialization function update the context for the next LEN bytes
1062a6b7db3Sskrll    starting at BUFFER.
1072a6b7db3Sskrll    It is NOT required that LEN is a multiple of 64.  */
1082a6b7db3Sskrll extern void sha1_process_bytes (const void *buffer, size_t len,
1092a6b7db3Sskrll 				struct sha1_ctx *ctx);
1102a6b7db3Sskrll 
111*cb63e24eSchristos typedef void (*sha1_process_bytes_fn) (const void *, size_t,
112*cb63e24eSchristos 				       struct sha1_ctx *);
113*cb63e24eSchristos 
114*cb63e24eSchristos /* Return sha1_process_bytes or some hardware optimized version thereof
115*cb63e24eSchristos    depending on current CPU.  */
116*cb63e24eSchristos extern sha1_process_bytes_fn sha1_choose_process_bytes (void);
117*cb63e24eSchristos 
1182a6b7db3Sskrll /* Process the remaining bytes in the buffer and put result from CTX
1192a6b7db3Sskrll    in first 20 bytes following RESBUF.  The result is always in little
1202a6b7db3Sskrll    endian byte order, so that a byte-wise output yields to the wanted
1212a6b7db3Sskrll    ASCII representation of the message digest.
1222a6b7db3Sskrll 
1232a6b7db3Sskrll    IMPORTANT: On some systems it is required that RESBUF be correctly
1242a6b7db3Sskrll    aligned for a 32 bits value.  */
1252a6b7db3Sskrll extern void *sha1_finish_ctx (struct sha1_ctx *ctx, void *resbuf);
1262a6b7db3Sskrll 
1272a6b7db3Sskrll 
1282a6b7db3Sskrll /* Put result from CTX in first 20 bytes following RESBUF.  The result is
1292a6b7db3Sskrll    always in little endian byte order, so that a byte-wise output yields
1302a6b7db3Sskrll    to the wanted ASCII representation of the message digest.
1312a6b7db3Sskrll 
1322a6b7db3Sskrll    IMPORTANT: On some systems it is required that RESBUF is correctly
1332a6b7db3Sskrll    aligned for a 32 bits value.  */
1342a6b7db3Sskrll extern void *sha1_read_ctx (const struct sha1_ctx *ctx, void *resbuf);
1352a6b7db3Sskrll 
1362a6b7db3Sskrll 
1372a6b7db3Sskrll /* Compute SHA1 message digest for bytes read from STREAM.  The
1382a6b7db3Sskrll    resulting message digest number will be written into the 20 bytes
1392a6b7db3Sskrll    beginning at RESBLOCK.  */
1402a6b7db3Sskrll extern int sha1_stream (FILE *stream, void *resblock);
1412a6b7db3Sskrll 
1422a6b7db3Sskrll /* Compute SHA1 message digest for LEN bytes beginning at BUFFER.  The
1432a6b7db3Sskrll    result is always in little endian byte order, so that a byte-wise
1442a6b7db3Sskrll    output yields to the wanted ASCII representation of the message
1452a6b7db3Sskrll    digest.  */
1462a6b7db3Sskrll extern void *sha1_buffer (const char *buffer, size_t len, void *resblock);
1472a6b7db3Sskrll 
1482a6b7db3Sskrll #ifdef __cplusplus
1492a6b7db3Sskrll }
1502a6b7db3Sskrll #endif
1512a6b7db3Sskrll 
1522a6b7db3Sskrll #endif
153