blob: b3790a2d7aac218383a34a902b6d464ff0e2d55a [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
Paul Bakker3ddfa662013-11-26 17:45:20 +01004int test_offset_idx;
Paul Bakker69e095c2011-12-10 21:55:01 +00005int entropy_func( void *data, unsigned char *buf, size_t len )
Paul Bakker0e04d0e2011-11-27 14:46:59 +00006{
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +01007 const unsigned char *p = (unsigned char *) data;
Paul Bakker3ddfa662013-11-26 17:45:20 +01008 memcpy( buf, p + test_offset_idx, len );
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +01009 test_offset_idx += len;
Paul Bakker0e04d0e2011-11-27 14:46:59 +000010 return( 0 );
11}
Paul Bakker33b43f12013-08-20 11:48:36 +020012/* END_HEADER */
Paul Bakker0e04d0e2011-11-27 14:46:59 +000013
Paul Bakker33b43f12013-08-20 11:48:36 +020014/* BEGIN_DEPENDENCIES
15 * depends_on:POLARSSL_CTR_DRBG_C
16 * END_DEPENDENCIES
17 */
Paul Bakker0e04d0e2011-11-27 14:46:59 +000018
Paul Bakker33b43f12013-08-20 11:48:36 +020019/* BEGIN_CASE */
20void ctr_drbg_validate_pr( char *add_init_string, char *entropy_string,
21 char *add1_string, char *add2_string,
22 char *result_str )
Paul Bakker0e04d0e2011-11-27 14:46:59 +000023{
24 unsigned char entropy[512];
25 unsigned char add_init[512];
26 unsigned char add1[512];
27 unsigned char add2[512];
28 ctr_drbg_context ctx;
29 unsigned char buf[512];
30 unsigned char output_str[512];
31 int add_init_len, add1_len, add2_len;
32
33 memset( output_str, 0, 512 );
34
Paul Bakker33b43f12013-08-20 11:48:36 +020035 unhexify( entropy, entropy_string );
36 add_init_len = unhexify( add_init, add_init_string );
37 add1_len = unhexify( add1, add1_string );
38 add2_len = unhexify( add2, add2_string );
Paul Bakker0e04d0e2011-11-27 14:46:59 +000039
Paul Bakker3ddfa662013-11-26 17:45:20 +010040 test_offset_idx = 0;
Paul Bakker18d32912011-12-10 21:42:49 +000041 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 +000042 ctr_drbg_set_prediction_resistance( &ctx, CTR_DRBG_PR_ON );
43
44 TEST_ASSERT( ctr_drbg_random_with_add( &ctx, buf, 16, add1, add1_len ) == 0 );
45 TEST_ASSERT( ctr_drbg_random_with_add( &ctx, buf, 16, add2, add2_len ) == 0 );
46 hexify( output_str, buf, 16 );
Paul Bakker33b43f12013-08-20 11:48:36 +020047 TEST_ASSERT( strcmp( (char *) output_str, result_str ) == 0 );
Paul Bakkera317a982014-06-18 16:44:11 +020048
49 ctr_drbg_free( &ctx );
Paul Bakker0e04d0e2011-11-27 14:46:59 +000050}
Paul Bakker33b43f12013-08-20 11:48:36 +020051/* END_CASE */
Paul Bakker0e04d0e2011-11-27 14:46:59 +000052
Paul Bakker33b43f12013-08-20 11:48:36 +020053/* BEGIN_CASE */
54void ctr_drbg_validate_nopr( char *add_init_string, char *entropy_string,
55 char *add1_string, char *add_reseed_string,
56 char *add2_string, char *result_str )
Paul Bakker0e04d0e2011-11-27 14:46:59 +000057{
58 unsigned char entropy[512];
59 unsigned char add_init[512];
60 unsigned char add1[512];
61 unsigned char add_reseed[512];
62 unsigned char add2[512];
63 ctr_drbg_context ctx;
64 unsigned char buf[512];
65 unsigned char output_str[512];
66 int add_init_len, add1_len, add_reseed_len, add2_len;
67
68 memset( output_str, 0, 512 );
69
Paul Bakker33b43f12013-08-20 11:48:36 +020070 unhexify( entropy, entropy_string );
71 add_init_len = unhexify( add_init, add_init_string );
72 add1_len = unhexify( add1, add1_string );
73 add_reseed_len = unhexify( add_reseed, add_reseed_string );
74 add2_len = unhexify( add2, add2_string );
Paul Bakker0e04d0e2011-11-27 14:46:59 +000075
Paul Bakker3ddfa662013-11-26 17:45:20 +010076 test_offset_idx = 0;
Paul Bakker18d32912011-12-10 21:42:49 +000077 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 +000078
79 TEST_ASSERT( ctr_drbg_random_with_add( &ctx, buf, 16, add1, add1_len ) == 0 );
80 TEST_ASSERT( ctr_drbg_reseed( &ctx, add_reseed, add_reseed_len ) == 0 );
81 TEST_ASSERT( ctr_drbg_random_with_add( &ctx, buf, 16, add2, add2_len ) == 0 );
82 hexify( output_str, buf, 16 );
Paul Bakker33b43f12013-08-20 11:48:36 +020083 TEST_ASSERT( strcmp( (char *) output_str, result_str ) == 0 );
Paul Bakkera317a982014-06-18 16:44:11 +020084
85 ctr_drbg_free( &ctx );
Paul Bakker0e04d0e2011-11-27 14:46:59 +000086}
Paul Bakker33b43f12013-08-20 11:48:36 +020087/* END_CASE */
Manuel Pégourié-Gonnardb3b205e2014-01-31 12:04:06 +010088
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +010089/* BEGIN_CASE */
90void ctr_drbg_entropy_usage( )
91{
92 unsigned char out[16];
93 unsigned char add[16];
94 unsigned char entropy[1024];
95 ctr_drbg_context ctx;
96 size_t i, reps = 10;
97 int last_idx;
98
99 test_offset_idx = 0;
100 memset( entropy, 0, sizeof( entropy ) );
101 memset( out, 0, sizeof( out ) );
102 memset( add, 0, sizeof( add ) );
103
104 /* Init must use entropy */
105 last_idx = test_offset_idx;
106 TEST_ASSERT( ctr_drbg_init( &ctx, entropy_func, entropy, NULL, 0 ) == 0 );
107 TEST_ASSERT( last_idx < test_offset_idx );
108
109 /* By default, PR is off and reseed_interval is large,
110 * so the next few calls should not use entropy */
111 last_idx = test_offset_idx;
112 for( i = 0; i < reps; i++ )
113 {
114 TEST_ASSERT( ctr_drbg_random( &ctx, out, sizeof( out ) - 4 ) == 0 );
115 TEST_ASSERT( ctr_drbg_random_with_add( &ctx, out, sizeof( out ) - 4,
116 add, sizeof( add ) ) == 0 );
117 }
118 TEST_ASSERT( last_idx == test_offset_idx );
119
120 /* While at it, make sure we didn't write past the requested length */
121 TEST_ASSERT( out[sizeof( out ) - 4] == 0 );
122 TEST_ASSERT( out[sizeof( out ) - 3] == 0 );
123 TEST_ASSERT( out[sizeof( out ) - 2] == 0 );
124 TEST_ASSERT( out[sizeof( out ) - 1] == 0 );
125
126 /* Set reseed_interval to the number of calls done,
127 * so the next call should reseed */
128 ctr_drbg_set_reseed_interval( &ctx, 2 * reps );
129 TEST_ASSERT( ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
130 TEST_ASSERT( last_idx < test_offset_idx );
131
132 /* The new few calls should not reseed */
133 last_idx = test_offset_idx;
134 for( i = 0; i < reps / 2; i++ )
135 {
136 TEST_ASSERT( ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
137 TEST_ASSERT( ctr_drbg_random_with_add( &ctx, out, sizeof( out ) ,
138 add, sizeof( add ) ) == 0 );
139 }
140 TEST_ASSERT( last_idx == test_offset_idx );
141
142 /* Now enable PR, so the next few calls should all reseed */
143 ctr_drbg_set_prediction_resistance( &ctx, CTR_DRBG_PR_ON );
144 TEST_ASSERT( ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
145 TEST_ASSERT( last_idx < test_offset_idx );
146
147 /* Finally, check setting entropy_len */
148 ctr_drbg_set_entropy_len( &ctx, 42 );
149 last_idx = test_offset_idx;
150 TEST_ASSERT( ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
151 TEST_ASSERT( test_offset_idx - last_idx == 42 );
152
153 ctr_drbg_set_entropy_len( &ctx, 13 );
154 last_idx = test_offset_idx;
155 TEST_ASSERT( ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
156 TEST_ASSERT( test_offset_idx - last_idx == 13 );
Paul Bakkera317a982014-06-18 16:44:11 +0200157
158 ctr_drbg_free( &ctx );
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100159}
160/* END_CASE */
161
162/* BEGIN_CASE depends_on:POLARSSL_FS_IO */
163void ctr_drbg_seed_file( char *path, int ret )
164{
165 ctr_drbg_context ctx;
166
167 TEST_ASSERT( ctr_drbg_init( &ctx, rnd_std_rand, NULL, NULL, 0 ) == 0 );
168 TEST_ASSERT( ctr_drbg_write_seed_file( &ctx, path ) == ret );
169 TEST_ASSERT( ctr_drbg_update_seed_file( &ctx, path ) == ret );
Paul Bakkera317a982014-06-18 16:44:11 +0200170
171 ctr_drbg_free( &ctx );
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100172}
173/* END_CASE */
174
Manuel Pégourié-Gonnardb3b205e2014-01-31 12:04:06 +0100175/* BEGIN_CASE depends_on:POLARSSL_SELF_TEST */
176void ctr_drbg_selftest( )
177{
178 TEST_ASSERT( ctr_drbg_self_test( 0 ) == 0 );
179}
180/* END_CASE */