blob: 811b0fd61961c5997b7f3161ee441ff96981c40f [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file sha2.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Paul Bakkerf3b86c12011-01-27 15:24:17 +00004 * \brief SHA-224 and SHA-256 cryptographic hash function
Paul Bakker37ca75d2011-01-06 12:28:03 +00005 *
Paul Bakker84f12b72010-07-18 10:13:04 +00006 * Copyright (C) 2006-2010, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
8 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00009 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +000010 *
Paul Bakker77b385e2009-07-28 17:23:11 +000011 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000012 *
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000013 * 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.
Paul Bakker5121ce52009-01-03 21:22:43 +000026 */
Paul Bakker40e46942009-01-03 21:51:57 +000027#ifndef POLARSSL_SHA2_H
28#define POLARSSL_SHA2_H
Paul Bakker5121ce52009-01-03 21:22:43 +000029
Paul Bakker23986e52011-04-24 08:57:21 +000030#include <string.h>
31
Paul Bakker69e095c2011-12-10 21:55:01 +000032#define POLARSSL_ERR_SHA2_FILE_IO_ERROR -0x0078 /**< Read/write error in file. */
33
Paul Bakker5121ce52009-01-03 21:22:43 +000034/**
35 * \brief SHA-256 context structure
36 */
37typedef struct
38{
39 unsigned long total[2]; /*!< number of bytes processed */
40 unsigned long state[8]; /*!< intermediate digest state */
41 unsigned char buffer[64]; /*!< data block being processed */
42
43 unsigned char ipad[64]; /*!< HMAC: inner padding */
44 unsigned char opad[64]; /*!< HMAC: outer padding */
45 int is224; /*!< 0 => SHA-256, else SHA-224 */
46}
47sha2_context;
48
49#ifdef __cplusplus
50extern "C" {
51#endif
52
53/**
54 * \brief SHA-256 context setup
55 *
56 * \param ctx context to be initialized
57 * \param is224 0 = use SHA256, 1 = use SHA224
58 */
59void sha2_starts( sha2_context *ctx, int is224 );
60
61/**
62 * \brief SHA-256 process buffer
63 *
64 * \param ctx SHA-256 context
65 * \param input buffer holding the data
66 * \param ilen length of the input data
67 */
Paul Bakker23986e52011-04-24 08:57:21 +000068void sha2_update( sha2_context *ctx, const unsigned char *input, size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +000069
70/**
71 * \brief SHA-256 final digest
72 *
73 * \param ctx SHA-256 context
74 * \param output SHA-224/256 checksum result
75 */
76void sha2_finish( sha2_context *ctx, unsigned char output[32] );
77
78/**
79 * \brief Output = SHA-256( input buffer )
80 *
81 * \param input buffer holding the data
82 * \param ilen length of the input data
83 * \param output SHA-224/256 checksum result
84 * \param is224 0 = use SHA256, 1 = use SHA224
85 */
Paul Bakker23986e52011-04-24 08:57:21 +000086void sha2( const unsigned char *input, size_t ilen,
Paul Bakker5121ce52009-01-03 21:22:43 +000087 unsigned char output[32], int is224 );
88
89/**
90 * \brief Output = SHA-256( file contents )
91 *
92 * \param path input file name
93 * \param output SHA-224/256 checksum result
94 * \param is224 0 = use SHA256, 1 = use SHA224
95 *
Paul Bakker69e095c2011-12-10 21:55:01 +000096 * \return 0 if successful, or POLARSSL_ERR_SHA2_FILE_IO_ERROR
Paul Bakker5121ce52009-01-03 21:22:43 +000097 */
Paul Bakkerff60ee62010-03-16 21:09:09 +000098int sha2_file( const char *path, unsigned char output[32], int is224 );
Paul Bakker5121ce52009-01-03 21:22:43 +000099
100/**
101 * \brief SHA-256 HMAC context setup
102 *
103 * \param ctx HMAC context to be initialized
104 * \param key HMAC secret key
105 * \param keylen length of the HMAC key
106 * \param is224 0 = use SHA256, 1 = use SHA224
107 */
Paul Bakker23986e52011-04-24 08:57:21 +0000108void sha2_hmac_starts( sha2_context *ctx, const unsigned char *key, size_t keylen,
Paul Bakker5121ce52009-01-03 21:22:43 +0000109 int is224 );
110
111/**
112 * \brief SHA-256 HMAC process buffer
113 *
114 * \param ctx HMAC context
115 * \param input buffer holding the data
116 * \param ilen length of the input data
117 */
Paul Bakker23986e52011-04-24 08:57:21 +0000118void sha2_hmac_update( sha2_context *ctx, const unsigned char *input, size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000119
120/**
121 * \brief SHA-256 HMAC final digest
122 *
123 * \param ctx HMAC context
124 * \param output SHA-224/256 HMAC checksum result
125 */
126void sha2_hmac_finish( sha2_context *ctx, unsigned char output[32] );
127
128/**
Paul Bakker7d3b6612010-03-21 16:23:13 +0000129 * \brief SHA-256 HMAC context reset
130 *
131 * \param ctx HMAC context to be reset
132 */
133void sha2_hmac_reset( sha2_context *ctx );
134
135/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000136 * \brief Output = HMAC-SHA-256( hmac key, input buffer )
137 *
138 * \param key HMAC secret key
139 * \param keylen length of the HMAC key
140 * \param input buffer holding the data
141 * \param ilen length of the input data
142 * \param output HMAC-SHA-224/256 result
143 * \param is224 0 = use SHA256, 1 = use SHA224
144 */
Paul Bakker23986e52011-04-24 08:57:21 +0000145void sha2_hmac( const unsigned char *key, size_t keylen,
146 const unsigned char *input, size_t ilen,
Paul Bakker5121ce52009-01-03 21:22:43 +0000147 unsigned char output[32], int is224 );
148
149/**
150 * \brief Checkup routine
151 *
152 * \return 0 if successful, or 1 if the test failed
153 */
154int sha2_self_test( int verbose );
155
156#ifdef __cplusplus
157}
158#endif
159
160#endif /* sha2.h */