blob: be4ae56a7798a90b52733a5cb29dff8d6948216b [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file sha2.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Paul Bakker84f12b72010-07-18 10:13:04 +00004 * Copyright (C) 2006-2010, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Paul Bakker5121ce52009-01-03 21:22:43 +000024 */
Paul Bakker40e46942009-01-03 21:51:57 +000025#ifndef POLARSSL_SHA2_H
26#define POLARSSL_SHA2_H
Paul Bakker5121ce52009-01-03 21:22:43 +000027
28/**
29 * \brief SHA-256 context structure
30 */
31typedef struct
32{
33 unsigned long total[2]; /*!< number of bytes processed */
34 unsigned long state[8]; /*!< intermediate digest state */
35 unsigned char buffer[64]; /*!< data block being processed */
36
37 unsigned char ipad[64]; /*!< HMAC: inner padding */
38 unsigned char opad[64]; /*!< HMAC: outer padding */
39 int is224; /*!< 0 => SHA-256, else SHA-224 */
40}
41sha2_context;
42
43#ifdef __cplusplus
44extern "C" {
45#endif
46
47/**
48 * \brief SHA-256 context setup
49 *
50 * \param ctx context to be initialized
51 * \param is224 0 = use SHA256, 1 = use SHA224
52 */
53void sha2_starts( sha2_context *ctx, int is224 );
54
55/**
56 * \brief SHA-256 process buffer
57 *
58 * \param ctx SHA-256 context
59 * \param input buffer holding the data
60 * \param ilen length of the input data
61 */
Paul Bakkerff60ee62010-03-16 21:09:09 +000062void sha2_update( sha2_context *ctx, const unsigned char *input, int ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +000063
64/**
65 * \brief SHA-256 final digest
66 *
67 * \param ctx SHA-256 context
68 * \param output SHA-224/256 checksum result
69 */
70void sha2_finish( sha2_context *ctx, unsigned char output[32] );
71
72/**
73 * \brief Output = SHA-256( input buffer )
74 *
75 * \param input buffer holding the data
76 * \param ilen length of the input data
77 * \param output SHA-224/256 checksum result
78 * \param is224 0 = use SHA256, 1 = use SHA224
79 */
Paul Bakkerff60ee62010-03-16 21:09:09 +000080void sha2( const unsigned char *input, int ilen,
Paul Bakker5121ce52009-01-03 21:22:43 +000081 unsigned char output[32], int is224 );
82
83/**
84 * \brief Output = SHA-256( file contents )
85 *
86 * \param path input file name
87 * \param output SHA-224/256 checksum result
88 * \param is224 0 = use SHA256, 1 = use SHA224
89 *
90 * \return 0 if successful, 1 if fopen failed,
91 * or 2 if fread failed
92 */
Paul Bakkerff60ee62010-03-16 21:09:09 +000093int sha2_file( const char *path, unsigned char output[32], int is224 );
Paul Bakker5121ce52009-01-03 21:22:43 +000094
95/**
96 * \brief SHA-256 HMAC context setup
97 *
98 * \param ctx HMAC context to be initialized
99 * \param key HMAC secret key
100 * \param keylen length of the HMAC key
101 * \param is224 0 = use SHA256, 1 = use SHA224
102 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000103void sha2_hmac_starts( sha2_context *ctx, const unsigned char *key, int keylen,
Paul Bakker5121ce52009-01-03 21:22:43 +0000104 int is224 );
105
106/**
107 * \brief SHA-256 HMAC process buffer
108 *
109 * \param ctx HMAC context
110 * \param input buffer holding the data
111 * \param ilen length of the input data
112 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000113void sha2_hmac_update( sha2_context *ctx, const unsigned char *input, int ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000114
115/**
116 * \brief SHA-256 HMAC final digest
117 *
118 * \param ctx HMAC context
119 * \param output SHA-224/256 HMAC checksum result
120 */
121void sha2_hmac_finish( sha2_context *ctx, unsigned char output[32] );
122
123/**
Paul Bakker7d3b6612010-03-21 16:23:13 +0000124 * \brief SHA-256 HMAC context reset
125 *
126 * \param ctx HMAC context to be reset
127 */
128void sha2_hmac_reset( sha2_context *ctx );
129
130/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000131 * \brief Output = HMAC-SHA-256( hmac key, input buffer )
132 *
133 * \param key HMAC secret key
134 * \param keylen length of the HMAC key
135 * \param input buffer holding the data
136 * \param ilen length of the input data
137 * \param output HMAC-SHA-224/256 result
138 * \param is224 0 = use SHA256, 1 = use SHA224
139 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000140void sha2_hmac( const unsigned char *key, int keylen,
141 const unsigned char *input, int ilen,
Paul Bakker5121ce52009-01-03 21:22:43 +0000142 unsigned char output[32], int is224 );
143
144/**
145 * \brief Checkup routine
146 *
147 * \return 0 if successful, or 1 if the test failed
148 */
149int sha2_self_test( int verbose );
150
151#ifdef __cplusplus
152}
153#endif
154
155#endif /* sha2.h */