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