1 /* Declarations of functions and data types used for SHA1 sum
2 library functions.
3 Copyright (C) 2000-2001, 2003, 2005-2006, 2008-2023 Free Software
4 Foundation, Inc.
5
6 This file is free software: you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
10
11 This file is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18
19 #ifndef SHA1_H
20 # define SHA1_H 1
21
22 /* This file uses HAVE_OPENSSL_SHA1. */
23 # if !_GL_CONFIG_H_INCLUDED
24 # error "Please include config.h first."
25 # endif
26
27 # include <stdio.h>
28 # include <stdint.h>
29
30 # if HAVE_OPENSSL_SHA1
31 # ifndef OPENSSL_API_COMPAT
32 # define OPENSSL_API_COMPAT 0x10101000L /* FIXME: Use OpenSSL 1.1+ API. */
33 # endif
34 /* If <openssl/macros.h> would give a compile-time error, don't use OpenSSL. */
35 # include <openssl/configuration.h>
36 # if (OPENSSL_CONFIGURED_API \
37 < (OPENSSL_API_COMPAT < 0x900000L ? OPENSSL_API_COMPAT : \
38 ((OPENSSL_API_COMPAT >> 28) & 0xF) * 10000 \
39 + ((OPENSSL_API_COMPAT >> 20) & 0xFF) * 100 \
40 + ((OPENSSL_API_COMPAT >> 12) & 0xFF)))
41 # undef HAVE_OPENSSL_SHA1
42 # else
43 # include <openssl/sha.h>
44 # endif
45 # endif
46
47 # ifdef __cplusplus
48 extern "C" {
49 # endif
50
51 # define SHA1_DIGEST_SIZE 20
52
53 # if HAVE_OPENSSL_SHA1
54 # define GL_OPENSSL_NAME 1
55 # include "gl_openssl.h"
56 # else
57 /* Structure to save state of computation between the single steps. */
58 struct sha1_ctx
59 {
60 uint32_t A;
61 uint32_t B;
62 uint32_t C;
63 uint32_t D;
64 uint32_t E;
65
66 uint32_t total[2];
67 uint32_t buflen; /* ≥ 0, ≤ 128 */
68 uint32_t buffer[32]; /* 128 bytes; the first buflen bytes are in use */
69 };
70
71 /* Initialize structure containing state of computation. */
72 extern void sha1_init_ctx (struct sha1_ctx *ctx);
73
74 /* Starting with the result of former calls of this function (or the
75 initialization function update the context for the next LEN bytes
76 starting at BUFFER.
77 It is necessary that LEN is a multiple of 64!!! */
78 extern void sha1_process_block (const void *buffer, size_t len,
79 struct sha1_ctx *ctx);
80
81 /* Starting with the result of former calls of this function (or the
82 initialization function update the context for the next LEN bytes
83 starting at BUFFER.
84 It is NOT required that LEN is a multiple of 64. */
85 extern void sha1_process_bytes (const void *buffer, size_t len,
86 struct sha1_ctx *ctx);
87
88 /* Process the remaining bytes in the buffer and put result from CTX
89 in first 20 bytes following RESBUF. The result is always in little
90 endian byte order, so that a byte-wise output yields to the wanted
91 ASCII representation of the message digest. */
92 extern void *sha1_finish_ctx (struct sha1_ctx *ctx, void *restrict resbuf);
93
94
95 /* Put result from CTX in first 20 bytes following RESBUF. The result is
96 always in little endian byte order, so that a byte-wise output yields
97 to the wanted ASCII representation of the message digest. */
98 extern void *sha1_read_ctx (const struct sha1_ctx *ctx, void *restrict resbuf);
99
100
101 /* Compute SHA1 message digest for LEN bytes beginning at BUFFER. The
102 result is always in little endian byte order, so that a byte-wise
103 output yields to the wanted ASCII representation of the message
104 digest. */
105 extern void *sha1_buffer (const char *buffer, size_t len,
106 void *restrict resblock);
107
108 # endif
109
110 /* Compute SHA1 message digest for bytes read from STREAM.
111 STREAM is an open file stream. Regular files are handled more efficiently.
112 The contents of STREAM from its current position to its end will be read.
113 The case that the last operation on STREAM was an 'ungetc' is not supported.
114 The resulting message digest number will be written into the 20 bytes
115 beginning at RESBLOCK. */
116 extern int sha1_stream (FILE *stream, void *resblock);
117
118
119 # ifdef __cplusplus
120 }
121 # endif
122
123 #endif
124
125 /*
126 * Hey Emacs!
127 * Local Variables:
128 * coding: utf-8
129 * End:
130 */