File: test_suite_ctr_drbg.function

package info (click to toggle)
0ad 0.0.23.1-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 78,412 kB
  • sloc: cpp: 245,162; ansic: 200,249; javascript: 19,244; python: 13,754; sh: 6,104; perl: 4,620; makefile: 977; xml: 810; java: 533; ruby: 229; erlang: 46; pascal: 30; sql: 21; tcl: 4
file content (193 lines) | stat: -rw-r--r-- 6,802 bytes parent folder | download | duplicates (3)
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
/* BEGIN_HEADER */
#include "mbedtls/ctr_drbg.h"

int test_offset_idx;
int mbedtls_entropy_func( void *data, unsigned char *buf, size_t len )
{
    const unsigned char *p = (unsigned char *) data;
    memcpy( buf, p + test_offset_idx, len );
    test_offset_idx += len;
    return( 0 );
}
/* END_HEADER */

/* BEGIN_DEPENDENCIES
 * depends_on:MBEDTLS_CTR_DRBG_C
 * END_DEPENDENCIES
 */

/* BEGIN_CASE */
void ctr_drbg_validate_pr( char *add_init_string, char *entropy_string,
                           char *add1_string, char *add2_string,
                           char *result_str )
{
    unsigned char entropy[512];
    unsigned char add_init[512];
    unsigned char add1[512];
    unsigned char add2[512];
    mbedtls_ctr_drbg_context ctx;
    unsigned char buf[512];
    unsigned char output_str[512];
    int add_init_len, add1_len, add2_len;

    mbedtls_ctr_drbg_init( &ctx );
    memset( output_str, 0, 512 );

    unhexify( entropy, entropy_string );
    add_init_len = unhexify( add_init, add_init_string );
    add1_len = unhexify( add1, add1_string );
    add2_len = unhexify( add2, add2_string );

    test_offset_idx = 0;
    TEST_ASSERT( mbedtls_ctr_drbg_seed_entropy_len( &ctx, mbedtls_entropy_func, entropy, add_init, add_init_len, 32 ) == 0 );
    mbedtls_ctr_drbg_set_prediction_resistance( &ctx, MBEDTLS_CTR_DRBG_PR_ON );

    TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( &ctx, buf, 16, add1, add1_len ) == 0 );
    TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( &ctx, buf, 16, add2, add2_len ) == 0 );
    hexify( output_str, buf, 16 );
    TEST_ASSERT( strcmp( (char *) output_str, result_str ) == 0 );

exit:
    mbedtls_ctr_drbg_free( &ctx );
}
/* END_CASE */

/* BEGIN_CASE */
void ctr_drbg_validate_nopr( char *add_init_string, char *entropy_string,
                             char *add1_string, char *add_reseed_string,
                             char *add2_string, char *result_str )
{
    unsigned char entropy[512];
    unsigned char add_init[512];
    unsigned char add1[512];
    unsigned char add_reseed[512];
    unsigned char add2[512];
    mbedtls_ctr_drbg_context ctx;
    unsigned char buf[512];
    unsigned char output_str[512];
    int add_init_len, add1_len, add_reseed_len, add2_len;

    mbedtls_ctr_drbg_init( &ctx );
    memset( output_str, 0, 512 );

    unhexify( entropy, entropy_string );
    add_init_len = unhexify( add_init, add_init_string );
    add1_len = unhexify( add1, add1_string );
    add_reseed_len = unhexify( add_reseed, add_reseed_string );
    add2_len = unhexify( add2, add2_string );

    test_offset_idx = 0;
    TEST_ASSERT( mbedtls_ctr_drbg_seed_entropy_len( &ctx, mbedtls_entropy_func, entropy, add_init, add_init_len, 32 ) == 0 );

    TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( &ctx, buf, 16, add1, add1_len ) == 0 );
    TEST_ASSERT( mbedtls_ctr_drbg_reseed( &ctx, add_reseed, add_reseed_len ) == 0 );
    TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( &ctx, buf, 16, add2, add2_len ) == 0 );
    hexify( output_str, buf, 16 );
    TEST_ASSERT( strcmp( (char *) output_str, result_str ) == 0 );

exit:
    mbedtls_ctr_drbg_free( &ctx );
}
/* END_CASE */

/* BEGIN_CASE */
void ctr_drbg_entropy_usage( )
{
    unsigned char out[16];
    unsigned char add[16];
    unsigned char entropy[1024];
    mbedtls_ctr_drbg_context ctx;
    size_t i, reps = 10;
    int last_idx;

    mbedtls_ctr_drbg_init( &ctx );
    test_offset_idx = 0;
    memset( entropy, 0, sizeof( entropy ) );
    memset( out, 0, sizeof( out ) );
    memset( add, 0, sizeof( add ) );

    /* Init must use entropy */
    last_idx = test_offset_idx;
    TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctx, mbedtls_entropy_func, entropy, NULL, 0 ) == 0 );
    TEST_ASSERT( last_idx < test_offset_idx );

    /* By default, PR is off and reseed_interval is large,
     * so the next few calls should not use entropy */
    last_idx = test_offset_idx;
    for( i = 0; i < reps; i++ )
    {
        TEST_ASSERT( mbedtls_ctr_drbg_random( &ctx, out, sizeof( out ) - 4 ) == 0 );
        TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( &ctx, out, sizeof( out ) - 4,
                                                add, sizeof( add ) ) == 0 );
    }
    TEST_ASSERT( last_idx == test_offset_idx );

    /* While at it, make sure we didn't write past the requested length */
    TEST_ASSERT( out[sizeof( out ) - 4] == 0 );
    TEST_ASSERT( out[sizeof( out ) - 3] == 0 );
    TEST_ASSERT( out[sizeof( out ) - 2] == 0 );
    TEST_ASSERT( out[sizeof( out ) - 1] == 0 );

    /* Set reseed_interval to the number of calls done,
     * so the next call should reseed */
    mbedtls_ctr_drbg_set_reseed_interval( &ctx, 2 * reps );
    TEST_ASSERT( mbedtls_ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
    TEST_ASSERT( last_idx < test_offset_idx );

    /* The new few calls should not reseed */
    last_idx = test_offset_idx;
    for( i = 0; i < reps / 2; i++ )
    {
        TEST_ASSERT( mbedtls_ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
        TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( &ctx, out, sizeof( out ) ,
                                                add, sizeof( add ) ) == 0 );
    }
    TEST_ASSERT( last_idx == test_offset_idx );

    /* Call update with too much data (sizeof entropy > MAX(_SEED)_INPUT)
     * (just make sure it doesn't cause memory corruption) */
    mbedtls_ctr_drbg_update( &ctx, entropy, sizeof( entropy ) );

    /* Now enable PR, so the next few calls should all reseed */
    mbedtls_ctr_drbg_set_prediction_resistance( &ctx, MBEDTLS_CTR_DRBG_PR_ON );
    TEST_ASSERT( mbedtls_ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
    TEST_ASSERT( last_idx < test_offset_idx );

    /* Finally, check setting entropy_len */
    mbedtls_ctr_drbg_set_entropy_len( &ctx, 42 );
    last_idx = test_offset_idx;
    TEST_ASSERT( mbedtls_ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
    TEST_ASSERT( test_offset_idx - last_idx == 42 );

    mbedtls_ctr_drbg_set_entropy_len( &ctx, 13 );
    last_idx = test_offset_idx;
    TEST_ASSERT( mbedtls_ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
    TEST_ASSERT( test_offset_idx - last_idx == 13 );

exit:
    mbedtls_ctr_drbg_free( &ctx );
}
/* END_CASE */

/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
void ctr_drbg_seed_file( char *path, int ret )
{
    mbedtls_ctr_drbg_context ctx;

    mbedtls_ctr_drbg_init( &ctx );

    TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctx, rnd_std_rand, NULL, NULL, 0 ) == 0 );
    TEST_ASSERT( mbedtls_ctr_drbg_write_seed_file( &ctx, path ) == ret );
    TEST_ASSERT( mbedtls_ctr_drbg_update_seed_file( &ctx, path ) == ret );

exit:
    mbedtls_ctr_drbg_free( &ctx );
}
/* END_CASE */

/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
void ctr_drbg_selftest( )
{
    TEST_ASSERT( mbedtls_ctr_drbg_self_test( 0 ) == 0 );
}
/* END_CASE */