aboutsummaryrefslogtreecommitdiffstats
path: root/roms/skiboot/libstb/crypto/mbedtls/tests/suites/target_test.function
blob: 56abf294894d9b245bb0645d86dc36651c3dd138 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
#line 2 "suites/target_test.function"

#include "greentea-client/test_env.h"

/**
 * \brief       Increments pointer and asserts that it does not overflow.
 *
 * \param p     Pointer to byte array
 * \param start Pointer to start of byte array
 * \param len   Length of byte array
 * \param step  Increment size
 *
 */
#define INCR_ASSERT(p, start, len, step) do                     \
{                                                               \
    assert( ( p ) >= ( start ) );                               \
    assert( sizeof( *( p ) ) == sizeof( *( start ) ) );         \
    /* <= is checked to support use inside a loop where         \
       pointer is incremented after reading data.       */      \
    assert( (uint32_t)( ( ( p ) - ( start ) ) + ( step ) ) <= ( len ) );\
    ( p ) += ( step );                                          \
}                                                               \
while( 0 )


/**
 * \brief       4 byte align unsigned char pointer
 *
 * \param p     Pointer to byte array
 * \param start Pointer to start of byte array
 * \param len   Length of byte array
 *
 */
#define ALIGN_32BIT(p, start, len) do               \
{                                                   \
    uint32_t align = ( - (uintptr_t)( p ) ) % 4;    \
    INCR_ASSERT( ( p ), ( start ), ( len ), align );\
}                                                   \
while( 0 )


/**
 * \brief       Verify dependencies. Dependency identifiers are
 *              encoded in the buffer as 8 bit unsigned integers.
 *
 * \param count     Number of dependencies.
 * \param dep_p     Pointer to buffer.
 *
 * \return          DEPENDENCY_SUPPORTED if success else DEPENDENCY_NOT_SUPPORTED.
 */
int verify_dependencies( uint8_t count, uint8_t * dep_p )
{
    uint8_t i;
    for ( i = 0; i < count; i++ )
    {
        if ( dep_check( (int)(dep_p[i]) ) != DEPENDENCY_SUPPORTED )
            return( DEPENDENCY_NOT_SUPPORTED );
    }
    return( DEPENDENCY_SUPPORTED );
}


/**
 * \brief       Receives unsigned integer on serial interface.
 *              Integers are encoded in network order.
 *
 * \param none
 *
 * \return      unsigned int
 */
uint32_t receive_uint32()
{
    uint32_t value;
    value =  (uint8_t)greentea_getc() << 24;
    value |= (uint8_t)greentea_getc() << 16;
    value |= (uint8_t)greentea_getc() << 8;
    value |= (uint8_t)greentea_getc();
    return( (uint32_t)value );
}

/**
 * \brief       Parses out an unsigned 32 int value from the byte array.
 *              Integers are encoded in network order.
 *
 * \param p     Pointer to byte array
 *
 * \return      unsigned int
 */
uint32_t parse_uint32( uint8_t * p )
{
    uint32_t value;
    value =  *p++ << 24;
    value |= *p++ << 16;
    value |= *p++ << 8;
    value |= *p;
    return( value );
}


/**
 * \brief       Receives test data on serial as greentea key,value pair:
 *              {{<length>;<byte array>}}
 *
 * \param data_len  Out pointer to hold received data length.
 *
 * \return      Byte array.
 */
uint8_t * receive_data( uint32_t * data_len )
{
    uint32_t i = 0, errors = 0;
    char c;
    uint8_t * data = NULL;

    /* Read opening braces */
    i = 0;
    while ( i < 2 )
    {
        c = greentea_getc();
        /* Ignore any prevous CR LF characters */
        if ( c == '\n' || c == '\r' )
            continue;
        i++;
        if ( c != '{' )
            return( NULL );
    }

    /* Read data length */
    *data_len = receive_uint32();
    data = (uint8_t *)malloc( *data_len );
    assert( data != NULL );

    greentea_getc(); // read ';' received after key i.e. *data_len

    for( i = 0; i < *data_len; i++ )
        data[i] = greentea_getc();

    /* Read closing braces */
    for( i = 0; i < 2; i++ )
    {
        c = greentea_getc();
        if ( c != '}' )
        {
            errors++;
            break;
        }
    }

    if ( errors )
    {
        free( data );
        data = NULL;
        *data_len = 0;
    }

    return( data );
}

/**
 * \brief       Parse the received byte array and count the number of arguments
 *              to the test function passed as type hex.
 *
 * \param count     Parameter count
 * \param data      Received Byte array
 * \param data_len  Byte array length
 *
 * \return      count of hex params
 */
uint32_t find_hex_count( uint8_t count, uint8_t * data, uint32_t data_len )
{
    uint32_t i = 0, sz = 0;
    char c;
    uint8_t * p = NULL;
    uint32_t hex_count = 0;

    p = data;

    for( i = 0; i < count; i++ )
    {
        c = (char)*p;
        INCR_ASSERT( p, data, data_len, 1 );

        /* Align p to 4 bytes for int, expression, string len or hex length */
        ALIGN_32BIT( p, data, data_len );

        /* Network to host conversion */
        sz = (int32_t)parse_uint32( p );

        INCR_ASSERT( p, data, data_len, sizeof( int32_t ) );

        if ( c == 'H' || c == 'S' )
        {
            INCR_ASSERT( p, data, data_len, sz );
            hex_count += ( c == 'H' )?1:0;
        }
    }

    return( hex_count );
}

