Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /**
  2.  * \file sha512.h
  3.  * \brief This file contains SHA-384 and SHA-512 definitions and functions.
  4.  *
  5.  * The Secure Hash Algorithms 384 and 512 (SHA-384 and SHA-512) cryptographic
  6.  * hash functions are defined in <em>FIPS 180-4: Secure Hash Standard (SHS)</em>.
  7.  */
  8. /*
  9.  *  Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
  10.  *  SPDX-License-Identifier: GPL-2.0
  11.  *
  12.  *  This program is free software; you can redistribute it and/or modify
  13.  *  it under the terms of the GNU General Public License as published by
  14.  *  the Free Software Foundation; either version 2 of the License, or
  15.  *  (at your option) any later version.
  16.  *
  17.  *  This program is distributed in the hope that it will be useful,
  18.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  19.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20.  *  GNU General Public License for more details.
  21.  *
  22.  *  You should have received a copy of the GNU General Public License along
  23.  *  with this program; if not, write to the Free Software Foundation, Inc.,
  24.  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  25.  *
  26.  *  This file is part of Mbed TLS (https://tls.mbed.org)
  27.  */
  28. #ifndef MBEDTLS_SHA512_H
  29. #define MBEDTLS_SHA512_H
  30.  
  31. #if !defined(MBEDTLS_CONFIG_FILE)
  32. #include "config.h"
  33. #else
  34. #include MBEDTLS_CONFIG_FILE
  35. #endif
  36.  
  37. #include <stddef.h>
  38. #include <stdint.h>
  39.  
  40. /* MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED is deprecated and should not be used. */
  41. #define MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED                -0x0039  /**< SHA-512 hardware accelerator failed */
  42. #define MBEDTLS_ERR_SHA512_BAD_INPUT_DATA                 -0x0075  /**< SHA-512 input data was malformed. */
  43.  
  44. #ifdef __cplusplus
  45. extern "C" {
  46. #endif
  47.  
  48. #if !defined(MBEDTLS_SHA512_ALT)
  49. // Regular implementation
  50. //
  51.  
  52. /**
  53.  * \brief          The SHA-512 context structure.
  54.  *
  55.  *                 The structure is used both for SHA-384 and for SHA-512
  56.  *                 checksum calculations. The choice between these two is
  57.  *                 made in the call to mbedtls_sha512_starts_ret().
  58.  */
  59. typedef struct mbedtls_sha512_context
  60. {
  61.     uint64_t total[2];          /*!< The number of Bytes processed. */
  62.     uint64_t state[8];          /*!< The intermediate digest state. */
  63.     unsigned char buffer[128];  /*!< The data block being processed. */
  64.     int is384;                  /*!< Determines which function to use:
  65.                                      0: Use SHA-512, or 1: Use SHA-384. */
  66. }
  67. mbedtls_sha512_context;
  68.  
  69. #else  /* MBEDTLS_SHA512_ALT */
  70. #include "sha512_alt.h"
  71. #endif /* MBEDTLS_SHA512_ALT */
  72.  
  73. /**
  74.  * \brief          This function initializes a SHA-512 context.
  75.  *
  76.  * \param ctx      The SHA-512 context to initialize. This must
  77.  *                 not be \c NULL.
  78.  */
  79. void mbedtls_sha512_init( mbedtls_sha512_context *ctx );
  80.  
  81. /**
  82.  * \brief          This function clears a SHA-512 context.
  83.  *
  84.  * \param ctx      The SHA-512 context to clear. This may be \c NULL,
  85.  *                 in which case this function does nothing. If it
  86.  *                 is not \c NULL, it must point to an initialized
  87.  *                 SHA-512 context.
  88.  */
  89. void mbedtls_sha512_free( mbedtls_sha512_context *ctx );
  90.  
  91. /**
  92.  * \brief          This function clones the state of a SHA-512 context.
  93.  *
  94.  * \param dst      The destination context. This must be initialized.
  95.  * \param src      The context to clone. This must be initialized.
  96.  */
  97. void mbedtls_sha512_clone( mbedtls_sha512_context *dst,
  98.                            const mbedtls_sha512_context *src );
  99.  
  100. /**
  101.  * \brief          This function starts a SHA-384 or SHA-512 checksum
  102.  *                 calculation.
  103.  *
  104.  * \param ctx      The SHA-512 context to use. This must be initialized.
  105.  * \param is384    Determines which function to use. This must be
  106.  *                 either \c for SHA-512, or \c 1 for SHA-384.
  107.  *
  108.  * \return         \c 0 on success.
  109.  * \return         A negative error code on failure.
  110.  */
  111. int mbedtls_sha512_starts_ret( mbedtls_sha512_context *ctx, int is384 );
  112.  
  113. /**
  114.  * \brief          This function feeds an input buffer into an ongoing
  115.  *                 SHA-512 checksum calculation.
  116.  *
  117.  * \param ctx      The SHA-512 context. This must be initialized
  118.  *                 and have a hash operation started.
  119.  * \param input    The buffer holding the input data. This must
  120.  *                 be a readable buffer of length \p ilen Bytes.
  121.  * \param ilen     The length of the input data in Bytes.
  122.  *
  123.  * \return         \c 0 on success.
  124.  * \return         A negative error code on failure.
  125.  */
  126. int mbedtls_sha512_update_ret( mbedtls_sha512_context *ctx,
  127.                     const unsigned char *input,
  128.                     size_t ilen );
  129.  
  130. /**
  131.  * \brief          This function finishes the SHA-512 operation, and writes
  132.  *                 the result to the output buffer. This function is for
  133.  *                 internal use only.
  134.  *
  135.  * \param ctx      The SHA-512 context. This must be initialized
  136.  *                 and have a hash operation started.
  137.  * \param output   The SHA-384 or SHA-512 checksum result.
  138.  *                 This must be a writable buffer of length \c 64 Bytes.
  139.  *
  140.  * \return         \c 0 on success.
  141.  * \return         A negative error code on failure.
  142.  */
  143. int mbedtls_sha512_finish_ret( mbedtls_sha512_context *ctx,
  144.                                unsigned char output[64] );
  145.  
  146. /**
  147.  * \brief          This function processes a single data block within
  148.  *                 the ongoing SHA-512 computation.
  149.  *
  150.  * \param ctx      The SHA-512 context. This must be initialized.
  151.  * \param data     The buffer holding one block of data. This
  152.  *                 must be a readable buffer of length \c 128 Bytes.
  153.  *
  154.  * \return         \c 0 on success.
  155.  * \return         A negative error code on failure.
  156.  */
  157. int mbedtls_internal_sha512_process( mbedtls_sha512_context *ctx,
  158.                                      const unsigned char data[128] );
  159. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  160. #if defined(MBEDTLS_DEPRECATED_WARNING)
  161. #define MBEDTLS_DEPRECATED      __attribute__((deprecated))
  162. #else
  163. #define MBEDTLS_DEPRECATED
  164. #endif
  165. /**
  166.  * \brief          This function starts a SHA-384 or SHA-512 checksum
  167.  *                 calculation.
  168.  *
  169.  * \deprecated     Superseded by mbedtls_sha512_starts_ret() in 2.7.0
  170.  *
  171.  * \param ctx      The SHA-512 context to use. This must be initialized.
  172.  * \param is384    Determines which function to use. This must be either
  173.  *                 \c 0 for SHA-512 or \c 1 for SHA-384.
  174.  */
  175. MBEDTLS_DEPRECATED void mbedtls_sha512_starts( mbedtls_sha512_context *ctx,
  176.                                                int is384 );
  177.  
  178. /**
  179.  * \brief          This function feeds an input buffer into an ongoing
  180.  *                 SHA-512 checksum calculation.
  181.  *
  182.  * \deprecated     Superseded by mbedtls_sha512_update_ret() in 2.7.0.
  183.  *
  184.  * \param ctx      The SHA-512 context. This must be initialized
  185.  *                 and have a hash operation started.
  186.  * \param input    The buffer holding the data. This must be a readable
  187.  *                 buffer of length \p ilen Bytes.
  188.  * \param ilen     The length of the input data in Bytes.
  189.  */
  190. MBEDTLS_DEPRECATED void mbedtls_sha512_update( mbedtls_sha512_context *ctx,
  191.                                                const unsigned char *input,
  192.                                                size_t ilen );
  193.  
  194. /**
  195.  * \brief          This function finishes the SHA-512 operation, and writes
  196.  *                 the result to the output buffer.
  197.  *
  198.  * \deprecated     Superseded by mbedtls_sha512_finish_ret() in 2.7.0.
  199.  *
  200.  * \param ctx      The SHA-512 context. This must be initialized
  201.  *                 and have a hash operation started.
  202.  * \param output   The SHA-384 or SHA-512 checksum result. This must
  203.  *                 be a writable buffer of size \c 64 Bytes.
  204.  */
  205. MBEDTLS_DEPRECATED void mbedtls_sha512_finish( mbedtls_sha512_context *ctx,
  206.                                                unsigned char output[64] );
  207.  
  208. /**
  209.  * \brief          This function processes a single data block within
  210.  *                 the ongoing SHA-512 computation. This function is for
  211.  *                 internal use only.
  212.  *
  213.  * \deprecated     Superseded by mbedtls_internal_sha512_process() in 2.7.0.
  214.  *
  215.  * \param ctx      The SHA-512 context. This must be initialized.
  216.  * \param data     The buffer holding one block of data. This must be
  217.  *                 a readable buffer of length \c 128 Bytes.
  218.  */
  219. MBEDTLS_DEPRECATED void mbedtls_sha512_process(
  220.                                             mbedtls_sha512_context *ctx,
  221.                                             const unsigned char data[128] );
  222.  
  223. #undef MBEDTLS_DEPRECATED
  224. #endif /* !MBEDTLS_DEPRECATED_REMOVED */
  225.  
  226. /**
  227.  * \brief          This function calculates the SHA-512 or SHA-384
  228.  *                 checksum of a buffer.
  229.  *
  230.  *                 The function allocates the context, performs the
  231.  *                 calculation, and frees the context.
  232.  *
  233.  *                 The SHA-512 result is calculated as
  234.  *                 output = SHA-512(input buffer).
  235.  *
  236.  * \param input    The buffer holding the input data. This must be
  237.  *                 a readable buffer of length \p ilen Bytes.
  238.  * \param ilen     The length of the input data in Bytes.
  239.  * \param output   The SHA-384 or SHA-512 checksum result.
  240.  *                 This must be a writable buffer of length \c 64 Bytes.
  241.  * \param is384    Determines which function to use. This must be either
  242.  *                 \c 0 for SHA-512, or \c 1 for SHA-384.
  243.  *
  244.  * \return         \c 0 on success.
  245.  * \return         A negative error code on failure.
  246.  */
  247. int mbedtls_sha512_ret( const unsigned char *input,
  248.                         size_t ilen,
  249.                         unsigned char output[64],
  250.                         int is384 );
  251.  
  252. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  253. #if defined(MBEDTLS_DEPRECATED_WARNING)
  254. #define MBEDTLS_DEPRECATED      __attribute__((deprecated))
  255. #else
  256. #define MBEDTLS_DEPRECATED
  257. #endif
  258.  
  259. /**
  260.  * \brief          This function calculates the SHA-512 or SHA-384
  261.  *                 checksum of a buffer.
  262.  *
  263.  *                 The function allocates the context, performs the
  264.  *                 calculation, and frees the context.
  265.  *
  266.  *                 The SHA-512 result is calculated as
  267.  *                 output = SHA-512(input buffer).
  268.  *
  269.  * \deprecated     Superseded by mbedtls_sha512_ret() in 2.7.0
  270.  *
  271.  * \param input    The buffer holding the data. This must be a
  272.  *                 readable buffer of length \p ilen Bytes.
  273.  * \param ilen     The length of the input data in Bytes.
  274.  * \param output   The SHA-384 or SHA-512 checksum result. This must
  275.  *                 be a writable buffer of length \c 64 Bytes.
  276.  * \param is384    Determines which function to use. This must be either
  277.  *                 \c 0 for SHA-512, or \c 1 for SHA-384.
  278.  */
  279. MBEDTLS_DEPRECATED void mbedtls_sha512( const unsigned char *input,
  280.                                         size_t ilen,
  281.                                         unsigned char output[64],
  282.                                         int is384 );
  283.  
  284. #undef MBEDTLS_DEPRECATED
  285. #endif /* !MBEDTLS_DEPRECATED_REMOVED */
  286.  
  287. #if defined(MBEDTLS_SELF_TEST)
  288.  
  289.  /**
  290.  * \brief          The SHA-384 or SHA-512 checkup routine.
  291.  *
  292.  * \return         \c 0 on success.
  293.  * \return         \c 1 on failure.
  294.  */
  295. int mbedtls_sha512_self_test( int verbose );
  296. #endif /* MBEDTLS_SELF_TEST */
  297.  
  298. #ifdef __cplusplus
  299. }
  300. #endif
  301.  
  302. #endif /* mbedtls_sha512.h */
  303.