Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file sha1.h |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 3 | * |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 4 | * \brief SHA-1 cryptographic hash function |
| 5 | * |
Manuel Pégourié-Gonnard | a658a40 | 2015-01-23 09:45:19 +0000 | [diff] [blame] | 6 | * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 7 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 8 | * This file is part of mbed TLS (https://tls.mbed.org) |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 9 | * |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License along |
| 21 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 22 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
Hanno Becker | ce0c9db | 2017-09-28 15:39:45 +0100 | [diff] [blame^] | 23 | * |
| 24 | * \warning SHA-1 is considered a weak message digest and its use constitutes |
| 25 | * a security risk. We recommend considering stronger message |
| 26 | * digests instead. |
| 27 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 28 | */ |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 29 | #ifndef POLARSSL_SHA1_H |
| 30 | #define POLARSSL_SHA1_H |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 31 | |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 32 | #if !defined(POLARSSL_CONFIG_FILE) |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 33 | #include "config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 34 | #else |
| 35 | #include POLARSSL_CONFIG_FILE |
| 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> |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 39 | |
Paul Bakker | fa6a620 | 2013-10-28 18:48:30 +0100 | [diff] [blame] | 40 | #if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32) |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 41 | #include <basetsd.h> |
| 42 | typedef UINT32 uint32_t; |
| 43 | #else |
| 44 | #include <inttypes.h> |
| 45 | #endif |
| 46 | |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 47 | #define POLARSSL_ERR_SHA1_FILE_IO_ERROR -0x0076 /**< Read/write error in file. */ |
| 48 | |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 49 | #if !defined(POLARSSL_SHA1_ALT) |
| 50 | // Regular implementation |
| 51 | // |
| 52 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 53 | #ifdef __cplusplus |
| 54 | extern "C" { |
| 55 | #endif |
| 56 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 57 | /** |
| 58 | * \brief SHA-1 context structure |
Hanno Becker | ce0c9db | 2017-09-28 15:39:45 +0100 | [diff] [blame^] | 59 | * |
| 60 | * \warning SHA-1 is considered a weak message digest and its use |
| 61 | * constitutes a security risk. We recommend considering |
| 62 | * stronger message digests instead. |
| 63 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 64 | */ |
| 65 | typedef struct |
| 66 | { |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 67 | uint32_t total[2]; /*!< number of bytes processed */ |
| 68 | uint32_t state[5]; /*!< intermediate digest state */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 69 | unsigned char buffer[64]; /*!< data block being processed */ |
| 70 | |
| 71 | unsigned char ipad[64]; /*!< HMAC: inner padding */ |
| 72 | unsigned char opad[64]; /*!< HMAC: outer padding */ |
| 73 | } |
| 74 | sha1_context; |
| 75 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 76 | /** |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 77 | * \brief Initialize SHA-1 context |
| 78 | * |
| 79 | * \param ctx SHA-1 context to be initialized |
Hanno Becker | ce0c9db | 2017-09-28 15:39:45 +0100 | [diff] [blame^] | 80 | * |
| 81 | * \warning SHA-1 is considered a weak message digest and its use |
| 82 | * constitutes a security risk. We recommend considering |
| 83 | * stronger message digests instead. |
| 84 | * |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 85 | */ |
| 86 | void sha1_init( sha1_context *ctx ); |
| 87 | |
| 88 | /** |
| 89 | * \brief Clear SHA-1 context |
| 90 | * |
| 91 | * \param ctx SHA-1 context to be cleared |
Hanno Becker | ce0c9db | 2017-09-28 15:39:45 +0100 | [diff] [blame^] | 92 | * |
| 93 | * \warning SHA-1 is considered a weak message digest and its use |
| 94 | * constitutes a security risk. We recommend considering |
| 95 | * stronger message digests instead. |
| 96 | * |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 97 | */ |
| 98 | void sha1_free( sha1_context *ctx ); |
| 99 | |
| 100 | /** |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 101 | * \brief SHA-1 context setup |
| 102 | * |
| 103 | * \param ctx context to be initialized |
Hanno Becker | ce0c9db | 2017-09-28 15:39:45 +0100 | [diff] [blame^] | 104 | * |
| 105 | * \warning SHA-1 is considered a weak message digest and its use |
| 106 | * constitutes a security risk. We recommend considering |
| 107 | * stronger message digests instead. |
| 108 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 109 | */ |
| 110 | void sha1_starts( sha1_context *ctx ); |
| 111 | |
| 112 | /** |
| 113 | * \brief SHA-1 process buffer |
| 114 | * |
| 115 | * \param ctx SHA-1 context |
| 116 | * \param input buffer holding the data |
| 117 | * \param ilen length of the input data |
Hanno Becker | ce0c9db | 2017-09-28 15:39:45 +0100 | [diff] [blame^] | 118 | * |
| 119 | * \warning SHA-1 is considered a weak message digest and its use |
| 120 | * constitutes a security risk. We recommend considering |
| 121 | * stronger message digests instead. |
| 122 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 123 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 124 | void sha1_update( sha1_context *ctx, const unsigned char *input, size_t ilen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 125 | |
| 126 | /** |
| 127 | * \brief SHA-1 final digest |
| 128 | * |
| 129 | * \param ctx SHA-1 context |
| 130 | * \param output SHA-1 checksum result |
Hanno Becker | ce0c9db | 2017-09-28 15:39:45 +0100 | [diff] [blame^] | 131 | * |
| 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 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 136 | */ |
| 137 | void sha1_finish( sha1_context *ctx, unsigned char output[20] ); |
| 138 | |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 139 | /* Internal use */ |
| 140 | void sha1_process( sha1_context *ctx, const unsigned char data[64] ); |
| 141 | |
| 142 | #ifdef __cplusplus |
| 143 | } |
| 144 | #endif |
| 145 | |
| 146 | #else /* POLARSSL_SHA1_ALT */ |
| 147 | #include "sha1_alt.h" |
| 148 | #endif /* POLARSSL_SHA1_ALT */ |
| 149 | |
| 150 | #ifdef __cplusplus |
| 151 | extern "C" { |
| 152 | #endif |
| 153 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 154 | /** |
| 155 | * \brief Output = SHA-1( input buffer ) |
| 156 | * |
| 157 | * \param input buffer holding the data |
| 158 | * \param ilen length of the input data |
| 159 | * \param output SHA-1 checksum result |
Hanno Becker | ce0c9db | 2017-09-28 15:39:45 +0100 | [diff] [blame^] | 160 | * |
| 161 | * \warning SHA-1 is considered a weak message digest and its use |
| 162 | * constitutes a security risk. We recommend considering |
| 163 | * stronger message digests instead. |
| 164 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 165 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 166 | void sha1( const unsigned char *input, size_t ilen, unsigned char output[20] ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 167 | |
| 168 | /** |
| 169 | * \brief Output = SHA-1( file contents ) |
| 170 | * |
| 171 | * \param path input file name |
| 172 | * \param output SHA-1 checksum result |
| 173 | * |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 174 | * \return 0 if successful, or POLARSSL_ERR_SHA1_FILE_IO_ERROR |
Hanno Becker | ce0c9db | 2017-09-28 15:39:45 +0100 | [diff] [blame^] | 175 | * |
| 176 | * \warning SHA-1 is considered a weak message digest and its use |
| 177 | * constitutes a security risk. We recommend considering |
| 178 | * stronger message digests instead. |
| 179 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 180 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 181 | int sha1_file( const char *path, unsigned char output[20] ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 182 | |
| 183 | /** |
| 184 | * \brief SHA-1 HMAC context setup |
| 185 | * |
| 186 | * \param ctx HMAC context to be initialized |
| 187 | * \param key HMAC secret key |
| 188 | * \param keylen length of the HMAC key |
Hanno Becker | ce0c9db | 2017-09-28 15:39:45 +0100 | [diff] [blame^] | 189 | * |
| 190 | * \warning SHA-1 is considered a weak message digest and its use |
| 191 | * constitutes a security risk. We recommend considering |
| 192 | * stronger message digests instead. |
| 193 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 194 | */ |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 195 | void sha1_hmac_starts( sha1_context *ctx, const unsigned char *key, |
| 196 | size_t keylen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 197 | |
| 198 | /** |
| 199 | * \brief SHA-1 HMAC process buffer |
| 200 | * |
| 201 | * \param ctx HMAC context |
| 202 | * \param input buffer holding the data |
| 203 | * \param ilen length of the input data |
Hanno Becker | ce0c9db | 2017-09-28 15:39:45 +0100 | [diff] [blame^] | 204 | * |
| 205 | * \warning SHA-1 is considered a weak message digest and its use |
| 206 | * constitutes a security risk. We recommend considering |
| 207 | * stronger message digests instead. |
| 208 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 209 | */ |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 210 | void sha1_hmac_update( sha1_context *ctx, const unsigned char *input, |
| 211 | size_t ilen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 212 | |
| 213 | /** |
| 214 | * \brief SHA-1 HMAC final digest |
| 215 | * |
| 216 | * \param ctx HMAC context |
| 217 | * \param output SHA-1 HMAC checksum result |
Hanno Becker | ce0c9db | 2017-09-28 15:39:45 +0100 | [diff] [blame^] | 218 | * |
| 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 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 223 | */ |
| 224 | void sha1_hmac_finish( sha1_context *ctx, unsigned char output[20] ); |
| 225 | |
| 226 | /** |
Paul Bakker | 7d3b661 | 2010-03-21 16:23:13 +0000 | [diff] [blame] | 227 | * \brief SHA-1 HMAC context reset |
| 228 | * |
| 229 | * \param ctx HMAC context to be reset |
Hanno Becker | ce0c9db | 2017-09-28 15:39:45 +0100 | [diff] [blame^] | 230 | * |
| 231 | * \warning SHA-1 is considered a weak message digest and its use |
| 232 | * constitutes a security risk. We recommend considering |
| 233 | * stronger message digests instead. |
| 234 | * |
Paul Bakker | 7d3b661 | 2010-03-21 16:23:13 +0000 | [diff] [blame] | 235 | */ |
| 236 | void sha1_hmac_reset( sha1_context *ctx ); |
| 237 | |
| 238 | /** |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 239 | * \brief Output = HMAC-SHA-1( hmac key, input buffer ) |
| 240 | * |
| 241 | * \param key HMAC secret key |
| 242 | * \param keylen length of the HMAC key |
| 243 | * \param input buffer holding the data |
| 244 | * \param ilen length of the input data |
| 245 | * \param output HMAC-SHA-1 result |
Hanno Becker | ce0c9db | 2017-09-28 15:39:45 +0100 | [diff] [blame^] | 246 | * |
| 247 | * \warning SHA-1 is considered a weak message digest and its use |
| 248 | * constitutes a security risk. We recommend considering |
| 249 | * stronger message digests instead. |
| 250 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 251 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 252 | void sha1_hmac( const unsigned char *key, size_t keylen, |
| 253 | const unsigned char *input, size_t ilen, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 254 | unsigned char output[20] ); |
| 255 | |
| 256 | /** |
| 257 | * \brief Checkup routine |
| 258 | * |
| 259 | * \return 0 if successful, or 1 if the test failed |
Hanno Becker | ce0c9db | 2017-09-28 15:39:45 +0100 | [diff] [blame^] | 260 | * |
| 261 | * \warning SHA-1 is considered a weak message digest and its use |
| 262 | * constitutes a security risk. We recommend considering |
| 263 | * stronger message digests instead. |
| 264 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 265 | */ |
| 266 | int sha1_self_test( int verbose ); |
| 267 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 268 | #ifdef __cplusplus |
| 269 | } |
| 270 | #endif |
| 271 | |
| 272 | #endif /* sha1.h */ |