• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#line 2 "suites/helpers.function"
2/*----------------------------------------------------------------------------*/
3/* Headers */
4
5#include <test/macros.h>
6#include <test/helpers.h>
7#include <test/random.h>
8#include <test/psa_crypto_helpers.h>
9
10#include <stdlib.h>
11
12#if defined(MBEDTLS_PLATFORM_C)
13#include "mbedtls/platform.h"
14#else
15#include <stdio.h>
16#define mbedtls_fprintf    fprintf
17#define mbedtls_snprintf   snprintf
18#define mbedtls_calloc     calloc
19#define mbedtls_free       free
20#define mbedtls_exit       exit
21#define mbedtls_time       time
22#define mbedtls_time_t     time_t
23#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
24#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
25#endif
26
27#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
28#include "mbedtls/memory_buffer_alloc.h"
29#endif
30
31#if defined(MBEDTLS_CHECK_PARAMS)
32#include "mbedtls/platform_util.h"
33#include <setjmp.h>
34#endif
35
36#ifdef _MSC_VER
37#include <basetsd.h>
38typedef UINT8 uint8_t;
39typedef INT32 int32_t;
40typedef UINT32 uint32_t;
41#define strncasecmp _strnicmp
42#define strcasecmp _stricmp
43#else
44#include <stdint.h>
45#endif
46
47#include <string.h>
48
49#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
50#include <unistd.h>
51#include <strings.h>
52#endif
53
54/* Type for Hex parameters */
55typedef struct data_tag
56{
57    uint8_t *   x;
58    uint32_t    len;
59} data_t;
60
61/*----------------------------------------------------------------------------*/
62/* Status and error constants */
63
64#define DEPENDENCY_SUPPORTED            0   /* Dependency supported by build */
65#define KEY_VALUE_MAPPING_FOUND         0   /* Integer expression found */
66#define DISPATCH_TEST_SUCCESS           0   /* Test dispatch successful */
67
68#define KEY_VALUE_MAPPING_NOT_FOUND     -1  /* Integer expression not found */
69#define DEPENDENCY_NOT_SUPPORTED        -2  /* Dependency not supported */
70#define DISPATCH_TEST_FN_NOT_FOUND      -3  /* Test function not found */
71#define DISPATCH_INVALID_TEST_DATA      -4  /* Invalid test parameter type.
72                                               Only int, string, binary data
73                                               and integer expressions are
74                                               allowed */
75#define DISPATCH_UNSUPPORTED_SUITE      -5  /* Test suite not supported by the
76                                               build */
77
78/*----------------------------------------------------------------------------*/
79/* Global variables */
80
81#if defined(MBEDTLS_CHECK_PARAMS)
82jmp_buf jmp_tmp;
83#endif
84
85/*----------------------------------------------------------------------------*/
86/* Helper flags for complex dependencies */
87
88/* Indicates whether we expect mbedtls_entropy_init
89 * to initialize some strong entropy source. */
90#if defined(MBEDTLS_TEST_NULL_ENTROPY) ||             \
91    ( !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) && \
92      ( !defined(MBEDTLS_NO_PLATFORM_ENTROPY)  ||     \
93         defined(MBEDTLS_HAVEGE_C)             ||     \
94         defined(MBEDTLS_ENTROPY_HARDWARE_ALT) ||     \
95         defined(ENTROPY_NV_SEED) ) )
96#define ENTROPY_HAVE_STRONG
97#endif
98
99
100/*----------------------------------------------------------------------------*/
101/* Helper Functions */
102
103#if defined(MBEDTLS_PSA_CRYPTO_C)
104/** Check that no PSA Crypto key slots are in use.
105 *
106 * If any slots are in use, mark the current test as failed.
107 *
108 * \return 0 if the key store is empty, 1 otherwise.
109 */
110int test_fail_if_psa_leaking( int line_no, const char *filename )
111{
112    const char *msg = mbedtls_test_helper_is_psa_leaking( );
113    if( msg == NULL )
114        return 0;
115    else
116    {
117        mbedtls_test_fail( msg, line_no, filename );
118        return 1;
119    }
120}
121#endif /* defined(MBEDTLS_PSA_CRYPTO_C) */
122
123#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
124static int redirect_output( FILE* out_stream, const char* path )
125{
126    int out_fd, dup_fd;
127    FILE* path_stream;
128
129    out_fd = fileno( out_stream );
130    dup_fd = dup( out_fd );
131
132    if( dup_fd == -1 )
133    {
134        return( -1 );
135    }
136
137    path_stream = fopen( path, "w" );
138    if( path_stream == NULL )
139    {
140        close( dup_fd );
141        return( -1 );
142    }
143
144    fflush( out_stream );
145    if( dup2( fileno( path_stream ), out_fd ) == -1 )
146    {
147        close( dup_fd );
148        fclose( path_stream );
149        return( -1 );
150    }
151
152    fclose( path_stream );
153    return( dup_fd );
154}
155
156static int restore_output( FILE* out_stream, int dup_fd )
157{
158    int out_fd = fileno( out_stream );
159
160    fflush( out_stream );
161    if( dup2( dup_fd, out_fd ) == -1 )
162    {
163        close( out_fd );
164        close( dup_fd );
165        return( -1 );
166    }
167
168    close( dup_fd );
169    return( 0 );
170}
171#endif /* __unix__ || __APPLE__ __MACH__ */
172