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