exp2l: Work around a NetBSD 10.0/i386 bug.
[gnulib.git] / lib / sm3.h
blob216246b0f142d862a8bb4f9f5e07dddb9dfddfd1
1 /* Declarations of functions and data types used for SM3 sum library
2 function.
3 Copyright (C) 2017-2024 Free Software Foundation, Inc.
5 This file is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation; either version 2.1 of the
8 License, or (at your option) any later version.
10 This file is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18 /* This module can be used to compute SM3 message digest of files or
19 memory blocks according to the specification GM/T 004-2012
20 Cryptographic Hash Algorithm SM3, published by State Cryptography
21 Administration, China.
23 The official SM3 cryptographic hash algorithm specification is
24 available at
25 <http://www.sca.gov.cn/sca/xwdt/2010-12/17/content_1002389.shtml>. */
27 #ifndef SM3_H
28 # define SM3_H 1
30 /* This file uses HAVE_OPENSSL_SM3. */
31 # if !_GL_CONFIG_H_INCLUDED
32 # error "Please include config.h first."
33 # endif
35 # include <stdio.h>
36 # include <stdint.h>
38 # if HAVE_OPENSSL_SM3
39 # ifndef OPENSSL_API_COMPAT
40 # define OPENSSL_API_COMPAT 0x10101000L /* FIXME: Use OpenSSL 1.1+ API. */
41 # endif
42 /* If <openssl/macros.h> would give a compile-time error, don't use OpenSSL. */
43 # include <openssl/opensslv.h>
44 # if OPENSSL_VERSION_MAJOR >= 3
45 # include <openssl/configuration.h>
46 # if (OPENSSL_CONFIGURED_API \
47 < (OPENSSL_API_COMPAT < 0x900000L ? OPENSSL_API_COMPAT : \
48 ((OPENSSL_API_COMPAT >> 28) & 0xF) * 10000 \
49 + ((OPENSSL_API_COMPAT >> 20) & 0xFF) * 100 \
50 + ((OPENSSL_API_COMPAT >> 12) & 0xFF)))
51 # undef HAVE_OPENSSL_SM3
52 # endif
53 # endif
54 # if HAVE_OPENSSL_SM3
55 # include <openssl/sm3.h>
56 # endif
57 # endif
59 # ifdef __cplusplus
60 extern "C" {
61 # endif
63 enum { SM3_DIGEST_SIZE = 256 / 8 };
65 # if HAVE_OPENSSL_SM3
66 # define GL_OPENSSL_NAME 3
67 # include "gl_openssl.h"
68 # else
69 /* Structure to save state of computation between the single steps. */
70 struct sm3_ctx
72 uint32_t state[8];
74 uint32_t total[2];
75 size_t buflen; /* ≥ 0, ≤ 128 */
76 uint32_t buffer[32]; /* 128 bytes; the first buflen bytes are in use */
79 /* Initialize structure containing state of computation. */
80 extern void sm3_init_ctx (struct sm3_ctx *ctx);
82 /* Starting with the result of former calls of this function (or the
83 initialization function update the context for the next LEN bytes
84 starting at BUFFER.
85 It is necessary that LEN is a multiple of 64!!! */
86 extern void sm3_process_block (const void *buffer, size_t len,
87 struct sm3_ctx *ctx);
89 /* Starting with the result of former calls of this function (or the
90 initialization function update the context for the next LEN bytes
91 starting at BUFFER.
92 It is NOT required that LEN is a multiple of 64. */
93 extern void sm3_process_bytes (const void *buffer, size_t len,
94 struct sm3_ctx *ctx);
96 /* Process the remaining bytes in the buffer and put result from CTX
97 in first 32 bytes following RESBUF. The result is always in little
98 endian byte order, so that a byte-wise output yields to the wanted
99 ASCII representation of the message digest. */
100 extern void *sm3_finish_ctx (struct sm3_ctx *ctx, void *restrict resbuf);
102 /* Put result from CTX in first 32 bytes following RESBUF. The result is
103 always in little endian byte order, so that a byte-wise output yields
104 to the wanted ASCII representation of the message digest. */
105 extern void *sm3_read_ctx (const struct sm3_ctx *ctx, void *restrict resbuf);
107 /* Compute SM3 message digest for LEN bytes beginning at BUFFER. The
108 result is always in little endian byte order, so that a byte-wise
109 output yields to the wanted ASCII representation of the message
110 digest. */
111 extern void *sm3_buffer (const char *buffer, size_t len,
112 void *restrict resblock);
114 # endif
116 /* Compute SM3 message digest for bytes read from STREAM. The
117 resulting message digest number will be written into the 32 bytes
118 beginning at RESBLOCK. */
119 extern int sm3_stream (FILE *stream, void *resblock);
122 # ifdef __cplusplus
124 # endif
126 #endif
129 * Hey Emacs!
130 * Local Variables:
131 * coding: utf-8
132 * End: