Move MPS trace macros to MBEDTLS_MPS_ namespace

Signed-off-by: Hanno Becker <hanno.becker@arm.com>
This commit is contained in:
Hanno Becker 2021-01-28 09:02:18 +00:00
parent c518c3b7bb
commit 984fbded58
6 changed files with 208 additions and 171 deletions

View file

@ -26,6 +26,8 @@
#ifndef MBEDTLS_MPS_COMMON_H #ifndef MBEDTLS_MPS_COMMON_H
#define MBEDTLS_MPS_COMMON_H #define MBEDTLS_MPS_COMMON_H
#include "mps_error.h"
#include <stdio.h> #include <stdio.h>
/** /**
@ -90,18 +92,18 @@
#define MBEDTLS_MPS_ENABLE_ASSERTIONS #define MBEDTLS_MPS_ENABLE_ASSERTIONS
/*! This flag controls whether tracing for MPS should be enabled. */ /*! This flag controls whether tracing for MPS should be enabled. */
//#define MBEDTLS_MPS_TRACE //#define MBEDTLS_MPS_ENABLE_TRACE
#if defined(MBEDTLS_MPS_STATE_VALIDATION) #if defined(MBEDTLS_MPS_STATE_VALIDATION)
#define MBEDTLS_MPS_STATE_VALIDATE_RAW( cond, string ) \ #define MBEDTLS_MPS_STATE_VALIDATE_RAW( cond, string ) \
do \ do \
{ \ { \
if( !(cond) ) \ if( !(cond) ) \
{ \ { \
TRACE( trace_error, string ); \ MBEDTLS_MPS_TRACE( mbedtls_mps_trace_error, string ); \
RETURN( MBEDTLS_ERR_MPS_OPERATION_UNEXPECTED ); \ MBEDTLS_MPS_TRACE_RETURN( MBEDTLS_ERR_MPS_OPERATION_UNEXPECTED ); \
} \ } \
} while( 0 ) } while( 0 )
#else /* MBEDTLS_MPS_STATE_VALIDATION */ #else /* MBEDTLS_MPS_STATE_VALIDATION */
@ -116,14 +118,14 @@
#if defined(MBEDTLS_MPS_ENABLE_ASSERTIONS) #if defined(MBEDTLS_MPS_ENABLE_ASSERTIONS)
#define MBEDTLS_MPS_ASSERT_RAW( cond, string ) \ #define MBEDTLS_MPS_ASSERT_RAW( cond, string ) \
do \ do \
{ \ { \
if( !(cond) ) \ if( !(cond) ) \
{ \ { \
TRACE( trace_error, string ); \ MBEDTLS_MPS_TRACE( mbedtls_mps_trace_error, string ); \
RETURN( MBEDTLS_ERR_MPS_INTERNAL_ERROR ); \ MBEDTLS_MPS_TRACE_RETURN( MBEDTLS_ERR_MPS_INTERNAL_ERROR ); \
} \ } \
} while( 0 ) } while( 0 )
#else /* MBEDTLS_MPS_ENABLE_ASSERTIONS */ #else /* MBEDTLS_MPS_ENABLE_ASSERTIONS */

View file

@ -30,7 +30,7 @@
/* TODO: The error code allocation needs to be revisited: /* TODO: The error code allocation needs to be revisited:
* *
* - Should we make (some of) the MPS Reader error codes public? * - Should we make (some of) the MPS Reader error codes public?
* If so, we need to adjust MBEDTLS_READER_MAKE_ERROR() to hit * If so, we need to adjust MBEDTLS_MPS_READER_MAKE_ERROR() to hit
* a gap in the Mbed TLS public error space. * a gap in the Mbed TLS public error space.
* If not, we have to make sure we don't forward those errors * If not, we have to make sure we don't forward those errors
* at the level of the public API -- no risk at the moment as * at the level of the public API -- no risk at the moment as
@ -57,6 +57,7 @@
#define MBEDTLS_ERR_MPS_OPERATION_UNEXPECTED MBEDTLS_MPS_MAKE_ERROR( 0x1 ) #define MBEDTLS_ERR_MPS_OPERATION_UNEXPECTED MBEDTLS_MPS_MAKE_ERROR( 0x1 )
#define MBEDTLS_ERR_MPS_INTERNAL_ERROR MBEDTLS_MPS_MAKE_ERROR( 0x2 )
/* \} name SECTION: MPS general error codes */ /* \} name SECTION: MPS general error codes */

View file

