Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-08-27 09:37:33

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 "mbedtls/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                 -0x0075
0023 
0024 #ifdef __cplusplus
0025 extern "C" {
0026 #endif
0027 
0028 #if !defined(MBEDTLS_SHA512_ALT)
0029 // Regular implementation
0030 //
0031 
0032 /**
0033  * \brief          The SHA-512 context structure.
0034  *
0035  *                 The structure is used both for SHA-384 and for SHA-512
0036  *                 checksum calculations. The choice between these two is
0037  *                 made in the call to mbedtls_sha512_starts().
0038  */
0039 typedef struct mbedtls_sha512_context {
0040     uint64_t MBEDTLS_PRIVATE(total)[2];          /*!< The number of Bytes processed. */
0041     uint64_t MBEDTLS_PRIVATE(state)[8];          /*!< The intermediate digest state. */
0042     unsigned char MBEDTLS_PRIVATE(buffer)[128];  /*!< The data block being processed. */
0043 #if defined(MBEDTLS_SHA384_C)
0044     int MBEDTLS_PRIVATE(is384);                  /*!< Determines which function to use:
0045                                                       0: Use SHA-512, or 1: Use SHA-384. */
0046 #endif
0047 }
0048 mbedtls_sha512_context;
0049 
0050 #else  /* MBEDTLS_SHA512_ALT */
0051 #include "sha512_alt.h"
0052 #endif /* MBEDTLS_SHA512_ALT */
0053 
0054 /**
0055  * \brief          This function initializes a SHA-512 context.
0056  *
0057  * \param ctx      The SHA-512 context to initialize. This must
0058  *                 not be \c NULL.
0059  */
0060 void mbedtls_sha512_init(mbedtls_sha512_context *ctx);
0061 
0062 /**
0063  * \brief          This function clears a SHA-512 context.
0064  *
0065  * \param ctx      The SHA-512 context to clear. This may be \c NULL,
0066  *                 in which case this function does nothing. If it
0067  *                 is not \c NULL, it must point to an initialized
0068  *                 SHA-512 context.
0069  */
0070 void mbedtls_sha512_free(mbedtls_sha512_context *ctx);
0071 
0072 /**
0073  * \brief          This function clones the state of a SHA-512 context.
0074  *
0075  * \param dst      The destination context. This must be initialized.
0076  * \param src      The context to clone. This must be initialized.
0077  */
0078 void mbedtls_sha512_clone(mbedtls_sha512_context *dst,
0079                           const mbedtls_sha512_context *src);
0080 
0081 /**
0082  * \brief          This function starts a SHA-384 or SHA-512 checksum
0083  *                 calculation.
0084  *
0085  * \param ctx      The SHA-512 context to use. This must be initialized.
0086  * \param is384    Determines which function to use. This must be
0087  *                 either \c 0 for SHA-512, or \c 1 for SHA-384.
0088  *
0089  * \note           is384 must be defined accordingly to the enabled
0090  *                 MBEDTLS_SHA384_C/MBEDTLS_SHA512_C symbols otherwise the
0091  *                 function will return #MBEDTLS_ERR_SHA512_BAD_INPUT_DATA.
0092  *
0093  * \return         \c 0 on success.
0094  * \return         A negative error code on failure.
0095  */
0096 int mbedtls_sha512_starts(mbedtls_sha512_context *ctx, int is384);
0097 
0098 /**
0099  * \brief          This function feeds an input buffer into an ongoing
0100  *                 SHA-512 checksum calculation.
0101  *
0102  * \param ctx      The SHA-512 context. This must be initialized
0103  *                 and have a hash operation started.
0104  * \param input    The buffer holding the input data. This must
0105  *                 be a readable buffer of length \p ilen Bytes.
0106  * \param ilen     The length of the input data in Bytes.
0107  *
0108  * \return         \c 0 on success.
0109  * \return         A negative error code on failure.
0110  */
0111 int mbedtls_sha512_update(mbedtls_sha512_context *ctx,
0112                           const unsigned char *input,
0113                           size_t ilen);
0114 
0115 /**
0116  * \brief          This function finishes the SHA-512 operation, and writes
0117  *                 the result to the output buffer.
0118  *
0119  * \param ctx      The SHA-512 context. This must be initialized
0120  *                 and have a hash operation started.
0121  * \param output   The SHA-384 or SHA-512 checksum result.
0122  *                 This must be a writable buffer of length \c 64 bytes
0123  *                 for SHA-512, \c 48 bytes for SHA-384.
0124  *
0125  * \return         \c 0 on success.
0126  * \return         A negative error code on failure.
0127  */
0128 int mbedtls_sha512_finish(mbedtls_sha512_context *ctx,
0129                           unsigned char *output);
0130 
0131 /**
0132  * \brief          This function processes a single data block within
0133  *                 the ongoing SHA-512 computation.
0134  *                 This function is for internal use only.
0135  *
0136  * \param ctx      The SHA-512 context. This must be initialized.
0137  * \param data     The buffer holding one block of data. This
0138  *                 must be a readable buffer of length \c 128 Bytes.
0139  *
0140  * \return         \c 0 on success.
0141  * \return         A negative error code on failure.
0142  */
0143 int mbedtls_internal_sha512_process(mbedtls_sha512_context *ctx,
0144                                     const unsigned char data[128]);
0145 
0146 /**
0147  * \brief          This function calculates the SHA-512 or SHA-384
0148  *                 checksum of a buffer.
0149  *
0150  *                 The function allocates the context, performs the
0151  *                 calculation, and frees the context.
0152  *
0153  *                 The SHA-512 result is calculated as
0154  *                 output = SHA-512(input buffer).
0155  *
0156  * \param input    The buffer holding the input data. This must be
0157  *                 a readable buffer of length \p ilen Bytes.
0158  * \param ilen     The length of the input data in Bytes.
0159  * \param output   The SHA-384 or SHA-512 checksum result.
0160  *                 This must be a writable buffer of length \c 64 bytes
0161  *                 for SHA-512, \c 48 bytes for SHA-384.
0162  * \param is384    Determines which function to use. This must be either
0163  *                 \c 0 for SHA-512, or \c 1 for SHA-384.
0164  *
0165  * \note           is384 must be defined accordingly with the supported
0166  *                 symbols in the config file. If:
0167  *                 - is384 is 0, but \c MBEDTLS_SHA384_C is not defined, or
0168  *                 - is384 is 1, but \c MBEDTLS_SHA512_C is not defined
0169  *                 then the function will return
0170  *                 #MBEDTLS_ERR_SHA512_BAD_INPUT_DATA.
0171  *
0172  * \return         \c 0 on success.
0173  * \return         A negative error code on failure.
0174  */
0175 int mbedtls_sha512(const unsigned char *input,
0176                    size_t ilen,
0177                    unsigned char *output,
0178                    int is384);
0179 
0180 #if defined(MBEDTLS_SELF_TEST)
0181 
0182 #if defined(MBEDTLS_SHA384_C)
0183 /**
0184  * \brief          The SHA-384 checkup routine.
0185  *
0186  * \return         \c 0 on success.
0187  * \return         \c 1 on failure.
0188  */
0189 int mbedtls_sha384_self_test(int verbose);
0190 #endif /* MBEDTLS_SHA384_C */
0191 
0192 #if defined(MBEDTLS_SHA512_C)
0193 /**
0194  * \brief          The SHA-512 checkup routine.
0195  *
0196  * \return         \c 0 on success.
0197  * \return         \c 1 on failure.
0198  */
0199 int mbedtls_sha512_self_test(int verbose);
0200 #endif /* MBEDTLS_SHA512_C */
0201 
0202 #endif /* MBEDTLS_SELF_TEST */
0203 
0204 #ifdef __cplusplus
0205 }
0206 #endif
0207 
0208 #endif /* mbedtls_sha512.h */