Example program to test AES-GCM functionality. Used for a workshop

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers sha512.h Source File

sha512.h

Go to the documentation of this file.
00001 /**
00002  * \file sha512.h
00003  *
00004  * \brief SHA-384 and SHA-512 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_SHA512_H
00028 #define POLARSSL_SHA512_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(__WATCOMC__)
00039   #define UL64(x) x##ui64
00040   typedef unsigned __int64 uint64_t;
00041 #else
00042   #include <inttypes.h>
00043   #define UL64(x) x##ULL
00044 #endif
00045 
00046 #define POLARSSL_ERR_SHA512_FILE_IO_ERROR              -0x007A  /**< Read/write error in file. */
00047 
00048 #if !defined(POLARSSL_SHA512_ALT)
00049 // Regular implementation
00050 //
00051 
00052 #ifdef __cplusplus
00053 extern "C" {
00054 #endif
00055 
00056 /**
00057  * \brief          SHA-512 context structure
00058  */
00059 typedef struct
00060 {
00061     uint64_t total[2];          /*!< number of bytes processed  */
00062     uint64_t state[8];          /*!< intermediate digest state  */
00063     unsigned char buffer[128];  /*!< data block being processed */
00064 
00065     unsigned char ipad[128];    /*!< HMAC: inner padding        */
00066     unsigned char opad[128];    /*!< HMAC: outer padding        */
00067     int is384 ;                  /*!< 0 => SHA-512, else SHA-384 */
00068 }
00069 sha512_context;
00070 
00071 /**
00072  * \brief          SHA-512 context setup
00073  *
00074  * \param ctx      context to be initialized
00075  * \param is384    0 = use SHA512, 1 = use SHA384
00076  */
00077 void sha512_starts( sha512_context *ctx, int is384 );
00078 
00079 /**
00080  * \brief          SHA-512 process buffer
00081  *
00082  * \param ctx      SHA-512 context
00083  * \param input    buffer holding the  data
00084  * \param ilen     length of the input data
00085  */
00086 void sha512_update( sha512_context *ctx, const unsigned char *input,
00087                     size_t ilen );
00088 
00089 /**
00090  * \brief          SHA-512 final digest
00091  *
00092  * \param ctx      SHA-512 context
00093  * \param output   SHA-384/512 checksum result
00094  */
00095 void sha512_finish( sha512_context *ctx, unsigned char output[64] );
00096 
00097 #ifdef __cplusplus
00098 }
00099 #endif
00100 
00101 #else  /* POLARSSL_SHA512_ALT */
00102 #include "sha512_alt.h"
00103 #endif /* POLARSSL_SHA512_ALT */
00104 
00105 #ifdef __cplusplus
00106 extern "C" {
00107 #endif
00108 
00109 /**
00110  * \brief          Output = SHA-512( input buffer )
00111  *
00112  * \param input    buffer holding the  data
00113  * \param ilen     length of the input data
00114  * \param output   SHA-384/512 checksum result
00115  * \param is384    0 = use SHA512, 1 = use SHA384
00116  */
00117 void sha512( const unsigned char *input, size_t ilen,
00118              unsigned char output[64], int is384 );
00119 
00120 /**
00121  * \brief          Output = SHA-512( file contents )
00122  *
00123  * \param path     input file name
00124  * \param output   SHA-384/512 checksum result
00125  * \param is384    0 = use SHA512, 1 = use SHA384
00126  *
00127  * \return         0 if successful, or POLARSSL_ERR_SHA512_FILE_IO_ERROR
00128  */
00129 int sha512_file( const char *path, unsigned char output[64], int is384 );
00130 
00131 /**
00132  * \brief          SHA-512 HMAC context setup
00133  *
00134  * \param ctx      HMAC context to be initialized
00135  * \param is384    0 = use SHA512, 1 = use SHA384
00136  * \param key      HMAC secret key
00137  * \param keylen   length of the HMAC key
00138  */
00139 void sha512_hmac_starts( sha512_context *ctx, const unsigned char *key,
00140                          size_t keylen, int is384 );
00141 
00142 /**
00143  * \brief          SHA-512 HMAC process buffer
00144  *
00145  * \param ctx      HMAC context
00146  * \param input    buffer holding the  data
00147  * \param ilen     length of the input data
00148  */
00149 void sha512_hmac_update( sha512_context *ctx, const unsigned char *input,
00150                          size_t ilen );
00151 
00152 /**
00153  * \brief          SHA-512 HMAC final digest
00154  *
00155  * \param ctx      HMAC context
00156  * \param output   SHA-384/512 HMAC checksum result
00157  */
00158 void sha512_hmac_finish( sha512_context *ctx, unsigned char output[64] );
00159 
00160 /**
00161  * \brief          SHA-512 HMAC context reset
00162  *
00163  * \param ctx      HMAC context to be reset
00164  */
00165 void sha512_hmac_reset( sha512_context *ctx );
00166 
00167 /**
00168  * \brief          Output = HMAC-SHA-512( hmac key, input buffer )
00169  *
00170  * \param key      HMAC secret key
00171  * \param keylen   length of the HMAC key
00172  * \param input    buffer holding the  data
00173  * \param ilen     length of the input data
00174  * \param output   HMAC-SHA-384/512 result
00175  * \param is384    0 = use SHA512, 1 = use SHA384
00176  */
00177 void sha512_hmac( const unsigned char *key, size_t keylen,
00178                 const unsigned char *input, size_t ilen,
00179                 unsigned char output[64], int is384 );
00180 
00181 /**
00182  * \brief          Checkup routine
00183  *
00184  * \return         0 if successful, or 1 if the test failed
00185  */
00186 int sha512_self_test( int verbose );
00187 
00188 /* Internal use */
00189 void sha512_process( sha512_context *ctx, const unsigned char data[128] );
00190 
00191 #ifdef __cplusplus
00192 }
00193 #endif
00194 
00195 #endif /* sha512.h */
00196 
00197