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-2021 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 /* Adapted by Simon Josefsson from gnulib md5.? and Libgcrypt 20 cipher/md4.c . */ 21 22 #include <config.h> 23 24 /* Specification. */ 25 #include "md4.h" 26 27 #include <stdlib.h> 28 29 #if USE_UNLOCKED_IO 30 # include "unlocked-io.h" 31 #endif 32 33 #define BLOCKSIZE 32768 34 #if BLOCKSIZE % 64 != 0 35 # error "invalid BLOCKSIZE" 36 #endif 37 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. */ 41 int 42 md4_stream (FILE * stream, void *resblock) /* */ 43 { 44 struct md4_ctx ctx; 45 size_t sum; 46 47 char *buffer = malloc (BLOCKSIZE + 72); 48 if (!buffer) 49 return 1; 50 51 /* Initialize the computation context. */ 52 md4_init_ctx (&ctx); 53 54 /* Iterate over full file contents. */ 55 while (1) 56 { 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. */ 60 size_t n; 61 sum = 0; 62 63 /* Read block. Take care for partial reads. */ 64 while (1) 65 { 66 n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream); 67 68 sum += n; 69 70 if (sum == BLOCKSIZE) 71 break; 72 73 if (n == 0) 74 { 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 77 or EWOULDBLOCK. */ 78 if (ferror (stream)) 79 { 80 free (buffer); 81 return 1; 82 } 83 goto process_partial_block; 84 } 85 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. */ 89 if (feof (stream)) 90 goto process_partial_block; 91 } 92 93 /* Process buffer with BLOCKSIZE bytes. Note that 94 BLOCKSIZE % 64 == 0 95 */ 96 md4_process_block (buffer, BLOCKSIZE, &ctx); 97 } 98 99 process_partial_block:; 100 101 /* Process any remaining bytes. */ 102 if (sum > 0) 103 md4_process_bytes (buffer, sum, &ctx); 104 105 /* Construct result in desired memory. */ 106 md4_finish_ctx (&ctx, resblock); 107 free (buffer); 108 return 0; 109 }