blob: 9007aea882afe9f7d871f67a84dedd73c36cba87 [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 Bakker530927b2015-02-13 14:24:10 +01006 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
Manuel Pégourié-Gonnarde12abf92015-01-28 17:13:45 +00008 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00009 *
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Paul Bakker5121ce52009-01-03 21:22:43 +000023 */
Paul Bakker40e46942009-01-03 21:51:57 +000024#ifndef POLARSSL_SHA2_H
25#define POLARSSL_SHA2_H
Paul Bakker5121ce52009-01-03 21:22:43 +000026
Paul Bakker4087c472013-06-12 16:49:10 +020027#include "config.h"
28
Paul Bakker23986e52011-04-24 08:57:21 +000029#include <string.h>
30
Paul Bakker5c2364c2012-10-01 14:41:15 +000031#ifdef _MSC_VER
32#include <basetsd.h>
33typedef UINT32 uint32_t;
34#else
35#include <inttypes.h>
36#endif
37
Paul Bakker69e095c2011-12-10 21:55:01 +000038#define POLARSSL_ERR_SHA2_FILE_IO_ERROR -0x0078 /**< Read/write error in file. */
39
Paul Bakker4087c472013-06-12 16:49:10 +020040#if !defined(POLARSSL_SHA2_ALT)
41// Regular implementation
42//
43
Paul Bakker5121ce52009-01-03 21:22:43 +000044/**
45 * \brief SHA-256 context structure
46 */
47typedef struct
48{
Paul Bakker5c2364c2012-10-01 14:41:15 +000049 uint32_t total[2]; /*!< number of bytes processed */
50 uint32_t state[8]; /*!< intermediate digest state */
Paul Bakker5121ce52009-01-03 21:22:43 +000051 unsigned char buffer[64]; /*!< data block being processed */
52
53 unsigned char ipad[64]; /*!< HMAC: inner padding */
54 unsigned char opad[64]; /*!< HMAC: outer padding */
55 int is224; /*!< 0 => SHA-256, else SHA-224 */
56}
57sha2_context;
58
59#ifdef __cplusplus
60extern "C" {
61#endif
62
63/**
64 * \brief SHA-256 context setup
65 *
66 * \param ctx context to be initialized
67 * \param is224 0 = use SHA256, 1 = use SHA224
68 */
69void sha2_starts( sha2_context *ctx, int is224 );
70
71/**
72 * \brief SHA-256 process buffer
73 *
74 * \param ctx SHA-256 context
75 * \param input buffer holding the data
76 * \param ilen length of the input data
77 */
Paul Bakker23986e52011-04-24 08:57:21 +000078void sha2_update( sha2_context *ctx, const unsigned char *input, size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +000079
80/**
81 * \brief SHA-256 final digest
82 *
83 * \param ctx SHA-256 context
84 * \param output SHA-224/256 checksum result
85 */
86void sha2_finish( sha2_context *ctx, unsigned char output[32] );
87
Paul Bakker4087c472013-06-12 16:49:10 +020088/* Internal use */
89void sha2_process( sha2_context *ctx, const unsigned char data[64] );
90
91#ifdef __cplusplus
92}
93#endif
94
95#else /* POLARSSL_SHA2_ALT */
96#include "sha2_alt.h"
97#endif /* POLARSSL_SHA2_ALT */
98
99#ifdef __cplusplus
100extern "C" {
101#endif
102
Paul Bakker5121ce52009-01-03 21:22:43 +0000103/**
104 * \brief Output = SHA-256( input buffer )
105 *
106 * \param input buffer holding the data
107 * \param ilen length of the input data
108 * \param output SHA-224/256 checksum result
109 * \param is224 0 = use SHA256, 1 = use SHA224
110 */
Paul Bakker23986e52011-04-24 08:57:21 +0000111void sha2( const unsigned char *input, size_t ilen,
Paul Bakker5121ce52009-01-03 21:22:43 +0000112 unsigned char output[32], int is224 );
113
114/**
115 * \brief Output = SHA-256( file contents )
116 *
117 * \param path input file name
118 * \param output SHA-224/256 checksum result
119 * \param is224 0 = use SHA256, 1 = use SHA224
120 *
Paul Bakker69e095c2011-12-10 21:55:01 +0000121 * \return 0 if successful, or POLARSSL_ERR_SHA2_FILE_IO_ERROR
Paul Bakker5121ce52009-01-03 21:22:43 +0000122 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000123int sha2_file( const char *path, unsigned char output[32], int is224 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000124
125/**
126 * \brief SHA-256 HMAC context setup
127 *
128 * \param ctx HMAC context to be initialized
129 * \param key HMAC secret key
130 * \param keylen length of the HMAC key
131 * \param is224 0 = use SHA256, 1 = use SHA224
132 */
Paul Bakker23986e52011-04-24 08:57:21 +0000133void sha2_hmac_starts( sha2_context *ctx, const unsigned char *key, size_t keylen,
Paul Bakker5121ce52009-01-03 21:22:43 +0000134 int is224 );
135
136/**
137 * \brief SHA-256 HMAC process buffer
138 *
139 * \param ctx HMAC context
140 * \param input buffer holding the data
141 * \param ilen length of the input data
142 */
Paul Bakker23986e52011-04-24 08:57:21 +0000143void sha2_hmac_update( sha2_context *ctx, const unsigned char *input, size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000144
145/**
146 * \brief SHA-256 HMAC final digest
147 *
148 * \param ctx HMAC context
149 * \param output SHA-224/256 HMAC checksum result
150 */
151void sha2_hmac_finish( sha2_context *ctx, unsigned char output[32] );
152
153/**
Paul Bakker7d3b6612010-03-21 16:23:13 +0000154 * \brief SHA-256 HMAC context reset
155 *
156 * \param ctx HMAC context to be reset
157 */
158void sha2_hmac_reset( sha2_context *ctx );
159
160/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000161 * \brief Output = HMAC-SHA-256( hmac key, input buffer )
162 *
163 * \param key HMAC secret key
164 * \param keylen length of the HMAC key
165 * \param input buffer holding the data
166 * \param ilen length of the input data
167 * \param output HMAC-SHA-224/256 result
168 * \param is224 0 = use SHA256, 1 = use SHA224
169 */
Paul Bakker23986e52011-04-24 08:57:21 +0000170void sha2_hmac( const unsigned char *key, size_t keylen,
171 const unsigned char *input, size_t ilen,
Paul Bakker5121ce52009-01-03 21:22:43 +0000172 unsigned char output[32], int is224 );
173
174/**
175 * \brief Checkup routine
176 *
177 * \return 0 if successful, or 1 if the test failed
178 */
179int sha2_self_test( int verbose );
180
Paul Bakker5121ce52009-01-03 21:22:43 +0000181#ifdef __cplusplus
182}
183#endif
184
185#endif /* sha2.h */