blob: edeb157bc4e3934334991e5d28f2e1b1039cdf6f [file] [log] [blame]
Jaeden Ameroe54e6932018-08-06 16:19:58 +01001/**
2 * \file sha256.h
3 *
4 * \brief This file contains SHA-224 and SHA-256 definitions and functions.
5 *
6 * The Secure Hash Algorithms 224 and 256 (SHA-224 and SHA-256) cryptographic
7 * hash functions are defined in <em>FIPS 180-4: Secure Hash Standard (SHS)</em>.
8 */
9/*
10 * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
11 * SPDX-License-Identifier: Apache-2.0
12 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
24 *
25 * This file is part of Mbed Crypto (https://tls.mbed.org)
26 */
27#ifndef MBEDCRYPTO_SHA256_H
28#define MBEDCRYPTO_SHA256_H
29
30#if !defined(MBEDCRYPTO_CONFIG_FILE)
31#include "config.h"
32#else
33#include MBEDCRYPTO_CONFIG_FILE
34#endif
35
36#include <stddef.h>
37#include <stdint.h>
38
39#define MBEDCRYPTO_ERR_SHA256_HW_ACCEL_FAILED -0x0037 /**< SHA-256 hardware accelerator failed */
40
41#ifdef __cplusplus
42extern "C" {
43#endif
44
45#if !defined(MBEDCRYPTO_SHA256_ALT)
46// Regular implementation
47//
48
49/**
50 * \brief The SHA-256 context structure.
51 *
52 * The structure is used both for SHA-256 and for SHA-224
53 * checksum calculations. The choice between these two is
54 * made in the call to mbedcrypto_sha256_starts_ret().
55 */
56typedef struct
57{
58 uint32_t total[2]; /*!< The number of Bytes processed. */
59 uint32_t state[8]; /*!< The intermediate digest state. */
60 unsigned char buffer[64]; /*!< The data block being processed. */
61 int is224; /*!< Determines which function to use:
62 0: Use SHA-256, or 1: Use SHA-224. */
63}
64mbedcrypto_sha256_context;
65
66#else /* MBEDCRYPTO_SHA256_ALT */
67#include "sha256_alt.h"
68#endif /* MBEDCRYPTO_SHA256_ALT */
69
70/**
71 * \brief This function initializes a SHA-256 context.
72 *
73 * \param ctx The SHA-256 context to initialize.
74 */
75void mbedcrypto_sha256_init( mbedcrypto_sha256_context *ctx );
76
77/**
78 * \brief This function clears a SHA-256 context.
79 *
80 * \param ctx The SHA-256 context to clear.
81 */
82void mbedcrypto_sha256_free( mbedcrypto_sha256_context *ctx );
83
84/**
85 * \brief This function clones the state of a SHA-256 context.
86 *
87 * \param dst The destination context.
88 * \param src The context to clone.
89 */
90void mbedcrypto_sha256_clone( mbedcrypto_sha256_context *dst,
91 const mbedcrypto_sha256_context *src );
92
93/**
94 * \brief This function starts a SHA-224 or SHA-256 checksum
95 * calculation.
96 *
97 * \param ctx The context to initialize.
98 * \param is224 Determines which function to use:
99 * 0: Use SHA-256, or 1: Use SHA-224.
100 *
101 * \return \c 0 on success.
102 */
103int mbedcrypto_sha256_starts_ret( mbedcrypto_sha256_context *ctx, int is224 );
104
105/**
106 * \brief This function feeds an input buffer into an ongoing
107 * SHA-256 checksum calculation.
108 *
109 * \param ctx The SHA-256 context.
110 * \param input The buffer holding the data.
111 * \param ilen The length of the input data.
112 *
113 * \return \c 0 on success.
114 */
115int mbedcrypto_sha256_update_ret( mbedcrypto_sha256_context *ctx,
116 const unsigned char *input,
117 size_t ilen );
118
119/**
120 * \brief This function finishes the SHA-256 operation, and writes
121 * the result to the output buffer.
122 *
123 * \param ctx The SHA-256 context.
124 * \param output The SHA-224 or SHA-256 checksum result.
125 *
126 * \return \c 0 on success.
127 */
128int mbedcrypto_sha256_finish_ret( mbedcrypto_sha256_context *ctx,
129 unsigned char output[32] );
130
131/**
132 * \brief This function processes a single data block within
133 * the ongoing SHA-256 computation. This function is for
134 * internal use only.
135 *
136 * \param ctx The SHA-256 context.
137 * \param data The buffer holding one block of data.
138 *
139 * \return \c 0 on success.
140 */
141int mbedcrypto_internal_sha256_process( mbedcrypto_sha256_context *ctx,
142 const unsigned char data[64] );
143
144#if !defined(MBEDCRYPTO_DEPRECATED_REMOVED)
145#if defined(MBEDCRYPTO_DEPRECATED_WARNING)
146#define MBEDCRYPTO_DEPRECATED __attribute__((deprecated))
147#else
148#define MBEDCRYPTO_DEPRECATED
149#endif
150/**
151 * \brief This function starts a SHA-224 or SHA-256 checksum
152 * calculation.
153 *
154 *
155 * \deprecated Superseded by mbedcrypto_sha256_starts_ret() in 2.7.0.
156 *
157 * \param ctx The context to initialize.
158 * \param is224 Determines which function to use:
159 * 0: Use SHA-256, or 1: Use SHA-224.
160 */
161MBEDCRYPTO_DEPRECATED void mbedcrypto_sha256_starts( mbedcrypto_sha256_context *ctx,
162 int is224 );
163
164/**
165 * \brief This function feeds an input buffer into an ongoing
166 * SHA-256 checksum calculation.
167 *
168 * \deprecated Superseded by mbedcrypto_sha256_update_ret() in 2.7.0.
169 *
170 * \param ctx The SHA-256 context to initialize.
171 * \param input The buffer holding the data.
172 * \param ilen The length of the input data.
173 */
174MBEDCRYPTO_DEPRECATED void mbedcrypto_sha256_update( mbedcrypto_sha256_context *ctx,
175 const unsigned char *input,
176 size_t ilen );
177
178/**
179 * \brief This function finishes the SHA-256 operation, and writes
180 * the result to the output buffer.
181 *
182 * \deprecated Superseded by mbedcrypto_sha256_finish_ret() in 2.7.0.
183 *
184 * \param ctx The SHA-256 context.
185 * \param output The SHA-224 or SHA-256 checksum result.
186 */
187MBEDCRYPTO_DEPRECATED void mbedcrypto_sha256_finish( mbedcrypto_sha256_context *ctx,
188 unsigned char output[32] );
189
190/**
191 * \brief This function processes a single data block within
192 * the ongoing SHA-256 computation. This function is for
193 * internal use only.
194 *
195 * \deprecated Superseded by mbedcrypto_internal_sha256_process() in 2.7.0.
196 *
197 * \param ctx The SHA-256 context.
198 * \param data The buffer holding one block of data.
199 */
200MBEDCRYPTO_DEPRECATED void mbedcrypto_sha256_process( mbedcrypto_sha256_context *ctx,
201 const unsigned char data[64] );
202
203#undef MBEDCRYPTO_DEPRECATED
204#endif /* !MBEDCRYPTO_DEPRECATED_REMOVED */
205
206/**
207 * \brief This function calculates the SHA-224 or SHA-256
208 * checksum of a buffer.
209 *
210 * The function allocates the context, performs the
211 * calculation, and frees the context.
212 *
213 * The SHA-256 result is calculated as
214 * output = SHA-256(input buffer).
215 *
216 * \param input The buffer holding the input data.
217 * \param ilen The length of the input data.
218 * \param output The SHA-224 or SHA-256 checksum result.
219 * \param is224 Determines which function to use:
220 * 0: Use SHA-256, or 1: Use SHA-224.
221 */
222int mbedcrypto_sha256_ret( const unsigned char *input,
223 size_t ilen,
224 unsigned char output[32],
225 int is224 );
226
227#if !defined(MBEDCRYPTO_DEPRECATED_REMOVED)
228#if defined(MBEDCRYPTO_DEPRECATED_WARNING)
229#define MBEDCRYPTO_DEPRECATED __attribute__((deprecated))
230#else
231#define MBEDCRYPTO_DEPRECATED
232#endif
233
234/**
235 * \brief This function calculates the SHA-224 or SHA-256 checksum
236 * of a buffer.
237 *
238 * The function allocates the context, performs the
239 * calculation, and frees the context.
240 *
241 * The SHA-256 result is calculated as
242 * output = SHA-256(input buffer).
243 *
244 * \deprecated Superseded by mbedcrypto_sha256_ret() in 2.7.0.
245 *
246 * \param input The buffer holding the data.
247 * \param ilen The length of the input data.
248 * \param output The SHA-224 or SHA-256 checksum result.
249 * \param is224 Determines which function to use:
250 * 0: Use SHA-256, or 1: Use SHA-224.
251 */
252MBEDCRYPTO_DEPRECATED void mbedcrypto_sha256( const unsigned char *input,
253 size_t ilen,
254 unsigned char output[32],
255 int is224 );
256
257#undef MBEDCRYPTO_DEPRECATED
258#endif /* !MBEDCRYPTO_DEPRECATED_REMOVED */
259
260/**
261 * \brief The SHA-224 and SHA-256 checkup routine.
262 *
263 * \return \c 0 on success.
264 * \return \c 1 on failure.
265 */
266int mbedcrypto_sha256_self_test( int verbose );
267
268#ifdef __cplusplus
269}
270#endif
271
272#endif /* mbedcrypto_sha256.h */