openat: don’t close (-1)
[gnulib.git] / lib / sm3-stream.c
blob0a215624e85fb23419e0811c8deb93e948eebe48
1 /* sm3.c - Functions to compute SM3 message digest of files or memory blocks
2 according to the specification GM/T 004-2012 Cryptographic Hash Algorithm
3 SM3, published by State Encryption Management Bureau, China.
5 SM3 cryptographic hash algorithm.
6 <https://www.sca.gov.cn/sca/xwdt/2010-12/17/content_1002389.shtml>
8 Copyright (C) 2017-2024 Free Software Foundation, Inc.
10 This file is free software: you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as
12 published by the Free Software Foundation; either version 2.1 of the
13 License, or (at your option) any later version.
15 This file is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
20 You should have received a copy of the GNU Lesser General Public License
21 along with this program. If not, see <https://www.gnu.org/licenses/>. */
23 /* Written by Jia Zhang <qianyue.zj@alibaba-inc.com>, 2017,
24 considerably copypasting from David Madore's sha256.c */
26 #include <config.h>
28 /* Specification. */
29 #include "sm3.h"
31 #include <stdlib.h>
33 #if USE_UNLOCKED_IO
34 # include "unlocked-io.h"
35 #endif
37 #define BLOCKSIZE 32768
38 #if BLOCKSIZE % 64 != 0
39 # error "invalid BLOCKSIZE"
40 #endif
42 /* Compute SM3 message digest for bytes read from STREAM. The
43 resulting message digest number will be written into the 32 bytes
44 beginning at RESBLOCK. */
45 int
46 sm3_stream (FILE *stream, void *resblock)
48 struct sm3_ctx ctx;
49 size_t sum;
51 char *buffer = malloc (BLOCKSIZE + 72);
52 if (!buffer)
53 return 1;
55 /* Initialize the computation context. */
56 sm3_init_ctx (&ctx);
58 /* Iterate over full file contents. */
59 while (1)
61 /* We read the file in blocks of BLOCKSIZE bytes. One call of the
62 computation function processes the whole buffer so that with the
63 next round of the loop another block can be read. */
64 size_t n;
65 sum = 0;
67 /* Read block. Take care for partial reads. */
68 while (1)
70 n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream);
72 sum += n;
74 if (sum == BLOCKSIZE)
75 break;
77 if (n == 0)
79 /* Check for the error flag IFF N == 0, so that we don't
80 exit the loop after a partial read due to e.g., EAGAIN
81 or EWOULDBLOCK. */
82 if (ferror (stream))
84 free (buffer);
85 return 1;
87 goto process_partial_block;
90 /* We've read at least one byte, so ignore errors. But always
91 check for EOF, since feof may be true even though N > 0.
92 Otherwise, we could end up calling fread after EOF. */
93 if (feof (stream))
94 goto process_partial_block;
97 /* Process buffer with BLOCKSIZE bytes. Note that
98 BLOCKSIZE % 64 == 0
100 sm3_process_block (buffer, BLOCKSIZE, &ctx);
103 process_partial_block:;
105 /* Process any remaining bytes. */
106 if (sum > 0)
107 sm3_process_bytes (buffer, sum, &ctx);
109 /* Construct result in desired memory. */
110 sm3_finish_ctx (&ctx, resblock);
111 free (buffer);
112 return 0;