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