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 | 0edee5e | 2015-01-26 15:29:40 +0000 | [diff] [blame^] | 6 | * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 7 | * |
Manuel Pégourié-Gonnard | 0edee5e | 2015-01-26 15:29:40 +0000 | [diff] [blame^] | 8 | * This file is part of mbed TLS (https://www.polarssl.org) |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +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. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 23 | */ |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 24 | #ifndef POLARSSL_SHA1_H |
| 25 | #define POLARSSL_SHA1_H |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 26 | |
Paul Bakker | 4087c47 | 2013-06-12 16:49:10 +0200 | [diff] [blame] | 27 | #include "config.h" |
| 28 | |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 29 | #include <string.h> |
| 30 | |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 31 | #ifdef _MSC_VER |
| 32 | #include <basetsd.h> |
| 33 | typedef UINT32 uint32_t; |
| 34 | #else |
| 35 | #include <inttypes.h> |
| 36 | #endif |
| 37 | |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 38 | #define POLARSSL_ERR_SHA1_FILE_IO_ERROR -0x0076 /**< Read/write error in file. */ |
| 39 | |
Paul Bakker | 4087c47 | 2013-06-12 16:49:10 +0200 | [diff] [blame] | 40 | #if !defined(POLARSSL_SHA1_ALT) |
| 41 | // Regular implementation |
| 42 | // |
| 43 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 44 | /** |
| 45 | * \brief SHA-1 context structure |
| 46 | */ |
| 47 | typedef struct |
| 48 | { |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 49 | uint32_t total[2]; /*!< number of bytes processed */ |
| 50 | uint32_t state[5]; /*!< intermediate digest state */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 51 | unsigned char buffer[64]; /*!< data block being processed */ |
| 52 | |
| 53 | unsigned char ipad[64]; /*!< HMAC: inner padding */ |
| 54 | unsigned char opad[64]; /*!< HMAC: outer padding */ |
| 55 | } |
| 56 | sha1_context; |
| 57 | |
| 58 | #ifdef __cplusplus |
| 59 | extern "C" { |
| 60 | #endif |
| 61 | |
| 62 | /** |
| 63 | * \brief SHA-1 context setup |
| 64 | * |
| 65 | * \param ctx context to be initialized |
| 66 | */ |
| 67 | void sha1_starts( sha1_context *ctx ); |
| 68 | |
| 69 | /** |
| 70 | * \brief SHA-1 process buffer |
| 71 | * |
| 72 | * \param ctx SHA-1 context |
| 73 | * \param input buffer holding the data |
| 74 | * \param ilen length of the input data |
| 75 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 76 | 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] | 77 | |
| 78 | /** |
| 79 | * \brief SHA-1 final digest |
| 80 | * |
| 81 | * \param ctx SHA-1 context |
| 82 | * \param output SHA-1 checksum result |
| 83 | */ |
| 84 | void sha1_finish( sha1_context *ctx, unsigned char output[20] ); |
| 85 | |
Paul Bakker | 4087c47 | 2013-06-12 16:49:10 +0200 | [diff] [blame] | 86 | /* Internal use */ |
| 87 | void sha1_process( sha1_context *ctx, const unsigned char data[64] ); |
| 88 | |
| 89 | #ifdef __cplusplus |
| 90 | } |
| 91 | #endif |
| 92 | |
| 93 | #else /* POLARSSL_SHA1_ALT */ |
| 94 | #include "sha1_alt.h" |
| 95 | #endif /* POLARSSL_SHA1_ALT */ |
| 96 | |
| 97 | #ifdef __cplusplus |
| 98 | extern "C" { |
| 99 | #endif |
| 100 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 101 | /** |
| 102 | * \brief Output = SHA-1( input buffer ) |
| 103 | * |
| 104 | * \param input buffer holding the data |
| 105 | * \param ilen length of the input data |
| 106 | * \param output SHA-1 checksum result |
| 107 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 108 | 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] | 109 | |
| 110 | /** |
| 111 | * \brief Output = SHA-1( file contents ) |
| 112 | * |
| 113 | * \param path input file name |
| 114 | * \param output SHA-1 checksum result |
| 115 | * |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 116 | * \return 0 if successful, or POLARSSL_ERR_SHA1_FILE_IO_ERROR |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 117 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 118 | int sha1_file( const char *path, unsigned char output[20] ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 119 | |
| 120 | /** |
| 121 | * \brief SHA-1 HMAC context setup |
| 122 | * |
| 123 | * \param ctx HMAC context to be initialized |
| 124 | * \param key HMAC secret key |
| 125 | * \param keylen length of the HMAC key |
| 126 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 127 | void sha1_hmac_starts( sha1_context *ctx, const unsigned char *key, size_t keylen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 128 | |
| 129 | /** |
| 130 | * \brief SHA-1 HMAC process buffer |
| 131 | * |
| 132 | * \param ctx HMAC context |
| 133 | * \param input buffer holding the data |
| 134 | * \param ilen length of the input data |
| 135 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 136 | void sha1_hmac_update( sha1_context *ctx, const unsigned char *input, size_t ilen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 137 | |
| 138 | /** |
| 139 | * \brief SHA-1 HMAC final digest |
| 140 | * |
| 141 | * \param ctx HMAC context |
| 142 | * \param output SHA-1 HMAC checksum result |
| 143 | */ |
| 144 | void sha1_hmac_finish( sha1_context *ctx, unsigned char output[20] ); |
| 145 | |
| 146 | /** |
Paul Bakker | 7d3b661 | 2010-03-21 16:23:13 +0000 | [diff] [blame] | 147 | * \brief SHA-1 HMAC context reset |
| 148 | * |
| 149 | * \param ctx HMAC context to be reset |
| 150 | */ |
| 151 | void sha1_hmac_reset( sha1_context *ctx ); |
| 152 | |
| 153 | /** |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 154 | * \brief Output = HMAC-SHA-1( hmac key, input buffer ) |
| 155 | * |
| 156 | * \param key HMAC secret key |
| 157 | * \param keylen length of the HMAC key |
| 158 | * \param input buffer holding the data |
| 159 | * \param ilen length of the input data |
| 160 | * \param output HMAC-SHA-1 result |
| 161 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 162 | void sha1_hmac( const unsigned char *key, size_t keylen, |
| 163 | const unsigned char *input, size_t ilen, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 164 | unsigned char output[20] ); |
| 165 | |
| 166 | /** |
| 167 | * \brief Checkup routine |
| 168 | * |
| 169 | * \return 0 if successful, or 1 if the test failed |
| 170 | */ |
| 171 | int sha1_self_test( int verbose ); |
| 172 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 173 | #ifdef __cplusplus |
| 174 | } |
| 175 | #endif |
| 176 | |
| 177 | #endif /* sha1.h */ |