blob: b83d760d22d4fee840d0eeb20410ec7064b4eb3c [file] [log] [blame]
Manuel Pégourié-Gonnard6801f392014-01-30 17:22:14 +01001/* BEGIN_HEADER */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002#include "mbedtls/hmac_drbg.h"
Mohammad Azim Khan67735d52017-04-06 11:55:43 +01003#include "string.h"
Rich Evans00ab4702015-02-06 13:43:58 +00004
Manuel Pégourié-Gonnard6801f392014-01-30 17:22:14 +01005typedef struct
6{
7 unsigned char *p;
8 size_t len;
9} entropy_ctx;
10
Reut Caspie278b362017-10-19 08:49:19 +010011static int mbedtls_test_entropy_func( void *data, unsigned char *buf, size_t len )
Manuel Pégourié-Gonnard6801f392014-01-30 17:22:14 +010012{
13 entropy_ctx *ctx = (entropy_ctx *) data;
14
15 if( len > ctx->len )
16 return( -1 );
17
18 memcpy( buf, ctx->p, len );
19
20 ctx->p += len;
21 ctx->len -= len;
22
23 return( 0 );
24}
25/* END_HEADER */
26
27/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028 * depends_on:MBEDTLS_HMAC_DRBG_C
Manuel Pégourié-Gonnard6801f392014-01-30 17:22:14 +010029 * END_DEPENDENCIES
30 */
31
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +010032/* BEGIN_CASE */
33void hmac_drbg_entropy_usage( int md_alg )
34{
35 unsigned char out[16];
36 unsigned char buf[1024];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037 const mbedtls_md_info_t *md_info;
38 mbedtls_hmac_drbg_context ctx;
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +010039 entropy_ctx entropy;
Gilles Peskine4d2d4ff2019-10-22 19:10:33 +020040 size_t i, reps = 10;
41 size_t default_entropy_len;
42 size_t expected_consumed_entropy = 0;
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +010043
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +020044 mbedtls_hmac_drbg_init( &ctx );
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +010045 memset( buf, 0, sizeof( buf ) );
46 memset( out, 0, sizeof( out ) );
47
48 entropy.len = sizeof( buf );
49 entropy.p = buf;
50
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakker94b916c2014-04-17 16:07:20 +020052 TEST_ASSERT( md_info != NULL );
Gilles Peskine4d2d4ff2019-10-22 19:10:33 +020053 if( mbedtls_md_get_size( md_info ) <= 20 )
54 default_entropy_len = 16;
55 else if( mbedtls_md_get_size( md_info ) <= 28 )
56 default_entropy_len = 24;
57 else
58 default_entropy_len = 32;
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +010059
Gavin Acquroff6aceb512020-03-01 17:06:11 -080060 /* Set reseed interval before seed */
61 mbedtls_hmac_drbg_set_reseed_interval( &ctx, 2 * reps );
62
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +010063 /* Init must use entropy */
Reut Caspie278b362017-10-19 08:49:19 +010064 TEST_ASSERT( mbedtls_hmac_drbg_seed( &ctx, md_info, mbedtls_test_entropy_func, &entropy,
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +010065 NULL, 0 ) == 0 );
Gilles Peskine4d2d4ff2019-10-22 19:10:33 +020066 /* default_entropy_len of entropy, plus half as much for the nonce */
67 expected_consumed_entropy += default_entropy_len * 3 / 2;
68 TEST_EQUAL( sizeof( buf ) - entropy.len, expected_consumed_entropy );
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +010069
Gavin Acquroff6aceb512020-03-01 17:06:11 -080070 /* By default, PR is off, and reseed interval was set to
71 * 2 * reps so the next few calls should not use entropy */
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +010072 for( i = 0; i < reps; i++ )
73 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074 TEST_ASSERT( mbedtls_hmac_drbg_random( &ctx, out, sizeof( out ) - 4 ) == 0 );
75 TEST_ASSERT( mbedtls_hmac_drbg_random_with_add( &ctx, out, sizeof( out ) - 4,
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +010076 buf, 16 ) == 0 );
77 }
Gilles Peskine4d2d4ff2019-10-22 19:10:33 +020078 TEST_EQUAL( sizeof( buf ) - entropy.len, expected_consumed_entropy );
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +010079
80 /* While at it, make sure we didn't write past the requested length */
81 TEST_ASSERT( out[sizeof( out ) - 4] == 0 );
82 TEST_ASSERT( out[sizeof( out ) - 3] == 0 );
83 TEST_ASSERT( out[sizeof( out ) - 2] == 0 );
84 TEST_ASSERT( out[sizeof( out ) - 1] == 0 );
85
Gavin Acquroff6aceb512020-03-01 17:06:11 -080086 /* There have been 2 * reps calls to random. The next call should reseed */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087 TEST_ASSERT( mbedtls_hmac_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
Gilles Peskine4d2d4ff2019-10-22 19:10:33 +020088 expected_consumed_entropy += default_entropy_len;
89 TEST_EQUAL( sizeof( buf ) - entropy.len, expected_consumed_entropy );
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +010090
Gavin Acquroff6aceb512020-03-01 17:06:11 -080091 /* Set reseed interval after seed */
92 mbedtls_hmac_drbg_set_reseed_interval( &ctx, 4 * reps + 1);
93
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +010094 /* The new few calls should not reseed */
Gavin Acquroff6aceb512020-03-01 17:06:11 -080095 for( i = 0; i < (2 * reps); i++ )
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +010096 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097 TEST_ASSERT( mbedtls_hmac_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
98 TEST_ASSERT( mbedtls_hmac_drbg_random_with_add( &ctx, out, sizeof( out ) ,
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +010099 buf, 16 ) == 0 );
100 }
Gilles Peskine4d2d4ff2019-10-22 19:10:33 +0200101 TEST_EQUAL( sizeof( buf ) - entropy.len, expected_consumed_entropy );
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +0100102
103 /* Now enable PR, so the next few calls should all reseed */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104 mbedtls_hmac_drbg_set_prediction_resistance( &ctx, MBEDTLS_HMAC_DRBG_PR_ON );
105 TEST_ASSERT( mbedtls_hmac_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
Gilles Peskine4d2d4ff2019-10-22 19:10:33 +0200106 expected_consumed_entropy += default_entropy_len;
107 TEST_EQUAL( sizeof( buf ) - entropy.len, expected_consumed_entropy );
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +0100108
109 /* Finally, check setting entropy_len */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110 mbedtls_hmac_drbg_set_entropy_len( &ctx, 42 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111 TEST_ASSERT( mbedtls_hmac_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
Gilles Peskine4d2d4ff2019-10-22 19:10:33 +0200112 expected_consumed_entropy += 42;
113 TEST_EQUAL( sizeof( buf ) - entropy.len, expected_consumed_entropy );
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +0100114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115 mbedtls_hmac_drbg_set_entropy_len( &ctx, 13 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116 TEST_ASSERT( mbedtls_hmac_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
Gilles Peskine4d2d4ff2019-10-22 19:10:33 +0200117 expected_consumed_entropy += 13;
118 TEST_EQUAL( sizeof( buf ) - entropy.len, expected_consumed_entropy );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200119
120exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121 mbedtls_hmac_drbg_free( &ctx );
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +0100122}
123/* END_CASE */
124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Azim Khanf1aaec92017-05-30 14:23:15 +0100126void hmac_drbg_seed_file( int md_alg, char * path, int ret )
Manuel Pégourié-Gonnard48bc3e82014-01-30 21:11:16 +0100127{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 const mbedtls_md_info_t *md_info;
129 mbedtls_hmac_drbg_context ctx;
Manuel Pégourié-Gonnard48bc3e82014-01-30 21:11:16 +0100130
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200131 mbedtls_hmac_drbg_init( &ctx );
132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakker94b916c2014-04-17 16:07:20 +0200134 TEST_ASSERT( md_info != NULL );
135
Ronald Cron6c5bd7f2020-06-10 14:08:26 +0200136 TEST_ASSERT( mbedtls_hmac_drbg_seed( &ctx, md_info,
137 mbedtls_test_rnd_std_rand, NULL,
138 NULL, 0 ) == 0 );
Manuel Pégourié-Gonnard48bc3e82014-01-30 21:11:16 +0100139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 TEST_ASSERT( mbedtls_hmac_drbg_write_seed_file( &ctx, path ) == ret );
141 TEST_ASSERT( mbedtls_hmac_drbg_update_seed_file( &ctx, path ) == ret );
Manuel Pégourié-Gonnard48bc3e82014-01-30 21:11:16 +0100142
Paul Bakkerbd51b262014-07-10 15:26:12 +0200143exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 mbedtls_hmac_drbg_free( &ctx );
Manuel Pégourié-Gonnard48bc3e82014-01-30 21:11:16 +0100145}
146/* END_CASE */
147
Manuel Pégourié-Gonnard6801f392014-01-30 17:22:14 +0100148/* BEGIN_CASE */
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +0100149void hmac_drbg_buf( int md_alg )
150{
151 unsigned char out[16];
152 unsigned char buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153 const mbedtls_md_info_t *md_info;
154 mbedtls_hmac_drbg_context ctx;
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +0100155 size_t i;
156
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200157 mbedtls_hmac_drbg_init( &ctx );
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +0100158 memset( buf, 0, sizeof( buf ) );
159 memset( out, 0, sizeof( out ) );
160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakker94b916c2014-04-17 16:07:20 +0200162 TEST_ASSERT( md_info != NULL );
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200163 TEST_ASSERT( mbedtls_hmac_drbg_seed_buf( &ctx, md_info, buf, sizeof( buf ) ) == 0 );
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +0100164
165 /* Make sure it never tries to reseed (would segfault otherwise) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166 mbedtls_hmac_drbg_set_reseed_interval( &ctx, 3 );
167 mbedtls_hmac_drbg_set_prediction_resistance( &ctx, MBEDTLS_HMAC_DRBG_PR_ON );
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +0100168
169 for( i = 0; i < 30; i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170 TEST_ASSERT( mbedtls_hmac_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +0100171
Paul Bakkerbd51b262014-07-10 15:26:12 +0200172exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173 mbedtls_hmac_drbg_free( &ctx );
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +0100174}
175/* END_CASE */
176
177/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100178void hmac_drbg_no_reseed( int md_alg, data_t * entropy,
179 data_t * custom, data_t * add1,
180 data_t * add2, data_t * output )
Manuel Pégourié-Gonnard6801f392014-01-30 17:22:14 +0100181{
Manuel Pégourié-Gonnarde6cdbbd2014-02-01 11:30:03 +0100182 unsigned char data[1024];
Manuel Pégourié-Gonnard6801f392014-01-30 17:22:14 +0100183 unsigned char my_output[512];
Manuel Pégourié-Gonnard6801f392014-01-30 17:22:14 +0100184 entropy_ctx p_entropy;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185 const mbedtls_md_info_t *md_info;
186 mbedtls_hmac_drbg_context ctx;
Manuel Pégourié-Gonnard6801f392014-01-30 17:22:14 +0100187
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200188 mbedtls_hmac_drbg_init( &ctx );
Manuel Pégourié-Gonnard6801f392014-01-30 17:22:14 +0100189
Azim Khand30ca132017-06-09 04:32:58 +0100190 p_entropy.p = entropy->x;
191 p_entropy.len = entropy->len;
Manuel Pégourié-Gonnard6801f392014-01-30 17:22:14 +0100192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakker94b916c2014-04-17 16:07:20 +0200194 TEST_ASSERT( md_info != NULL );
Manuel Pégourié-Gonnarde6cdbbd2014-02-01 11:30:03 +0100195
196 /* Test the simplified buffer-based variant */
Azim Khand30ca132017-06-09 04:32:58 +0100197 memcpy( data, entropy->x, p_entropy.len );
198 memcpy( data + p_entropy.len, custom->x, custom->len );
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200199 TEST_ASSERT( mbedtls_hmac_drbg_seed_buf( &ctx, md_info,
Azim Khand30ca132017-06-09 04:32:58 +0100200 data, p_entropy.len + custom->len ) == 0 );
201 TEST_ASSERT( mbedtls_hmac_drbg_random_with_add( &ctx, my_output, output->len,
202 add1->x, add1->len ) == 0 );
203 TEST_ASSERT( mbedtls_hmac_drbg_random_with_add( &ctx, my_output, output->len,
204 add2->x, add2->len ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200205
Gavin Acquroff6aceb512020-03-01 17:06:11 -0800206 /* Reset context for second run */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207 mbedtls_hmac_drbg_free( &ctx );
Manuel Pégourié-Gonnarde6cdbbd2014-02-01 11:30:03 +0100208
Azim Khand30ca132017-06-09 04:32:58 +0100209 TEST_ASSERT( memcmp( my_output, output->x, output->len ) == 0 );
Manuel Pégourié-Gonnarde6cdbbd2014-02-01 11:30:03 +0100210
211 /* And now the normal entropy-based variant */
Reut Caspie278b362017-10-19 08:49:19 +0100212 TEST_ASSERT( mbedtls_hmac_drbg_seed( &ctx, md_info, mbedtls_test_entropy_func, &p_entropy,
Azim Khand30ca132017-06-09 04:32:58 +0100213 custom->x, custom->len ) == 0 );
214 TEST_ASSERT( mbedtls_hmac_drbg_random_with_add( &ctx, my_output, output->len,
215 add1->x, add1->len ) == 0 );
216 TEST_ASSERT( mbedtls_hmac_drbg_random_with_add( &ctx, my_output, output->len,
217 add2->x, add2->len ) == 0 );
218 TEST_ASSERT( memcmp( my_output, output->x, output->len ) == 0 );
Manuel Pégourié-Gonnarde6cdbbd2014-02-01 11:30:03 +0100219
Paul Bakkerbd51b262014-07-10 15:26:12 +0200220exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221 mbedtls_hmac_drbg_free( &ctx );
Manuel Pégourié-Gonnard24600b72014-01-31 09:54:14 +0100222}
223/* END_CASE */
224
225/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100226void hmac_drbg_nopr( int md_alg, data_t * entropy, data_t * custom,
227 data_t * add1, data_t * add2, data_t * add3,
228 data_t * output )
Manuel Pégourié-Gonnard24600b72014-01-31 09:54:14 +0100229{
Manuel Pégourié-Gonnard24600b72014-01-31 09:54:14 +0100230 unsigned char my_output[512];
Manuel Pégourié-Gonnard24600b72014-01-31 09:54:14 +0100231 entropy_ctx p_entropy;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232 const mbedtls_md_info_t *md_info;
233 mbedtls_hmac_drbg_context ctx;
Manuel Pégourié-Gonnard24600b72014-01-31 09:54:14 +0100234
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200235 mbedtls_hmac_drbg_init( &ctx );
Manuel Pégourié-Gonnard24600b72014-01-31 09:54:14 +0100236
Azim Khand30ca132017-06-09 04:32:58 +0100237 p_entropy.p = entropy->x;
238 p_entropy.len = entropy->len;
Manuel Pégourié-Gonnard24600b72014-01-31 09:54:14 +0100239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakker94b916c2014-04-17 16:07:20 +0200241 TEST_ASSERT( md_info != NULL );
242
Reut Caspie278b362017-10-19 08:49:19 +0100243 TEST_ASSERT( mbedtls_hmac_drbg_seed( &ctx, md_info, mbedtls_test_entropy_func, &p_entropy,
Azim Khand30ca132017-06-09 04:32:58 +0100244 custom->x, custom->len ) == 0 );
245 TEST_ASSERT( mbedtls_hmac_drbg_reseed( &ctx, add1->x, add1->len ) == 0 );
246 TEST_ASSERT( mbedtls_hmac_drbg_random_with_add( &ctx, my_output, output->len,
247 add2->x, add2->len ) == 0 );
248 TEST_ASSERT( mbedtls_hmac_drbg_random_with_add( &ctx, my_output, output->len,
249 add3->x, add3->len ) == 0 );
Manuel Pégourié-Gonnard24600b72014-01-31 09:54:14 +0100250
Azim Khand30ca132017-06-09 04:32:58 +0100251 TEST_ASSERT( memcmp( my_output, output->x, output->len ) == 0 );
Manuel Pégourié-Gonnard6801f392014-01-30 17:22:14 +0100252
Paul Bakkerbd51b262014-07-10 15:26:12 +0200253exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 mbedtls_hmac_drbg_free( &ctx );
Manuel Pégourié-Gonnard6801f392014-01-30 17:22:14 +0100255}
256/* END_CASE */
257
Manuel Pégourié-Gonnard62273b82014-01-31 10:16:57 +0100258/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100259void hmac_drbg_pr( int md_alg, data_t * entropy, data_t * custom,
260 data_t * add1, data_t * add2, data_t * output )
Manuel Pégourié-Gonnard62273b82014-01-31 10:16:57 +0100261{
Manuel Pégourié-Gonnard62273b82014-01-31 10:16:57 +0100262 unsigned char my_output[512];
Manuel Pégourié-Gonnard62273b82014-01-31 10:16:57 +0100263 entropy_ctx p_entropy;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264 const mbedtls_md_info_t *md_info;
265 mbedtls_hmac_drbg_context ctx;
Manuel Pégourié-Gonnard62273b82014-01-31 10:16:57 +0100266
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200267 mbedtls_hmac_drbg_init( &ctx );
Manuel Pégourié-Gonnard62273b82014-01-31 10:16:57 +0100268
Azim Khand30ca132017-06-09 04:32:58 +0100269 p_entropy.p = entropy->x;
270 p_entropy.len = entropy->len;
Manuel Pégourié-Gonnard62273b82014-01-31 10:16:57 +0100271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakker94b916c2014-04-17 16:07:20 +0200273 TEST_ASSERT( md_info != NULL );
274
Reut Caspie278b362017-10-19 08:49:19 +0100275 TEST_ASSERT( mbedtls_hmac_drbg_seed( &ctx, md_info, mbedtls_test_entropy_func, &p_entropy,
Azim Khand30ca132017-06-09 04:32:58 +0100276 custom->x, custom->len ) == 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277 mbedtls_hmac_drbg_set_prediction_resistance( &ctx, MBEDTLS_HMAC_DRBG_PR_ON );
Azim Khand30ca132017-06-09 04:32:58 +0100278 TEST_ASSERT( mbedtls_hmac_drbg_random_with_add( &ctx, my_output, output->len,
279 add1->x, add1->len ) == 0 );
280 TEST_ASSERT( mbedtls_hmac_drbg_random_with_add( &ctx, my_output, output->len,
281 add2->x, add2->len ) == 0 );
Manuel Pégourié-Gonnard62273b82014-01-31 10:16:57 +0100282
Azim Khand30ca132017-06-09 04:32:58 +0100283 TEST_ASSERT( memcmp( my_output, output->x, output->len ) == 0 );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200284
285exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286 mbedtls_hmac_drbg_free( &ctx );
Manuel Pégourié-Gonnard62273b82014-01-31 10:16:57 +0100287}
288/* END_CASE */
289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200290/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +0100291void hmac_drbg_selftest( )
Manuel Pégourié-Gonnard79afaa02014-01-31 11:12:09 +0100292{
Andres AG93012e82016-09-09 09:10:28 +0100293 TEST_ASSERT( mbedtls_hmac_drbg_self_test( 1 ) == 0 );
Manuel Pégourié-Gonnard79afaa02014-01-31 11:12:09 +0100294}
295/* END_CASE */