mbed TLS library
Dependents: HTTPClient-SSL WS_SERVER
include/polarssl/sha256.h@0:137634ff4186, 2015-06-11 (annotated)
- Committer:
- ansond
- Date:
- Thu Jun 11 03:27:03 2015 +0000
- Revision:
- 0:137634ff4186
initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ansond | 0:137634ff4186 | 1 | /** |
ansond | 0:137634ff4186 | 2 | * \file sha256.h |
ansond | 0:137634ff4186 | 3 | * |
ansond | 0:137634ff4186 | 4 | * \brief SHA-224 and SHA-256 cryptographic hash function |
ansond | 0:137634ff4186 | 5 | * |
ansond | 0:137634ff4186 | 6 | * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved |
ansond | 0:137634ff4186 | 7 | * |
ansond | 0:137634ff4186 | 8 | * This file is part of mbed TLS (https://tls.mbed.org) |
ansond | 0:137634ff4186 | 9 | * |
ansond | 0:137634ff4186 | 10 | * This program is free software; you can redistribute it and/or modify |
ansond | 0:137634ff4186 | 11 | * it under the terms of the GNU General Public License as published by |
ansond | 0:137634ff4186 | 12 | * the Free Software Foundation; either version 2 of the License, or |
ansond | 0:137634ff4186 | 13 | * (at your option) any later version. |
ansond | 0:137634ff4186 | 14 | * |
ansond | 0:137634ff4186 | 15 | * This program is distributed in the hope that it will be useful, |
ansond | 0:137634ff4186 | 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
ansond | 0:137634ff4186 | 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
ansond | 0:137634ff4186 | 18 | * GNU General Public License for more details. |
ansond | 0:137634ff4186 | 19 | * |
ansond | 0:137634ff4186 | 20 | * You should have received a copy of the GNU General Public License along |
ansond | 0:137634ff4186 | 21 | * with this program; if not, write to the Free Software Foundation, Inc., |
ansond | 0:137634ff4186 | 22 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
ansond | 0:137634ff4186 | 23 | */ |
ansond | 0:137634ff4186 | 24 | #ifndef POLARSSL_SHA256_H |
ansond | 0:137634ff4186 | 25 | #define POLARSSL_SHA256_H |
ansond | 0:137634ff4186 | 26 | |
ansond | 0:137634ff4186 | 27 | #if !defined(POLARSSL_CONFIG_FILE) |
ansond | 0:137634ff4186 | 28 | #include "config.h" |
ansond | 0:137634ff4186 | 29 | #else |
ansond | 0:137634ff4186 | 30 | #include POLARSSL_CONFIG_FILE |
ansond | 0:137634ff4186 | 31 | #endif |
ansond | 0:137634ff4186 | 32 | |
ansond | 0:137634ff4186 | 33 | #include <stddef.h> |
ansond | 0:137634ff4186 | 34 | |
ansond | 0:137634ff4186 | 35 | #if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32) |
ansond | 0:137634ff4186 | 36 | #include <basetsd.h> |
ansond | 0:137634ff4186 | 37 | typedef UINT32 uint32_t; |
ansond | 0:137634ff4186 | 38 | #else |
ansond | 0:137634ff4186 | 39 | #include <inttypes.h> |
ansond | 0:137634ff4186 | 40 | #endif |
ansond | 0:137634ff4186 | 41 | |
ansond | 0:137634ff4186 | 42 | #define POLARSSL_ERR_SHA256_FILE_IO_ERROR -0x0078 /**< Read/write error in file. */ |
ansond | 0:137634ff4186 | 43 | |
ansond | 0:137634ff4186 | 44 | #if !defined(POLARSSL_SHA256_ALT) |
ansond | 0:137634ff4186 | 45 | // Regular implementation |
ansond | 0:137634ff4186 | 46 | // |
ansond | 0:137634ff4186 | 47 | |
ansond | 0:137634ff4186 | 48 | #ifdef __cplusplus |
ansond | 0:137634ff4186 | 49 | extern "C" { |
ansond | 0:137634ff4186 | 50 | #endif |
ansond | 0:137634ff4186 | 51 | |
ansond | 0:137634ff4186 | 52 | /** |
ansond | 0:137634ff4186 | 53 | * \brief SHA-256 context structure |
ansond | 0:137634ff4186 | 54 | */ |
ansond | 0:137634ff4186 | 55 | typedef struct |
ansond | 0:137634ff4186 | 56 | { |
ansond | 0:137634ff4186 | 57 | uint32_t total[2]; /*!< number of bytes processed */ |
ansond | 0:137634ff4186 | 58 | uint32_t state[8]; /*!< intermediate digest state */ |
ansond | 0:137634ff4186 | 59 | unsigned char buffer[64]; /*!< data block being processed */ |
ansond | 0:137634ff4186 | 60 | |
ansond | 0:137634ff4186 | 61 | unsigned char ipad[64]; /*!< HMAC: inner padding */ |
ansond | 0:137634ff4186 | 62 | unsigned char opad[64]; /*!< HMAC: outer padding */ |
ansond | 0:137634ff4186 | 63 | int is224; /*!< 0 => SHA-256, else SHA-224 */ |
ansond | 0:137634ff4186 | 64 | } |
ansond | 0:137634ff4186 | 65 | sha256_context; |
ansond | 0:137634ff4186 | 66 | |
ansond | 0:137634ff4186 | 67 | /** |
ansond | 0:137634ff4186 | 68 | * \brief Initialize SHA-256 context |
ansond | 0:137634ff4186 | 69 | * |
ansond | 0:137634ff4186 | 70 | * \param ctx SHA-256 context to be initialized |
ansond | 0:137634ff4186 | 71 | */ |
ansond | 0:137634ff4186 | 72 | void sha256_init( sha256_context *ctx ); |
ansond | 0:137634ff4186 | 73 | |
ansond | 0:137634ff4186 | 74 | /** |
ansond | 0:137634ff4186 | 75 | * \brief Clear SHA-256 context |
ansond | 0:137634ff4186 | 76 | * |
ansond | 0:137634ff4186 | 77 | * \param ctx SHA-256 context to be cleared |
ansond | 0:137634ff4186 | 78 | */ |
ansond | 0:137634ff4186 | 79 | void sha256_free( sha256_context *ctx ); |
ansond | 0:137634ff4186 | 80 | |
ansond | 0:137634ff4186 | 81 | /** |
ansond | 0:137634ff4186 | 82 | * \brief SHA-256 context setup |
ansond | 0:137634ff4186 | 83 | * |
ansond | 0:137634ff4186 | 84 | * \param ctx context to be initialized |
ansond | 0:137634ff4186 | 85 | * \param is224 0 = use SHA256, 1 = use SHA224 |
ansond | 0:137634ff4186 | 86 | */ |
ansond | 0:137634ff4186 | 87 | void sha256_starts( sha256_context *ctx, int is224 ); |
ansond | 0:137634ff4186 | 88 | |
ansond | 0:137634ff4186 | 89 | /** |
ansond | 0:137634ff4186 | 90 | * \brief SHA-256 process buffer |
ansond | 0:137634ff4186 | 91 | * |
ansond | 0:137634ff4186 | 92 | * \param ctx SHA-256 context |
ansond | 0:137634ff4186 | 93 | * \param input buffer holding the data |
ansond | 0:137634ff4186 | 94 | * \param ilen length of the input data |
ansond | 0:137634ff4186 | 95 | */ |
ansond | 0:137634ff4186 | 96 | void sha256_update( sha256_context *ctx, const unsigned char *input, |
ansond | 0:137634ff4186 | 97 | size_t ilen ); |
ansond | 0:137634ff4186 | 98 | |
ansond | 0:137634ff4186 | 99 | /** |
ansond | 0:137634ff4186 | 100 | * \brief SHA-256 final digest |
ansond | 0:137634ff4186 | 101 | * |
ansond | 0:137634ff4186 | 102 | * \param ctx SHA-256 context |
ansond | 0:137634ff4186 | 103 | * \param output SHA-224/256 checksum result |
ansond | 0:137634ff4186 | 104 | */ |
ansond | 0:137634ff4186 | 105 | void sha256_finish( sha256_context *ctx, unsigned char output[32] ); |
ansond | 0:137634ff4186 | 106 | |
ansond | 0:137634ff4186 | 107 | /* Internal use */ |
ansond | 0:137634ff4186 | 108 | void sha256_process( sha256_context *ctx, const unsigned char data[64] ); |
ansond | 0:137634ff4186 | 109 | |
ansond | 0:137634ff4186 | 110 | #ifdef __cplusplus |
ansond | 0:137634ff4186 | 111 | } |
ansond | 0:137634ff4186 | 112 | #endif |
ansond | 0:137634ff4186 | 113 | |
ansond | 0:137634ff4186 | 114 | #else /* POLARSSL_SHA256_ALT */ |
ansond | 0:137634ff4186 | 115 | #include "sha256_alt.h" |
ansond | 0:137634ff4186 | 116 | #endif /* POLARSSL_SHA256_ALT */ |
ansond | 0:137634ff4186 | 117 | |
ansond | 0:137634ff4186 | 118 | #ifdef __cplusplus |
ansond | 0:137634ff4186 | 119 | extern "C" { |
ansond | 0:137634ff4186 | 120 | #endif |
ansond | 0:137634ff4186 | 121 | |
ansond | 0:137634ff4186 | 122 | /** |
ansond | 0:137634ff4186 | 123 | * \brief Output = SHA-256( input buffer ) |
ansond | 0:137634ff4186 | 124 | * |
ansond | 0:137634ff4186 | 125 | * \param input buffer holding the data |
ansond | 0:137634ff4186 | 126 | * \param ilen length of the input data |
ansond | 0:137634ff4186 | 127 | * \param output SHA-224/256 checksum result |
ansond | 0:137634ff4186 | 128 | * \param is224 0 = use SHA256, 1 = use SHA224 |
ansond | 0:137634ff4186 | 129 | */ |
ansond | 0:137634ff4186 | 130 | void sha256( const unsigned char *input, size_t ilen, |
ansond | 0:137634ff4186 | 131 | unsigned char output[32], int is224 ); |
ansond | 0:137634ff4186 | 132 | |
ansond | 0:137634ff4186 | 133 | /** |
ansond | 0:137634ff4186 | 134 | * \brief Output = SHA-256( file contents ) |
ansond | 0:137634ff4186 | 135 | * |
ansond | 0:137634ff4186 | 136 | * \param path input file name |
ansond | 0:137634ff4186 | 137 | * \param output SHA-224/256 checksum result |
ansond | 0:137634ff4186 | 138 | * \param is224 0 = use SHA256, 1 = use SHA224 |
ansond | 0:137634ff4186 | 139 | * |
ansond | 0:137634ff4186 | 140 | * \return 0 if successful, or POLARSSL_ERR_SHA256_FILE_IO_ERROR |
ansond | 0:137634ff4186 | 141 | */ |
ansond | 0:137634ff4186 | 142 | int sha256_file( const char *path, unsigned char output[32], int is224 ); |
ansond | 0:137634ff4186 | 143 | |
ansond | 0:137634ff4186 | 144 | /** |
ansond | 0:137634ff4186 | 145 | * \brief SHA-256 HMAC context setup |
ansond | 0:137634ff4186 | 146 | * |
ansond | 0:137634ff4186 | 147 | * \param ctx HMAC context to be initialized |
ansond | 0:137634ff4186 | 148 | * \param key HMAC secret key |
ansond | 0:137634ff4186 | 149 | * \param keylen length of the HMAC key |
ansond | 0:137634ff4186 | 150 | * \param is224 0 = use SHA256, 1 = use SHA224 |
ansond | 0:137634ff4186 | 151 | */ |
ansond | 0:137634ff4186 | 152 | void sha256_hmac_starts( sha256_context *ctx, const unsigned char *key, |
ansond | 0:137634ff4186 | 153 | size_t keylen, int is224 ); |
ansond | 0:137634ff4186 | 154 | |
ansond | 0:137634ff4186 | 155 | /** |
ansond | 0:137634ff4186 | 156 | * \brief SHA-256 HMAC process buffer |
ansond | 0:137634ff4186 | 157 | * |
ansond | 0:137634ff4186 | 158 | * \param ctx HMAC context |
ansond | 0:137634ff4186 | 159 | * \param input buffer holding the data |
ansond | 0:137634ff4186 | 160 | * \param ilen length of the input data |
ansond | 0:137634ff4186 | 161 | */ |
ansond | 0:137634ff4186 | 162 | void sha256_hmac_update( sha256_context *ctx, const unsigned char *input, |
ansond | 0:137634ff4186 | 163 | size_t ilen ); |
ansond | 0:137634ff4186 | 164 | |
ansond | 0:137634ff4186 | 165 | /** |
ansond | 0:137634ff4186 | 166 | * \brief SHA-256 HMAC final digest |
ansond | 0:137634ff4186 | 167 | * |
ansond | 0:137634ff4186 | 168 | * \param ctx HMAC context |
ansond | 0:137634ff4186 | 169 | * \param output SHA-224/256 HMAC checksum result |
ansond | 0:137634ff4186 | 170 | */ |
ansond | 0:137634ff4186 | 171 | void sha256_hmac_finish( sha256_context *ctx, unsigned char output[32] ); |
ansond | 0:137634ff4186 | 172 | |
ansond | 0:137634ff4186 | 173 | /** |
ansond | 0:137634ff4186 | 174 | * \brief SHA-256 HMAC context reset |
ansond | 0:137634ff4186 | 175 | * |
ansond | 0:137634ff4186 | 176 | * \param ctx HMAC context to be reset |
ansond | 0:137634ff4186 | 177 | */ |
ansond | 0:137634ff4186 | 178 | void sha256_hmac_reset( sha256_context *ctx ); |
ansond | 0:137634ff4186 | 179 | |
ansond | 0:137634ff4186 | 180 | /** |
ansond | 0:137634ff4186 | 181 | * \brief Output = HMAC-SHA-256( hmac key, input buffer ) |
ansond | 0:137634ff4186 | 182 | * |
ansond | 0:137634ff4186 | 183 | * \param key HMAC secret key |
ansond | 0:137634ff4186 | 184 | * \param keylen length of the HMAC key |
ansond | 0:137634ff4186 | 185 | * \param input buffer holding the data |
ansond | 0:137634ff4186 | 186 | * \param ilen length of the input data |
ansond | 0:137634ff4186 | 187 | * \param output HMAC-SHA-224/256 result |
ansond | 0:137634ff4186 | 188 | * \param is224 0 = use SHA256, 1 = use SHA224 |
ansond | 0:137634ff4186 | 189 | */ |
ansond | 0:137634ff4186 | 190 | void sha256_hmac( const unsigned char *key, size_t keylen, |
ansond | 0:137634ff4186 | 191 | const unsigned char *input, size_t ilen, |
ansond | 0:137634ff4186 | 192 | unsigned char output[32], int is224 ); |
ansond | 0:137634ff4186 | 193 | |
ansond | 0:137634ff4186 | 194 | /** |
ansond | 0:137634ff4186 | 195 | * \brief Checkup routine |
ansond | 0:137634ff4186 | 196 | * |
ansond | 0:137634ff4186 | 197 | * \return 0 if successful, or 1 if the test failed |
ansond | 0:137634ff4186 | 198 | */ |
ansond | 0:137634ff4186 | 199 | int sha256_self_test( int verbose ); |
ansond | 0:137634ff4186 | 200 | |
ansond | 0:137634ff4186 | 201 | #ifdef __cplusplus |
ansond | 0:137634ff4186 | 202 | } |
ansond | 0:137634ff4186 | 203 | #endif |
ansond | 0:137634ff4186 | 204 | |
ansond | 0:137634ff4186 | 205 | #endif /* sha256.h */ |
ansond | 0:137634ff4186 | 206 |