exp2l: Work around a NetBSD 10.0/i386 bug.
[gnulib.git] / lib / sha512.h
blobf6bac85488eea7a3deafd6737372ac9ba2ce64dd
1 /* Declarations of functions and data types used for SHA512 and SHA384 sum
2 library functions.
3 Copyright (C) 2005-2006, 2008-2024 Free Software Foundation, Inc.
5 This file is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation; either version 2.1 of the
8 License, or (at your option) any later version.
10 This file is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18 #ifndef SHA512_H
19 # define SHA512_H 1
21 /* This file uses HAVE_OPENSSL_SHA512. */
22 # if !_GL_CONFIG_H_INCLUDED
23 # error "Please include config.h first."
24 # endif
26 # include <stdio.h>
27 # include "u64.h"
29 # if HAVE_OPENSSL_SHA512
30 # ifndef OPENSSL_API_COMPAT
31 # define OPENSSL_API_COMPAT 0x10101000L /* FIXME: Use OpenSSL 1.1+ API. */
32 # endif
33 /* If <openssl/macros.h> would give a compile-time error, don't use OpenSSL. */
34 # include <openssl/opensslv.h>
35 # if OPENSSL_VERSION_MAJOR >= 3
36 # include <openssl/configuration.h>
37 # if (OPENSSL_CONFIGURED_API \
38 < (OPENSSL_API_COMPAT < 0x900000L ? OPENSSL_API_COMPAT : \
39 ((OPENSSL_API_COMPAT >> 28) & 0xF) * 10000 \
40 + ((OPENSSL_API_COMPAT >> 20) & 0xFF) * 100 \
41 + ((OPENSSL_API_COMPAT >> 12) & 0xFF)))
42 # undef HAVE_OPENSSL_SHA512
43 # endif
44 # endif
45 # if HAVE_OPENSSL_SHA512
46 # include <openssl/sha.h>
47 # endif
48 # endif
50 # ifdef __cplusplus
51 extern "C" {
52 # endif
54 enum { SHA384_DIGEST_SIZE = 384 / 8 };
55 enum { SHA512_DIGEST_SIZE = 512 / 8 };
57 # if HAVE_OPENSSL_SHA512
58 # define GL_OPENSSL_NAME 384
59 # include "gl_openssl.h"
60 # define GL_OPENSSL_NAME 512
61 # include "gl_openssl.h"
62 # else
63 /* Structure to save state of computation between the single steps. */
64 struct sha512_ctx
66 u64 state[8];
68 u64 total[2];
69 size_t buflen; /* ≥ 0, ≤ 256 */
70 u64 buffer[32]; /* 256 bytes; the first buflen bytes are in use */
73 /* Initialize structure containing state of computation. */
74 extern void sha512_init_ctx (struct sha512_ctx *ctx);
75 extern void sha384_init_ctx (struct sha512_ctx *ctx);
77 /* Starting with the result of former calls of this function (or the
78 initialization function update the context for the next LEN bytes
79 starting at BUFFER.
80 It is necessary that LEN is a multiple of 128!!! */
81 extern void sha512_process_block (const void *buffer, size_t len,
82 struct sha512_ctx *ctx);
84 /* Starting with the result of former calls of this function (or the
85 initialization function update the context for the next LEN bytes
86 starting at BUFFER.
87 It is NOT required that LEN is a multiple of 128. */
88 extern void sha512_process_bytes (const void *buffer, size_t len,
89 struct sha512_ctx *ctx);
91 /* Process the remaining bytes in the buffer and put result from CTX
92 in first 64 (48) bytes following RESBUF. The result is always in little
93 endian byte order, so that a byte-wise output yields to the wanted
94 ASCII representation of the message digest. */
95 extern void *sha512_finish_ctx (struct sha512_ctx *ctx, void *restrict resbuf);
96 extern void *sha384_finish_ctx (struct sha512_ctx *ctx, void *restrict resbuf);
99 /* Put result from CTX in first 64 (48) bytes following RESBUF. The result is
100 always in little endian byte order, so that a byte-wise output yields
101 to the wanted ASCII representation of the message digest.
103 IMPORTANT: On some systems it is required that RESBUF is correctly
104 aligned for a 32 bits value. */
105 extern void *sha512_read_ctx (const struct sha512_ctx *ctx,
106 void *restrict resbuf);
107 extern void *sha384_read_ctx (const struct sha512_ctx *ctx,
108 void *restrict resbuf);
111 /* Compute SHA512 (SHA384) message digest for LEN bytes beginning at BUFFER.
112 The result is always in little endian byte order, so that a byte-wise
113 output yields to the wanted ASCII representation of the message
114 digest. */
115 extern void *sha512_buffer (const char *buffer, size_t len,
116 void *restrict resblock);
117 extern void *sha384_buffer (const char *buffer, size_t len,
118 void *restrict resblock);
120 # endif
122 /* Compute SHA512 (SHA384) message digest for bytes read from STREAM.
123 STREAM is an open file stream. Regular files are handled more efficiently.
124 The contents of STREAM from its current position to its end will be read.
125 The case that the last operation on STREAM was an 'ungetc' is not supported.
126 The resulting message digest number will be written into the 64 (48) bytes
127 beginning at RESBLOCK. */
128 extern int sha512_stream (FILE *stream, void *resblock);
129 extern int sha384_stream (FILE *stream, void *resblock);
132 # ifdef __cplusplus
134 # endif
136 #endif
139 * Hey Emacs!
140 * Local Variables:
141 * coding: utf-8
142 * End: