4 * \brief SHA-1 cryptographic hash function
6 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
8 * This file is part of mbed TLS (https://tls.mbed.org)
10 * 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.
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.
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.
24 #ifndef POLARSSL_SHA1_H
25 #define POLARSSL_SHA1_H
27 #if !defined(POLARSSL_CONFIG_FILE)
30 * \def POLARSSL_SHA1_C
32 * Enable the SHA1 cryptographic hash algorithm.
34 * Module: library/sha1.c
35 * Caller: library/md.c
39 * library/x509write_crt.c
41 * This module is required for SSL/TLS and SHA1-signed certificates.
43 #define POLARSSL_SHA1_C
46 #include POLARSSL_CONFIG_FILE
51 #if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
53 typedef UINT32
uint32_t;
58 #define POLARSSL_ERR_SHA1_FILE_IO_ERROR -0x0076 /**< Read/write error in file. */
60 #if !defined(POLARSSL_SHA1_ALT)
61 // Regular implementation
69 * \brief SHA-1 context structure
73 uint32_t total
[2]; /*!< number of bytes processed */
74 uint32_t state
[5]; /*!< intermediate digest state */
75 unsigned char buffer
[64]; /*!< data block being processed */
77 unsigned char ipad
[64]; /*!< HMAC: inner padding */
78 unsigned char opad
[64]; /*!< HMAC: outer padding */
83 * \brief Initialize SHA-1 context
85 * \param ctx SHA-1 context to be initialized
87 void sha1_init( sha1_context
*ctx
);
90 * \brief Clear SHA-1 context
92 * \param ctx SHA-1 context to be cleared
94 void sha1_free( sha1_context
*ctx
);
97 * \brief SHA-1 context setup
99 * \param ctx context to be initialized
101 void sha1_starts( sha1_context
*ctx
);
104 * \brief SHA-1 process buffer
106 * \param ctx SHA-1 context
107 * \param input buffer holding the data
108 * \param ilen length of the input data
110 void sha1_update( sha1_context
*ctx
, const unsigned char *input
, size_t ilen
);
113 * \brief SHA-1 final digest
115 * \param ctx SHA-1 context
116 * \param output SHA-1 checksum result
118 void sha1_finish( sha1_context
*ctx
, unsigned char output
[20] );
121 void sha1_process( sha1_context
*ctx
, const unsigned char data
[64] );
127 #else /* POLARSSL_SHA1_ALT */
128 #include "sha1_alt.h"
129 #endif /* POLARSSL_SHA1_ALT */
136 * \brief Output = SHA-1( input buffer )
138 * \param input buffer holding the data
139 * \param ilen length of the input data
140 * \param output SHA-1 checksum result
142 void sha1( const unsigned char *input
, size_t ilen
, unsigned char output
[20] );
145 * \brief Output = SHA-1( file contents )
147 * \param path input file name
148 * \param output SHA-1 checksum result
150 * \return 0 if successful, or POLARSSL_ERR_SHA1_FILE_IO_ERROR
152 int sha1_file( const char *path
, unsigned char output
[20] );
155 * \brief SHA-1 HMAC context setup
157 * \param ctx HMAC context to be initialized
158 * \param key HMAC secret key
159 * \param keylen length of the HMAC key
161 void sha1_hmac_starts( sha1_context
*ctx
, const unsigned char *key
,
165 * \brief SHA-1 HMAC process buffer
167 * \param ctx HMAC context
168 * \param input buffer holding the data
169 * \param ilen length of the input data
171 void sha1_hmac_update( sha1_context
*ctx
, const unsigned char *input
,
175 * \brief SHA-1 HMAC final digest
177 * \param ctx HMAC context
178 * \param output SHA-1 HMAC checksum result
180 void sha1_hmac_finish( sha1_context
*ctx
, unsigned char output
[20] );
183 * \brief SHA-1 HMAC context reset
185 * \param ctx HMAC context to be reset
187 void sha1_hmac_reset( sha1_context
*ctx
);
190 * \brief Output = HMAC-SHA-1( hmac key, input buffer )
192 * \param key HMAC secret key
193 * \param keylen length of the HMAC key
194 * \param input buffer holding the data
195 * \param ilen length of the input data
196 * \param output HMAC-SHA-1 result
198 void sha1_hmac( const unsigned char *key
, size_t keylen
,
199 const unsigned char *input
, size_t ilen
,
200 unsigned char output
[20] );
203 * \brief Checkup routine
205 * \return 0 if successful, or 1 if the test failed
207 int sha1_self_test( int verbose
);