blob: 9fdbb1d930c0bc201f75a25de5a0a6d7478464cb [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file sha1.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Paul Bakker37ca75d2011-01-06 12:28:03 +00004 * \brief SHA-1 cryptographic hash function
5 *
Manuel Pégourié-Gonnard0edee5e2015-01-26 15:29:40 +00006 * Copyright (C) 2006-2013, 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_SHA1_H
25#define POLARSSL_SHA1_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_SHA1_FILE_IO_ERROR -0x0076 /**< Read/write error in file. */
39
Paul Bakker4087c472013-06-12 16:49:10 +020040#if !defined(POLARSSL_SHA1_ALT)
41// Regular implementation
42//
43
Paul Bakker5121ce52009-01-03 21:22:43 +000044/**
45 * \brief SHA-1 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[5]; /*!< 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}
56sha1_context;
57
58#ifdef __cplusplus
59extern "C" {
60#endif
61
62/**
63 * \brief SHA-1 context setup
64 *
65 * \param ctx context to be initialized
66 */
67void sha1_starts( sha1_context *ctx );
68
69/**
70 * \brief SHA-1 process buffer
71 *
72 * \param ctx SHA-1 context
73 * \param input buffer holding the data
74 * \param ilen length of the input data
75 */
Paul Bakker23986e52011-04-24 08:57:21 +000076void sha1_update( sha1_context *ctx, const unsigned char *input, size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +000077
78/**
79 * \brief SHA-1 final digest
80 *
81 * \param ctx SHA-1 context
82 * \param output SHA-1 checksum result
83 */
84void sha1_finish( sha1_context *ctx, unsigned char output[20] );
85
Paul Bakker4087c472013-06-12 16:49:10 +020086/* Internal use */
87void sha1_process( sha1_context *ctx, const unsigned char data[64] );
88
89#ifdef __cplusplus
90}
91#endif
92
93#else /* POLARSSL_SHA1_ALT */
94#include "sha1_alt.h"
95#endif /* POLARSSL_SHA1_ALT */
96
97#ifdef __cplusplus
98extern "C" {
99#endif
100
Paul Bakker5121ce52009-01-03 21:22:43 +0000101/**
102 * \brief Output = SHA-1( input buffer )
103 *
104 * \param input buffer holding the data
105 * \param ilen length of the input data
106 * \param output SHA-1 checksum result
107 */
Paul Bakker23986e52011-04-24 08:57:21 +0000108void sha1( const unsigned char *input, size_t ilen, unsigned char output[20] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000109
110/**
111 * \brief Output = SHA-1( file contents )
112 *
113 * \param path input file name
114 * \param output SHA-1 checksum result
115 *
Paul Bakker69e095c2011-12-10 21:55:01 +0000116 * \return 0 if successful, or POLARSSL_ERR_SHA1_FILE_IO_ERROR
Paul Bakker5121ce52009-01-03 21:22:43 +0000117 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000118int sha1_file( const char *path, unsigned char output[20] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000119
120/**
121 * \brief SHA-1 HMAC context setup
122 *
123 * \param ctx HMAC context to be initialized
124 * \param key HMAC secret key
125 * \param keylen length of the HMAC key
126 */
Paul Bakker23986e52011-04-24 08:57:21 +0000127void sha1_hmac_starts( sha1_context *ctx, const unsigned char *key, size_t keylen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000128
129/**
130 * \brief SHA-1 HMAC process buffer
131 *
132 * \param ctx HMAC context
133 * \param input buffer holding the data
134 * \param ilen length of the input data
135 */
Paul Bakker23986e52011-04-24 08:57:21 +0000136void sha1_hmac_update( sha1_context *ctx, const unsigned char *input, size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000137
138/**
139 * \brief SHA-1 HMAC final digest
140 *
141 * \param ctx HMAC context
142 * \param output SHA-1 HMAC checksum result
143 */
144void sha1_hmac_finish( sha1_context *ctx, unsigned char output[20] );
145
146/**
Paul Bakker7d3b6612010-03-21 16:23:13 +0000147 * \brief SHA-1 HMAC context reset
148 *
149 * \param ctx HMAC context to be reset
150 */
151void sha1_hmac_reset( sha1_context *ctx );
152
153/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000154 * \brief Output = HMAC-SHA-1( hmac key, input buffer )
155 *
156 * \param key HMAC secret key
157 * \param keylen length of the HMAC key
158 * \param input buffer holding the data
159 * \param ilen length of the input data
160 * \param output HMAC-SHA-1 result
161 */
Paul Bakker23986e52011-04-24 08:57:21 +0000162void sha1_hmac( const unsigned char *key, size_t keylen,
163 const unsigned char *input, size_t ilen,
Paul Bakker5121ce52009-01-03 21:22:43 +0000164 unsigned char output[20] );
165
166/**
167 * \brief Checkup routine
168 *
169 * \return 0 if successful, or 1 if the test failed
170 */
171int sha1_self_test( int verbose );
172
Paul Bakker5121ce52009-01-03 21:22:43 +0000173#ifdef __cplusplus
174}
175#endif
176
177#endif /* sha1.h */