@ -30,9 +30,9 @@
#define inline __inline #define inline __inline
#endif #endif
#if defined(MBEDTLS_MPS_TRACE) #if defined(MBEDTLS_MPS_ENABLE_TRACE)
static int trace_id = TRACE_BIT_READER; static int mbedtls_mps_trace_id = MBEDTLS_MPS_TRACE_BIT_READER;
#endif /* MBEDTLS_MPS_TRACE */ #endif /* MBEDTLS_MPS_ENABLE_TRACE */
/* /*
* GENERAL NOTE ON CODING STYLE * GENERAL NOTE ON CODING STYLE
@ -92,18 +92,18 @@ int mbedtls_reader_init( mbedtls_reader *rd,
unsigned char *acc, unsigned char *acc,
mbedtls_mps_size_t acc_len ) mbedtls_mps_size_t acc_len )
{ {
TRACE_INIT( "reader_init, acc len %u", (unsigned) acc_len ); MBEDTLS_MPS_TRACE_INIT( "reader_init, acc len %u", (unsigned) acc_len );
mps_reader_zero( rd ); mps_reader_zero( rd );
rd->acc = acc; rd->acc = acc;
rd->acc_len = acc_len; rd->acc_len = acc_len;
RETURN( 0 ); MBEDTLS_MPS_TRACE_RETURN( 0 );
} }
int mbedtls_reader_free( mbedtls_reader *rd ) int mbedtls_reader_free( mbedtls_reader *rd )
{ {
TRACE_INIT( "reader_free" ); MBEDTLS_MPS_TRACE_INIT( "reader_free" );
mps_reader_zero( rd ); mps_reader_zero( rd );
RETURN( 0 ); MBEDTLS_MPS_TRACE_RETURN( 0 );
} }
int mbedtls_reader_feed( mbedtls_reader *rd, int mbedtls_reader_feed( mbedtls_reader *rd,
@ -112,11 +112,11 @@ int mbedtls_reader_feed( mbedtls_reader *rd,
{ {
unsigned char *acc; unsigned char *acc;
mbedtls_mps_size_t copy_to_acc; mbedtls_mps_size_t copy_to_acc;
TRACE_INIT( "reader_feed, frag %p, len %u", MBEDTLS_MPS_TRACE_INIT( "reader_feed, frag %p, len %u",
(void*) new_frag, (unsigned) new_frag_len ); (void*) new_frag, (unsigned) new_frag_len );
if( new_frag == NULL ) if( new_frag == NULL )
RETURN( MBEDTLS_ERR_MPS_READER_INVALID_ARG ); MBEDTLS_MPS_TRACE_RETURN( MBEDTLS_ERR_MPS_READER_INVALID_ARG );
MBEDTLS_MPS_STATE_VALIDATE_RAW( rd->frag == NULL, MBEDTLS_MPS_STATE_VALIDATE_RAW( rd->frag == NULL,
"mbedtls_reader_feed() requires reader to be in producing mode" ); "mbedtls_reader_feed() requires reader to be in producing mode" );
@ -138,7 +138,8 @@ int mbedtls_reader_feed( mbedtls_reader *rd,
if( copy_to_acc > 0 ) if( copy_to_acc > 0 )
memcpy( acc, new_frag, copy_to_acc ); memcpy( acc, new_frag, copy_to_acc );
TRACE( trace_comment, "Copy new data of size %u of %u into accumulator at offset %u", MBEDTLS_MPS_TRACE( mbedtls_mps_trace_comment,
"Copy new data of size %u of %u into accumulator at offset %u",
(unsigned) copy_to_acc, (unsigned) new_frag_len, (unsigned) aa ); (unsigned) copy_to_acc, (unsigned) new_frag_len, (unsigned) aa );
/* Check if, with the new fragment, we have enough data. */ /* Check if, with the new fragment, we have enough data. */
@ -149,10 +150,11 @@ int mbedtls_reader_feed( mbedtls_reader *rd,
aa += copy_to_acc; aa += copy_to_acc;
rd->acc_share.acc_remaining = ar; rd->acc_share.acc_remaining = ar;
rd->acc_avail = aa; rd->acc_avail = aa;
RETURN( MBEDTLS_ERR_MPS_READER_NEED_MORE ); MBEDTLS_MPS_TRACE_RETURN( MBEDTLS_ERR_MPS_READER_NEED_MORE );
} }
TRACE( trace_comment, "Enough data available to serve user request" ); MBEDTLS_MPS_TRACE( mbedtls_mps_trace_comment,
"Enough data available to serve user request" );
rd->acc_share.frag_offset = aa; rd->acc_share.frag_offset = aa;
aa += copy_to_acc; aa += copy_to_acc;
@ -167,7 +169,7 @@ int mbedtls_reader_feed( mbedtls_reader *rd,
rd->frag_len = new_frag_len; rd->frag_len = new_frag_len;
rd->commit = 0; rd->commit = 0;
rd->end = 0; rd->end = 0;
RETURN( 0 ); MBEDTLS_MPS_TRACE_RETURN( 0 );
} }
@ -178,7 +180,8 @@ int mbedtls_reader_get( mbedtls_reader *rd,
{ {
unsigned char *frag, *acc; unsigned char *frag, *acc;
mbedtls_mps_size_t end, fo, fl, frag_fetched, frag_remaining; mbedtls_mps_size_t end, fo, fl, frag_fetched, frag_remaining;
TRACE_INIT( "reader_get %p, desired %u", (void*) rd, (unsigned) desired ); MBEDTLS_MPS_TRACE_INIT( "reader_get %p, desired %u",
(void*) rd, (unsigned) desired );
frag = rd->frag; frag = rd->frag;
MBEDTLS_MPS_STATE_VALIDATE_RAW( frag != NULL, MBEDTLS_MPS_STATE_VALIDATE_RAW( frag != NULL,
@ -193,7 +196,8 @@ int mbedtls_reader_get( mbedtls_reader *rd,
else else
fo = rd->acc_share.frag_offset; fo = rd->acc_share.frag_offset;
TRACE( trace_comment, "frag_off %u, end %u, acc_avail %d", MBEDTLS_MPS_TRACE( mbedtls_mps_trace_comment,
"frag_off %u, end %u, acc_avail %d",
(unsigned) fo, (unsigned) rd->end, (unsigned) fo, (unsigned) rd->end,
acc == NULL ? -1 : (int) rd->acc_avail ); acc == NULL ? -1 : (int) rd->acc_avail );
@ -201,7 +205,8 @@ int mbedtls_reader_get( mbedtls_reader *rd,
end = rd->end; end = rd->end;
if( end < fo ) if( end < fo )
{ {
TRACE( trace_comment, "Serve the request from the accumulator" ); MBEDTLS_MPS_TRACE( mbedtls_mps_trace_comment,
"Serve the request from the accumulator" );
if( fo - end < desired ) if( fo - end < desired )
{ {
/* Illustration of supported and unsupported cases: /* Illustration of supported and unsupported cases:
@ -281,7 +286,8 @@ int mbedtls_reader_get( mbedtls_reader *rd,
* If we believe we adhere to this restricted usage throughout * If we believe we adhere to this restricted usage throughout
* the library, this check is a good opportunity to * the library, this check is a good opportunity to
* validate this. */ * validate this. */
RETURN( MBEDTLS_ERR_MPS_READER_INCONSISTENT_REQUESTS ); MBEDTLS_MPS_TRACE_RETURN(
MBEDTLS_ERR_MPS_READER_INCONSISTENT_REQUESTS );
} }
} }
@ -294,11 +300,12 @@ int mbedtls_reader_get( mbedtls_reader *rd,
rd->end = end; rd->end = end;
rd->pending = 0; rd->pending = 0;
RETURN( 0 ); MBEDTLS_MPS_TRACE_RETURN( 0 );
} }
/* Attempt to serve the request from the current fragment */ /* Attempt to serve the request from the current fragment */
TRACE( trace_comment, "Serve the request from the current fragment." ); MBEDTLS_MPS_TRACE( mbedtls_mps_trace_comment,
"Serve the request from the current fragment." );
fl = rd->frag_len; fl = rd->frag_len;
frag_fetched = end - fo; /* The amount of data from the current fragment frag_fetched = end - fo; /* The amount of data from the current fragment
@ -309,7 +316,9 @@ int mbedtls_reader_get( mbedtls_reader *rd,
/* Check if we can serve the read request from the fragment. */ /* Check if we can serve the read request from the fragment. */
if( frag_remaining < desired ) if( frag_remaining < desired )
{ {
TRACE( trace_comment, "There's not enough data in the current fragment to serve the request." ); MBEDTLS_MPS_TRACE( mbedtls_mps_trace_comment,
"There's not enough data in the current fragment "
"to serve the request." );
/* There's not enough data in the current fragment, /* There's not enough data in the current fragment,
* so either just RETURN what we have or fail. */ * so either just RETURN what we have or fail. */
if( buflen == NULL ) if( buflen == NULL )
@ -317,10 +326,11 @@ int mbedtls_reader_get( mbedtls_reader *rd,
if( frag_remaining > 0 ) if( frag_remaining > 0 )
{ {
rd->pending = desired - frag_remaining; rd->pending = desired - frag_remaining;
TRACE( trace_comment, "Remember to collect %u bytes before re-opening", MBEDTLS_MPS_TRACE( mbedtls_mps_trace_comment,
"Remember to collect %u bytes before re-opening",
(unsigned) rd->pending ); (unsigned) rd->pending );
} }
RETURN( MBEDTLS_ERR_MPS_READER_OUT_OF_DATA ); MBEDTLS_MPS_TRACE_RETURN( MBEDTLS_ERR_MPS_READER_OUT_OF_DATA );
} }
desired = frag_remaining; desired = frag_remaining;
@ -335,14 +345,14 @@ int mbedtls_reader_get( mbedtls_reader *rd,
end += desired; end += desired;
rd->end = end; rd->end = end;
rd->pending = 0; rd->pending = 0;
RETURN( 0 ); MBEDTLS_MPS_TRACE_RETURN( 0 );
} }
int mbedtls_reader_commit( mbedtls_reader *rd ) int mbedtls_reader_commit( mbedtls_reader *rd )
{ {
unsigned char *acc; unsigned char *acc;
mbedtls_mps_size_t aa, end, fo, shift; mbedtls_mps_size_t aa, end, fo, shift;
TRACE_INIT( "reader_commit" ); MBEDTLS_MPS_TRACE_INIT( "reader_commit" );
MBEDTLS_MPS_STATE_VALIDATE_RAW( rd->frag != NULL, MBEDTLS_MPS_STATE_VALIDATE_RAW( rd->frag != NULL,
"mbedtls_reader_commit() requires reader to be in consuming mode" ); "mbedtls_reader_commit() requires reader to be in consuming mode" );
@ -352,21 +362,24 @@ int mbedtls_reader_commit( mbedtls_reader *rd )
if( acc == NULL ) if( acc == NULL )
{ {
TRACE( trace_comment, "No accumulator, just shift end" ); MBEDTLS_MPS_TRACE( mbedtls_mps_trace_comment,
"No accumulator, just shift end" );
rd->commit = end; rd->commit = end;
RETURN( 0 ); MBEDTLS_MPS_TRACE_RETURN( 0 );
} }
fo = rd->acc_share.frag_offset; fo = rd->acc_share.frag_offset;
if( end >= fo ) if( end >= fo )
{ {
TRACE( trace_comment, "Started to serve fragment, get rid of accumulator" ); MBEDTLS_MPS_TRACE( mbedtls_mps_trace_comment,
"Started to serve fragment, get rid of accumulator" );
shift = fo; shift = fo;
aa = 0; aa = 0;
} }
else else
{ {
TRACE( trace_comment, "Still serving from accumulator" ); MBEDTLS_MPS_TRACE( mbedtls_mps_trace_comment,
"Still serving from accumulator" );
aa = rd->acc_avail; aa = rd->acc_avail;
shift = end; shift = end;
memmove( acc, acc + shift, aa - shift ); memmove( acc, acc + shift, aa - shift );
@ -381,9 +394,10 @@ int mbedtls_reader_commit( mbedtls_reader *rd )
rd->commit = end; rd->commit = end;
rd->end = end; rd->end = end;
TRACE( trace_comment, "Final state: (end=commit,fo,avail) = (%u,%u,%u)", MBEDTLS_MPS_TRACE( mbedtls_mps_trace_comment,
(unsigned) end, (unsigned) fo, (unsigned) aa ); "Final state: (end=commit,fo,avail) = (%u,%u,%u)",
RETURN( 0 ); (unsigned) end, (unsigned) fo, (unsigned) aa );
MBEDTLS_MPS_TRACE_RETURN( 0 );
} }
int mbedtls_reader_reclaim( mbedtls_reader *rd, int mbedtls_reader_reclaim( mbedtls_reader *rd,
@ -392,7 +406,7 @@ int mbedtls_reader_reclaim( mbedtls_reader *rd,
unsigned char *frag, *acc; unsigned char *frag, *acc;
mbedtls_mps_size_t pending, commit; mbedtls_mps_size_t pending, commit;
mbedtls_mps_size_t al, fo, fl; mbedtls_mps_size_t al, fo, fl;
TRACE_INIT( "reader_reclaim" ); MBEDTLS_MPS_TRACE_INIT( "reader_reclaim" );
if( paused != NULL ) if( paused != NULL )
*paused = 0; *paused = 0;
@ -413,27 +427,33 @@ int mbedtls_reader_reclaim( mbedtls_reader *rd,
if( pending == 0 ) if( pending == 0 )
{ {
TRACE( trace_comment, "No unsatisfied read-request has been logged." ); MBEDTLS_MPS_TRACE( mbedtls_mps_trace_comment,
"No unsatisfied read-request has been logged." );
/* Check if there's data left to be consumed. */ /* Check if there's data left to be consumed. */
if( commit < fo || commit - fo < fl ) if( commit < fo || commit - fo < fl )
{ {
TRACE( trace_comment, "There is data left to be consumed." ); MBEDTLS_MPS_TRACE( mbedtls_mps_trace_comment,
"There is data left to be consumed." );
rd->end = commit; rd->end = commit;
RETURN( MBEDTLS_ERR_MPS_READER_DATA_LEFT ); MBEDTLS_MPS_TRACE_RETURN( MBEDTLS_ERR_MPS_READER_DATA_LEFT );
} }
TRACE( trace_comment, "The fragment has been completely processed and committed." ); MBEDTLS_MPS_TRACE( mbedtls_mps_trace_comment,
"The fragment has been completely processed and committed." );
} }
else else
{ {
mbedtls_mps_size_t frag_backup_offset; mbedtls_mps_size_t frag_backup_offset;
mbedtls_mps_size_t frag_backup_len; mbedtls_mps_size_t frag_backup_len;
TRACE( trace_comment, "There has been an unsatisfied read-request with %u bytes overhead.", MBEDTLS_MPS_TRACE( mbedtls_mps_trace_comment,
(unsigned) pending ); "There has been an unsatisfied read-request with %u bytes overhead.",
(unsigned) pending );
if( acc == NULL ) if( acc == NULL )
{ {
TRACE( trace_comment, "No accumulator present" ); MBEDTLS_MPS_TRACE( mbedtls_mps_trace_comment,
RETURN( MBEDTLS_ERR_MPS_READER_NEED_ACCUMULATOR ); "No accumulator present" );
MBEDTLS_MPS_TRACE_RETURN(
MBEDTLS_ERR_MPS_READER_NEED_ACCUMULATOR );
} }
al = rd->acc_len; al = rd->acc_len;
@ -443,7 +463,8 @@ int mbedtls_reader_reclaim( mbedtls_reader *rd,
{ {
/* No, accumulator is still being processed. */ /* No, accumulator is still being processed. */
int overflow; int overflow;
TRACE( trace_comment, "Still processing data from the accumulator" ); MBEDTLS_MPS_TRACE( mbedtls_mps_trace_comment,
"Still processing data from the accumulator" );
overflow = overflow =
( fo + fl < fo ) || ( fo + fl + pending < fo + fl ); ( fo + fl < fo ) || ( fo + fl + pending < fo + fl );
@ -451,12 +472,16 @@ int mbedtls_reader_reclaim( mbedtls_reader *rd,
{ {
rd->end = commit; rd->end = commit;
rd->pending = 0; rd->pending = 0;
TRACE( trace_error, "The accumulator is too small to handle the backup." ); MBEDTLS_MPS_TRACE( mbedtls_mps_trace_error,
TRACE( trace_error, "* Remaining size: %u", (unsigned) al ); "The accumulator is too small to handle the backup." );
TRACE( trace_error, "* Needed: %u (%u + %u + %u)", MBEDTLS_MPS_TRACE( mbedtls_mps_trace_error,
(unsigned) ( fo + fl + pending ), "* Remaining size: %u", (unsigned) al );
(unsigned) fo, (unsigned) fl, (unsigned) pending ); MBEDTLS_MPS_TRACE( mbedtls_mps_trace_error,
RETURN( MBEDTLS_ERR_MPS_READER_ACCUMULATOR_TOO_SMALL ); "* Needed: %u (%u + %u + %u)",
(unsigned) ( fo + fl + pending ),
(unsigned) fo, (unsigned) fl, (unsigned) pending );
MBEDTLS_MPS_TRACE_RETURN(
MBEDTLS_ERR_MPS_READER_ACCUMULATOR_TOO_SMALL );
} }
frag_backup_offset = 0; frag_backup_offset = 0;
frag_backup_len = fl; frag_backup_len = fl;
@ -465,7 +490,8 @@ int mbedtls_reader_reclaim( mbedtls_reader *rd,
{ {
/* Yes, the accumulator is already processed. */ /* Yes, the accumulator is already processed. */
int overflow; int overflow;
TRACE( trace_comment, "The accumulator has already been processed" ); MBEDTLS_MPS_TRACE( mbedtls_mps_trace_comment,
"The accumulator has already been processed" );
frag_backup_offset = commit; frag_backup_offset = commit;
frag_backup_len = fl - commit; frag_backup_len = fl - commit;
@ -476,12 +502,17 @@ int mbedtls_reader_reclaim( mbedtls_reader *rd,
{ {
rd->end = commit; rd->end = commit;
rd->pending = 0; rd->pending = 0;
TRACE( trace_error, "The accumulator is too small to handle the backup." );
TRACE( trace_error, "* Remaining size: %u", (unsigned) ( al - fo ) ); MBEDTLS_MPS_TRACE( mbedtls_mps_trace_error,
TRACE( trace_error, "* Needed: %u (%u + %u)", "The accumulator is too small to handle the backup." );
(unsigned) ( frag_backup_len + pending ), MBEDTLS_MPS_TRACE( mbedtls_mps_trace_error,
(unsigned) frag_backup_len, (unsigned) pending ); "* Remaining size: %u", (unsigned) ( al - fo ) );
RETURN( MBEDTLS_ERR_MPS_READER_ACCUMULATOR_TOO_SMALL ); MBEDTLS_MPS_TRACE( mbedtls_mps_trace_error,
"* Needed: %u (%u + %u)",
(unsigned) ( frag_backup_len + pending ),
(unsigned) frag_backup_len, (unsigned) pending );
MBEDTLS_MPS_TRACE_RETURN(
MBEDTLS_ERR_MPS_READER_ACCUMULATOR_TOO_SMALL );
} }
} }
@ -489,8 +520,9 @@ int mbedtls_reader_reclaim( mbedtls_reader *rd,
acc += fo; acc += fo;
memcpy( acc, frag, frag_backup_len ); memcpy( acc, frag, frag_backup_len );
TRACE( trace_comment, "Backup %u bytes into accumulator", MBEDTLS_MPS_TRACE( mbedtls_mps_trace_comment,
(unsigned) frag_backup_len ); "Backup %u bytes into accumulator",
(unsigned) frag_backup_len );
rd->acc_avail = fo + frag_backup_len; rd->acc_avail = fo + frag_backup_len;
rd->acc_share.acc_remaining = pending; rd->acc_share.acc_remaining = pending;
@ -506,8 +538,10 @@ int mbedtls_reader_reclaim( mbedtls_reader *rd,
rd->end = 0; rd->end = 0;
rd->pending = 0; rd->pending = 0;
TRACE( trace_comment, "Final state: aa %u, al %u, ar %u", MBEDTLS_MPS_TRACE( mbedtls_mps_trace_comment,
(unsigned) rd->acc_avail, (unsigned) rd->acc_len, "Final state: aa %u, al %u, ar %u",
(unsigned) rd->acc_share.acc_remaining ); (unsigned) rd->acc_avail, (unsigned) rd->acc_len,
RETURN( 0 ); (unsigned) rd->acc_share.acc_remaining );
MBEDTLS_MPS_TRACE_RETURN( 0 );
} }

View file

@ -257,7 +257,7 @@ int mbedtls_reader_free( mbedtls_reader *reader );
* moved to consuming state, and ownership of \p buf * moved to consuming state, and ownership of \p buf
* will be passed to the reader until mbedtls_reader_reclaim() * will be passed to the reader until mbedtls_reader_reclaim()
* is called. * is called.
* \return \c MBEDTLS_ERR_READER_NEED_MORE if more input data is * \return \c MBEDTLS_ERR_MPS_READER_NEED_MORE if more input data is
* required to fulfill a previous request to mbedtls_reader_get(). * required to fulfill a previous request to mbedtls_reader_get().
* In this case, the reader remains in producing state and * In this case, the reader remains in producing state and
* takes no ownership of the provided buffer (an internal copy * takes no ownership of the provided buffer (an internal copy
@ -308,7 +308,7 @@ int mbedtls_reader_reclaim( mbedtls_reader *reader,
* (if \c buflen == \c NULL). The user hass ownership * (if \c buflen == \c NULL). The user hass ownership
* of the buffer until the next call to mbedtls_reader_commit(). * of the buffer until the next call to mbedtls_reader_commit().
* or mbedtls_reader_reclaim(). * or mbedtls_reader_reclaim().
* \return #MBEDTLS_ERR_READER_OUT_OF_DATA if there is not enough * \return #MBEDTLS_ERR_MPS_READER_OUT_OF_DATA if there is not enough
* data available to serve the read request. In this case, * data available to serve the read request. In this case,
* the reader remains intact, and additional data can be * the reader remains intact, and additional data can be
* provided by reclaiming the current input buffer via * provided by reclaiming the current input buffer via

View file

@ -21,7 +21,7 @@
#include "mps_common.h" #include "mps_common.h"
#if defined(MBEDTLS_MPS_TRACE) #if defined(MBEDTLS_MPS_ENABLE_TRACE)
#include "mps_trace.h" #include "mps_trace.h"
#include <stdarg.h> #include <stdarg.h>
@ -50,7 +50,7 @@ static char const * colors[] =
#define MPS_TRACE_BUF_SIZE 100 #define MPS_TRACE_BUF_SIZE 100
void trace_print_msg( int id, int line, const char *format, ... ) void mbedtls_mps_trace_print_msg( int id, int line, const char *format, ... )
{ {
int ret; int ret;
char str[MPS_TRACE_BUF_SIZE]; char str[MPS_TRACE_BUF_SIZE];
@ -66,27 +66,27 @@ void trace_print_msg( int id, int line, const char *format, ... )
} }
} }
int trace_get_depth() int mbedtls_mps_trace_get_depth()
{ {
return trace_depth_; return trace_depth_;
} }
void trace_dec_depth() void mbedtls_mps_trace_dec_depth()
{ {
trace_depth_--; trace_depth_--;
} }
void trace_inc_depth() void mbedtls_mps_trace_inc_depth()
{ {
trace_depth_++; trace_depth_++;
} }
void trace_color( int id ) void mbedtls_mps_trace_color( int id )
{ {
if( id > (int) ( sizeof( colors ) / sizeof( *colors ) ) ) if( id > (int) ( sizeof( colors ) / sizeof( *colors ) ) )
return; return;
printf( "%s", colors[ id ] ); printf( "%s", colors[ id ] );
} }
void trace_indent( int level, trace_type ty ) void mbedtls_mps_trace_indent( int level, mbedtls_mps_trace_type ty )
{ {
if( level > 0 ) if( level > 0 )
{ {
@ -98,19 +98,19 @@ void trace_indent( int level, trace_type ty )
switch( ty ) switch( ty )
{ {
case trace_comment: case mbedtls_mps_trace_comment:
mbedtls_printf( "@ " ); mbedtls_printf( "@ " );
break; break;
case trace_call: case mbedtls_mps_trace_call:
mbedtls_printf( "+--> " ); mbedtls_printf( "+--> " );
break; break;
case trace_error: case mbedtls_mps_trace_error:
mbedtls_printf( "E " ); mbedtls_printf( "E " );
break; break;
case trace_return: case mbedtls_mps_trace_return:
mbedtls_printf( "< " ); mbedtls_printf( "< " );
break; break;
@ -119,4 +119,4 @@ void trace_indent( int level, trace_type ty )
} }
} }
#endif /* MBEDTLS_MPS_TRACE */ #endif /* MBEDTLS_MPS_ENABLE_TRACE */

View file

@ -23,8 +23,8 @@
* \brief Tracing module for MPS * \brief Tracing module for MPS
*/ */
#ifndef MBEDTLS_MPS_TRACE_H #ifndef MBEDTLS_MPS_MBEDTLS_MPS_TRACE_H
#define MBEDTLS_MPS_TRACE_H #define MBEDTLS_MPS_MBEDTLS_MPS_TRACE_H
#include "common.h" #include "common.h"
#include "mps_common.h" #include "mps_common.h"
@ -38,138 +38,138 @@
#define mbedtls_vsnprintf vsnprintf #define mbedtls_vsnprintf vsnprintf
#endif /* MBEDTLS_PLATFORM_C */ #endif /* MBEDTLS_PLATFORM_C */
#if defined(MBEDTLS_MPS_TRACE) #if defined(MBEDTLS_MPS_ENABLE_TRACE)
/* /*
* Adapt this to enable/disable tracing output * Adapt this to enable/disable tracing output
* from the various layers of the MPS. * from the various layers of the MPS.
*/ */
#define TRACE_ENABLE_LAYER_1 #define MBEDTLS_MPS_TRACE_ENABLE_LAYER_1
#define TRACE_ENABLE_LAYER_2 #define MBEDTLS_MPS_TRACE_ENABLE_LAYER_2
#define TRACE_ENABLE_LAYER_3 #define MBEDTLS_MPS_TRACE_ENABLE_LAYER_3
#define TRACE_ENABLE_LAYER_4 #define MBEDTLS_MPS_TRACE_ENABLE_LAYER_4
#define TRACE_ENABLE_READER #define MBEDTLS_MPS_TRACE_ENABLE_READER
#define TRACE_ENABLE_WRITER #define MBEDTLS_MPS_TRACE_ENABLE_WRITER
/* /*
* To use the existing trace module, only change * To use the existing trace module, only change
* TRACE_ENABLE_XXX above, but don't modify the * MBEDTLS_MPS_TRACE_ENABLE_XXX above, but don't modify the
* rest of this file. * rest of this file.
*/ */
typedef enum typedef enum
{ {
trace_comment, mbedtls_mps_trace_comment,
trace_call, mbedtls_mps_trace_call,
trace_error, mbedtls_mps_trace_error,
trace_return mbedtls_mps_trace_return
} trace_type; } mbedtls_mps_trace_type;
#define TRACE_BIT_LAYER_1 1 #define MBEDTLS_MPS_TRACE_BIT_LAYER_1 1
#define TRACE_BIT_LAYER_2 2 #define MBEDTLS_MPS_TRACE_BIT_LAYER_2 2
#define TRACE_BIT_LAYER_3 3 #define MBEDTLS_MPS_TRACE_BIT_LAYER_3 3
#define TRACE_BIT_LAYER_4 4 #define MBEDTLS_MPS_TRACE_BIT_LAYER_4 4
#define TRACE_BIT_WRITER 5 #define MBEDTLS_MPS_TRACE_BIT_WRITER 5
#define TRACE_BIT_READER 6 #define MBEDTLS_MPS_TRACE_BIT_READER 6
#if defined(TRACE_ENABLE_LAYER_1) #if defined(MBEDTLS_MPS_TRACE_ENABLE_LAYER_1)
#define TRACE_MASK_LAYER_1 (1u << TRACE_BIT_LAYER_1 ) #define MBEDTLS_MPS_TRACE_MASK_LAYER_1 (1u << MBEDTLS_MPS_TRACE_BIT_LAYER_1 )
#else #else
#define TRACE_MASK_LAYER_1 0 #define MBEDTLS_MPS_TRACE_MASK_LAYER_1 0
#endif #endif
#if defined(TRACE_ENABLE_LAYER_2) #if defined(MBEDTLS_MPS_TRACE_ENABLE_LAYER_2)
#define TRACE_MASK_LAYER_2 (1u << TRACE_BIT_LAYER_2 ) #define MBEDTLS_MPS_TRACE_MASK_LAYER_2 (1u << MBEDTLS_MPS_TRACE_BIT_LAYER_2 )
#else #else
#define TRACE_MASK_LAYER_2 0 #define MBEDTLS_MPS_TRACE_MASK_LAYER_2 0
#endif #endif
#if defined(TRACE_ENABLE_LAYER_3) #if defined(MBEDTLS_MPS_TRACE_ENABLE_LAYER_3)
#define TRACE_MASK_LAYER_3 (1u << TRACE_BIT_LAYER_3 ) #define MBEDTLS_MPS_TRACE_MASK_LAYER_3 (1u << MBEDTLS_MPS_TRACE_BIT_LAYER_3 )
#else #else
#define TRACE_MASK_LAYER_3 0 #define MBEDTLS_MPS_TRACE_MASK_LAYER_3 0
#endif #endif
#if defined(TRACE_ENABLE_LAYER_4) #if defined(MBEDTLS_MPS_TRACE_ENABLE_LAYER_4)
#define TRACE_MASK_LAYER_4 (1u << TRACE_BIT_LAYER_4 ) #define MBEDTLS_MPS_TRACE_MASK_LAYER_4 (1u << MBEDTLS_MPS_TRACE_BIT_LAYER_4 )
#else #else
#define TRACE_MASK_LAYER_4 0 #define MBEDTLS_MPS_TRACE_MASK_LAYER_4 0
#endif #endif
#if defined(TRACE_ENABLE_READER) #if defined(MBEDTLS_MPS_TRACE_ENABLE_READER)
#define TRACE_MASK_READER (1u << TRACE_BIT_READER ) #define MBEDTLS_MPS_TRACE_MASK_READER (1u << MBEDTLS_MPS_TRACE_BIT_READER )
#else #else
#define TRACE_MASK_READER 0 #define MBEDTLS_MPS_TRACE_MASK_READER 0
#endif #endif
#if defined(TRACE_ENABLE_WRITER) #if defined(MBEDTLS_MPS_TRACE_ENABLE_WRITER)
#define TRACE_MASK_WRITER (1u << TRACE_BIT_WRITER ) #define MBEDTLS_MPS_TRACE_MASK_WRITER (1u << MBEDTLS_MPS_TRACE_BIT_WRITER )
#else #else
#define TRACE_MASK_WRITER 0 #define MBEDTLS_MPS_TRACE_MASK_WRITER 0
#endif #endif
#define TRACE_MASK ( TRACE_MASK_LAYER_1 | \ #define MBEDTLS_MPS_TRACE_MASK ( MBEDTLS_MPS_TRACE_MASK_LAYER_1 | \
TRACE_MASK_LAYER_2 | \ MBEDTLS_MPS_TRACE_MASK_LAYER_2 | \
TRACE_MASK_LAYER_3 | \ MBEDTLS_MPS_TRACE_MASK_LAYER_3 | \
TRACE_MASK_LAYER_4 | \ MBEDTLS_MPS_TRACE_MASK_LAYER_4 | \
TRACE_MASK_READER | \ MBEDTLS_MPS_TRACE_MASK_READER | \
TRACE_MASK_WRITER ) MBEDTLS_MPS_TRACE_MASK_WRITER )
/* We have to avoid globals because E-ACSL chokes on them... /* We have to avoid globals because E-ACSL chokes on them...
* Wrap everything in stub functions. */ * Wrap everything in stub functions. */
int trace_get_depth( void ); int mbedtls_mps_trace_get_depth( void );
void trace_inc_depth( void ); void mbedtls_mps_trace_inc_depth( void );
void trace_dec_depth( void ); void mbedtls_mps_trace_dec_depth( void );
void trace_color( int id ); void mbedtls_mps_trace_color( int id );
void trace_indent( int level, trace_type ty ); void mbedtls_mps_trace_indent( int level, mbedtls_mps_trace_type ty );
void trace_print_msg( int id, int line, const char *format, ... ); void mbedtls_mps_trace_print_msg( int id, int line, const char *format, ... );
#define TRACE( type, ... ) \ #define MBEDTLS_MPS_TRACE( type, ... ) \
do { \ do { \
if( ! ( TRACE_MASK & ( 1u << trace_id ) ) ) \ if( ! ( MBEDTLS_MPS_TRACE_MASK & ( 1u << mbedtls_mps_trace_id ) ) ) \
break; \ break; \
trace_indent( trace_get_depth(), type ); \ mbedtls_mps_trace_indent( mbedtls_mps_trace_get_depth(), type ); \
trace_color( trace_id ); \ mbedtls_mps_trace_color( mbedtls_mps_trace_id ); \
trace_print_msg( trace_id, __LINE__, __VA_ARGS__ ); \ mbedtls_mps_trace_print_msg( mbedtls_mps_trace_id, __LINE__, __VA_ARGS__ ); \
trace_color( 0 ); \ mbedtls_mps_trace_color( 0 ); \
} while( 0 ) } while( 0 )
#define TRACE_INIT( ... ) \ #define MBEDTLS_MPS_TRACE_INIT( ... ) \
do { \ do { \
if( ! ( TRACE_MASK & ( 1u << trace_id ) ) ) \ if( ! ( MBEDTLS_MPS_TRACE_MASK & ( 1u << mbedtls_mps_trace_id ) ) ) \
break; \ break; \
TRACE( trace_call, __VA_ARGS__ ); \ MBEDTLS_MPS_TRACE( mbedtls_mps_trace_call, __VA_ARGS__ ); \
trace_inc_depth(); \ mbedtls_mps_trace_inc_depth(); \
} while( 0 ) } while( 0 )
#define TRACE_END( val ) \ #define MBEDTLS_MPS_TRACE_END( val ) \
do { \ do { \
if( ! ( TRACE_MASK & ( 1u << trace_id ) ) ) \ if( ! ( MBEDTLS_MPS_TRACE_MASK & ( 1u << mbedtls_mps_trace_id ) ) ) \
break; \ break; \
TRACE( trace_return, "%d (-%#04x)", \ MBEDTLS_MPS_TRACE( mbedtls_mps_trace_return, "%d (-%#04x)", \
(int) (val), -((unsigned)(val)) ); \ (int) (val), -((unsigned)(val)) ); \
trace_dec_depth(); \ mbedtls_mps_trace_dec_depth(); \
} while( 0 ) } while( 0 )
#define RETURN( val ) \ #define MBEDTLS_MPS_TRACE_RETURN( val ) \
do { \ do { \
/* Breaks tail recursion. */ \ /* Breaks tail recursion. */ \
int ret__ = val; \ int ret__ = val; \
TRACE_END( ret__ ); \ MBEDTLS_MPS_TRACE_END( ret__ ); \
return( ret__ ); \ return( ret__ ); \
} while( 0 ) } while( 0 )
#else /* MBEDTLS_MPS_TRACE */ #else /* MBEDTLS_MPS_TRACE */
#define TRACE( type, ... ) do { } while( 0 ) #define MBEDTLS_MPS_TRACE( type, ... ) do { } while( 0 )
#define TRACE_INIT( ... ) do { } while( 0 ) #define MBEDTLS_MPS_TRACE_INIT( ... ) do { } while( 0 )
#define TRACE_END do { } while( 0 ) #define MBEDTLS_MPS_TRACE_END do { } while( 0 )
#define RETURN( val ) return( val ); #define MBEDTLS_MPS_TRACE_RETURN( val ) return( val );
#endif /* MBEDTLS_MPS_TRACE */ #endif /* MBEDTLS_MPS_TRACE */
#endif /* MBEDTLS_MPS_TRACE_H */ #endif /* MBEDTLS_MPS_MBEDTLS_MPS_TRACE_H */