Move MPS reader to mbedtls_mps_ namespace

Signed-off-by: Hanno Becker <hanno.becker@arm.com>
This commit is contained in:
Hanno Becker 2021-01-28 09:45:47 +00:00
parent 984fbded58
commit 8899396fd1
4 changed files with 271 additions and 271 deletions

View file

@ -81,7 +81,7 @@
/*! An invalid argument was passed to the reader. */ /*! An invalid argument was passed to the reader. */
#define MBEDTLS_ERR_MPS_READER_INVALID_ARG MBEDTLS_MPS_READER_MAKE_ERROR( 0x2 ) #define MBEDTLS_ERR_MPS_READER_INVALID_ARG MBEDTLS_MPS_READER_MAKE_ERROR( 0x2 )
/*! An attempt to move a reader to consuming mode through mbedtls_reader_feed() /*! An attempt to move a reader to consuming mode through mbedtls_mps_reader_feed()
* after pausing failed because the provided data is not sufficient to serve the * after pausing failed because the provided data is not sufficient to serve the
* the read requests that lead to the pausing. */ * the read requests that lead to the pausing. */
#define MBEDTLS_ERR_MPS_READER_NEED_MORE MBEDTLS_MPS_READER_MAKE_ERROR( 0x3 ) #define MBEDTLS_ERR_MPS_READER_NEED_MORE MBEDTLS_MPS_READER_MAKE_ERROR( 0x3 )

View file

