blob: fd9f087f29738b3f4f7d45445217a41d052ee845 [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
Paul Bakkerbd51b262014-07-10 15:26:12 +020049exit:
Paul Bakkera317a982014-06-18 16:44:11 +020050 ctr_drbg_free( &ctx );
Paul Bakker0e04d0e2011-11-27 14:46:59 +000051}
Paul Bakker33b43f12013-08-20 11:48:36 +020052/* END_CASE */
Paul Bakker0e04d0e2011-11-27 14:46:59 +000053
Paul Bakker33b43f12013-08-20 11:48:36 +020054/* BEGIN_CASE */
55void ctr_drbg_validate_nopr( char *add_init_string, char *entropy_string,
56 char *add1_string, char *add_reseed_string,
57 char *add2_string, char *result_str )
Paul Bakker0e04d0e2011-11-27 14:46:59 +000058{
59 unsigned char entropy[512];
60 unsigned char add_init[512];
61 unsigned char add1[512];
62 unsigned char add_reseed[512];
63 unsigned char add2[512];
64 ctr_drbg_context ctx;
65 unsigned char buf[512];
66 unsigned char output_str[512];
67 int add_init_len, add1_len, add_reseed_len, add2_len;
68
69 memset( output_str, 0, 512 );
70
Paul Bakker33b43f12013-08-20 11:48:36 +020071 unhexify( entropy, entropy_string );
72 add_init_len = unhexify( add_init, add_init_string );
73 add1_len = unhexify( add1, add1_string );
74 add_reseed_len = unhexify( add_reseed, add_reseed_string );
75 add2_len = unhexify( add2, add2_string );
Paul Bakker0e04d0e2011-11-27 14:46:59 +000076
Paul Bakker3ddfa662013-11-26 17:45:20 +010077 test_offset_idx = 0;
Paul Bakker18d32912011-12-10 21:42:49 +000078 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 +000079
80 TEST_ASSERT( ctr_drbg_random_with_add( &ctx, buf, 16, add1, add1_len ) == 0 );
81 TEST_ASSERT( ctr_drbg_reseed( &ctx, add_reseed, add_reseed_len ) == 0 );
82 TEST_ASSERT( ctr_drbg_random_with_add( &ctx, buf, 16, add2, add2_len ) == 0 );
83 hexify( output_str, buf, 16 );
Paul Bakker33b43f12013-08-20 11:48:36 +020084 TEST_ASSERT( strcmp( (char *) output_str, result_str ) == 0 );
Paul Bakkera317a982014-06-18 16:44:11 +020085
Paul Bakkerbd51b262014-07-10 15:26:12 +020086exit:
Paul Bakkera317a982014-06-18 16:44:11 +020087 ctr_drbg_free( &ctx );
Paul Bakker0e04d0e2011-11-27 14:46:59 +000088}
Paul Bakker33b43f12013-08-20 11:48:36 +020089/* END_CASE */
Manuel Pégourié-Gonnardb3b205e2014-01-31 12:04:06 +010090
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +010091/* BEGIN_CASE */
92void ctr_drbg_entropy_usage( )
93{
94 unsigned char out[16];
95 unsigned char add[16];
96 unsigned char entropy[1024];
97 ctr_drbg_context ctx;
98 size_t i, reps = 10;
99 int last_idx;
100
101 test_offset_idx = 0;
102 memset( entropy, 0, sizeof( entropy ) );
103 memset( out, 0, sizeof( out ) );
104 memset( add, 0, sizeof( add ) );
105
106 /* Init must use entropy */
107 last_idx = test_offset_idx;
108 TEST_ASSERT( ctr_drbg_init( &ctx, entropy_func, entropy, NULL, 0 ) == 0 );
109 TEST_ASSERT( last_idx < test_offset_idx );
110
111 /* By default, PR is off and reseed_interval is large,
112 * so the next few calls should not use entropy */
113 last_idx = test_offset_idx;
114 for( i = 0; i < reps; i++ )
115 {
116 TEST_ASSERT( ctr_drbg_random( &ctx, out, sizeof( out ) - 4 ) == 0 );
117 TEST_ASSERT( ctr_drbg_random_with_add( &ctx, out, sizeof( out ) - 4,
118 add, sizeof( add ) ) == 0 );
119 }
120 TEST_ASSERT( last_idx == test_offset_idx );
121
122 /* While at it, make sure we didn't write past the requested length */
123 TEST_ASSERT( out[sizeof( out ) - 4] == 0 );
124 TEST_ASSERT( out[sizeof( out ) - 3] == 0 );
125 TEST_ASSERT( out[sizeof( out ) - 2] == 0 );
126 TEST_ASSERT( out[sizeof( out ) - 1] == 0 );
127
128 /* Set reseed_interval to the number of calls done,
129 * so the next call should reseed */
130 ctr_drbg_set_reseed_interval( &ctx, 2 * reps );
131 TEST_ASSERT( ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
132 TEST_ASSERT( last_idx < test_offset_idx );
133
134 /* The new few calls should not reseed */
135 last_idx = test_offset_idx;
136 for( i = 0; i < reps / 2; i++ )
137 {
138 TEST_ASSERT( ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
139 TEST_ASSERT( ctr_drbg_random_with_add( &ctx, out, sizeof( out ) ,
140 add, sizeof( add ) ) == 0 );
141 }
142 TEST_ASSERT( last_idx == test_offset_idx );
143
144 /* Now enable PR, so the next few calls should all reseed */
145 ctr_drbg_set_prediction_resistance( &ctx, CTR_DRBG_PR_ON );
146 TEST_ASSERT( ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
147 TEST_ASSERT( last_idx < test_offset_idx );
148
149 /* Finally, check setting entropy_len */
150 ctr_drbg_set_entropy_len( &ctx, 42 );
151 last_idx = test_offset_idx;
152 TEST_ASSERT( ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
153 TEST_ASSERT( test_offset_idx - last_idx == 42 );
154
155 ctr_drbg_set_entropy_len( &ctx, 13 );
156 last_idx = test_offset_idx;
157 TEST_ASSERT( ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
158 TEST_ASSERT( test_offset_idx - last_idx == 13 );
Paul Bakkera317a982014-06-18 16:44:11 +0200159
Paul Bakkerbd51b262014-07-10 15:26:12 +0200160exit:
Paul Bakkera317a982014-06-18 16:44:11 +0200161 ctr_drbg_free( &ctx );
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100162}
163/* END_CASE */
164
165/* BEGIN_CASE depends_on:POLARSSL_FS_IO */
166void ctr_drbg_seed_file( char *path, int ret )
167{
168 ctr_drbg_context ctx;
169
170 TEST_ASSERT( ctr_drbg_init( &ctx, rnd_std_rand, NULL, NULL, 0 ) == 0 );
171 TEST_ASSERT( ctr_drbg_write_seed_file( &ctx, path ) == ret );
172 TEST_ASSERT( ctr_drbg_update_seed_file( &ctx, path ) == ret );
Paul Bakkera317a982014-06-18 16:44:11 +0200173
Paul Bakkerbd51b262014-07-10 15:26:12 +0200174exit:
Paul Bakkera317a982014-06-18 16:44:11 +0200175 ctr_drbg_free( &ctx );
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100176}
177/* END_CASE */
178
Manuel Pégourié-Gonnardb3b205e2014-01-31 12:04:06 +0100179/* BEGIN_CASE depends_on:POLARSSL_SELF_TEST */
180void ctr_drbg_selftest( )
181{
182 TEST_ASSERT( ctr_drbg_self_test( 0 ) == 0 );
183}
184/* END_CASE */