blob: 2ddb22a3bef14ff9cd80337401c5581c16df9482 [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
Pol Henarejosa6779282023-02-08 00:50:04 +010049typedef enum {
Pol Henarejos0cd1f1c2022-05-09 01:04:15 +020050 MBEDTLS_SHA3_NONE = 0, /*!< Operation not defined. */
51 MBEDTLS_SHA3_224, /*!< SHA3-224 */
52 MBEDTLS_SHA3_256, /*!< SHA3-256 */
53 MBEDTLS_SHA3_384, /*!< SHA3-384 */
54 MBEDTLS_SHA3_512, /*!< SHA3-512 */
55} mbedtls_sha3_id;
56
Pol Henarejos0cd1f1c2022-05-09 01:04:15 +020057struct mbedtls_sha3_context;
Pol Henarejosa6779282023-02-08 00:50:04 +010058typedef struct mbedtls_sha3_family_functions {
Pol Henarejos0cd1f1c2022-05-09 01:04:15 +020059 mbedtls_sha3_id id;
60
61 uint16_t r;
62 uint16_t olen;
63 uint8_t xor_byte;
64}
65mbedtls_sha3_family_functions;
66
67/**
68 * \brief The SHA-3 context structure.
69 *
70 * The structure is used SHA-3 checksum calculations.
71 */
72typedef struct mbedtls_sha3_context {
73 uint64_t state[25];
74 uint8_t index;
75 uint8_t id;
76
77 uint16_t r;
78 uint16_t olen;
79 uint8_t xor_byte;
80 uint16_t max_block_size;
81}
82mbedtls_sha3_context;
83
Pol Henarejos0cd1f1c2022-05-09 01:04:15 +020084/**
85 * \brief This function initializes a SHA-3 context.
86 *
87 * \param ctx The SHA-3 context to initialize. This must not be \c NULL.
88 */
Pol Henarejosa6779282023-02-08 00:50:04 +010089void mbedtls_sha3_init(mbedtls_sha3_context *ctx);
Pol Henarejos0cd1f1c2022-05-09 01:04:15 +020090
91/**
92 * \brief This function clears a SHA-3 context.
93 *
94 * \param ctx The SHA-3 context to clear. This may be \c NULL, in which
95 * case this function returns immediately. If it is not \c NULL,
96 * it must point to an initialized SHA-3 context.
97 */
Pol Henarejosa6779282023-02-08 00:50:04 +010098void mbedtls_sha3_free(mbedtls_sha3_context *ctx);
Pol Henarejos0cd1f1c2022-05-09 01:04:15 +020099
100/**
101 * \brief This function clones the state of a SHA-3 context.
102 *
103 * \param dst The destination context. This must be initialized.
104 * \param src The context to clone. This must be initialized.
105 */
Pol Henarejosa6779282023-02-08 00:50:04 +0100106void mbedtls_sha3_clone(mbedtls_sha3_context *dst,
107 const mbedtls_sha3_context *src);
Pol Henarejos0cd1f1c2022-05-09 01:04:15 +0200108
109/**
110 * \brief This function starts a SHA-3 checksum
111 * calculation.
112 *
113 * \param ctx The context to use. This must be initialized.
114 * \param id The id of the SHA-3 family.
115 *
116 * \return \c 0 on success.
117 * \return A negative error code on failure.
118 */
Pol Henarejosa6779282023-02-08 00:50:04 +0100119int mbedtls_sha3_starts(mbedtls_sha3_context *ctx, mbedtls_sha3_id id);
Pol Henarejos0cd1f1c2022-05-09 01:04:15 +0200120
121/**
122 * \brief This function feeds an input buffer into an ongoing
123 * SHA-3 checksum calculation.
124 *
125 * \param ctx The SHA-3 context. This must be initialized
126 * and have a hash operation started.
127 * \param input The buffer holding the data. This must be a readable
128 * buffer of length \p ilen Bytes.
129 * \param ilen The length of the input data in Bytes.
130 *
131 * \return \c 0 on success.
132 * \return A negative error code on failure.
133 */
Pol Henarejosa6779282023-02-08 00:50:04 +0100134int mbedtls_sha3_update(mbedtls_sha3_context *ctx,
135 const uint8_t *input,
136 size_t ilen);
Pol Henarejos0cd1f1c2022-05-09 01:04:15 +0200137
138/**
139 * \brief This function finishes the SHA-3 operation, and writes
140 * the result to the output buffer.
141 *
142 * \param ctx The SHA-3 context. This must be initialized
143 * and have a hash operation started.
144 * \param output The SHA-3 checksum result.
145 * This must be a writable buffer of length \c olen bytes.
Pol Henarejos1f3ae162022-05-17 12:53:30 +0200146 * \param olen Defines the length of output buffer (in bytes). For SHA-3 224, SHA-3 256,
147 * SHA-3 384 and SHA-3 512 \c olen must equal to 28, 32, 48 and 64,
148 * respectively.
Pol Henarejos0cd1f1c2022-05-09 01:04:15 +0200149 *
150 * \return \c 0 on success.
151 * \return A negative error code on failure.
152 */
Pol Henarejosa6779282023-02-08 00:50:04 +0100153int mbedtls_sha3_finish(mbedtls_sha3_context *ctx,
154 uint8_t *output, size_t olen);
Pol Henarejos0cd1f1c2022-05-09 01:04:15 +0200155
156/**
157 * \brief This function calculates the SHA-3
158 * checksum of a buffer.
159 *
160 * The function allocates the context, performs the
161 * calculation, and frees the context.
162 *
163 * The SHA-3 result is calculated as
164 * output = SHA-3(id, input buffer, d).
165 *
166 * \param id The id of the SHA-3 family.
167 * \param input The buffer holding the data. This must be a readable
168 * buffer of length \p ilen Bytes.
169 * \param ilen The length of the input data in Bytes.
170 * \param output The SHA-3 checksum result.
171 * This must be a writable buffer of length \c olen bytes.
Pol Henarejos1f3ae162022-05-17 12:53:30 +0200172 * \param olen Defines the length of output buffer (in bytes). For SHA-3 224, SHA-3 256,
173 * SHA-3 384 and SHA-3 512 \c olen must equal to 28, 32, 48 and 64,
174 * respectively.
Pol Henarejos0cd1f1c2022-05-09 01:04:15 +0200175 *
176 * \return \c 0 on success.
177 * \return A negative error code on failure.
178 */
Pol Henarejosa6779282023-02-08 00:50:04 +0100179int mbedtls_sha3(mbedtls_sha3_id id, const uint8_t *input,
180 size_t ilen,
181 uint8_t *output,
182 size_t olen);
Pol Henarejos0cd1f1c2022-05-09 01:04:15 +0200183
Pol Henarejos7dbd5d12022-05-20 20:42:33 +0200184#if defined(MBEDTLS_SELF_TEST)
185/**
186 * \brief Checkup routine for the algorithms implemented
187 * by this module: SHA3-224, SHA3-256, SHA3-384, SHA3-512,
188 * SHAKE128, SHAKE256, cSHAKE128 and cSHAKE256.
189 *
190 * \return 0 if successful, or 1 if the test failed.
191 */
Pol Henarejosa6779282023-02-08 00:50:04 +0100192int mbedtls_sha3_self_test(int verbose);
Pol Henarejos7dbd5d12022-05-20 20:42:33 +0200193#endif /* MBEDTLS_SELF_TEST */
194
Pol Henarejos0cd1f1c2022-05-09 01:04:15 +0200195#ifdef __cplusplus
196}
197#endif
198
199#endif /* mbedtls_sha3.h */