@ -66,7 +66,7 @@ static int mbedtls_mps_trace_id = MBEDTLS_MPS_TRACE_BIT_READER;
* *
*/ */
static inline void mps_reader_zero( mbedtls_reader *rd ) static inline void mps_reader_zero( mbedtls_mps_reader *rd )
{ {
/* A plain memset() would likely be more efficient, /* A plain memset() would likely be more efficient,
* but the current way of zeroing makes it harder * but the current way of zeroing makes it harder
@ -74,7 +74,7 @@ static inline void mps_reader_zero( mbedtls_reader *rd )
* It's also more suitable for VF efforts since it * It's also more suitable for VF efforts since it
* doesn't require reasoning about structs being * doesn't require reasoning about structs being
* interpreted as unstructured binary blobs. */ * interpreted as unstructured binary blobs. */
static mbedtls_reader const zero = static mbedtls_mps_reader const zero =
{ .frag = NULL, { .frag = NULL,
.frag_len = 0, .frag_len = 0,
.commit = 0, .commit = 0,
@ -88,7 +88,7 @@ static inline void mps_reader_zero( mbedtls_reader *rd )
*rd = zero; *rd = zero;
} }
int mbedtls_reader_init( mbedtls_reader *rd, int mbedtls_mps_reader_init( mbedtls_mps_reader *rd,
unsigned char *acc, unsigned char *acc,
mbedtls_mps_size_t acc_len ) mbedtls_mps_size_t acc_len )
{ {
@ -99,14 +99,14 @@ int mbedtls_reader_init( mbedtls_reader *rd,
MBEDTLS_MPS_TRACE_RETURN( 0 ); MBEDTLS_MPS_TRACE_RETURN( 0 );
} }
int mbedtls_reader_free( mbedtls_reader *rd ) int mbedtls_mps_reader_free( mbedtls_mps_reader *rd )
{ {
MBEDTLS_MPS_TRACE_INIT( "reader_free" ); MBEDTLS_MPS_TRACE_INIT( "reader_free" );
mps_reader_zero( rd ); mps_reader_zero( rd );
MBEDTLS_MPS_TRACE_RETURN( 0 ); MBEDTLS_MPS_TRACE_RETURN( 0 );
} }
int mbedtls_reader_feed( mbedtls_reader *rd, int mbedtls_mps_reader_feed( mbedtls_mps_reader *rd,
unsigned char *new_frag, unsigned char *new_frag,
mbedtls_mps_size_t new_frag_len ) mbedtls_mps_size_t new_frag_len )
{ {
@ -119,7 +119,7 @@ int mbedtls_reader_feed( mbedtls_reader *rd,
MBEDTLS_MPS_TRACE_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_mps_reader_feed() requires reader to be in producing mode" );
acc = rd->acc; acc = rd->acc;
if( acc != NULL ) if( acc != NULL )
@ -173,7 +173,7 @@ int mbedtls_reader_feed( mbedtls_reader *rd,
} }
int mbedtls_reader_get( mbedtls_reader *rd, int mbedtls_mps_reader_get( mbedtls_mps_reader *rd,
mbedtls_mps_size_t desired, mbedtls_mps_size_t desired,
unsigned char **buffer, unsigned char **buffer,
mbedtls_mps_size_t *buflen ) mbedtls_mps_size_t *buflen )
@ -185,7 +185,7 @@ int mbedtls_reader_get( mbedtls_reader *rd,
frag = rd->frag; frag = rd->frag;
MBEDTLS_MPS_STATE_VALIDATE_RAW( frag != NULL, MBEDTLS_MPS_STATE_VALIDATE_RAW( frag != NULL,
"mbedtls_reader_get() requires reader to be in consuming mode" ); "mbedtls_mps_reader_get() requires reader to be in consuming mode" );
/* The fragment offset indicates the offset of the fragment /* The fragment offset indicates the offset of the fragment
* from the accmulator, if the latter is present. Use a offset * from the accmulator, if the latter is present. Use a offset
@ -269,7 +269,7 @@ int mbedtls_reader_get( mbedtls_reader *rd,
* fo/frag_offset aa/acc_avail * fo/frag_offset aa/acc_avail
* *
* In case of Allowed #1 and #2 we're switching to serve from * In case of Allowed #1 and #2 we're switching to serve from
* `frag` starting from the next call to mbedtls_reader_get(). * `frag` starting from the next call to mbedtls_mps_reader_get().
*/ */
mbedtls_mps_size_t aa; mbedtls_mps_size_t aa;
@ -348,14 +348,14 @@ int mbedtls_reader_get( mbedtls_reader *rd,
MBEDTLS_MPS_TRACE_RETURN( 0 ); MBEDTLS_MPS_TRACE_RETURN( 0 );
} }
int mbedtls_reader_commit( mbedtls_reader *rd ) int mbedtls_mps_reader_commit( mbedtls_mps_reader *rd )
{ {
unsigned char *acc; unsigned char *acc;
mbedtls_mps_size_t aa, end, fo, shift; mbedtls_mps_size_t aa, end, fo, shift;
MBEDTLS_MPS_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_mps_reader_commit() requires reader to be in consuming mode" );
acc = rd->acc; acc = rd->acc;
end = rd->end; end = rd->end;
@ -400,7 +400,7 @@ int mbedtls_reader_commit( mbedtls_reader *rd )
MBEDTLS_MPS_TRACE_RETURN( 0 ); MBEDTLS_MPS_TRACE_RETURN( 0 );
} }
int mbedtls_reader_reclaim( mbedtls_reader *rd, int mbedtls_mps_reader_reclaim( mbedtls_mps_reader *rd,
mbedtls_mps_size_t *paused ) mbedtls_mps_size_t *paused )
{ {
unsigned char *frag, *acc; unsigned char *frag, *acc;
@ -413,7 +413,7 @@ int mbedtls_reader_reclaim( mbedtls_reader *rd,
frag = rd->frag; frag = rd->frag;
MBEDTLS_MPS_STATE_VALIDATE_RAW( frag != NULL, MBEDTLS_MPS_STATE_VALIDATE_RAW( frag != NULL,
"mbedtls_reader_reclaim() requires reader to be in consuming mode" ); "mbedtls_mps_reader_reclaim() requires reader to be in consuming mode" );
acc = rd->acc; acc = rd->acc;
pending = rd->pending; pending = rd->pending;

View file

@ -57,9 +57,9 @@
* From the perspective of the consumer, the state of the * From the perspective of the consumer, the state of the
* reader is a potentially empty list of input buffers that * reader is a potentially empty list of input buffers that
* the reader has provided to the consumer. * the reader has provided to the consumer.
* New buffers can be requested through calls to mbedtls_reader_get(), * New buffers can be requested through calls to mbedtls_mps_reader_get(),
* while previously obtained input buffers can be marked processed * while previously obtained input buffers can be marked processed
* through calls to mbedtls_reader_consume(), emptying the list of * through calls to mbedtls_mps_reader_consume(), emptying the list of
* input buffers and invalidating them from the consumer's perspective. * input buffers and invalidating them from the consumer's perspective.
* The consumer need not be aware of the distinction between consumer * The consumer need not be aware of the distinction between consumer
* and producer mode, because he only interfaces with the reader * and producer mode, because he only interfaces with the reader
@ -82,9 +82,9 @@
* while the Attached state belongs to consuming mode. * while the Attached state belongs to consuming mode.
* *
* Transitioning from Unset or Accumulating to Attached is * Transitioning from Unset or Accumulating to Attached is
* done via calls to mbedtls_reader_feed(), while transitioning * done via calls to mbedtls_mps_reader_feed(), while transitioning
* from Consuming to either Unset or Accumulating (depending * from Consuming to either Unset or Accumulating (depending
* on what has been processed) is done via mbedtls_reader_reclaim(). * on what has been processed) is done via mbedtls_mps_reader_reclaim().
* *
* The following diagram depicts the producer-state progression: * The following diagram depicts the producer-state progression:
* *
@ -119,18 +119,18 @@
#include "mps_common.h" #include "mps_common.h"
#include "mps_error.h" #include "mps_error.h"
struct mbedtls_reader; struct mbedtls_mps_reader;
typedef struct mbedtls_reader mbedtls_reader; typedef struct mbedtls_mps_reader mbedtls_mps_reader;
/* /*
* Structure definitions * Structure definitions
*/ */
struct mbedtls_reader struct mbedtls_mps_reader
{ {
unsigned char *frag; /*!< The fragment of incoming data managed by unsigned char *frag; /*!< The fragment of incoming data managed by
* the reader; it is provided to the reader * the reader; it is provided to the reader
* through mbedtls_reader_feed(). The reader * through mbedtls_mps_reader_feed(). The reader
* does not own the fragment and does not * does not own the fragment and does not
* perform any allocation operations on it, * perform any allocation operations on it,
* but does have read and write access to it. */ * but does have read and write access to it. */
@ -146,18 +146,18 @@ struct mbedtls_reader
mbedtls_mps_stored_size_t end; mbedtls_mps_stored_size_t end;
/*!< The offset of the end of the last chunk /*!< The offset of the end of the last chunk
* passed to the user through a call to * passed to the user through a call to
* mbedtls_reader_get(), relative to the first * mbedtls_mps_reader_get(), relative to the first
* byte in the accumulator. * byte in the accumulator.
* This is only used when the reader is in * This is only used when the reader is in
* consuming mode, i.e. \c frag != \c NULL; * consuming mode, i.e. \c frag != \c NULL;
* otherwise, its value is \c 0. */ * otherwise, its value is \c 0. */
mbedtls_mps_stored_size_t pending; mbedtls_mps_stored_size_t pending;
/*!< The amount of incoming data missing on the /*!< The amount of incoming data missing on the
* last call to mbedtls_reader_get(). * last call to mbedtls_mps_reader_get().
* In particular, it is \c 0 if the last call * In particular, it is \c 0 if the last call
* was successful. * was successful.
* If a reader is reclaimed after an * If a reader is reclaimed after an
* unsuccessful call to mbedtls_reader_get(), * unsuccessful call to mbedtls_mps_reader_get(),
* this variable is used to have the reader * this variable is used to have the reader
* remember how much data should be accumulated * remember how much data should be accumulated
* before the reader can be passed back to * before the reader can be passed back to
@ -171,7 +171,7 @@ struct mbedtls_reader
* separate struct and using a pointer here. */ * separate struct and using a pointer here. */
unsigned char *acc; /*!< The accumulator is used to gather incoming unsigned char *acc; /*!< The accumulator is used to gather incoming
* data if a read-request via mbedtls_reader_get() * data if a read-request via mbedtls_mps_reader_get()
* cannot be served from the current fragment. */ * cannot be served from the current fragment. */
mbedtls_mps_stored_size_t acc_len; mbedtls_mps_stored_size_t acc_len;
/*!< The total size of the accumulator. */ /*!< The total size of the accumulator. */
@ -218,8 +218,8 @@ struct mbedtls_reader
* *
* \param reader The reader to be initialized. * \param reader The reader to be initialized.
* \param acc The buffer to be used as a temporary accumulator * \param acc The buffer to be used as a temporary accumulator
* in case read requests through mbedtls_reader_get() * in case read requests through mbedtls_mps_reader_get()
* exceed the buffer provided by mbedtls_reader_feed(). * exceed the buffer provided by mbedtls_mps_reader_feed().
* This buffer is owned by the caller and exclusive use * This buffer is owned by the caller and exclusive use
* for reading and writing is given to the reade for the * for reading and writing is given to the reade for the
* duration of the reader's lifetime. It is thus the caller's * duration of the reader's lifetime. It is thus the caller's
@ -231,7 +231,7 @@ struct mbedtls_reader
* \return \c 0 on success. * \return \c 0 on success.
* \return A negative \c MBEDTLS_ERR_READER_XXX error code on failure. * \return A negative \c MBEDTLS_ERR_READER_XXX error code on failure.
*/ */
int mbedtls_reader_init( mbedtls_reader *reader, int mbedtls_mps_reader_init( mbedtls_mps_reader *reader,
unsigned char *acc, unsigned char *acc,
mbedtls_mps_size_t acc_len ); mbedtls_mps_size_t acc_len );
@ -243,7 +243,7 @@ int mbedtls_reader_init( mbedtls_reader *reader,
* \return \c 0 on success. * \return \c 0 on success.
* \return A negative \c MBEDTLS_ERR_READER_XXX error code on failure. * \return A negative \c MBEDTLS_ERR_READER_XXX error code on failure.
*/ */
int mbedtls_reader_free( mbedtls_reader *reader ); int mbedtls_mps_reader_free( mbedtls_mps_reader *reader );
/** /**
* \brief Pass chunk of data for the reader to manage. * \brief Pass chunk of data for the reader to manage.
@ -255,17 +255,17 @@ int mbedtls_reader_free( mbedtls_reader *reader );
* *
* \return \c 0 on success. In this case, the reader will be * \return \c 0 on success. In this case, the reader will be
* 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_mps_reader_reclaim()
* is called. * is called.
* \return \c MBEDTLS_ERR_MPS_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_mps_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
* is made instead). * is made instead).
* \return Another negative \c MBEDTLS_ERR_READER_XXX error code on * \return Another negative \c MBEDTLS_ERR_READER_XXX error code on
* different kinds of failures. * different kinds of failures.
*/ */
int mbedtls_reader_feed( mbedtls_reader *reader, int mbedtls_mps_reader_feed( mbedtls_mps_reader *reader,
unsigned char *buf, unsigned char *buf,
mbedtls_mps_size_t buflen ); mbedtls_mps_size_t buflen );
@ -278,13 +278,13 @@ int mbedtls_reader_feed( mbedtls_reader *reader,
* modified to indicate whether the reader has been paused * modified to indicate whether the reader has been paused
* (value \c 1) or not (value \c 0). Pausing happens if there * (value \c 1) or not (value \c 0). Pausing happens if there
* is uncommitted data and a previous request to * is uncommitted data and a previous request to
* mbedtls_reader_get() has exceeded the bounds of the * mbedtls_mps_reader_get() has exceeded the bounds of the
* input buffer. * input buffer.
* *
* \return \c 0 on success. * \return \c 0 on success.
* \return A negative \c MBEDTLS_ERR_READER_XXX error code on failure. * \return A negative \c MBEDTLS_ERR_READER_XXX error code on failure.
*/ */
int mbedtls_reader_reclaim( mbedtls_reader *reader, int mbedtls_mps_reader_reclaim( mbedtls_mps_reader *reader,
mbedtls_mps_size_t *paused ); mbedtls_mps_size_t *paused );
/* /*
@ -306,14 +306,14 @@ int mbedtls_reader_reclaim( mbedtls_reader *reader,
* address of a buffer of size \c *buflen * address of a buffer of size \c *buflen
* (if \c buflen != \c NULL) or \c desired * (if \c buflen != \c NULL) or \c desired
* (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_mps_reader_commit().
* or mbedtls_reader_reclaim(). * or mbedtls_mps_reader_reclaim().
* \return #MBEDTLS_ERR_MPS_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
* mbedtls_reader_reclaim() and feeding a new one via * mbedtls_mps_reader_reclaim() and feeding a new one via
* mbedtls_reader_feed(). * mbedtls_mps_reader_feed().
* \return Another negative \c MBEDTLS_ERR_READER_XXX error * \return Another negative \c MBEDTLS_ERR_READER_XXX error
* code for different kinds of failure. * code for different kinds of failure.
* *
@ -323,7 +323,7 @@ int mbedtls_reader_reclaim( mbedtls_reader *reader,
* address as buflen and checking \c *buflen == \c desired * address as buflen and checking \c *buflen == \c desired
* afterwards. * afterwards.
*/ */
int mbedtls_reader_get( mbedtls_reader *reader, int mbedtls_mps_reader_get( mbedtls_mps_reader *reader,
mbedtls_mps_size_t desired, mbedtls_mps_size_t desired,
unsigned char **buffer, unsigned char **buffer,
mbedtls_mps_size_t *buflen ); mbedtls_mps_size_t *buflen );
@ -344,6 +344,6 @@ int mbedtls_reader_get( mbedtls_reader *reader,
* pointers corresponding to the committed data anymore. * pointers corresponding to the committed data anymore.
* *
*/ */
int mbedtls_reader_commit( mbedtls_reader *reader ); int mbedtls_mps_reader_commit( mbedtls_mps_reader *reader );
#endif /* MBEDTLS_READER_H */ #endif /* MBEDTLS_READER_H */

View file

@ -40,24 +40,24 @@ void mbedtls_mps_reader_no_pausing_single_step_single_round( int with_acc )
unsigned char bufA[100]; unsigned char bufA[100];
unsigned char acc[10]; unsigned char acc[10];
unsigned char *tmp; unsigned char *tmp;
mbedtls_reader rd; mbedtls_mps_reader rd;
for( int i=0; (unsigned) i < sizeof( bufA ); i++ ) for( int i=0; (unsigned) i < sizeof( bufA ); i++ )
bufA[i] = (unsigned char) i; bufA[i] = (unsigned char) i;
/* Preparation (lower layer) */ /* Preparation (lower layer) */
if( with_acc == 0 ) if( with_acc == 0 )
mbedtls_reader_init( &rd, NULL, 0 ); mbedtls_mps_reader_init( &rd, NULL, 0 );
else else
mbedtls_reader_init( &rd, acc, sizeof( acc ) ); mbedtls_mps_reader_init( &rd, acc, sizeof( acc ) );
TEST_ASSERT( mbedtls_reader_feed( &rd, bufA, sizeof( bufA ) ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_feed( &rd, bufA, sizeof( bufA ) ) == 0 );
/* Consumption (upper layer) */ /* Consumption (upper layer) */
/* Consume exactly what's available */ /* Consume exactly what's available */
TEST_ASSERT( mbedtls_reader_get( &rd, 100, &tmp, NULL ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_get( &rd, 100, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 100, bufA, 100 ); ASSERT_COMPARE( tmp, 100, bufA, 100 );
TEST_ASSERT( mbedtls_reader_commit( &rd ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
/* Wrapup (lower layer) */ /* Wrapup (lower layer) */
TEST_ASSERT( mbedtls_reader_reclaim( &rd, NULL ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_reclaim( &rd, NULL ) == 0 );
mbedtls_reader_free( &rd ); mbedtls_mps_reader_free( &rd );
} }
/* END_CASE */ /* END_CASE */
@ -82,7 +82,7 @@ void mbedtls_mps_reader_no_pausing_single_step_multiple_rounds( int with_acc )
unsigned char bufA[100], bufB[100]; unsigned char bufA[100], bufB[100];
unsigned char acc[10]; unsigned char acc[10];
unsigned char *tmp; unsigned char *tmp;
mbedtls_reader rd; mbedtls_mps_reader rd;
for( int i=0; (unsigned) i < sizeof( bufA ); i++ ) for( int i=0; (unsigned) i < sizeof( bufA ); i++ )
bufA[i] = (unsigned char) i; bufA[i] = (unsigned char) i;
for( int i=0; (unsigned) i < sizeof( bufB ); i++ ) for( int i=0; (unsigned) i < sizeof( bufB ); i++ )
@ -90,25 +90,25 @@ void mbedtls_mps_reader_no_pausing_single_step_multiple_rounds( int with_acc )
/* Preparation (lower layer) */ /* Preparation (lower layer) */
if( with_acc == 0 ) if( with_acc == 0 )
mbedtls_reader_init( &rd, NULL, 0 ); mbedtls_mps_reader_init( &rd, NULL, 0 );
else else
mbedtls_reader_init( &rd, acc, sizeof( acc ) ); mbedtls_mps_reader_init( &rd, acc, sizeof( acc ) );
TEST_ASSERT( mbedtls_reader_feed( &rd, bufA, sizeof( bufA ) ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_feed( &rd, bufA, sizeof( bufA ) ) == 0 );
/* Consumption (upper layer) */ /* Consumption (upper layer) */
/* Consume exactly what's available */ /* Consume exactly what's available */
TEST_ASSERT( mbedtls_reader_get( &rd, 100, &tmp, NULL ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_get( &rd, 100, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 100, bufA, 100 ); ASSERT_COMPARE( tmp, 100, bufA, 100 );
TEST_ASSERT( mbedtls_reader_commit( &rd ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
/* Preparation */ /* Preparation */
TEST_ASSERT( mbedtls_reader_reclaim( &rd, NULL ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_reclaim( &rd, NULL ) == 0 );
TEST_ASSERT( mbedtls_reader_feed( &rd, bufB, sizeof( bufB ) ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_feed( &rd, bufB, sizeof( bufB ) ) == 0 );
/* Consumption */ /* Consumption */
TEST_ASSERT( mbedtls_reader_get( &rd, 100, &tmp, NULL ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_get( &rd, 100, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 100, bufB, 100 ); ASSERT_COMPARE( tmp, 100, bufB, 100 );
TEST_ASSERT( mbedtls_reader_commit( &rd ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
/* Wrapup (lower layer) */ /* Wrapup (lower layer) */
TEST_ASSERT( mbedtls_reader_reclaim( &rd, NULL ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_reclaim( &rd, NULL ) == 0 );
mbedtls_reader_free( &rd ); mbedtls_mps_reader_free( &rd );
} }
/* END_CASE */ /* END_CASE */
@ -118,7 +118,7 @@ void mbedtls_mps_reader_no_pausing_multiple_steps_single_round( int with_acc )
/* This test exercises one round of the following: /* This test exercises one round of the following:
* - The 'producing' layer provides a buffer * - The 'producing' layer provides a buffer
* - The 'consuming' layer fetches it in multiple calls * - The 'consuming' layer fetches it in multiple calls
* to `mbedtls_reader_get()`, without comitting in between. * to `mbedtls_mps_reader_get()`, without comitting in between.
* - After processing, the consuming layer commit the data * - After processing, the consuming layer commit the data
* and returns back to the producing layer. * and returns back to the producing layer.
* *
@ -137,27 +137,27 @@ void mbedtls_mps_reader_no_pausing_multiple_steps_single_round( int with_acc )
unsigned char acc[10]; unsigned char acc[10];
unsigned char *tmp; unsigned char *tmp;
mbedtls_mps_size_t tmp_len; mbedtls_mps_size_t tmp_len;
mbedtls_reader rd; mbedtls_mps_reader rd;
for( int i=0; (unsigned) i < sizeof( buf ); i++ ) for( int i=0; (unsigned) i < sizeof( buf ); i++ )
buf[i] = (unsigned char) i; buf[i] = (unsigned char) i;
/* Preparation (lower layer) */ /* Preparation (lower layer) */
if( with_acc == 0 ) if( with_acc == 0 )
mbedtls_reader_init( &rd, NULL, 0 ); mbedtls_mps_reader_init( &rd, NULL, 0 );
else else
mbedtls_reader_init( &rd, acc, sizeof( acc ) ); mbedtls_mps_reader_init( &rd, acc, sizeof( acc ) );
TEST_ASSERT( mbedtls_reader_feed( &rd, buf, sizeof( buf ) ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_feed( &rd, buf, sizeof( buf ) ) == 0 );
/* Consumption (upper layer) */ /* Consumption (upper layer) */
TEST_ASSERT( mbedtls_reader_get( &rd, 10, &tmp, NULL ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_get( &rd, 10, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 10, buf, 10 ); ASSERT_COMPARE( tmp, 10, buf, 10 );
TEST_ASSERT( mbedtls_reader_get( &rd, 70, &tmp, NULL ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_get( &rd, 70, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 70, buf + 10, 70 ); ASSERT_COMPARE( tmp, 70, buf + 10, 70 );
TEST_ASSERT( mbedtls_reader_get( &rd, 30, &tmp, &tmp_len ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_get( &rd, 30, &tmp, &tmp_len ) == 0 );
ASSERT_COMPARE( tmp, tmp_len, buf + 80, 20 ); ASSERT_COMPARE( tmp, tmp_len, buf + 80, 20 );
TEST_ASSERT( mbedtls_reader_commit( &rd ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
/* Wrapup (lower layer) */ /* Wrapup (lower layer) */
TEST_ASSERT( mbedtls_reader_reclaim( &rd, NULL ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_reclaim( &rd, NULL ) == 0 );
mbedtls_reader_free( &rd ); mbedtls_mps_reader_free( &rd );
} }
/* END_CASE */ /* END_CASE */
@ -172,7 +172,7 @@ void mbedtls_mps_reader_no_pausing_multiple_steps_multiple_rounds( int with_acc
unsigned char acc[10]; unsigned char acc[10];
unsigned char *tmp; unsigned char *tmp;
mbedtls_mps_size_t tmp_len; mbedtls_mps_size_t tmp_len;
mbedtls_reader rd; mbedtls_mps_reader rd;
for( int i=0; (unsigned) i < sizeof( bufA ); i++ ) for( int i=0; (unsigned) i < sizeof( bufA ); i++ )
bufA[i] = (unsigned char) i; bufA[i] = (unsigned char) i;
for( int i=0; (unsigned) i < sizeof( bufB ); i++ ) for( int i=0; (unsigned) i < sizeof( bufB ); i++ )
@ -180,28 +180,28 @@ void mbedtls_mps_reader_no_pausing_multiple_steps_multiple_rounds( int with_acc
/* Preparation (lower layer) */ /* Preparation (lower layer) */
if( with_acc == 0 ) if( with_acc == 0 )
mbedtls_reader_init( &rd, NULL, 0 ); mbedtls_mps_reader_init( &rd, NULL, 0 );
else else
mbedtls_reader_init( &rd, acc, sizeof( acc ) ); mbedtls_mps_reader_init( &rd, acc, sizeof( acc ) );
TEST_ASSERT( mbedtls_reader_feed( &rd, bufA, sizeof( bufA ) ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_feed( &rd, bufA, sizeof( bufA ) ) == 0 );
/* Consumption (upper layer) */ /* Consumption (upper layer) */
TEST_ASSERT( mbedtls_reader_get( &rd, 10, &tmp, NULL ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_get( &rd, 10, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 10, bufA, 10 ); ASSERT_COMPARE( tmp, 10, bufA, 10 );
TEST_ASSERT( mbedtls_reader_get( &rd, 70, &tmp, NULL ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_get( &rd, 70, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 70, bufA + 10, 70 ); ASSERT_COMPARE( tmp, 70, bufA + 10, 70 );
TEST_ASSERT( mbedtls_reader_get( &rd, 30, &tmp, &tmp_len ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_get( &rd, 30, &tmp, &tmp_len ) == 0 );
ASSERT_COMPARE( tmp, tmp_len, bufA + 80, 20 ); ASSERT_COMPARE( tmp, tmp_len, bufA + 80, 20 );
TEST_ASSERT( mbedtls_reader_commit( &rd ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
/* Preparation */ /* Preparation */
TEST_ASSERT( mbedtls_reader_reclaim( &rd, NULL ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_reclaim( &rd, NULL ) == 0 );
TEST_ASSERT( mbedtls_reader_feed( &rd, bufB, sizeof( bufB ) ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_feed( &rd, bufB, sizeof( bufB ) ) == 0 );
/* Consumption */ /* Consumption */
TEST_ASSERT( mbedtls_reader_get( &rd, 100, &tmp, NULL ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_get( &rd, 100, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 100, bufB, 100 ); ASSERT_COMPARE( tmp, 100, bufB, 100 );
TEST_ASSERT( mbedtls_reader_commit( &rd ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
/* Wrapup */ /* Wrapup */
TEST_ASSERT( mbedtls_reader_reclaim( &rd, NULL ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_reclaim( &rd, NULL ) == 0 );
mbedtls_reader_free( &rd ); mbedtls_mps_reader_free( &rd );
} }
/* END_CASE */ /* END_CASE */
@ -217,23 +217,23 @@ void mbedtls_mps_reader_pausing_needed_disabled()
unsigned char buf[100]; unsigned char buf[100];
unsigned char *tmp; unsigned char *tmp;
mbedtls_reader rd; mbedtls_mps_reader rd;
for( int i=0; (unsigned) i < sizeof( buf ); i++ ) for( int i=0; (unsigned) i < sizeof( buf ); i++ )
buf[i] = (unsigned char) i; buf[i] = (unsigned char) i;
/* Preparation (lower layer) */ /* Preparation (lower layer) */
mbedtls_reader_init( &rd, NULL, 0 ); mbedtls_mps_reader_init( &rd, NULL, 0 );
TEST_ASSERT( mbedtls_reader_feed( &rd, buf, sizeof( buf ) ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_feed( &rd, buf, sizeof( buf ) ) == 0 );
/* Consumption (upper layer) */ /* Consumption (upper layer) */
TEST_ASSERT( mbedtls_reader_get( &rd, 50, &tmp, NULL ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_get( &rd, 50, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 50, buf, 50 ); ASSERT_COMPARE( tmp, 50, buf, 50 );
TEST_ASSERT( mbedtls_reader_commit( &rd ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
TEST_ASSERT( mbedtls_reader_get( &rd, 100, &tmp, NULL ) == TEST_ASSERT( mbedtls_mps_reader_get( &rd, 100, &tmp, NULL ) ==
MBEDTLS_ERR_MPS_READER_OUT_OF_DATA ); MBEDTLS_ERR_MPS_READER_OUT_OF_DATA );
/* Wrapup (lower layer) */ /* Wrapup (lower layer) */
TEST_ASSERT( mbedtls_reader_reclaim( &rd, NULL ) == TEST_ASSERT( mbedtls_mps_reader_reclaim( &rd, NULL ) ==
MBEDTLS_ERR_MPS_READER_NEED_ACCUMULATOR ); MBEDTLS_ERR_MPS_READER_NEED_ACCUMULATOR );
mbedtls_reader_free( &rd ); mbedtls_mps_reader_free( &rd );
} }
/* END_CASE */ /* END_CASE */
@ -250,23 +250,23 @@ void mbedtls_mps_reader_pausing_needed_buffer_too_small()
unsigned char buf[100]; unsigned char buf[100];
unsigned char acc[10]; unsigned char acc[10];
unsigned char *tmp; unsigned char *tmp;
mbedtls_reader rd; mbedtls_mps_reader rd;
for( int i=0; (unsigned) i < sizeof( buf ); i++ ) for( int i=0; (unsigned) i < sizeof( buf ); i++ )
buf[i] = (unsigned char) i; buf[i] = (unsigned char) i;
/* Preparation (lower layer) */ /* Preparation (lower layer) */
mbedtls_reader_init( &rd, acc, sizeof( acc ) ); mbedtls_mps_reader_init( &rd, acc, sizeof( acc ) );
TEST_ASSERT( mbedtls_reader_feed( &rd, buf, sizeof( buf ) ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_feed( &rd, buf, sizeof( buf ) ) == 0 );
/* Consumption (upper layer) */ /* Consumption (upper layer) */
TEST_ASSERT( mbedtls_reader_get( &rd, 50, &tmp, NULL ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_get( &rd, 50, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 50, buf, 50 ); ASSERT_COMPARE( tmp, 50, buf, 50 );
TEST_ASSERT( mbedtls_reader_commit( &rd ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
TEST_ASSERT( mbedtls_reader_get( &rd, 100, &tmp, NULL ) == TEST_ASSERT( mbedtls_mps_reader_get( &rd, 100, &tmp, NULL ) ==
MBEDTLS_ERR_MPS_READER_OUT_OF_DATA ); MBEDTLS_ERR_MPS_READER_OUT_OF_DATA );
/* Wrapup (lower layer) */ /* Wrapup (lower layer) */
TEST_ASSERT( mbedtls_reader_reclaim( &rd, NULL ) == TEST_ASSERT( mbedtls_mps_reader_reclaim( &rd, NULL ) ==
MBEDTLS_ERR_MPS_READER_ACCUMULATOR_TOO_SMALL ); MBEDTLS_ERR_MPS_READER_ACCUMULATOR_TOO_SMALL );
mbedtls_reader_free( &rd ); mbedtls_mps_reader_free( &rd );
} }
/* END_CASE */ /* END_CASE */
@ -293,89 +293,89 @@ void mbedtls_mps_reader_pausing( int option )
unsigned char bufA[100], bufB[100]; unsigned char bufA[100], bufB[100];
unsigned char *tmp; unsigned char *tmp;
unsigned char acc[40]; unsigned char acc[40];
mbedtls_reader rd; mbedtls_mps_reader rd;
for( int i=0; (unsigned) i < sizeof( bufA ); i++ ) for( int i=0; (unsigned) i < sizeof( bufA ); i++ )
bufA[i] = (unsigned char) i; bufA[i] = (unsigned char) i;
for( int i=0; (unsigned) i < sizeof( bufB ); i++ ) for( int i=0; (unsigned) i < sizeof( bufB ); i++ )
bufB[i] = ~ ((unsigned char) i); bufB[i] = ~ ((unsigned char) i);
/* Preparation (lower layer) */ /* Preparation (lower layer) */
mbedtls_reader_init( &rd, acc, sizeof( acc ) ); mbedtls_mps_reader_init( &rd, acc, sizeof( acc ) );
TEST_ASSERT( mbedtls_reader_feed( &rd, bufA, sizeof( bufA ) ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_feed( &rd, bufA, sizeof( bufA ) ) == 0 );
/* Consumption (upper layer) */ /* Consumption (upper layer) */
/* Ask for more than what's available. */ /* Ask for more than what's available. */
TEST_ASSERT( mbedtls_reader_get( &rd, 80, &tmp, NULL ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_get( &rd, 80, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 80, bufA, 80 ); ASSERT_COMPARE( tmp, 80, bufA, 80 );
TEST_ASSERT( mbedtls_reader_commit( &rd ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
TEST_ASSERT( mbedtls_reader_get( &rd, 10, &tmp, NULL ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_get( &rd, 10, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 10, bufA + 80, 10 ); ASSERT_COMPARE( tmp, 10, bufA + 80, 10 );
switch( option ) switch( option )
{ {
case 0: /* Single uncommitted fetch at pausing */ case 0: /* Single uncommitted fetch at pausing */
case 1: case 1:
TEST_ASSERT( mbedtls_reader_commit( &rd ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
break; break;
default: /* Multiple uncommitted fetches at pausing */ default: /* Multiple uncommitted fetches at pausing */
break; break;
} }
TEST_ASSERT( mbedtls_reader_get( &rd, 20, &tmp, NULL ) == TEST_ASSERT( mbedtls_mps_reader_get( &rd, 20, &tmp, NULL ) ==
MBEDTLS_ERR_MPS_READER_OUT_OF_DATA ); MBEDTLS_ERR_MPS_READER_OUT_OF_DATA );
/* Preparation */ /* Preparation */
TEST_ASSERT( mbedtls_reader_reclaim( &rd, NULL ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_reclaim( &rd, NULL ) == 0 );
TEST_ASSERT( mbedtls_reader_feed( &rd, bufB, sizeof( bufB ) ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_feed( &rd, bufB, sizeof( bufB ) ) == 0 );
/* Consumption */ /* Consumption */
switch( option ) switch( option )
{ {
case 0: /* Single fetch at pausing, re-fetch with commit. */ case 0: /* Single fetch at pausing, re-fetch with commit. */
TEST_ASSERT( mbedtls_reader_get( &rd, 20, &tmp, NULL ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_get( &rd, 20, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 10, bufA + 90, 10 ); ASSERT_COMPARE( tmp, 10, bufA + 90, 10 );
ASSERT_COMPARE( tmp + 10, 10, bufB, 10 ); ASSERT_COMPARE( tmp + 10, 10, bufB, 10 );
TEST_ASSERT( mbedtls_reader_commit( &rd ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
break; break;
case 1: /* Single fetch at pausing, re-fetch without commit. */ case 1: /* Single fetch at pausing, re-fetch without commit. */
TEST_ASSERT( mbedtls_reader_get( &rd, 20, &tmp, NULL ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_get( &rd, 20, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 10, bufA + 90, 10 ); ASSERT_COMPARE( tmp, 10, bufA + 90, 10 );
ASSERT_COMPARE( tmp + 10, 10, bufB, 10 ); ASSERT_COMPARE( tmp + 10, 10, bufB, 10 );
break; break;
case 2: /* Multiple fetches at pausing, repeat without commit. */ case 2: /* Multiple fetches at pausing, repeat without commit. */
TEST_ASSERT( mbedtls_reader_get( &rd, 10, &tmp, NULL ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_get( &rd, 10, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 10, bufA + 80, 10 ); ASSERT_COMPARE( tmp, 10, bufA + 80, 10 );
TEST_ASSERT( mbedtls_reader_get( &rd, 20, &tmp, NULL ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_get( &rd, 20, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 10, bufA + 90, 10 ); ASSERT_COMPARE( tmp, 10, bufA + 90, 10 );
ASSERT_COMPARE( tmp + 10, 10, bufB, 10 ); ASSERT_COMPARE( tmp + 10, 10, bufB, 10 );
break; break;
case 3: /* Multiple fetches at pausing, repeat with commit 1. */ case 3: /* Multiple fetches at pausing, repeat with commit 1. */
TEST_ASSERT( mbedtls_reader_get( &rd, 10, &tmp, NULL ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_get( &rd, 10, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 10, bufA + 80, 10 ); ASSERT_COMPARE( tmp, 10, bufA + 80, 10 );
TEST_ASSERT( mbedtls_reader_commit( &rd ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
TEST_ASSERT( mbedtls_reader_get( &rd, 20, &tmp, NULL ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_get( &rd, 20, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 10, bufA + 90, 10 ); ASSERT_COMPARE( tmp, 10, bufA + 90, 10 );
ASSERT_COMPARE( tmp + 10, 10, bufB, 10 ); ASSERT_COMPARE( tmp + 10, 10, bufB, 10 );
break; break;
case 4: /* Multiple fetches at pausing, repeat with commit 2. */ case 4: /* Multiple fetches at pausing, repeat with commit 2. */
TEST_ASSERT( mbedtls_reader_get( &rd, 10, &tmp, NULL ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_get( &rd, 10, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 10, bufA + 80, 10 ); ASSERT_COMPARE( tmp, 10, bufA + 80, 10 );
TEST_ASSERT( mbedtls_reader_get( &rd, 20, &tmp, NULL ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_get( &rd, 20, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 10, bufA + 90, 10 ); ASSERT_COMPARE( tmp, 10, bufA + 90, 10 );
ASSERT_COMPARE( tmp + 10, 10, bufB, 10 ); ASSERT_COMPARE( tmp + 10, 10, bufB, 10 );
TEST_ASSERT( mbedtls_reader_commit( &rd ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
break; break;
case 5: /* Multiple fetches at pausing, repeat with commit 3. */ case 5: /* Multiple fetches at pausing, repeat with commit 3. */
TEST_ASSERT( mbedtls_reader_get( &rd, 10, &tmp, NULL ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_get( &rd, 10, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 10, bufA + 80, 10 ); ASSERT_COMPARE( tmp, 10, bufA + 80, 10 );
TEST_ASSERT( mbedtls_reader_commit( &rd ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
TEST_ASSERT( mbedtls_reader_get( &rd, 20, &tmp, NULL ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_get( &rd, 20, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 10, bufA + 90, 10 ); ASSERT_COMPARE( tmp, 10, bufA + 90, 10 );
ASSERT_COMPARE( tmp + 10, 10, bufB, 10 ); ASSERT_COMPARE( tmp + 10, 10, bufB, 10 );
TEST_ASSERT( mbedtls_reader_commit( &rd ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
break; break;
default: default:
@ -383,13 +383,13 @@ void mbedtls_mps_reader_pausing( int option )
} }
/* In all cases, fetch the rest of the second buffer. */ /* In all cases, fetch the rest of the second buffer. */
TEST_ASSERT( mbedtls_reader_get( &rd, 90, &tmp, NULL ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_get( &rd, 90, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 90, bufB + 10, 90 ); ASSERT_COMPARE( tmp, 90, bufB + 10, 90 );
TEST_ASSERT( mbedtls_reader_commit( &rd ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
/* Wrapup */ /* Wrapup */
TEST_ASSERT( mbedtls_reader_reclaim( &rd, NULL ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_reclaim( &rd, NULL ) == 0 );
mbedtls_reader_free( &rd ); mbedtls_mps_reader_free( &rd );
} }
/* END_CASE */ /* END_CASE */
@ -400,8 +400,8 @@ void mbedtls_mps_reader_pausing_multiple_feeds( int option )
* in the following situation: * in the following situation:
* - The consumer has asked for mre than what's available, so the * - The consumer has asked for mre than what's available, so the
* reader pauses and waits for further input data via * reader pauses and waits for further input data via
* `mbedtls_reader_feed()` * `mbedtls_mps_reader_feed()`
* - Multiple such calls to `mbedtls_reader_feed()` are necessary * - Multiple such calls to `mbedtls_mps_reader_feed()` are necessary
* to fulfill the original request, and the reader needs to do * to fulfill the original request, and the reader needs to do
* the necessary bookkeeping under the hood. * the necessary bookkeeping under the hood.
* *
@ -413,7 +413,7 @@ void mbedtls_mps_reader_pausing_multiple_feeds( int option )
unsigned char bufA[100], bufB[100]; unsigned char bufA[100], bufB[100];
unsigned char *tmp; unsigned char *tmp;
unsigned char acc[70]; unsigned char acc[70];
mbedtls_reader rd; mbedtls_mps_reader rd;
mbedtls_mps_size_t fetch_len; mbedtls_mps_size_t fetch_len;
for( int i=0; (unsigned) i < sizeof( bufA ); i++ ) for( int i=0; (unsigned) i < sizeof( bufA ); i++ )
bufA[i] = (unsigned char) i; bufA[i] = (unsigned char) i;
@ -421,46 +421,46 @@ void mbedtls_mps_reader_pausing_multiple_feeds( int option )
bufB[i] = ~ ((unsigned char) i); bufB[i] = ~ ((unsigned char) i);
/* Preparation (lower layer) */ /* Preparation (lower layer) */
mbedtls_reader_init( &rd, acc, sizeof( acc ) ); mbedtls_mps_reader_init( &rd, acc, sizeof( acc ) );
TEST_ASSERT( mbedtls_reader_feed( &rd, bufA, sizeof( bufA ) ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_feed( &rd, bufA, sizeof( bufA ) ) == 0 );
/* Consumption (upper layer) */ /* Consumption (upper layer) */
/* Ask for more than what's available. */ /* Ask for more than what's available. */
TEST_ASSERT( mbedtls_reader_get( &rd, 80, &tmp, NULL ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_get( &rd, 80, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 80, bufA, 80 ); ASSERT_COMPARE( tmp, 80, bufA, 80 );
TEST_ASSERT( mbedtls_reader_commit( &rd ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
/* 20 left, ask for 70 -> 50 overhead */ /* 20 left, ask for 70 -> 50 overhead */
TEST_ASSERT( mbedtls_reader_get( &rd, 70, &tmp, NULL ) == TEST_ASSERT( mbedtls_mps_reader_get( &rd, 70, &tmp, NULL ) ==
MBEDTLS_ERR_MPS_READER_OUT_OF_DATA ); MBEDTLS_ERR_MPS_READER_OUT_OF_DATA );
/* Preparation */ /* Preparation */
TEST_ASSERT( mbedtls_reader_reclaim( &rd, NULL ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_reclaim( &rd, NULL ) == 0 );
switch( option ) switch( option )
{ {
case 0: /* 10 + 10 + 80 byte feed */ case 0: /* 10 + 10 + 80 byte feed */
TEST_ASSERT( mbedtls_reader_feed( &rd, bufB, 10 ) == TEST_ASSERT( mbedtls_mps_reader_feed( &rd, bufB, 10 ) ==
MBEDTLS_ERR_MPS_READER_NEED_MORE ); MBEDTLS_ERR_MPS_READER_NEED_MORE );
TEST_ASSERT( mbedtls_reader_feed( &rd, bufB + 10, 10 ) == TEST_ASSERT( mbedtls_mps_reader_feed( &rd, bufB + 10, 10 ) ==
MBEDTLS_ERR_MPS_READER_NEED_MORE ); MBEDTLS_ERR_MPS_READER_NEED_MORE );
TEST_ASSERT( mbedtls_reader_feed( &rd, bufB + 20, 80 ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_feed( &rd, bufB + 20, 80 ) == 0 );
break; break;
case 1: /* 50 x 1byte */ case 1: /* 50 x 1byte */
for( int num_feed=0; num_feed<49; num_feed++ ) for( int num_feed=0; num_feed<49; num_feed++ )
{ {
TEST_ASSERT( mbedtls_reader_feed( &rd, bufB + num_feed, 1 ) == TEST_ASSERT( mbedtls_mps_reader_feed( &rd, bufB + num_feed, 1 ) ==
MBEDTLS_ERR_MPS_READER_NEED_MORE ); MBEDTLS_ERR_MPS_READER_NEED_MORE );
} }
TEST_ASSERT( mbedtls_reader_feed( &rd, bufB + 49, 1 ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_feed( &rd, bufB + 49, 1 ) == 0 );
break; break;
case 2: /* 49 x 1byte + 51bytes */ case 2: /* 49 x 1byte + 51bytes */
for( int num_feed=0; num_feed<49; num_feed++ ) for( int num_feed=0; num_feed<49; num_feed++ )
{ {
TEST_ASSERT( mbedtls_reader_feed( &rd, bufB + num_feed, 1 ) == TEST_ASSERT( mbedtls_mps_reader_feed( &rd, bufB + num_feed, 1 ) ==
MBEDTLS_ERR_MPS_READER_NEED_MORE ); MBEDTLS_ERR_MPS_READER_NEED_MORE );
} }
TEST_ASSERT( mbedtls_reader_feed( &rd, bufB + 49, 51 ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_feed( &rd, bufB + 49, 51 ) == 0 );
break; break;
default: default:
@ -469,10 +469,10 @@ void mbedtls_mps_reader_pausing_multiple_feeds( int option )
} }
/* Consumption */ /* Consumption */
TEST_ASSERT( mbedtls_reader_get( &rd, 70, &tmp, NULL ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_get( &rd, 70, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 20, bufA + 80, 20 ); ASSERT_COMPARE( tmp, 20, bufA + 80, 20 );
ASSERT_COMPARE( tmp + 20, 50, bufB, 50 ); ASSERT_COMPARE( tmp + 20, 50, bufB, 50 );
TEST_ASSERT( mbedtls_reader_get( &rd, 1000, &tmp, &fetch_len ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_get( &rd, 1000, &tmp, &fetch_len ) == 0 );
switch( option ) switch( option )
{ {
case 0: case 0:
@ -491,11 +491,11 @@ void mbedtls_mps_reader_pausing_multiple_feeds( int option )
TEST_ASSERT( 0 ); TEST_ASSERT( 0 );
break; break;
} }
TEST_ASSERT( mbedtls_reader_commit( &rd ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
/* Wrapup */ /* Wrapup */
TEST_ASSERT( mbedtls_reader_reclaim( &rd, NULL ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_reclaim( &rd, NULL ) == 0 );
mbedtls_reader_free( &rd ); mbedtls_mps_reader_free( &rd );
} }
/* END_CASE */ /* END_CASE */
@ -504,32 +504,32 @@ void mbedtls_mps_reader_pausing_multiple_feeds( int option )
void mbedtls_mps_reader_reclaim_data_left( int option ) void mbedtls_mps_reader_reclaim_data_left( int option )
{ {
/* This test exercises the behaviour of the MPS reader when a /* This test exercises the behaviour of the MPS reader when a
* call to mbedtls_reader_reclaim() is made before all data * call to mbedtls_mps_reader_reclaim() is made before all data
* provided by the producer has been fetched and committed. */ * provided by the producer has been fetched and committed. */
unsigned char buf[100]; unsigned char buf[100];
unsigned char *tmp; unsigned char *tmp;
mbedtls_reader rd; mbedtls_mps_reader rd;
for( int i=0; (unsigned) i < sizeof( buf ); i++ ) for( int i=0; (unsigned) i < sizeof( buf ); i++ )
buf[i] = (unsigned char) i; buf[i] = (unsigned char) i;
/* Preparation (lower layer) */ /* Preparation (lower layer) */
mbedtls_reader_init( &rd, NULL, 0 ); mbedtls_mps_reader_init( &rd, NULL, 0 );
TEST_ASSERT( mbedtls_reader_feed( &rd, buf, sizeof( buf ) ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_feed( &rd, buf, sizeof( buf ) ) == 0 );
/* Consumption (upper layer) */ /* Consumption (upper layer) */
switch( option ) switch( option )
{ {
case 0: case 0:
/* Fetch (but not commit) the entire buffer. */ /* Fetch (but not commit) the entire buffer. */
TEST_ASSERT( mbedtls_reader_get( &rd, sizeof( buf ), &tmp, NULL ) TEST_ASSERT( mbedtls_mps_reader_get( &rd, sizeof( buf ), &tmp, NULL )
== 0 ); == 0 );
ASSERT_COMPARE( tmp, 100, buf, 100 ); ASSERT_COMPARE( tmp, 100, buf, 100 );
break; break;
case 1: case 1:
/* Fetch (but not commit) parts of the buffer. */ /* Fetch (but not commit) parts of the buffer. */
TEST_ASSERT( mbedtls_reader_get( &rd, sizeof( buf ) / 2, TEST_ASSERT( mbedtls_mps_reader_get( &rd, sizeof( buf ) / 2,
&tmp, NULL ) == 0 ); &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, sizeof( buf ) / 2, buf, sizeof( buf ) / 2 ); ASSERT_COMPARE( tmp, sizeof( buf ) / 2, buf, sizeof( buf ) / 2 );
break; break;
@ -537,11 +537,11 @@ void mbedtls_mps_reader_reclaim_data_left( int option )
case 2: case 2:
/* Fetch and commit parts of the buffer, then /* Fetch and commit parts of the buffer, then
* fetch but not commit the rest of the buffer. */ * fetch but not commit the rest of the buffer. */
TEST_ASSERT( mbedtls_reader_get( &rd, sizeof( buf ) / 2, TEST_ASSERT( mbedtls_mps_reader_get( &rd, sizeof( buf ) / 2,
&tmp, NULL ) == 0 ); &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, sizeof( buf ) / 2, buf, sizeof( buf ) / 2 ); ASSERT_COMPARE( tmp, sizeof( buf ) / 2, buf, sizeof( buf ) / 2 );
TEST_ASSERT( mbedtls_reader_commit( &rd ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
TEST_ASSERT( mbedtls_reader_get( &rd, sizeof( buf ) / 2, TEST_ASSERT( mbedtls_mps_reader_get( &rd, sizeof( buf ) / 2,
&tmp, NULL ) == 0 ); &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, sizeof( buf ) / 2, ASSERT_COMPARE( tmp, sizeof( buf ) / 2,
buf + sizeof( buf ) / 2, buf + sizeof( buf ) / 2,
@ -554,9 +554,9 @@ void mbedtls_mps_reader_reclaim_data_left( int option )
} }
/* Wrapup */ /* Wrapup */
TEST_ASSERT( mbedtls_reader_reclaim( &rd, NULL ) == TEST_ASSERT( mbedtls_mps_reader_reclaim( &rd, NULL ) ==
MBEDTLS_ERR_MPS_READER_DATA_LEFT ); MBEDTLS_ERR_MPS_READER_DATA_LEFT );
mbedtls_reader_free( &rd ); mbedtls_mps_reader_free( &rd );
} }
/* END_CASE */ /* END_CASE */
@ -568,30 +568,30 @@ void mbedtls_mps_reader_reclaim_data_left_retry()
* to be processed, and the consumer subsequently fetches more data. */ * to be processed, and the consumer subsequently fetches more data. */
unsigned char buf[100]; unsigned char buf[100];
unsigned char *tmp; unsigned char *tmp;
mbedtls_reader rd; mbedtls_mps_reader rd;
for( int i=0; (unsigned) i < sizeof( buf ); i++ ) for( int i=0; (unsigned) i < sizeof( buf ); i++ )
buf[i] = (unsigned char) i; buf[i] = (unsigned char) i;
/* Preparation (lower layer) */ /* Preparation (lower layer) */
mbedtls_reader_init( &rd, NULL, 0 ); mbedtls_mps_reader_init( &rd, NULL, 0 );
TEST_ASSERT( mbedtls_reader_feed( &rd, buf, sizeof( buf ) ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_feed( &rd, buf, sizeof( buf ) ) == 0 );
/* Consumption (upper layer) */ /* Consumption (upper layer) */
TEST_ASSERT( mbedtls_reader_get( &rd, 50, &tmp, NULL ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_get( &rd, 50, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 50, buf, 50 ); ASSERT_COMPARE( tmp, 50, buf, 50 );
TEST_ASSERT( mbedtls_reader_commit( &rd ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
TEST_ASSERT( mbedtls_reader_get( &rd, 50, &tmp, NULL ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_get( &rd, 50, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 50, buf + 50, 50 ); ASSERT_COMPARE( tmp, 50, buf + 50, 50 );
/* Preparation */ /* Preparation */
TEST_ASSERT( mbedtls_reader_reclaim( &rd, NULL ) == TEST_ASSERT( mbedtls_mps_reader_reclaim( &rd, NULL ) ==
MBEDTLS_ERR_MPS_READER_DATA_LEFT ); MBEDTLS_ERR_MPS_READER_DATA_LEFT );
/* Consumption */ /* Consumption */
TEST_ASSERT( mbedtls_reader_get( &rd, 50, &tmp, NULL ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_get( &rd, 50, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 50, buf + 50, 50 ); ASSERT_COMPARE( tmp, 50, buf + 50, 50 );
TEST_ASSERT( mbedtls_reader_commit( &rd ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
/* Wrapup */ /* Wrapup */
TEST_ASSERT( mbedtls_reader_reclaim( &rd, NULL ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_reclaim( &rd, NULL ) == 0 );
mbedtls_reader_free( &rd ); mbedtls_mps_reader_free( &rd );
} }
/* END_CASE */ /* END_CASE */
@ -600,7 +600,7 @@ void mbedtls_mps_reader_multiple_pausing( int option )
{ {
/* This test exercises the behaviour of the MPS reader /* This test exercises the behaviour of the MPS reader
* in the following situation: * in the following situation:
* - A read request via `mbedtls_reader_get()` can't * - A read request via `mbedtls_mps_reader_get()` can't
* be served and the reader is paused to accumulate * be served and the reader is paused to accumulate
* the desired amount of data from the producer. * the desired amount of data from the producer.
* - Once enough data is availble, the consumer successfully * - Once enough data is availble, the consumer successfully
@ -613,7 +613,7 @@ void mbedtls_mps_reader_multiple_pausing( int option )
unsigned char *tmp; unsigned char *tmp;
unsigned char acc[50]; unsigned char acc[50];
mbedtls_mps_size_t tmp_len; mbedtls_mps_size_t tmp_len;
mbedtls_reader rd; mbedtls_mps_reader rd;
for( int i=0; (unsigned) i < sizeof( bufA ); i++ ) for( int i=0; (unsigned) i < sizeof( bufA ); i++ )
bufA[i] = (unsigned char) i; bufA[i] = (unsigned char) i;
for( int i=0; (unsigned) i < sizeof( bufB ); i++ ) for( int i=0; (unsigned) i < sizeof( bufB ); i++ )
@ -622,22 +622,22 @@ void mbedtls_mps_reader_multiple_pausing( int option )
bufC[i] = ~ ((unsigned char) i); bufC[i] = ~ ((unsigned char) i);
/* Preparation (lower layer) */ /* Preparation (lower layer) */
mbedtls_reader_init( &rd, acc, sizeof( acc ) ); mbedtls_mps_reader_init( &rd, acc, sizeof( acc ) );
TEST_ASSERT( mbedtls_reader_feed( &rd, bufA, sizeof( bufA ) ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_feed( &rd, bufA, sizeof( bufA ) ) == 0 );
/* Consumption (upper layer) */ /* Consumption (upper layer) */
/* Ask for more than what's available. */ /* Ask for more than what's available. */
TEST_ASSERT( mbedtls_reader_get( &rd, 80, &tmp, NULL ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_get( &rd, 80, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 80, bufA, 80 ); ASSERT_COMPARE( tmp, 80, bufA, 80 );
TEST_ASSERT( mbedtls_reader_commit( &rd ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
TEST_ASSERT( mbedtls_reader_get( &rd, 10, &tmp, NULL ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_get( &rd, 10, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 10, bufA + 80, 10 ); ASSERT_COMPARE( tmp, 10, bufA + 80, 10 );
TEST_ASSERT( mbedtls_reader_get( &rd, 20, &tmp, NULL ) == TEST_ASSERT( mbedtls_mps_reader_get( &rd, 20, &tmp, NULL ) ==
MBEDTLS_ERR_MPS_READER_OUT_OF_DATA ); MBEDTLS_ERR_MPS_READER_OUT_OF_DATA );
/* Preparation */ /* Preparation */
TEST_ASSERT( mbedtls_reader_reclaim( &rd, NULL ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_reclaim( &rd, NULL ) == 0 );
TEST_ASSERT( mbedtls_reader_feed( &rd, bufB, sizeof( bufB ) ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_feed( &rd, bufB, sizeof( bufB ) ) == 0 );
switch( option ) switch( option )
{ {
@ -646,21 +646,21 @@ void mbedtls_mps_reader_multiple_pausing( int option )
* large enough. */ * large enough. */
/* Consume */ /* Consume */
TEST_ASSERT( mbedtls_reader_get( &rd, 10, &tmp, &tmp_len ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_get( &rd, 10, &tmp, &tmp_len ) == 0 );
ASSERT_COMPARE( tmp, tmp_len, bufA + 80, 10 ); ASSERT_COMPARE( tmp, tmp_len, bufA + 80, 10 );
TEST_ASSERT( mbedtls_reader_get( &rd, 20, &tmp, NULL ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_get( &rd, 20, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 10, bufA + 90, 10 ); ASSERT_COMPARE( tmp, 10, bufA + 90, 10 );
ASSERT_COMPARE( tmp + 10, 10, bufB, 10 ); ASSERT_COMPARE( tmp + 10, 10, bufB, 10 );
TEST_ASSERT( mbedtls_reader_commit( &rd ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
TEST_ASSERT( mbedtls_reader_get( &rd, 20, &tmp, NULL ) == TEST_ASSERT( mbedtls_mps_reader_get( &rd, 20, &tmp, NULL ) ==
MBEDTLS_ERR_MPS_READER_OUT_OF_DATA ); MBEDTLS_ERR_MPS_READER_OUT_OF_DATA );
/* Prepare */ /* Prepare */
TEST_ASSERT( mbedtls_reader_reclaim( &rd, NULL ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_reclaim( &rd, NULL ) == 0 );
TEST_ASSERT( mbedtls_reader_feed( &rd, bufC, sizeof( bufC ) ) == 0 );; TEST_ASSERT( mbedtls_mps_reader_feed( &rd, bufC, sizeof( bufC ) ) == 0 );;
/* Consume */ /* Consume */
TEST_ASSERT( mbedtls_reader_get( &rd, 20, &tmp, NULL ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_get( &rd, 20, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 10, bufB + 10, 10 ); ASSERT_COMPARE( tmp, 10, bufB + 10, 10 );
ASSERT_COMPARE( tmp + 10, 10, bufC, 10 ); ASSERT_COMPARE( tmp + 10, 10, bufC, 10 );
break; break;
@ -668,37 +668,37 @@ void mbedtls_mps_reader_multiple_pausing( int option )
case 1: /* Fetch same chunks, commit afterwards, and case 1: /* Fetch same chunks, commit afterwards, and
* then exceed bounds of new buffer; accumulator * then exceed bounds of new buffer; accumulator
* not large enough. */ * not large enough. */
TEST_ASSERT( mbedtls_reader_get( &rd, 10, &tmp, NULL ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_get( &rd, 10, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 10, bufA + 80, 10 ); ASSERT_COMPARE( tmp, 10, bufA + 80, 10 );
TEST_ASSERT( mbedtls_reader_get( &rd, 20, &tmp, NULL ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_get( &rd, 20, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 10, bufA + 90, 10 ); ASSERT_COMPARE( tmp, 10, bufA + 90, 10 );
ASSERT_COMPARE( tmp + 10, 10, bufB, 10 ); ASSERT_COMPARE( tmp + 10, 10, bufB, 10 );
TEST_ASSERT( mbedtls_reader_commit( &rd ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
TEST_ASSERT( mbedtls_reader_get( &rd, 51, &tmp, NULL ) == TEST_ASSERT( mbedtls_mps_reader_get( &rd, 51, &tmp, NULL ) ==
MBEDTLS_ERR_MPS_READER_OUT_OF_DATA ); MBEDTLS_ERR_MPS_READER_OUT_OF_DATA );
/* Prepare */ /* Prepare */
TEST_ASSERT( mbedtls_reader_reclaim( &rd, NULL ) == TEST_ASSERT( mbedtls_mps_reader_reclaim( &rd, NULL ) ==
MBEDTLS_ERR_MPS_READER_ACCUMULATOR_TOO_SMALL ); MBEDTLS_ERR_MPS_READER_ACCUMULATOR_TOO_SMALL );
break; break;
case 2: /* Fetch same chunks, don't commit afterwards, and case 2: /* Fetch same chunks, don't commit afterwards, and
* then exceed bounds of new buffer; accumulator * then exceed bounds of new buffer; accumulator
* large enough. */ * large enough. */
TEST_ASSERT( mbedtls_reader_get( &rd, 10, &tmp, NULL ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_get( &rd, 10, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 10, bufA + 80, 10 ); ASSERT_COMPARE( tmp, 10, bufA + 80, 10 );
TEST_ASSERT( mbedtls_reader_get( &rd, 20, &tmp, NULL ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_get( &rd, 20, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 10, bufA + 90, 10 ); ASSERT_COMPARE( tmp, 10, bufA + 90, 10 );
ASSERT_COMPARE( tmp + 10, 10, bufB, 10 ); ASSERT_COMPARE( tmp + 10, 10, bufB, 10 );
TEST_ASSERT( mbedtls_reader_get( &rd, 20, &tmp, NULL ) == TEST_ASSERT( mbedtls_mps_reader_get( &rd, 20, &tmp, NULL ) ==
MBEDTLS_ERR_MPS_READER_OUT_OF_DATA ); MBEDTLS_ERR_MPS_READER_OUT_OF_DATA );
/* Prepare */ /* Prepare */
TEST_ASSERT( mbedtls_reader_reclaim( &rd, NULL ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_reclaim( &rd, NULL ) == 0 );
TEST_ASSERT( mbedtls_reader_feed( &rd, bufC, sizeof( bufC ) ) == 0 );; TEST_ASSERT( mbedtls_mps_reader_feed( &rd, bufC, sizeof( bufC ) ) == 0 );;
/* Consume */ /* Consume */
TEST_ASSERT( mbedtls_reader_get( &rd, 50, &tmp, NULL ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_get( &rd, 50, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 20, bufA + 80, 20 ); ASSERT_COMPARE( tmp, 20, bufA + 80, 20 );
ASSERT_COMPARE( tmp + 20, 20, bufB, 20 ); ASSERT_COMPARE( tmp + 20, 20, bufB, 20 );
ASSERT_COMPARE( tmp + 40, 10, bufC, 10 ); ASSERT_COMPARE( tmp + 40, 10, bufC, 10 );
@ -707,16 +707,16 @@ void mbedtls_mps_reader_multiple_pausing( int option )
case 3: /* Fetch same chunks, don't commit afterwards, and case 3: /* Fetch same chunks, don't commit afterwards, and
* then exceed bounds of new buffer; accumulator * then exceed bounds of new buffer; accumulator
* not large enough. */ * not large enough. */
TEST_ASSERT( mbedtls_reader_get( &rd, 10, &tmp, NULL ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_get( &rd, 10, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 10, bufA + 80, 10 ); ASSERT_COMPARE( tmp, 10, bufA + 80, 10 );
TEST_ASSERT( mbedtls_reader_get( &rd, 20, &tmp, NULL ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_get( &rd, 20, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 10, bufA + 90, 10 ); ASSERT_COMPARE( tmp, 10, bufA + 90, 10 );
ASSERT_COMPARE( tmp + 10, 10, bufB, 10 ); ASSERT_COMPARE( tmp + 10, 10, bufB, 10 );
TEST_ASSERT( mbedtls_reader_get( &rd, 21, &tmp, NULL ) == TEST_ASSERT( mbedtls_mps_reader_get( &rd, 21, &tmp, NULL ) ==
MBEDTLS_ERR_MPS_READER_OUT_OF_DATA ); MBEDTLS_ERR_MPS_READER_OUT_OF_DATA );
/* Prepare */ /* Prepare */
TEST_ASSERT( mbedtls_reader_reclaim( &rd, NULL ) == TEST_ASSERT( mbedtls_mps_reader_reclaim( &rd, NULL ) ==
MBEDTLS_ERR_MPS_READER_ACCUMULATOR_TOO_SMALL ); MBEDTLS_ERR_MPS_READER_ACCUMULATOR_TOO_SMALL );
break; break;
@ -725,7 +725,7 @@ void mbedtls_mps_reader_multiple_pausing( int option )
break; break;
} }
mbedtls_reader_free( &rd ); mbedtls_mps_reader_free( &rd );
} }
/* END_CASE */ /* END_CASE */
@ -771,7 +771,7 @@ void mbedtls_mps_reader_random_usage( int num_out_chunks,
int mode = 0; /* Lower layer (0) or Upper layer (1) */ int mode = 0; /* Lower layer (0) or Upper layer (1) */
int reclaimed = 1; /* Have to call reclaim at least once before int reclaimed = 1; /* Have to call reclaim at least once before
* returning the reader to the upper layer. */ * returning the reader to the upper layer. */
mbedtls_reader rd; mbedtls_mps_reader rd;
if( acc_size > 0 ) if( acc_size > 0 )
{ {
@ -785,7 +785,7 @@ void mbedtls_mps_reader_random_usage( int num_out_chunks,
ASSERT_ALLOC( outgoing, num_out_chunks * max_chunk_size ); ASSERT_ALLOC( outgoing, num_out_chunks * max_chunk_size );
ASSERT_ALLOC( incoming, num_out_chunks * max_chunk_size ); ASSERT_ALLOC( incoming, num_out_chunks * max_chunk_size );
mbedtls_reader_init( &rd, acc, acc_size ); mbedtls_mps_reader_init( &rd, acc, acc_size );
cur_out_chunk = 0; cur_out_chunk = 0;
in_commit = 0; in_commit = 0;
@ -801,7 +801,7 @@ void mbedtls_mps_reader_random_usage( int num_out_chunks,
if( rand_op == 0 ) if( rand_op == 0 )
{ {
/* Reclaim */ /* Reclaim */
ret = mbedtls_reader_reclaim( &rd, NULL ); ret = mbedtls_mps_reader_reclaim( &rd, NULL );
if( ret == 0 ) if( ret == 0 )
{ {
@ -823,7 +823,7 @@ void mbedtls_mps_reader_random_usage( int num_out_chunks,
ASSERT_ALLOC( tmp, tmp_size ); ASSERT_ALLOC( tmp, tmp_size );
TEST_ASSERT( mbedtls_test_rnd_std_rand( NULL, tmp, tmp_size ) == 0 ); TEST_ASSERT( mbedtls_test_rnd_std_rand( NULL, tmp, tmp_size ) == 0 );
ret = mbedtls_reader_feed( &rd, tmp, tmp_size ); ret = mbedtls_mps_reader_feed( &rd, tmp, tmp_size );
if( ret == 0 || ret == MBEDTLS_ERR_MPS_READER_NEED_MORE ) if( ret == 0 || ret == MBEDTLS_ERR_MPS_READER_NEED_MORE )
{ {
@ -864,13 +864,13 @@ void mbedtls_mps_reader_random_usage( int num_out_chunks,
get_size = ( rand() % max_request ) + 1; get_size = ( rand() % max_request ) + 1;
if( rand_op == 0 ) if( rand_op == 0 )
{ {
ret = mbedtls_reader_get( &rd, get_size, &chunk_get, ret = mbedtls_mps_reader_get( &rd, get_size, &chunk_get,
&real_size ); &real_size );
} }
else else
{ {
real_size = get_size; real_size = get_size;
ret = mbedtls_reader_get( &rd, get_size, &chunk_get, NULL ); ret = mbedtls_mps_reader_get( &rd, get_size, &chunk_get, NULL );
} }
/* Check if output is in accordance with what was written */ /* Check if output is in accordance with what was written */
@ -886,7 +886,7 @@ void mbedtls_mps_reader_random_usage( int num_out_chunks,
} }
else if( rand_op == 2 ) /* Commit */ else if( rand_op == 2 ) /* Commit */
{ {
ret = mbedtls_reader_commit( &rd ); ret = mbedtls_mps_reader_commit( &rd );
if( ret == 0 ) if( ret == 0 )
{ {
in_commit += in_fetch; in_commit += in_fetch;
@ -904,7 +904,7 @@ void mbedtls_mps_reader_random_usage( int num_out_chunks,
} }
/* Cleanup */ /* Cleanup */
mbedtls_reader_free( &rd ); mbedtls_mps_reader_free( &rd );
mbedtls_free( incoming ); mbedtls_free( incoming );
mbedtls_free( outgoing ); mbedtls_free( outgoing );
mbedtls_free( acc ); mbedtls_free( acc );
@ -931,7 +931,7 @@ void mbedtls_reader_inconsistent_usage( int option )
unsigned char bufA[100], bufB[100]; unsigned char bufA[100], bufB[100];
unsigned char *tmp; unsigned char *tmp;
unsigned char acc[40]; unsigned char acc[40];
mbedtls_reader rd; mbedtls_mps_reader rd;
int success = 0; int success = 0;
for( int i=0; (unsigned) i < sizeof( bufA ); i++ ) for( int i=0; (unsigned) i < sizeof( bufA ); i++ )
bufA[i] = (unsigned char) i; bufA[i] = (unsigned char) i;
@ -939,23 +939,23 @@ void mbedtls_reader_inconsistent_usage( int option )
bufB[i] = ~ ((unsigned char) i); bufB[i] = ~ ((unsigned char) i);
/* Preparation (lower layer) */ /* Preparation (lower layer) */
mbedtls_reader_init( &rd, acc, sizeof( acc ) ); mbedtls_mps_reader_init( &rd, acc, sizeof( acc ) );
TEST_ASSERT( mbedtls_reader_feed( &rd, bufA, sizeof( bufA ) ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_feed( &rd, bufA, sizeof( bufA ) ) == 0 );
/* Consumption (upper layer) */ /* Consumption (upper layer) */
TEST_ASSERT( mbedtls_reader_get( &rd, 80, &tmp, NULL ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_get( &rd, 80, &tmp, NULL ) == 0 );
TEST_ASSERT( mbedtls_reader_commit( &rd ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
TEST_ASSERT( mbedtls_reader_get( &rd, 10, &tmp, NULL ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_get( &rd, 10, &tmp, NULL ) == 0 );
TEST_ASSERT( mbedtls_reader_get( &rd, 20, &tmp, NULL ) == TEST_ASSERT( mbedtls_mps_reader_get( &rd, 20, &tmp, NULL ) ==
MBEDTLS_ERR_MPS_READER_OUT_OF_DATA ); MBEDTLS_ERR_MPS_READER_OUT_OF_DATA );
/* Preparation */ /* Preparation */
TEST_ASSERT( mbedtls_reader_reclaim( &rd, NULL ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_reclaim( &rd, NULL ) == 0 );
TEST_ASSERT( mbedtls_reader_feed( &rd, bufB, sizeof( bufB ) ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_feed( &rd, bufB, sizeof( bufB ) ) == 0 );
/* Consumption */ /* Consumption */
switch( option ) switch( option )
{ {
case 0: case 0:
/* Ask for buffered data in a single chunk, no commit */ /* Ask for buffered data in a single chunk, no commit */
TEST_ASSERT( mbedtls_reader_get( &rd, 30, &tmp, NULL ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_get( &rd, 30, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 20, bufA + 80, 20 ); ASSERT_COMPARE( tmp, 20, bufA + 80, 20 );
ASSERT_COMPARE( tmp + 20, 10, bufB, 10 ); ASSERT_COMPARE( tmp + 20, 10, bufB, 10 );
success = 1; success = 1;
@ -963,40 +963,40 @@ void mbedtls_reader_inconsistent_usage( int option )
case 1: case 1:
/* Ask for buffered data in a single chunk, with commit */ /* Ask for buffered data in a single chunk, with commit */
TEST_ASSERT( mbedtls_reader_get( &rd, 30, &tmp, NULL ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_get( &rd, 30, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 20, bufA + 80, 20 ); ASSERT_COMPARE( tmp, 20, bufA + 80, 20 );
ASSERT_COMPARE( tmp + 20, 10, bufB, 10 ); ASSERT_COMPARE( tmp + 20, 10, bufB, 10 );
TEST_ASSERT( mbedtls_reader_commit( &rd ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
success = 1; success = 1;
break; break;
case 2: case 2:
/* Ask for more than was requested when pausing, #1 */ /* Ask for more than was requested when pausing, #1 */
TEST_ASSERT( mbedtls_reader_get( &rd, 31, &tmp, NULL ) == TEST_ASSERT( mbedtls_mps_reader_get( &rd, 31, &tmp, NULL ) ==
MBEDTLS_ERR_MPS_READER_INCONSISTENT_REQUESTS ); MBEDTLS_ERR_MPS_READER_INCONSISTENT_REQUESTS );
break; break;
case 3: case 3:
/* Ask for more than was requested when pausing #2 */ /* Ask for more than was requested when pausing #2 */
TEST_ASSERT( mbedtls_reader_get( &rd, (mbedtls_mps_size_t) -1, &tmp, NULL ) == TEST_ASSERT( mbedtls_mps_reader_get( &rd, (mbedtls_mps_size_t) -1, &tmp, NULL ) ==
MBEDTLS_ERR_MPS_READER_INCONSISTENT_REQUESTS ); MBEDTLS_ERR_MPS_READER_INCONSISTENT_REQUESTS );
break; break;
case 4: case 4:
/* Asking for buffered data in different /* Asking for buffered data in different
* chunks than before CAN fail. */ * chunks than before CAN fail. */
TEST_ASSERT( mbedtls_reader_get( &rd, 15, &tmp, NULL ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_get( &rd, 15, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 15, bufA + 80, 15 ); ASSERT_COMPARE( tmp, 15, bufA + 80, 15 );
TEST_ASSERT( mbedtls_reader_get( &rd, 10, &tmp, NULL ) == TEST_ASSERT( mbedtls_mps_reader_get( &rd, 10, &tmp, NULL ) ==
MBEDTLS_ERR_MPS_READER_INCONSISTENT_REQUESTS ); MBEDTLS_ERR_MPS_READER_INCONSISTENT_REQUESTS );
break; break;
case 5: case 5:
/* Asking for buffered data different chunks /* Asking for buffered data different chunks
* than before NEED NOT fail - no commits */ * than before NEED NOT fail - no commits */
TEST_ASSERT( mbedtls_reader_get( &rd, 15, &tmp, NULL ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_get( &rd, 15, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 15, bufA + 80, 15 ); ASSERT_COMPARE( tmp, 15, bufA + 80, 15 );
TEST_ASSERT( mbedtls_reader_get( &rd, 15, &tmp, NULL ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_get( &rd, 15, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 5, bufA + 95, 5 ); ASSERT_COMPARE( tmp, 5, bufA + 95, 5 );
ASSERT_COMPARE( tmp + 5, 10, bufB, 10 ); ASSERT_COMPARE( tmp + 5, 10, bufB, 10 );
success = 1; success = 1;
@ -1005,10 +1005,10 @@ void mbedtls_reader_inconsistent_usage( int option )
case 6: case 6:
/* Asking for buffered data different chunks /* Asking for buffered data different chunks
* than before NEED NOT fail - intermediate commit */ * than before NEED NOT fail - intermediate commit */
TEST_ASSERT( mbedtls_reader_get( &rd, 15, &tmp, NULL ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_get( &rd, 15, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 15, bufA + 80, 15 ); ASSERT_COMPARE( tmp, 15, bufA + 80, 15 );
TEST_ASSERT( mbedtls_reader_commit( &rd ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
TEST_ASSERT( mbedtls_reader_get( &rd, 15, &tmp, NULL ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_get( &rd, 15, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 5, bufA + 95, 5 ); ASSERT_COMPARE( tmp, 5, bufA + 95, 5 );
ASSERT_COMPARE( tmp + 5, 10, bufB, 10 ); ASSERT_COMPARE( tmp + 5, 10, bufB, 10 );
success = 1; success = 1;
@ -1017,25 +1017,25 @@ void mbedtls_reader_inconsistent_usage( int option )
case 7: case 7:
/* Asking for buffered data different chunks /* Asking for buffered data different chunks
* than before NEED NOT fail - end commit */ * than before NEED NOT fail - end commit */
TEST_ASSERT( mbedtls_reader_get( &rd, 15, &tmp, NULL ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_get( &rd, 15, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 15, bufA + 80, 15 ); ASSERT_COMPARE( tmp, 15, bufA + 80, 15 );
TEST_ASSERT( mbedtls_reader_get( &rd, 15, &tmp, NULL ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_get( &rd, 15, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 5, bufA + 95, 5 ); ASSERT_COMPARE( tmp, 5, bufA + 95, 5 );
ASSERT_COMPARE( tmp + 5, 10, bufB, 10 ); ASSERT_COMPARE( tmp + 5, 10, bufB, 10 );
TEST_ASSERT( mbedtls_reader_commit( &rd ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
success = 1; success = 1;
break; break;
case 8: case 8:
/* Asking for buffered data different chunks /* Asking for buffered data different chunks
* than before NEED NOT fail - intermediate & end commit */ * than before NEED NOT fail - intermediate & end commit */
TEST_ASSERT( mbedtls_reader_get( &rd, 15, &tmp, NULL ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_get( &rd, 15, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 15, bufA + 80, 15 ); ASSERT_COMPARE( tmp, 15, bufA + 80, 15 );
TEST_ASSERT( mbedtls_reader_get( &rd, 15, &tmp, NULL ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_get( &rd, 15, &tmp, NULL ) == 0 );
TEST_ASSERT( mbedtls_reader_commit( &rd ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
ASSERT_COMPARE( tmp, 5, bufA + 95, 5 ); ASSERT_COMPARE( tmp, 5, bufA + 95, 5 );
ASSERT_COMPARE( tmp + 5, 10, bufB, 10 ); ASSERT_COMPARE( tmp + 5, 10, bufB, 10 );
TEST_ASSERT( mbedtls_reader_commit( &rd ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
success = 1; success = 1;
break; break;
@ -1047,16 +1047,16 @@ void mbedtls_reader_inconsistent_usage( int option )
if( success == 1 ) if( success == 1 )
{ {
/* In all succeeding cases, fetch the rest of the second buffer. */ /* In all succeeding cases, fetch the rest of the second buffer. */
TEST_ASSERT( mbedtls_reader_get( &rd, 90, &tmp, NULL ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_get( &rd, 90, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 90, bufB + 10, 90 ); ASSERT_COMPARE( tmp, 90, bufB + 10, 90 );
TEST_ASSERT( mbedtls_reader_commit( &rd ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
/* Wrapup */ /* Wrapup */
TEST_ASSERT( mbedtls_reader_reclaim( &rd, NULL ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_reclaim( &rd, NULL ) == 0 );
} }
/* Wrapup */ /* Wrapup */
mbedtls_reader_free( &rd ); mbedtls_mps_reader_free( &rd );
} }
/* END_CASE */ /* END_CASE */
@ -1067,16 +1067,16 @@ void mbedtls_mps_reader_feed_empty( int option )
* fed a NULL buffer. */ * fed a NULL buffer. */
unsigned char buf[100]; unsigned char buf[100];
unsigned char *tmp; unsigned char *tmp;
mbedtls_reader rd; mbedtls_mps_reader rd;
for( int i=0; (unsigned) i < sizeof( buf ); i++ ) for( int i=0; (unsigned) i < sizeof( buf ); i++ )
buf[i] = (unsigned char) i; buf[i] = (unsigned char) i;
/* Preparation (lower layer) */ /* Preparation (lower layer) */
mbedtls_reader_init( &rd, NULL, 0 ); mbedtls_mps_reader_init( &rd, NULL, 0 );
switch( option ) switch( option )
{ {
case 0: /* NULL buffer */ case 0: /* NULL buffer */
TEST_ASSERT( mbedtls_reader_feed( &rd, NULL, sizeof( buf ) ) == TEST_ASSERT( mbedtls_mps_reader_feed( &rd, NULL, sizeof( buf ) ) ==
MBEDTLS_ERR_MPS_READER_INVALID_ARG ); MBEDTLS_ERR_MPS_READER_INVALID_ARG );
break; break;
@ -1085,15 +1085,15 @@ void mbedtls_mps_reader_feed_empty( int option )
break; break;
} }
/* Subsequent feed-calls should still succeed. */ /* Subsequent feed-calls should still succeed. */
TEST_ASSERT( mbedtls_reader_feed( &rd, buf, sizeof( buf ) ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_feed( &rd, buf, sizeof( buf ) ) == 0 );
/* Consumption (upper layer) */ /* Consumption (upper layer) */
TEST_ASSERT( mbedtls_reader_get( &rd, 100, &tmp, NULL ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_get( &rd, 100, &tmp, NULL ) == 0 );
ASSERT_COMPARE( tmp, 100, buf, 100 ); ASSERT_COMPARE( tmp, 100, buf, 100 );
TEST_ASSERT( mbedtls_reader_commit( &rd ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
/* Wrapup */ /* Wrapup */
TEST_ASSERT( mbedtls_reader_reclaim( &rd, NULL ) == 0 ); TEST_ASSERT( mbedtls_mps_reader_reclaim( &rd, NULL ) == 0 );
mbedtls_reader_free( &rd ); mbedtls_mps_reader_free( &rd );
} }
/* END_CASE */ /* END_CASE */