mbed TLS library

Dependents:   HTTPClient-SSL WS_SERVER

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers sha256.h Source File

sha256.h

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