1 /**
2  * \file sha256.h
3  *
4  * \brief The SHA-224 and SHA-256 cryptographic hash function.
5  */
6 /*
7  *  Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
8  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
9  *
10  *  This file is provided under the Apache License 2.0, or the
11  *  GNU General Public License v2.0 or later.
12  *
13  *  **********
14  *  Apache License 2.0:
15  *
16  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
17  *  not use this file except in compliance with the License.
18  *  You may obtain a copy of the License at
19  *
20  *  http://www.apache.org/licenses/LICENSE-2.0
21  *
22  *  Unless required by applicable law or agreed to in writing, software
23  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
24  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25  *  See the License for the specific language governing permissions and
26  *  limitations under the License.
27  *
28  *  **********
29  *
30  *  **********
31  *  GNU General Public License v2.0 or later:
32  *
33  *  This program is free software; you can redistribute it and/or modify
34  *  it under the terms of the GNU General Public License as published by
35  *  the Free Software Foundation; either version 2 of the License, or
36  *  (at your option) any later version.
37  *
38  *  This program is distributed in the hope that it will be useful,
39  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
40  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
41  *  GNU General Public License for more details.
42  *
43  *  You should have received a copy of the GNU General Public License along
44  *  with this program; if not, write to the Free Software Foundation, Inc.,
45  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
46  *
47  *  **********
48  *
49  *  This file is part of Mbed TLS (https://tls.mbed.org)
50  */
51 #ifndef MBEDTLS_SHA256_H
52 #define MBEDTLS_SHA256_H
53 
54 #if !defined(MBEDTLS_CONFIG_FILE)
55 #include "config.h"
56 #else
57 #include MBEDTLS_CONFIG_FILE
58 #endif
59 
60 #include <stddef.h>
61 #include <stdint.h>
62 
63 #define MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED                -0x0037  /**< SHA-256 hardware accelerator failed */
64 
65 #if !defined(MBEDTLS_SHA256_ALT)
66 // Regular implementation
67 //
68 
69 #ifdef __cplusplus
70 extern "C" {
71 #endif
72 
73 /**
74  * \brief          The SHA-256 context structure.
75  *
76  *                 The structure is used both for SHA-256 and for SHA-224
77  *                 checksum calculations. The choice between these two is
78  *                 made in the call to mbedtls_sha256_starts_ret().
79  */
80 typedef struct
81 {
82     uint32_t total[2];          /*!< The number of Bytes processed.  */
83     uint32_t state[8];          /*!< The intermediate digest state.  */
84     unsigned char buffer[64];   /*!< The data block being processed. */
85     int is224;                  /*!< Determines which function to use.
86                                      <ul><li>0: Use SHA-256.</li>
87                                      <li>1: Use SHA-224.</li></ul> */
88 }
89 mbedtls_sha256_context;
90 
91 /**
92  * \brief          This function initializes a SHA-256 context.
93  *
94  * \param ctx      The SHA-256 context to initialize.
95  */
96 void mbedtls_sha256_init( mbedtls_sha256_context *ctx );
97 
98 /**
99  * \brief          This function clears a SHA-256 context.
100  *
101  * \param ctx      The SHA-256 context to clear.
102  */
103 void mbedtls_sha256_free( mbedtls_sha256_context *ctx );
104 
105 /**
106  * \brief          This function clones the state of a SHA-256 context.
107  *
108  * \param dst      The destination context.
109  * \param src      The context to clone.
110  */
111 void mbedtls_sha256_clone( mbedtls_sha256_context *dst,
112                            const mbedtls_sha256_context *src );
113 
114 /**
115  * \brief          This function starts a SHA-224 or SHA-256 checksum
116  *                 calculation.
117  *
118  * \param ctx      The context to initialize.
119  * \param is224    Determines which function to use.
120  *                 <ul><li>0: Use SHA-256.</li>
121  *                 <li>1: Use SHA-224.</li></ul>
122  *
123  * \return         \c 0 on success.
124  */
125 int mbedtls_sha256_starts_ret( mbedtls_sha256_context *ctx, int is224 );
126 
127 /**
128  * \brief          This function feeds an input buffer into an ongoing
129  *                 SHA-256 checksum calculation.
130  *
131  * \param ctx      SHA-256 context
132  * \param input    buffer holding the data
133  * \param ilen     length of the input data
134  *
135  * \return         \c 0 on success.
136  */
137 int mbedtls_sha256_update_ret( mbedtls_sha256_context *ctx,
138                                const unsigned char *input,
139                                size_t ilen );
140 
141 /**
142  * \brief          This function finishes the SHA-256 operation, and writes
143  *                 the result to the output buffer.
144  *
145  * \param ctx      The SHA-256 context.
146  * \param output   The SHA-224 or SHA-256 checksum result.
147  *
148  * \return         \c 0 on success.
149  */
150 int mbedtls_sha256_finish_ret( mbedtls_sha256_context *ctx,
151                                unsigned char output[32] );
152 
153 /**
154  * \brief          This function processes a single data block within
155  *                 the ongoing SHA-256 computation. This function is for
156  *                 internal use only.
157  *
158  * \param ctx      The SHA-256 context.
159  * \param data     The buffer holding one block of data.
160  *
161  * \return         \c 0 on success.
162  */
163 int mbedtls_internal_sha256_process( mbedtls_sha256_context *ctx,
164                                      const unsigned char data[64] );
165 
166 #if !defined(MBEDTLS_DEPRECATED_REMOVED)
167 #if defined(MBEDTLS_DEPRECATED_WARNING)
168 #define MBEDTLS_DEPRECATED      __attribute__((deprecated))
169 #else
170 #define MBEDTLS_DEPRECATED
171 #endif
172 /**
173  * \brief          This function starts a SHA-256 checksum calculation.
174  *
175  * \deprecated     Superseded by mbedtls_sha256_starts_ret() in 2.7.0.
176  *
177  * \param ctx      The SHA-256 context to initialize.
178  * \param is224    Determines which function to use.
179  *                 <ul><li>0: Use SHA-256.</li>
180  *                 <li>1: Use SHA-224.</li></ul>
181  */
182 MBEDTLS_DEPRECATED void mbedtls_sha256_starts( mbedtls_sha256_context *ctx,
183                                                int is224 );
184 
185 /**
186  * \brief          This function feeds an input buffer into an ongoing
187  *                 SHA-256 checksum calculation.
188  *
189  * \deprecated     Superseded by mbedtls_sha256_update_ret() in 2.7.0.
190  *
191  * \param ctx      The SHA-256 context to initialize.
192  * \param input    The buffer holding the data.
193  * \param ilen     The length of the input data.
194  */
195 MBEDTLS_DEPRECATED void mbedtls_sha256_update( mbedtls_sha256_context *ctx,
196                                                const unsigned char *input,
197                                                size_t ilen );
198 
199 /**
200  * \brief          This function finishes the SHA-256 operation, and writes
201  *                 the result to the output buffer.
202  *
203  * \deprecated     Superseded by mbedtls_sha256_finish_ret() in 2.7.0.
204  *
205  * \param ctx      The SHA-256 context.
206  * \param output   The SHA-224or SHA-256 checksum result.
207  */
208 MBEDTLS_DEPRECATED void mbedtls_sha256_finish( mbedtls_sha256_context *ctx,
209                                                unsigned char output[32] );
210 
211 /**
212  * \brief          This function processes a single data block within
213  *                 the ongoing SHA-256 computation. This function is for
214  *                 internal use only.
215  *
216  * \deprecated     Superseded by mbedtls_internal_sha256_process() in 2.7.0.
217  *
218  * \param ctx      The SHA-256 context.
219  * \param data     The buffer holding one block of data.
220  */
221 MBEDTLS_DEPRECATED void mbedtls_sha256_process( mbedtls_sha256_context *ctx,
222                                                 const unsigned char data[64] );
223 
224 #undef MBEDTLS_DEPRECATED
225 #endif /* !MBEDTLS_DEPRECATED_REMOVED */
226 #ifdef __cplusplus
227 }
228 #endif
229 
230 #else  /* MBEDTLS_SHA256_ALT */
231 #include "sha256_alt.h"
232 #endif /* MBEDTLS_SHA256_ALT */
233 
234 #ifdef __cplusplus
235 extern "C" {
236 #endif
237 
238 /**
239  * \brief          This function calculates the SHA-224 or SHA-256
240  *                 checksum of a buffer.
241  *
242  *                 The function allocates the context, performs the
243  *                 calculation, and frees the context.
244  *
245  *                 The SHA-256 result is calculated as
246  *                 output = SHA-256(input buffer).
247  *
248  * \param input    The buffer holding the input data.
249  * \param ilen     The length of the input data.
250  * \param output   The SHA-224 or SHA-256 checksum result.
251  * \param is224    Determines which function to use.
252  *                 <ul><li>0: Use SHA-256.</li>
253  *                 <li>1: Use SHA-224.</li></ul>
254  */
255 int mbedtls_sha256_ret( const unsigned char *input,
256                         size_t ilen,
257                         unsigned char output[32],
258                         int is224 );
259 
260 #if !defined(MBEDTLS_DEPRECATED_REMOVED)
261 #if defined(MBEDTLS_DEPRECATED_WARNING)
262 #define MBEDTLS_DEPRECATED      __attribute__((deprecated))
263 #else
264 #define MBEDTLS_DEPRECATED
265 #endif
266 
267 /**
268  * \brief          This function calculates the SHA-224 or SHA-256 checksum
269  *                 of a buffer.
270  *
271  *                 The function allocates the context, performs the
272  *                 calculation, and frees the context.
273  *
274  *                 The SHA-256 result is calculated as
275  *                 output = SHA-256(input buffer).
276  *
277  * \deprecated     Superseded by mbedtls_sha256_ret() in 2.7.0.
278  *
279  * \param input    The buffer holding the data.
280  * \param ilen     The length of the input data.
281  * \param output   The SHA-224 or SHA-256 checksum result.
282  * \param is224    Determines which function to use.
283  *                 <ul><li>0: Use SHA-256.</li>
284  *                 <li>1: Use SHA-224.</li></ul>
285  */
286 MBEDTLS_DEPRECATED void mbedtls_sha256( const unsigned char *input,
287                                         size_t ilen,
288                                         unsigned char output[32],
289                                         int is224 );
290 
291 #undef MBEDTLS_DEPRECATED
292 #endif /* !MBEDTLS_DEPRECATED_REMOVED */
293 
294 /**
295  * \brief          The SHA-224 and SHA-256 checkup routine.
296  *
297  * \return         \c 0 on success, or \c 1 on failure.
298  */
299 int mbedtls_sha256_self_test( int verbose );
300 
301 #ifdef __cplusplus
302 }
303 #endif
304 
305 #endif /* mbedtls_sha256.h */
306