1 /* Functions to compute MD4 message digest of files or memory blocks.
2 according to the definition of MD4 in RFC 1320 from April 1992.
3 Copyright (C) 1995-1997, 1999-2003, 2005-2006, 2008-2024 Free Software
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 /* Adapted by Simon Josefsson from gnulib md5.? and Libgcrypt
30 # include "unlocked-io.h"
33 #define BLOCKSIZE 32768
34 #if BLOCKSIZE % 64 != 0
35 # error "invalid BLOCKSIZE"
38 /* Compute MD4 message digest for bytes read from STREAM. The
39 resulting message digest number will be written into the 16 bytes
40 beginning at RESBLOCK. */
42 md4_stream (FILE * stream
, void *resblock
)
47 char *buffer
= malloc (BLOCKSIZE
+ 72);
51 /* Initialize the computation context. */
54 /* Iterate over full file contents. */
57 /* We read the file in blocks of BLOCKSIZE bytes. One call of the
58 computation function processes the whole buffer so that with the
59 next round of the loop another block can be read. */
63 /* Read block. Take care for partial reads. */
66 n
= fread (buffer
+ sum
, 1, BLOCKSIZE
- sum
, stream
);
75 /* Check for the error flag IFF N == 0, so that we don't
76 exit the loop after a partial read due to e.g., EAGAIN
83 goto process_partial_block
;
86 /* We've read at least one byte, so ignore errors. But always
87 check for EOF, since feof may be true even though N > 0.
88 Otherwise, we could end up calling fread after EOF. */
90 goto process_partial_block
;
93 /* Process buffer with BLOCKSIZE bytes. Note that
96 md4_process_block (buffer
, BLOCKSIZE
, &ctx
);
99 process_partial_block
:;
101 /* Process any remaining bytes. */
103 md4_process_bytes (buffer
, sum
, &ctx
);
105 /* Construct result in desired memory. */
106 md4_finish_ctx (&ctx
, resblock
);