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 | * |
Paul Bakker | 84f12b7 | 2010-07-18 10:13:04 +0000 | [diff] [blame] | 6 | * Copyright (C) 2006-2010, Brainspark B.V. |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 7 | * |
| 8 | * This file is part of PolarSSL (http://www.polarssl.org) |
Paul Bakker | 84f12b7 | 2010-07-18 10:13:04 +0000 | [diff] [blame] | 9 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 10 | * |
Paul Bakker | 77b385e | 2009-07-28 17:23:11 +0000 | [diff] [blame] | 11 | * All rights reserved. |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 12 | * |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 13 | * This program is free software; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU General Public License as published by |
| 15 | * the Free Software Foundation; either version 2 of the License, or |
| 16 | * (at your option) any later version. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | * GNU General Public License for more details. |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public License along |
| 24 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 25 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 26 | */ |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 27 | #ifndef POLARSSL_SHA1_H |
| 28 | #define POLARSSL_SHA1_H |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * \brief SHA-1 context structure |
| 32 | */ |
| 33 | typedef struct |
| 34 | { |
| 35 | unsigned long total[2]; /*!< number of bytes processed */ |
| 36 | unsigned long state[5]; /*!< intermediate digest state */ |
| 37 | unsigned char buffer[64]; /*!< data block being processed */ |
| 38 | |
| 39 | unsigned char ipad[64]; /*!< HMAC: inner padding */ |
| 40 | unsigned char opad[64]; /*!< HMAC: outer padding */ |
| 41 | } |
| 42 | sha1_context; |
| 43 | |
| 44 | #ifdef __cplusplus |
| 45 | extern "C" { |
| 46 | #endif |
| 47 | |
| 48 | /** |
| 49 | * \brief SHA-1 context setup |
| 50 | * |
| 51 | * \param ctx context to be initialized |
| 52 | */ |
| 53 | void sha1_starts( sha1_context *ctx ); |
| 54 | |
| 55 | /** |
| 56 | * \brief SHA-1 process buffer |
| 57 | * |
| 58 | * \param ctx SHA-1 context |
| 59 | * \param input buffer holding the data |
| 60 | * \param ilen length of the input data |
| 61 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 62 | void sha1_update( sha1_context *ctx, const unsigned char *input, int ilen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 63 | |
| 64 | /** |
| 65 | * \brief SHA-1 final digest |
| 66 | * |
| 67 | * \param ctx SHA-1 context |
| 68 | * \param output SHA-1 checksum result |
| 69 | */ |
| 70 | void sha1_finish( sha1_context *ctx, unsigned char output[20] ); |
| 71 | |
| 72 | /** |
| 73 | * \brief Output = SHA-1( input buffer ) |
| 74 | * |
| 75 | * \param input buffer holding the data |
| 76 | * \param ilen length of the input data |
| 77 | * \param output SHA-1 checksum result |
| 78 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 79 | void sha1( const unsigned char *input, int ilen, unsigned char output[20] ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 80 | |
| 81 | /** |
| 82 | * \brief Output = SHA-1( file contents ) |
| 83 | * |
| 84 | * \param path input file name |
| 85 | * \param output SHA-1 checksum result |
| 86 | * |
| 87 | * \return 0 if successful, 1 if fopen failed, |
| 88 | * or 2 if fread failed |
| 89 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 90 | int sha1_file( const char *path, unsigned char output[20] ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 91 | |
| 92 | /** |
| 93 | * \brief SHA-1 HMAC context setup |
| 94 | * |
| 95 | * \param ctx HMAC context to be initialized |
| 96 | * \param key HMAC secret key |
| 97 | * \param keylen length of the HMAC key |
| 98 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 99 | void sha1_hmac_starts( sha1_context *ctx, const unsigned char *key, int keylen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 100 | |
| 101 | /** |
| 102 | * \brief SHA-1 HMAC process buffer |
| 103 | * |
| 104 | * \param ctx HMAC context |
| 105 | * \param input buffer holding the data |
| 106 | * \param ilen length of the input data |
| 107 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 108 | void sha1_hmac_update( sha1_context *ctx, const unsigned char *input, int ilen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 109 | |
| 110 | /** |
| 111 | * \brief SHA-1 HMAC final digest |
| 112 | * |
| 113 | * \param ctx HMAC context |
| 114 | * \param output SHA-1 HMAC checksum result |
| 115 | */ |
| 116 | void sha1_hmac_finish( sha1_context *ctx, unsigned char output[20] ); |
| 117 | |
| 118 | /** |
Paul Bakker | 7d3b661 | 2010-03-21 16:23:13 +0000 | [diff] [blame] | 119 | * \brief SHA-1 HMAC context reset |
| 120 | * |
| 121 | * \param ctx HMAC context to be reset |
| 122 | */ |
| 123 | void sha1_hmac_reset( sha1_context *ctx ); |
| 124 | |
| 125 | /** |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 126 | * \brief Output = HMAC-SHA-1( hmac key, input buffer ) |
| 127 | * |
| 128 | * \param key HMAC secret key |
| 129 | * \param keylen length of the HMAC key |
| 130 | * \param input buffer holding the data |
| 131 | * \param ilen length of the input data |
| 132 | * \param output HMAC-SHA-1 result |
| 133 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 134 | void sha1_hmac( const unsigned char *key, int keylen, |
| 135 | const unsigned char *input, int ilen, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 136 | unsigned char output[20] ); |
| 137 | |
| 138 | /** |
| 139 | * \brief Checkup routine |
| 140 | * |
| 141 | * \return 0 if successful, or 1 if the test failed |
| 142 | */ |
| 143 | int sha1_self_test( int verbose ); |
| 144 | |
| 145 | #ifdef __cplusplus |
| 146 | } |
| 147 | #endif |
| 148 | |
| 149 | #endif /* sha1.h */ |