blob: c9cb22ea1f42562c3a47603f7aa57d6571f46493 [file] [log] [blame]
Paul Bakker33b43f12013-08-20 11:48:36 +02001/* BEGIN_HEADER */
Paul Bakker0e04d0e2011-11-27 14:46:59 +00002#include <polarssl/ctr_drbg.h>
3
Rich Evans00ab4702015-02-06 13:43:58 +00004#include <string.h>
5
Paul Bakker3ddfa662013-11-26 17:45:20 +01006int test_offset_idx;
Paul Bakker69e095c2011-12-10 21:55:01 +00007int entropy_func( void *data, unsigned char *buf, size_t len )
Paul Bakker0e04d0e2011-11-27 14:46:59 +00008{
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +01009 const unsigned char *p = (unsigned char *) data;
Paul Bakker3ddfa662013-11-26 17:45:20 +010010 memcpy( buf, p + test_offset_idx, len );
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +010011 test_offset_idx += len;
Paul Bakker0e04d0e2011-11-27 14:46:59 +000012 return( 0 );
13}
Paul Bakker33b43f12013-08-20 11:48:36 +020014/* END_HEADER */
Paul Bakker0e04d0e2011-11-27 14:46:59 +000015
Paul Bakker33b43f12013-08-20 11:48:36 +020016/* BEGIN_DEPENDENCIES
17 * depends_on:POLARSSL_CTR_DRBG_C
18 * END_DEPENDENCIES
19 */
Paul Bakker0e04d0e2011-11-27 14:46:59 +000020
Paul Bakker33b43f12013-08-20 11:48:36 +020021/* BEGIN_CASE */
22void ctr_drbg_validate_pr( char *add_init_string, char *entropy_string,
23 char *add1_string, char *add2_string,
24 char *result_str )
Paul Bakker0e04d0e2011-11-27 14:46:59 +000025{
26 unsigned char entropy[512];
27 unsigned char add_init[512];
28 unsigned char add1[512];
29 unsigned char add2[512];
30 ctr_drbg_context ctx;
31 unsigned char buf[512];
32 unsigned char output_str[512];
33 int add_init_len, add1_len, add2_len;
34
35 memset( output_str, 0, 512 );
36
Paul Bakker33b43f12013-08-20 11:48:36 +020037 unhexify( entropy, entropy_string );
38 add_init_len = unhexify( add_init, add_init_string );
39 add1_len = unhexify( add1, add1_string );
40 add2_len = unhexify( add2, add2_string );
Paul Bakker0e04d0e2011-11-27 14:46:59 +000041
Paul Bakker3ddfa662013-11-26 17:45:20 +010042 test_offset_idx = 0;
Paul Bakker18d32912011-12-10 21:42:49 +000043 TEST_ASSERT( ctr_drbg_init_entropy_len( &ctx, entropy_func, entropy, add_init, add_init_len, 32 ) == 0 );
Paul Bakker0e04d0e2011-11-27 14:46:59 +000044 ctr_drbg_set_prediction_resistance( &ctx, CTR_DRBG_PR_ON );
45
46 TEST_ASSERT( ctr_drbg_random_with_add( &ctx, buf, 16, add1, add1_len ) == 0 );
47 TEST_ASSERT( ctr_drbg_random_with_add( &ctx, buf, 16, add2, add2_len ) == 0 );
48 hexify( output_str, buf, 16 );
Paul Bakker33b43f12013-08-20 11:48:36 +020049 TEST_ASSERT( strcmp( (char *) output_str, result_str ) == 0 );
Paul Bakkera317a982014-06-18 16:44:11 +020050
Paul Bakkerbd51b262014-07-10 15:26:12 +020051exit:
Paul Bakkera317a982014-06-18 16:44:11 +020052 ctr_drbg_free( &ctx );
Paul Bakker0e04d0e2011-11-27 14:46:59 +000053}
Paul Bakker33b43f12013-08-20 11:48:36 +020054/* END_CASE */
Paul Bakker0e04d0e2011-11-27 14:46:59 +000055
Paul Bakker33b43f12013-08-20 11:48:36 +020056/* BEGIN_CASE */
57void ctr_drbg_validate_nopr( char *add_init_string, char *entropy_string,
58 char *add1_string, char *add_reseed_string,
59 char *add2_string, char *result_str )
Paul Bakker0e04d0e2011-11-27 14:46:59 +000060{
61 unsigned char entropy[512];
62 unsigned char add_init[512];
63 unsigned char add1[512];
64 unsigned char add_reseed[512];
65 unsigned char add2[512];
66 ctr_drbg_context ctx;
67 unsigned char buf[512];
68 unsigned char output_str[512];
69 int add_init_len, add1_len, add_reseed_len, add2_len;
70
71 memset( output_str, 0, 512 );
72
Paul Bakker33b43f12013-08-20 11:48:36 +020073 unhexify( entropy, entropy_string );
74 add_init_len = unhexify( add_init, add_init_string );
75 add1_len = unhexify( add1, add1_string );
76 add_reseed_len = unhexify( add_reseed, add_reseed_string );
77 add2_len = unhexify( add2, add2_string );
Paul Bakker0e04d0e2011-11-27 14:46:59 +000078
Paul Bakker3ddfa662013-11-26 17:45:20 +010079 test_offset_idx = 0;
Paul Bakker18d32912011-12-10 21:42:49 +000080 TEST_ASSERT( ctr_drbg_init_entropy_len( &ctx, entropy_func, entropy, add_init, add_init_len, 32 ) == 0 );
Paul Bakker0e04d0e2011-11-27 14:46:59 +000081
82 TEST_ASSERT( ctr_drbg_random_with_add( &ctx, buf, 16, add1, add1_len ) == 0 );
83 TEST_ASSERT( ctr_drbg_reseed( &ctx, add_reseed, add_reseed_len ) == 0 );
84 TEST_ASSERT( ctr_drbg_random_with_add( &ctx, buf, 16, add2, add2_len ) == 0 );
85 hexify( output_str, buf, 16 );
Paul Bakker33b43f12013-08-20 11:48:36 +020086 TEST_ASSERT( strcmp( (char *) output_str, result_str ) == 0 );
Paul Bakkera317a982014-06-18 16:44:11 +020087
Paul Bakkerbd51b262014-07-10 15:26:12 +020088exit:
Paul Bakkera317a982014-06-18 16:44:11 +020089 ctr_drbg_free( &ctx );
Paul Bakker0e04d0e2011-11-27 14:46:59 +000090}
Paul Bakker33b43f12013-08-20 11:48:36 +020091/* END_CASE */
Manuel Pégourié-Gonnardb3b205e2014-01-31 12:04:06 +010092
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +010093/* BEGIN_CASE */
94void ctr_drbg_entropy_usage( )
95{
96 unsigned char out[16];
97 unsigned char add[16];
98 unsigned char entropy[1024];
99 ctr_drbg_context ctx;
100 size_t i, reps = 10;
101 int last_idx;
102
103 test_offset_idx = 0;
104 memset( entropy, 0, sizeof( entropy ) );
105 memset( out, 0, sizeof( out ) );
106 memset( add, 0, sizeof( add ) );
107
108 /* Init must use entropy */
109 last_idx = test_offset_idx;
110 TEST_ASSERT( ctr_drbg_init( &ctx, entropy_func, entropy, NULL, 0 ) == 0 );
111 TEST_ASSERT( last_idx < test_offset_idx );
112
113 /* By default, PR is off and reseed_interval is large,
114 * so the next few calls should not use entropy */
115 last_idx = test_offset_idx;
116 for( i = 0; i < reps; i++ )
117 {
118 TEST_ASSERT( ctr_drbg_random( &ctx, out, sizeof( out ) - 4 ) == 0 );
119 TEST_ASSERT( ctr_drbg_random_with_add( &ctx, out, sizeof( out ) - 4,
120 add, sizeof( add ) ) == 0 );
121 }
122 TEST_ASSERT( last_idx == test_offset_idx );
123
124 /* While at it, make sure we didn't write past the requested length */
125 TEST_ASSERT( out[sizeof( out ) - 4] == 0 );
126 TEST_ASSERT( out[sizeof( out ) - 3] == 0 );
127 TEST_ASSERT( out[sizeof( out ) - 2] == 0 );
128 TEST_ASSERT( out[sizeof( out ) - 1] == 0 );
129
130 /* Set reseed_interval to the number of calls done,
131 * so the next call should reseed */
132 ctr_drbg_set_reseed_interval( &ctx, 2 * reps );
133 TEST_ASSERT( ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
134 TEST_ASSERT( last_idx < test_offset_idx );
135
136 /* The new few calls should not reseed */
137 last_idx = test_offset_idx;
138 for( i = 0; i < reps / 2; i++ )
139 {
140 TEST_ASSERT( ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
141 TEST_ASSERT( ctr_drbg_random_with_add( &ctx, out, sizeof( out ) ,
142 add, sizeof( add ) ) == 0 );
143 }
144 TEST_ASSERT( last_idx == test_offset_idx );
145
Manuel Pégourié-Gonnardf5f25b32014-11-27 14:04:56 +0100146 /* Call update with too much data (sizeof entropy > MAX(_SEED)_INPUT)
147 * (just make sure it doesn't cause memory corruption) */
148 ctr_drbg_update( &ctx, entropy, sizeof( entropy ) );
149
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100150 /* Now enable PR, so the next few calls should all reseed */
151 ctr_drbg_set_prediction_resistance( &ctx, CTR_DRBG_PR_ON );
152 TEST_ASSERT( ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
153 TEST_ASSERT( last_idx < test_offset_idx );
154
155 /* Finally, check setting entropy_len */
156 ctr_drbg_set_entropy_len( &ctx, 42 );
157 last_idx = test_offset_idx;
158 TEST_ASSERT( ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
159 TEST_ASSERT( test_offset_idx - last_idx == 42 );
160
161 ctr_drbg_set_entropy_len( &ctx, 13 );
162 last_idx = test_offset_idx;
163 TEST_ASSERT( ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
164 TEST_ASSERT( test_offset_idx - last_idx == 13 );
Paul Bakkera317a982014-06-18 16:44:11 +0200165
Paul Bakkerbd51b262014-07-10 15:26:12 +0200166exit:
Paul Bakkera317a982014-06-18 16:44:11 +0200167 ctr_drbg_free( &ctx );
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100168}
169/* END_CASE */
170
171/* BEGIN_CASE depends_on:POLARSSL_FS_IO */
172void ctr_drbg_seed_file( char *path, int ret )
173{
174 ctr_drbg_context ctx;
175
176 TEST_ASSERT( ctr_drbg_init( &ctx, rnd_std_rand, NULL, NULL, 0 ) == 0 );
177 TEST_ASSERT( ctr_drbg_write_seed_file( &ctx, path ) == ret );
178 TEST_ASSERT( ctr_drbg_update_seed_file( &ctx, path ) == ret );
Paul Bakkera317a982014-06-18 16:44:11 +0200179
Paul Bakkerbd51b262014-07-10 15:26:12 +0200180exit:
Paul Bakkera317a982014-06-18 16:44:11 +0200181 ctr_drbg_free( &ctx );
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100182}
183/* END_CASE */
184
Manuel Pégourié-Gonnardb3b205e2014-01-31 12:04:06 +0100185/* BEGIN_CASE depends_on:POLARSSL_SELF_TEST */
186void ctr_drbg_selftest( )
187{
188 TEST_ASSERT( ctr_drbg_self_test( 0 ) == 0 );
189}
190/* END_CASE */