Back to home page

EIC code displayed by LXR

 
 

    


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

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