blob: adecdc722fa2d93b3edcc6888efc650a73cf860d [file] [log] [blame]
Pol Henarejos0cd1f1c2022-05-09 01:04:15 +02001/**
2 * \file sha3.h
3 *
4 * \brief This file contains SHA3 definitions and functions.
5 *
6 * The Secure Hash Algorithms cryptographic
7 * hash functions are defined in <em>FIPS 202: SHA-3 Standard:
8 * Permutation-Based Hash and Extendable-Output Functions </em>.
9 */
10/*
11 * Copyright The Mbed TLS Contributors
12 * SPDX-License-Identifier: Apache-2.0
13 *
14 * Licensed under the Apache License, Version 2.0 (the "License"); you may
15 * not use this file except in compliance with the License.
16 * You may obtain a copy of the License at
17 *
18 * http://www.apache.org/licenses/LICENSE-2.0
19 *
20 * Unless required by applicable law or agreed to in writing, software
21 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
22 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23 * See the License for the specific language governing permissions and
24 * limitations under the License.
25 */
26
27#ifndef MBEDTLS_SHA3_H
28#define MBEDTLS_SHA3_H
29#include "mbedtls/private_access.h"
30
31#include "mbedtls/build_info.h"
32
33#include <stddef.h>
34#include <stdint.h>
35
36#ifdef __cplusplus
37extern "C" {
38#endif
39
40/** SHA3 input data was malformed. */
41#define MBEDTLS_ERR_SHA3_BAD_INPUT_DATA -0x0076
42
43/**
44 * SHA-3 family id.
45 *
46 * It identifies the family (SHA3-256, SHA3-512, etc.)
47 */
48
49typedef enum
50{
51 MBEDTLS_SHA3_NONE = 0, /*!< Operation not defined. */
52 MBEDTLS_SHA3_224, /*!< SHA3-224 */
53 MBEDTLS_SHA3_256, /*!< SHA3-256 */
54 MBEDTLS_SHA3_384, /*!< SHA3-384 */
55 MBEDTLS_SHA3_512, /*!< SHA3-512 */
56} mbedtls_sha3_id;
57
58#if !defined(MBEDTLS_SHA3_ALT)
59// Regular implementation
60//
61
62struct mbedtls_sha3_context;
63typedef struct mbedtls_sha3_family_functions
64{
65 mbedtls_sha3_id id;
66
67 uint16_t r;
68 uint16_t olen;
69 uint8_t xor_byte;
70}
71mbedtls_sha3_family_functions;
72
73/**
74 * \brief The SHA-3 context structure.
75 *
76 * The structure is used SHA-3 checksum calculations.
77 */
78typedef struct mbedtls_sha3_context {
79 uint64_t state[25];
80 uint8_t index;
81 uint8_t id;
82
83 uint16_t r;
84 uint16_t olen;
85 uint8_t xor_byte;
86 uint16_t max_block_size;
87}
88mbedtls_sha3_context;
89
90#else /* MBEDTLS_SHA3_ALT */
91#include "sha3_alt.h"
92#endif /* MBEDTLS_SHA3_ALT */
93
94/**
95 * \brief This function initializes a SHA-3 context.
96 *
97 * \param ctx The SHA-3 context to initialize. This must not be \c NULL.
98 */
99void mbedtls_sha3_init( mbedtls_sha3_context *ctx );
100
101/**
102 * \brief This function clears a SHA-3 context.
103 *
104 * \param ctx The SHA-3 context to clear. This may be \c NULL, in which
105 * case this function returns immediately. If it is not \c NULL,
106 * it must point to an initialized SHA-3 context.
107 */
108void mbedtls_sha3_free( mbedtls_sha3_context *ctx );
109
110/**
111 * \brief This function clones the state of a SHA-3 context.
112 *
113 * \param dst The destination context. This must be initialized.
114 * \param src The context to clone. This must be initialized.
115 */
116void mbedtls_sha3_clone( mbedtls_sha3_context *dst,
117 const mbedtls_sha3_context *src );
118
119/**
120 * \brief This function starts a SHA-3 checksum
121 * calculation.
122 *
123 * \param ctx The context to use. This must be initialized.
124 * \param id The id of the SHA-3 family.
125 *
126 * \return \c 0 on success.
127 * \return A negative error code on failure.
128 */
129int mbedtls_sha3_starts( mbedtls_sha3_context *ctx, mbedtls_sha3_id id );
130
131/**
132 * \brief This function feeds an input buffer into an ongoing
133 * SHA-3 checksum calculation.
134 *
135 * \param ctx The SHA-3 context. This must be initialized
136 * and have a hash operation started.
137 * \param input The buffer holding the data. This must be a readable
138 * buffer of length \p ilen Bytes.
139 * \param ilen The length of the input data in Bytes.
140 *
141 * \return \c 0 on success.
142 * \return A negative error code on failure.
143 */
144int mbedtls_sha3_update( mbedtls_sha3_context *ctx,
145 const uint8_t *input,
146 size_t ilen );
147
148/**
149 * \brief This function finishes the SHA-3 operation, and writes
150 * the result to the output buffer.
151 *
152 * \param ctx The SHA-3 context. This must be initialized
153 * and have a hash operation started.
154 * \param output The SHA-3 checksum result.
155 * This must be a writable buffer of length \c olen bytes.
156 * \param olen Defines a variable output length (in bytes). \c output must be
157 * \c olen bytes length. For SHA-3 224, SHA-3 256, SHA-3 384 and
158 * SHA-3 512 must equal to 28, 32, 48 and 64, respectively.
159 *
160 * \return \c 0 on success.
161 * \return A negative error code on failure.
162 */
163int mbedtls_sha3_finish( mbedtls_sha3_context *ctx,
164 uint8_t *output, size_t olen );
165
166/**
167 * \brief This function calculates the SHA-3
168 * checksum of a buffer.
169 *
170 * The function allocates the context, performs the
171 * calculation, and frees the context.
172 *
173 * The SHA-3 result is calculated as
174 * output = SHA-3(id, input buffer, d).
175 *
176 * \param id The id of the SHA-3 family.
177 * \param input The buffer holding the data. This must be a readable
178 * buffer of length \p ilen Bytes.
179 * \param ilen The length of the input data in Bytes.
180 * \param output The SHA-3 checksum result.
181 * This must be a writable buffer of length \c olen bytes.
182 * \param olen Determines the length (in bytes) of the output. \c output
183 * must be \c olen bytes length.
184 *
185 * \return \c 0 on success.
186 * \return A negative error code on failure.
187 */
188int mbedtls_sha3( mbedtls_sha3_id id, const uint8_t *input,
189 size_t ilen,
190 uint8_t *output,
191 size_t olen );
192
193#ifdef __cplusplus
194}
195#endif
196
197#endif /* mbedtls_sha3.h */
198