Kenji Arai / TYBLE16_mbedlized_os5_several_examples_1st

Dependencies:   nRF51_Vdd TextLCD BME280

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers md.h Source File

md.h

Go to the documentation of this file.
00001  /**
00002  * \file md.h
00003  *
00004  * \brief This file contains the generic message-digest wrapper.
00005  *
00006  * \author Adriaan de Jong <dejong@fox-it.com>
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 
00027 #ifndef MBEDTLS_MD_H
00028 #define MBEDTLS_MD_H
00029 
00030 #include <stddef.h>
00031 
00032 #if !defined(MBEDTLS_CONFIG_FILE)
00033 #include "config.h"
00034 #else
00035 #include MBEDTLS_CONFIG_FILE
00036 #endif
00037 
00038 #define MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE                -0x5080  /**< The selected feature is not available. */
00039 #define MBEDTLS_ERR_MD_BAD_INPUT_DATA                     -0x5100  /**< Bad input parameters to function. */
00040 #define MBEDTLS_ERR_MD_ALLOC_FAILED                       -0x5180  /**< Failed to allocate memory. */
00041 #define MBEDTLS_ERR_MD_FILE_IO_ERROR                      -0x5200  /**< Opening or reading of file failed. */
00042 #define MBEDTLS_ERR_MD_HW_ACCEL_FAILED                    -0x5280  /**< MD hardware accelerator failed. */
00043 
00044 #ifdef __cplusplus
00045 extern "C" {
00046 #endif
00047 
00048 /**
00049  * \brief     Supported message digests.
00050  *
00051  * \warning   MD2, MD4, MD5 and SHA-1 are considered weak message digests and
00052  *            their use constitutes a security risk. We recommend considering
00053  *            stronger message digests instead.
00054  *
00055  */
00056 typedef enum {
00057     MBEDTLS_MD_NONE=0,    /**< None. */
00058     MBEDTLS_MD_MD2,       /**< The MD2 message digest. */
00059     MBEDTLS_MD_MD4,       /**< The MD4 message digest. */
00060     MBEDTLS_MD_MD5,       /**< The MD5 message digest. */
00061     MBEDTLS_MD_SHA1,      /**< The SHA-1 message digest. */
00062     MBEDTLS_MD_SHA224,    /**< The SHA-224 message digest. */
00063     MBEDTLS_MD_SHA256,    /**< The SHA-256 message digest. */
00064     MBEDTLS_MD_SHA384,    /**< The SHA-384 message digest. */
00065     MBEDTLS_MD_SHA512,    /**< The SHA-512 message digest. */
00066     MBEDTLS_MD_RIPEMD160, /**< The RIPEMD-160 message digest. */
00067 } mbedtls_md_type_t;
00068 
00069 #if defined(MBEDTLS_SHA512_C)
00070 #define MBEDTLS_MD_MAX_SIZE         64  /* longest known is SHA512 */
00071 #else
00072 #define MBEDTLS_MD_MAX_SIZE         32  /* longest known is SHA256 or less */
00073 #endif
00074 
00075 /**
00076  * Opaque struct defined in md_internal.h.
00077  */
00078 typedef struct mbedtls_md_info_t mbedtls_md_info_t;
00079 
00080 /**
00081  * The generic message-digest context.
00082  */
00083 typedef struct mbedtls_md_context_t
00084 {
00085     /** Information about the associated message digest. */
00086     const mbedtls_md_info_t *md_info;
00087 
00088     /** The digest-specific context. */
00089     void *md_ctx;
00090 
00091     /** The HMAC part of the context. */
00092     void *hmac_ctx;
00093 } mbedtls_md_context_t;
00094 
00095 /**
00096  * \brief           This function returns the list of digests supported by the
00097  *                  generic digest module.
00098  *
00099  * \return          A statically allocated array of digests. Each element
00100  *                  in the returned list is an integer belonging to the
00101  *                  message-digest enumeration #mbedtls_md_type_t.
00102  *                  The last entry is 0.
00103  */
00104 const int *mbedtls_md_list( void );
00105 
00106 /**
00107  * \brief           This function returns the message-digest information
00108  *                  associated with the given digest name.
00109  *
00110  * \param md_name   The name of the digest to search for.
00111  *
00112  * \return          The message-digest information associated with \p md_name.
00113  * \return          NULL if the associated message-digest information is not found.
00114  */
00115 const mbedtls_md_info_t *mbedtls_md_info_from_string( const char *md_name );
00116 
00117 /**
00118  * \brief           This function returns the message-digest information
00119  *                  associated with the given digest type.
00120  *
00121  * \param md_type   The type of digest to search for.
00122  *
00123  * \return          The message-digest information associated with \p md_type.
00124  * \return          NULL if the associated message-digest information is not found.
00125  */
00126 const mbedtls_md_info_t *mbedtls_md_info_from_type( mbedtls_md_type_t md_type );
00127 
00128 /**
00129  * \brief           This function initializes a message-digest context without
00130  *                  binding it to a particular message-digest algorithm.
00131  *
00132  *                  This function should always be called first. It prepares the
00133  *                  context for mbedtls_md_setup() for binding it to a
00134  *                  message-digest algorithm.
00135  */
00136 void mbedtls_md_init( mbedtls_md_context_t *ctx );
00137 
00138 /**
00139  * \brief           This function clears the internal structure of \p ctx and
00140  *                  frees any embedded internal structure, but does not free
00141  *                  \p ctx itself.
00142  *
00143  *                  If you have called mbedtls_md_setup() on \p ctx, you must
00144  *                  call mbedtls_md_free() when you are no longer using the
00145  *                  context.
00146  *                  Calling this function if you have previously
00147  *                  called mbedtls_md_init() and nothing else is optional.
00148  *                  You must not call this function if you have not called
00149  *                  mbedtls_md_init().
00150  */
00151 void mbedtls_md_free( mbedtls_md_context_t *ctx );
00152 
00153 #if ! defined(MBEDTLS_DEPRECATED_REMOVED)
00154 #if defined(MBEDTLS_DEPRECATED_WARNING)
00155 #define MBEDTLS_DEPRECATED    __attribute__((deprecated))
00156 #else
00157 #define MBEDTLS_DEPRECATED
00158 #endif
00159 /**
00160  * \brief           This function selects the message digest algorithm to use,
00161  *                  and allocates internal structures.
00162  *
00163  *                  It should be called after mbedtls_md_init() or mbedtls_md_free().
00164  *                  Makes it necessary to call mbedtls_md_free() later.
00165  *
00166  * \deprecated      Superseded by mbedtls_md_setup() in 2.0.0
00167  *
00168  * \param ctx       The context to set up.
00169  * \param md_info   The information structure of the message-digest algorithm
00170  *                  to use.
00171  *
00172  * \return          \c 0 on success.
00173  * \return          #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
00174  *                  failure.
00175  * \return          #MBEDTLS_ERR_MD_ALLOC_FAILED on memory-allocation failure.
00176  */
00177 int mbedtls_md_init_ctx( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info ) MBEDTLS_DEPRECATED;
00178 #undef MBEDTLS_DEPRECATED
00179 #endif /* MBEDTLS_DEPRECATED_REMOVED */
00180 
00181 /**
00182  * \brief           This function selects the message digest algorithm to use,
00183  *                  and allocates internal structures.
00184  *
00185  *                  It should be called after mbedtls_md_init() or
00186  *                  mbedtls_md_free(). Makes it necessary to call
00187  *                  mbedtls_md_free() later.
00188  *
00189  * \param ctx       The context to set up.
00190  * \param md_info   The information structure of the message-digest algorithm
00191  *                  to use.
00192  * \param hmac      Defines if HMAC is used. 0: HMAC is not used (saves some memory),
00193  *                  or non-zero: HMAC is used with this context.
00194  *
00195  * \return          \c 0 on success.
00196  * \return          #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
00197  *                  failure.
00198  * \return          #MBEDTLS_ERR_MD_ALLOC_FAILED on memory-allocation failure.
00199  */
00200 int mbedtls_md_setup( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac );
00201 
00202 /**
00203  * \brief           This function clones the state of an message-digest
00204  *                  context.
00205  *
00206  * \note            You must call mbedtls_md_setup() on \c dst before calling
00207  *                  this function.
00208  *
00209  * \note            The two contexts must have the same type,
00210  *                  for example, both are SHA-256.
00211  *
00212  * \warning         This function clones the message-digest state, not the
00213  *                  HMAC state.
00214  *
00215  * \param dst       The destination context.
00216  * \param src       The context to be cloned.
00217  *
00218  * \return          \c 0 on success.
00219  * \return          #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification failure.
00220  */
00221 int mbedtls_md_clone( mbedtls_md_context_t *dst,
00222                       const mbedtls_md_context_t *src );
00223 
00224 /**
00225  * \brief           This function extracts the message-digest size from the
00226  *                  message-digest information structure.
00227  *
00228  * \param md_info   The information structure of the message-digest algorithm
00229  *                  to use.
00230  *
00231  * \return          The size of the message-digest output in Bytes.
00232  */
00233 unsigned char mbedtls_md_get_size( const mbedtls_md_info_t *md_info );
00234 
00235 /**
00236  * \brief           This function extracts the message-digest type from the
00237  *                  message-digest information structure.
00238  *
00239  * \param md_info   The information structure of the message-digest algorithm
00240  *                  to use.
00241  *
00242  * \return          The type of the message digest.
00243  */
00244 mbedtls_md_type_t mbedtls_md_get_type( const mbedtls_md_info_t *md_info );
00245 
00246 /**
00247  * \brief           This function extracts the message-digest name from the
00248  *                  message-digest information structure.
00249  *
00250  * \param md_info   The information structure of the message-digest algorithm
00251  *                  to use.
00252  *
00253  * \return          The name of the message digest.
00254  */
00255 const char *mbedtls_md_get_name( const mbedtls_md_info_t *md_info );
00256 
00257 /**
00258  * \brief           This function starts a message-digest computation.
00259  *
00260  *                  You must call this function after setting up the context
00261  *                  with mbedtls_md_setup(), and before passing data with
00262  *                  mbedtls_md_update().
00263  *
00264  * \param ctx       The generic message-digest context.
00265  *
00266  * \return          \c 0 on success.
00267  * \return          #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
00268  *                  failure.
00269  */
00270 int mbedtls_md_starts( mbedtls_md_context_t *ctx );
00271 
00272 /**
00273  * \brief           This function feeds an input buffer into an ongoing
00274  *                  message-digest computation.
00275  *
00276  *                  You must call mbedtls_md_starts() before calling this
00277  *                  function. You may call this function multiple times.
00278  *                  Afterwards, call mbedtls_md_finish().
00279  *
00280  * \param ctx       The generic message-digest context.
00281  * \param input     The buffer holding the input data.
00282  * \param ilen      The length of the input data.
00283  *
00284  * \return          \c 0 on success.
00285  * \return          #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
00286  *                  failure.
00287  */
00288 int mbedtls_md_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen );
00289 
00290 /**
00291  * \brief           This function finishes the digest operation,
00292  *                  and writes the result to the output buffer.
00293  *
00294  *                  Call this function after a call to mbedtls_md_starts(),
00295  *                  followed by any number of calls to mbedtls_md_update().
00296  *                  Afterwards, you may either clear the context with
00297  *                  mbedtls_md_free(), or call mbedtls_md_starts() to reuse
00298  *                  the context for another digest operation with the same
00299  *                  algorithm.
00300  *
00301  * \param ctx       The generic message-digest context.
00302  * \param output    The buffer for the generic message-digest checksum result.
00303  *
00304  * \return          \c 0 on success.
00305  * \return          #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
00306  *                  failure.
00307  */
00308 int mbedtls_md_finish( mbedtls_md_context_t *ctx, unsigned char *output );
00309 
00310 /**
00311  * \brief          This function calculates the message-digest of a buffer,
00312  *                 with respect to a configurable message-digest algorithm
00313  *                 in a single call.
00314  *
00315  *                 The result is calculated as
00316  *                 Output = message_digest(input buffer).
00317  *
00318  * \param md_info  The information structure of the message-digest algorithm
00319  *                 to use.
00320  * \param input    The buffer holding the data.
00321  * \param ilen     The length of the input data.
00322  * \param output   The generic message-digest checksum result.
00323  *
00324  * \return         \c 0 on success.
00325  * \return         #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
00326  *                 failure.
00327  */
00328 int mbedtls_md( const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen,
00329         unsigned char *output );
00330 
00331 #if defined(MBEDTLS_FS_IO)
00332 /**
00333  * \brief          This function calculates the message-digest checksum
00334  *                 result of the contents of the provided file.
00335  *
00336  *                 The result is calculated as
00337  *                 Output = message_digest(file contents).
00338  *
00339  * \param md_info  The information structure of the message-digest algorithm
00340  *                 to use.
00341  * \param path     The input file name.
00342  * \param output   The generic message-digest checksum result.
00343  *
00344  * \return         \c 0 on success.
00345  * \return         #MBEDTLS_ERR_MD_FILE_IO_ERROR on an I/O error accessing
00346  *                 the file pointed by \p path.
00347  * \return         #MBEDTLS_ERR_MD_BAD_INPUT_DATA if \p md_info was NULL.
00348  */
00349 int mbedtls_md_file( const mbedtls_md_info_t *md_info, const char *path,
00350                      unsigned char *output );
00351 #endif /* MBEDTLS_FS_IO */
00352 
00353 /**
00354  * \brief           This function sets the HMAC key and prepares to
00355  *                  authenticate a new message.
00356  *
00357  *                  Call this function after mbedtls_md_setup(), to use
00358  *                  the MD context for an HMAC calculation, then call
00359  *                  mbedtls_md_hmac_update() to provide the input data, and
00360  *                  mbedtls_md_hmac_finish() to get the HMAC value.
00361  *
00362  * \param ctx       The message digest context containing an embedded HMAC
00363  *                  context.
00364  * \param key       The HMAC secret key.
00365  * \param keylen    The length of the HMAC key in Bytes.
00366  *
00367  * \return          \c 0 on success.
00368  * \return          #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
00369  *                  failure.
00370  */
00371 int mbedtls_md_hmac_starts( mbedtls_md_context_t *ctx, const unsigned char *key,
00372                     size_t keylen );
00373 
00374 /**
00375  * \brief           This function feeds an input buffer into an ongoing HMAC
00376  *                  computation.
00377  *
00378  *                  Call mbedtls_md_hmac_starts() or mbedtls_md_hmac_reset()
00379  *                  before calling this function.
00380  *                  You may call this function multiple times to pass the
00381  *                  input piecewise.
00382  *                  Afterwards, call mbedtls_md_hmac_finish().
00383  *
00384  * \param ctx       The message digest context containing an embedded HMAC
00385  *                  context.
00386  * \param input     The buffer holding the input data.
00387  * \param ilen      The length of the input data.
00388  *
00389  * \return          \c 0 on success.
00390  * \return          #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
00391  *                  failure.
00392  */
00393 int mbedtls_md_hmac_update( mbedtls_md_context_t *ctx, const unsigned char *input,
00394                     size_t ilen );
00395 
00396 /**
00397  * \brief           This function finishes the HMAC operation, and writes
00398  *                  the result to the output buffer.
00399  *
00400  *                  Call this function after mbedtls_md_hmac_starts() and
00401  *                  mbedtls_md_hmac_update() to get the HMAC value. Afterwards
00402  *                  you may either call mbedtls_md_free() to clear the context,
00403  *                  or call mbedtls_md_hmac_reset() to reuse the context with
00404  *                  the same HMAC key.
00405  *
00406  * \param ctx       The message digest context containing an embedded HMAC
00407  *                  context.
00408  * \param output    The generic HMAC checksum result.
00409  *
00410  * \return          \c 0 on success.
00411  * \return          #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
00412  *                  failure.
00413  */
00414 int mbedtls_md_hmac_finish( mbedtls_md_context_t *ctx, unsigned char *output);
00415 
00416 /**
00417  * \brief           This function prepares to authenticate a new message with
00418  *                  the same key as the previous HMAC operation.
00419  *
00420  *                  You may call this function after mbedtls_md_hmac_finish().
00421  *                  Afterwards call mbedtls_md_hmac_update() to pass the new
00422  *                  input.
00423  *
00424  * \param ctx       The message digest context containing an embedded HMAC
00425  *                  context.
00426  *
00427  * \return          \c 0 on success.
00428  * \return          #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
00429  *                  failure.
00430  */
00431 int mbedtls_md_hmac_reset( mbedtls_md_context_t *ctx );
00432 
00433 /**
00434  * \brief          This function calculates the full generic HMAC
00435  *                 on the input buffer with the provided key.
00436  *
00437  *                 The function allocates the context, performs the
00438  *                 calculation, and frees the context.
00439  *
00440  *                 The HMAC result is calculated as
00441  *                 output = generic HMAC(hmac key, input buffer).
00442  *
00443  * \param md_info  The information structure of the message-digest algorithm
00444  *                 to use.
00445  * \param key      The HMAC secret key.
00446  * \param keylen   The length of the HMAC secret key in Bytes.
00447  * \param input    The buffer holding the input data.
00448  * \param ilen     The length of the input data.
00449  * \param output   The generic HMAC result.
00450  *
00451  * \return         \c 0 on success.
00452  * \return         #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
00453  *                 failure.
00454  */
00455 int mbedtls_md_hmac( const mbedtls_md_info_t *md_info, const unsigned char *key, size_t keylen,
00456                 const unsigned char *input, size_t ilen,
00457                 unsigned char *output );
00458 
00459 /* Internal use */
00460 int mbedtls_md_process( mbedtls_md_context_t *ctx, const unsigned char *data );
00461 
00462 #ifdef __cplusplus
00463 }
00464 #endif
00465 
00466 #endif /* MBEDTLS_MD_H */