Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. /**
  2.  * \file sha2.h
  3.  *
  4.  * \brief SHA-224 and SHA-256 cryptographic hash function
  5.  *
  6.  *  Copyright (C) 2006-2010, Brainspark B.V.
  7.  *
  8.  *  This file is part of PolarSSL (http://www.polarssl.org)
  9.  *  Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
  10.  *
  11.  *  All rights reserved.
  12.  *
  13.  *  This program is free software; you can redistribute it and/or modify
  14.  *  it under the terms of the GNU General Public License as published by
  15.  *  the Free Software Foundation; either version 2 of the License, or
  16.  *  (at your option) any later version.
  17.  *
  18.  *  This program is distributed in the hope that it will be useful,
  19.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  20.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21.  *  GNU General Public License for more details.
  22.  *
  23.  *  You should have received a copy of the GNU General Public License along
  24.  *  with this program; if not, write to the Free Software Foundation, Inc.,
  25.  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  26.  */
  27. #ifndef POLARSSL_SHA2_H
  28. #define POLARSSL_SHA2_H
  29.  
  30. #include <string.h>
  31.  
  32. #ifdef _MSC_VER
  33. #include <basetsd.h>
  34. typedef UINT32 uint32_t;
  35. #else
  36. #include <inttypes.h>
  37. #endif
  38.  
  39. #define POLARSSL_ERR_SHA2_FILE_IO_ERROR                -0x0078  /**< Read/write error in file. */
  40.  
  41. /**
  42.  * \brief          SHA-256 context structure
  43.  */
  44. typedef struct
  45. {
  46.     uint32_t total[2];          /*!< number of bytes processed  */
  47.     uint32_t state[8];          /*!< intermediate digest state  */
  48.     unsigned char buffer[64];   /*!< data block being processed */
  49.  
  50.     unsigned char ipad[64];     /*!< HMAC: inner padding        */
  51.     unsigned char opad[64];     /*!< HMAC: outer padding        */
  52.     int is224;                  /*!< 0 => SHA-256, else SHA-224 */
  53. }
  54. sha2_context;
  55.  
  56. #ifdef __cplusplus
  57. extern "C" {
  58. #endif
  59.  
  60. /**
  61.  * \brief          SHA-256 context setup
  62.  *
  63.  * \param ctx      context to be initialized
  64.  * \param is224    0 = use SHA256, 1 = use SHA224
  65.  */
  66. void sha2_starts( sha2_context *ctx, int is224 );
  67.  
  68. /**
  69.  * \brief          SHA-256 process buffer
  70.  *
  71.  * \param ctx      SHA-256 context
  72.  * \param input    buffer holding the  data
  73.  * \param ilen     length of the input data
  74.  */
  75. void sha2_update( sha2_context *ctx, const unsigned char *input, size_t ilen );
  76.  
  77. /**
  78.  * \brief          SHA-256 final digest
  79.  *
  80.  * \param ctx      SHA-256 context
  81.  * \param output   SHA-224/256 checksum result
  82.  */
  83. void sha2_finish( sha2_context *ctx, unsigned char output[32] );
  84.  
  85. /**
  86.  * \brief          Output = SHA-256( input buffer )
  87.  *
  88.  * \param input    buffer holding the  data
  89.  * \param ilen     length of the input data
  90.  * \param output   SHA-224/256 checksum result
  91.  * \param is224    0 = use SHA256, 1 = use SHA224
  92.  */
  93. void sha2( const unsigned char *input, size_t ilen,
  94.            unsigned char output[32], int is224 );
  95.  
  96. /**
  97.  * \brief          Output = SHA-256( file contents )
  98.  *
  99.  * \param path     input file name
  100.  * \param output   SHA-224/256 checksum result
  101.  * \param is224    0 = use SHA256, 1 = use SHA224
  102.  *
  103.  * \return         0 if successful, or POLARSSL_ERR_SHA2_FILE_IO_ERROR
  104.  */
  105. int sha2_file( const char *path, unsigned char output[32], int is224 );
  106.  
  107. /**
  108.  * \brief          SHA-256 HMAC context setup
  109.  *
  110.  * \param ctx      HMAC context to be initialized
  111.  * \param key      HMAC secret key
  112.  * \param keylen   length of the HMAC key
  113.  * \param is224    0 = use SHA256, 1 = use SHA224
  114.  */
  115. void sha2_hmac_starts( sha2_context *ctx, const unsigned char *key, size_t keylen,
  116.                        int is224 );
  117.  
  118. /**
  119.  * \brief          SHA-256 HMAC process buffer
  120.  *
  121.  * \param ctx      HMAC context
  122.  * \param input    buffer holding the  data
  123.  * \param ilen     length of the input data
  124.  */
  125. void sha2_hmac_update( sha2_context *ctx, const unsigned char *input, size_t ilen );
  126.  
  127. /**
  128.  * \brief          SHA-256 HMAC final digest
  129.  *
  130.  * \param ctx      HMAC context
  131.  * \param output   SHA-224/256 HMAC checksum result
  132.  */
  133. void sha2_hmac_finish( sha2_context *ctx, unsigned char output[32] );
  134.  
  135. /**
  136.  * \brief          SHA-256 HMAC context reset
  137.  *
  138.  * \param ctx      HMAC context to be reset
  139.  */
  140. void sha2_hmac_reset( sha2_context *ctx );
  141.  
  142. /**
  143.  * \brief          Output = HMAC-SHA-256( hmac key, input buffer )
  144.  *
  145.  * \param key      HMAC secret key
  146.  * \param keylen   length of the HMAC key
  147.  * \param input    buffer holding the  data
  148.  * \param ilen     length of the input data
  149.  * \param output   HMAC-SHA-224/256 result
  150.  * \param is224    0 = use SHA256, 1 = use SHA224
  151.  */
  152. void sha2_hmac( const unsigned char *key, size_t keylen,
  153.                 const unsigned char *input, size_t ilen,
  154.                 unsigned char output[32], int is224 );
  155.  
  156. /**
  157.  * \brief          Checkup routine
  158.  *
  159.  * \return         0 if successful, or 1 if the test failed
  160.  */
  161. int sha2_self_test( int verbose );
  162.  
  163. /* Internal use */
  164. void sha2_process( sha2_context *ctx, const unsigned char data[64] );
  165.  
  166. #ifdef __cplusplus
  167. }
  168. #endif
  169.  
  170. #endif /* sha2.h */
  171.