blob: c963ca1e175690528c71ec713b5dd0dc3f6fc7b0 [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 Bakker5121ce52009-01-03 21:22:43 +000032/**
33 * \brief SHA-256 context structure
34 */
35typedef struct
36{
37 unsigned long total[2]; /*!< number of bytes processed */
38 unsigned long state[8]; /*!< intermediate digest state */
39 unsigned char buffer[64]; /*!< data block being processed */
40
41 unsigned char ipad[64]; /*!< HMAC: inner padding */
42 unsigned char opad[64]; /*!< HMAC: outer padding */
43 int is224; /*!< 0 => SHA-256, else SHA-224 */
44}
45sha2_context;
46
47#ifdef __cplusplus
48extern "C" {
49#endif
50
51/**
52 * \brief SHA-256 context setup
53 *
54 * \param ctx context to be initialized
55 * \param is224 0 = use SHA256, 1 = use SHA224
56 */
57void sha2_starts( sha2_context *ctx, int is224 );
58
59/**
60 * \brief SHA-256 process buffer
61 *
62 * \param ctx SHA-256 context
63 * \param input buffer holding the data
64 * \param ilen length of the input data
65 */
Paul Bakker23986e52011-04-24 08:57:21 +000066void sha2_update( sha2_context *ctx, const unsigned char *input, size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +000067
68/**
69 * \brief SHA-256 final digest
70 *
71 * \param ctx SHA-256 context
72 * \param output SHA-224/256 checksum result
73 */
74void sha2_finish( sha2_context *ctx, unsigned char output[32] );
75
76/**
77 * \brief Output = SHA-256( input buffer )
78 *
79 * \param input buffer holding the data
80 * \param ilen length of the input data
81 * \param output SHA-224/256 checksum result
82 * \param is224 0 = use SHA256, 1 = use SHA224
83 */
Paul Bakker23986e52011-04-24 08:57:21 +000084void sha2( const unsigned char *input, size_t ilen,
Paul Bakker5121ce52009-01-03 21:22:43 +000085 unsigned char output[32], int is224 );
86
87/**
88 * \brief Output = SHA-256( file contents )
89 *
90 * \param path input file name
91 * \param output SHA-224/256 checksum result
92 * \param is224 0 = use SHA256, 1 = use SHA224
93 *
94 * \return 0 if successful, 1 if fopen failed,
95 * or 2 if fread failed
96 */
Paul Bakkerff60ee62010-03-16 21:09:09 +000097int sha2_file( const char *path, unsigned char output[32], int is224 );
Paul Bakker5121ce52009-01-03 21:22:43 +000098
99/**
100 * \brief SHA-256 HMAC context setup
101 *
102 * \param ctx HMAC context to be initialized
103 * \param key HMAC secret key
104 * \param keylen length of the HMAC key
105 * \param is224 0 = use SHA256, 1 = use SHA224
106 */
Paul Bakker23986e52011-04-24 08:57:21 +0000107void sha2_hmac_starts( sha2_context *ctx, const unsigned char *key, size_t keylen,
Paul Bakker5121ce52009-01-03 21:22:43 +0000108 int is224 );
109
110/**
111 * \brief SHA-256 HMAC process buffer
112 *
113 * \param ctx HMAC context
114 * \param input buffer holding the data
115 * \param ilen length of the input data
116 */
Paul Bakker23986e52011-04-24 08:57:21 +0000117void sha2_hmac_update( sha2_context *ctx, const unsigned char *input, size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000118
119/**
120 * \brief SHA-256 HMAC final digest
121 *
122 * \param ctx HMAC context
123 * \param output SHA-224/256 HMAC checksum result
124 */
125void sha2_hmac_finish( sha2_context *ctx, unsigned char output[32] );
126
127/**
Paul Bakker7d3b6612010-03-21 16:23:13 +0000128 * \brief SHA-256 HMAC context reset
129 *
130 * \param ctx HMAC context to be reset
131 */
132void sha2_hmac_reset( sha2_context *ctx );
133
134/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000135 * \brief Output = HMAC-SHA-256( hmac key, input buffer )
136 *
137 * \param key HMAC secret key
138 * \param keylen length of the HMAC key
139 * \param input buffer holding the data
140 * \param ilen length of the input data
141 * \param output HMAC-SHA-224/256 result
142 * \param is224 0 = use SHA256, 1 = use SHA224
143 */
Paul Bakker23986e52011-04-24 08:57:21 +0000144void sha2_hmac( const unsigned char *key, size_t keylen,
145 const unsigned char *input, size_t ilen,
Paul Bakker5121ce52009-01-03 21:22:43 +0000146 unsigned char output[32], int is224 );
147
148/**
149 * \brief Checkup routine
150 *
151 * \return 0 if successful, or 1 if the test failed
152 */
153int sha2_self_test( int verbose );
154
155#ifdef __cplusplus
156}
157#endif
158
159#endif /* sha2.h */