Adjust check_swap tests
[monitoring-plugins.git] / gl / sha256-stream.c
blob0e83380aa74233bed50f63da80f39418ab873381
1 /* sha256.c - Functions to compute SHA256 and SHA224 message digest of files or
2 memory blocks according to the NIST specification FIPS-180-2.
4 Copyright (C) 2005-2006, 2008-2023 Free Software Foundation, Inc.
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.
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.
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/>. */
19 /* Written by David Madore, considerably copypasting from
20 Scott G. Miller's sha1.c
23 #include <config.h>
25 /* Specification. */
26 #if HAVE_OPENSSL_SHA256
27 # define GL_OPENSSL_INLINE _GL_EXTERN_INLINE
28 #endif
29 #include "sha256.h"
31 #include <stdlib.h>
33 #if USE_UNLOCKED_IO
34 # include "unlocked-io.h"
35 #endif
37 #include "af_alg.h"
39 #define BLOCKSIZE 32768
40 #if BLOCKSIZE % 64 != 0
41 # error "invalid BLOCKSIZE"
42 #endif
44 /* Compute message digest for bytes read from STREAM using algorithm ALG.
45 Write the message digest into RESBLOCK, which contains HASHLEN bytes.
46 The initial and finishing operations are INIT_CTX and FINISH_CTX.
47 Return zero if and only if successful. */
48 static int
49 shaxxx_stream (FILE *stream, char const *alg, void *resblock,
50 ssize_t hashlen, void (*init_ctx) (struct sha256_ctx *),
51 void *(*finish_ctx) (struct sha256_ctx *, void *))
53 switch (afalg_stream (stream, alg, resblock, hashlen))
55 case 0: return 0;
56 case -EIO: return 1;
59 char *buffer = malloc (BLOCKSIZE + 72);
60 if (!buffer)
61 return 1;
63 struct sha256_ctx ctx;
64 init_ctx (&ctx);
65 size_t sum;
67 /* Iterate over full file contents. */
68 while (1)
70 /* We read the file in blocks of BLOCKSIZE bytes. One call of the
71 computation function processes the whole buffer so that with the
72 next round of the loop another block can be read. */
73 size_t n;
74 sum = 0;
76 /* Read block. Take care for partial reads. */
77 while (1)
79 /* Either process a partial fread() from this loop,
80 or the fread() in afalg_stream may have gotten EOF.
81 We need to avoid a subsequent fread() as EOF may
82 not be sticky. For details of such systems, see:
83 https://sourceware.org/bugzilla/show_bug.cgi?id=1190 */
84 if (feof (stream))
85 goto process_partial_block;
87 n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream);
89 sum += n;
91 if (sum == BLOCKSIZE)
92 break;
94 if (n == 0)
96 /* Check for the error flag IFF N == 0, so that we don't
97 exit the loop after a partial read due to e.g., EAGAIN
98 or EWOULDBLOCK. */
99 if (ferror (stream))
101 free (buffer);
102 return 1;
104 goto process_partial_block;
108 /* Process buffer with BLOCKSIZE bytes. Note that
109 BLOCKSIZE % 64 == 0
111 sha256_process_block (buffer, BLOCKSIZE, &ctx);
114 process_partial_block:;
116 /* Process any remaining bytes. */
117 if (sum > 0)
118 sha256_process_bytes (buffer, sum, &ctx);
120 /* Construct result in desired memory. */
121 finish_ctx (&ctx, resblock);
122 free (buffer);
123 return 0;
127 sha256_stream (FILE *stream, void *resblock)
129 return shaxxx_stream (stream, "sha256", resblock, SHA256_DIGEST_SIZE,
130 sha256_init_ctx, sha256_finish_ctx);
134 sha224_stream (FILE *stream, void *resblock)
136 return shaxxx_stream (stream, "sha224", resblock, SHA224_DIGEST_SIZE,
137 sha224_init_ctx, sha224_finish_ctx);
141 * Hey Emacs!
142 * Local Variables:
143 * coding: utf-8
144 * End: