exp2l: Work around a NetBSD 10.0/i386 bug.
[gnulib.git] / lib / sha256.h
bloba9d7abb8a2cb6f26fb5bbda174d3edce085834bd
1 /* Declarations of functions and data types used for SHA256 and SHA224 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 SHA256_H
19 # define SHA256_H 1
21 /* This file uses HAVE_OPENSSL_SHA256. */
22 # if !_GL_CONFIG_H_INCLUDED
23 # error "Please include config.h first."
24 # endif
26 # include <stdio.h>
27 # include <stdint.h>
29 # if HAVE_OPENSSL_SHA256
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_SHA256
43 # endif
44 # endif
45 # if HAVE_OPENSSL_SHA256
46 # include <openssl/sha.h>
47 # endif
48 # endif
50 # ifdef __cplusplus
51 extern "C" {
52 # endif
54 enum { SHA224_DIGEST_SIZE = 224 / 8 };
55 enum { SHA256_DIGEST_SIZE = 256 / 8 };
57 # if HAVE_OPENSSL_SHA256
58 # define GL_OPENSSL_NAME 224
59 # include "gl_openssl.h"
60 # define GL_OPENSSL_NAME 256
61 # include "gl_openssl.h"
62 # else
63 /* Structure to save state of computation between the single steps. */
64 struct sha256_ctx
66 uint32_t state[8];
68 uint32_t total[2];
69 size_t buflen; /* ≥ 0, ≤ 128 */
70 uint32_t buffer[32]; /* 128 bytes; the first buflen bytes are in use */
73 /* Initialize structure containing state of computation. */
74 extern void sha256_init_ctx (struct sha256_ctx *ctx);
75 extern void sha224_init_ctx (struct sha256_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 64!!! */
81 extern void sha256_process_block (const void *buffer, size_t len,
82 struct sha256_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 64. */
88 extern void sha256_process_bytes (const void *buffer, size_t len,
89 struct sha256_ctx *ctx);
91 /* Process the remaining bytes in the buffer and put result from CTX
92 in first 32 (28) 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 *sha256_finish_ctx (struct sha256_ctx *ctx, void *restrict resbuf);
96 extern void *sha224_finish_ctx (struct sha256_ctx *ctx, void *restrict resbuf);
99 /* Put result from CTX in first 32 (28) 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. */
102 extern void *sha256_read_ctx (const struct sha256_ctx *ctx,
103 void *restrict resbuf);
104 extern void *sha224_read_ctx (const struct sha256_ctx *ctx,
105 void *restrict resbuf);
108 /* Compute SHA256 (SHA224) message digest for LEN bytes beginning at BUFFER.
109 The result is always in little endian byte order, so that a byte-wise
110 output yields to the wanted ASCII representation of the message
111 digest. */
112 extern void *sha256_buffer (const char *buffer, size_t len,
113 void *restrict resblock);
114 extern void *sha224_buffer (const char *buffer, size_t len,
115 void *restrict resblock);
117 # endif
119 /* Compute SHA256 (SHA224) message digest for bytes read from STREAM.
120 STREAM is an open file stream. Regular files are handled more efficiently.
121 The contents of STREAM from its current position to its end will be read.
122 The case that the last operation on STREAM was an 'ungetc' is not supported.
123 The resulting message digest number will be written into the 32 (28) bytes
124 beginning at RESBLOCK. */
125 extern int sha256_stream (FILE *stream, void *resblock);
126 extern int sha224_stream (FILE *stream, void *resblock);
129 # ifdef __cplusplus
131 # endif
133 #endif
136 * Hey Emacs!
137 * Local Variables:
138 * coding: utf-8
139 * End: