initial commit; it works
[psslcertgen.git] / src / libpolarssl / sha2.h
blob795299ee69526fbb0aef136fabf27b69e4e65997
1 /**
2 * \file sha2.h
4 * \brief SHA-224 and SHA-256 cryptographic hash function
6 * Copyright (C) 2006-2013, Brainspark B.V.
8 * This file is part of PolarSSL (http://www.polarssl.org)
9 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
11 * All rights reserved.
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.
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.
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.
27 #ifndef POLARSSL_SHA2_H
28 #define POLARSSL_SHA2_H
30 #include "config.h"
32 #include <string.h>
34 #ifdef _MSC_VER
35 #include <basetsd.h>
36 typedef UINT32 uint32_t;
37 #else
38 #include <inttypes.h>
39 #endif
41 #define POLARSSL_ERR_SHA2_FILE_IO_ERROR -0x0078 /**< Read/write error in file. */
43 #if !defined(POLARSSL_SHA2_ALT)
44 // Regular implementation
47 /**
48 * \brief SHA-256 context structure
50 typedef struct
52 uint32_t total[2]; /*!< number of bytes processed */
53 uint32_t state[8]; /*!< intermediate digest state */
54 unsigned char buffer[64]; /*!< data block being processed */
56 unsigned char ipad[64]; /*!< HMAC: inner padding */
57 unsigned char opad[64]; /*!< HMAC: outer padding */
58 int is224; /*!< 0 => SHA-256, else SHA-224 */
60 sha2_context;
62 #ifdef __cplusplus
63 extern "C" {
64 #endif
66 /**
67 * \brief SHA-256 context setup
69 * \param ctx context to be initialized
70 * \param is224 0 = use SHA256, 1 = use SHA224
72 void sha2_starts( sha2_context *ctx, int is224 );
74 /**
75 * \brief SHA-256 process buffer
77 * \param ctx SHA-256 context
78 * \param input buffer holding the data
79 * \param ilen length of the input data
81 void sha2_update( sha2_context *ctx, const unsigned char *input, size_t ilen );
83 /**
84 * \brief SHA-256 final digest
86 * \param ctx SHA-256 context
87 * \param output SHA-224/256 checksum result
89 void sha2_finish( sha2_context *ctx, unsigned char output[32] );
91 /* Internal use */
92 void sha2_process( sha2_context *ctx, const unsigned char data[64] );
94 #ifdef __cplusplus
96 #endif
98 #else /* POLARSSL_SHA2_ALT */
99 #include "sha2_alt.h"
100 #endif /* POLARSSL_SHA2_ALT */
102 #ifdef __cplusplus
103 extern "C" {
104 #endif
107 * \brief Output = SHA-256( input buffer )
109 * \param input buffer holding the data
110 * \param ilen length of the input data
111 * \param output SHA-224/256 checksum result
112 * \param is224 0 = use SHA256, 1 = use SHA224
114 void sha2( const unsigned char *input, size_t ilen,
115 unsigned char output[32], int is224 );
118 * \brief Output = SHA-256( file contents )
120 * \param path input file name
121 * \param output SHA-224/256 checksum result
122 * \param is224 0 = use SHA256, 1 = use SHA224
124 * \return 0 if successful, or POLARSSL_ERR_SHA2_FILE_IO_ERROR
126 int sha2_file( const char *path, unsigned char output[32], int is224 );
129 * \brief SHA-256 HMAC context setup
131 * \param ctx HMAC context to be initialized
132 * \param key HMAC secret key
133 * \param keylen length of the HMAC key
134 * \param is224 0 = use SHA256, 1 = use SHA224
136 void sha2_hmac_starts( sha2_context *ctx, const unsigned char *key, size_t keylen,
137 int is224 );
140 * \brief SHA-256 HMAC process buffer
142 * \param ctx HMAC context
143 * \param input buffer holding the data
144 * \param ilen length of the input data
146 void sha2_hmac_update( sha2_context *ctx, const unsigned char *input, size_t ilen );
149 * \brief SHA-256 HMAC final digest
151 * \param ctx HMAC context
152 * \param output SHA-224/256 HMAC checksum result
154 void sha2_hmac_finish( sha2_context *ctx, unsigned char output[32] );
157 * \brief SHA-256 HMAC context reset
159 * \param ctx HMAC context to be reset
161 void sha2_hmac_reset( sha2_context *ctx );
164 * \brief Output = HMAC-SHA-256( hmac key, input buffer )
166 * \param key HMAC secret key
167 * \param keylen length of the HMAC key
168 * \param input buffer holding the data
169 * \param ilen length of the input data
170 * \param output HMAC-SHA-224/256 result
171 * \param is224 0 = use SHA256, 1 = use SHA224
173 void sha2_hmac( const unsigned char *key, size_t keylen,
174 const unsigned char *input, size_t ilen,
175 unsigned char output[32], int is224 );
178 * \brief Checkup routine
180 * \return 0 if successful, or 1 if the test failed
182 int sha2_self_test( int verbose );
184 #ifdef __cplusplus
186 #endif
188 #endif /* sha2.h */