Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
sha256.h
00001 /** 00002 * \file sha256.h 00003 * 00004 * \brief SHA-224 and SHA-256 cryptographic hash function 00005 * 00006 * Copyright (C) 2006-2014, Brainspark B.V. 00007 * 00008 * This file is part of PolarSSL (http://www.polarssl.org) 00009 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> 00010 * 00011 * All rights reserved. 00012 * 00013 * This program is free software; you can redistribute it and/or modify 00014 * it under the terms of the GNU General Public License as published by 00015 * the Free Software Foundation; either version 2 of the License, or 00016 * (at your option) any later version. 00017 * 00018 * This program is distributed in the hope that it will be useful, 00019 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00020 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00021 * GNU General Public License for more details. 00022 * 00023 * You should have received a copy of the GNU General Public License along 00024 * with this program; if not, write to the Free Software Foundation, Inc., 00025 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 00026 */ 00027 #ifndef POLARSSL_SHA256_H 00028 #define POLARSSL_SHA256_H 00029 00030 #if !defined(POLARSSL_CONFIG_FILE) 00031 #include "config.h" 00032 #else 00033 #include POLARSSL_CONFIG_FILE 00034 #endif 00035 00036 #include <string.h> 00037 00038 #if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32) 00039 #include <basetsd.h> 00040 typedef UINT32 uint32_t; 00041 #else 00042 #include <inttypes.h> 00043 #endif 00044 00045 #define POLARSSL_ERR_SHA256_FILE_IO_ERROR -0x0078 /**< Read/write error in file. */ 00046 00047 #if !defined(POLARSSL_SHA256_ALT) 00048 // Regular implementation 00049 // 00050 00051 #ifdef __cplusplus 00052 extern "C" { 00053 #endif 00054 00055 /** 00056 * \brief SHA-256 context structure 00057 */ 00058 typedef struct 00059 { 00060 uint32_t total[2]; /*!< number of bytes processed */ 00061 uint32_t state[8]; /*!< intermediate digest state */ 00062 unsigned char buffer[64]; /*!< data block being processed */ 00063 00064 unsigned char ipad[64]; /*!< HMAC: inner padding */ 00065 unsigned char opad[64]; /*!< HMAC: outer padding */ 00066 int is224 ; /*!< 0 => SHA-256, else SHA-224 */ 00067 } 00068 sha256_context; 00069 00070 /** 00071 * \brief SHA-256 context setup 00072 * 00073 * \param ctx context to be initialized 00074 * \param is224 0 = use SHA256, 1 = use SHA224 00075 */ 00076 void sha256_starts( sha256_context *ctx, int is224 ); 00077 00078 /** 00079 * \brief SHA-256 process buffer 00080 * 00081 * \param ctx SHA-256 context 00082 * \param input buffer holding the data 00083 * \param ilen length of the input data 00084 */ 00085 void sha256_update( sha256_context *ctx, const unsigned char *input, 00086 size_t ilen ); 00087 00088 /** 00089 * \brief SHA-256 final digest 00090 * 00091 * \param ctx SHA-256 context 00092 * \param output SHA-224/256 checksum result 00093 */ 00094 void sha256_finish( sha256_context *ctx, unsigned char output[32] ); 00095 00096 /* Internal use */ 00097 void sha256_process( sha256_context *ctx, const unsigned char data[64] ); 00098 00099 #ifdef __cplusplus 00100 } 00101 #endif 00102 00103 #else /* POLARSSL_SHA256_ALT */ 00104 #include "sha256_alt.h" 00105 #endif /* POLARSSL_SHA256_ALT */ 00106 00107 #ifdef __cplusplus 00108 extern "C" { 00109 #endif 00110 00111 /** 00112 * \brief Output = SHA-256( input buffer ) 00113 * 00114 * \param input buffer holding the data 00115 * \param ilen length of the input data 00116 * \param output SHA-224/256 checksum result 00117 * \param is224 0 = use SHA256, 1 = use SHA224 00118 */ 00119 void sha256( const unsigned char *input, size_t ilen, 00120 unsigned char output[32], int is224 ); 00121 00122 /** 00123 * \brief Output = SHA-256( file contents ) 00124 * 00125 * \param path input file name 00126 * \param output SHA-224/256 checksum result 00127 * \param is224 0 = use SHA256, 1 = use SHA224 00128 * 00129 * \return 0 if successful, or POLARSSL_ERR_SHA256_FILE_IO_ERROR 00130 */ 00131 int sha256_file( const char *path, unsigned char output[32], int is224 ); 00132 00133 /** 00134 * \brief SHA-256 HMAC context setup 00135 * 00136 * \param ctx HMAC context to be initialized 00137 * \param key HMAC secret key 00138 * \param keylen length of the HMAC key 00139 * \param is224 0 = use SHA256, 1 = use SHA224 00140 */ 00141 void sha256_hmac_starts( sha256_context *ctx, const unsigned char *key, 00142 size_t keylen, int is224 ); 00143 00144 /** 00145 * \brief SHA-256 HMAC process buffer 00146 * 00147 * \param ctx HMAC context 00148 * \param input buffer holding the data 00149 * \param ilen length of the input data 00150 */ 00151 void sha256_hmac_update( sha256_context *ctx, const unsigned char *input, 00152 size_t ilen ); 00153 00154 /** 00155 * \brief SHA-256 HMAC final digest 00156 * 00157 * \param ctx HMAC context 00158 * \param output SHA-224/256 HMAC checksum result 00159 */ 00160 void sha256_hmac_finish( sha256_context *ctx, unsigned char output[32] ); 00161 00162 /** 00163 * \brief SHA-256 HMAC context reset 00164 * 00165 * \param ctx HMAC context to be reset 00166 */ 00167 void sha256_hmac_reset( sha256_context *ctx ); 00168 00169 /** 00170 * \brief Output = HMAC-SHA-256( hmac key, input buffer ) 00171 * 00172 * \param key HMAC secret key 00173 * \param keylen length of the HMAC key 00174 * \param input buffer holding the data 00175 * \param ilen length of the input data 00176 * \param output HMAC-SHA-224/256 result 00177 * \param is224 0 = use SHA256, 1 = use SHA224 00178 */ 00179 void sha256_hmac( const unsigned char *key, size_t keylen, 00180 const unsigned char *input, size_t ilen, 00181 unsigned char output[32], int is224 ); 00182 00183 /** 00184 * \brief Checkup routine 00185 * 00186 * \return 0 if successful, or 1 if the test failed 00187 */ 00188 int sha256_self_test( int verbose ); 00189 00190 #ifdef __cplusplus 00191 } 00192 #endif 00193 00194 #endif /* sha256.h */ 00195 00196
Generated on Tue Jul 12 2022 19:40:20 by
1.7.2