/**
 * \brief       Parses received byte array for test parameters.
 *
 * \param count     Parameter count
 * \param data      Received Byte array
 * \param data_len  Byte array length
 * \param error     Parsing error out variable.
 *
 * \return      Array of parsed parameters allocated on heap.
 *              Note: Caller has the responsibility to delete
 *                    the memory after use.
 */
void ** parse_parameters( uint8_t count, uint8_t * data, uint32_t data_len,
                          int * error )
{
    uint32_t i = 0, hex_count = 0;
    char c;
    void ** params = NULL;
    void ** cur = NULL;
    uint8_t * p = NULL;

    hex_count = find_hex_count(count, data, data_len);

    params = (void **)malloc( sizeof( void *) * ( count + hex_count ) );
    assert( params != NULL );
    cur = params;

    p = data;

    /* Parameters */
    for( i = 0; i < count; i++ )
    {
        c = (char)*p;
        INCR_ASSERT( p, data, data_len, 1 );

        /* Align p to 4 bytes for int, expression, string len or hex length */
        ALIGN_32BIT( p, data, data_len );

        /* Network to host conversion */
        *( (int32_t *)p ) = (int32_t)parse_uint32( p );

        switch( c )
        {
            case 'E':
                {
                    if ( get_expression( *( (int32_t *)p ), (int32_t *)p ) )
                    {
                        *error = KEY_VALUE_MAPPING_NOT_FOUND;
                        goto exit;
                    }
                } /* Intentional fall through */
            case 'I':
                {
                    *cur++ = (void *)p;
                    INCR_ASSERT( p, data, data_len, sizeof( int32_t ) );
                }
                break;
            case 'H': /* Intentional fall through */
            case 'S':
                {
                    uint32_t * sz = (uint32_t *)p;
                    INCR_ASSERT( p, data, data_len, sizeof( int32_t ) );
                    *cur++ = (void *)p;
                    if ( c == 'H' )
                        *cur++ = (void *)sz;
                    INCR_ASSERT( p, data, data_len, ( *sz ) );
                }
                break;
            default:
                    {
                        *error = DISPATCH_INVALID_TEST_DATA;
                        goto exit;
                    }
                break;
        }
    }

exit:
    if ( *error )
    {
        free( params );
        params = NULL;
    }

    return( params );
}

/**
 * \brief       Sends greentea key and int value pair to host.
 *
 * \param key   key string
 * \param value integer value
 *
 * \return      void
 */
void send_key_integer( char * key, int value )
{
    char str[50];
    snprintf( str, sizeof( str ), "%d", value );
    greentea_send_kv( key, str );
}

/**
 * \brief       Sends test setup failure to the host.
 *
 * \param failure   Test set failure
 *
 * \return      void
 */
void send_failure( int failure )
{
    send_key_integer( "F", failure );
}

/**
 * \brief       Sends test status to the host.
 *
 * \param status    Test status (PASS=0/FAIL=!0)
 *
 * \return      void
 */
void send_status( int status )
{
    send_key_integer( "R", status );
}


/**
 * \brief       Embedded implementation of execute_tests().
 *              Ignores command line and received test data
 *              on serial.
 *
 * \param argc  not used
 * \param argv  not used
 *
 * \return      Program exit status.
 */
int execute_tests( int args, const char ** argv )
{
    int ret = 0;
    uint32_t data_len = 0;
    uint8_t count = 0, function_id;
    void ** params = NULL;
    uint8_t * data = NULL, * p = NULL;

    GREENTEA_SETUP( 180, "mbedtls_test" );
    greentea_send_kv( "GO", " " );

    while ( 1 )
    {
        ret = 0;
        test_info.failed = 0;
        data_len = 0;

        data = receive_data( &data_len );
        if ( data == NULL )
            continue;
        p = data;

        do
        {
            /* Read dependency count */
            count = *p;
            assert( count < data_len );
            INCR_ASSERT( p, data, data_len, sizeof( uint8_t ) );
            ret = verify_dependencies( count, p );
            if ( ret != DEPENDENCY_SUPPORTED )
                break;

            if ( count )
                INCR_ASSERT( p, data, data_len, count );

            /* Read function id */
            function_id = *p;
            INCR_ASSERT( p, data, data_len, sizeof( uint8_t ) );
            if ( ( ret = check_test( function_id ) ) != DISPATCH_TEST_SUCCESS )
                break;

            /* Read number of parameters */
            count = *p;
            INCR_ASSERT( p, data, data_len, sizeof( uint8_t ) );

            /* Parse parameters if present */
            if ( count )
            {
                params = parse_parameters( count, p, data_len - ( p - data ), &ret );
                if ( ret )
                    break;
            }

            ret = dispatch_test( function_id, params );
        }
        while ( 0 );

        if ( data )
        {
            free( data );
            data = NULL;
        }

        if ( params )
        {
            free( params );
            params = NULL;
        }

        if ( ret )
            send_failure( ret );
        else
            send_status( test_info.failed );
    }
    return( 0 );
}