Commit c7673b02 authored by Luiz Felipe Silva's avatar Luiz Felipe Silva
Browse files

Merge remote-tracking branch 'upstream/dev' into dev

parents ba9a938e 5c8af3c4
...@@ -2,7 +2,8 @@ ...@@ -2,7 +2,8 @@
* \file net_sockets.h * \file net_sockets.h
* *
* \brief Network communication functions * \brief Network communication functions
* */
/*
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
* *
......
...@@ -2,7 +2,8 @@ ...@@ -2,7 +2,8 @@
* \file oid.h * \file oid.h
* *
* \brief Object Identifier (OID) database * \brief Object Identifier (OID) database
* */
/*
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
* *
......
...@@ -3,7 +3,8 @@ ...@@ -3,7 +3,8 @@
* *
* \brief VIA PadLock ACE for HW encryption/decryption supported by some * \brief VIA PadLock ACE for HW encryption/decryption supported by some
* processors * processors
* */
/*
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
* *
......
...@@ -2,7 +2,8 @@ ...@@ -2,7 +2,8 @@
* \file pem.h * \file pem.h
* *
* \brief Privacy Enhanced Mail (PEM) decoding * \brief Privacy Enhanced Mail (PEM) decoding
* */
/*
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
* *
......
...@@ -2,7 +2,8 @@ ...@@ -2,7 +2,8 @@
* \file pk.h * \file pk.h
* *
* \brief Public Key abstraction layer * \brief Public Key abstraction layer
* */
/*
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
* *
...@@ -63,6 +64,7 @@ ...@@ -63,6 +64,7 @@
#define MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE -0x3A00 /**< Elliptic curve is unsupported (only NIST curves are supported). */ #define MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE -0x3A00 /**< Elliptic curve is unsupported (only NIST curves are supported). */
#define MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE -0x3980 /**< Unavailable feature, e.g. RSA disabled for RSA key. */ #define MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE -0x3980 /**< Unavailable feature, e.g. RSA disabled for RSA key. */
#define MBEDTLS_ERR_PK_SIG_LEN_MISMATCH -0x3900 /**< The signature is valid but its length is less than expected. */ #define MBEDTLS_ERR_PK_SIG_LEN_MISMATCH -0x3900 /**< The signature is valid but its length is less than expected. */
#define MBEDTLS_ERR_PK_HW_ACCEL_FAILED -0x3880 /**< PK hardware accelerator failed. */
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
......
/** /**
* \file pk.h * \file pk_internal.h
* *
* \brief Public Key abstraction layer: wrapper functions * \brief Public Key abstraction layer: wrapper functions
* */
/*
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
* *
......
...@@ -4,7 +4,8 @@ ...@@ -4,7 +4,8 @@
* \brief Wrapper for PKCS#11 library libpkcs11-helper * \brief Wrapper for PKCS#11 library libpkcs11-helper
* *
* \author Adriaan de Jong <dejong@fox-it.com> * \author Adriaan de Jong <dejong@fox-it.com>
* */
/*
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
* *
......
...@@ -2,7 +2,8 @@ ...@@ -2,7 +2,8 @@
* \file pkcs12.h * \file pkcs12.h
* *
* \brief PKCS#12 Personal Information Exchange Syntax * \brief PKCS#12 Personal Information Exchange Syntax
* */
/*
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
* *
......
...@@ -4,7 +4,8 @@ ...@@ -4,7 +4,8 @@
* \brief PKCS#5 functions * \brief PKCS#5 functions
* *
* \author Mathias Olsson <mathias@kompetensum.com> * \author Mathias Olsson <mathias@kompetensum.com>
* */
/*
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
* *
......
/** /**
* \file platform.h * \file platform.h
* *
* \brief mbed TLS Platform abstraction layer * \brief The Mbed TLS platform abstraction layer.
* */
* Copyright (C) 2006-2016, ARM Limited, All Rights Reserved /*
* Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
* *
* Licensed under the Apache License, Version 2.0 (the "License"); you may * Licensed under the Apache License, Version 2.0 (the "License"); you may
...@@ -18,7 +19,7 @@ ...@@ -18,7 +19,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
* *
* This file is part of mbed TLS (https://tls.mbed.org) * This file is part of Mbed TLS (https://tls.mbed.org)
*/ */
#ifndef MBEDTLS_PLATFORM_H #ifndef MBEDTLS_PLATFORM_H
#define MBEDTLS_PLATFORM_H #define MBEDTLS_PLATFORM_H
...@@ -51,34 +52,34 @@ extern "C" { ...@@ -51,34 +52,34 @@ extern "C" {
#include <time.h> #include <time.h>
#if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF) #if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
#if defined(_WIN32) #if defined(_WIN32)
#define MBEDTLS_PLATFORM_STD_SNPRINTF mbedtls_platform_win32_snprintf /**< Default snprintf to use */ #define MBEDTLS_PLATFORM_STD_SNPRINTF mbedtls_platform_win32_snprintf /**< The default \c snprintf function to use. */
#else #else
#define MBEDTLS_PLATFORM_STD_SNPRINTF snprintf /**< Default snprintf to use */ #define MBEDTLS_PLATFORM_STD_SNPRINTF snprintf /**< The default \c snprintf function to use. */
#endif #endif
#endif #endif
#if !defined(MBEDTLS_PLATFORM_STD_PRINTF) #if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
#define MBEDTLS_PLATFORM_STD_PRINTF printf /**< Default printf to use */ #define MBEDTLS_PLATFORM_STD_PRINTF printf /**< The default \c printf function to use. */
#endif #endif
#if !defined(MBEDTLS_PLATFORM_STD_FPRINTF) #if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
#define MBEDTLS_PLATFORM_STD_FPRINTF fprintf /**< Default fprintf to use */ #define MBEDTLS_PLATFORM_STD_FPRINTF fprintf /**< The default \c fprintf function to use. */
#endif #endif
#if !defined(MBEDTLS_PLATFORM_STD_CALLOC) #if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
#define MBEDTLS_PLATFORM_STD_CALLOC calloc /**< Default allocator to use */ #define MBEDTLS_PLATFORM_STD_CALLOC calloc /**< The default \c calloc function to use. */
#endif #endif
#if !defined(MBEDTLS_PLATFORM_STD_FREE) #if !defined(MBEDTLS_PLATFORM_STD_FREE)
#define MBEDTLS_PLATFORM_STD_FREE free /**< Default free to use */ #define MBEDTLS_PLATFORM_STD_FREE free /**< The default \c free function to use. */
#endif #endif
#if !defined(MBEDTLS_PLATFORM_STD_EXIT) #if !defined(MBEDTLS_PLATFORM_STD_EXIT)
#define MBEDTLS_PLATFORM_STD_EXIT exit /**< Default exit to use */ #define MBEDTLS_PLATFORM_STD_EXIT exit /**< The default \c exit function to use. */
#endif #endif
#if !defined(MBEDTLS_PLATFORM_STD_TIME) #if !defined(MBEDTLS_PLATFORM_STD_TIME)
#define MBEDTLS_PLATFORM_STD_TIME time /**< Default time to use */ #define MBEDTLS_PLATFORM_STD_TIME time /**< The default \c time function to use. */
#endif #endif
#if !defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS) #if !defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
#define MBEDTLS_PLATFORM_STD_EXIT_SUCCESS EXIT_SUCCESS /**< Default exit value to use */ #define MBEDTLS_PLATFORM_STD_EXIT_SUCCESS EXIT_SUCCESS /**< The default exit value to use. */
#endif #endif
#if !defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE) #if !defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
#define MBEDTLS_PLATFORM_STD_EXIT_FAILURE EXIT_FAILURE /**< Default exit value to use */ #define MBEDTLS_PLATFORM_STD_EXIT_FAILURE EXIT_FAILURE /**< The default exit value to use. */
#endif #endif
#if defined(MBEDTLS_FS_IO) #if defined(MBEDTLS_FS_IO)
#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ) #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ)
...@@ -115,12 +116,12 @@ extern void * (*mbedtls_calloc)( size_t n, size_t size ); ...@@ -115,12 +116,12 @@ extern void * (*mbedtls_calloc)( size_t n, size_t size );
extern void (*mbedtls_free)( void *ptr ); extern void (*mbedtls_free)( void *ptr );
/** /**
* \brief Set your own memory implementation function pointers * \brief This function allows configuring custom memory-management functions.
* *
* \param calloc_func the calloc function implementation * \param calloc_func The \c calloc function implementation.
* \param free_func the free function implementation * \param free_func The \c free function implementation.
* *
* \return 0 if successful * \return \c 0.
*/ */
int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ), int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ),
void (*free_func)( void * ) ); void (*free_func)( void * ) );
...@@ -139,11 +140,11 @@ int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ), ...@@ -139,11 +140,11 @@ int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ),
extern int (*mbedtls_fprintf)( FILE *stream, const char *format, ... ); extern int (*mbedtls_fprintf)( FILE *stream, const char *format, ... );
/** /**
* \brief Set your own fprintf function pointer * \brief This function allows configuring a custom \p fprintf function pointer.
* *
* \param fprintf_func the fprintf function implementation * \param fprintf_func The \c fprintf function implementation.
* *
* \return 0 * \return \c 0.
*/ */
int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *stream, const char *, int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *stream, const char *,
... ) ); ... ) );
...@@ -162,11 +163,12 @@ int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *stream, const char ...@@ -162,11 +163,12 @@ int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *stream, const char
extern int (*mbedtls_printf)( const char *format, ... ); extern int (*mbedtls_printf)( const char *format, ... );
/** /**
* \brief Set your own printf function pointer * \brief This function allows configuring a custom \c printf function
* pointer.
* *
* \param printf_func the printf function implementation * \param printf_func The \c printf function implementation.
* *
* \return 0 * \return \c 0 on success.
*/ */
int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) ); int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) );
#else /* !MBEDTLS_PLATFORM_PRINTF_ALT */ #else /* !MBEDTLS_PLATFORM_PRINTF_ALT */
...@@ -195,11 +197,12 @@ int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... ); ...@@ -195,11 +197,12 @@ int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... );
extern int (*mbedtls_snprintf)( char * s, size_t n, const char * format, ... ); extern int (*mbedtls_snprintf)( char * s, size_t n, const char * format, ... );
/** /**
* \brief Set your own snprintf function pointer * \brief This function allows configuring a custom \c snprintf function
* pointer.
* *
* \param snprintf_func the snprintf function implementation * \param snprintf_func The \c snprintf function implementation.
* *
* \return 0 * \return \c 0 on success.
*/ */
int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n, int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
const char * format, ... ) ); const char * format, ... ) );
...@@ -220,11 +223,12 @@ int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n, ...@@ -220,11 +223,12 @@ int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
extern void (*mbedtls_exit)( int status ); extern void (*mbedtls_exit)( int status );
/** /**
* \brief Set your own exit function pointer * \brief This function allows configuring a custom \c exit function
* pointer.
* *
* \param exit_func the exit function implementation * \param exit_func The \c exit function implementation.
* *
* \return 0 * \return \c 0 on success.
*/ */
int mbedtls_platform_set_exit( void (*exit_func)( int status ) ); int mbedtls_platform_set_exit( void (*exit_func)( int status ) );
#else #else
...@@ -267,12 +271,13 @@ extern int (*mbedtls_nv_seed_read)( unsigned char *buf, size_t buf_len ); ...@@ -267,12 +271,13 @@ extern int (*mbedtls_nv_seed_read)( unsigned char *buf, size_t buf_len );
extern int (*mbedtls_nv_seed_write)( unsigned char *buf, size_t buf_len ); extern int (*mbedtls_nv_seed_write)( unsigned char *buf, size_t buf_len );
/** /**
* \brief Set your own seed file writing/reading functions * \brief This function allows configuring custom seed file writing and
* reading functions.
* *
* \param nv_seed_read_func the seed reading function implementation * \param nv_seed_read_func The seed reading function implementation.
* \param nv_seed_write_func the seed writing function implementation * \param nv_seed_write_func The seed writing function implementation.
* *
* \return 0 * \return \c 0 on success.
*/ */
int mbedtls_platform_set_nv_seed( int mbedtls_platform_set_nv_seed(
int (*nv_seed_read_func)( unsigned char *buf, size_t buf_len ), int (*nv_seed_read_func)( unsigned char *buf, size_t buf_len ),
...@@ -293,13 +298,13 @@ int mbedtls_platform_set_nv_seed( ...@@ -293,13 +298,13 @@ int mbedtls_platform_set_nv_seed(
#if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT) #if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
/** /**
* \brief Platform context structure * \brief The platform context structure.
* *
* \note This structure may be used to assist platform-specific * \note This structure may be used to assist platform-specific
* setup/teardown operations. * setup or teardown operations.
*/ */
typedef struct { typedef struct {
char dummy; /**< Placeholder member as empty structs are not portable */ char dummy; /**< Placeholder member, as empty structs are not portable. */
} }
mbedtls_platform_context; mbedtls_platform_context;
...@@ -308,32 +313,32 @@ mbedtls_platform_context; ...@@ -308,32 +313,32 @@ mbedtls_platform_context;
#endif /* !MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */ #endif /* !MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */
/** /**
* \brief Perform any platform initialisation operations * \brief This function performs any platform initialization operations.
* *
* \param ctx mbed TLS context * \param ctx The Mbed TLS context.
* *
* \return 0 if successful * \return \c 0 on success.
* *
* \note This function is intended to allow platform specific initialisation, * \note This function is intended to allow platform-specific initialization,
* and should be called before any other library functions. Its * and should be called before any other library functions. Its
* implementation is platform specific, and by default, unless platform * implementation is platform-specific, and unless
* specific code is provided, it does nothing. * platform-specific code is provided, it does nothing.
* *
* Its use and whether its necessary to be called is dependent on the * Its use and whether it is necessary to call it is dependent on the
* platform. * platform.
*/ */
int mbedtls_platform_setup( mbedtls_platform_context *ctx ); int mbedtls_platform_setup( mbedtls_platform_context *ctx );
/** /**
* \brief Perform any platform teardown operations * \brief This function performs any platform teardown operations.
* *
* \param ctx mbed TLS context * \param ctx The Mbed TLS context.
* *
* \note This function should be called after every other mbed TLS module has * \note This function should be called after every other Mbed TLS module
* been correctly freed using the appropriate free function. * has been correctly freed using the appropriate free function.
* Its implementation is platform specific, and by default, unless * Its implementation is platform-specific, and unless
* platform specific code is provided, it does nothing. * platform-specific code is provided, it does nothing.
* *
* Its use and whether its necessary to be called is dependent on the * Its use and whether it is necessary to call it is dependent on the
* platform. * platform.
*/ */
void mbedtls_platform_teardown( mbedtls_platform_context *ctx ); void mbedtls_platform_teardown( mbedtls_platform_context *ctx );
......
...@@ -2,7 +2,8 @@ ...@@ -2,7 +2,8 @@
* \file platform_time.h * \file platform_time.h
* *
* \brief mbed TLS Platform time abstraction * \brief mbed TLS Platform time abstraction
* */
/*
* Copyright (C) 2006-2016, ARM Limited, All Rights Reserved * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
* *
......
...@@ -2,7 +2,8 @@ ...@@ -2,7 +2,8 @@
* \file ripemd160.h * \file ripemd160.h
* *
* \brief RIPE MD-160 message digest * \brief RIPE MD-160 message digest
* */
/*
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
* *
...@@ -32,6 +33,13 @@ ...@@ -32,6 +33,13 @@
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
#define MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED -0x0031 /**< RIPEMD160 hardware accelerator failed */
#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
!defined(inline) && !defined(__cplusplus)
#define inline __inline
#endif
#if !defined(MBEDTLS_RIPEMD160_ALT) #if !defined(MBEDTLS_RIPEMD160_ALT)
// Regular implementation // Regular implementation
// //
...@@ -78,36 +86,121 @@ void mbedtls_ripemd160_clone( mbedtls_ripemd160_context *dst, ...@@ -78,36 +86,121 @@ void mbedtls_ripemd160_clone( mbedtls_ripemd160_context *dst,
* \brief RIPEMD-160 context setup * \brief RIPEMD-160 context setup
* *
* \param ctx context to be initialized * \param ctx context to be initialized
*
* \return 0 if successful
*/ */
void mbedtls_ripemd160_starts( mbedtls_ripemd160_context *ctx ); int mbedtls_ripemd160_starts_ret( mbedtls_ripemd160_context *ctx );
/** /**
* \brief RIPEMD-160 process buffer * \brief RIPEMD-160 process buffer
* *
* \param ctx RIPEMD-160 context * \param ctx RIPEMD-160 context
* \param input buffer holding the data * \param input buffer holding the data
* \param ilen length of the input data * \param ilen length of the input data
*
* \return 0 if successful
*/ */
void mbedtls_ripemd160_update( mbedtls_ripemd160_context *ctx, int mbedtls_ripemd160_update_ret( mbedtls_ripemd160_context *ctx,
const unsigned char *input, size_t ilen ); const unsigned char *input,
size_t ilen );
/** /**
* \brief RIPEMD-160 final digest * \brief RIPEMD-160 final digest
* *
* \param ctx RIPEMD-160 context * \param ctx RIPEMD-160 context
* \param output RIPEMD-160 checksum result * \param output RIPEMD-160 checksum result
*
* \return 0 if successful
*/
int mbedtls_ripemd160_finish_ret( mbedtls_ripemd160_context *ctx,
unsigned char output[20] );
/**
* \brief RIPEMD-160 process data block (internal use only)
*
* \param ctx RIPEMD-160 context
* \param data buffer holding one block of data
*
* \return 0 if successful
*/
int mbedtls_internal_ripemd160_process( mbedtls_ripemd160_context *ctx,
const unsigned char data[64] );
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
#if defined(MBEDTLS_DEPRECATED_WARNING)
#define MBEDTLS_DEPRECATED __attribute__((deprecated))
#else
#define MBEDTLS_DEPRECATED
#endif
/**
* \brief RIPEMD-160 context setup
*
* \deprecated Superseded by mbedtls_ripemd160_starts_ret() in 2.7.0
*
* \param ctx context to be initialized
*/ */
void mbedtls_ripemd160_finish( mbedtls_ripemd160_context *ctx, unsigned char output[20] ); MBEDTLS_DEPRECATED static inline void mbedtls_ripemd160_starts(
mbedtls_ripemd160_context *ctx )
{
mbedtls_ripemd160_starts_ret( ctx );
}
/* Internal use */ /**
void mbedtls_ripemd160_process( mbedtls_ripemd160_context *ctx, const unsigned char data[64] ); * \brief RIPEMD-160 process buffer
*
* \deprecated Superseded by mbedtls_ripemd160_update_ret() in 2.7.0
*
* \param ctx RIPEMD-160 context
* \param input buffer holding the data
* \param ilen length of the input data
*/
MBEDTLS_DEPRECATED static inline void mbedtls_ripemd160_update(
mbedtls_ripemd160_context *ctx,
const unsigned char *input,
size_t ilen )
{
mbedtls_ripemd160_update_ret( ctx, input, ilen );
}
/**
* \brief RIPEMD-160 final digest
*
* \deprecated Superseded by mbedtls_ripemd160_finish_ret() in 2.7.0
*
* \param ctx RIPEMD-160 context
* \param output RIPEMD-160 checksum result
*/
MBEDTLS_DEPRECATED static inline void mbedtls_ripemd160_finish(
mbedtls_ripemd160_context *ctx,
unsigned char output[20] )
{
mbedtls_ripemd160_finish_ret( ctx, output );
}
/**
* \brief RIPEMD-160 process data block (internal use only)
*
* \deprecated Superseded by mbedtls_internal_ripemd160_process() in 2.7.0
*
* \param ctx RIPEMD-160 context
* \param data buffer holding one block of data
*/
MBEDTLS_DEPRECATED static inline void mbedtls_ripemd160_process(
mbedtls_ripemd160_context *ctx,
const unsigned char data[64] )
{
mbedtls_internal_ripemd160_process( ctx, data );
}
#undef MBEDTLS_DEPRECATED
#endif /* !MBEDTLS_DEPRECATED_REMOVED */
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#else /* MBEDTLS_RIPEMD160_ALT */ #else /* MBEDTLS_RIPEMD160_ALT */
#include "ripemd160.h" #include "ripemd160_alt.h"
#endif /* MBEDTLS_RIPEMD160_ALT */ #endif /* MBEDTLS_RIPEMD160_ALT */
#ifdef __cplusplus #ifdef __cplusplus
...@@ -117,12 +210,41 @@ extern "C" { ...@@ -117,12 +210,41 @@ extern "C" {
/** /**
* \brief Output = RIPEMD-160( input buffer ) * \brief Output = RIPEMD-160( input buffer )
* *
* \param input buffer holding the data * \param input buffer holding the data
* \param ilen length of the input data
* \param output RIPEMD-160 checksum result
*
* \return 0 if successful
*/
int mbedtls_ripemd160_ret( const unsigned char *input,
size_t ilen,
unsigned char output[20] );
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
#if defined(MBEDTLS_DEPRECATED_WARNING)
#define MBEDTLS_DEPRECATED __attribute__((deprecated))
#else
#define MBEDTLS_DEPRECATED
#endif
/**
* \brief Output = RIPEMD-160( input buffer )
*
* \deprecated Superseded by mbedtls_ripemd160_ret() in 2.7.0
*
* \param input buffer holding the data
* \param ilen length of the input data * \param ilen length of the input data
* \param output RIPEMD-160 checksum result * \param output RIPEMD-160 checksum result
*/ */
void mbedtls_ripemd160( const unsigned char *input, size_t ilen, MBEDTLS_DEPRECATED static inline void mbedtls_ripemd160(
unsigned char output[20] ); const unsigned char *input,
size_t ilen,
unsigned char output[20] )
{
mbedtls_ripemd160_ret( input, ilen, output );
}
#undef MBEDTLS_DEPRECATED
#endif /* !MBEDTLS_DEPRECATED_REMOVED */
/** /**
* \brief Checkup routine * \brief Checkup routine
......
/** /**
* \file rsa.h * \file rsa.h
* *
* \brief The RSA public-key cryptosystem * \brief The RSA public-key cryptosystem.
* *
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved * For more information, see <em>Public-Key Cryptography Standards (PKCS)
* #1 v1.5: RSA Encryption</em> and <em>Public-Key Cryptography Standards
* (PKCS) #1 v2.1: RSA Cryptography Specifications</em>.
*
*/
/*
* Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
* *
* Licensed under the Apache License, Version 2.0 (the "License"); you may * Licensed under the Apache License, Version 2.0 (the "License"); you may
...@@ -18,7 +24,7 @@ ...@@ -18,7 +24,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
* *
* This file is part of mbed TLS (https://tls.mbed.org) * This file is part of Mbed TLS (https://tls.mbed.org)
*/ */
#ifndef MBEDTLS_RSA_H #ifndef MBEDTLS_RSA_H
#define MBEDTLS_RSA_H #define MBEDTLS_RSA_H
...@@ -42,24 +48,26 @@ ...@@ -42,24 +48,26 @@
#define MBEDTLS_ERR_RSA_BAD_INPUT_DATA -0x4080 /**< Bad input parameters to function. */ #define MBEDTLS_ERR_RSA_BAD_INPUT_DATA -0x4080 /**< Bad input parameters to function. */
#define MBEDTLS_ERR_RSA_INVALID_PADDING -0x4100 /**< Input data contains invalid padding and is rejected. */ #define MBEDTLS_ERR_RSA_INVALID_PADDING -0x4100 /**< Input data contains invalid padding and is rejected. */
#define MBEDTLS_ERR_RSA_KEY_GEN_FAILED -0x4180 /**< Something failed during generation of a key. */ #define MBEDTLS_ERR_RSA_KEY_GEN_FAILED -0x4180 /**< Something failed during generation of a key. */
#define MBEDTLS_ERR_RSA_KEY_CHECK_FAILED -0x4200 /**< Key failed to pass the library's validity check. */ #define MBEDTLS_ERR_RSA_KEY_CHECK_FAILED -0x4200 /**< Key failed to pass the validity check of the library. */
#define MBEDTLS_ERR_RSA_PUBLIC_FAILED -0x4280 /**< The public key operation failed. */ #define MBEDTLS_ERR_RSA_PUBLIC_FAILED -0x4280 /**< The public key operation failed. */
#define MBEDTLS_ERR_RSA_PRIVATE_FAILED -0x4300 /**< The private key operation failed. */ #define MBEDTLS_ERR_RSA_PRIVATE_FAILED -0x4300 /**< The private key operation failed. */
#define MBEDTLS_ERR_RSA_VERIFY_FAILED -0x4380 /**< The PKCS#1 verification failed. */ #define MBEDTLS_ERR_RSA_VERIFY_FAILED -0x4380 /**< The PKCS#1 verification failed. */
#define MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE -0x4400 /**< The output buffer for decryption is not large enough. */ #define MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE -0x4400 /**< The output buffer for decryption is not large enough. */
#define MBEDTLS_ERR_RSA_RNG_FAILED -0x4480 /**< The random generator failed to generate non-zeros. */ #define MBEDTLS_ERR_RSA_RNG_FAILED -0x4480 /**< The random generator failed to generate non-zeros. */
#define MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION -0x4500 /**< The implementation does not offer the requested operation, for example, because of security violations or lack of functionality. */
#define MBEDTLS_ERR_RSA_HW_ACCEL_FAILED -0x4580 /**< RSA hardware accelerator failed. */
/* /*
* RSA constants * RSA constants
*/ */
#define MBEDTLS_RSA_PUBLIC 0 #define MBEDTLS_RSA_PUBLIC 0 /**< Request private key operation. */
#define MBEDTLS_RSA_PRIVATE 1 #define MBEDTLS_RSA_PRIVATE 1 /**< Request public key operation. */
#define MBEDTLS_RSA_PKCS_V15 0 #define MBEDTLS_RSA_PKCS_V15 0 /**< Use PKCS-1 v1.5 encoding. */
#define MBEDTLS_RSA_PKCS_V21 1 #define MBEDTLS_RSA_PKCS_V21 1 /**< Use PKCS-1 v2.1 encoding. */
#define MBEDTLS_RSA_SIGN 1 #define MBEDTLS_RSA_SIGN 1 /**< Identifier for RSA signature operations. */
#define MBEDTLS_RSA_CRYPT 2 #define MBEDTLS_RSA_CRYPT 2 /**< Identifier for RSA encryption and decryption operations. */
#define MBEDTLS_RSA_SALT_LEN_ANY -1 #define MBEDTLS_RSA_SALT_LEN_ANY -1
...@@ -67,168 +75,449 @@ ...@@ -67,168 +75,449 @@
* The above constants may be used even if the RSA module is compile out, * The above constants may be used even if the RSA module is compile out,
* eg for alternative (PKCS#11) RSA implemenations in the PK layers. * eg for alternative (PKCS#11) RSA implemenations in the PK layers.
*/ */
#if defined(MBEDTLS_RSA_C)
#if !defined(MBEDTLS_RSA_ALT)
// Regular implementation
//
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
/** /**
* \brief RSA context structure * \brief The RSA context structure.
*
* \note Direct manipulation of the members of this structure
* is deprecated. All manipulation should instead be done through
* the public interface functions.
*/ */
typedef struct typedef struct
{ {
int ver; /*!< always 0 */ int ver; /*!< Always 0.*/
size_t len; /*!< size(N) in chars */ size_t len; /*!< The size of \p N in Bytes. */
mbedtls_mpi N; /*!< The public modulus. */
mbedtls_mpi E; /*!< The public exponent. */
mbedtls_mpi N; /*!< public modulus */ mbedtls_mpi D; /*!< The private exponent. */
mbedtls_mpi E; /*!< public exponent */ mbedtls_mpi P; /*!< The first prime factor. */
mbedtls_mpi Q; /*!< The second prime factor. */
mbedtls_mpi D; /*!< private exponent */ mbedtls_mpi DP; /*!< \p D % (P - 1) */
mbedtls_mpi P; /*!< 1st prime factor */ mbedtls_mpi DQ; /*!< \p D % (Q - 1) */
mbedtls_mpi Q; /*!< 2nd prime factor */
mbedtls_mpi DP; /*!< D % (P - 1) */
mbedtls_mpi DQ; /*!< D % (Q - 1) */
mbedtls_mpi QP; /*!< 1 / (Q % P) */ mbedtls_mpi QP; /*!< 1 / (Q % P) */
mbedtls_mpi RN; /*!< cached R^2 mod N */ mbedtls_mpi RN; /*!< cached R^2 mod \p N */
mbedtls_mpi RP; /*!< cached R^2 mod P */
mbedtls_mpi RQ; /*!< cached R^2 mod Q */
mbedtls_mpi Vi; /*!< cached blinding value */ mbedtls_mpi RP; /*!< cached R^2 mod \p P */
mbedtls_mpi Vf; /*!< cached un-blinding value */ mbedtls_mpi RQ; /*!< cached R^2 mod \p Q */
int padding; /*!< MBEDTLS_RSA_PKCS_V15 for 1.5 padding and mbedtls_mpi Vi; /*!< The cached blinding value. */
MBEDTLS_RSA_PKCS_v21 for OAEP/PSS */ mbedtls_mpi Vf; /*!< The cached un-blinding value. */
int hash_id; /*!< Hash identifier of mbedtls_md_type_t as
specified in the mbedtls_md.h header file int padding; /*!< Selects padding mode:
for the EME-OAEP and EMSA-PSS #MBEDTLS_RSA_PKCS_V15 for 1.5 padding and
encoding */ #MBEDTLS_RSA_PKCS_V21 for OAEP or PSS. */
int hash_id; /*!< Hash identifier of mbedtls_md_type_t type,
as specified in md.h for use in the MGF
mask generating function used in the
EME-OAEP and EMSA-PSS encodings. */
#if defined(MBEDTLS_THREADING_C) #if defined(MBEDTLS_THREADING_C)
mbedtls_threading_mutex_t mutex; /*!< Thread-safety mutex */ mbedtls_threading_mutex_t mutex; /*!< Thread-safety mutex. */
#endif #endif
} }
mbedtls_rsa_context; mbedtls_rsa_context;
/** /**
* \brief Initialize an RSA context * \brief This function initializes an RSA context.
* *
* Note: Set padding to MBEDTLS_RSA_PKCS_V21 for the RSAES-OAEP * \note Set padding to #MBEDTLS_RSA_PKCS_V21 for the RSAES-OAEP
* encryption scheme and the RSASSA-PSS signature scheme. * encryption scheme and the RSASSA-PSS signature scheme.
* *
* \param ctx RSA context to be initialized * \param ctx The RSA context to initialize.
* \param padding MBEDTLS_RSA_PKCS_V15 or MBEDTLS_RSA_PKCS_V21 * \param padding Selects padding mode: #MBEDTLS_RSA_PKCS_V15 or
* \param hash_id MBEDTLS_RSA_PKCS_V21 hash identifier * #MBEDTLS_RSA_PKCS_V21.
* \param hash_id The hash identifier of #mbedtls_md_type_t type, if
* \p padding is #MBEDTLS_RSA_PKCS_V21.
* *
* \note The hash_id parameter is actually ignored * \note The \p hash_id parameter is ignored when using
* when using MBEDTLS_RSA_PKCS_V15 padding. * #MBEDTLS_RSA_PKCS_V15 padding.
* *
* \note Choice of padding mode is strictly enforced for private key * \note The choice of padding mode is strictly enforced for private key
* operations, since there might be security concerns in * operations, since there might be security concerns in
* mixing padding modes. For public key operations it's merely * mixing padding modes. For public key operations it is
* a default value, which can be overriden by calling specific * a default value, which can be overriden by calling specific
* rsa_rsaes_xxx or rsa_rsassa_xxx functions. * \c rsa_rsaes_xxx or \c rsa_rsassa_xxx functions.
* *
* \note The chosen hash is always used for OEAP encryption. * \note The hash selected in \p hash_id is always used for OEAP
* For PSS signatures, it's always used for making signatures, * encryption. For PSS signatures, it is always used for
* but can be overriden (and always is, if set to * making signatures, but can be overriden for verifying them.
* MBEDTLS_MD_NONE) for verifying them. * If set to #MBEDTLS_MD_NONE, it is always overriden.
*/ */
void mbedtls_rsa_init( mbedtls_rsa_context *ctx, void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
int padding, int padding,
int hash_id); int hash_id);
/** /**
* \brief Set padding for an already initialized RSA context * \brief This function imports a set of core parameters into an
* See \c mbedtls_rsa_init() for details. * RSA context.
*
* \param ctx The initialized RSA context to store the parameters in.
* \param N The RSA modulus, or NULL.
* \param P The first prime factor of \p N, or NULL.
* \param Q The second prime factor of \p N, or NULL.
* \param D The private exponent, or NULL.
* \param E The public exponent, or NULL.
* *
* \param ctx RSA context to be set * \note This function can be called multiple times for successive
* \param padding MBEDTLS_RSA_PKCS_V15 or MBEDTLS_RSA_PKCS_V21 * imports, if the parameters are not simultaneously present.
* \param hash_id MBEDTLS_RSA_PKCS_V21 hash identifier *
* Any sequence of calls to this function should be followed
* by a call to mbedtls_rsa_complete(), which checks and
* completes the provided information to a ready-for-use
* public or private RSA key.
*
* \note See mbedtls_rsa_complete() for more information on which
* parameters are necessary to set up a private or public
* RSA key.
*
* \note The imported parameters are copied and need not be preserved
* for the lifetime of the RSA context being set up.
*
* \return \c 0 on success, or a non-zero error code on failure.
*/
int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
const mbedtls_mpi *N,
const mbedtls_mpi *P, const mbedtls_mpi *Q,
const mbedtls_mpi *D, const mbedtls_mpi *E );
/**
* \brief This function imports core RSA parameters, in raw big-endian
* binary format, into an RSA context.
*
* \param ctx The initialized RSA context to store the parameters in.
* \param N The RSA modulus, or NULL.
* \param N_len The Byte length of \p N, ignored if \p N == NULL.
* \param P The first prime factor of \p N, or NULL.
* \param P_len The Byte length of \p P, ignored if \p P == NULL.
* \param Q The second prime factor of \p N, or NULL.
* \param Q_len The Byte length of \p Q, ignored if \p Q == NULL.
* \param D The private exponent, or NULL.
* \param D_len The Byte length of \p D, ignored if \p D == NULL.
* \param E The public exponent, or NULL.
* \param E_len The Byte length of \p E, ignored if \p E == NULL.
*
* \note This function can be called multiple times for successive
* imports, if the parameters are not simultaneously present.
*
* Any sequence of calls to this function should be followed
* by a call to mbedtls_rsa_complete(), which checks and
* completes the provided information to a ready-for-use
* public or private RSA key.
*
* \note See mbedtls_rsa_complete() for more information on which
* parameters are necessary to set up a private or public
* RSA key.
*
* \note The imported parameters are copied and need not be preserved
* for the lifetime of the RSA context being set up.
*
* \return \c 0 on success, or a non-zero error code on failure.
*/ */
void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding, int hash_id); int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
unsigned char const *N, size_t N_len,
unsigned char const *P, size_t P_len,
unsigned char const *Q, size_t Q_len,
unsigned char const *D, size_t D_len,
unsigned char const *E, size_t E_len );
/** /**
* \brief Generate an RSA keypair * \brief This function completes an RSA context from
* a set of imported core parameters.
* *
* \param ctx RSA context that will hold the key * To setup an RSA public key, precisely \p N and \p E
* \param f_rng RNG function * must have been imported.
* \param p_rng RNG parameter
* \param nbits size of the public key in bits
* \param exponent public exponent (e.g., 65537)
* *
* \note mbedtls_rsa_init() must be called beforehand to setup * To setup an RSA private key, sufficient information must
* the RSA context. * be present for the other parameters to be derivable.
*
* The default implementation supports the following:
* <ul><li>Derive \p P, \p Q from \p N, \p D, \p E.</li>
* <li>Derive \p N, \p D from \p P, \p Q, \p E.</li></ul>
* Alternative implementations need not support these.
*
* If this function runs successfully, it guarantees that
* the RSA context can be used for RSA operations without
* the risk of failure or crash.
*
* \param ctx The initialized RSA context holding imported parameters.
*
* \return \c 0 on success, or #MBEDTLS_ERR_RSA_BAD_INPUT_DATA if the
* attempted derivations failed.
*
* \warning This function need not perform consistency checks
* for the imported parameters. In particular, parameters that
* are not needed by the implementation might be silently
* discarded and left unchecked. To check the consistency
* of the key material, see mbedtls_rsa_check_privkey().
* *
* \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
*/ */
int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx, int mbedtls_rsa_complete( mbedtls_rsa_context *ctx );
int (*f_rng)(void *, unsigned char *, size_t),
void *p_rng,
unsigned int nbits, int exponent );
/** /**
* \brief Check a public RSA key * \brief This function exports the core parameters of an RSA key.
*
* If this function runs successfully, the non-NULL buffers
* pointed to by \p N, \p P, \p Q, \p D, and \p E are fully
* written, with additional unused space filled leading by
* zero Bytes.
*
* Possible reasons for returning
* #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:<ul>
* <li>An alternative RSA implementation is in use, which
* stores the key externally, and either cannot or should
* not export it into RAM.</li>
* <li>A SW or HW implementation might not support a certain
* deduction. For example, \p P, \p Q from \p N, \p D,
* and \p E if the former are not part of the
* implementation.</li></ul>
*
* If the function fails due to an unsupported operation,
* the RSA context stays intact and remains usable.
*
* \param ctx The initialized RSA context.
* \param N The MPI to hold the RSA modulus, or NULL.
* \param P The MPI to hold the first prime factor of \p N, or NULL.
* \param Q The MPI to hold the second prime factor of \p N, or NULL.
* \param D The MPI to hold the private exponent, or NULL.
* \param E The MPI to hold the public exponent, or NULL.
*
* \return \c 0 on success,
* #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION if exporting the
* requested parameters cannot be done due to missing
* functionality or because of security policies,
* or a non-zero return code on any other failure.
* *
* \param ctx RSA context to be checked */
int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
mbedtls_mpi *D, mbedtls_mpi *E );
/**
* \brief This function exports core parameters of an RSA key
* in raw big-endian binary format.
*
* If this function runs successfully, the non-NULL buffers
* pointed to by \p N, \p P, \p Q, \p D, and \p E are fully
* written, with additional unused space filled leading by
* zero Bytes.
*
* Possible reasons for returning
* #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:<ul>
* <li>An alternative RSA implementation is in use, which
* stores the key externally, and either cannot or should
* not export it into RAM.</li>
* <li>A SW or HW implementation might not support a certain
* deduction. For example, \p P, \p Q from \p N, \p D,
* and \p E if the former are not part of the
* implementation.</li></ul>
* If the function fails due to an unsupported operation,
* the RSA context stays intact and remains usable.
*
* \param ctx The initialized RSA context.
* \param N The Byte array to store the RSA modulus, or NULL.
* \param N_len The size of the buffer for the modulus.
* \param P The Byte array to hold the first prime factor of \p N, or
* NULL.
* \param P_len The size of the buffer for the first prime factor.
* \param Q The Byte array to hold the second prime factor of \p N, or
NULL.
* \param Q_len The size of the buffer for the second prime factor.
* \param D The Byte array to hold the private exponent, or NULL.
* \param D_len The size of the buffer for the private exponent.
* \param E The Byte array to hold the public exponent, or NULL.
* \param E_len The size of the buffer for the public exponent.
*
* \note The length fields are ignored if the corresponding
* buffer pointers are NULL.
*
* \return \c 0 on success,
* #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION if exporting the
* requested parameters cannot be done due to missing
* functionality or because of security policies,
* or a non-zero return code on any other failure.
*/
int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
unsigned char *N, size_t N_len,
unsigned char *P, size_t P_len,
unsigned char *Q, size_t Q_len,
unsigned char *D, size_t D_len,
unsigned char *E, size_t E_len );
/**
* \brief This function exports CRT parameters of a private RSA key.
*
* \param ctx The initialized RSA context.
* \param DP The MPI to hold D modulo P-1, or NULL.
* \param DQ The MPI to hold D modulo Q-1, or NULL.
* \param QP The MPI to hold modular inverse of Q modulo P, or NULL.
*
* \return \c 0 on success, non-zero error code otherwise.
*
* \note Alternative RSA implementations not using CRT-parameters
* internally can implement this function based on
* mbedtls_rsa_deduce_opt().
* *
* \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
*/ */
int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx ); int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP );
/**
* \brief This function sets padding for an already initialized RSA
* context. See mbedtls_rsa_init() for details.
*
* \param ctx The RSA context to be set.
* \param padding Selects padding mode: #MBEDTLS_RSA_PKCS_V15 or
* #MBEDTLS_RSA_PKCS_V21.
* \param hash_id The #MBEDTLS_RSA_PKCS_V21 hash identifier.
*/
void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
int hash_id);
/**
* \brief This function retrieves the length of RSA modulus in Bytes.
*
* \param ctx The initialized RSA context.
*
* \return The length of the RSA modulus in Bytes.
*
*/
size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx );
/** /**
* \brief Check a private RSA key * \brief This function generates an RSA keypair.
* *
* \param ctx RSA context to be checked * \param ctx The RSA context used to hold the key.
* \param f_rng The RNG function.
* \param p_rng The RNG parameter.
* \param nbits The size of the public key in bits.
* \param exponent The public exponent. For example, 65537.
*
* \note mbedtls_rsa_init() must be called before this function,
* to set up the RSA context.
*
* \return \c 0 on success, or an \c MBEDTLS_ERR_RSA_XXX error code
on failure.
*/
int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
int (*f_rng)(void *, unsigned char *, size_t),
void *p_rng,
unsigned int nbits, int exponent );
/**
* \brief This function checks if a context contains at least an RSA
* public key.
*
* If the function runs successfully, it is guaranteed that
* enough information is present to perform an RSA public key
* operation using mbedtls_rsa_public().
*
* \param ctx The RSA context to check.
*
* \return \c 0 on success, or an \c MBEDTLS_ERR_RSA_XXX error code
* on failure.
* *
* \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code */
int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx );
/**
* \brief This function checks if a context contains an RSA private key
* and perform basic consistency checks.
*
* \param ctx The RSA context to check.
*
* \return \c 0 on success, or an \c MBEDTLS_ERR_RSA_XXX error code on
* failure.
*
* \note The consistency checks performed by this function not only
* ensure that mbedtls_rsa_private() can be called successfully
* on the given context, but that the various parameters are
* mutually consistent with high probability, in the sense that
* mbedtls_rsa_public() and mbedtls_rsa_private() are inverses.
*
* \warning This function should catch accidental misconfigurations
* like swapping of parameters, but it cannot establish full
* trust in neither the quality nor the consistency of the key
* material that was used to setup the given RSA context:
* <ul><li>Consistency: Imported parameters that are irrelevant
* for the implementation might be silently dropped. If dropped,
* the current function does not have access to them,
* and therefore cannot check them. See mbedtls_rsa_complete().
* If you want to check the consistency of the entire
* content of an PKCS1-encoded RSA private key, for example, you
* should use mbedtls_rsa_validate_params() before setting
* up the RSA context.
* Additionally, if the implementation performs empirical checks,
* these checks substantiate but do not guarantee consistency.</li>
* <li>Quality: This function is not expected to perform
* extended quality assessments like checking that the prime
* factors are safe. Additionally, it is the responsibility of the
* user to ensure the trustworthiness of the source of his RSA
* parameters, which goes beyond what is effectively checkable
* by the library.</li></ul>
*/ */
int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx ); int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx );
/** /**
* \brief Check a public-private RSA key pair. * \brief This function checks a public-private RSA key pair.
* Check each of the contexts, and make sure they match. *
* It checks each of the contexts, and makes sure they match.
* *
* \param pub RSA context holding the public key * \param pub The RSA context holding the public key.
* \param prv RSA context holding the private key * \param prv The RSA context holding the private key.
* *
* \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code * \return \c 0 on success, or an \c MBEDTLS_ERR_RSA_XXX error code
* on failure.
*/ */
int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub, const mbedtls_rsa_context *prv ); int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
const mbedtls_rsa_context *prv );
/** /**
* \brief Do an RSA public key operation * \brief This function performs an RSA public key operation.
* *
* \param ctx RSA context * \param ctx The RSA context.
* \param input input buffer * \param input The input buffer.
* \param output output buffer * \param output The output buffer.
* *
* \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code * \return \c 0 on success, or an \c MBEDTLS_ERR_RSA_XXX error code
* on failure.
* *
* \note This function does NOT take care of message * \note This function does not handle message padding.
* padding. Also, be sure to set input[0] = 0 or ensure that *
* input is smaller than N. * \note Make sure to set \p input[0] = 0 or ensure that
* input is smaller than \p N.
* *
* \note The input and output buffers must be large * \note The input and output buffers must be large
* enough (eg. 128 bytes if RSA-1024 is used). * enough. For example, 128 Bytes if RSA-1024 is used.
*/ */
int mbedtls_rsa_public( mbedtls_rsa_context *ctx, int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
const unsigned char *input, const unsigned char *input,
unsigned char *output ); unsigned char *output );
/** /**
* \brief Do an RSA private key operation * \brief This function performs an RSA private key operation.
* *
* \param ctx RSA context * \param ctx The RSA context.
* \param f_rng RNG function (Needed for blinding) * \param f_rng The RNG function. Needed for blinding.
* \param p_rng RNG parameter * \param p_rng The RNG parameter.
* \param input input buffer * \param input The input buffer.
* \param output output buffer * \param output The output buffer.
* *
* \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code * \return \c 0 on success, or an \c MBEDTLS_ERR_RSA_XXX error code
* on failure.
* *
* \note The input and output buffers must be large * \note The input and output buffers must be large
* enough (eg. 128 bytes if RSA-1024 is used). * enough. For example, 128 Bytes if RSA-1024 is used.
*/ */
int mbedtls_rsa_private( mbedtls_rsa_context *ctx, int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
int (*f_rng)(void *, unsigned char *, size_t), int (*f_rng)(void *, unsigned char *, size_t),
...@@ -237,23 +526,36 @@ int mbedtls_rsa_private( mbedtls_rsa_context *ctx, ...@@ -237,23 +526,36 @@ int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
unsigned char *output ); unsigned char *output );
/** /**
* \brief Generic wrapper to perform a PKCS#1 encryption using the * \brief This function adds the message padding, then performs an RSA
* mode from the context. Add the message padding, then do an * operation.
* RSA operation.
* *
* \param ctx RSA context * It is the generic wrapper for performing a PKCS#1 encryption
* \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding * operation using the \p mode from the context.
* and MBEDTLS_RSA_PRIVATE)
* \param p_rng RNG parameter
* \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE
* \param ilen contains the plaintext length
* \param input buffer holding the data to be encrypted
* \param output buffer that will hold the ciphertext
* *
* \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
* *
* \note The output buffer must be as large as the size * \param ctx The RSA context.
* of ctx->N (eg. 128 bytes if RSA-1024 is used). * \param f_rng The RNG function. Needed for padding, PKCS#1 v2.1
* encoding, and #MBEDTLS_RSA_PRIVATE.
* \param p_rng The RNG parameter.
* \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
* \param ilen The length of the plaintext.
* \param input The buffer holding the data to encrypt.
* \param output The buffer used to hold the ciphertext.
*
* \deprecated It is deprecated and discouraged to call this function
* in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
* are likely to remove the \p mode argument and have it
* implicitly set to #MBEDTLS_RSA_PUBLIC.
*
* \note Alternative implementations of RSA need not support
* mode being set to #MBEDTLS_RSA_PRIVATE and might instead
* return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
*
* \return \c 0 on success, or an \c MBEDTLS_ERR_RSA_XXX error code
* on failure.
*
* \note The input and output buffers must be as large as the size
* of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
*/ */
int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx, int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
int (*f_rng)(void *, unsigned char *, size_t), int (*f_rng)(void *, unsigned char *, size_t),
...@@ -263,20 +565,32 @@ int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx, ...@@ -263,20 +565,32 @@ int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
unsigned char *output ); unsigned char *output );
/** /**
* \brief Perform a PKCS#1 v1.5 encryption (RSAES-PKCS1-v1_5-ENCRYPT) * \brief This function performs a PKCS#1 v1.5 encryption operation
* * (RSAES-PKCS1-v1_5-ENCRYPT).
* \param ctx RSA context *
* \param f_rng RNG function (Needed for padding and MBEDTLS_RSA_PRIVATE) * \param ctx The RSA context.
* \param p_rng RNG parameter * \param f_rng The RNG function. Needed for padding and
* \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE * #MBEDTLS_RSA_PRIVATE.
* \param ilen contains the plaintext length * \param p_rng The RNG parameter.
* \param input buffer holding the data to be encrypted * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
* \param output buffer that will hold the ciphertext * \param ilen The length of the plaintext.
* * \param input The buffer holding the data to encrypt.
* \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code * \param output The buffer used to hold the ciphertext.
*
* \deprecated It is deprecated and discouraged to call this function
* in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
* are likely to remove the \p mode argument and have it
* implicitly set to #MBEDTLS_RSA_PUBLIC.
*
* \note Alternative implementations of RSA need not support
* mode being set to #MBEDTLS_RSA_PRIVATE and might instead
* return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
*
* \return \c 0 on success, or an \c MBEDTLS_ERR_RSA_XXX error code
* on failure.
* *
* \note The output buffer must be as large as the size * \note The output buffer must be as large as the size
* of ctx->N (eg. 128 bytes if RSA-1024 is used). * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
*/ */
int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx, int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
int (*f_rng)(void *, unsigned char *, size_t), int (*f_rng)(void *, unsigned char *, size_t),
...@@ -286,23 +600,34 @@ int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx, ...@@ -286,23 +600,34 @@ int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
unsigned char *output ); unsigned char *output );
/** /**
* \brief Perform a PKCS#1 v2.1 OAEP encryption (RSAES-OAEP-ENCRYPT) * \brief This function performs a PKCS#1 v2.1 OAEP encryption
* * operation (RSAES-OAEP-ENCRYPT).
* \param ctx RSA context *
* \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding * \param ctx The RSA context.
* and MBEDTLS_RSA_PRIVATE) * \param f_rng The RNG function. Needed for padding and PKCS#1 v2.1
* \param p_rng RNG parameter * encoding and #MBEDTLS_RSA_PRIVATE.
* \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE * \param p_rng The RNG parameter.
* \param label buffer holding the custom label to use * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
* \param label_len contains the label length * \param label The buffer holding the custom label to use.
* \param ilen contains the plaintext length * \param label_len The length of the label.
* \param input buffer holding the data to be encrypted * \param ilen The length of the plaintext.
* \param output buffer that will hold the ciphertext * \param input The buffer holding the data to encrypt.
* * \param output The buffer used to hold the ciphertext.
* \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code *
* \deprecated It is deprecated and discouraged to call this function
* in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
* are likely to remove the \p mode argument and have it
* implicitly set to #MBEDTLS_RSA_PUBLIC.
*
* \note Alternative implementations of RSA need not support
* mode being set to #MBEDTLS_RSA_PRIVATE and might instead
* return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
*
* \return \c 0 on success, or an \c MBEDTLS_ERR_RSA_XXX error code
* on failure.
* *
* \note The output buffer must be as large as the size * \note The output buffer must be as large as the size
* of ctx->N (eg. 128 bytes if RSA-1024 is used). * of ctx->N. For example, 128 Bytes if RSA-1024 is used.
*/ */
int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx, int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
int (*f_rng)(void *, unsigned char *, size_t), int (*f_rng)(void *, unsigned char *, size_t),
...@@ -314,30 +639,42 @@ int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx, ...@@ -314,30 +639,42 @@ int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
unsigned char *output ); unsigned char *output );
/** /**
* \brief Generic wrapper to perform a PKCS#1 decryption using the * \brief This function performs an RSA operation, then removes the
* mode from the context. Do an RSA operation, then remove * message padding.
* the message padding *
* It is the generic wrapper for performing a PKCS#1 decryption
* operation using the \p mode from the context.
*
* \param ctx The RSA context.
* \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
* \param p_rng The RNG parameter.
* \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
* \param olen The length of the plaintext.
* \param input The buffer holding the encrypted data.
* \param output The buffer used to hold the plaintext.
* \param output_max_len The maximum length of the output buffer.
* *
* \param ctx RSA context * \deprecated It is deprecated and discouraged to call this function
* \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE) * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
* \param p_rng RNG parameter * are likely to remove the \p mode argument and have it
* \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE * implicitly set to #MBEDTLS_RSA_PRIVATE.
* \param olen will contain the plaintext length
* \param input buffer holding the encrypted data
* \param output buffer that will hold the plaintext
* \param output_max_len maximum length of the output buffer
* *
* \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code * \note Alternative implementations of RSA need not support
* mode being set to #MBEDTLS_RSA_PUBLIC and might instead
* return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
*
* \return \c 0 on success, or an \c MBEDTLS_ERR_RSA_XXX error code
* on failure.
* *
* \note The output buffer length \c output_max_len should be * \note The output buffer length \c output_max_len should be
* as large as the size ctx->len of ctx->N (eg. 128 bytes * as large as the size \p ctx->len of \p ctx->N (for example,
* if RSA-1024 is used) to be able to hold an arbitrary * 128 Bytes if RSA-1024 is used) to be able to hold an
* decrypted message. If it is not large enough to hold * arbitrary decrypted message. If it is not large enough to
* the decryption of the particular ciphertext provided, * hold the decryption of the particular ciphertext provided,
* the function will return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE. * the function returns \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
* *
* \note The input buffer must be as large as the size * \note The input buffer must be as large as the size
* of ctx->N (eg. 128 bytes if RSA-1024 is used). * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
*/ */
int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx, int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
int (*f_rng)(void *, unsigned char *, size_t), int (*f_rng)(void *, unsigned char *, size_t),
...@@ -348,28 +685,39 @@ int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx, ...@@ -348,28 +685,39 @@ int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
size_t output_max_len ); size_t output_max_len );
/** /**
* \brief Perform a PKCS#1 v1.5 decryption (RSAES-PKCS1-v1_5-DECRYPT) * \brief This function performs a PKCS#1 v1.5 decryption
* * operation (RSAES-PKCS1-v1_5-DECRYPT).
* \param ctx RSA context *
* \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE) * \param ctx The RSA context.
* \param p_rng RNG parameter * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
* \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE * \param p_rng The RNG parameter.
* \param olen will contain the plaintext length * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
* \param input buffer holding the encrypted data * \param olen The length of the plaintext.
* \param output buffer that will hold the plaintext * \param input The buffer holding the encrypted data.
* \param output_max_len maximum length of the output buffer * \param output The buffer to hold the plaintext.
* * \param output_max_len The maximum length of the output buffer.
* \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code *
* \deprecated It is deprecated and discouraged to call this function
* in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
* are likely to remove the \p mode argument and have it
* implicitly set to #MBEDTLS_RSA_PRIVATE.
*
* \note Alternative implementations of RSA need not support
* mode being set to #MBEDTLS_RSA_PUBLIC and might instead
* return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
*
* \return \c 0 on success, or an \c MBEDTLS_ERR_RSA_XXX error code
* on failure.
* *
* \note The output buffer length \c output_max_len should be * \note The output buffer length \c output_max_len should be
* as large as the size ctx->len of ctx->N (eg. 128 bytes * as large as the size \p ctx->len of \p ctx->N, for example,
* if RSA-1024 is used) to be able to hold an arbitrary * 128 Bytes if RSA-1024 is used, to be able to hold an
* decrypted message. If it is not large enough to hold * arbitrary decrypted message. If it is not large enough to
* the decryption of the particular ciphertext provided, * hold the decryption of the particular ciphertext provided,
* the function will return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE. * the function returns #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
* *
* \note The input buffer must be as large as the size * \note The input buffer must be as large as the size
* of ctx->N (eg. 128 bytes if RSA-1024 is used). * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
*/ */
int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx, int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
int (*f_rng)(void *, unsigned char *, size_t), int (*f_rng)(void *, unsigned char *, size_t),
...@@ -380,30 +728,42 @@ int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx, ...@@ -380,30 +728,42 @@ int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
size_t output_max_len ); size_t output_max_len );
/** /**
* \brief Perform a PKCS#1 v2.1 OAEP decryption (RSAES-OAEP-DECRYPT) * \brief This function performs a PKCS#1 v2.1 OAEP decryption
* * operation (RSAES-OAEP-DECRYPT).
* \param ctx RSA context *
* \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE) * \param ctx The RSA context.
* \param p_rng RNG parameter * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
* \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE * \param p_rng The RNG parameter.
* \param label buffer holding the custom label to use * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
* \param label_len contains the label length * \param label The buffer holding the custom label to use.
* \param olen will contain the plaintext length * \param label_len The length of the label.
* \param input buffer holding the encrypted data * \param olen The length of the plaintext.
* \param output buffer that will hold the plaintext * \param input The buffer holding the encrypted data.
* \param output_max_len maximum length of the output buffer * \param output The buffer to hold the plaintext.
* * \param output_max_len The maximum length of the output buffer.
* \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code *
* \deprecated It is deprecated and discouraged to call this function
* in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
* are likely to remove the \p mode argument and have it
* implicitly set to #MBEDTLS_RSA_PRIVATE.
*
* \note Alternative implementations of RSA need not support
* mode being set to #MBEDTLS_RSA_PUBLIC and might instead
* return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
*
* \return \c 0 on success, or an \c MBEDTLS_ERR_RSA_XXX error code
* on failure.
* *
* \note The output buffer length \c output_max_len should be * \note The output buffer length \c output_max_len should be
* as large as the size ctx->len of ctx->N (eg. 128 bytes * as large as the size \p ctx->len of \p ctx->N, for
* if RSA-1024 is used) to be able to hold an arbitrary * example, 128 Bytes if RSA-1024 is used, to be able to
* decrypted message. If it is not large enough to hold * hold an arbitrary decrypted message. If it is not
* the decryption of the particular ciphertext provided, * large enough to hold the decryption of the particular
* the function will return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE. * ciphertext provided, the function returns
* #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
* *
* \note The input buffer must be as large as the size * \note The input buffer must be as large as the size
* of ctx->N (eg. 128 bytes if RSA-1024 is used). * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
*/ */
int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx, int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
int (*f_rng)(void *, unsigned char *, size_t), int (*f_rng)(void *, unsigned char *, size_t),
...@@ -416,28 +776,41 @@ int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx, ...@@ -416,28 +776,41 @@ int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
size_t output_max_len ); size_t output_max_len );
/** /**
* \brief Generic wrapper to perform a PKCS#1 signature using the * \brief This function performs a private RSA operation to sign
* mode from the context. Do a private RSA operation to sign * a message digest using PKCS#1.
* a message digest *
* * It is the generic wrapper for performing a PKCS#1
* \param ctx RSA context * signature using the \p mode from the context.
* \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for *
* MBEDTLS_RSA_PRIVATE) * \param ctx The RSA context.
* \param p_rng RNG parameter * \param f_rng The RNG function. Needed for PKCS#1 v2.1 encoding and for
* \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE * #MBEDTLS_RSA_PRIVATE.
* \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data) * \param p_rng The RNG parameter.
* \param hashlen message digest length (for MBEDTLS_MD_NONE only) * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
* \param hash buffer holding the message digest * \param md_alg The message-digest algorithm used to hash the original data.
* \param sig buffer that will hold the ciphertext * Use #MBEDTLS_MD_NONE for signing raw data.
* * \param hashlen The length of the message digest. Only used if \p md_alg is #MBEDTLS_MD_NONE.
* \return 0 if the signing operation was successful, * \param hash The buffer holding the message digest.
* or an MBEDTLS_ERR_RSA_XXX error code * \param sig The buffer to hold the ciphertext.
* *
* \note The "sig" buffer must be as large as the size * \deprecated It is deprecated and discouraged to call this function
* of ctx->N (eg. 128 bytes if RSA-1024 is used). * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
* * are likely to remove the \p mode argument and have it
* \note In case of PKCS#1 v2.1 encoding, see comments on * implicitly set to #MBEDTLS_RSA_PRIVATE.
* \note \c mbedtls_rsa_rsassa_pss_sign() for details on md_alg and hash_id. *
* \note Alternative implementations of RSA need not support
* mode being set to #MBEDTLS_RSA_PUBLIC and might instead
* return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
*
* \return \c 0 if the signing operation was successful,
* or an \c MBEDTLS_ERR_RSA_XXX error code on failure.
*
* \note The \p sig buffer must be as large as the size
* of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
*
* \note For PKCS#1 v2.1 encoding, see comments on
* mbedtls_rsa_rsassa_pss_sign() for details on
* \p md_alg and \p hash_id.
*/ */
int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx, int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
int (*f_rng)(void *, unsigned char *, size_t), int (*f_rng)(void *, unsigned char *, size_t),
...@@ -449,22 +822,34 @@ int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx, ...@@ -449,22 +822,34 @@ int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
unsigned char *sig ); unsigned char *sig );
/** /**
* \brief Perform a PKCS#1 v1.5 signature (RSASSA-PKCS1-v1_5-SIGN) * \brief This function performs a PKCS#1 v1.5 signature
* * operation (RSASSA-PKCS1-v1_5-SIGN).
* \param ctx RSA context *
* \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE) * \param ctx The RSA context.
* \param p_rng RNG parameter * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
* \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE * \param p_rng The RNG parameter.
* \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data) * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
* \param hashlen message digest length (for MBEDTLS_MD_NONE only) * \param md_alg The message-digest algorithm used to hash the original data.
* \param hash buffer holding the message digest * Use #MBEDTLS_MD_NONE for signing raw data.
* \param sig buffer that will hold the ciphertext * \param hashlen The length of the message digest. Only used if \p md_alg is #MBEDTLS_MD_NONE.
* * \param hash The buffer holding the message digest.
* \return 0 if the signing operation was successful, * \param sig The buffer to hold the ciphertext.
* or an MBEDTLS_ERR_RSA_XXX error code *
* * \deprecated It is deprecated and discouraged to call this function
* \note The "sig" buffer must be as large as the size * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
* of ctx->N (eg. 128 bytes if RSA-1024 is used). * are likely to remove the \p mode argument and have it
* implicitly set to #MBEDTLS_RSA_PRIVATE.
*
* \note Alternative implementations of RSA need not support
* mode being set to #MBEDTLS_RSA_PUBLIC and might instead
* return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
*
* \return \c 0 if the signing operation was successful,
* or an \c MBEDTLS_ERR_RSA_XXX error code
* on failure.
*
* \note The \p sig buffer must be as large as the size
* of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
*/ */
int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx, int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
int (*f_rng)(void *, unsigned char *, size_t), int (*f_rng)(void *, unsigned char *, size_t),
...@@ -476,28 +861,42 @@ int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx, ...@@ -476,28 +861,42 @@ int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
unsigned char *sig ); unsigned char *sig );
/** /**
* \brief Perform a PKCS#1 v2.1 PSS signature (RSASSA-PSS-SIGN) * \brief This function performs a PKCS#1 v2.1 PSS signature
* * operation (RSASSA-PSS-SIGN).
* \param ctx RSA context *
* \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for * \param ctx The RSA context.
* MBEDTLS_RSA_PRIVATE) * \param f_rng The RNG function. Needed for PKCS#1 v2.1 encoding and for
* \param p_rng RNG parameter * #MBEDTLS_RSA_PRIVATE.
* \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE * \param p_rng The RNG parameter.
* \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data) * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
* \param hashlen message digest length (for MBEDTLS_MD_NONE only) * \param md_alg The message-digest algorithm used to hash the original data.
* \param hash buffer holding the message digest * Use #MBEDTLS_MD_NONE for signing raw data.
* \param sig buffer that will hold the ciphertext * \param hashlen The length of the message digest. Only used if \p md_alg is #MBEDTLS_MD_NONE.
* * \param hash The buffer holding the message digest.
* \return 0 if the signing operation was successful, * \param sig The buffer to hold the ciphertext.
* or an MBEDTLS_ERR_RSA_XXX error code *
* * \deprecated It is deprecated and discouraged to call this function
* \note The "sig" buffer must be as large as the size * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
* of ctx->N (eg. 128 bytes if RSA-1024 is used). * are likely to remove the \p mode argument and have it
* * implicitly set to #MBEDTLS_RSA_PRIVATE.
* \note The hash_id in the RSA context is the one used for the *
* encoding. md_alg in the function call is the type of hash * \note Alternative implementations of RSA need not support
* that is encoded. According to RFC 3447 it is advised to * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
* keep both hashes the same. * return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
*
* \return \c 0 if the signing operation was successful,
* or an \c MBEDTLS_ERR_RSA_XXX error code
* on failure.
*
* \note The \p sig buffer must be as large as the size
* of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
*
* \note The \p hash_id in the RSA context is the one used for the
* encoding. \p md_alg in the function call is the type of hash
* that is encoded. According to <em>RFC-3447: Public-Key
* Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
* Specifications</em> it is advised to keep both hashes the
* same.
*/ */
int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx, int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
int (*f_rng)(void *, unsigned char *, size_t), int (*f_rng)(void *, unsigned char *, size_t),
...@@ -509,27 +908,41 @@ int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx, ...@@ -509,27 +908,41 @@ int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
unsigned char *sig ); unsigned char *sig );
/** /**
* \brief Generic wrapper to perform a PKCS#1 verification using the * \brief This function performs a public RSA operation and checks
* mode from the context. Do a public RSA operation and check * the message digest.
* the message digest *
* * This is the generic wrapper for performing a PKCS#1
* \param ctx points to an RSA public key * verification using the mode from the context.
* \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE) *
* \param p_rng RNG parameter * \param ctx The RSA public key context.
* \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
* \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data) * \param p_rng The RNG parameter.
* \param hashlen message digest length (for MBEDTLS_MD_NONE only) * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
* \param hash buffer holding the message digest * \param md_alg The message-digest algorithm used to hash the original data.
* \param sig buffer holding the ciphertext * Use #MBEDTLS_MD_NONE for signing raw data.
* * \param hashlen The length of the message digest. Only used if \p md_alg is #MBEDTLS_MD_NONE.
* \return 0 if the verify operation was successful, * \param hash The buffer holding the message digest.
* or an MBEDTLS_ERR_RSA_XXX error code * \param sig The buffer holding the ciphertext.
* *
* \note The "sig" buffer must be as large as the size * \deprecated It is deprecated and discouraged to call this function
* of ctx->N (eg. 128 bytes if RSA-1024 is used). * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
* * are likely to remove the \p mode argument and have it
* \note In case of PKCS#1 v2.1 encoding, see comments on * set to #MBEDTLS_RSA_PUBLIC.
* \c mbedtls_rsa_rsassa_pss_verify() about md_alg and hash_id. *
* \note Alternative implementations of RSA need not support
* mode being set to #MBEDTLS_RSA_PRIVATE and might instead
* return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
*
* \return \c 0 if the verify operation was successful,
* or an \c MBEDTLS_ERR_RSA_XXX error code
* on failure.
*
* \note The \p sig buffer must be as large as the size
* of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
*
* \note For PKCS#1 v2.1 encoding, see comments on
* mbedtls_rsa_rsassa_pss_verify() about \p md_alg and
* \p hash_id.
*/ */
int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx, int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
int (*f_rng)(void *, unsigned char *, size_t), int (*f_rng)(void *, unsigned char *, size_t),
...@@ -541,22 +954,34 @@ int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx, ...@@ -541,22 +954,34 @@ int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
const unsigned char *sig ); const unsigned char *sig );
/** /**
* \brief Perform a PKCS#1 v1.5 verification (RSASSA-PKCS1-v1_5-VERIFY) * \brief This function performs a PKCS#1 v1.5 verification
* * operation (RSASSA-PKCS1-v1_5-VERIFY).
* \param ctx points to an RSA public key *
* \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE) * \param ctx The RSA public key context.
* \param p_rng RNG parameter * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
* \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE * \param p_rng The RNG parameter.
* \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data) * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
* \param hashlen message digest length (for MBEDTLS_MD_NONE only) * \param md_alg The message-digest algorithm used to hash the original data.
* \param hash buffer holding the message digest * Use #MBEDTLS_MD_NONE for signing raw data.
* \param sig buffer holding the ciphertext * \param hashlen The length of the message digest. Only used if \p md_alg is #MBEDTLS_MD_NONE.
* * \param hash The buffer holding the message digest.
* \return 0 if the verify operation was successful, * \param sig The buffer holding the ciphertext.
* or an MBEDTLS_ERR_RSA_XXX error code *
* * \deprecated It is deprecated and discouraged to call this function
* \note The "sig" buffer must be as large as the size * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
* of ctx->N (eg. 128 bytes if RSA-1024 is used). * are likely to remove the \p mode argument and have it
* set to #MBEDTLS_RSA_PUBLIC.
*
* \note Alternative implementations of RSA need not support
* mode being set to #MBEDTLS_RSA_PRIVATE and might instead
* return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
*
* \return \c 0 if the verify operation was successful,
* or an \c MBEDTLS_ERR_RSA_XXX error code
* on failure.
*
* \note The \p sig buffer must be as large as the size
* of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
*/ */
int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx, int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
int (*f_rng)(void *, unsigned char *, size_t), int (*f_rng)(void *, unsigned char *, size_t),
...@@ -568,29 +993,45 @@ int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx, ...@@ -568,29 +993,45 @@ int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
const unsigned char *sig ); const unsigned char *sig );
/** /**
* \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY) * \brief This function performs a PKCS#1 v2.1 PSS verification
* (This is the "simple" version.) * operation (RSASSA-PSS-VERIFY).
* *
* \param ctx points to an RSA public key * The hash function for the MGF mask generating function
* \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE) * is that specified in the RSA context.
* \param p_rng RNG parameter *
* \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE * \param ctx The RSA public key context.
* \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data) * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
* \param hashlen message digest length (for MBEDTLS_MD_NONE only) * \param p_rng The RNG parameter.
* \param hash buffer holding the message digest * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
* \param sig buffer holding the ciphertext * \param md_alg The message-digest algorithm used to hash the original data.
* * Use #MBEDTLS_MD_NONE for signing raw data.
* \return 0 if the verify operation was successful, * \param hashlen The length of the message digest. Only used if \p md_alg is #MBEDTLS_MD_NONE.
* or an MBEDTLS_ERR_RSA_XXX error code * \param hash The buffer holding the message digest.
* * \param sig The buffer holding the ciphertext.
* \note The "sig" buffer must be as large as the size *
* of ctx->N (eg. 128 bytes if RSA-1024 is used). * \deprecated It is deprecated and discouraged to call this function
* * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
* \note The hash_id in the RSA context is the one used for the * are likely to remove the \p mode argument and have it
* verification. md_alg in the function call is the type of * implicitly set to #MBEDTLS_RSA_PUBLIC.
* hash that is verified. According to RFC 3447 it is advised to *
* keep both hashes the same. If hash_id in the RSA context is * \note Alternative implementations of RSA need not support
* unset, the md_alg from the function call is used. * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
* return #MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION.
*
* \return \c 0 if the verify operation was successful,
* or an \c MBEDTLS_ERR_RSA_XXX error code
* on failure.
*
* \note The \p sig buffer must be as large as the size
* of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
*
* \note The \p hash_id in the RSA context is the one used for the
* verification. \p md_alg in the function call is the type of
* hash that is verified. According to <em>RFC-3447: Public-Key
* Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
* Specifications</em> it is advised to keep both hashes the
* same. If \p hash_id in the RSA context is unset,
* the \p md_alg from the function call is used.
*/ */
int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx, int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
int (*f_rng)(void *, unsigned char *, size_t), int (*f_rng)(void *, unsigned char *, size_t),
...@@ -602,28 +1043,33 @@ int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx, ...@@ -602,28 +1043,33 @@ int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
const unsigned char *sig ); const unsigned char *sig );
/** /**
* \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY) * \brief This function performs a PKCS#1 v2.1 PSS verification
* (This is the version with "full" options.) * operation (RSASSA-PSS-VERIFY).
* *
* \param ctx points to an RSA public key * The hash function for the MGF mask generating function
* \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE) * is that specified in \p mgf1_hash_id.
* \param p_rng RNG parameter *
* \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE * \param ctx The RSA public key context.
* \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data) * \param f_rng The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE.
* \param hashlen message digest length (for MBEDTLS_MD_NONE only) * \param p_rng The RNG parameter.
* \param hash buffer holding the message digest * \param mode #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
* \param mgf1_hash_id message digest used for mask generation * \param md_alg The message-digest algorithm used to hash the original data.
* \param expected_salt_len Length of the salt used in padding, use * Use #MBEDTLS_MD_NONE for signing raw data.
* MBEDTLS_RSA_SALT_LEN_ANY to accept any salt length * \param hashlen The length of the message digest. Only used if \p md_alg is #MBEDTLS_MD_NONE.
* \param sig buffer holding the ciphertext * \param hash The buffer holding the message digest.
* * \param mgf1_hash_id The message digest used for mask generation.
* \return 0 if the verify operation was successful, * \param expected_salt_len The length of the salt used in padding. Use
* or an MBEDTLS_ERR_RSA_XXX error code * #MBEDTLS_RSA_SALT_LEN_ANY to accept any salt length.
* * \param sig The buffer holding the ciphertext.
* \note The "sig" buffer must be as large as the size *
* of ctx->N (eg. 128 bytes if RSA-1024 is used). * \return \c 0 if the verify operation was successful,
* * or an \c MBEDTLS_ERR_RSA_XXX error code
* \note The hash_id in the RSA context is ignored. * on failure.
*
* \note The \p sig buffer must be as large as the size
* of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
*
* \note The \p hash_id in the RSA context is ignored.
*/ */
int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx, int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
int (*f_rng)(void *, unsigned char *, size_t), int (*f_rng)(void *, unsigned char *, size_t),
...@@ -637,27 +1083,39 @@ int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx, ...@@ -637,27 +1083,39 @@ int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
const unsigned char *sig ); const unsigned char *sig );
/** /**
* \brief Copy the components of an RSA context * \brief This function copies the components of an RSA context.
* *
* \param dst Destination context * \param dst The destination context.
* \param src Source context * \param src The source context.
* *
* \return 0 on success, * \return \c 0 on success,
* MBEDTLS_ERR_MPI_ALLOC_FAILED on memory allocation failure * #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory allocation failure.
*/ */
int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src ); int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src );
/** /**
* \brief Free the components of an RSA key * \brief This function frees the components of an RSA key.
* *
* \param ctx RSA Context to free * \param ctx The RSA Context to free.
*/ */
void mbedtls_rsa_free( mbedtls_rsa_context *ctx ); void mbedtls_rsa_free( mbedtls_rsa_context *ctx );
#ifdef __cplusplus
}
#endif
#else /* MBEDTLS_RSA_ALT */
#include "rsa_alt.h"
#endif /* MBEDTLS_RSA_ALT */
#ifdef __cplusplus
extern "C" {
#endif
/** /**
* \brief Checkup routine * \brief The RSA checkup routine.
* *
* \return 0 if successful, or 1 if the test failed * \return \c 0 on success, or \c 1 on failure.
*/ */
int mbedtls_rsa_self_test( int verbose ); int mbedtls_rsa_self_test( int verbose );
...@@ -665,6 +1123,4 @@ int mbedtls_rsa_self_test( int verbose ); ...@@ -665,6 +1123,4 @@ int mbedtls_rsa_self_test( int verbose );
} }
#endif #endif
#endif /* MBEDTLS_RSA_C */
#endif /* rsa.h */ #endif /* rsa.h */
/**
* \file rsa_internal.h
*
* \brief Context-independent RSA helper functions
*/
/*
* Copyright (C) 2006-2017, ARM Limited, All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* This file is part of mbed TLS (https://tls.mbed.org)
*
*
* This file declares some RSA-related helper functions useful when
* implementing the RSA interface. They are public and provided in a
* separate compilation unit in order to make it easy for designers of
* alternative RSA implementations to use them in their code, as it is
* conceived that the functionality they provide will be necessary
* for most complete implementations.
*
* End-users of Mbed TLS not intending to re-implement the RSA functionality
* are not expected to get into the need of making use of these functions directly,
* but instead should be able to use the functions declared in rsa.h.
*
* There are two classes of helper functions:
* (1) Parameter-generating helpers. These are:
* - mbedtls_rsa_deduce_primes
* - mbedtls_rsa_deduce_private_exponent
* - mbedtls_rsa_deduce_crt
* Each of these functions takes a set of core RSA parameters
* and generates some other, or CRT related parameters.
* (2) Parameter-checking helpers. These are:
* - mbedtls_rsa_validate_params
* - mbedtls_rsa_validate_crt
* They take a set of core or CRT related RSA parameters
* and check their validity.
*
*/
#ifndef MBEDTLS_RSA_INTERNAL_H
#define MBEDTLS_RSA_INTERNAL_H
#if !defined(MBEDTLS_CONFIG_FILE)
#include "config.h"
#else
#include MBEDTLS_CONFIG_FILE
#endif
#include "bignum.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* \brief Compute RSA prime moduli P, Q from public modulus N=PQ
* and a pair of private and public key.
*
* \note This is a 'static' helper function not operating on
* an RSA context. Alternative implementations need not
* overwrite it.
*
* \param N RSA modulus N = PQ, with P, Q to be found
* \param E RSA public exponent
* \param D RSA private exponent
* \param P Pointer to MPI holding first prime factor of N on success
* \param Q Pointer to MPI holding second prime factor of N on success
*
* \return
* - 0 if successful. In this case, P and Q constitute a
* factorization of N.
* - A non-zero error code otherwise.
*
* \note It is neither checked that P, Q are prime nor that
* D, E are modular inverses wrt. P-1 and Q-1. For that,
* use the helper function \c mbedtls_rsa_validate_params.
*
*/
int mbedtls_rsa_deduce_primes( mbedtls_mpi const *N, mbedtls_mpi const *E,
mbedtls_mpi const *D,
mbedtls_mpi *P, mbedtls_mpi *Q );
/**
* \brief Compute RSA private exponent from
* prime moduli and public key.
*
* \note This is a 'static' helper function not operating on
* an RSA context. Alternative implementations need not
* overwrite it.
*
* \param P First prime factor of RSA modulus
* \param Q Second prime factor of RSA modulus
* \param E RSA public exponent
* \param D Pointer to MPI holding the private exponent on success.
*
* \return
* - 0 if successful. In this case, D is set to a simultaneous
* modular inverse of E modulo both P-1 and Q-1.
* - A non-zero error code otherwise.
*
* \note This function does not check whether P and Q are primes.
*
*/
int mbedtls_rsa_deduce_private_exponent( mbedtls_mpi const *P,
mbedtls_mpi const *Q,
mbedtls_mpi const *E,
mbedtls_mpi *D );
/**
* \brief Generate RSA-CRT parameters
*
* \note This is a 'static' helper function not operating on
* an RSA context. Alternative implementations need not
* overwrite it.
*
* \param P First prime factor of N
* \param Q Second prime factor of N
* \param D RSA private exponent
* \param DP Output variable for D modulo P-1
* \param DQ Output variable for D modulo Q-1
* \param QP Output variable for the modular inverse of Q modulo P.
*
* \return 0 on success, non-zero error code otherwise.
*
* \note This function does not check whether P, Q are
* prime and whether D is a valid private exponent.
*
*/
int mbedtls_rsa_deduce_crt( const mbedtls_mpi *P, const mbedtls_mpi *Q,
const mbedtls_mpi *D, mbedtls_mpi *DP,
mbedtls_mpi *DQ, mbedtls_mpi *QP );
/**
* \brief Check validity of core RSA parameters
*
* \note This is a 'static' helper function not operating on
* an RSA context. Alternative implementations need not
* overwrite it.
*
* \param N RSA modulus N = PQ
* \param P First prime factor of N
* \param Q Second prime factor of N
* \param D RSA private exponent
* \param E RSA public exponent
* \param f_rng PRNG to be used for primality check, or NULL
* \param p_rng PRNG context for f_rng, or NULL
*
* \return
* - 0 if the following conditions are satisfied
* if all relevant parameters are provided:
* - P prime if f_rng != NULL (%)
* - Q prime if f_rng != NULL (%)
* - 1 < N = P * Q
* - 1 < D, E < N
* - D and E are modular inverses modulo P-1 and Q-1
* (%) This is only done if MBEDTLS_GENPRIME is defined.
* - A non-zero error code otherwise.
*
* \note The function can be used with a restricted set of arguments
* to perform specific checks only. E.g., calling it with
* (-,P,-,-,-) and a PRNG amounts to a primality check for P.
*/
int mbedtls_rsa_validate_params( const mbedtls_mpi *N, const mbedtls_mpi *P,
const mbedtls_mpi *Q, const mbedtls_mpi *D,
const mbedtls_mpi *E,
int (*f_rng)(void *, unsigned char *, size_t),
void *p_rng );
/**
* \brief Check validity of RSA CRT parameters
*
* \note This is a 'static' helper function not operating on
* an RSA context. Alternative implementations need not
* overwrite it.
*
* \param P First prime factor of RSA modulus
* \param Q Second prime factor of RSA modulus
* \param D RSA private exponent
* \param DP MPI to check for D modulo P-1
* \param DQ MPI to check for D modulo P-1
* \param QP MPI to check for the modular inverse of Q modulo P.
*
* \return
* - 0 if the following conditions are satisfied:
* - D = DP mod P-1 if P, D, DP != NULL
* - Q = DQ mod P-1 if P, D, DQ != NULL
* - QP = Q^-1 mod P if P, Q, QP != NULL
* - \c MBEDTLS_ERR_RSA_KEY_CHECK_FAILED if check failed,
* potentially including \c MBEDTLS_ERR_MPI_XXX if some
* MPI calculations failed.
* - \c MBEDTLS_ERR_RSA_BAD_INPUT_DATA if insufficient
* data was provided to check DP, DQ or QP.
*
* \note The function can be used with a restricted set of arguments
* to perform specific checks only. E.g., calling it with the
* parameters (P, -, D, DP, -, -) will check DP = D mod P-1.
*/
int mbedtls_rsa_validate_crt( const mbedtls_mpi *P, const mbedtls_mpi *Q,
const mbedtls_mpi *D, const mbedtls_mpi *DP,
const mbedtls_mpi *DQ, const mbedtls_mpi *QP );
#endif /* rsa_internal.h */
/** /**
* \file sha1.h * \file sha1.h
* *
* \brief SHA-1 cryptographic hash function * \brief The SHA-1 cryptographic hash function.
* *
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved * \warning SHA-1 is considered a weak message digest and its use constitutes
* a security risk. We recommend considering stronger message
* digests instead.
*/
/*
* Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
* *
* Licensed under the Apache License, Version 2.0 (the "License"); you may * Licensed under the Apache License, Version 2.0 (the "License"); you may
...@@ -18,7 +23,7 @@ ...@@ -18,7 +23,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
* *
* This file is part of mbed TLS (https://tls.mbed.org) * This file is part of Mbed TLS (https://tls.mbed.org)
*/ */
#ifndef MBEDTLS_SHA1_H #ifndef MBEDTLS_SHA1_H
#define MBEDTLS_SHA1_H #define MBEDTLS_SHA1_H
...@@ -32,6 +37,13 @@ ...@@ -32,6 +37,13 @@
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
#define MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED -0x0035 /**< SHA-1 hardware accelerator failed */
#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
!defined(inline) && !defined(__cplusplus)
#define inline __inline
#endif
#if !defined(MBEDTLS_SHA1_ALT) #if !defined(MBEDTLS_SHA1_ALT)
// Regular implementation // Regular implementation
// //
...@@ -41,65 +53,213 @@ extern "C" { ...@@ -41,65 +53,213 @@ extern "C" {
#endif #endif
/** /**
* \brief SHA-1 context structure * \brief The SHA-1 context structure.
*
* \warning SHA-1 is considered a weak message digest and its use
* constitutes a security risk. We recommend considering
* stronger message digests instead.
*
*/ */
typedef struct typedef struct
{ {
uint32_t total[2]; /*!< number of bytes processed */ uint32_t total[2]; /*!< The number of Bytes processed. */
uint32_t state[5]; /*!< intermediate digest state */ uint32_t state[5]; /*!< The intermediate digest state. */
unsigned char buffer[64]; /*!< data block being processed */ unsigned char buffer[64]; /*!< The data block being processed. */
} }
mbedtls_sha1_context; mbedtls_sha1_context;
/** /**
* \brief Initialize SHA-1 context * \brief This function initializes a SHA-1 context.
*
* \param ctx The SHA-1 context to initialize.
*
* \warning SHA-1 is considered a weak message digest and its use
* constitutes a security risk. We recommend considering
* stronger message digests instead.
* *
* \param ctx SHA-1 context to be initialized
*/ */
void mbedtls_sha1_init( mbedtls_sha1_context *ctx ); void mbedtls_sha1_init( mbedtls_sha1_context *ctx );
/** /**
* \brief Clear SHA-1 context * \brief This function clears a SHA-1 context.
*
* \param ctx The SHA-1 context to clear.
*
* \warning SHA-1 is considered a weak message digest and its use
* constitutes a security risk. We recommend considering
* stronger message digests instead.
* *
* \param ctx SHA-1 context to be cleared
*/ */
void mbedtls_sha1_free( mbedtls_sha1_context *ctx ); void mbedtls_sha1_free( mbedtls_sha1_context *ctx );
/** /**
* \brief Clone (the state of) a SHA-1 context * \brief This function clones the state of a SHA-1 context.
*
* \param dst The destination context.
* \param src The context to clone.
*
* \warning SHA-1 is considered a weak message digest and its use
* constitutes a security risk. We recommend considering
* stronger message digests instead.
* *
* \param dst The destination context
* \param src The context to be cloned
*/ */
void mbedtls_sha1_clone( mbedtls_sha1_context *dst, void mbedtls_sha1_clone( mbedtls_sha1_context *dst,
const mbedtls_sha1_context *src ); const mbedtls_sha1_context *src );
/**
* \brief This function starts a SHA-1 checksum calculation.
*
* \param ctx The context to initialize.
*
* \return \c 0 if successful
*
* \warning SHA-1 is considered a weak message digest and its use
* constitutes a security risk. We recommend considering
* stronger message digests instead.
*
*/
int mbedtls_sha1_starts_ret( mbedtls_sha1_context *ctx );
/**
* \brief This function feeds an input buffer into an ongoing SHA-1
* checksum calculation.
*
* \param ctx The SHA-1 context.
* \param input The buffer holding the input data.
* \param ilen The length of the input data.
*
* \return \c 0 if successful
*
* \warning SHA-1 is considered a weak message digest and its use
* constitutes a security risk. We recommend considering
* stronger message digests instead.
*
*/
int mbedtls_sha1_update_ret( mbedtls_sha1_context *ctx,
const unsigned char *input,
size_t ilen );
/**
* \brief This function finishes the SHA-1 operation, and writes
* the result to the output buffer.
*
* \param ctx The SHA-1 context.
* \param output The SHA-1 checksum result.
*
* \return \c 0 if successful
*
* \warning SHA-1 is considered a weak message digest and its use
* constitutes a security risk. We recommend considering
* stronger message digests instead.
*
*/
int mbedtls_sha1_finish_ret( mbedtls_sha1_context *ctx,
unsigned char output[20] );
/**
* \brief SHA-1 process data block (internal use only)
*
* \param ctx SHA-1 context
* \param data The data block being processed.
*
* \return \c 0 if successful
*
* \warning SHA-1 is considered a weak message digest and its use
* constitutes a security risk. We recommend considering
* stronger message digests instead.
*
*/
int mbedtls_internal_sha1_process( mbedtls_sha1_context *ctx,
const unsigned char data[64] );
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
#if defined(MBEDTLS_DEPRECATED_WARNING)
#define MBEDTLS_DEPRECATED __attribute__((deprecated))
#else
#define MBEDTLS_DEPRECATED
#endif
/** /**
* \brief SHA-1 context setup * \brief SHA-1 context setup
* *
* \param ctx context to be initialized * \deprecated Superseded by mbedtls_sha1_starts_ret() in 2.7.0
*
* \param ctx The SHA-1 context to be initialized.
*
* \warning SHA-1 is considered a weak message digest and its use
* constitutes a security risk. We recommend considering
* stronger message digests instead.
*
*/ */
void mbedtls_sha1_starts( mbedtls_sha1_context *ctx ); MBEDTLS_DEPRECATED static inline void mbedtls_sha1_starts(
mbedtls_sha1_context *ctx )
{
mbedtls_sha1_starts_ret( ctx );
}
/** /**
* \brief SHA-1 process buffer * \brief SHA-1 process buffer
* *
* \param ctx SHA-1 context * \deprecated Superseded by mbedtls_sha1_update_ret() in 2.7.0
* \param input buffer holding the data *
* \param ilen length of the input data * \param ctx The SHA-1 context.
* \param input The buffer holding the input data.
* \param ilen The length of the input data.
*
* \warning SHA-1 is considered a weak message digest and its use
* constitutes a security risk. We recommend considering
* stronger message digests instead.
*
*/ */
void mbedtls_sha1_update( mbedtls_sha1_context *ctx, const unsigned char *input, size_t ilen ); MBEDTLS_DEPRECATED static inline void mbedtls_sha1_update(
mbedtls_sha1_context *ctx,
const unsigned char *input,
size_t ilen )
{
mbedtls_sha1_update_ret( ctx, input, ilen );
}
/** /**
* \brief SHA-1 final digest * \brief SHA-1 final digest
* *
* \param ctx SHA-1 context * \deprecated Superseded by mbedtls_sha1_finish_ret() in 2.7.0
* \param output SHA-1 checksum result *
* \param ctx The SHA-1 context.
* \param output The SHA-1 checksum result.
*
* \warning SHA-1 is considered a weak message digest and its use
* constitutes a security risk. We recommend considering
* stronger message digests instead.
*
*/
MBEDTLS_DEPRECATED static inline void mbedtls_sha1_finish(
mbedtls_sha1_context *ctx,
unsigned char output[20] )
{
mbedtls_sha1_finish_ret( ctx, output );
}
/**
* \brief SHA-1 process data block (internal use only)
*
* \deprecated Superseded by mbedtls_internal_sha1_process() in 2.7.0
*
* \param ctx The SHA-1 context.
* \param data The data block being processed.
*
* \warning SHA-1 is considered a weak message digest and its use
* constitutes a security risk. We recommend considering
* stronger message digests instead.
*
*/ */
void mbedtls_sha1_finish( mbedtls_sha1_context *ctx, unsigned char output[20] ); MBEDTLS_DEPRECATED static inline void mbedtls_sha1_process(
mbedtls_sha1_context *ctx,
const unsigned char data[64] )
{
mbedtls_internal_sha1_process( ctx, data );
}
/* Internal use */ #undef MBEDTLS_DEPRECATED
void mbedtls_sha1_process( mbedtls_sha1_context *ctx, const unsigned char data[64] ); #endif /* !MBEDTLS_DEPRECATED_REMOVED */
#ifdef __cplusplus #ifdef __cplusplus
} }
...@@ -113,19 +273,69 @@ void mbedtls_sha1_process( mbedtls_sha1_context *ctx, const unsigned char data[6 ...@@ -113,19 +273,69 @@ void mbedtls_sha1_process( mbedtls_sha1_context *ctx, const unsigned char data[6
extern "C" { extern "C" {
#endif #endif
/**
* \brief This function calculates the SHA-1 checksum of a buffer.
*
* The function allocates the context, performs the
* calculation, and frees the context.
*
* The SHA-1 result is calculated as
* output = SHA-1(input buffer).
*
* \param input The buffer holding the input data.
* \param ilen The length of the input data.
* \param output The SHA-1 checksum result.
*
* \return \c 0 if successful
*
* \warning SHA-1 is considered a weak message digest and its use
* constitutes a security risk. We recommend considering
* stronger message digests instead.
*
*/
int mbedtls_sha1_ret( const unsigned char *input,
size_t ilen,
unsigned char output[20] );
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
#if defined(MBEDTLS_DEPRECATED_WARNING)
#define MBEDTLS_DEPRECATED __attribute__((deprecated))
#else
#define MBEDTLS_DEPRECATED
#endif
/** /**
* \brief Output = SHA-1( input buffer ) * \brief Output = SHA-1( input buffer )
* *
* \param input buffer holding the data * \deprecated Superseded by mbedtls_sha1_ret() in 2.7.0
* \param ilen length of the input data *
* \param output SHA-1 checksum result * \param input The buffer holding the input data.
* \param ilen The length of the input data.
* \param output The SHA-1 checksum result.
*
* \warning SHA-1 is considered a weak message digest and its use
* constitutes a security risk. We recommend considering
* stronger message digests instead.
*
*/ */
void mbedtls_sha1( const unsigned char *input, size_t ilen, unsigned char output[20] ); MBEDTLS_DEPRECATED static inline void mbedtls_sha1( const unsigned char *input,
size_t ilen,
unsigned char output[20] )
{
mbedtls_sha1_ret( input, ilen, output );
}
#undef MBEDTLS_DEPRECATED
#endif /* !MBEDTLS_DEPRECATED_REMOVED */
/** /**
* \brief Checkup routine * \brief The SHA-1 checkup routine.
*
* \return \c 0 on success, or \c 1 on failure.
*
* \warning SHA-1 is considered a weak message digest and its use
* constitutes a security risk. We recommend considering
* stronger message digests instead.
* *
* \return 0 if successful, or 1 if the test failed
*/ */
int mbedtls_sha1_self_test( int verbose ); int mbedtls_sha1_self_test( int verbose );
......
/** /**
* \file sha256.h * \file sha256.h
* *
* \brief SHA-224 and SHA-256 cryptographic hash function * \brief The SHA-224 and SHA-256 cryptographic hash function.
* */
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved /*
* Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
* *
* Licensed under the Apache License, Version 2.0 (the "License"); you may * Licensed under the Apache License, Version 2.0 (the "License"); you may
...@@ -18,7 +19,7 @@ ...@@ -18,7 +19,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
* *
* This file is part of mbed TLS (https://tls.mbed.org) * This file is part of Mbed TLS (https://tls.mbed.org)
*/ */
#ifndef MBEDTLS_SHA256_H #ifndef MBEDTLS_SHA256_H
#define MBEDTLS_SHA256_H #define MBEDTLS_SHA256_H
...@@ -32,6 +33,12 @@ ...@@ -32,6 +33,12 @@
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
#define MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED -0x0037 /**< SHA-256 hardware accelerator failed */
#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
!defined(inline) && !defined(__cplusplus)
#define inline __inline
#endif
#if !defined(MBEDTLS_SHA256_ALT) #if !defined(MBEDTLS_SHA256_ALT)
// Regular implementation // Regular implementation
// //
...@@ -41,69 +48,174 @@ extern "C" { ...@@ -41,69 +48,174 @@ extern "C" {
#endif #endif
/** /**
* \brief SHA-256 context structure * \brief The SHA-256 context structure.
*
* The structure is used both for SHA-256 and for SHA-224
* checksum calculations. The choice between these two is
* made in the call to mbedtls_sha256_starts_ret().
*/ */
typedef struct typedef struct
{ {
uint32_t total[2]; /*!< number of bytes processed */ uint32_t total[2]; /*!< The number of Bytes processed. */
uint32_t state[8]; /*!< intermediate digest state */ uint32_t state[8]; /*!< The intermediate digest state. */
unsigned char buffer[64]; /*!< data block being processed */ unsigned char buffer[64]; /*!< The data block being processed. */
int is224; /*!< 0 => SHA-256, else SHA-224 */ int is224; /*!< Determines which function to use.
<ul><li>0: Use SHA-256.</li>
<li>1: Use SHA-224.</li></ul> */
} }
mbedtls_sha256_context; mbedtls_sha256_context;
/** /**
* \brief Initialize SHA-256 context * \brief This function initializes a SHA-256 context.
* *
* \param ctx SHA-256 context to be initialized * \param ctx The SHA-256 context to initialize.
*/ */
void mbedtls_sha256_init( mbedtls_sha256_context *ctx ); void mbedtls_sha256_init( mbedtls_sha256_context *ctx );
/** /**
* \brief Clear SHA-256 context * \brief This function clears a SHA-256 context.
* *
* \param ctx SHA-256 context to be cleared * \param ctx The SHA-256 context to clear.
*/ */
void mbedtls_sha256_free( mbedtls_sha256_context *ctx ); void mbedtls_sha256_free( mbedtls_sha256_context *ctx );
/** /**
* \brief Clone (the state of) a SHA-256 context * \brief This function clones the state of a SHA-256 context.
* *
* \param dst The destination context * \param dst The destination context.
* \param src The context to be cloned * \param src The context to clone.
*/ */
void mbedtls_sha256_clone( mbedtls_sha256_context *dst, void mbedtls_sha256_clone( mbedtls_sha256_context *dst,
const mbedtls_sha256_context *src ); const mbedtls_sha256_context *src );
/** /**
* \brief SHA-256 context setup * \brief This function starts a SHA-224 or SHA-256 checksum
* calculation.
*
* \param ctx The context to initialize.
* \param is224 Determines which function to use.
* <ul><li>0: Use SHA-256.</li>
* <li>1: Use SHA-224.</li></ul>
* *
* \param ctx context to be initialized * \return \c 0 on success.
* \param is224 0 = use SHA256, 1 = use SHA224
*/ */
void mbedtls_sha256_starts( mbedtls_sha256_context *ctx, int is224 ); int mbedtls_sha256_starts_ret( mbedtls_sha256_context *ctx, int is224 );
/** /**
* \brief SHA-256 process buffer * \brief This function feeds an input buffer into an ongoing
* SHA-256 checksum calculation.
* *
* \param ctx SHA-256 context * \param ctx SHA-256 context
* \param input buffer holding the data * \param input buffer holding the data
* \param ilen length of the input data * \param ilen length of the input data
*
* \return \c 0 on success.
*/ */
void mbedtls_sha256_update( mbedtls_sha256_context *ctx, const unsigned char *input, int mbedtls_sha256_update_ret( mbedtls_sha256_context *ctx,
size_t ilen ); const unsigned char *input,
size_t ilen );
/** /**
* \brief SHA-256 final digest * \brief This function finishes the SHA-256 operation, and writes
* the result to the output buffer.
* *
* \param ctx SHA-256 context * \param ctx The SHA-256 context.
* \param output SHA-224/256 checksum result * \param output The SHA-224 or SHA-256 checksum result.
*
* \return \c 0 on success.
*/
int mbedtls_sha256_finish_ret( mbedtls_sha256_context *ctx,
unsigned char output[32] );
/**
* \brief This function processes a single data block within
* the ongoing SHA-256 computation. This function is for
* internal use only.
*
* \param ctx The SHA-256 context.
* \param data The buffer holding one block of data.
*
* \return \c 0 on success.
*/
int mbedtls_internal_sha256_process( mbedtls_sha256_context *ctx,
const unsigned char data[64] );
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
#if defined(MBEDTLS_DEPRECATED_WARNING)
#define MBEDTLS_DEPRECATED __attribute__((deprecated))
#else
#define MBEDTLS_DEPRECATED
#endif
/**
* \brief This function starts a SHA-256 checksum calculation.
*
* \deprecated Superseded by mbedtls_sha256_starts_ret() in 2.7.0.
*
* \param ctx The SHA-256 context to initialize.
* \param is224 Determines which function to use.
* <ul><li>0: Use SHA-256.</li>
* <li>1: Use SHA-224.</li></ul>
*/ */
void mbedtls_sha256_finish( mbedtls_sha256_context *ctx, unsigned char output[32] ); MBEDTLS_DEPRECATED static inline void mbedtls_sha256_starts(
mbedtls_sha256_context *ctx,
int is224 )
{
mbedtls_sha256_starts_ret( ctx, is224 );
}
/* Internal use */ /**
void mbedtls_sha256_process( mbedtls_sha256_context *ctx, const unsigned char data[64] ); * \brief This function feeds an input buffer into an ongoing
* SHA-256 checksum calculation.
*
* \deprecated Superseded by mbedtls_sha256_update_ret() in 2.7.0.
*
* \param ctx The SHA-256 context to initialize.
* \param input The buffer holding the data.
* \param ilen The length of the input data.
*/
MBEDTLS_DEPRECATED static inline void mbedtls_sha256_update(
mbedtls_sha256_context *ctx,
const unsigned char *input,
size_t ilen )
{
mbedtls_sha256_update_ret( ctx, input, ilen );
}
/**
* \brief This function finishes the SHA-256 operation, and writes
* the result to the output buffer.
*
* \deprecated Superseded by mbedtls_sha256_finish_ret() in 2.7.0.
*
* \param ctx The SHA-256 context.
* \param output The SHA-224or SHA-256 checksum result.
*/
MBEDTLS_DEPRECATED static inline void mbedtls_sha256_finish(
mbedtls_sha256_context *ctx,
unsigned char output[32] )
{
mbedtls_sha256_finish_ret( ctx, output );
}
/**
* \brief This function processes a single data block within
* the ongoing SHA-256 computation. This function is for
* internal use only.
*
* \deprecated Superseded by mbedtls_internal_sha256_process() in 2.7.0.
*
* \param ctx The SHA-256 context.
* \param data The buffer holding one block of data.
*/
MBEDTLS_DEPRECATED static inline void mbedtls_sha256_process(
mbedtls_sha256_context *ctx,
const unsigned char data[64] )
{
mbedtls_internal_sha256_process( ctx, data );
}
#undef MBEDTLS_DEPRECATED
#endif /* !MBEDTLS_DEPRECATED_REMOVED */
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
...@@ -117,20 +229,69 @@ extern "C" { ...@@ -117,20 +229,69 @@ extern "C" {
#endif #endif
/** /**
* \brief Output = SHA-256( input buffer ) * \brief This function calculates the SHA-224 or SHA-256
* checksum of a buffer.
* *
* \param input buffer holding the data * The function allocates the context, performs the
* \param ilen length of the input data * calculation, and frees the context.
* \param output SHA-224/256 checksum result *
* \param is224 0 = use SHA256, 1 = use SHA224 * The SHA-256 result is calculated as
* output = SHA-256(input buffer).
*
* \param input The buffer holding the input data.
* \param ilen The length of the input data.
* \param output The SHA-224 or SHA-256 checksum result.
* \param is224 Determines which function to use.
* <ul><li>0: Use SHA-256.</li>
* <li>1: Use SHA-224.</li></ul>
*/
int mbedtls_sha256_ret( const unsigned char *input,
size_t ilen,
unsigned char output[32],
int is224 );
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
#if defined(MBEDTLS_DEPRECATED_WARNING)
#define MBEDTLS_DEPRECATED __attribute__((deprecated))
#else
#define MBEDTLS_DEPRECATED
#endif
/**
* \brief This function calculates the SHA-224 or SHA-256 checksum
* of a buffer.
*
* The function allocates the context, performs the
* calculation, and frees the context.
*
* The SHA-256 result is calculated as
* output = SHA-256(input buffer).
*
* \deprecated Superseded by mbedtls_sha256_ret() in 2.7.0.
*
* \param input The buffer holding the data.
* \param ilen The length of the input data.
* \param output The SHA-224 or SHA-256 checksum result.
* \param is224 Determines which function to use.
* <ul><li>0: Use SHA-256.</li>
* <li>1: Use SHA-224.</li></ul>
*/ */
void mbedtls_sha256( const unsigned char *input, size_t ilen, MBEDTLS_DEPRECATED static inline void mbedtls_sha256(
unsigned char output[32], int is224 ); const unsigned char *input,
size_t ilen,
unsigned char output[32],
int is224 )
{
mbedtls_sha256_ret( input, ilen, output, is224 );
}
#undef MBEDTLS_DEPRECATED
#endif /* !MBEDTLS_DEPRECATED_REMOVED */
/** /**
* \brief Checkup routine * \brief The SHA-224 and SHA-256 checkup routine.
* *
* \return 0 if successful, or 1 if the test failed * \return \c 0 on success, or \c 1 on failure.
*/ */
int mbedtls_sha256_self_test( int verbose ); int mbedtls_sha256_self_test( int verbose );
......
/** /**
* \file sha512.h * \file sha512.h
* *
* \brief SHA-384 and SHA-512 cryptographic hash function * \brief The SHA-384 and SHA-512 cryptographic hash function.
* */
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved /*
* Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
* *
* Licensed under the Apache License, Version 2.0 (the "License"); you may * Licensed under the Apache License, Version 2.0 (the "License"); you may
...@@ -18,7 +19,7 @@ ...@@ -18,7 +19,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
* *
* This file is part of mbed TLS (https://tls.mbed.org) * This file is part of Mbed TLS (https://tls.mbed.org)
*/ */
#ifndef MBEDTLS_SHA512_H #ifndef MBEDTLS_SHA512_H
#define MBEDTLS_SHA512_H #define MBEDTLS_SHA512_H
...@@ -32,6 +33,12 @@ ...@@ -32,6 +33,12 @@
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
#define MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED -0x0039 /**< SHA-512 hardware accelerator failed */
#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
!defined(inline) && !defined(__cplusplus)
#define inline __inline
#endif
#if !defined(MBEDTLS_SHA512_ALT) #if !defined(MBEDTLS_SHA512_ALT)
// Regular implementation // Regular implementation
// //
...@@ -41,65 +48,174 @@ extern "C" { ...@@ -41,65 +48,174 @@ extern "C" {
#endif #endif
/** /**
* \brief SHA-512 context structure * \brief The SHA-512 context structure.
*
* The structure is used both for SHA-384 and for SHA-512
* checksum calculations. The choice between these two is
* made in the call to mbedtls_sha512_starts_ret().
*/ */
typedef struct typedef struct
{ {
uint64_t total[2]; /*!< number of bytes processed */ uint64_t total[2]; /*!< The number of Bytes processed. */
uint64_t state[8]; /*!< intermediate digest state */ uint64_t state[8]; /*!< The intermediate digest state. */
unsigned char buffer[128]; /*!< data block being processed */ unsigned char buffer[128]; /*!< The data block being processed. */
int is384; /*!< 0 => SHA-512, else SHA-384 */ int is384; /*!< Determines which function to use.
* <ul><li>0: Use SHA-512.</li>
* <li>1: Use SHA-384.</li></ul> */
} }
mbedtls_sha512_context; mbedtls_sha512_context;
/** /**
* \brief Initialize SHA-512 context * \brief This function initializes a SHA-512 context.
* *
* \param ctx SHA-512 context to be initialized * \param ctx The SHA-512 context to initialize.
*/ */
void mbedtls_sha512_init( mbedtls_sha512_context *ctx ); void mbedtls_sha512_init( mbedtls_sha512_context *ctx );
/** /**
* \brief Clear SHA-512 context * \brief This function clears a SHA-512 context.
* *
* \param ctx SHA-512 context to be cleared * \param ctx The SHA-512 context to clear.
*/ */
void mbedtls_sha512_free( mbedtls_sha512_context *ctx ); void mbedtls_sha512_free( mbedtls_sha512_context *ctx );
/** /**
* \brief Clone (the state of) a SHA-512 context * \brief This function clones the state of a SHA-512 context.
* *
* \param dst The destination context * \param dst The destination context.
* \param src The context to be cloned * \param src The context to clone.
*/ */
void mbedtls_sha512_clone( mbedtls_sha512_context *dst, void mbedtls_sha512_clone( mbedtls_sha512_context *dst,
const mbedtls_sha512_context *src ); const mbedtls_sha512_context *src );
/** /**
* \brief SHA-512 context setup * \brief This function starts a SHA-384 or SHA-512 checksum
* calculation.
*
* \param ctx The SHA-512 context to initialize.
* \param is384 Determines which function to use.
* <ul><li>0: Use SHA-512.</li>
* <li>1: Use SHA-384.</li></ul>
* *
* \param ctx context to be initialized * \return \c 0 on success.
* \param is384 0 = use SHA512, 1 = use SHA384
*/ */
void mbedtls_sha512_starts( mbedtls_sha512_context *ctx, int is384 ); int mbedtls_sha512_starts_ret( mbedtls_sha512_context *ctx, int is384 );
/** /**
* \brief SHA-512 process buffer * \brief This function feeds an input buffer into an ongoing
* SHA-512 checksum calculation.
* *
* \param ctx SHA-512 context * \param ctx The SHA-512 context.
* \param input buffer holding the data * \param input The buffer holding the input data.
* \param ilen length of the input data * \param ilen The length of the input data.
*
* \return \c 0 on success.
*/ */
void mbedtls_sha512_update( mbedtls_sha512_context *ctx, const unsigned char *input, int mbedtls_sha512_update_ret( mbedtls_sha512_context *ctx,
const unsigned char *input,
size_t ilen ); size_t ilen );
/** /**
* \brief SHA-512 final digest * \brief This function finishes the SHA-512 operation, and writes
* the result to the output buffer. This function is for
* internal use only.
*
* \param ctx The SHA-512 context.
* \param output The SHA-384 or SHA-512 checksum result.
* *
* \param ctx SHA-512 context * \return \c 0 on success.
* \param output SHA-384/512 checksum result
*/ */
void mbedtls_sha512_finish( mbedtls_sha512_context *ctx, unsigned char output[64] ); int mbedtls_sha512_finish_ret( mbedtls_sha512_context *ctx,
unsigned char output[64] );
/**
* \brief This function processes a single data block within
* the ongoing SHA-512 computation.
*
* \param ctx The SHA-512 context.
* \param data The buffer holding one block of data.
*
* \return \c 0 on success.
*/
int mbedtls_internal_sha512_process( mbedtls_sha512_context *ctx,
const unsigned char data[128] );
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
#if defined(MBEDTLS_DEPRECATED_WARNING)
#define MBEDTLS_DEPRECATED __attribute__((deprecated))
#else
#define MBEDTLS_DEPRECATED
#endif
/**
* \brief This function starts a SHA-384 or SHA-512 checksum
* calculation.
*
* \deprecated Superseded by mbedtls_sha512_starts_ret() in 2.7.0
*
* \param ctx The SHA-512 context to initialize.
* \param is384 Determines which function to use.
* <ul><li>0: Use SHA-512.</li>
* <li>1: Use SHA-384.</li></ul>
*/
MBEDTLS_DEPRECATED static inline void mbedtls_sha512_starts(
mbedtls_sha512_context *ctx,
int is384 )
{
mbedtls_sha512_starts_ret( ctx, is384 );
}
/**
* \brief This function feeds an input buffer into an ongoing
* SHA-512 checksum calculation.
*
* \deprecated Superseded by mbedtls_sha512_update_ret() in 2.7.0
*
* \param ctx The SHA-512 context.
* \param input The buffer holding the data.
* \param ilen The length of the input data.
*/
MBEDTLS_DEPRECATED static inline void mbedtls_sha512_update(
mbedtls_sha512_context *ctx,
const unsigned char *input,
size_t ilen )
{
mbedtls_sha512_update_ret( ctx, input, ilen );
}
/**
* \brief This function finishes the SHA-512 operation, and writes
* the result to the output buffer.
*
* \deprecated Superseded by mbedtls_sha512_finish_ret() in 2.7.0
*
* \param ctx The SHA-512 context.
* \param output The SHA-384 or SHA-512 checksum result.
*/
MBEDTLS_DEPRECATED static inline void mbedtls_sha512_finish(
mbedtls_sha512_context *ctx,
unsigned char output[64] )
{
mbedtls_sha512_finish_ret( ctx, output );
}
/**
* \brief This function processes a single data block within
* the ongoing SHA-512 computation. This function is for
* internal use only.
*
* \deprecated Superseded by mbedtls_internal_sha512_process() in 2.7.0
*
* \param ctx The SHA-512 context.
* \param data The buffer holding one block of data.
*/
MBEDTLS_DEPRECATED static inline void mbedtls_sha512_process(
mbedtls_sha512_context *ctx,
const unsigned char data[128] )
{
mbedtls_internal_sha512_process( ctx, data );
}
#undef MBEDTLS_DEPRECATED
#endif /* !MBEDTLS_DEPRECATED_REMOVED */
#ifdef __cplusplus #ifdef __cplusplus
} }
...@@ -114,25 +230,71 @@ extern "C" { ...@@ -114,25 +230,71 @@ extern "C" {
#endif #endif
/** /**
* \brief Output = SHA-512( input buffer ) * \brief This function calculates the SHA-512 or SHA-384
* checksum of a buffer.
* *
* \param input buffer holding the data * The function allocates the context, performs the
* \param ilen length of the input data * calculation, and frees the context.
* \param output SHA-384/512 checksum result *
* \param is384 0 = use SHA512, 1 = use SHA384 * The SHA-512 result is calculated as
* output = SHA-512(input buffer).
*
* \param input The buffer holding the input data.
* \param ilen The length of the input data.
* \param output The SHA-384 or SHA-512 checksum result.
* \param is384 Determines which function to use.
* <ul><li>0: Use SHA-512.</li>
* <li>1: Use SHA-384.</li></ul>
*
* \return \c 0 on success.
*/ */
void mbedtls_sha512( const unsigned char *input, size_t ilen, int mbedtls_sha512_ret( const unsigned char *input,
unsigned char output[64], int is384 ); size_t ilen,
unsigned char output[64],
int is384 );
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
#if defined(MBEDTLS_DEPRECATED_WARNING)
#define MBEDTLS_DEPRECATED __attribute__((deprecated))
#else
#define MBEDTLS_DEPRECATED
#endif
/** /**
* \brief Checkup routine * \brief This function calculates the SHA-512 or SHA-384
* checksum of a buffer.
*
* The function allocates the context, performs the
* calculation, and frees the context.
*
* The SHA-512 result is calculated as
* output = SHA-512(input buffer).
* *
* \return 0 if successful, or 1 if the test failed * \deprecated Superseded by mbedtls_sha512_ret() in 2.7.0
*
* \param input The buffer holding the data.
* \param ilen The length of the input data.
* \param output The SHA-384 or SHA-512 checksum result.
* \param is384 Determines which function to use.
* <ul><li>0: Use SHA-512.</li>
* <li>1: Use SHA-384.</li></ul>
*/ */
int mbedtls_sha512_self_test( int verbose ); MBEDTLS_DEPRECATED static inline void mbedtls_sha512(
const unsigned char *input,
size_t ilen,
unsigned char output[64],
int is384 )
{
mbedtls_sha512_ret( input, ilen, output, is384 );
}
/* Internal use */ #undef MBEDTLS_DEPRECATED
void mbedtls_sha512_process( mbedtls_sha512_context *ctx, const unsigned char data[128] ); #endif /* !MBEDTLS_DEPRECATED_REMOVED */
/**
* \brief The SHA-384 or SHA-512 checkup routine.
*
* \return \c 0 on success, or \c 1 on failure.
*/
int mbedtls_sha512_self_test( int verbose );
#ifdef __cplusplus #ifdef __cplusplus
} }
......
...@@ -2,7 +2,8 @@ ...@@ -2,7 +2,8 @@
* \file ssl.h * \file ssl.h
* *
* \brief SSL/TLS functions. * \brief SSL/TLS functions.
* */
/*
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
* *
...@@ -1699,18 +1700,50 @@ void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf, ...@@ -1699,18 +1700,50 @@ void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */ #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
#if defined(MBEDTLS_DEPRECATED_WARNING)
#define MBEDTLS_DEPRECATED __attribute__((deprecated))
#else
#define MBEDTLS_DEPRECATED
#endif
/** /**
* \brief Set the Diffie-Hellman public P and G values, * \brief Set the Diffie-Hellman public P and G values,
* read as hexadecimal strings (server-side only) * read as hexadecimal strings (server-side only)
* (Default: MBEDTLS_DHM_RFC5114_MODP_2048_[PG]) * (Default values: MBEDTLS_DHM_RFC3526_MODP_2048_[PG])
* *
* \param conf SSL configuration * \param conf SSL configuration
* \param dhm_P Diffie-Hellman-Merkle modulus * \param dhm_P Diffie-Hellman-Merkle modulus
* \param dhm_G Diffie-Hellman-Merkle generator * \param dhm_G Diffie-Hellman-Merkle generator
* *
* \deprecated Superseded by \c mbedtls_ssl_conf_dh_param_bin.
*
* \return 0 if successful * \return 0 if successful
*/ */
int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G ); MBEDTLS_DEPRECATED int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf,
const char *dhm_P,
const char *dhm_G );
#endif /* MBEDTLS_DEPRECATED_REMOVED */
/**
* \brief Set the Diffie-Hellman public P and G values
* from big-endian binary presentations.
* (Default values: MBEDTLS_DHM_RFC3526_MODP_2048_[PG]_BIN)
*
* \param conf SSL configuration
* \param dhm_P Diffie-Hellman-Merkle modulus in big-endian binary form
* \param P_len Length of DHM modulus
* \param dhm_G Diffie-Hellman-Merkle generator in big-endian binary form
* \param G_len Length of DHM generator
*
* \return 0 if successful
*/
int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
const unsigned char *dhm_P, size_t P_len,
const unsigned char *dhm_G, size_t G_len );
/** /**
* \brief Set the Diffie-Hellman public P and G values, * \brief Set the Diffie-Hellman public P and G values,
...@@ -1794,15 +1827,22 @@ void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf, ...@@ -1794,15 +1827,22 @@ void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
#if defined(MBEDTLS_X509_CRT_PARSE_C) #if defined(MBEDTLS_X509_CRT_PARSE_C)
/** /**
* \brief Set the hostname to check against the received server * \brief Set or reset the hostname to check against the received
* certificate. It sets the ServerName TLS extension too, * server certificate. It sets the ServerName TLS extension,
* if the extension is enabled. * too, if that extension is enabled. (client-side only)
* (client-side only)
* *
* \param ssl SSL context * \param ssl SSL context
* \param hostname the server hostname * \param hostname the server hostname, may be NULL to clear hostname
* \note Maximum hostname length MBEDTLS_SSL_MAX_HOST_NAME_LEN.
* *
* \return 0 if successful or MBEDTLS_ERR_SSL_ALLOC_FAILED * \return 0 if successful, MBEDTLS_ERR_SSL_ALLOC_FAILED on
* allocation failure, MBEDTLS_ERR_SSL_BAD_INPUT_DATA on
* too long input hostname.
*
* Hostname set to the one provided on success (cleared
* when NULL). On allocation failure hostname is cleared.
* On too long input failure, old hostname is unchanged.
*/ */
int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname ); int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname );
#endif /* MBEDTLS_X509_CRT_PARSE_C */ #endif /* MBEDTLS_X509_CRT_PARSE_C */
......
...@@ -2,7 +2,8 @@ ...@@ -2,7 +2,8 @@
* \file ssl_cache.h * \file ssl_cache.h
* *
* \brief SSL session cache implementation * \brief SSL session cache implementation
* */
/*
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
* *
......
...@@ -2,7 +2,8 @@ ...@@ -2,7 +2,8 @@
* \file ssl_ciphersuites.h * \file ssl_ciphersuites.h
* *
* \brief SSL Ciphersuites for mbed TLS * \brief SSL Ciphersuites for mbed TLS
* */
/*
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
* *
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment