xref: /netbsd-src/external/gpl3/binutils.old/dist/include/sha1.h (revision e992f068c547fd6e84b3f104dc2340adcc955732)
175fd0b74Schristos /* Declarations of functions and data types used for SHA1 sum
275fd0b74Schristos    library functions.
3*e992f068Schristos    Copyright (C) 2000-2022 Free Software Foundation, Inc.
475fd0b74Schristos 
575fd0b74Schristos    This program is free software; you can redistribute it and/or modify it
675fd0b74Schristos    under the terms of the GNU General Public License as published by the
775fd0b74Schristos    Free Software Foundation; either version 3, or (at your option) any
875fd0b74Schristos    later version.
975fd0b74Schristos 
1075fd0b74Schristos    This program is distributed in the hope that it will be useful,
1175fd0b74Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
1275fd0b74Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1375fd0b74Schristos    GNU General Public License for more details.
1475fd0b74Schristos 
1575fd0b74Schristos    You should have received a copy of the GNU General Public License
1675fd0b74Schristos    along with this program; if not, write to the Free Software Foundation,
1775fd0b74Schristos    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
1875fd0b74Schristos 
1975fd0b74Schristos #ifndef SHA1_H
2075fd0b74Schristos # define SHA1_H 1
2175fd0b74Schristos 
2275fd0b74Schristos #include <stdio.h>
2375fd0b74Schristos 
2475fd0b74Schristos #if defined HAVE_LIMITS_H || _LIBC
2575fd0b74Schristos # include <limits.h>
2675fd0b74Schristos #endif
2775fd0b74Schristos 
2875fd0b74Schristos #include "ansidecl.h"
2975fd0b74Schristos 
3075fd0b74Schristos /* The following contortions are an attempt to use the C preprocessor
3175fd0b74Schristos    to determine an unsigned integral type that is 32 bits wide.  An
3275fd0b74Schristos    alternative approach is to use autoconf's AC_CHECK_SIZEOF macro, but
3375fd0b74Schristos    doing that would require that the configure script compile and *run*
3475fd0b74Schristos    the resulting executable.  Locally running cross-compiled executables
3575fd0b74Schristos    is usually not possible.  */
3675fd0b74Schristos 
3775fd0b74Schristos #ifdef _LIBC
3875fd0b74Schristos # include <sys/types.h>
3975fd0b74Schristos typedef u_int32_t sha1_uint32;
4075fd0b74Schristos typedef uintptr_t sha1_uintptr;
4175fd0b74Schristos #elif defined (HAVE_SYS_TYPES_H) && defined (HAVE_STDINT_H)
4275fd0b74Schristos #include <stdint.h>
4375fd0b74Schristos #include <sys/types.h>
4475fd0b74Schristos typedef uint32_t sha1_uint32;
4575fd0b74Schristos typedef uintptr_t sha1_uintptr;
4675fd0b74Schristos #else
4775fd0b74Schristos #  define INT_MAX_32_BITS 2147483647
4875fd0b74Schristos 
4975fd0b74Schristos /* If UINT_MAX isn't defined, assume it's a 32-bit type.
5075fd0b74Schristos    This should be valid for all systems GNU cares about because
5175fd0b74Schristos    that doesn't include 16-bit systems, and only modern systems
5275fd0b74Schristos    (that certainly have <limits.h>) have 64+-bit integral types.  */
5375fd0b74Schristos 
5475fd0b74Schristos # ifndef INT_MAX
5575fd0b74Schristos #  define INT_MAX INT_MAX_32_BITS
5675fd0b74Schristos # endif
5775fd0b74Schristos 
5875fd0b74Schristos # if INT_MAX == INT_MAX_32_BITS
5975fd0b74Schristos    typedef unsigned int sha1_uint32;
6075fd0b74Schristos # else
6175fd0b74Schristos #  if SHRT_MAX == INT_MAX_32_BITS
6275fd0b74Schristos     typedef unsigned short sha1_uint32;
6375fd0b74Schristos #  else
6475fd0b74Schristos #   if LONG_MAX == INT_MAX_32_BITS
6575fd0b74Schristos      typedef unsigned long sha1_uint32;
6675fd0b74Schristos #   else
6775fd0b74Schristos      /* The following line is intended to evoke an error.
6875fd0b74Schristos         Using #error is not portable enough.  */
6975fd0b74Schristos      "Cannot determine unsigned 32-bit data type."
7075fd0b74Schristos #   endif
7175fd0b74Schristos #  endif
7275fd0b74Schristos # endif
7375fd0b74Schristos #endif
7475fd0b74Schristos 
7575fd0b74Schristos #ifdef __cplusplus
7675fd0b74Schristos extern "C" {
7775fd0b74Schristos #endif
7875fd0b74Schristos 
7975fd0b74Schristos /* Structure to save state of computation between the single steps.  */
8075fd0b74Schristos struct sha1_ctx
8175fd0b74Schristos {
8275fd0b74Schristos   sha1_uint32 A;
8375fd0b74Schristos   sha1_uint32 B;
8475fd0b74Schristos   sha1_uint32 C;
8575fd0b74Schristos   sha1_uint32 D;
8675fd0b74Schristos   sha1_uint32 E;
8775fd0b74Schristos 
8875fd0b74Schristos   sha1_uint32 total[2];
8975fd0b74Schristos   sha1_uint32 buflen;
9075fd0b74Schristos   sha1_uint32 buffer[32];
9175fd0b74Schristos };
9275fd0b74Schristos 
9375fd0b74Schristos 
9475fd0b74Schristos /* Initialize structure containing state of computation. */
9575fd0b74Schristos extern void sha1_init_ctx (struct sha1_ctx *ctx);
9675fd0b74Schristos 
9775fd0b74Schristos /* Starting with the result of former calls of this function (or the
9875fd0b74Schristos    initialization function update the context for the next LEN bytes
9975fd0b74Schristos    starting at BUFFER.
10075fd0b74Schristos    It is necessary that LEN is a multiple of 64!!! */
10175fd0b74Schristos extern void sha1_process_block (const void *buffer, size_t len,
10275fd0b74Schristos 				struct sha1_ctx *ctx);
10375fd0b74Schristos 
10475fd0b74Schristos /* Starting with the result of former calls of this function (or the
10575fd0b74Schristos    initialization function update the context for the next LEN bytes
10675fd0b74Schristos    starting at BUFFER.
10775fd0b74Schristos    It is NOT required that LEN is a multiple of 64.  */
10875fd0b74Schristos extern void sha1_process_bytes (const void *buffer, size_t len,
10975fd0b74Schristos 				struct sha1_ctx *ctx);
11075fd0b74Schristos 
11175fd0b74Schristos /* Process the remaining bytes in the buffer and put result from CTX
11275fd0b74Schristos    in first 20 bytes following RESBUF.  The result is always in little
11375fd0b74Schristos    endian byte order, so that a byte-wise output yields to the wanted
11475fd0b74Schristos    ASCII representation of the message digest.
11575fd0b74Schristos 
11675fd0b74Schristos    IMPORTANT: On some systems it is required that RESBUF be correctly
11775fd0b74Schristos    aligned for a 32 bits value.  */
11875fd0b74Schristos extern void *sha1_finish_ctx (struct sha1_ctx *ctx, void *resbuf);
11975fd0b74Schristos 
12075fd0b74Schristos 
12175fd0b74Schristos /* Put result from CTX in first 20 bytes following RESBUF.  The result is
12275fd0b74Schristos    always in little endian byte order, so that a byte-wise output yields
12375fd0b74Schristos    to the wanted ASCII representation of the message digest.
12475fd0b74Schristos 
12575fd0b74Schristos    IMPORTANT: On some systems it is required that RESBUF is correctly
12675fd0b74Schristos    aligned for a 32 bits value.  */
12775fd0b74Schristos extern void *sha1_read_ctx (const struct sha1_ctx *ctx, void *resbuf);
12875fd0b74Schristos 
12975fd0b74Schristos 
13075fd0b74Schristos /* Compute SHA1 message digest for bytes read from STREAM.  The
13175fd0b74Schristos    resulting message digest number will be written into the 20 bytes
13275fd0b74Schristos    beginning at RESBLOCK.  */
13375fd0b74Schristos extern int sha1_stream (FILE *stream, void *resblock);
13475fd0b74Schristos 
13575fd0b74Schristos /* Compute SHA1 message digest for LEN bytes beginning at BUFFER.  The
13675fd0b74Schristos    result is always in little endian byte order, so that a byte-wise
13775fd0b74Schristos    output yields to the wanted ASCII representation of the message
13875fd0b74Schristos    digest.  */
13975fd0b74Schristos extern void *sha1_buffer (const char *buffer, size_t len, void *resblock);
14075fd0b74Schristos 
14175fd0b74Schristos #ifdef __cplusplus
14275fd0b74Schristos }
14375fd0b74Schristos #endif
14475fd0b74Schristos 
14575fd0b74Schristos #endif
146