1 /**
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 The Mbed TLS Contributors
11  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
12  */
13 #ifndef MBEDTLS_SHA256_H
14 #define MBEDTLS_SHA256_H
15 #include "mbedtls/private_access.h"
16 
17 #include "mbedtls/build_info.h"
18 
19 #include <stddef.h>
20 #include <stdint.h>
21 
22 /** SHA-256 input data was malformed. */
23 #define MBEDTLS_ERR_SHA256_BAD_INPUT_DATA                 -0x0074
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
29 #if !defined(MBEDTLS_SHA256_ALT)
30 // Regular implementation
31 //
32 
33 /**
34  * \brief          The SHA-256 context structure.
35  *
36  *                 The structure is used both for SHA-256 and for SHA-224
37  *                 checksum calculations. The choice between these two is
38  *                 made in the call to mbedtls_sha256_starts().
39  */
40 typedef struct mbedtls_sha256_context {
41     unsigned char MBEDTLS_PRIVATE(buffer)[64];   /*!< The data block being processed. */
42     uint32_t MBEDTLS_PRIVATE(total)[2];          /*!< The number of Bytes processed.  */
43     uint32_t MBEDTLS_PRIVATE(state)[8];          /*!< The intermediate digest state.  */
44     int MBEDTLS_PRIVATE(is224);                  /*!< Determines which function to use:
45                                                     0: Use SHA-256, or 1: Use SHA-224. */
46 }
47 mbedtls_sha256_context;
48 
49 #else  /* MBEDTLS_SHA256_ALT */
50 #include "sha256_alt.h"
51 #endif /* MBEDTLS_SHA256_ALT */
52 
53 /**
54  * \brief          This function initializes a SHA-256 context.
55  *
56  * \param ctx      The SHA-256 context to initialize. This must not be \c NULL.
57  */
58 void mbedtls_sha256_init(mbedtls_sha256_context *ctx);
59 
60 /**
61  * \brief          This function clears a SHA-256 context.
62  *
63  * \param ctx      The SHA-256 context to clear. This may be \c NULL, in which
64  *                 case this function returns immediately. If it is not \c NULL,
65  *                 it must point to an initialized SHA-256 context.
66  */
67 void mbedtls_sha256_free(mbedtls_sha256_context *ctx);
68 
69 /**
70  * \brief          This function clones the state of a SHA-256 context.
71  *
72  * \param dst      The destination context. This must be initialized.
73  * \param src      The context to clone. This must be initialized.
74  */
75 void mbedtls_sha256_clone(mbedtls_sha256_context *dst,
76                           const mbedtls_sha256_context *src);
77 
78 /**
79  * \brief          This function starts a SHA-224 or SHA-256 checksum
80  *                 calculation.
81  *
82  * \param ctx      The context to use. This must be initialized.
83  * \param is224    This determines which function to use. This must be
84  *                 either \c 0 for SHA-256, or \c 1 for SHA-224.
85  *
86  * \note           is224 must be defined accordingly to the enabled
87  *                 MBEDTLS_SHA224_C/MBEDTLS_SHA256_C symbols otherwise the
88  *                 function will return #MBEDTLS_ERR_SHA512_BAD_INPUT_DATA.
89  *
90  * \return         \c 0 on success.
91  * \return         A negative error code on failure.
92  */
93 int mbedtls_sha256_starts(mbedtls_sha256_context *ctx, int is224);
94 
95 /**
96  * \brief          This function feeds an input buffer into an ongoing
97  *                 SHA-256 checksum calculation.
98  *
99  * \param ctx      The SHA-256 context. This must be initialized
100  *                 and have a hash operation started.
101  * \param input    The buffer holding the data. This must be a readable
102  *                 buffer of length \p ilen Bytes.
103  * \param ilen     The length of the input data in Bytes.
104  *
105  * \return         \c 0 on success.
106  * \return         A negative error code on failure.
107  */
108 int mbedtls_sha256_update(mbedtls_sha256_context *ctx,
109                           const unsigned char *input,
110                           size_t ilen);
111 
112 /**
113  * \brief          This function finishes the SHA-256 operation, and writes
114  *                 the result to the output buffer.
115  *
116  * \param ctx      The SHA-256 context. This must be initialized
117  *                 and have a hash operation started.
118  * \param output   The SHA-224 or SHA-256 checksum result.
119  *                 This must be a writable buffer of length \c 32 bytes
120  *                 for SHA-256, \c 28 bytes for SHA-224.
121  *
122  * \return         \c 0 on success.
123  * \return         A negative error code on failure.
124  */
125 int mbedtls_sha256_finish(mbedtls_sha256_context *ctx,
126                           unsigned char *output);
127 
128 /**
129  * \brief          This function processes a single data block within
130  *                 the ongoing SHA-256 computation. This function is for
131  *                 internal use only.
132  *
133  * \param ctx      The SHA-256 context. This must be initialized.
134  * \param data     The buffer holding one block of data. This must
135  *                 be a readable buffer of length \c 64 Bytes.
136  *
137  * \return         \c 0 on success.
138  * \return         A negative error code on failure.
139  */
140 int mbedtls_internal_sha256_process(mbedtls_sha256_context *ctx,
141                                     const unsigned char data[64]);
142 
143 /**
144  * \brief          This function calculates the SHA-224 or SHA-256
145  *                 checksum of a buffer.
146  *
147  *                 The function allocates the context, performs the
148  *                 calculation, and frees the context.
149  *
150  *                 The SHA-256 result is calculated as
151  *                 output = SHA-256(input buffer).
152  *
153  * \param input    The buffer holding the data. This must be a readable
154  *                 buffer of length \p ilen Bytes.
155  * \param ilen     The length of the input data in Bytes.
156  * \param output   The SHA-224 or SHA-256 checksum result.
157  *                 This must be a writable buffer of length \c 32 bytes
158  *                 for SHA-256, \c 28 bytes for SHA-224.
159  * \param is224    Determines which function to use. This must be
160  *                 either \c 0 for SHA-256, or \c 1 for SHA-224.
161  *
162  * \return         \c 0 on success.
163  * \return         A negative error code on failure.
164  */
165 int mbedtls_sha256(const unsigned char *input,
166                    size_t ilen,
167                    unsigned char *output,
168                    int is224);
169 
170 #if defined(MBEDTLS_SELF_TEST)
171 
172 #if defined(MBEDTLS_SHA224_C)
173 /**
174  * \brief          The SHA-224 checkup routine.
175  *
176  * \return         \c 0 on success.
177  * \return         \c 1 on failure.
178  */
179 int mbedtls_sha224_self_test(int verbose);
180 #endif /* MBEDTLS_SHA224_C */
181 
182 #if defined(MBEDTLS_SHA256_C)
183 /**
184  * \brief          The SHA-256 checkup routine.
185  *
186  * \return         \c 0 on success.
187  * \return         \c 1 on failure.
188  */
189 int mbedtls_sha256_self_test(int verbose);
190 #endif /* MBEDTLS_SHA256_C */
191 
192 #endif /* MBEDTLS_SELF_TEST */
193 
194 #ifdef __cplusplus
195 }
196 #endif
197 
198 #endif /* mbedtls_sha256.h */
199