Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:50

0001 /**
0002  * \file sha256.h
0003  *
0004  * \brief This file contains SHA-224 and SHA-256 definitions and functions.
0005  *
0006  * The Secure Hash Algorithms 224 and 256 (SHA-224 and SHA-256) cryptographic
0007  * hash functions are defined in <em>FIPS 180-4: Secure Hash Standard (SHS)</em>.
0008  */
0009 /*
0010  *  Copyright The Mbed TLS Contributors
0011  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
0012  */
0013 #ifndef MBEDTLS_SHA256_H
0014 #define MBEDTLS_SHA256_H
0015 #include "mbedtls/private_access.h"
0016 
0017 #include "tf-psa-crypto/build_info.h"
0018 
0019 #include <stddef.h>
0020 #include <stdint.h>
0021 
0022 /** SHA-256 input data was malformed. */
0023 #define MBEDTLS_ERR_SHA256_BAD_INPUT_DATA                 PSA_ERROR_INVALID_ARGUMENT
0024 
0025 #ifdef __cplusplus
0026 extern "C" {
0027 #endif
0028 
0029 /**
0030  * \brief          The SHA-256 context structure.
0031  *
0032  *                 The structure is used both for SHA-256 and for SHA-224
0033  *                 checksum calculations. The choice between these two is
0034  *                 made in the call to mbedtls_sha256_starts().
0035  */
0036 typedef struct mbedtls_sha256_context {
0037     unsigned char MBEDTLS_PRIVATE(buffer)[64];   /*!< The data block being processed. */
0038     uint32_t MBEDTLS_PRIVATE(total)[2];          /*!< The number of Bytes processed.  */
0039     uint32_t MBEDTLS_PRIVATE(state)[8];          /*!< The intermediate digest state.  */
0040 #if defined(MBEDTLS_SHA224_C)
0041     int MBEDTLS_PRIVATE(is224);                  /*!< Determines which function to use:
0042                                                     0: Use SHA-256, or 1: Use SHA-224. */
0043 #endif
0044 }
0045 mbedtls_sha256_context;
0046 
0047 #if defined(MBEDTLS_DECLARE_PRIVATE_IDENTIFIERS)
0048 /**
0049  * \brief          This function initializes a SHA-256 context.
0050  *
0051  * \param ctx      The SHA-256 context to initialize. This must not be \c NULL.
0052  */
0053 void mbedtls_sha256_init(mbedtls_sha256_context *ctx);
0054 
0055 /**
0056  * \brief          This function clears a SHA-256 context.
0057  *
0058  * \param ctx      The SHA-256 context to clear. This may be \c NULL, in which
0059  *                 case this function returns immediately. If it is not \c NULL,
0060  *                 it must point to an initialized SHA-256 context.
0061  */
0062 void mbedtls_sha256_free(mbedtls_sha256_context *ctx);
0063 
0064 /**
0065  * \brief          This function clones the state of a SHA-256 context.
0066  *
0067  * \param dst      The destination context. This must be initialized.
0068  * \param src      The context to clone. This must be initialized.
0069  */
0070 void mbedtls_sha256_clone(mbedtls_sha256_context *dst,
0071                           const mbedtls_sha256_context *src);
0072 
0073 /**
0074  * \brief          This function starts a SHA-224 or SHA-256 checksum
0075  *                 calculation.
0076  *
0077  * \param ctx      The context to use. This must be initialized.
0078  * \param is224    This determines which function to use. This must be
0079  *                 either \c 0 for SHA-256, or \c 1 for SHA-224.
0080  *
0081  * \note           is224 must be defined accordingly to the enabled
0082  *                 MBEDTLS_SHA224_C/MBEDTLS_SHA256_C symbols otherwise the
0083  *                 function will return #MBEDTLS_ERR_SHA512_BAD_INPUT_DATA.
0084  *
0085  * \return         \c 0 on success.
0086  * \return         A negative error code on failure.
0087  */
0088 int mbedtls_sha256_starts(mbedtls_sha256_context *ctx, int is224);
0089 
0090 /**
0091  * \brief          This function feeds an input buffer into an ongoing
0092  *                 SHA-256 checksum calculation.
0093  *
0094  * \param ctx      The SHA-256 context. This must be initialized
0095  *                 and have a hash operation started.
0096  * \param input    The buffer holding the data. This must be a readable
0097  *                 buffer of length \p ilen Bytes.
0098  * \param ilen     The length of the input data in Bytes.
0099  *
0100  * \return         \c 0 on success.
0101  * \return         A negative error code on failure.
0102  */
0103 int mbedtls_sha256_update(mbedtls_sha256_context *ctx,
0104                           const unsigned char *input,
0105                           size_t ilen);
0106 
0107 /**
0108  * \brief          This function finishes the SHA-256 operation, and writes
0109  *                 the result to the output buffer.
0110  *
0111  * \param ctx      The SHA-256 context. This must be initialized
0112  *                 and have a hash operation started.
0113  * \param output   The SHA-224 or SHA-256 checksum result.
0114  *                 This must be a writable buffer of length \c 32 bytes
0115  *                 for SHA-256, \c 28 bytes for SHA-224.
0116  *
0117  * \return         \c 0 on success.
0118  * \return         A negative error code on failure.
0119  */
0120 int mbedtls_sha256_finish(mbedtls_sha256_context *ctx,
0121                           unsigned char *output);
0122 
0123 /**
0124  * \brief          This function calculates the SHA-224 or SHA-256
0125  *                 checksum of a buffer.
0126  *
0127  *                 The function allocates the context, performs the
0128  *                 calculation, and frees the context.
0129  *
0130  *                 The SHA-256 result is calculated as
0131  *                 output = SHA-256(input buffer).
0132  *
0133  * \param input    The buffer holding the data. This must be a readable
0134  *                 buffer of length \p ilen Bytes.
0135  * \param ilen     The length of the input data in Bytes.
0136  * \param output   The SHA-224 or SHA-256 checksum result.
0137  *                 This must be a writable buffer of length \c 32 bytes
0138  *                 for SHA-256, \c 28 bytes for SHA-224.
0139  * \param is224    Determines which function to use. This must be
0140  *                 either \c 0 for SHA-256, or \c 1 for SHA-224.
0141  *
0142  * \return         \c 0 on success.
0143  * \return         A negative error code on failure.
0144  */
0145 int mbedtls_sha256(const unsigned char *input,
0146                    size_t ilen,
0147                    unsigned char *output,
0148                    int is224);
0149 
0150 #if defined(MBEDTLS_SELF_TEST)
0151 
0152 #if defined(MBEDTLS_SHA224_C)
0153 /**
0154  * \brief          The SHA-224 checkup routine.
0155  *
0156  * \return         \c 0 on success.
0157  * \return         \c 1 on failure.
0158  */
0159 int mbedtls_sha224_self_test(int verbose);
0160 #endif /* MBEDTLS_SHA224_C */
0161 
0162 #if defined(MBEDTLS_SHA256_C)
0163 /**
0164  * \brief          The SHA-256 checkup routine.
0165  *
0166  * \return         \c 0 on success.
0167  * \return         \c 1 on failure.
0168  */
0169 int mbedtls_sha256_self_test(int verbose);
0170 #endif /* MBEDTLS_SHA256_C */
0171 
0172 #endif /* MBEDTLS_SELF_TEST */
0173 
0174 #endif /* MBEDTLS_DECLARE_PRIVATE_IDENTIFIERS */
0175 
0176 #ifdef __cplusplus
0177 }
0178 #endif
0179 
0180 #endif /* mbedtls_sha256.h */