Move MBEDTLS_ERR_ADD macro and functions to error.*

`error.c` and  error.h are the more logical place to keep this code and it
prevents issues with building `common.c` and conflicts with other projects
that use mbedtls (such as mbedOS).

`error.c` has been automatically generated by first adding the code to
`error.fmt` and then running `./scripts/generate_errors.pl`.

Also add parenthesis to the addition in `MBEDTLS_ERR_ADD`.

Signed-off-by: Chris Jones <christopher.jones@arm.com>
This commit is contained in:
Chris Jones 2021-01-26 17:50:48 +00:00
parent 808b7c8a8a
commit ef180af350
9 changed files with 57 additions and 64 deletions

View file

@ -114,6 +114,28 @@ extern "C" {
#define MBEDTLS_ERR_ERROR_GENERIC_ERROR -0x0001 /**< Generic error */ #define MBEDTLS_ERR_ERROR_GENERIC_ERROR -0x0001 /**< Generic error */
#define MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED -0x006E /**< This is a bug in the library */ #define MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED -0x006E /**< This is a bug in the library */
/** Helper macro and function to combine a high and low level error code.
*
* This function uses a hook (`mbedtls_test_err_add_hook`) to allow invasive
* testing of its inputs. This is used in the test infrastructure to report
* on errors when combining two error codes of the same level (e.g: two high
* or two low level errors).
*
* To set a hook use
* ```
* mbedtls_set_err_add_hook(&mbedtls_check_foo);
* ```
*/
#if defined(MBEDTLS_TEST_HOOKS)
void mbedtls_set_err_add_hook( void *hook );
int mbedtls_err_add( int high, int low, const char *file, int line );
#define MBEDTLS_ERR_ADD( high, low ) \
( mbedtls_err_add( high, low, __FILE__, __LINE__ ) )
#else
#define MBEDTLS_ERR_ADD( high, low ) \
( ( high ) + ( low ) )
#endif /* MBEDTLS_TEST_HOOKS */
/** /**
* \brief Translate a mbed TLS error code into a string representation, * \brief Translate a mbed TLS error code into a string representation,
* Result is truncated if necessary and always includes a terminating * Result is truncated if necessary and always includes a terminating

View file

@ -27,7 +27,6 @@ set(src_crypto
cipher.c cipher.c
cipher_wrap.c cipher_wrap.c
cmac.c cmac.c
common.c
ctr_drbg.c ctr_drbg.c
des.c des.c
dhm.c dhm.c

View file

@ -84,7 +84,6 @@ OBJS_CRYPTO= \
cipher.o \ cipher.o \
cipher_wrap.o \ cipher_wrap.o \
cmac.o \ cmac.o \
common.o \
ctr_drbg.o \ ctr_drbg.o \
des.o \ des.o \
dhm.o \ dhm.o \

View file

@ -1,36 +0,0 @@
/*
* Internal invasive testing helper functions
*
* Copyright The Mbed TLS Contributors
* 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.
*/
#include "common.h"
#include <stddef.h>
#if defined(MBEDTLS_TEST_HOOKS)
static void (*err_add_hook)( int, int, const char *, int );
void mbedtls_set_err_add_hook(void *hook)
{
err_add_hook = hook;
}
int mbedtls_err_add( int high, int low, const char *file, int line )
{
if( err_add_hook != NULL )
(*err_add_hook)( high, low, file, line );
return ( high + low );
}
#endif

View file

@ -29,7 +29,6 @@
#include "mbedtls/config.h" #include "mbedtls/config.h"
#endif #endif
#if defined(MBEDTLS_TEST_HOOKS)
/** Helper to define a function as static except when building invasive tests. /** Helper to define a function as static except when building invasive tests.
* *
* If a function is only used inside its own source file and should be * If a function is only used inside its own source file and should be
@ -45,31 +44,10 @@
* #endif * #endif
* ``` * ```
*/ */
#if defined(MBEDTLS_TEST_HOOKS)
#define MBEDTLS_STATIC_TESTABLE #define MBEDTLS_STATIC_TESTABLE
/** Helper macro and function to combine a high and low level error code.
*
* This function uses a hook (`mbedtls_test_err_add_hook`) to allow invasive
* testing of its inputs. This is used in the test infrastructure to report
* on errors when combining two error codes of the same level (e.g: two high
* or two low level errors).
*
* To set a hook use
* ```
* mbedtls_set_err_add_hook(&mbedtls_check_foo);
* ```
*/
void mbedtls_set_err_add_hook( void *hook );
int mbedtls_err_add( int high, int low, const char *file, int line );
#define MBEDTLS_ERR_ADD( high, low ) \
( mbedtls_err_add( high, low, __FILE__, __LINE__ ) )
#else #else
#define MBEDTLS_STATIC_TESTABLE static #define MBEDTLS_STATIC_TESTABLE static
#endif
#define MBEDTLS_ERR_ADD( high, low ) \
( high + low )
#endif /* MBEDTLS_TEST_HOOKS */
#endif /* MBEDTLS_LIBRARY_COMMON_H */ #endif /* MBEDTLS_LIBRARY_COMMON_H */

View file

@ -893,6 +893,22 @@ const char * mbedtls_low_level_strerr( int error_code )
return( NULL ); return( NULL );
} }
#if defined(MBEDTLS_TEST_HOOKS)
static void (*err_add_hook)( int, int, const char *, int );
void mbedtls_set_err_add_hook(void *hook)
{
err_add_hook = hook;
}
int mbedtls_err_add( int high, int low, const char *file, int line )
{
if( err_add_hook != NULL )
(*err_add_hook)( high, low, file, line );
return ( high + low );
}
#endif /* MBEDTLS_TEST_HOOKS */
void mbedtls_strerror( int ret, char *buf, size_t buflen ) void mbedtls_strerror( int ret, char *buf, size_t buflen )
{ {
size_t len; size_t len;

View file

@ -82,6 +82,22 @@ LOW_LEVEL_CODE_CHECKS
return( NULL ); return( NULL );
} }
#if defined(MBEDTLS_TEST_HOOKS)
static void (*err_add_hook)( int, int, const char *, int );
void mbedtls_set_err_add_hook(void *hook)
{
err_add_hook = hook;
}
int mbedtls_err_add( int high, int low, const char *file, int line )
{
if( err_add_hook != NULL )
(*err_add_hook)( high, low, file, line );
return ( high + low );
}
#endif /* MBEDTLS_TEST_HOOKS */
void mbedtls_strerror( int ret, char *buf, size_t buflen ) void mbedtls_strerror( int ret, char *buf, size_t buflen )
{ {
size_t len; size_t len;

View file

@ -34,7 +34,7 @@
#endif /* MBEDTLS_USE_PSA_CRYPTO */ #endif /* MBEDTLS_USE_PSA_CRYPTO */
#if defined(MBEDTLS_TEST_HOOKS) #if defined(MBEDTLS_TEST_HOOKS)
#include "common.h" #include "mbedtls/error.h"
#endif #endif
/* Test code may use deprecated identifiers only if the preprocessor symbol /* Test code may use deprecated identifiers only if the preprocessor symbol

View file

@ -293,7 +293,6 @@
<ClCompile Include="..\..\library\cipher.c" /> <ClCompile Include="..\..\library\cipher.c" />
<ClCompile Include="..\..\library\cipher_wrap.c" /> <ClCompile Include="..\..\library\cipher_wrap.c" />
<ClCompile Include="..\..\library\cmac.c" /> <ClCompile Include="..\..\library\cmac.c" />
<ClCompile Include="..\..\library\common.c" />
<ClCompile Include="..\..\library\ctr_drbg.c" /> <ClCompile Include="..\..\library\ctr_drbg.c" />
<ClCompile Include="..\..\library\debug.c" /> <ClCompile Include="..\..\library\debug.c" />
<ClCompile Include="..\..\library\des.c" /> <ClCompile Include="..\..\library\des.c" />