blob: 397888331228cc756d5a4c0950aa97996d5c948e [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é-Gonnarda658a402015-01-23 09:45:19 +00006 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00008 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkerb96f1542010-07-18 20:36:00 +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
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020027#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker90995b52013-06-24 19:20:35 +020028#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020029#else
30#include POLARSSL_CONFIG_FILE
31#endif
Paul Bakker90995b52013-06-24 19:20:35 +020032
Rich Evans00ab4702015-02-06 13:43:58 +000033#include <stddef.h>
Paul Bakker23986e52011-04-24 08:57:21 +000034
Paul Bakkerfa6a6202013-10-28 18:48:30 +010035#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker5c2364c2012-10-01 14:41:15 +000036#include <basetsd.h>
37typedef UINT32 uint32_t;
38#else
39#include <inttypes.h>
40#endif
41
Paul Bakker69e095c2011-12-10 21:55:01 +000042#define POLARSSL_ERR_SHA1_FILE_IO_ERROR -0x0076 /**< Read/write error in file. */
43
Paul Bakker90995b52013-06-24 19:20:35 +020044#if !defined(POLARSSL_SHA1_ALT)
45// Regular implementation
46//
47
Paul Bakker407a0da2013-06-27 14:29:21 +020048#ifdef __cplusplus
49extern "C" {
50#endif
51
Paul Bakker5121ce52009-01-03 21:22:43 +000052/**
53 * \brief SHA-1 context structure
54 */
55typedef struct
56{
Paul Bakker5c2364c2012-10-01 14:41:15 +000057 uint32_t total[2]; /*!< number of bytes processed */
58 uint32_t state[5]; /*!< intermediate digest state */
Paul Bakker5121ce52009-01-03 21:22:43 +000059 unsigned char buffer[64]; /*!< data block being processed */
Paul Bakker5121ce52009-01-03 21:22:43 +000060}
61sha1_context;
62
Paul Bakker5121ce52009-01-03 21:22:43 +000063/**
Paul Bakker5b4af392014-06-26 12:09:34 +020064 * \brief Initialize SHA-1 context
65 *
66 * \param ctx SHA-1 context to be initialized
67 */
68void sha1_init( sha1_context *ctx );
69
70/**
71 * \brief Clear SHA-1 context
72 *
73 * \param ctx SHA-1 context to be cleared
74 */
75void sha1_free( sha1_context *ctx );
76
77/**
Paul Bakker5121ce52009-01-03 21:22:43 +000078 * \brief SHA-1 context setup
79 *
80 * \param ctx context to be initialized
81 */
82void sha1_starts( sha1_context *ctx );
83
84/**
85 * \brief SHA-1 process buffer
86 *
87 * \param ctx SHA-1 context
88 * \param input buffer holding the data
89 * \param ilen length of the input data
90 */
Paul Bakker23986e52011-04-24 08:57:21 +000091void sha1_update( sha1_context *ctx, const unsigned char *input, size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +000092
93/**
94 * \brief SHA-1 final digest
95 *
96 * \param ctx SHA-1 context
97 * \param output SHA-1 checksum result
98 */
99void sha1_finish( sha1_context *ctx, unsigned char output[20] );
100
Paul Bakker90995b52013-06-24 19:20:35 +0200101/* Internal use */
102void sha1_process( sha1_context *ctx, const unsigned char data[64] );
103
104#ifdef __cplusplus
105}
106#endif
107
108#else /* POLARSSL_SHA1_ALT */
109#include "sha1_alt.h"
110#endif /* POLARSSL_SHA1_ALT */
111
112#ifdef __cplusplus
113extern "C" {
114#endif
115
Paul Bakker5121ce52009-01-03 21:22:43 +0000116/**
117 * \brief Output = SHA-1( input buffer )
118 *
119 * \param input buffer holding the data
120 * \param ilen length of the input data
121 * \param output SHA-1 checksum result
122 */
Paul Bakker23986e52011-04-24 08:57:21 +0000123void sha1( const unsigned char *input, size_t ilen, unsigned char output[20] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000124
125/**
126 * \brief Output = SHA-1( file contents )
127 *
128 * \param path input file name
129 * \param output SHA-1 checksum result
130 *
Paul Bakker69e095c2011-12-10 21:55:01 +0000131 * \return 0 if successful, or POLARSSL_ERR_SHA1_FILE_IO_ERROR
Paul Bakker5121ce52009-01-03 21:22:43 +0000132 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000133int sha1_file( const char *path, unsigned char output[20] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000134
135/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000136 * \brief Checkup routine
137 *
138 * \return 0 if successful, or 1 if the test failed
139 */
140int sha1_self_test( int verbose );
141
Paul Bakker5121ce52009-01-03 21:22:43 +0000142#ifdef __cplusplus
143}
144#endif
145
146#endif /* sha1.h */