1 /* Declarations of functions and data types used for SHA512 and SHA384 sum
2 library functions.
3 Copyright (C) 2005-2006, 2008-2023 Free Software Foundation, Inc.
4
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.
9
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.
14
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/>. */
17
18 #ifndef SHA512_H
19 # define SHA512_H 1
20
21 /* This file uses HAVE_OPENSSL_SHA512. */
22 # if !_GL_CONFIG_H_INCLUDED
23 # error "Please include config.h first."
24 # endif
25
26 # include <stdio.h>
27 # include "u64.h"
28
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 # include <openssl/sha.h>
34 # endif
35
36 # ifdef __cplusplus
37 extern "C" {
38 # endif
39
40 enum { SHA384_DIGEST_SIZE = 384 / 8 };
41 enum { SHA512_DIGEST_SIZE = 512 / 8 };
42
43 # if HAVE_OPENSSL_SHA512
44 # define GL_OPENSSL_NAME 384
45 # include "gl_openssl.h"
46 # define GL_OPENSSL_NAME 512
47 # include "gl_openssl.h"
48 # else
49 /* Structure to save state of computation between the single steps. */
50 struct sha512_ctx
51 {
52 u64 state[8];
53
54 u64 total[2];
55 size_t buflen; /* ≥ 0, ≤ 256 */
56 u64 buffer[32]; /* 256 bytes; the first buflen bytes are in use */
57 };
58
59 /* Initialize structure containing state of computation. */
60 extern void sha512_init_ctx (struct sha512_ctx *ctx);
61 extern void sha384_init_ctx (struct sha512_ctx *ctx);
62
63 /* Starting with the result of former calls of this function (or the
64 initialization function update the context for the next LEN bytes
65 starting at BUFFER.
66 It is necessary that LEN is a multiple of 128!!! */
67 extern void sha512_process_block (const void *buffer, size_t len,
68 struct sha512_ctx *ctx);
69
70 /* Starting with the result of former calls of this function (or the
71 initialization function update the context for the next LEN bytes
72 starting at BUFFER.
73 It is NOT required that LEN is a multiple of 128. */
74 extern void sha512_process_bytes (const void *buffer, size_t len,
75 struct sha512_ctx *ctx);
76
77 /* Process the remaining bytes in the buffer and put result from CTX
78 in first 64 (48) bytes following RESBUF. The result is always in little
79 endian byte order, so that a byte-wise output yields to the wanted
80 ASCII representation of the message digest. */
81 extern void *sha512_finish_ctx (struct sha512_ctx *ctx, void *restrict resbuf);
82 extern void *sha384_finish_ctx (struct sha512_ctx *ctx, void *restrict resbuf);
83
84
85 /* Put result from CTX in first 64 (48) bytes following RESBUF. The result is
86 always in little endian byte order, so that a byte-wise output yields
87 to the wanted ASCII representation of the message digest.
88
89 IMPORTANT: On some systems it is required that RESBUF is correctly
90 aligned for a 32 bits value. */
91 extern void *sha512_read_ctx (const struct sha512_ctx *ctx,
92 void *restrict resbuf);
93 extern void *sha384_read_ctx (const struct sha512_ctx *ctx,
94 void *restrict resbuf);
95
96
97 /* Compute SHA512 (SHA384) message digest for LEN bytes beginning at BUFFER.
98 The result is always in little endian byte order, so that a byte-wise
99 output yields to the wanted ASCII representation of the message
100 digest. */
101 extern void *sha512_buffer (const char *buffer, size_t len,
102 void *restrict resblock);
103 extern void *sha384_buffer (const char *buffer, size_t len,
104 void *restrict resblock);
105
106 # endif
107
108 /* Compute SHA512 (SHA384) message digest for bytes read from STREAM.
109 STREAM is an open file stream. Regular files are handled more efficiently.
110 The contents of STREAM from its current position to its end will be read.
111 The case that the last operation on STREAM was an 'ungetc' is not supported.
112 The resulting message digest number will be written into the 64 (48) bytes
113 beginning at RESBLOCK. */
114 extern int sha512_stream (FILE *stream, void *resblock);
115 extern int sha384_stream (FILE *stream, void *resblock);
116
117
118 # ifdef __cplusplus
119 }
120 # endif
121
122 #endif
123
124 /*
125 * Hey Emacs!
126 * Local Variables:
127 * coding: utf-8
128 * End:
129 */