| Pol Henarejos | 0cd1f1c | 2022-05-09 01:04:15 +0200 | [diff] [blame] | 1 | /** | 
|  | 2 | * \file sha3.h | 
|  | 3 | * | 
| Dave Rodgman | cf4d2bd | 2023-06-07 17:08:09 +0100 | [diff] [blame] | 4 | * \brief This file contains SHA-3 definitions and functions. | 
| Pol Henarejos | 0cd1f1c | 2022-05-09 01:04:15 +0200 | [diff] [blame] | 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 | 
|  | 37 | extern "C" { | 
|  | 38 | #endif | 
|  | 39 |  | 
| Dave Rodgman | cf4d2bd | 2023-06-07 17:08:09 +0100 | [diff] [blame] | 40 | /** SHA-3 input data was malformed. */ | 
| Pol Henarejos | 0cd1f1c | 2022-05-09 01:04:15 +0200 | [diff] [blame] | 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 Henarejos | a677928 | 2023-02-08 00:50:04 +0100 | [diff] [blame] | 49 | typedef enum { | 
| Pol Henarejos | 0cd1f1c | 2022-05-09 01:04:15 +0200 | [diff] [blame] | 50 | 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 Henarejos | 0cd1f1c | 2022-05-09 01:04:15 +0200 | [diff] [blame] | 57 | /** | 
|  | 58 | * \brief          The SHA-3 context structure. | 
|  | 59 | * | 
|  | 60 | *                 The structure is used SHA-3 checksum calculations. | 
|  | 61 | */ | 
| Dave Rodgman | c3048b3 | 2023-05-29 22:08:19 +0100 | [diff] [blame] | 62 | typedef struct { | 
| Dave Rodgman | a35551e | 2023-06-07 17:08:19 +0100 | [diff] [blame] | 63 | uint64_t MBEDTLS_PRIVATE(state[25]); | 
|  | 64 | uint32_t MBEDTLS_PRIVATE(index); | 
|  | 65 | uint16_t MBEDTLS_PRIVATE(olen); | 
|  | 66 | uint16_t MBEDTLS_PRIVATE(max_block_size); | 
| Pol Henarejos | 0cd1f1c | 2022-05-09 01:04:15 +0200 | [diff] [blame] | 67 | } | 
|  | 68 | mbedtls_sha3_context; | 
|  | 69 |  | 
| Pol Henarejos | 0cd1f1c | 2022-05-09 01:04:15 +0200 | [diff] [blame] | 70 | /** | 
|  | 71 | * \brief          This function initializes a SHA-3 context. | 
|  | 72 | * | 
|  | 73 | * \param ctx      The SHA-3 context to initialize. This must not be \c NULL. | 
|  | 74 | */ | 
| Pol Henarejos | a677928 | 2023-02-08 00:50:04 +0100 | [diff] [blame] | 75 | void mbedtls_sha3_init(mbedtls_sha3_context *ctx); | 
| Pol Henarejos | 0cd1f1c | 2022-05-09 01:04:15 +0200 | [diff] [blame] | 76 |  | 
|  | 77 | /** | 
|  | 78 | * \brief          This function clears a SHA-3 context. | 
|  | 79 | * | 
|  | 80 | * \param ctx      The SHA-3 context to clear. This may be \c NULL, in which | 
|  | 81 | *                 case this function returns immediately. If it is not \c NULL, | 
|  | 82 | *                 it must point to an initialized SHA-3 context. | 
|  | 83 | */ | 
| Pol Henarejos | a677928 | 2023-02-08 00:50:04 +0100 | [diff] [blame] | 84 | void mbedtls_sha3_free(mbedtls_sha3_context *ctx); | 
| Pol Henarejos | 0cd1f1c | 2022-05-09 01:04:15 +0200 | [diff] [blame] | 85 |  | 
|  | 86 | /** | 
|  | 87 | * \brief          This function clones the state of a SHA-3 context. | 
|  | 88 | * | 
|  | 89 | * \param dst      The destination context. This must be initialized. | 
|  | 90 | * \param src      The context to clone. This must be initialized. | 
|  | 91 | */ | 
| Pol Henarejos | a677928 | 2023-02-08 00:50:04 +0100 | [diff] [blame] | 92 | void mbedtls_sha3_clone(mbedtls_sha3_context *dst, | 
|  | 93 | const mbedtls_sha3_context *src); | 
| Pol Henarejos | 0cd1f1c | 2022-05-09 01:04:15 +0200 | [diff] [blame] | 94 |  | 
|  | 95 | /** | 
|  | 96 | * \brief          This function starts a SHA-3 checksum | 
|  | 97 | *                 calculation. | 
|  | 98 | * | 
|  | 99 | * \param ctx      The context to use. This must be initialized. | 
|  | 100 | * \param id       The id of the SHA-3 family. | 
|  | 101 | * | 
|  | 102 | * \return         \c 0 on success. | 
|  | 103 | * \return         A negative error code on failure. | 
|  | 104 | */ | 
| Pol Henarejos | a677928 | 2023-02-08 00:50:04 +0100 | [diff] [blame] | 105 | int mbedtls_sha3_starts(mbedtls_sha3_context *ctx, mbedtls_sha3_id id); | 
| Pol Henarejos | 0cd1f1c | 2022-05-09 01:04:15 +0200 | [diff] [blame] | 106 |  | 
|  | 107 | /** | 
|  | 108 | * \brief          This function feeds an input buffer into an ongoing | 
|  | 109 | *                 SHA-3 checksum calculation. | 
|  | 110 | * | 
|  | 111 | * \param ctx      The SHA-3 context. This must be initialized | 
|  | 112 | *                 and have a hash operation started. | 
|  | 113 | * \param input    The buffer holding the data. This must be a readable | 
|  | 114 | *                 buffer of length \p ilen Bytes. | 
|  | 115 | * \param ilen     The length of the input data in Bytes. | 
|  | 116 | * | 
|  | 117 | * \return         \c 0 on success. | 
|  | 118 | * \return         A negative error code on failure. | 
|  | 119 | */ | 
| Pol Henarejos | a677928 | 2023-02-08 00:50:04 +0100 | [diff] [blame] | 120 | int mbedtls_sha3_update(mbedtls_sha3_context *ctx, | 
|  | 121 | const uint8_t *input, | 
|  | 122 | size_t ilen); | 
| Pol Henarejos | 0cd1f1c | 2022-05-09 01:04:15 +0200 | [diff] [blame] | 123 |  | 
|  | 124 | /** | 
|  | 125 | * \brief          This function finishes the SHA-3 operation, and writes | 
|  | 126 | *                 the result to the output buffer. | 
|  | 127 | * | 
|  | 128 | * \param ctx      The SHA-3 context. This must be initialized | 
|  | 129 | *                 and have a hash operation started. | 
|  | 130 | * \param output   The SHA-3 checksum result. | 
|  | 131 | *                 This must be a writable buffer of length \c olen bytes. | 
| Pol Henarejos | 1f3ae16 | 2022-05-17 12:53:30 +0200 | [diff] [blame] | 132 | * \param olen     Defines the length of output buffer (in bytes). For SHA-3 224, SHA-3 256, | 
|  | 133 | *                 SHA-3 384 and SHA-3 512 \c olen must equal to 28, 32, 48 and 64, | 
|  | 134 | *                 respectively. | 
| Pol Henarejos | 0cd1f1c | 2022-05-09 01:04:15 +0200 | [diff] [blame] | 135 | * | 
|  | 136 | * \return         \c 0 on success. | 
|  | 137 | * \return         A negative error code on failure. | 
|  | 138 | */ | 
| Pol Henarejos | a677928 | 2023-02-08 00:50:04 +0100 | [diff] [blame] | 139 | int mbedtls_sha3_finish(mbedtls_sha3_context *ctx, | 
|  | 140 | uint8_t *output, size_t olen); | 
| Pol Henarejos | 0cd1f1c | 2022-05-09 01:04:15 +0200 | [diff] [blame] | 141 |  | 
|  | 142 | /** | 
|  | 143 | * \brief          This function calculates the SHA-3 | 
|  | 144 | *                 checksum of a buffer. | 
|  | 145 | * | 
|  | 146 | *                 The function allocates the context, performs the | 
|  | 147 | *                 calculation, and frees the context. | 
|  | 148 | * | 
|  | 149 | *                 The SHA-3 result is calculated as | 
|  | 150 | *                 output = SHA-3(id, input buffer, d). | 
|  | 151 | * | 
|  | 152 | * \param id       The id of the SHA-3 family. | 
|  | 153 | * \param input    The buffer holding the data. This must be a readable | 
|  | 154 | *                 buffer of length \p ilen Bytes. | 
|  | 155 | * \param ilen     The length of the input data in Bytes. | 
|  | 156 | * \param output   The SHA-3 checksum result. | 
|  | 157 | *                 This must be a writable buffer of length \c olen bytes. | 
| Pol Henarejos | 1f3ae16 | 2022-05-17 12:53:30 +0200 | [diff] [blame] | 158 | * \param olen     Defines the length of output buffer (in bytes). For SHA-3 224, SHA-3 256, | 
|  | 159 | *                 SHA-3 384 and SHA-3 512 \c olen must equal to 28, 32, 48 and 64, | 
|  | 160 | *                 respectively. | 
| Pol Henarejos | 0cd1f1c | 2022-05-09 01:04:15 +0200 | [diff] [blame] | 161 | * | 
|  | 162 | * \return         \c 0 on success. | 
|  | 163 | * \return         A negative error code on failure. | 
|  | 164 | */ | 
| Pol Henarejos | a677928 | 2023-02-08 00:50:04 +0100 | [diff] [blame] | 165 | int mbedtls_sha3(mbedtls_sha3_id id, const uint8_t *input, | 
|  | 166 | size_t ilen, | 
|  | 167 | uint8_t *output, | 
|  | 168 | size_t olen); | 
| Pol Henarejos | 0cd1f1c | 2022-05-09 01:04:15 +0200 | [diff] [blame] | 169 |  | 
| Pol Henarejos | 7dbd5d1 | 2022-05-20 20:42:33 +0200 | [diff] [blame] | 170 | #if defined(MBEDTLS_SELF_TEST) | 
|  | 171 | /** | 
|  | 172 | * \brief          Checkup routine for the algorithms implemented | 
| Dave Rodgman | f9d8f4c | 2023-06-07 17:08:29 +0100 | [diff] [blame] | 173 | *                 by this module: SHA3-224, SHA3-256, SHA3-384, SHA3-512. | 
| Pol Henarejos | 7dbd5d1 | 2022-05-20 20:42:33 +0200 | [diff] [blame] | 174 | * | 
|  | 175 | * \return         0 if successful, or 1 if the test failed. | 
|  | 176 | */ | 
| Pol Henarejos | a677928 | 2023-02-08 00:50:04 +0100 | [diff] [blame] | 177 | int mbedtls_sha3_self_test(int verbose); | 
| Pol Henarejos | 7dbd5d1 | 2022-05-20 20:42:33 +0200 | [diff] [blame] | 178 | #endif /* MBEDTLS_SELF_TEST */ | 
|  | 179 |  | 
| Pol Henarejos | 0cd1f1c | 2022-05-09 01:04:15 +0200 | [diff] [blame] | 180 | #ifdef __cplusplus | 
|  | 181 | } | 
|  | 182 | #endif | 
|  | 183 |  | 
|  | 184 | #endif /* mbedtls_sha3.h */ |