Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /** |
Simon Butcher | 5b331b9 | 2016-01-03 16:14:14 +0000 | [diff] [blame] | 2 | * \file sha1.h |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 3 | * |
Rose Zadik | 8274142 | 2018-03-27 12:49:48 +0100 | [diff] [blame] | 4 | * \brief This file contains SHA-1 definitions and functions. |
| 5 | * |
Darryl Green | 11999bb | 2018-03-13 15:22:58 +0000 | [diff] [blame] | 6 | * The Secure Hash Algorithm 1 (SHA-1) cryptographic hash function is defined in |
Rose Zadik | 8274142 | 2018-03-27 12:49:48 +0100 | [diff] [blame] | 7 | * <em>FIPS 180-4: Secure Hash Standard (SHS)</em>. |
Hanno Becker | bbca8c5 | 2017-09-25 14:53:51 +0100 | [diff] [blame] | 8 | * |
| 9 | * \warning SHA-1 is considered a weak message digest and its use constitutes |
| 10 | * a security risk. We recommend considering stronger message |
| 11 | * digests instead. |
Darryl Green | a40a101 | 2018-01-05 15:33:17 +0000 | [diff] [blame] | 12 | */ |
| 13 | /* |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 14 | * Copyright The Mbed TLS Contributors |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 15 | * SPDX-License-Identifier: Apache-2.0 |
| 16 | * |
| 17 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 18 | * not use this file except in compliance with the License. |
| 19 | * You may obtain a copy of the License at |
| 20 | * |
| 21 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 22 | * |
| 23 | * Unless required by applicable law or agreed to in writing, software |
| 24 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 25 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 26 | * See the License for the specific language governing permissions and |
| 27 | * limitations under the License. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 28 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 29 | #ifndef MBEDTLS_SHA1_H |
| 30 | #define MBEDTLS_SHA1_H |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 31 | #include "mbedtls/private_access.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 32 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 33 | #if !defined(MBEDTLS_CONFIG_FILE) |
Jaeden Amero | c49fbbf | 2019-07-04 20:01:14 +0100 | [diff] [blame] | 34 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 35 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 36 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 37 | #endif |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 38 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 39 | #include <stddef.h> |
Manuel Pégourié-Gonnard | ab22910 | 2015-04-15 11:53:16 +0200 | [diff] [blame] | 40 | #include <stdint.h> |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 41 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 42 | #define MBEDTLS_ERR_SHA1_BAD_INPUT_DATA -0x0073 /**< SHA-1 input data was malformed. */ |
Gilles Peskine | a381fe8 | 2018-01-23 18:16:11 +0100 | [diff] [blame] | 43 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 44 | #ifdef __cplusplus |
| 45 | extern "C" { |
| 46 | #endif |
| 47 | |
Ron Eldor | b2aacec | 2017-05-18 16:53:08 +0300 | [diff] [blame] | 48 | #if !defined(MBEDTLS_SHA1_ALT) |
| 49 | // Regular implementation |
| 50 | // |
| 51 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 52 | /** |
Rose Zadik | 44833d9 | 2018-01-26 08:41:09 +0000 | [diff] [blame] | 53 | * \brief The SHA-1 context structure. |
Hanno Becker | bbca8c5 | 2017-09-25 14:53:51 +0100 | [diff] [blame] | 54 | * |
| 55 | * \warning SHA-1 is considered a weak message digest and its use |
| 56 | * constitutes a security risk. We recommend considering |
| 57 | * stronger message digests instead. |
| 58 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 59 | */ |
Dawid Drozd | 428cc52 | 2018-07-24 10:02:47 +0200 | [diff] [blame] | 60 | typedef struct mbedtls_sha1_context |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 61 | { |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 62 | uint32_t MBEDTLS_PRIVATE(total)[2]; /*!< The number of Bytes processed. */ |
| 63 | uint32_t MBEDTLS_PRIVATE(state)[5]; /*!< The intermediate digest state. */ |
| 64 | unsigned char MBEDTLS_PRIVATE(buffer)[64]; /*!< The data block being processed. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 65 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 66 | mbedtls_sha1_context; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 67 | |
Ron Eldor | b2aacec | 2017-05-18 16:53:08 +0300 | [diff] [blame] | 68 | #else /* MBEDTLS_SHA1_ALT */ |
| 69 | #include "sha1_alt.h" |
| 70 | #endif /* MBEDTLS_SHA1_ALT */ |
| 71 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 72 | /** |
Rose Zadik | 44833d9 | 2018-01-26 08:41:09 +0000 | [diff] [blame] | 73 | * \brief This function initializes a SHA-1 context. |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 74 | * |
Hanno Becker | bbca8c5 | 2017-09-25 14:53:51 +0100 | [diff] [blame] | 75 | * \warning SHA-1 is considered a weak message digest and its use |
| 76 | * constitutes a security risk. We recommend considering |
| 77 | * stronger message digests instead. |
| 78 | * |
Rose Zadik | 8274142 | 2018-03-27 12:49:48 +0100 | [diff] [blame] | 79 | * \param ctx The SHA-1 context to initialize. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 80 | * This must not be \c NULL. |
Rose Zadik | 8274142 | 2018-03-27 12:49:48 +0100 | [diff] [blame] | 81 | * |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 82 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 83 | void mbedtls_sha1_init( mbedtls_sha1_context *ctx ); |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 84 | |
| 85 | /** |
Rose Zadik | 44833d9 | 2018-01-26 08:41:09 +0000 | [diff] [blame] | 86 | * \brief This function clears a SHA-1 context. |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 87 | * |
Hanno Becker | bbca8c5 | 2017-09-25 14:53:51 +0100 | [diff] [blame] | 88 | * \warning SHA-1 is considered a weak message digest and its use |
| 89 | * constitutes a security risk. We recommend considering |
| 90 | * stronger message digests instead. |
| 91 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 92 | * \param ctx The SHA-1 context to clear. This may be \c NULL, |
| 93 | * in which case this function does nothing. If it is |
| 94 | * not \c NULL, it must point to an initialized |
| 95 | * SHA-1 context. |
Rose Zadik | 8274142 | 2018-03-27 12:49:48 +0100 | [diff] [blame] | 96 | * |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 97 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 98 | void mbedtls_sha1_free( mbedtls_sha1_context *ctx ); |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 99 | |
| 100 | /** |
Rose Zadik | 44833d9 | 2018-01-26 08:41:09 +0000 | [diff] [blame] | 101 | * \brief This function clones the state of a SHA-1 context. |
Manuel Pégourié-Gonnard | 16d412f | 2015-07-06 15:26:26 +0200 | [diff] [blame] | 102 | * |
Hanno Becker | bbca8c5 | 2017-09-25 14:53:51 +0100 | [diff] [blame] | 103 | * \warning SHA-1 is considered a weak message digest and its use |
| 104 | * constitutes a security risk. We recommend considering |
| 105 | * stronger message digests instead. |
| 106 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 107 | * \param dst The SHA-1 context to clone to. This must be initialized. |
| 108 | * \param src The SHA-1 context to clone from. This must be initialized. |
Rose Zadik | 8274142 | 2018-03-27 12:49:48 +0100 | [diff] [blame] | 109 | * |
Manuel Pégourié-Gonnard | 16d412f | 2015-07-06 15:26:26 +0200 | [diff] [blame] | 110 | */ |
| 111 | void mbedtls_sha1_clone( mbedtls_sha1_context *dst, |
| 112 | const mbedtls_sha1_context *src ); |
| 113 | |
| 114 | /** |
Rose Zadik | 44833d9 | 2018-01-26 08:41:09 +0000 | [diff] [blame] | 115 | * \brief This function starts a SHA-1 checksum calculation. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 116 | * |
Hanno Becker | bbca8c5 | 2017-09-25 14:53:51 +0100 | [diff] [blame] | 117 | * \warning SHA-1 is considered a weak message digest and its use |
| 118 | * constitutes a security risk. We recommend considering |
| 119 | * stronger message digests instead. |
| 120 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 121 | * \param ctx The SHA-1 context to initialize. This must be initialized. |
Rose Zadik | 8274142 | 2018-03-27 12:49:48 +0100 | [diff] [blame] | 122 | * |
| 123 | * \return \c 0 on success. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 124 | * \return A negative error code on failure. |
Rose Zadik | 8274142 | 2018-03-27 12:49:48 +0100 | [diff] [blame] | 125 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 126 | */ |
TRodziewicz | 26371e4 | 2021-06-08 16:45:41 +0200 | [diff] [blame] | 127 | int mbedtls_sha1_starts( mbedtls_sha1_context *ctx ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 128 | |
| 129 | /** |
Rose Zadik | 44833d9 | 2018-01-26 08:41:09 +0000 | [diff] [blame] | 130 | * \brief This function feeds an input buffer into an ongoing SHA-1 |
| 131 | * checksum calculation. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 132 | * |
Hanno Becker | bbca8c5 | 2017-09-25 14:53:51 +0100 | [diff] [blame] | 133 | * \warning SHA-1 is considered a weak message digest and its use |
| 134 | * constitutes a security risk. We recommend considering |
| 135 | * stronger message digests instead. |
| 136 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 137 | * \param ctx The SHA-1 context. This must be initialized |
| 138 | * and have a hash operation started. |
Rose Zadik | 8274142 | 2018-03-27 12:49:48 +0100 | [diff] [blame] | 139 | * \param input The buffer holding the input data. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 140 | * This must be a readable buffer of length \p ilen Bytes. |
| 141 | * \param ilen The length of the input data \p input in Bytes. |
Rose Zadik | 8274142 | 2018-03-27 12:49:48 +0100 | [diff] [blame] | 142 | * |
| 143 | * \return \c 0 on success. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 144 | * \return A negative error code on failure. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 145 | */ |
TRodziewicz | 26371e4 | 2021-06-08 16:45:41 +0200 | [diff] [blame] | 146 | int mbedtls_sha1_update( mbedtls_sha1_context *ctx, |
| 147 | const unsigned char *input, |
| 148 | size_t ilen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 149 | |
| 150 | /** |
Rose Zadik | 44833d9 | 2018-01-26 08:41:09 +0000 | [diff] [blame] | 151 | * \brief This function finishes the SHA-1 operation, and writes |
| 152 | * the result to the output buffer. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 153 | * |
Hanno Becker | bbca8c5 | 2017-09-25 14:53:51 +0100 | [diff] [blame] | 154 | * \warning SHA-1 is considered a weak message digest and its use |
| 155 | * constitutes a security risk. We recommend considering |
| 156 | * stronger message digests instead. |
| 157 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 158 | * \param ctx The SHA-1 context to use. This must be initialized and |
| 159 | * have a hash operation started. |
| 160 | * \param output The SHA-1 checksum result. This must be a writable |
| 161 | * buffer of length \c 20 Bytes. |
Rose Zadik | 8274142 | 2018-03-27 12:49:48 +0100 | [diff] [blame] | 162 | * |
| 163 | * \return \c 0 on success. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 164 | * \return A negative error code on failure. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 165 | */ |
TRodziewicz | 26371e4 | 2021-06-08 16:45:41 +0200 | [diff] [blame] | 166 | int mbedtls_sha1_finish( mbedtls_sha1_context *ctx, |
| 167 | unsigned char output[20] ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 168 | |
Andres Amaya Garcia | 034ea7e | 2017-04-28 15:14:50 +0100 | [diff] [blame] | 169 | /** |
Rose Zadik | 8274142 | 2018-03-27 12:49:48 +0100 | [diff] [blame] | 170 | * \brief SHA-1 process data block (internal use only). |
Hanno Becker | bbca8c5 | 2017-09-25 14:53:51 +0100 | [diff] [blame] | 171 | * |
| 172 | * \warning SHA-1 is considered a weak message digest and its use |
| 173 | * constitutes a security risk. We recommend considering |
| 174 | * stronger message digests instead. |
| 175 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 176 | * \param ctx The SHA-1 context to use. This must be initialized. |
| 177 | * \param data The data block being processed. This must be a |
| 178 | * readable buffer of length \c 64 Bytes. |
Rose Zadik | 8274142 | 2018-03-27 12:49:48 +0100 | [diff] [blame] | 179 | * |
| 180 | * \return \c 0 on success. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 181 | * \return A negative error code on failure. |
Rose Zadik | 8274142 | 2018-03-27 12:49:48 +0100 | [diff] [blame] | 182 | * |
Andres Amaya Garcia | 034ea7e | 2017-04-28 15:14:50 +0100 | [diff] [blame] | 183 | */ |
Andres Amaya Garcia | cccfe08 | 2017-06-28 10:36:39 +0100 | [diff] [blame] | 184 | int mbedtls_internal_sha1_process( mbedtls_sha1_context *ctx, |
| 185 | const unsigned char data[64] ); |
Andres Amaya Garcia | 034ea7e | 2017-04-28 15:14:50 +0100 | [diff] [blame] | 186 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 187 | /** |
Rose Zadik | 44833d9 | 2018-01-26 08:41:09 +0000 | [diff] [blame] | 188 | * \brief This function calculates the SHA-1 checksum of a buffer. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 189 | * |
Rose Zadik | 44833d9 | 2018-01-26 08:41:09 +0000 | [diff] [blame] | 190 | * The function allocates the context, performs the |
| 191 | * calculation, and frees the context. |
Andres Amaya Garcia | 034ea7e | 2017-04-28 15:14:50 +0100 | [diff] [blame] | 192 | * |
Rose Zadik | 44833d9 | 2018-01-26 08:41:09 +0000 | [diff] [blame] | 193 | * The SHA-1 result is calculated as |
| 194 | * output = SHA-1(input buffer). |
| 195 | * |
Rose Zadik | 8274142 | 2018-03-27 12:49:48 +0100 | [diff] [blame] | 196 | * \warning SHA-1 is considered a weak message digest and its use |
| 197 | * constitutes a security risk. We recommend considering |
| 198 | * stronger message digests instead. |
| 199 | * |
Rose Zadik | 44833d9 | 2018-01-26 08:41:09 +0000 | [diff] [blame] | 200 | * \param input The buffer holding the input data. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 201 | * This must be a readable buffer of length \p ilen Bytes. |
| 202 | * \param ilen The length of the input data \p input in Bytes. |
Rose Zadik | 44833d9 | 2018-01-26 08:41:09 +0000 | [diff] [blame] | 203 | * \param output The SHA-1 checksum result. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 204 | * This must be a writable buffer of length \c 20 Bytes. |
Rose Zadik | 44833d9 | 2018-01-26 08:41:09 +0000 | [diff] [blame] | 205 | * |
Rose Zadik | 8274142 | 2018-03-27 12:49:48 +0100 | [diff] [blame] | 206 | * \return \c 0 on success. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 207 | * \return A negative error code on failure. |
Hanno Becker | bbca8c5 | 2017-09-25 14:53:51 +0100 | [diff] [blame] | 208 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 209 | */ |
TRodziewicz | 26371e4 | 2021-06-08 16:45:41 +0200 | [diff] [blame] | 210 | int mbedtls_sha1( const unsigned char *input, |
| 211 | size_t ilen, |
| 212 | unsigned char output[20] ); |
Andres Amaya Garcia | 034ea7e | 2017-04-28 15:14:50 +0100 | [diff] [blame] | 213 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 214 | #if defined(MBEDTLS_SELF_TEST) |
| 215 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 216 | /** |
Rose Zadik | 44833d9 | 2018-01-26 08:41:09 +0000 | [diff] [blame] | 217 | * \brief The SHA-1 checkup routine. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 218 | * |
Hanno Becker | bbca8c5 | 2017-09-25 14:53:51 +0100 | [diff] [blame] | 219 | * \warning SHA-1 is considered a weak message digest and its use |
| 220 | * constitutes a security risk. We recommend considering |
| 221 | * stronger message digests instead. |
| 222 | * |
Rose Zadik | 8274142 | 2018-03-27 12:49:48 +0100 | [diff] [blame] | 223 | * \return \c 0 on success. |
| 224 | * \return \c 1 on failure. |
| 225 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 226 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 227 | int mbedtls_sha1_self_test( int verbose ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 228 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 229 | #endif /* MBEDTLS_SELF_TEST */ |
| 230 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 231 | #ifdef __cplusplus |
| 232 | } |
| 233 | #endif |
| 234 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 235 | #endif /* mbedtls_sha1.h */ |