2011-01-06 15:20:01 +01:00
|
|
|
/**
|
|
|
|
* \file md.h
|
2014-05-01 13:03:14 +02:00
|
|
|
*
|
2011-01-06 15:20:01 +01:00
|
|
|
* \brief Generic message digest wrapper
|
|
|
|
*
|
|
|
|
* \author Adriaan de Jong <dejong@fox-it.com>
|
|
|
|
*
|
2015-01-23 10:45:19 +01:00
|
|
|
* Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
|
2011-01-06 15:20:01 +01:00
|
|
|
*
|
2015-01-23 12:06:27 +01:00
|
|
|
* This file is part of mbed TLS (https://www.polarssl.org)
|
2011-01-06 15:20:01 +01:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
#ifndef POLARSSL_MD_H
|
|
|
|
#define POLARSSL_MD_H
|
|
|
|
|
2011-04-24 10:57:21 +02:00
|
|
|
#include <string.h>
|
|
|
|
|
2011-07-27 18:28:54 +02:00
|
|
|
#if defined(_MSC_VER) && !defined(inline)
|
2011-04-18 05:47:52 +02:00
|
|
|
#define inline _inline
|
2011-06-21 09:48:07 +02:00
|
|
|
#else
|
2011-07-27 18:28:54 +02:00
|
|
|
#if defined(__ARMCC_VERSION) && !defined(inline)
|
2011-06-21 09:48:07 +02:00
|
|
|
#define inline __inline
|
2011-06-21 15:36:18 +02:00
|
|
|
#endif /* __ARMCC_VERSION */
|
2011-06-21 09:48:07 +02:00
|
|
|
#endif /*_MSC_VER */
|
2011-04-18 05:47:52 +02:00
|
|
|
|
2011-05-09 18:17:09 +02:00
|
|
|
#define POLARSSL_ERR_MD_FEATURE_UNAVAILABLE -0x5080 /**< The selected feature is not available. */
|
2011-06-09 17:55:11 +02:00
|
|
|
#define POLARSSL_ERR_MD_BAD_INPUT_DATA -0x5100 /**< Bad input parameters to function. */
|
|
|
|
#define POLARSSL_ERR_MD_ALLOC_FAILED -0x5180 /**< Failed to allocate memory. */
|
2012-01-14 19:07:41 +01:00
|
|
|
#define POLARSSL_ERR_MD_FILE_IO_ERROR -0x5200 /**< Opening or reading of file failed. */
|
2011-04-25 17:28:35 +02:00
|
|
|
|
2013-06-27 14:29:21 +02:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2011-01-06 15:20:01 +01:00
|
|
|
typedef enum {
|
2011-01-20 17:42:01 +01:00
|
|
|
POLARSSL_MD_NONE=0,
|
|
|
|
POLARSSL_MD_MD2,
|
2011-01-06 15:20:01 +01:00
|
|
|
POLARSSL_MD_MD4,
|
|
|
|
POLARSSL_MD_MD5,
|
|
|
|
POLARSSL_MD_SHA1,
|
|
|
|
POLARSSL_MD_SHA224,
|
|
|
|
POLARSSL_MD_SHA256,
|
|
|
|
POLARSSL_MD_SHA384,
|
|
|
|
POLARSSL_MD_SHA512,
|
2014-01-22 13:35:29 +01:00
|
|
|
POLARSSL_MD_RIPEMD160,
|
2011-01-06 15:20:01 +01:00
|
|
|
} md_type_t;
|
|
|
|
|
2013-09-10 11:10:57 +02:00
|
|
|
#if defined(POLARSSL_SHA512_C)
|
2011-01-06 16:48:19 +01:00
|
|
|
#define POLARSSL_MD_MAX_SIZE 64 /* longest known is SHA512 */
|
2013-09-10 11:10:57 +02:00
|
|
|
#else
|
|
|
|
#define POLARSSL_MD_MAX_SIZE 32 /* longest known is SHA256 or less */
|
|
|
|
#endif
|
2011-01-06 16:48:19 +01:00
|
|
|
|
2011-01-06 15:20:01 +01:00
|
|
|
/**
|
|
|
|
* Message digest information. Allows message digest functions to be called
|
|
|
|
* in a generic way.
|
|
|
|
*/
|
|
|
|
typedef struct {
|
|
|
|
/** Digest identifier */
|
|
|
|
md_type_t type;
|
|
|
|
|
|
|
|
/** Name of the message digest */
|
|
|
|
const char * name;
|
|
|
|
|
|
|
|
/** Output length of the digest function */
|
|
|
|
int size;
|
|
|
|
|
|
|
|
/** Digest initialisation function */
|
|
|
|
void (*starts_func)( void *ctx );
|
|
|
|
|
|
|
|
/** Digest update function */
|
2011-04-24 10:57:21 +02:00
|
|
|
void (*update_func)( void *ctx, const unsigned char *input, size_t ilen );
|
2011-01-06 15:20:01 +01:00
|
|
|
|
|
|
|
/** Digest finalisation function */
|
|
|
|
void (*finish_func)( void *ctx, unsigned char *output );
|
|
|
|
|
|
|
|
/** Generic digest function */
|
2011-04-24 10:57:21 +02:00
|
|
|
void (*digest_func)( const unsigned char *input, size_t ilen,
|
2014-05-01 14:18:25 +02:00
|
|
|
unsigned char *output );
|
2011-01-06 15:20:01 +01:00
|
|
|
|
|
|
|
/** Generic file digest function */
|
|
|
|
int (*file_func)( const char *path, unsigned char *output );
|
|
|
|
|
|
|
|
/** HMAC Initialisation function */
|
2014-05-01 14:18:25 +02:00
|
|
|
void (*hmac_starts_func)( void *ctx, const unsigned char *key,
|
|
|
|
size_t keylen );
|
2011-01-06 15:20:01 +01:00
|
|
|
|
|
|
|
/** HMAC update function */
|
2014-05-01 14:18:25 +02:00
|
|
|
void (*hmac_update_func)( void *ctx, const unsigned char *input,
|
|
|
|
size_t ilen );
|
2011-01-06 15:20:01 +01:00
|
|
|
|
|
|
|
/** HMAC finalisation function */
|
|
|
|
void (*hmac_finish_func)( void *ctx, unsigned char *output);
|
|
|
|
|
|
|
|
/** HMAC context reset function */
|
|
|
|
void (*hmac_reset_func)( void *ctx );
|
|
|
|
|
|
|
|
/** Generic HMAC function */
|
2011-04-24 10:57:21 +02:00
|
|
|
void (*hmac_func)( const unsigned char *key, size_t keylen,
|
2014-05-01 14:18:25 +02:00
|
|
|
const unsigned char *input, size_t ilen,
|
|
|
|
unsigned char *output );
|
2011-01-06 15:20:01 +01:00
|
|
|
|
|
|
|
/** Allocate a new context */
|
|
|
|
void * (*ctx_alloc_func)( void );
|
|
|
|
|
|
|
|
/** Free the given context */
|
|
|
|
void (*ctx_free_func)( void *ctx );
|
|
|
|
|
2013-03-13 10:26:44 +01:00
|
|
|
/** Internal use only */
|
|
|
|
void (*process_func)( void *ctx, const unsigned char *input );
|
2011-01-06 15:20:01 +01:00
|
|
|
} md_info_t;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generic message digest context.
|
|
|
|
*/
|
|
|
|
typedef struct {
|
|
|
|
/** Information about the associated message digest */
|
|
|
|
const md_info_t *md_info;
|
|
|
|
|
|
|
|
/** Digest-specific context */
|
|
|
|
void *md_ctx;
|
|
|
|
} md_context_t;
|
|
|
|
|
|
|
|
#define MD_CONTEXT_T_INIT { \
|
|
|
|
NULL, /* md_info */ \
|
|
|
|
NULL, /* md_ctx */ \
|
|
|
|
}
|
|
|
|
|
2011-01-16 22:27:44 +01:00
|
|
|
/**
|
|
|
|
* \brief Returns the list of digests supported by the generic digest module.
|
|
|
|
*
|
|
|
|
* \return a statically allocated array of digests, the last entry
|
|
|
|
* is 0.
|
|
|
|
*/
|
|
|
|
const int *md_list( void );
|
|
|
|
|
2011-01-06 15:20:01 +01:00
|
|
|
/**
|
|
|
|
* \brief Returns the message digest information associated with the
|
|
|
|
* given digest name.
|
|
|
|
*
|
2011-04-24 10:57:21 +02:00
|
|
|
* \param md_name Name of the digest to search for.
|
2011-01-06 15:20:01 +01:00
|
|
|
*
|
|
|
|
* \return The message digest information associated with md_name or
|
|
|
|
* NULL if not found.
|
|
|
|
*/
|
|
|
|
const md_info_t *md_info_from_string( const char *md_name );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Returns the message digest information associated with the
|
|
|
|
* given digest type.
|
|
|
|
*
|
|
|
|
* \param md_type type of digest to search for.
|
|
|
|
*
|
|
|
|
* \return The message digest information associated with md_type or
|
|
|
|
* NULL if not found.
|
|
|
|
*/
|
|
|
|
const md_info_t *md_info_from_type( md_type_t md_type );
|
|
|
|
|
2014-07-01 14:53:22 +02:00
|
|
|
/**
|
|
|
|
* \brief Initialize a md_context (as NONE)
|
|
|
|
*/
|
|
|
|
void md_init( md_context_t *ctx );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Free and clear the message-specific context of ctx.
|
|
|
|
* Freeing ctx itself remains the responsibility of the
|
|
|
|
* caller.
|
|
|
|
*/
|
|
|
|
void md_free( md_context_t *ctx );
|
|
|
|
|
2011-01-20 17:42:01 +01:00
|
|
|
/**
|
2014-05-01 14:18:25 +02:00
|
|
|
* \brief Initialises and fills the message digest context structure
|
|
|
|
* with the appropriate values.
|
2011-01-20 17:42:01 +01:00
|
|
|
*
|
2014-07-01 14:53:22 +02:00
|
|
|
* \note Currently also clears structure. In future versions you
|
|
|
|
* will be required to call md_init() on the structure
|
|
|
|
* first.
|
|
|
|
*
|
2011-01-20 17:42:01 +01:00
|
|
|
* \param ctx context to initialise. May not be NULL. The
|
|
|
|
* digest-specific context (ctx->md_ctx) must be NULL. It will
|
|
|
|
* be allocated, and must be freed using md_free_ctx() later.
|
|
|
|
* \param md_info message digest to use.
|
|
|
|
*
|
2011-06-09 17:55:11 +02:00
|
|
|
* \returns \c 0 on success, \c POLARSSL_ERR_MD_BAD_INPUT_DATA on
|
|
|
|
* parameter failure, \c POLARSSL_ERR_MD_ALLOC_FAILED if
|
2011-11-11 11:34:04 +01:00
|
|
|
* allocation of the digest-specific context failed.
|
2011-01-20 17:42:01 +01:00
|
|
|
*/
|
|
|
|
int md_init_ctx( md_context_t *ctx, const md_info_t *md_info );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Free the message-specific context of ctx. Freeing ctx itself
|
|
|
|
* remains the responsibility of the caller.
|
|
|
|
*
|
2014-07-01 14:53:22 +02:00
|
|
|
* \note Deprecated: Redirects to md_free()
|
|
|
|
*
|
2011-01-27 16:24:17 +01:00
|
|
|
* \param ctx Free the message-specific context
|
2011-01-20 17:42:01 +01:00
|
|
|
*
|
2014-07-01 14:53:22 +02:00
|
|
|
* \returns 0
|
2011-01-20 17:42:01 +01:00
|
|
|
*/
|
|
|
|
int md_free_ctx( md_context_t *ctx );
|
|
|
|
|
2011-01-06 15:20:01 +01:00
|
|
|
/**
|
|
|
|
* \brief Returns the size of the message digest output.
|
|
|
|
*
|
|
|
|
* \param md_info message digest info
|
|
|
|
*
|
|
|
|
* \return size of the message digest output.
|
|
|
|
*/
|
2011-04-24 10:57:21 +02:00
|
|
|
static inline unsigned char md_get_size( const md_info_t *md_info )
|
2011-01-06 15:20:01 +01:00
|
|
|
{
|
2013-04-02 11:13:39 +02:00
|
|
|
if( md_info == NULL )
|
|
|
|
return( 0 );
|
|
|
|
|
2011-01-06 15:20:01 +01:00
|
|
|
return md_info->size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Returns the type of the message digest output.
|
|
|
|
*
|
|
|
|
* \param md_info message digest info
|
|
|
|
*
|
|
|
|
* \return type of the message digest output.
|
|
|
|
*/
|
2011-04-24 10:57:21 +02:00
|
|
|
static inline md_type_t md_get_type( const md_info_t *md_info )
|
2011-01-06 15:20:01 +01:00
|
|
|
{
|
2013-04-02 11:13:39 +02:00
|
|
|
if( md_info == NULL )
|
|
|
|
return( POLARSSL_MD_NONE );
|
|
|
|
|
2011-01-06 15:20:01 +01:00
|
|
|
return md_info->type;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Returns the name of the message digest output.
|
|
|
|
*
|
|
|
|
* \param md_info message digest info
|
|
|
|
*
|
|
|
|
* \return name of the message digest output.
|
|
|
|
*/
|
2011-04-24 10:57:21 +02:00
|
|
|
static inline const char *md_get_name( const md_info_t *md_info )
|
2011-01-06 15:20:01 +01:00
|
|
|
{
|
2013-04-02 11:13:39 +02:00
|
|
|
if( md_info == NULL )
|
|
|
|
return( NULL );
|
|
|
|
|
2011-01-06 15:20:01 +01:00
|
|
|
return md_info->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-01-20 17:42:01 +01:00
|
|
|
* \brief Set-up the given context for a new message digest
|
2011-01-06 15:20:01 +01:00
|
|
|
*
|
2011-01-20 17:42:01 +01:00
|
|
|
* \param ctx generic message digest context.
|
2011-01-06 15:20:01 +01:00
|
|
|
*
|
2011-06-09 17:55:11 +02:00
|
|
|
* \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
|
|
|
|
* verification fails.
|
2011-01-06 15:20:01 +01:00
|
|
|
*/
|
2011-01-20 17:42:01 +01:00
|
|
|
int md_starts( md_context_t *ctx );
|
2011-01-06 15:20:01 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Generic message digest process buffer
|
|
|
|
*
|
|
|
|
* \param ctx Generic message digest context
|
|
|
|
* \param input buffer holding the datal
|
|
|
|
* \param ilen length of the input data
|
|
|
|
*
|
2011-06-09 17:55:11 +02:00
|
|
|
* \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
|
|
|
|
* verification fails.
|
2011-01-06 15:20:01 +01:00
|
|
|
*/
|
2011-04-24 10:57:21 +02:00
|
|
|
int md_update( md_context_t *ctx, const unsigned char *input, size_t ilen );
|
2011-01-06 15:20:01 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Generic message digest final digest
|
|
|
|
*
|
|
|
|
* \param ctx Generic message digest context
|
|
|
|
* \param output Generic message digest checksum result
|
|
|
|
*
|
2011-06-09 17:55:11 +02:00
|
|
|
* \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
|
|
|
|
* verification fails.
|
2011-01-06 15:20:01 +01:00
|
|
|
*/
|
|
|
|
int md_finish( md_context_t *ctx, unsigned char *output );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Output = message_digest( input buffer )
|
|
|
|
*
|
|
|
|
* \param md_info message digest info
|
|
|
|
* \param input buffer holding the data
|
|
|
|
* \param ilen length of the input data
|
|
|
|
* \param output Generic message digest checksum result
|
|
|
|
*
|
2011-06-09 17:55:11 +02:00
|
|
|
* \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
|
|
|
|
* verification fails.
|
2011-01-06 15:20:01 +01:00
|
|
|
*/
|
2011-04-24 10:57:21 +02:00
|
|
|
int md( const md_info_t *md_info, const unsigned char *input, size_t ilen,
|
2011-01-06 15:20:01 +01:00
|
|
|
unsigned char *output );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Output = message_digest( file contents )
|
|
|
|
*
|
|
|
|
* \param md_info message digest info
|
|
|
|
* \param path input file name
|
|
|
|
* \param output generic message digest checksum result
|
|
|
|
*
|
2011-06-09 17:55:11 +02:00
|
|
|
* \return 0 if successful, POLARSSL_ERR_MD_FILE_OPEN_FAILED if fopen
|
|
|
|
* failed, POLARSSL_ERR_MD_FILE_READ_FAILED if fread failed,
|
|
|
|
* POLARSSL_ERR_MD_BAD_INPUT_DATA if md_info was NULL.
|
2011-01-06 15:20:01 +01:00
|
|
|
*/
|
2014-05-01 14:18:25 +02:00
|
|
|
int md_file( const md_info_t *md_info, const char *path,
|
|
|
|
unsigned char *output );
|
2011-01-06 15:20:01 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Generic HMAC context setup
|
|
|
|
*
|
|
|
|
* \param ctx HMAC context to be initialized
|
|
|
|
* \param key HMAC secret key
|
|
|
|
* \param keylen length of the HMAC key
|
|
|
|
*
|
2011-06-09 17:55:11 +02:00
|
|
|
* \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
|
|
|
|
* verification fails.
|
2011-01-06 15:20:01 +01:00
|
|
|
*/
|
2014-05-01 14:18:25 +02:00
|
|
|
int md_hmac_starts( md_context_t *ctx, const unsigned char *key,
|
|
|
|
size_t keylen );
|
2011-01-06 15:20:01 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Generic HMAC process buffer
|
|
|
|
*
|
|
|
|
* \param ctx HMAC context
|
|
|
|
* \param input buffer holding the data
|
|
|
|
* \param ilen length of the input data
|
|
|
|
*
|
2011-06-09 17:55:11 +02:00
|
|
|
* \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
|
|
|
|
* verification fails.
|
2011-01-06 15:20:01 +01:00
|
|
|
*/
|
2014-05-01 14:18:25 +02:00
|
|
|
int md_hmac_update( md_context_t *ctx, const unsigned char *input,
|
|
|
|
size_t ilen );
|
2011-01-06 15:20:01 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Generic HMAC final digest
|
|
|
|
*
|
|
|
|
* \param ctx HMAC context
|
|
|
|
* \param output Generic HMAC checksum result
|
|
|
|
*
|
2011-06-09 17:55:11 +02:00
|
|
|
* \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
|
|
|
|
* verification fails.
|
2011-01-06 15:20:01 +01:00
|
|
|
*/
|
|
|
|
int md_hmac_finish( md_context_t *ctx, unsigned char *output);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Generic HMAC context reset
|
|
|
|
*
|
|
|
|
* \param ctx HMAC context to be reset
|
|
|
|
*
|
2011-06-09 17:55:11 +02:00
|
|
|
* \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
|
|
|
|
* verification fails.
|
2011-01-06 15:20:01 +01:00
|
|
|
*/
|
|
|
|
int md_hmac_reset( md_context_t *ctx );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Output = Generic_HMAC( hmac key, input buffer )
|
|
|
|
*
|
|
|
|
* \param md_info message digest info
|
|
|
|
* \param key HMAC secret key
|
|
|
|
* \param keylen length of the HMAC key
|
|
|
|
* \param input buffer holding the data
|
|
|
|
* \param ilen length of the input data
|
|
|
|
* \param output Generic HMAC-result
|
|
|
|
*
|
2011-06-09 17:55:11 +02:00
|
|
|
* \returns 0 on success, POLARSSL_ERR_MD_BAD_INPUT_DATA if parameter
|
|
|
|
* verification fails.
|
2011-01-06 15:20:01 +01:00
|
|
|
*/
|
2011-04-24 10:57:21 +02:00
|
|
|
int md_hmac( const md_info_t *md_info, const unsigned char *key, size_t keylen,
|
|
|
|
const unsigned char *input, size_t ilen,
|
2011-01-06 15:20:01 +01:00
|
|
|
unsigned char *output );
|
|
|
|
|
2013-03-13 10:26:44 +01:00
|
|
|
/* Internal use */
|
|
|
|
int md_process( md_context_t *ctx, const unsigned char *data );
|
|
|
|
|
2011-01-06 15:20:01 +01:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* POLARSSL_MD_H */
|