Subversion Repositories Kolibri OS

Rev

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

  1.  /**
  2.  * \file md.h
  3.  *
  4.  * \brief This file contains the generic message-digest wrapper.
  5.  *
  6.  * \author Adriaan de Jong <dejong@fox-it.com>
  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.  
  29. #ifndef MBEDTLS_MD_H
  30. #define MBEDTLS_MD_H
  31.  
  32. #include <stddef.h>
  33.  
  34. #if !defined(MBEDTLS_CONFIG_FILE)
  35. #include "config.h"
  36. #else
  37. #include MBEDTLS_CONFIG_FILE
  38. #endif
  39.  
  40. #define MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE                -0x5080  /**< The selected feature is not available. */
  41. #define MBEDTLS_ERR_MD_BAD_INPUT_DATA                     -0x5100  /**< Bad input parameters to function. */
  42. #define MBEDTLS_ERR_MD_ALLOC_FAILED                       -0x5180  /**< Failed to allocate memory. */
  43. #define MBEDTLS_ERR_MD_FILE_IO_ERROR                      -0x5200  /**< Opening or reading of file failed. */
  44.  
  45. /* MBEDTLS_ERR_MD_HW_ACCEL_FAILED is deprecated and should not be used. */
  46. #define MBEDTLS_ERR_MD_HW_ACCEL_FAILED                    -0x5280  /**< MD hardware accelerator failed. */
  47.  
  48. #ifdef __cplusplus
  49. extern "C" {
  50. #endif
  51.  
  52. /**
  53.  * \brief     Supported message digests.
  54.  *
  55.  * \warning   MD2, MD4, MD5 and SHA-1 are considered weak message digests and
  56.  *            their use constitutes a security risk. We recommend considering
  57.  *            stronger message digests instead.
  58.  *
  59.  */
  60. typedef enum {
  61.     MBEDTLS_MD_NONE=0,    /**< None. */
  62.     MBEDTLS_MD_MD2,       /**< The MD2 message digest. */
  63.     MBEDTLS_MD_MD4,       /**< The MD4 message digest. */
  64.     MBEDTLS_MD_MD5,       /**< The MD5 message digest. */
  65.     MBEDTLS_MD_SHA1,      /**< The SHA-1 message digest. */
  66.     MBEDTLS_MD_SHA224,    /**< The SHA-224 message digest. */
  67.     MBEDTLS_MD_SHA256,    /**< The SHA-256 message digest. */
  68.     MBEDTLS_MD_SHA384,    /**< The SHA-384 message digest. */
  69.     MBEDTLS_MD_SHA512,    /**< The SHA-512 message digest. */
  70.     MBEDTLS_MD_RIPEMD160, /**< The RIPEMD-160 message digest. */
  71. } mbedtls_md_type_t;
  72.  
  73. #if defined(MBEDTLS_SHA512_C)
  74. #define MBEDTLS_MD_MAX_SIZE         64  /* longest known is SHA512 */
  75. #else
  76. #define MBEDTLS_MD_MAX_SIZE         32  /* longest known is SHA256 or less */
  77. #endif
  78.  
  79. /**
  80.  * Opaque struct defined in md_internal.h.
  81.  */
  82. typedef struct mbedtls_md_info_t mbedtls_md_info_t;
  83.  
  84. /**
  85.  * The generic message-digest context.
  86.  */
  87. typedef struct mbedtls_md_context_t
  88. {
  89.     /** Information about the associated message digest. */
  90.     const mbedtls_md_info_t *md_info;
  91.  
  92.     /** The digest-specific context. */
  93.     void *md_ctx;
  94.  
  95.     /** The HMAC part of the context. */
  96.     void *hmac_ctx;
  97. } mbedtls_md_context_t;
  98.  
  99. /**
  100.  * \brief           This function returns the list of digests supported by the
  101.  *                  generic digest module.
  102.  *
  103.  * \return          A statically allocated array of digests. Each element
  104.  *                  in the returned list is an integer belonging to the
  105.  *                  message-digest enumeration #mbedtls_md_type_t.
  106.  *                  The last entry is 0.
  107.  */
  108. const int *mbedtls_md_list( void );
  109.  
  110. /**
  111.  * \brief           This function returns the message-digest information
  112.  *                  associated with the given digest name.
  113.  *
  114.  * \param md_name   The name of the digest to search for.
  115.  *
  116.  * \return          The message-digest information associated with \p md_name.
  117.  * \return          NULL if the associated message-digest information is not found.
  118.  */
  119. const mbedtls_md_info_t *mbedtls_md_info_from_string( const char *md_name );
  120.  
  121. /**
  122.  * \brief           This function returns the message-digest information
  123.  *                  associated with the given digest type.
  124.  *
  125.  * \param md_type   The type of digest to search for.
  126.  *
  127.  * \return          The message-digest information associated with \p md_type.
  128.  * \return          NULL if the associated message-digest information is not found.
  129.  */
  130. const mbedtls_md_info_t *mbedtls_md_info_from_type( mbedtls_md_type_t md_type );
  131.  
  132. /**
  133.  * \brief           This function initializes a message-digest context without
  134.  *                  binding it to a particular message-digest algorithm.
  135.  *
  136.  *                  This function should always be called first. It prepares the
  137.  *                  context for mbedtls_md_setup() for binding it to a
  138.  *                  message-digest algorithm.
  139.  */
  140. void mbedtls_md_init( mbedtls_md_context_t *ctx );
  141.  
  142. /**
  143.  * \brief           This function clears the internal structure of \p ctx and
  144.  *                  frees any embedded internal structure, but does not free
  145.  *                  \p ctx itself.
  146.  *
  147.  *                  If you have called mbedtls_md_setup() on \p ctx, you must
  148.  *                  call mbedtls_md_free() when you are no longer using the
  149.  *                  context.
  150.  *                  Calling this function if you have previously
  151.  *                  called mbedtls_md_init() and nothing else is optional.
  152.  *                  You must not call this function if you have not called
  153.  *                  mbedtls_md_init().
  154.  */
  155. void mbedtls_md_free( mbedtls_md_context_t *ctx );
  156.  
  157. #if ! defined(MBEDTLS_DEPRECATED_REMOVED)
  158. #if defined(MBEDTLS_DEPRECATED_WARNING)
  159. #define MBEDTLS_DEPRECATED    __attribute__((deprecated))
  160. #else
  161. #define MBEDTLS_DEPRECATED
  162. #endif
  163. /**
  164.  * \brief           This function selects the message digest algorithm to use,
  165.  *                  and allocates internal structures.
  166.  *
  167.  *                  It should be called after mbedtls_md_init() or mbedtls_md_free().
  168.  *                  Makes it necessary to call mbedtls_md_free() later.
  169.  *
  170.  * \deprecated      Superseded by mbedtls_md_setup() in 2.0.0
  171.  *
  172.  * \param ctx       The context to set up.
  173.  * \param md_info   The information structure of the message-digest algorithm
  174.  *                  to use.
  175.  *
  176.  * \return          \c 0 on success.
  177.  * \return          #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
  178.  *                  failure.
  179.  * \return          #MBEDTLS_ERR_MD_ALLOC_FAILED on memory-allocation failure.
  180.  */
  181. int mbedtls_md_init_ctx( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info ) MBEDTLS_DEPRECATED;
  182. #undef MBEDTLS_DEPRECATED
  183. #endif /* MBEDTLS_DEPRECATED_REMOVED */
  184.  
  185. /**
  186.  * \brief           This function selects the message digest algorithm to use,
  187.  *                  and allocates internal structures.
  188.  *
  189.  *                  It should be called after mbedtls_md_init() or
  190.  *                  mbedtls_md_free(). Makes it necessary to call
  191.  *                  mbedtls_md_free() later.
  192.  *
  193.  * \param ctx       The context to set up.
  194.  * \param md_info   The information structure of the message-digest algorithm
  195.  *                  to use.
  196.  * \param hmac      Defines if HMAC is used. 0: HMAC is not used (saves some memory),
  197.  *                  or non-zero: HMAC is used with this context.
  198.  *
  199.  * \return          \c 0 on success.
  200.  * \return          #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
  201.  *                  failure.
  202.  * \return          #MBEDTLS_ERR_MD_ALLOC_FAILED on memory-allocation failure.
  203.  */
  204. int mbedtls_md_setup( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac );
  205.  
  206. /**
  207.  * \brief           This function clones the state of an message-digest
  208.  *                  context.
  209.  *
  210.  * \note            You must call mbedtls_md_setup() on \c dst before calling
  211.  *                  this function.
  212.  *
  213.  * \note            The two contexts must have the same type,
  214.  *                  for example, both are SHA-256.
  215.  *
  216.  * \warning         This function clones the message-digest state, not the
  217.  *                  HMAC state.
  218.  *
  219.  * \param dst       The destination context.
  220.  * \param src       The context to be cloned.
  221.  *
  222.  * \return          \c 0 on success.
  223.  * \return          #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification failure.
  224.  */
  225. int mbedtls_md_clone( mbedtls_md_context_t *dst,
  226.                       const mbedtls_md_context_t *src );
  227.  
  228. /**
  229.  * \brief           This function extracts the message-digest size from the
  230.  *                  message-digest information structure.
  231.  *
  232.  * \param md_info   The information structure of the message-digest algorithm
  233.  *                  to use.
  234.  *
  235.  * \return          The size of the message-digest output in Bytes.
  236.  */
  237. unsigned char mbedtls_md_get_size( const mbedtls_md_info_t *md_info );
  238.  
  239. /**
  240.  * \brief           This function extracts the message-digest type from the
  241.  *                  message-digest information structure.
  242.  *
  243.  * \param md_info   The information structure of the message-digest algorithm
  244.  *                  to use.
  245.  *
  246.  * \return          The type of the message digest.
  247.  */
  248. mbedtls_md_type_t mbedtls_md_get_type( const mbedtls_md_info_t *md_info );
  249.  
  250. /**
  251.  * \brief           This function extracts the message-digest name from the
  252.  *                  message-digest information structure.
  253.  *
  254.  * \param md_info   The information structure of the message-digest algorithm
  255.  *                  to use.
  256.  *
  257.  * \return          The name of the message digest.
  258.  */
  259. const char *mbedtls_md_get_name( const mbedtls_md_info_t *md_info );
  260.  
  261. /**
  262.  * \brief           This function starts a message-digest computation.
  263.  *
  264.  *                  You must call this function after setting up the context
  265.  *                  with mbedtls_md_setup(), and before passing data with
  266.  *                  mbedtls_md_update().
  267.  *
  268.  * \param ctx       The generic message-digest context.
  269.  *
  270.  * \return          \c 0 on success.
  271.  * \return          #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
  272.  *                  failure.
  273.  */
  274. int mbedtls_md_starts( mbedtls_md_context_t *ctx );
  275.  
  276. /**
  277.  * \brief           This function feeds an input buffer into an ongoing
  278.  *                  message-digest computation.
  279.  *
  280.  *                  You must call mbedtls_md_starts() before calling this
  281.  *                  function. You may call this function multiple times.
  282.  *                  Afterwards, call mbedtls_md_finish().
  283.  *
  284.  * \param ctx       The generic message-digest context.
  285.  * \param input     The buffer holding the input data.
  286.  * \param ilen      The length of the input data.
  287.  *
  288.  * \return          \c 0 on success.
  289.  * \return          #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
  290.  *                  failure.
  291.  */
  292. int mbedtls_md_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen );
  293.  
  294. /**
  295.  * \brief           This function finishes the digest operation,
  296.  *                  and writes the result to the output buffer.
  297.  *
  298.  *                  Call this function after a call to mbedtls_md_starts(),
  299.  *                  followed by any number of calls to mbedtls_md_update().
  300.  *                  Afterwards, you may either clear the context with
  301.  *                  mbedtls_md_free(), or call mbedtls_md_starts() to reuse
  302.  *                  the context for another digest operation with the same
  303.  *                  algorithm.
  304.  *
  305.  * \param ctx       The generic message-digest context.
  306.  * \param output    The buffer for the generic message-digest checksum result.
  307.  *
  308.  * \return          \c 0 on success.
  309.  * \return          #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
  310.  *                  failure.
  311.  */
  312. int mbedtls_md_finish( mbedtls_md_context_t *ctx, unsigned char *output );
  313.  
  314. /**
  315.  * \brief          This function calculates the message-digest of a buffer,
  316.  *                 with respect to a configurable message-digest algorithm
  317.  *                 in a single call.
  318.  *
  319.  *                 The result is calculated as
  320.  *                 Output = message_digest(input buffer).
  321.  *
  322.  * \param md_info  The information structure of the message-digest algorithm
  323.  *                 to use.
  324.  * \param input    The buffer holding the data.
  325.  * \param ilen     The length of the input data.
  326.  * \param output   The generic message-digest checksum result.
  327.  *
  328.  * \return         \c 0 on success.
  329.  * \return         #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
  330.  *                 failure.
  331.  */
  332. int mbedtls_md( const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen,
  333.         unsigned char *output );
  334.  
  335. #if defined(MBEDTLS_FS_IO)
  336. /**
  337.  * \brief          This function calculates the message-digest checksum
  338.  *                 result of the contents of the provided file.
  339.  *
  340.  *                 The result is calculated as
  341.  *                 Output = message_digest(file contents).
  342.  *
  343.  * \param md_info  The information structure of the message-digest algorithm
  344.  *                 to use.
  345.  * \param path     The input file name.
  346.  * \param output   The generic message-digest checksum result.
  347.  *
  348.  * \return         \c 0 on success.
  349.  * \return         #MBEDTLS_ERR_MD_FILE_IO_ERROR on an I/O error accessing
  350.  *                 the file pointed by \p path.
  351.  * \return         #MBEDTLS_ERR_MD_BAD_INPUT_DATA if \p md_info was NULL.
  352.  */
  353. int mbedtls_md_file( const mbedtls_md_info_t *md_info, const char *path,
  354.                      unsigned char *output );
  355. #endif /* MBEDTLS_FS_IO */
  356.  
  357. /**
  358.  * \brief           This function sets the HMAC key and prepares to
  359.  *                  authenticate a new message.
  360.  *
  361.  *                  Call this function after mbedtls_md_setup(), to use
  362.  *                  the MD context for an HMAC calculation, then call
  363.  *                  mbedtls_md_hmac_update() to provide the input data, and
  364.  *                  mbedtls_md_hmac_finish() to get the HMAC value.
  365.  *
  366.  * \param ctx       The message digest context containing an embedded HMAC
  367.  *                  context.
  368.  * \param key       The HMAC secret key.
  369.  * \param keylen    The length of the HMAC key in Bytes.
  370.  *
  371.  * \return          \c 0 on success.
  372.  * \return          #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
  373.  *                  failure.
  374.  */
  375. int mbedtls_md_hmac_starts( mbedtls_md_context_t *ctx, const unsigned char *key,
  376.                     size_t keylen );
  377.  
  378. /**
  379.  * \brief           This function feeds an input buffer into an ongoing HMAC
  380.  *                  computation.
  381.  *
  382.  *                  Call mbedtls_md_hmac_starts() or mbedtls_md_hmac_reset()
  383.  *                  before calling this function.
  384.  *                  You may call this function multiple times to pass the
  385.  *                  input piecewise.
  386.  *                  Afterwards, call mbedtls_md_hmac_finish().
  387.  *
  388.  * \param ctx       The message digest context containing an embedded HMAC
  389.  *                  context.
  390.  * \param input     The buffer holding the input data.
  391.  * \param ilen      The length of the input data.
  392.  *
  393.  * \return          \c 0 on success.
  394.  * \return          #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
  395.  *                  failure.
  396.  */
  397. int mbedtls_md_hmac_update( mbedtls_md_context_t *ctx, const unsigned char *input,
  398.                     size_t ilen );
  399.  
  400. /**
  401.  * \brief           This function finishes the HMAC operation, and writes
  402.  *                  the result to the output buffer.
  403.  *
  404.  *                  Call this function after mbedtls_md_hmac_starts() and
  405.  *                  mbedtls_md_hmac_update() to get the HMAC value. Afterwards
  406.  *                  you may either call mbedtls_md_free() to clear the context,
  407.  *                  or call mbedtls_md_hmac_reset() to reuse the context with
  408.  *                  the same HMAC key.
  409.  *
  410.  * \param ctx       The message digest context containing an embedded HMAC
  411.  *                  context.
  412.  * \param output    The generic HMAC checksum result.
  413.  *
  414.  * \return          \c 0 on success.
  415.  * \return          #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
  416.  *                  failure.
  417.  */
  418. int mbedtls_md_hmac_finish( mbedtls_md_context_t *ctx, unsigned char *output);
  419.  
  420. /**
  421.  * \brief           This function prepares to authenticate a new message with
  422.  *                  the same key as the previous HMAC operation.
  423.  *
  424.  *                  You may call this function after mbedtls_md_hmac_finish().
  425.  *                  Afterwards call mbedtls_md_hmac_update() to pass the new
  426.  *                  input.
  427.  *
  428.  * \param ctx       The message digest context containing an embedded HMAC
  429.  *                  context.
  430.  *
  431.  * \return          \c 0 on success.
  432.  * \return          #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
  433.  *                  failure.
  434.  */
  435. int mbedtls_md_hmac_reset( mbedtls_md_context_t *ctx );
  436.  
  437. /**
  438.  * \brief          This function calculates the full generic HMAC
  439.  *                 on the input buffer with the provided key.
  440.  *
  441.  *                 The function allocates the context, performs the
  442.  *                 calculation, and frees the context.
  443.  *
  444.  *                 The HMAC result is calculated as
  445.  *                 output = generic HMAC(hmac key, input buffer).
  446.  *
  447.  * \param md_info  The information structure of the message-digest algorithm
  448.  *                 to use.
  449.  * \param key      The HMAC secret key.
  450.  * \param keylen   The length of the HMAC secret key in Bytes.
  451.  * \param input    The buffer holding the input data.
  452.  * \param ilen     The length of the input data.
  453.  * \param output   The generic HMAC result.
  454.  *
  455.  * \return         \c 0 on success.
  456.  * \return         #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
  457.  *                 failure.
  458.  */
  459. int mbedtls_md_hmac( const mbedtls_md_info_t *md_info, const unsigned char *key, size_t keylen,
  460.                 const unsigned char *input, size_t ilen,
  461.                 unsigned char *output );
  462.  
  463. /* Internal use */
  464. int mbedtls_md_process( mbedtls_md_context_t *ctx, const unsigned char *data );
  465.  
  466. #ifdef __cplusplus
  467. }
  468. #endif
  469.  
  470. #endif /* MBEDTLS_MD_H */
  471.