blob: 73f63b9764a11af2e1d230cd39a4e733587908b9 [file] [log] [blame]
Paul Bakker33b43f12013-08-20 11:48:36 +02001/* BEGIN_HEADER */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002#include "mbedtls/ctr_drbg.h"
Mohammad Azim Khan67735d52017-04-06 11:55:43 +01003#include "string.h"
Rich Evans00ab4702015-02-06 13:43:58 +00004
Reut Caspie278b362017-10-19 08:49:19 +01005static int test_offset_idx;
6static int mbedtls_test_entropy_func( void *data, unsigned char *buf, size_t len )
Paul Bakker0e04d0e2011-11-27 14:46:59 +00007{
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +01008 const unsigned char *p = (unsigned char *) data;
Paul Bakker3ddfa662013-11-26 17:45:20 +01009 memcpy( buf, p + test_offset_idx, len );
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +010010 test_offset_idx += len;
Paul Bakker0e04d0e2011-11-27 14:46:59 +000011 return( 0 );
12}
Paul Bakker33b43f12013-08-20 11:48:36 +020013/* END_HEADER */
Paul Bakker0e04d0e2011-11-27 14:46:59 +000014
Paul Bakker33b43f12013-08-20 11:48:36 +020015/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020016 * depends_on:MBEDTLS_CTR_DRBG_C
Paul Bakker33b43f12013-08-20 11:48:36 +020017 * END_DEPENDENCIES
18 */
Paul Bakker0e04d0e2011-11-27 14:46:59 +000019
Paul Bakker33b43f12013-08-20 11:48:36 +020020/* BEGIN_CASE */
Paul Bakker185ccf72016-07-14 13:21:10 +010021void ctr_drbg_special_behaviours( )
22{
23 mbedtls_ctr_drbg_context ctx;
24 unsigned char output[512];
25 unsigned char additional[512];
26
27 mbedtls_ctr_drbg_init( &ctx );
28 memset( output, 0, sizeof( output ) );
29 memset( additional, 0, sizeof( additional ) );
30
31 TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( &ctx,
32 output, MBEDTLS_CTR_DRBG_MAX_REQUEST + 1,
33 additional, 16 ) ==
34 MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG );
35 TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( &ctx,
36 output, 16,
37 additional, MBEDTLS_CTR_DRBG_MAX_INPUT + 1 ) ==
38 MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG );
39
40 TEST_ASSERT( mbedtls_ctr_drbg_reseed( &ctx, additional,
41 MBEDTLS_CTR_DRBG_MAX_SEED_INPUT + 1 ) ==
42 MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG );
Andres Amaya Garcia6a543362017-01-17 23:04:22 +000043
44 mbedtls_ctr_drbg_set_entropy_len( &ctx, ~0 );
45 TEST_ASSERT( mbedtls_ctr_drbg_reseed( &ctx, additional,
46 MBEDTLS_CTR_DRBG_MAX_SEED_INPUT ) ==
47 MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG );
Paul Bakker185ccf72016-07-14 13:21:10 +010048exit:
49 mbedtls_ctr_drbg_free( &ctx );
50}
51/* END_CASE */
52
53/* BEGIN_CASE */
Paul Bakker33b43f12013-08-20 11:48:36 +020054void ctr_drbg_validate_pr( char *add_init_string, char *entropy_string,
55 char *add1_string, char *add2_string,
56 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 add2[512];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062 mbedtls_ctr_drbg_context ctx;
Paul Bakker0e04d0e2011-11-27 14:46:59 +000063 unsigned char buf[512];
64 unsigned char output_str[512];
65 int add_init_len, add1_len, add2_len;
66
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +020067 mbedtls_ctr_drbg_init( &ctx );
Paul Bakker0e04d0e2011-11-27 14:46:59 +000068 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 add2_len = unhexify( add2, add2_string );
Paul Bakker0e04d0e2011-11-27 14:46:59 +000074
Paul Bakker3ddfa662013-11-26 17:45:20 +010075 test_offset_idx = 0;
Reut Caspie278b362017-10-19 08:49:19 +010076 TEST_ASSERT( mbedtls_ctr_drbg_seed_entropy_len( &ctx, mbedtls_test_entropy_func, entropy, add_init, add_init_len, 32 ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077 mbedtls_ctr_drbg_set_prediction_resistance( &ctx, MBEDTLS_CTR_DRBG_PR_ON );
Paul Bakker0e04d0e2011-11-27 14:46:59 +000078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079 TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( &ctx, buf, 16, add1, add1_len ) == 0 );
80 TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( &ctx, buf, 16, add2, add2_len ) == 0 );
Paul Bakker0e04d0e2011-11-27 14:46:59 +000081 hexify( output_str, buf, 16 );
Paul Bakker33b43f12013-08-20 11:48:36 +020082 TEST_ASSERT( strcmp( (char *) output_str, result_str ) == 0 );
Paul Bakkera317a982014-06-18 16:44:11 +020083
Paul Bakkerbd51b262014-07-10 15:26:12 +020084exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085 mbedtls_ctr_drbg_free( &ctx );
Paul Bakker0e04d0e2011-11-27 14:46:59 +000086}
Paul Bakker33b43f12013-08-20 11:48:36 +020087/* END_CASE */
Paul Bakker0e04d0e2011-11-27 14:46:59 +000088
Paul Bakker33b43f12013-08-20 11:48:36 +020089/* BEGIN_CASE */
90void ctr_drbg_validate_nopr( char *add_init_string, char *entropy_string,
91 char *add1_string, char *add_reseed_string,
92 char *add2_string, char *result_str )
Paul Bakker0e04d0e2011-11-27 14:46:59 +000093{
94 unsigned char entropy[512];
95 unsigned char add_init[512];
96 unsigned char add1[512];
97 unsigned char add_reseed[512];
98 unsigned char add2[512];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099 mbedtls_ctr_drbg_context ctx;
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000100 unsigned char buf[512];
101 unsigned char output_str[512];
102 int add_init_len, add1_len, add_reseed_len, add2_len;
103
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200104 mbedtls_ctr_drbg_init( &ctx );
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000105 memset( output_str, 0, 512 );
106
Paul Bakker33b43f12013-08-20 11:48:36 +0200107 unhexify( entropy, entropy_string );
108 add_init_len = unhexify( add_init, add_init_string );
109 add1_len = unhexify( add1, add1_string );
110 add_reseed_len = unhexify( add_reseed, add_reseed_string );
111 add2_len = unhexify( add2, add2_string );
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000112
Paul Bakker3ddfa662013-11-26 17:45:20 +0100113 test_offset_idx = 0;
Reut Caspie278b362017-10-19 08:49:19 +0100114 TEST_ASSERT( mbedtls_ctr_drbg_seed_entropy_len( &ctx, mbedtls_test_entropy_func, entropy, add_init, add_init_len, 32 ) == 0 );
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116 TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( &ctx, buf, 16, add1, add1_len ) == 0 );
117 TEST_ASSERT( mbedtls_ctr_drbg_reseed( &ctx, add_reseed, add_reseed_len ) == 0 );
118 TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( &ctx, buf, 16, add2, add2_len ) == 0 );
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000119 hexify( output_str, buf, 16 );
Paul Bakker33b43f12013-08-20 11:48:36 +0200120 TEST_ASSERT( strcmp( (char *) output_str, result_str ) == 0 );
Paul Bakkera317a982014-06-18 16:44:11 +0200121
Paul Bakkerbd51b262014-07-10 15:26:12 +0200122exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123 mbedtls_ctr_drbg_free( &ctx );
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000124}
Paul Bakker33b43f12013-08-20 11:48:36 +0200125/* END_CASE */
Manuel Pégourié-Gonnardb3b205e2014-01-31 12:04:06 +0100126
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100127/* BEGIN_CASE */
128void ctr_drbg_entropy_usage( )
129{
130 unsigned char out[16];
131 unsigned char add[16];
132 unsigned char entropy[1024];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 mbedtls_ctr_drbg_context ctx;
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100134 size_t i, reps = 10;
135 int last_idx;
136
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200137 mbedtls_ctr_drbg_init( &ctx );
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100138 test_offset_idx = 0;
139 memset( entropy, 0, sizeof( entropy ) );
140 memset( out, 0, sizeof( out ) );
141 memset( add, 0, sizeof( add ) );
142
143 /* Init must use entropy */
144 last_idx = test_offset_idx;
Reut Caspie278b362017-10-19 08:49:19 +0100145 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctx, mbedtls_test_entropy_func, entropy, NULL, 0 ) == 0 );
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100146 TEST_ASSERT( last_idx < test_offset_idx );
147
148 /* By default, PR is off and reseed_interval is large,
149 * so the next few calls should not use entropy */
150 last_idx = test_offset_idx;
151 for( i = 0; i < reps; i++ )
152 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153 TEST_ASSERT( mbedtls_ctr_drbg_random( &ctx, out, sizeof( out ) - 4 ) == 0 );
154 TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( &ctx, out, sizeof( out ) - 4,
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100155 add, sizeof( add ) ) == 0 );
156 }
157 TEST_ASSERT( last_idx == test_offset_idx );
158
159 /* While at it, make sure we didn't write past the requested length */
160 TEST_ASSERT( out[sizeof( out ) - 4] == 0 );
161 TEST_ASSERT( out[sizeof( out ) - 3] == 0 );
162 TEST_ASSERT( out[sizeof( out ) - 2] == 0 );
163 TEST_ASSERT( out[sizeof( out ) - 1] == 0 );
164
165 /* Set reseed_interval to the number of calls done,
166 * so the next call should reseed */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167 mbedtls_ctr_drbg_set_reseed_interval( &ctx, 2 * reps );
168 TEST_ASSERT( mbedtls_ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100169 TEST_ASSERT( last_idx < test_offset_idx );
170
171 /* The new few calls should not reseed */
172 last_idx = test_offset_idx;
173 for( i = 0; i < reps / 2; i++ )
174 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 TEST_ASSERT( mbedtls_ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
176 TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( &ctx, out, sizeof( out ) ,
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100177 add, sizeof( add ) ) == 0 );
178 }
179 TEST_ASSERT( last_idx == test_offset_idx );
180
Manuel Pégourié-Gonnardf5f25b32014-11-27 14:04:56 +0100181 /* Call update with too much data (sizeof entropy > MAX(_SEED)_INPUT)
182 * (just make sure it doesn't cause memory corruption) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183 mbedtls_ctr_drbg_update( &ctx, entropy, sizeof( entropy ) );
Manuel Pégourié-Gonnardf5f25b32014-11-27 14:04:56 +0100184
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100185 /* Now enable PR, so the next few calls should all reseed */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186 mbedtls_ctr_drbg_set_prediction_resistance( &ctx, MBEDTLS_CTR_DRBG_PR_ON );
187 TEST_ASSERT( mbedtls_ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100188 TEST_ASSERT( last_idx < test_offset_idx );
189
190 /* Finally, check setting entropy_len */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191 mbedtls_ctr_drbg_set_entropy_len( &ctx, 42 );
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100192 last_idx = test_offset_idx;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193 TEST_ASSERT( mbedtls_ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100194 TEST_ASSERT( test_offset_idx - last_idx == 42 );
195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196 mbedtls_ctr_drbg_set_entropy_len( &ctx, 13 );
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100197 last_idx = test_offset_idx;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198 TEST_ASSERT( mbedtls_ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100199 TEST_ASSERT( test_offset_idx - last_idx == 13 );
Paul Bakkera317a982014-06-18 16:44:11 +0200200
Paul Bakkerbd51b262014-07-10 15:26:12 +0200201exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202 mbedtls_ctr_drbg_free( &ctx );
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100203}
204/* END_CASE */
205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100207void ctr_drbg_seed_file( char *path, int ret )
208{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 mbedtls_ctr_drbg_context ctx;
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100210
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200211 mbedtls_ctr_drbg_init( &ctx );
212
213 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctx, rnd_std_rand, NULL, NULL, 0 ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214 TEST_ASSERT( mbedtls_ctr_drbg_write_seed_file( &ctx, path ) == ret );
215 TEST_ASSERT( mbedtls_ctr_drbg_update_seed_file( &ctx, path ) == ret );
Paul Bakkera317a982014-06-18 16:44:11 +0200216
Paul Bakkerbd51b262014-07-10 15:26:12 +0200217exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218 mbedtls_ctr_drbg_free( &ctx );
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100219}
220/* END_CASE */
221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Manuel Pégourié-Gonnardb3b205e2014-01-31 12:04:06 +0100223void ctr_drbg_selftest( )
224{
Andres AG93012e82016-09-09 09:10:28 +0100225 TEST_ASSERT( mbedtls_ctr_drbg_self_test( 1 ) == 0 );
Manuel Pégourié-Gonnardb3b205e2014-01-31 12:04:06 +0100226}
227/* END_CASE */