1 /* Declarations of functions and data types used for SHA256 and SHA224 sum
3 Copyright (C) 2005-2006, 2008-2023 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/>. */
24 # if HAVE_OPENSSL_SHA256
25 # ifndef OPENSSL_API_COMPAT
26 # define OPENSSL_API_COMPAT 0x10101000L /* FIXME: Use OpenSSL 1.1+ API. */
28 # include <openssl/sha.h>
35 enum { SHA224_DIGEST_SIZE
= 224 / 8 };
36 enum { SHA256_DIGEST_SIZE
= 256 / 8 };
38 # if HAVE_OPENSSL_SHA256
39 # define GL_OPENSSL_NAME 224
40 # include "gl_openssl.h"
41 # define GL_OPENSSL_NAME 256
42 # include "gl_openssl.h"
44 /* Structure to save state of computation between the single steps. */
50 size_t buflen
; /* ≥ 0, ≤ 128 */
51 uint32_t buffer
[32]; /* 128 bytes; the first buflen bytes are in use */
54 /* Initialize structure containing state of computation. */
55 extern void sha256_init_ctx (struct sha256_ctx
*ctx
);
56 extern void sha224_init_ctx (struct sha256_ctx
*ctx
);
58 /* Starting with the result of former calls of this function (or the
59 initialization function update the context for the next LEN bytes
61 It is necessary that LEN is a multiple of 64!!! */
62 extern void sha256_process_block (const void *buffer
, size_t len
,
63 struct sha256_ctx
*ctx
);
65 /* Starting with the result of former calls of this function (or the
66 initialization function update the context for the next LEN bytes
68 It is NOT required that LEN is a multiple of 64. */
69 extern void sha256_process_bytes (const void *buffer
, size_t len
,
70 struct sha256_ctx
*ctx
);
72 /* Process the remaining bytes in the buffer and put result from CTX
73 in first 32 (28) bytes following RESBUF. The result is always in little
74 endian byte order, so that a byte-wise output yields to the wanted
75 ASCII representation of the message digest. */
76 extern void *sha256_finish_ctx (struct sha256_ctx
*ctx
, void *restrict resbuf
);
77 extern void *sha224_finish_ctx (struct sha256_ctx
*ctx
, void *restrict resbuf
);
80 /* Put result from CTX in first 32 (28) bytes following RESBUF. The result is
81 always in little endian byte order, so that a byte-wise output yields
82 to the wanted ASCII representation of the message digest. */
83 extern void *sha256_read_ctx (const struct sha256_ctx
*ctx
,
84 void *restrict resbuf
);
85 extern void *sha224_read_ctx (const struct sha256_ctx
*ctx
,
86 void *restrict resbuf
);
89 /* Compute SHA256 (SHA224) message digest for LEN bytes beginning at BUFFER.
90 The result is always in little endian byte order, so that a byte-wise
91 output yields to the wanted ASCII representation of the message
93 extern void *sha256_buffer (const char *buffer
, size_t len
,
94 void *restrict resblock
);
95 extern void *sha224_buffer (const char *buffer
, size_t len
,
96 void *restrict resblock
);
100 /* Compute SHA256 (SHA224) message digest for bytes read from STREAM.
101 STREAM is an open file stream. Regular files are handled more efficiently.
102 The contents of STREAM from its current position to its end will be read.
103 The case that the last operation on STREAM was an 'ungetc' is not supported.
104 The resulting message digest number will be written into the 32 (28) bytes
105 beginning at RESBLOCK. */
106 extern int sha256_stream (FILE *stream
, void *resblock
);
107 extern int sha224_stream (FILE *stream
, void *resblock
);