Knight KE / Mbed OS Game_Master
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers sha512.h Source File

sha512.h

Go to the documentation of this file.
00001 /**
00002  * \file sha512.h
00003  * \brief This file contains SHA-384 and SHA-512 definitions and functions.
00004  *
00005  * The Secure Hash Algorithms 384 and 512 (SHA-384 and SHA-512) cryptographic
00006  * hash functions are defined in <em>FIPS 180-4: Secure Hash Standard (SHS)</em>.
00007  */
00008 /*
00009  *  Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
00010  *  SPDX-License-Identifier: Apache-2.0
00011  *
00012  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
00013  *  not use this file except in compliance with the License.
00014  *  You may obtain a copy of the License at
00015  *
00016  *  http://www.apache.org/licenses/LICENSE-2.0
00017  *
00018  *  Unless required by applicable law or agreed to in writing, software
00019  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00020  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00021  *  See the License for the specific language governing permissions and
00022  *  limitations under the License.
00023  *
00024  *  This file is part of Mbed TLS (https://tls.mbed.org)
00025  */
00026 #ifndef MBEDTLS_SHA512_H
00027 #define MBEDTLS_SHA512_H
00028 
00029 #if !defined(MBEDTLS_CONFIG_FILE)
00030 #include "config.h"
00031 #else
00032 #include MBEDTLS_CONFIG_FILE
00033 #endif
00034 
00035 #include <stddef.h>
00036 #include <stdint.h>
00037 
00038 #define MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED                -0x0039  /**< SHA-512 hardware accelerator failed */
00039 
00040 #ifdef __cplusplus
00041 extern "C" {
00042 #endif
00043 
00044 #if !defined(MBEDTLS_SHA512_ALT)
00045 // Regular implementation
00046 //
00047 
00048 /**
00049  * \brief          The SHA-512 context structure.
00050  *
00051  *                 The structure is used both for SHA-384 and for SHA-512
00052  *                 checksum calculations. The choice between these two is
00053  *                 made in the call to mbedtls_sha512_starts_ret().
00054  */
00055 typedef struct
00056 {
00057     uint64_t total[2];          /*!< The number of Bytes processed. */
00058     uint64_t state[8];          /*!< The intermediate digest state. */
00059     unsigned char buffer[128];  /*!< The data block being processed. */
00060     int is384;                  /*!< Determines which function to use:
00061                                      0: Use SHA-512, or 1: Use SHA-384. */
00062 }
00063 mbedtls_sha512_context;
00064 
00065 #else  /* MBEDTLS_SHA512_ALT */
00066 #include "sha512_alt.h"
00067 #endif /* MBEDTLS_SHA512_ALT */
00068 
00069 /**
00070  * \brief          This function initializes a SHA-512 context.
00071  *
00072  * \param ctx      The SHA-512 context to initialize.
00073  */
00074 void mbedtls_sha512_init( mbedtls_sha512_context *ctx );
00075 
00076 /**
00077  * \brief          This function clears a SHA-512 context.
00078  *
00079  * \param ctx      The SHA-512 context to clear.
00080  */
00081 void mbedtls_sha512_free( mbedtls_sha512_context *ctx );
00082 
00083 /**
00084  * \brief          This function clones the state of a SHA-512 context.
00085  *
00086  * \param dst      The destination context.
00087  * \param src      The context to clone.
00088  */
00089 void mbedtls_sha512_clone( mbedtls_sha512_context *dst,
00090                            const mbedtls_sha512_context *src );
00091 
00092 /**
00093  * \brief          This function starts a SHA-384 or SHA-512 checksum
00094  *                 calculation.
00095  *
00096  * \param ctx      The SHA-512 context to initialize.
00097  * \param is384    Determines which function to use:
00098  *                 0: Use SHA-512, or 1: Use SHA-384.
00099  *
00100  * \return         \c 0 on success.
00101  */
00102 int mbedtls_sha512_starts_ret( mbedtls_sha512_context *ctx, int is384 );
00103 
00104 /**
00105  * \brief          This function feeds an input buffer into an ongoing
00106  *                 SHA-512 checksum calculation.
00107  *
00108  * \param ctx      The SHA-512 context.
00109  * \param input    The buffer holding the input data.
00110  * \param ilen     The length of the input data.
00111  *
00112  * \return         \c 0 on success.
00113  */
00114 int mbedtls_sha512_update_ret( mbedtls_sha512_context *ctx,
00115                     const unsigned char *input,
00116                     size_t ilen );
00117 
00118 /**
00119  * \brief          This function finishes the SHA-512 operation, and writes
00120  *                 the result to the output buffer. This function is for
00121  *                 internal use only.
00122  *
00123  * \param ctx      The SHA-512 context.
00124  * \param output   The SHA-384 or SHA-512 checksum result.
00125  *
00126  * \return         \c 0 on success.
00127  */
00128 int mbedtls_sha512_finish_ret( mbedtls_sha512_context *ctx,
00129                                unsigned char output[64] );
00130 
00131 /**
00132  * \brief          This function processes a single data block within
00133  *                 the ongoing SHA-512 computation.
00134  *
00135  * \param ctx      The SHA-512 context.
00136  * \param data     The buffer holding one block of data.
00137  *
00138  * \return         \c 0 on success.
00139  */
00140 int mbedtls_internal_sha512_process( mbedtls_sha512_context *ctx,
00141                                      const unsigned char data[128] );
00142 #if !defined(MBEDTLS_DEPRECATED_REMOVED)
00143 #if defined(MBEDTLS_DEPRECATED_WARNING)
00144 #define MBEDTLS_DEPRECATED      __attribute__((deprecated))
00145 #else
00146 #define MBEDTLS_DEPRECATED
00147 #endif
00148 /**
00149  * \brief          This function starts a SHA-384 or SHA-512 checksum
00150  *                 calculation.
00151  *
00152  * \deprecated     Superseded by mbedtls_sha512_starts_ret() in 2.7.0
00153  *
00154  * \param ctx      The SHA-512 context to initialize.
00155  * \param is384    Determines which function to use:
00156  *                 0: Use SHA-512, or 1: Use SHA-384.
00157  */
00158 MBEDTLS_DEPRECATED void mbedtls_sha512_starts( mbedtls_sha512_context *ctx,
00159                                                int is384 );
00160 
00161 /**
00162  * \brief          This function feeds an input buffer into an ongoing
00163  *                 SHA-512 checksum calculation.
00164  *
00165  * \deprecated     Superseded by mbedtls_sha512_update_ret() in 2.7.0.
00166  *
00167  * \param ctx      The SHA-512 context.
00168  * \param input    The buffer holding the data.
00169  * \param ilen     The length of the input data.
00170  */
00171 MBEDTLS_DEPRECATED void mbedtls_sha512_update( mbedtls_sha512_context *ctx,
00172                                                const unsigned char *input,
00173                                                size_t ilen );
00174 
00175 /**
00176  * \brief          This function finishes the SHA-512 operation, and writes
00177  *                 the result to the output buffer.
00178  *
00179  * \deprecated     Superseded by mbedtls_sha512_finish_ret() in 2.7.0.
00180  *
00181  * \param ctx      The SHA-512 context.
00182  * \param output   The SHA-384 or SHA-512 checksum result.
00183  */
00184 MBEDTLS_DEPRECATED void mbedtls_sha512_finish( mbedtls_sha512_context *ctx,
00185                                                unsigned char output[64] );
00186 
00187 /**
00188  * \brief          This function processes a single data block within
00189  *                 the ongoing SHA-512 computation. This function is for
00190  *                 internal use only.
00191  *
00192  * \deprecated     Superseded by mbedtls_internal_sha512_process() in 2.7.0.
00193  *
00194  * \param ctx      The SHA-512 context.
00195  * \param data     The buffer holding one block of data.
00196  */
00197 MBEDTLS_DEPRECATED void mbedtls_sha512_process(
00198                                             mbedtls_sha512_context *ctx,
00199                                             const unsigned char data[128] );
00200 
00201 #undef MBEDTLS_DEPRECATED
00202 #endif /* !MBEDTLS_DEPRECATED_REMOVED */
00203 
00204 /**
00205  * \brief          This function calculates the SHA-512 or SHA-384
00206  *                 checksum of a buffer.
00207  *
00208  *                 The function allocates the context, performs the
00209  *                 calculation, and frees the context.
00210  *
00211  *                 The SHA-512 result is calculated as
00212  *                 output = SHA-512(input buffer).
00213  *
00214  * \param input    The buffer holding the input data.
00215  * \param ilen     The length of the input data.
00216  * \param output   The SHA-384 or SHA-512 checksum result.
00217  * \param is384    Determines which function to use:
00218  *                 0: Use SHA-512, or 1: Use SHA-384.
00219  *
00220  * \return         \c 0 on success.
00221  */
00222 int mbedtls_sha512_ret( const unsigned char *input,
00223                         size_t ilen,
00224                         unsigned char output[64],
00225                         int is384 );
00226 
00227 #if !defined(MBEDTLS_DEPRECATED_REMOVED)
00228 #if defined(MBEDTLS_DEPRECATED_WARNING)
00229 #define MBEDTLS_DEPRECATED      __attribute__((deprecated))
00230 #else
00231 #define MBEDTLS_DEPRECATED
00232 #endif
00233 /**
00234  * \brief          This function calculates the SHA-512 or SHA-384
00235  *                 checksum of a buffer.
00236  *
00237  *                 The function allocates the context, performs the
00238  *                 calculation, and frees the context.
00239  *
00240  *                 The SHA-512 result is calculated as
00241  *                 output = SHA-512(input buffer).
00242  *
00243  * \deprecated     Superseded by mbedtls_sha512_ret() in 2.7.0
00244  *
00245  * \param input    The buffer holding the data.
00246  * \param ilen     The length of the input data.
00247  * \param output   The SHA-384 or SHA-512 checksum result.
00248  * \param is384    Determines which function to use:
00249  *                 0: Use SHA-512, or 1: Use SHA-384.
00250  */
00251 MBEDTLS_DEPRECATED void mbedtls_sha512( const unsigned char *input,
00252                                         size_t ilen,
00253                                         unsigned char output[64],
00254                                         int is384 );
00255 
00256 #undef MBEDTLS_DEPRECATED
00257 #endif /* !MBEDTLS_DEPRECATED_REMOVED */
00258  /**
00259  * \brief          The SHA-384 or SHA-512 checkup routine.
00260  *
00261  * \return         \c 0 on success.
00262  * \return         \c 1 on failure.
00263  */
00264 int mbedtls_sha512_self_test( int verbose );
00265 
00266 #ifdef __cplusplus
00267 }
00268 #endif
00269 
00270 #endif /* mbedtls_sha512.h */