Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file sha2.h |
| 3 | */ |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame^] | 4 | #ifndef POLARSSL_SHA2_H |
| 5 | #define POLARSSL_SHA2_H |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6 | |
| 7 | /** |
| 8 | * \brief SHA-256 context structure |
| 9 | */ |
| 10 | typedef struct |
| 11 | { |
| 12 | unsigned long total[2]; /*!< number of bytes processed */ |
| 13 | unsigned long state[8]; /*!< intermediate digest state */ |
| 14 | unsigned char buffer[64]; /*!< data block being processed */ |
| 15 | |
| 16 | unsigned char ipad[64]; /*!< HMAC: inner padding */ |
| 17 | unsigned char opad[64]; /*!< HMAC: outer padding */ |
| 18 | int is224; /*!< 0 => SHA-256, else SHA-224 */ |
| 19 | } |
| 20 | sha2_context; |
| 21 | |
| 22 | #ifdef __cplusplus |
| 23 | extern "C" { |
| 24 | #endif |
| 25 | |
| 26 | /** |
| 27 | * \brief SHA-256 context setup |
| 28 | * |
| 29 | * \param ctx context to be initialized |
| 30 | * \param is224 0 = use SHA256, 1 = use SHA224 |
| 31 | */ |
| 32 | void sha2_starts( sha2_context *ctx, int is224 ); |
| 33 | |
| 34 | /** |
| 35 | * \brief SHA-256 process buffer |
| 36 | * |
| 37 | * \param ctx SHA-256 context |
| 38 | * \param input buffer holding the data |
| 39 | * \param ilen length of the input data |
| 40 | */ |
| 41 | void sha2_update( sha2_context *ctx, unsigned char *input, int ilen ); |
| 42 | |
| 43 | /** |
| 44 | * \brief SHA-256 final digest |
| 45 | * |
| 46 | * \param ctx SHA-256 context |
| 47 | * \param output SHA-224/256 checksum result |
| 48 | */ |
| 49 | void sha2_finish( sha2_context *ctx, unsigned char output[32] ); |
| 50 | |
| 51 | /** |
| 52 | * \brief Output = SHA-256( input buffer ) |
| 53 | * |
| 54 | * \param input buffer holding the data |
| 55 | * \param ilen length of the input data |
| 56 | * \param output SHA-224/256 checksum result |
| 57 | * \param is224 0 = use SHA256, 1 = use SHA224 |
| 58 | */ |
| 59 | void sha2( unsigned char *input, int ilen, |
| 60 | unsigned char output[32], int is224 ); |
| 61 | |
| 62 | /** |
| 63 | * \brief Output = SHA-256( file contents ) |
| 64 | * |
| 65 | * \param path input file name |
| 66 | * \param output SHA-224/256 checksum result |
| 67 | * \param is224 0 = use SHA256, 1 = use SHA224 |
| 68 | * |
| 69 | * \return 0 if successful, 1 if fopen failed, |
| 70 | * or 2 if fread failed |
| 71 | */ |
| 72 | int sha2_file( char *path, unsigned char output[32], int is224 ); |
| 73 | |
| 74 | /** |
| 75 | * \brief SHA-256 HMAC context setup |
| 76 | * |
| 77 | * \param ctx HMAC context to be initialized |
| 78 | * \param key HMAC secret key |
| 79 | * \param keylen length of the HMAC key |
| 80 | * \param is224 0 = use SHA256, 1 = use SHA224 |
| 81 | */ |
| 82 | void sha2_hmac_starts( sha2_context *ctx, unsigned char *key, int keylen, |
| 83 | int is224 ); |
| 84 | |
| 85 | /** |
| 86 | * \brief SHA-256 HMAC process buffer |
| 87 | * |
| 88 | * \param ctx HMAC context |
| 89 | * \param input buffer holding the data |
| 90 | * \param ilen length of the input data |
| 91 | */ |
| 92 | void sha2_hmac_update( sha2_context *ctx, unsigned char *input, int ilen ); |
| 93 | |
| 94 | /** |
| 95 | * \brief SHA-256 HMAC final digest |
| 96 | * |
| 97 | * \param ctx HMAC context |
| 98 | * \param output SHA-224/256 HMAC checksum result |
| 99 | */ |
| 100 | void sha2_hmac_finish( sha2_context *ctx, unsigned char output[32] ); |
| 101 | |
| 102 | /** |
| 103 | * \brief Output = HMAC-SHA-256( hmac key, input buffer ) |
| 104 | * |
| 105 | * \param key HMAC secret key |
| 106 | * \param keylen length of the HMAC key |
| 107 | * \param input buffer holding the data |
| 108 | * \param ilen length of the input data |
| 109 | * \param output HMAC-SHA-224/256 result |
| 110 | * \param is224 0 = use SHA256, 1 = use SHA224 |
| 111 | */ |
| 112 | void sha2_hmac( unsigned char *key, int keylen, |
| 113 | unsigned char *input, int ilen, |
| 114 | unsigned char output[32], int is224 ); |
| 115 | |
| 116 | /** |
| 117 | * \brief Checkup routine |
| 118 | * |
| 119 | * \return 0 if successful, or 1 if the test failed |
| 120 | */ |
| 121 | int sha2_self_test( int verbose ); |
| 122 | |
| 123 | #ifdef __cplusplus |
| 124 | } |
| 125 | #endif |
| 126 | |
| 127 | #endif /* sha2.h */ |