blob: 56906750b9c7780febb33b28642b54955ee4dc1c [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 Bakker0e04d0e2011-11-27 14:46:59 +000048}
Paul Bakker33b43f12013-08-20 11:48:36 +020049/* END_CASE */
Paul Bakker0e04d0e2011-11-27 14:46:59 +000050
Paul Bakker33b43f12013-08-20 11:48:36 +020051/* BEGIN_CASE */
52void ctr_drbg_validate_nopr( char *add_init_string, char *entropy_string,
53 char *add1_string, char *add_reseed_string,
54 char *add2_string, char *result_str )
Paul Bakker0e04d0e2011-11-27 14:46:59 +000055{
56 unsigned char entropy[512];
57 unsigned char add_init[512];
58 unsigned char add1[512];
59 unsigned char add_reseed[512];
60 unsigned char add2[512];
61 ctr_drbg_context ctx;
62 unsigned char buf[512];
63 unsigned char output_str[512];
64 int add_init_len, add1_len, add_reseed_len, add2_len;
65
66 memset( output_str, 0, 512 );
67
Paul Bakker33b43f12013-08-20 11:48:36 +020068 unhexify( entropy, entropy_string );
69 add_init_len = unhexify( add_init, add_init_string );
70 add1_len = unhexify( add1, add1_string );
71 add_reseed_len = unhexify( add_reseed, add_reseed_string );
72 add2_len = unhexify( add2, add2_string );
Paul Bakker0e04d0e2011-11-27 14:46:59 +000073
Paul Bakker3ddfa662013-11-26 17:45:20 +010074 test_offset_idx = 0;
Paul Bakker18d32912011-12-10 21:42:49 +000075 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 +000076
77 TEST_ASSERT( ctr_drbg_random_with_add( &ctx, buf, 16, add1, add1_len ) == 0 );
78 TEST_ASSERT( ctr_drbg_reseed( &ctx, add_reseed, add_reseed_len ) == 0 );
79 TEST_ASSERT( ctr_drbg_random_with_add( &ctx, buf, 16, add2, add2_len ) == 0 );
80 hexify( output_str, buf, 16 );
Paul Bakker33b43f12013-08-20 11:48:36 +020081 TEST_ASSERT( strcmp( (char *) output_str, result_str ) == 0 );
Paul Bakker0e04d0e2011-11-27 14:46:59 +000082}
Paul Bakker33b43f12013-08-20 11:48:36 +020083/* END_CASE */
Manuel Pégourié-Gonnardb3b205e2014-01-31 12:04:06 +010084
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +010085/* BEGIN_CASE */
86void ctr_drbg_entropy_usage( )
87{
88 unsigned char out[16];
89 unsigned char add[16];
90 unsigned char entropy[1024];
91 ctr_drbg_context ctx;
92 size_t i, reps = 10;
93 int last_idx;
94
95 test_offset_idx = 0;
96 memset( entropy, 0, sizeof( entropy ) );
97 memset( out, 0, sizeof( out ) );
98 memset( add, 0, sizeof( add ) );
99
100 /* Init must use entropy */
101 last_idx = test_offset_idx;
102 TEST_ASSERT( ctr_drbg_init( &ctx, entropy_func, entropy, NULL, 0 ) == 0 );
103 TEST_ASSERT( last_idx < test_offset_idx );
104
105 /* By default, PR is off and reseed_interval is large,
106 * so the next few calls should not use entropy */
107 last_idx = test_offset_idx;
108 for( i = 0; i < reps; i++ )
109 {
110 TEST_ASSERT( ctr_drbg_random( &ctx, out, sizeof( out ) - 4 ) == 0 );
111 TEST_ASSERT( ctr_drbg_random_with_add( &ctx, out, sizeof( out ) - 4,
112 add, sizeof( add ) ) == 0 );
113 }
114 TEST_ASSERT( last_idx == test_offset_idx );
115
116 /* While at it, make sure we didn't write past the requested length */
117 TEST_ASSERT( out[sizeof( out ) - 4] == 0 );
118 TEST_ASSERT( out[sizeof( out ) - 3] == 0 );
119 TEST_ASSERT( out[sizeof( out ) - 2] == 0 );
120 TEST_ASSERT( out[sizeof( out ) - 1] == 0 );
121
122 /* Set reseed_interval to the number of calls done,
123 * so the next call should reseed */
124 ctr_drbg_set_reseed_interval( &ctx, 2 * reps );
125 TEST_ASSERT( ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
126 TEST_ASSERT( last_idx < test_offset_idx );
127
128 /* The new few calls should not reseed */
129 last_idx = test_offset_idx;
130 for( i = 0; i < reps / 2; i++ )
131 {
132 TEST_ASSERT( ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
133 TEST_ASSERT( ctr_drbg_random_with_add( &ctx, out, sizeof( out ) ,
134 add, sizeof( add ) ) == 0 );
135 }
136 TEST_ASSERT( last_idx == test_offset_idx );
137
138 /* Now enable PR, so the next few calls should all reseed */
139 ctr_drbg_set_prediction_resistance( &ctx, CTR_DRBG_PR_ON );
140 TEST_ASSERT( ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
141 TEST_ASSERT( last_idx < test_offset_idx );
142
143 /* Finally, check setting entropy_len */
144 ctr_drbg_set_entropy_len( &ctx, 42 );
145 last_idx = test_offset_idx;
146 TEST_ASSERT( ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
147 TEST_ASSERT( test_offset_idx - last_idx == 42 );
148
149 ctr_drbg_set_entropy_len( &ctx, 13 );
150 last_idx = test_offset_idx;
151 TEST_ASSERT( ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
152 TEST_ASSERT( test_offset_idx - last_idx == 13 );
153}
154/* END_CASE */
155
156/* BEGIN_CASE depends_on:POLARSSL_FS_IO */
157void ctr_drbg_seed_file( char *path, int ret )
158{
159 ctr_drbg_context ctx;
160
161 TEST_ASSERT( ctr_drbg_init( &ctx, rnd_std_rand, NULL, NULL, 0 ) == 0 );
162 TEST_ASSERT( ctr_drbg_write_seed_file( &ctx, path ) == ret );
163 TEST_ASSERT( ctr_drbg_update_seed_file( &ctx, path ) == ret );
164}
165/* END_CASE */
166
Manuel Pégourié-Gonnardb3b205e2014-01-31 12:04:06 +0100167/* BEGIN_CASE depends_on:POLARSSL_SELF_TEST */
168void ctr_drbg_selftest( )
169{
170 TEST_ASSERT( ctr_drbg_self_test( 0 ) == 0 );
171}
172/* END_CASE */