exp2l: Work around a NetBSD 10.0/i386 bug.
[gnulib.git] / lib / sm3-stream.c
blobc534edb12650f2f652b08e5f3b1f198e93ab7a0e
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 <http://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 #if HAVE_OPENSSL_SM3
30 # define GL_OPENSSL_INLINE _GL_EXTERN_INLINE
31 #endif
32 #include "sm3.h"
34 #include <stdlib.h>
36 #if USE_UNLOCKED_IO
37 # include "unlocked-io.h"
38 #endif
40 #define BLOCKSIZE 32768
41 #if BLOCKSIZE % 64 != 0
42 # error "invalid BLOCKSIZE"
43 #endif
45 /* Compute SM3 message digest for bytes read from STREAM. The
46 resulting message digest number will be written into the 32 bytes
47 beginning at RESBLOCK. */
48 int
49 sm3_stream (FILE *stream, void *resblock)
51 struct sm3_ctx ctx;
52 size_t sum;
54 char *buffer = malloc (BLOCKSIZE + 72);
55 if (!buffer)
56 return 1;
58 /* Initialize the computation context. */
59 sm3_init_ctx (&ctx);
61 /* Iterate over full file contents. */
62 while (1)
64 /* We read the file in blocks of BLOCKSIZE bytes. One call of the
65 computation function processes the whole buffer so that with the
66 next round of the loop another block can be read. */
67 size_t n;
68 sum = 0;
70 /* Read block. Take care for partial reads. */
71 while (1)
73 n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream);
75 sum += n;
77 if (sum == BLOCKSIZE)
78 break;
80 if (n == 0)
82 /* Check for the error flag IFF N == 0, so that we don't
83 exit the loop after a partial read due to e.g., EAGAIN
84 or EWOULDBLOCK. */
85 if (ferror (stream))
87 free (buffer);
88 return 1;
90 goto process_partial_block;
93 /* We've read at least one byte, so ignore errors. But always
94 check for EOF, since feof may be true even though N > 0.
95 Otherwise, we could end up calling fread after EOF. */
96 if (feof (stream))
97 goto process_partial_block;
100 /* Process buffer with BLOCKSIZE bytes. Note that
101 BLOCKSIZE % 64 == 0
103 sm3_process_block (buffer, BLOCKSIZE, &ctx);
106 process_partial_block:;
108 /* Process any remaining bytes. */
109 if (sum > 0)
110 sm3_process_bytes (buffer, sum, &ctx);
112 /* Construct result in desired memory. */
113 sm3_finish_ctx (&ctx, resblock);
114 free (buffer);
115 return 0;
119 * Hey Emacs!
120 * Local Variables:
121 * coding: utf-8
122 * End: