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