execute, spawn-pipe: Make multithread-safe on native Windows.
[gnulib.git] / lib / sha256.h
blob750d78a269652abade752c966d50cf98ed8ff76c
1 /* Declarations of functions and data types used for SHA256 and SHA224 sum
2 library functions.
3 Copyright (C) 2005-2006, 2008-2020 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program 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 General Public License for more details.
15 You should have received a copy of the GNU 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 # include <stdio.h>
22 # include <stdint.h>
24 # if HAVE_OPENSSL_SHA256
25 # include <openssl/sha.h>
26 # endif
28 # ifdef __cplusplus
29 extern "C" {
30 # endif
32 enum { SHA224_DIGEST_SIZE = 224 / 8 };
33 enum { SHA256_DIGEST_SIZE = 256 / 8 };
35 # if HAVE_OPENSSL_SHA256
36 # define GL_OPENSSL_NAME 224
37 # include "gl_openssl.h"
38 # define GL_OPENSSL_NAME 256
39 # include "gl_openssl.h"
40 # else
41 /* Structure to save state of computation between the single steps. */
42 struct sha256_ctx
44 uint32_t state[8];
46 uint32_t total[2];
47 size_t buflen; /* ≥ 0, ≤ 128 */
48 uint32_t buffer[32]; /* 128 bytes; the first buflen bytes are in use */
51 /* Initialize structure containing state of computation. */
52 extern void sha256_init_ctx (struct sha256_ctx *ctx);
53 extern void sha224_init_ctx (struct sha256_ctx *ctx);
55 /* Starting with the result of former calls of this function (or the
56 initialization function update the context for the next LEN bytes
57 starting at BUFFER.
58 It is necessary that LEN is a multiple of 64!!! */
59 extern void sha256_process_block (const void *buffer, size_t len,
60 struct sha256_ctx *ctx);
62 /* Starting with the result of former calls of this function (or the
63 initialization function update the context for the next LEN bytes
64 starting at BUFFER.
65 It is NOT required that LEN is a multiple of 64. */
66 extern void sha256_process_bytes (const void *buffer, size_t len,
67 struct sha256_ctx *ctx);
69 /* Process the remaining bytes in the buffer and put result from CTX
70 in first 32 (28) bytes following RESBUF. The result is always in little
71 endian byte order, so that a byte-wise output yields to the wanted
72 ASCII representation of the message digest. */
73 extern void *sha256_finish_ctx (struct sha256_ctx *ctx, void *restrict resbuf);
74 extern void *sha224_finish_ctx (struct sha256_ctx *ctx, void *restrict resbuf);
77 /* Put result from CTX in first 32 (28) bytes following RESBUF. The result is
78 always in little endian byte order, so that a byte-wise output yields
79 to the wanted ASCII representation of the message digest. */
80 extern void *sha256_read_ctx (const struct sha256_ctx *ctx,
81 void *restrict resbuf);
82 extern void *sha224_read_ctx (const struct sha256_ctx *ctx,
83 void *restrict resbuf);
86 /* Compute SHA256 (SHA224) message digest for LEN bytes beginning at BUFFER.
87 The result is always in little endian byte order, so that a byte-wise
88 output yields to the wanted ASCII representation of the message
89 digest. */
90 extern void *sha256_buffer (const char *buffer, size_t len,
91 void *restrict resblock);
92 extern void *sha224_buffer (const char *buffer, size_t len,
93 void *restrict resblock);
95 # endif
96 /* Compute SHA256 (SHA224) message digest for bytes read from STREAM.
97 STREAM is an open file stream. Regular files are handled more efficiently.
98 The contents of STREAM from its current position to its end will be read.
99 The case that the last operation on STREAM was an 'ungetc' is not supported.
100 The resulting message digest number will be written into the 32 (28) bytes
101 beginning at RESBLOCK. */
102 extern int sha256_stream (FILE *stream, void *resblock);
103 extern int sha224_stream (FILE *stream, void *resblock);
106 # ifdef __cplusplus
108 # endif
110 #endif
113 * Hey Emacs!
114 * Local Variables:
115 * coding: utf-8
116 * End: