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 (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
11  *  Copyright (C) 2018-2022, Intel Corporation.
12  *  SPDX-License-Identifier: Apache-2.0
13  *
14  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
15  *  not use this file except in compliance with the License.
16  *  You may obtain a copy of the License at
17  *
18  *  http://www.apache.org/licenses/LICENSE-2.0
19  *
20  *  Unless required by applicable law or agreed to in writing, software
21  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
22  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23  *  See the License for the specific language governing permissions and
24  *  limitations under the License.
25  *
26  *  This file is part of Mbed TLS (https://tls.mbed.org)
27  */
28 #ifndef MBEDTLS_SHA256_H
29 #define MBEDTLS_SHA256_H
30 
31 #include <types.h>
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_ret().
39  */
40 typedef struct
41 {
42     uint32_t total[2];          /*!< The number of Bytes processed.  */
43     uint32_t state[8];          /*!< The intermediate digest state.  */
44     uint8_t buffer[64];   /*!< The data block being processed. */
45     int32_t is224;                  /*!< Determines which function to use:
46                                      0: Use SHA-256, or 1: Use SHA-224. */
47 }
48 mbedtls_sha256_context;
49 
50 /**
51  * \brief          This function initializes a SHA-256 context.
52  *
53  * \param ctx      The SHA-256 context to initialize.
54  */
55 void mbedtls_sha256_init( mbedtls_sha256_context *ctx );
56 
57 /**
58  * \brief          This function clears a SHA-256 context.
59  *
60  * \param ctx      The SHA-256 context to clear.
61  */
62 void mbedtls_sha256_free( mbedtls_sha256_context *ctx );
63 
64 /**
65  * \brief          This function clones the state of a SHA-256 context.
66  *
67  * \param dst      The destination context.
68  * \param src      The context to clone.
69  */
70 void mbedtls_sha256_clone( mbedtls_sha256_context *dst,
71                            const mbedtls_sha256_context *src );
72 
73 /**
74  * \brief          This function starts a SHA-224 or SHA-256 checksum
75  *                 calculation.
76  *
77  * \param ctx      The context to initialize.
78  * \param is224    Determines which function to use:
79  *                 0: Use SHA-256, or 1: Use SHA-224.
80  *
81  * \return         \c 0 on success.
82  */
83 int32_t mbedtls_sha256_starts_ret( mbedtls_sha256_context *ctx, int32_t is224 );
84 
85 /**
86  * \brief          This function feeds an input buffer into an ongoing
87  *                 SHA-256 checksum calculation.
88  *
89  * \param ctx      The SHA-256 context.
90  * \param input    The buffer holding the data.
91  * \param ilen     The length of the input data.
92  *
93  * \return         \c 0 on success.
94  */
95 int32_t mbedtls_sha256_update_ret( mbedtls_sha256_context *ctx,
96                                const uint8_t *input,
97                                size_t ilen );
98 
99 /**
100  * \brief          This function finishes the SHA-256 operation, and writes
101  *                 the result to the output buffer.
102  *
103  * \param ctx      The SHA-256 context.
104  * \param output   The SHA-224 or SHA-256 checksum result.
105  *
106  * \return         \c 0 on success.
107  */
108 int32_t mbedtls_sha256_finish_ret( mbedtls_sha256_context *ctx,
109                                uint8_t output[32] );
110 
111 /**
112  * \brief          This function processes a single data block within
113  *                 the ongoing SHA-256 computation. This function is for
114  *                 internal use only.
115  *
116  * \param ctx      The SHA-256 context.
117  * \param data     The buffer holding one block of data.
118  *
119  * \return         \c 0 on success.
120  */
121 int32_t mbedtls_internal_sha256_process( mbedtls_sha256_context *ctx,
122                                      const uint8_t data[64] );
123 
124 /**
125  * \brief          This function calculates the SHA-224 or SHA-256
126  *                 checksum of a buffer.
127  *
128  *                 The function allocates the context, performs the
129  *                 calculation, and frees the context.
130  *
131  *                 The SHA-256 result is calculated as
132  *                 output = SHA-256(input buffer).
133  *
134  * \param input    The buffer holding the input data.
135  * \param ilen     The length of the input data.
136  * \param output   The SHA-224 or SHA-256 checksum result.
137  * \param is224    Determines which function to use:
138  *                 0: Use SHA-256, or 1: Use SHA-224.
139  */
140 int32_t mbedtls_sha256_ret( const uint8_t *input,
141                         size_t ilen,
142                         uint8_t output[32],
143                         int32_t is224 );
144 
145 #endif /* mbedtls_sha256.h */
146