blob: e9cc0ca21a82b18892f941689f54647acd6f1d12 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
Simon Butcher5b331b92016-01-03 16:14:14 +00002 * \file sha256.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 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02006 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02007 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000020 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000021 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000022 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#ifndef MBEDTLS_SHA256_H
24#define MBEDTLS_SHA256_H
Paul Bakker5121ce52009-01-03 21:22:43 +000025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakker90995b52013-06-24 19:20:35 +020027#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#endif
Paul Bakker90995b52013-06-24 19:20:35 +020031
Rich Evans00ab4702015-02-06 13:43:58 +000032#include <stddef.h>
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020033#include <stdint.h>
Paul Bakker5c2364c2012-10-01 14:41:15 +000034
Gilles Peskinea381fe82018-01-23 18:16:11 +010035#define MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED -0x0037 /**< SHA-256 hardware accelerator failed */
36
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +010037#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
38 !defined(inline) && !defined(__cplusplus)
39#define inline __inline
40#endif
41
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#if !defined(MBEDTLS_SHA256_ALT)
Paul Bakker90995b52013-06-24 19:20:35 +020043// Regular implementation
44//
45
Paul Bakker407a0da2013-06-27 14:29:21 +020046#ifdef __cplusplus
47extern "C" {
48#endif
49
Paul Bakker5121ce52009-01-03 21:22:43 +000050/**
51 * \brief SHA-256 context structure
52 */
53typedef struct
54{
Paul Bakker5c2364c2012-10-01 14:41:15 +000055 uint32_t total[2]; /*!< number of bytes processed */
56 uint32_t state[8]; /*!< intermediate digest state */
Paul Bakker5121ce52009-01-03 21:22:43 +000057 unsigned char buffer[64]; /*!< data block being processed */
Paul Bakker5121ce52009-01-03 21:22:43 +000058 int is224; /*!< 0 => SHA-256, else SHA-224 */
59}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060mbedtls_sha256_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000061
Paul Bakker5121ce52009-01-03 21:22:43 +000062/**
Paul Bakker5b4af392014-06-26 12:09:34 +020063 * \brief Initialize SHA-256 context
64 *
65 * \param ctx SHA-256 context to be initialized
66 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067void mbedtls_sha256_init( mbedtls_sha256_context *ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +020068
69/**
70 * \brief Clear SHA-256 context
71 *
72 * \param ctx SHA-256 context to be cleared
73 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074void mbedtls_sha256_free( mbedtls_sha256_context *ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +020075
76/**
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020077 * \brief Clone (the state of) a SHA-256 context
78 *
79 * \param dst The destination context
80 * \param src The context to be cloned
81 */
82void mbedtls_sha256_clone( mbedtls_sha256_context *dst,
83 const mbedtls_sha256_context *src );
84
85/**
Paul Bakker5121ce52009-01-03 21:22:43 +000086 * \brief SHA-256 context setup
87 *
88 * \param ctx context to be initialized
89 * \param is224 0 = use SHA256, 1 = use SHA224
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +010090 *
91 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +000092 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010093int mbedtls_sha256_starts_ret( mbedtls_sha256_context *ctx, int is224 );
Paul Bakker5121ce52009-01-03 21:22:43 +000094
95/**
96 * \brief SHA-256 process buffer
97 *
98 * \param ctx SHA-256 context
Andres Amaya Garciaa21247e2017-07-20 14:01:08 +010099 * \param input buffer holding the data
Paul Bakker5121ce52009-01-03 21:22:43 +0000100 * \param ilen length of the input data
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100101 *
102 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000103 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100104int mbedtls_sha256_update_ret( mbedtls_sha256_context *ctx,
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100105 const unsigned char *input,
106 size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000107
108/**
109 * \brief SHA-256 final digest
110 *
111 * \param ctx SHA-256 context
112 * \param output SHA-224/256 checksum result
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100113 *
114 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000115 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100116int mbedtls_sha256_finish_ret( mbedtls_sha256_context *ctx,
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100117 unsigned char output[32] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000118
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100119/**
120 * \brief SHA-256 process data block (internal use only)
121 *
122 * \param ctx SHA-256 context
123 * \param data buffer holding one block of data
124 *
125 * \return 0 if successful
126 */
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100127int mbedtls_internal_sha256_process( mbedtls_sha256_context *ctx,
128 const unsigned char data[64] );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100129
130#if !defined(MBEDTLS_DEPRECATED_REMOVED)
131#if defined(MBEDTLS_DEPRECATED_WARNING)
132#define MBEDTLS_DEPRECATED __attribute__((deprecated))
133#else
134#define MBEDTLS_DEPRECATED
135#endif
136/**
137 * \brief SHA-256 context setup
138 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100139 * \deprecated Superseded by mbedtls_sha256_starts_ret() in 2.7.0
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100140 *
141 * \param ctx context to be initialized
142 * \param is224 0 = use SHA256, 1 = use SHA224
143 */
144MBEDTLS_DEPRECATED static inline void mbedtls_sha256_starts(
145 mbedtls_sha256_context *ctx,
146 int is224 )
147{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100148 mbedtls_sha256_starts_ret( ctx, is224 );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100149}
150
151/**
152 * \brief SHA-256 process buffer
153 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100154 * \deprecated Superseded by mbedtls_sha256_update_ret() in 2.7.0
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100155 *
156 * \param ctx SHA-256 context
Andres Amaya Garciaa21247e2017-07-20 14:01:08 +0100157 * \param input buffer holding the data
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100158 * \param ilen length of the input data
159 */
160MBEDTLS_DEPRECATED static inline void mbedtls_sha256_update(
161 mbedtls_sha256_context *ctx,
162 const unsigned char *input,
163 size_t ilen )
164{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100165 mbedtls_sha256_update_ret( ctx, input, ilen );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100166}
167
168/**
169 * \brief SHA-256 final digest
170 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100171 * \deprecated Superseded by mbedtls_sha256_finish_ret() in 2.7.0
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100172 *
173 * \param ctx SHA-256 context
174 * \param output SHA-224/256 checksum result
175 */
176MBEDTLS_DEPRECATED static inline void mbedtls_sha256_finish(
177 mbedtls_sha256_context *ctx,
178 unsigned char output[32] )
179{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100180 mbedtls_sha256_finish_ret( ctx, output );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100181}
182
183/**
184 * \brief SHA-256 process data block (internal use only)
185 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100186 * \deprecated Superseded by mbedtls_internal_sha256_process() in 2.7.0
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100187 *
188 * \param ctx SHA-256 context
189 * \param data buffer holding one block of data
190 */
191MBEDTLS_DEPRECATED static inline void mbedtls_sha256_process(
192 mbedtls_sha256_context *ctx,
193 const unsigned char data[64] )
194{
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100195 mbedtls_internal_sha256_process( ctx, data );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100196}
197
198#undef MBEDTLS_DEPRECATED
199#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker90995b52013-06-24 19:20:35 +0200200
201#ifdef __cplusplus
202}
203#endif
204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205#else /* MBEDTLS_SHA256_ALT */
Paul Bakkerd2681d82013-06-30 14:49:12 +0200206#include "sha256_alt.h"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207#endif /* MBEDTLS_SHA256_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200208
209#ifdef __cplusplus
210extern "C" {
211#endif
212
Paul Bakker5121ce52009-01-03 21:22:43 +0000213/**
214 * \brief Output = SHA-256( input buffer )
215 *
Andres Amaya Garciaa21247e2017-07-20 14:01:08 +0100216 * \param input buffer holding the data
Paul Bakker5121ce52009-01-03 21:22:43 +0000217 * \param ilen length of the input data
218 * \param output SHA-224/256 checksum result
219 * \param is224 0 = use SHA256, 1 = use SHA224
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100220 *
221 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000222 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100223int mbedtls_sha256_ret( const unsigned char *input,
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100224 size_t ilen,
225 unsigned char output[32],
226 int is224 );
227
228#if !defined(MBEDTLS_DEPRECATED_REMOVED)
229#if defined(MBEDTLS_DEPRECATED_WARNING)
230#define MBEDTLS_DEPRECATED __attribute__((deprecated))
231#else
232#define MBEDTLS_DEPRECATED
233#endif
234/**
235 * \brief Output = SHA-256( input buffer )
236 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100237 * \deprecated Superseded by mbedtls_sha256_ret() in 2.7.0
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100238 *
Andres Amaya Garciaa21247e2017-07-20 14:01:08 +0100239 * \param input buffer holding the data
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100240 * \param ilen length of the input data
241 * \param output SHA-224/256 checksum result
242 * \param is224 0 = use SHA256, 1 = use SHA224
243 */
244MBEDTLS_DEPRECATED static inline void mbedtls_sha256(
245 const unsigned char *input,
246 size_t ilen,
247 unsigned char output[32],
248 int is224 )
249{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100250 mbedtls_sha256_ret( input, ilen, output, is224 );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100251}
252
253#undef MBEDTLS_DEPRECATED
254#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +0000255
256/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000257 * \brief Checkup routine
258 *
259 * \return 0 if successful, or 1 if the test failed
260 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261int mbedtls_sha256_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000262
Paul Bakker5121ce52009-01-03 21:22:43 +0000263#ifdef __cplusplus
264}
265#endif
266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267#endif /* mbedtls_sha256.h */