Subversion Repositories Kolibri OS

Rev

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

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