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