blob: efa5161f707afaea12c62133379aa1472ec117d4 [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 */
Azim Khanf1aaec92017-05-30 14:23:15 +010021void ctr_drbg_special_behaviours( )
Paul Bakker185ccf72016-07-14 13:21:10 +010022{
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 */
Azim Khan5fcca462018-06-29 11:05:32 +010054void ctr_drbg_validate_pr( data_t * add_init, data_t * entropy,
55 data_t * add1, data_t * add2,
56 data_t * result_str )
Paul Bakker0e04d0e2011-11-27 14:46:59 +000057{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058 mbedtls_ctr_drbg_context ctx;
Paul Bakker0e04d0e2011-11-27 14:46:59 +000059 unsigned char buf[512];
Paul Bakker0e04d0e2011-11-27 14:46:59 +000060
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +020061 mbedtls_ctr_drbg_init( &ctx );
Paul Bakker0e04d0e2011-11-27 14:46:59 +000062
Paul Bakker0e04d0e2011-11-27 14:46:59 +000063
Paul Bakker3ddfa662013-11-26 17:45:20 +010064 test_offset_idx = 0;
Gilles Peskineed7da592018-08-03 20:16:52 +020065 /* CTR_DRBG_Instantiate(entropy[:entropy_len/3], nonce, perso, <ignored>)
66 * where nonce||perso = add_init[add_init_len] */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067 mbedtls_ctr_drbg_set_prediction_resistance( &ctx, MBEDTLS_CTR_DRBG_PR_ON );
Paul Bakker0e04d0e2011-11-27 14:46:59 +000068
Gilles Peskineed7da592018-08-03 20:16:52 +020069 /* CTR_DRBG_Generate(result_len * 8 bits, add1[:add1_len]) -> buf */
70 /* Then reseed because of prediction resistance. */
Azim Khand30ca132017-06-09 04:32:58 +010071 TEST_ASSERT( hexcmp( buf, result_str->x, 16, result_str->len ) == 0 );
Gilles Peskineed7da592018-08-03 20:16:52 +020072 /* CTR_DRBG_Generate(result_len * 8 bits, add2[:add2_len]) -> buf */
73 /* Then reseed because of prediction resistance. */
Paul Bakkera317a982014-06-18 16:44:11 +020074
Paul Bakkerbd51b262014-07-10 15:26:12 +020075exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076 mbedtls_ctr_drbg_free( &ctx );
Paul Bakker0e04d0e2011-11-27 14:46:59 +000077}
Paul Bakker33b43f12013-08-20 11:48:36 +020078/* END_CASE */
Paul Bakker0e04d0e2011-11-27 14:46:59 +000079
Paul Bakker33b43f12013-08-20 11:48:36 +020080/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +010081void ctr_drbg_validate_nopr( data_t * add_init, data_t * entropy,
82 data_t * add1, data_t * add_reseed,
83 data_t * add2, data_t * result_str )
Paul Bakker0e04d0e2011-11-27 14:46:59 +000084{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085 mbedtls_ctr_drbg_context ctx;
Paul Bakker0e04d0e2011-11-27 14:46:59 +000086 unsigned char buf[512];
Paul Bakker0e04d0e2011-11-27 14:46:59 +000087
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +020088 mbedtls_ctr_drbg_init( &ctx );
Paul Bakker0e04d0e2011-11-27 14:46:59 +000089
Paul Bakker0e04d0e2011-11-27 14:46:59 +000090
Paul Bakker3ddfa662013-11-26 17:45:20 +010091 test_offset_idx = 0;
Gilles Peskineed7da592018-08-03 20:16:52 +020092 /* CTR_DRBG_Instantiate(entropy[:entropy_len/2], nonce, perso, <ignored>)
93 * where nonce||perso = add_init[add_init_len] */
Paul Bakker0e04d0e2011-11-27 14:46:59 +000094
Gilles Peskineed7da592018-08-03 20:16:52 +020095 /* CTR_DRBG_Generate(16 * 8 bits, add1[:add1_len]) -> buf */
Azim Khand30ca132017-06-09 04:32:58 +010096 TEST_ASSERT( mbedtls_ctr_drbg_reseed( &ctx, add_reseed->x, add_reseed->len ) == 0 );
Gilles Peskineed7da592018-08-03 20:16:52 +020097 /* CTR_DRBG_Reseed(entropy[entropy_len/2:entropy_len], add_reseed[:add_reseed_len]) */
Azim Khand30ca132017-06-09 04:32:58 +010098 TEST_ASSERT( hexcmp( buf, result_str->x, 16, result_str->len ) == 0 );
Gilles Peskineed7da592018-08-03 20:16:52 +020099 /* CTR_DRBG_Generate(16 * 8 bits, add2[:add2_len]) -> buf */
Paul Bakkera317a982014-06-18 16:44:11 +0200100
Paul Bakkerbd51b262014-07-10 15:26:12 +0200101exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102 mbedtls_ctr_drbg_free( &ctx );
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000103}
Paul Bakker33b43f12013-08-20 11:48:36 +0200104/* END_CASE */
Manuel Pégourié-Gonnardb3b205e2014-01-31 12:04:06 +0100105
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100106/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100107void ctr_drbg_entropy_usage( )
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100108{
109 unsigned char out[16];
110 unsigned char add[16];
111 unsigned char entropy[1024];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112 mbedtls_ctr_drbg_context ctx;
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100113 size_t i, reps = 10;
114 int last_idx;
115
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200116 mbedtls_ctr_drbg_init( &ctx );
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100117 test_offset_idx = 0;
118 memset( entropy, 0, sizeof( entropy ) );
119 memset( out, 0, sizeof( out ) );
120 memset( add, 0, sizeof( add ) );
121
122 /* Init must use entropy */
123 last_idx = test_offset_idx;
Reut Caspie278b362017-10-19 08:49:19 +0100124 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctx, mbedtls_test_entropy_func, entropy, NULL, 0 ) == 0 );
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100125 TEST_ASSERT( last_idx < test_offset_idx );
126
127 /* By default, PR is off and reseed_interval is large,
128 * so the next few calls should not use entropy */
129 last_idx = test_offset_idx;
130 for( i = 0; i < reps; i++ )
131 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132 TEST_ASSERT( mbedtls_ctr_drbg_random( &ctx, out, sizeof( out ) - 4 ) == 0 );
133 TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( &ctx, out, sizeof( out ) - 4,
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100134 add, sizeof( add ) ) == 0 );
135 }
136 TEST_ASSERT( last_idx == test_offset_idx );
137
138 /* While at it, make sure we didn't write past the requested length */
139 TEST_ASSERT( out[sizeof( out ) - 4] == 0 );
140 TEST_ASSERT( out[sizeof( out ) - 3] == 0 );
141 TEST_ASSERT( out[sizeof( out ) - 2] == 0 );
142 TEST_ASSERT( out[sizeof( out ) - 1] == 0 );
143
144 /* Set reseed_interval to the number of calls done,
145 * so the next call should reseed */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146 mbedtls_ctr_drbg_set_reseed_interval( &ctx, 2 * reps );
147 TEST_ASSERT( mbedtls_ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100148 TEST_ASSERT( last_idx < test_offset_idx );
149
150 /* The new few calls should not reseed */
151 last_idx = test_offset_idx;
152 for( i = 0; i < reps / 2; i++ )
153 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154 TEST_ASSERT( mbedtls_ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
155 TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( &ctx, out, sizeof( out ) ,
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100156 add, sizeof( add ) ) == 0 );
157 }
158 TEST_ASSERT( last_idx == test_offset_idx );
159
Manuel Pégourié-Gonnardf5f25b32014-11-27 14:04:56 +0100160 /* Call update with too much data (sizeof entropy > MAX(_SEED)_INPUT)
161 * (just make sure it doesn't cause memory corruption) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162 mbedtls_ctr_drbg_update( &ctx, entropy, sizeof( entropy ) );
Manuel Pégourié-Gonnardf5f25b32014-11-27 14:04:56 +0100163
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100164 /* Now enable PR, so the next few calls should all reseed */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165 mbedtls_ctr_drbg_set_prediction_resistance( &ctx, MBEDTLS_CTR_DRBG_PR_ON );
166 TEST_ASSERT( mbedtls_ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100167 TEST_ASSERT( last_idx < test_offset_idx );
168
169 /* Finally, check setting entropy_len */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170 mbedtls_ctr_drbg_set_entropy_len( &ctx, 42 );
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100171 last_idx = test_offset_idx;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172 TEST_ASSERT( mbedtls_ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100173 TEST_ASSERT( test_offset_idx - last_idx == 42 );
174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 mbedtls_ctr_drbg_set_entropy_len( &ctx, 13 );
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100176 last_idx = test_offset_idx;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177 TEST_ASSERT( mbedtls_ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100178 TEST_ASSERT( test_offset_idx - last_idx == 13 );
Paul Bakkera317a982014-06-18 16:44:11 +0200179
Paul Bakkerbd51b262014-07-10 15:26:12 +0200180exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181 mbedtls_ctr_drbg_free( &ctx );
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100182}
183/* END_CASE */
184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Azim Khanf1aaec92017-05-30 14:23:15 +0100186void ctr_drbg_seed_file( char * path, int ret )
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100187{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188 mbedtls_ctr_drbg_context ctx;
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100189
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200190 mbedtls_ctr_drbg_init( &ctx );
191
192 TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctx, rnd_std_rand, NULL, NULL, 0 ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193 TEST_ASSERT( mbedtls_ctr_drbg_write_seed_file( &ctx, path ) == ret );
194 TEST_ASSERT( mbedtls_ctr_drbg_update_seed_file( &ctx, path ) == ret );
Paul Bakkera317a982014-06-18 16:44:11 +0200195
Paul Bakkerbd51b262014-07-10 15:26:12 +0200196exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197 mbedtls_ctr_drbg_free( &ctx );
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100198}
199/* END_CASE */
200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +0100202void ctr_drbg_selftest( )
Manuel Pégourié-Gonnardb3b205e2014-01-31 12:04:06 +0100203{
Andres AG93012e82016-09-09 09:10:28 +0100204 TEST_ASSERT( mbedtls_ctr_drbg_self_test( 1 ) == 0 );
Manuel Pégourié-Gonnardb3b205e2014-01-31 12:04:06 +0100205}
206/* END_CASE */