Manuel Pégourié-Gonnard | 6801f39 | 2014-01-30 17:22:14 +0100 | [diff] [blame] | 1 | /* BEGIN_HEADER */ |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 2 | #include "mbedtls/hmac_drbg.h" |
Mohammad Azim Khan | 67735d5 | 2017-04-06 11:55:43 +0100 | [diff] [blame] | 3 | #include "string.h" |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 4 | |
Manuel Pégourié-Gonnard | 6801f39 | 2014-01-30 17:22:14 +0100 | [diff] [blame] | 5 | typedef struct |
| 6 | { |
| 7 | unsigned char *p; |
| 8 | size_t len; |
| 9 | } entropy_ctx; |
| 10 | |
Reut Caspi | e278b36 | 2017-10-19 08:49:19 +0100 | [diff] [blame] | 11 | static int mbedtls_test_entropy_func( void *data, unsigned char *buf, size_t len ) |
Manuel Pégourié-Gonnard | 6801f39 | 2014-01-30 17:22:14 +0100 | [diff] [blame] | 12 | { |
| 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é-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 28 | * depends_on:MBEDTLS_HMAC_DRBG_C |
Manuel Pégourié-Gonnard | 6801f39 | 2014-01-30 17:22:14 +0100 | [diff] [blame] | 29 | * END_DEPENDENCIES |
| 30 | */ |
| 31 | |
Manuel Pégourié-Gonnard | 4f880a5 | 2014-01-30 22:39:42 +0100 | [diff] [blame] | 32 | /* BEGIN_CASE */ |
| 33 | void hmac_drbg_entropy_usage( int md_alg ) |
| 34 | { |
| 35 | unsigned char out[16]; |
| 36 | unsigned char buf[1024]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 37 | const mbedtls_md_info_t *md_info; |
| 38 | mbedtls_hmac_drbg_context ctx; |
Manuel Pégourié-Gonnard | 4f880a5 | 2014-01-30 22:39:42 +0100 | [diff] [blame] | 39 | entropy_ctx entropy; |
| 40 | size_t last_len, i, reps = 10; |
| 41 | |
Manuel Pégourié-Gonnard | f9e9481 | 2015-04-28 22:07:14 +0200 | [diff] [blame] | 42 | mbedtls_hmac_drbg_init( &ctx ); |
Manuel Pégourié-Gonnard | 4f880a5 | 2014-01-30 22:39:42 +0100 | [diff] [blame] | 43 | memset( buf, 0, sizeof( buf ) ); |
| 44 | memset( out, 0, sizeof( out ) ); |
| 45 | |
| 46 | entropy.len = sizeof( buf ); |
| 47 | entropy.p = buf; |
| 48 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 49 | md_info = mbedtls_md_info_from_type( md_alg ); |
Paul Bakker | 94b916c | 2014-04-17 16:07:20 +0200 | [diff] [blame] | 50 | TEST_ASSERT( md_info != NULL ); |
Manuel Pégourié-Gonnard | 4f880a5 | 2014-01-30 22:39:42 +0100 | [diff] [blame] | 51 | |
| 52 | /* Init must use entropy */ |
| 53 | last_len = entropy.len; |
Reut Caspi | e278b36 | 2017-10-19 08:49:19 +0100 | [diff] [blame] | 54 | TEST_ASSERT( mbedtls_hmac_drbg_seed( &ctx, md_info, mbedtls_test_entropy_func, &entropy, |
Manuel Pégourié-Gonnard | 4f880a5 | 2014-01-30 22:39:42 +0100 | [diff] [blame] | 55 | NULL, 0 ) == 0 ); |
| 56 | TEST_ASSERT( entropy.len < last_len ); |
| 57 | |
| 58 | /* By default, PR is off and reseed_interval is large, |
| 59 | * so the next few calls should not use entropy */ |
| 60 | last_len = entropy.len; |
| 61 | for( i = 0; i < reps; i++ ) |
| 62 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 63 | TEST_ASSERT( mbedtls_hmac_drbg_random( &ctx, out, sizeof( out ) - 4 ) == 0 ); |
| 64 | TEST_ASSERT( mbedtls_hmac_drbg_random_with_add( &ctx, out, sizeof( out ) - 4, |
Manuel Pégourié-Gonnard | 4f880a5 | 2014-01-30 22:39:42 +0100 | [diff] [blame] | 65 | buf, 16 ) == 0 ); |
| 66 | } |
| 67 | TEST_ASSERT( entropy.len == last_len ); |
| 68 | |
| 69 | /* While at it, make sure we didn't write past the requested length */ |
| 70 | TEST_ASSERT( out[sizeof( out ) - 4] == 0 ); |
| 71 | TEST_ASSERT( out[sizeof( out ) - 3] == 0 ); |
| 72 | TEST_ASSERT( out[sizeof( out ) - 2] == 0 ); |
| 73 | TEST_ASSERT( out[sizeof( out ) - 1] == 0 ); |
| 74 | |
| 75 | /* Set reseed_interval to the number of calls done, |
| 76 | * so the next call should reseed */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 77 | mbedtls_hmac_drbg_set_reseed_interval( &ctx, 2 * reps ); |
| 78 | TEST_ASSERT( mbedtls_hmac_drbg_random( &ctx, out, sizeof( out ) ) == 0 ); |
Manuel Pégourié-Gonnard | 4f880a5 | 2014-01-30 22:39:42 +0100 | [diff] [blame] | 79 | TEST_ASSERT( entropy.len < last_len ); |
| 80 | |
| 81 | /* The new few calls should not reseed */ |
| 82 | last_len = entropy.len; |
| 83 | for( i = 0; i < reps / 2; i++ ) |
| 84 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 85 | TEST_ASSERT( mbedtls_hmac_drbg_random( &ctx, out, sizeof( out ) ) == 0 ); |
| 86 | TEST_ASSERT( mbedtls_hmac_drbg_random_with_add( &ctx, out, sizeof( out ) , |
Manuel Pégourié-Gonnard | 4f880a5 | 2014-01-30 22:39:42 +0100 | [diff] [blame] | 87 | buf, 16 ) == 0 ); |
| 88 | } |
| 89 | TEST_ASSERT( entropy.len == last_len ); |
| 90 | |
| 91 | /* Now enable PR, so the next few calls should all reseed */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 92 | mbedtls_hmac_drbg_set_prediction_resistance( &ctx, MBEDTLS_HMAC_DRBG_PR_ON ); |
| 93 | TEST_ASSERT( mbedtls_hmac_drbg_random( &ctx, out, sizeof( out ) ) == 0 ); |
Manuel Pégourié-Gonnard | 4f880a5 | 2014-01-30 22:39:42 +0100 | [diff] [blame] | 94 | TEST_ASSERT( entropy.len < last_len ); |
| 95 | |
| 96 | /* Finally, check setting entropy_len */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 97 | mbedtls_hmac_drbg_set_entropy_len( &ctx, 42 ); |
Manuel Pégourié-Gonnard | 4f880a5 | 2014-01-30 22:39:42 +0100 | [diff] [blame] | 98 | last_len = entropy.len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 99 | TEST_ASSERT( mbedtls_hmac_drbg_random( &ctx, out, sizeof( out ) ) == 0 ); |
Manuel Pégourié-Gonnard | 4f880a5 | 2014-01-30 22:39:42 +0100 | [diff] [blame] | 100 | TEST_ASSERT( (int) last_len - entropy.len == 42 ); |
| 101 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 102 | mbedtls_hmac_drbg_set_entropy_len( &ctx, 13 ); |
Manuel Pégourié-Gonnard | 4f880a5 | 2014-01-30 22:39:42 +0100 | [diff] [blame] | 103 | last_len = entropy.len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 104 | TEST_ASSERT( mbedtls_hmac_drbg_random( &ctx, out, sizeof( out ) ) == 0 ); |
Manuel Pégourié-Gonnard | 4f880a5 | 2014-01-30 22:39:42 +0100 | [diff] [blame] | 105 | TEST_ASSERT( (int) last_len - entropy.len == 13 ); |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 106 | |
| 107 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 108 | mbedtls_hmac_drbg_free( &ctx ); |
Manuel Pégourié-Gonnard | 4f880a5 | 2014-01-30 22:39:42 +0100 | [diff] [blame] | 109 | } |
| 110 | /* END_CASE */ |
| 111 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 112 | /* BEGIN_CASE depends_on:MBEDTLS_FS_IO */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 113 | void hmac_drbg_seed_file( int md_alg, char * path, int ret ) |
Manuel Pégourié-Gonnard | 48bc3e8 | 2014-01-30 21:11:16 +0100 | [diff] [blame] | 114 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 115 | const mbedtls_md_info_t *md_info; |
| 116 | mbedtls_hmac_drbg_context ctx; |
Manuel Pégourié-Gonnard | 48bc3e8 | 2014-01-30 21:11:16 +0100 | [diff] [blame] | 117 | |
Manuel Pégourié-Gonnard | f9e9481 | 2015-04-28 22:07:14 +0200 | [diff] [blame] | 118 | mbedtls_hmac_drbg_init( &ctx ); |
| 119 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 120 | md_info = mbedtls_md_info_from_type( md_alg ); |
Paul Bakker | 94b916c | 2014-04-17 16:07:20 +0200 | [diff] [blame] | 121 | TEST_ASSERT( md_info != NULL ); |
| 122 | |
Manuel Pégourié-Gonnard | f9e9481 | 2015-04-28 22:07:14 +0200 | [diff] [blame] | 123 | TEST_ASSERT( mbedtls_hmac_drbg_seed( &ctx, md_info, rnd_std_rand, NULL, |
Manuel Pégourié-Gonnard | 48bc3e8 | 2014-01-30 21:11:16 +0100 | [diff] [blame] | 124 | NULL, 0 ) == 0 ); |
| 125 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 126 | TEST_ASSERT( mbedtls_hmac_drbg_write_seed_file( &ctx, path ) == ret ); |
| 127 | TEST_ASSERT( mbedtls_hmac_drbg_update_seed_file( &ctx, path ) == ret ); |
Manuel Pégourié-Gonnard | 48bc3e8 | 2014-01-30 21:11:16 +0100 | [diff] [blame] | 128 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 129 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 130 | mbedtls_hmac_drbg_free( &ctx ); |
Manuel Pégourié-Gonnard | 48bc3e8 | 2014-01-30 21:11:16 +0100 | [diff] [blame] | 131 | } |
| 132 | /* END_CASE */ |
| 133 | |
Manuel Pégourié-Gonnard | 6801f39 | 2014-01-30 17:22:14 +0100 | [diff] [blame] | 134 | /* BEGIN_CASE */ |
Manuel Pégourié-Gonnard | 4f880a5 | 2014-01-30 22:39:42 +0100 | [diff] [blame] | 135 | void hmac_drbg_buf( int md_alg ) |
| 136 | { |
| 137 | unsigned char out[16]; |
| 138 | unsigned char buf[100]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 139 | const mbedtls_md_info_t *md_info; |
| 140 | mbedtls_hmac_drbg_context ctx; |
Manuel Pégourié-Gonnard | 4f880a5 | 2014-01-30 22:39:42 +0100 | [diff] [blame] | 141 | size_t i; |
| 142 | |
Manuel Pégourié-Gonnard | f9e9481 | 2015-04-28 22:07:14 +0200 | [diff] [blame] | 143 | mbedtls_hmac_drbg_init( &ctx ); |
Manuel Pégourié-Gonnard | 4f880a5 | 2014-01-30 22:39:42 +0100 | [diff] [blame] | 144 | memset( buf, 0, sizeof( buf ) ); |
| 145 | memset( out, 0, sizeof( out ) ); |
| 146 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 147 | md_info = mbedtls_md_info_from_type( md_alg ); |
Paul Bakker | 94b916c | 2014-04-17 16:07:20 +0200 | [diff] [blame] | 148 | TEST_ASSERT( md_info != NULL ); |
Manuel Pégourié-Gonnard | f9e9481 | 2015-04-28 22:07:14 +0200 | [diff] [blame] | 149 | TEST_ASSERT( mbedtls_hmac_drbg_seed_buf( &ctx, md_info, buf, sizeof( buf ) ) == 0 ); |
Manuel Pégourié-Gonnard | 4f880a5 | 2014-01-30 22:39:42 +0100 | [diff] [blame] | 150 | |
| 151 | /* Make sure it never tries to reseed (would segfault otherwise) */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 152 | mbedtls_hmac_drbg_set_reseed_interval( &ctx, 3 ); |
| 153 | mbedtls_hmac_drbg_set_prediction_resistance( &ctx, MBEDTLS_HMAC_DRBG_PR_ON ); |
Manuel Pégourié-Gonnard | 4f880a5 | 2014-01-30 22:39:42 +0100 | [diff] [blame] | 154 | |
| 155 | for( i = 0; i < 30; i++ ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 156 | TEST_ASSERT( mbedtls_hmac_drbg_random( &ctx, out, sizeof( out ) ) == 0 ); |
Manuel Pégourié-Gonnard | 4f880a5 | 2014-01-30 22:39:42 +0100 | [diff] [blame] | 157 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 158 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 159 | mbedtls_hmac_drbg_free( &ctx ); |
Manuel Pégourié-Gonnard | 4f880a5 | 2014-01-30 22:39:42 +0100 | [diff] [blame] | 160 | } |
| 161 | /* END_CASE */ |
| 162 | |
| 163 | /* BEGIN_CASE */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 164 | void hmac_drbg_no_reseed( int md_alg, uint8_t * entropy, |
| 165 | uint32_t entropy_len, uint8_t * custom, |
| 166 | uint32_t custom_len, uint8_t * add1, |
| 167 | uint32_t add1_len, uint8_t * add2, |
| 168 | uint32_t add2_len, uint8_t * output, |
| 169 | uint32_t out_len ) |
Manuel Pégourié-Gonnard | 6801f39 | 2014-01-30 17:22:14 +0100 | [diff] [blame] | 170 | { |
Manuel Pégourié-Gonnard | e6cdbbd | 2014-02-01 11:30:03 +0100 | [diff] [blame] | 171 | unsigned char data[1024]; |
Manuel Pégourié-Gonnard | 6801f39 | 2014-01-30 17:22:14 +0100 | [diff] [blame] | 172 | unsigned char my_output[512]; |
Manuel Pégourié-Gonnard | 6801f39 | 2014-01-30 17:22:14 +0100 | [diff] [blame] | 173 | entropy_ctx p_entropy; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 174 | const mbedtls_md_info_t *md_info; |
| 175 | mbedtls_hmac_drbg_context ctx; |
Manuel Pégourié-Gonnard | 6801f39 | 2014-01-30 17:22:14 +0100 | [diff] [blame] | 176 | |
Manuel Pégourié-Gonnard | f9e9481 | 2015-04-28 22:07:14 +0200 | [diff] [blame] | 177 | mbedtls_hmac_drbg_init( &ctx ); |
Manuel Pégourié-Gonnard | 6801f39 | 2014-01-30 17:22:14 +0100 | [diff] [blame] | 178 | |
Manuel Pégourié-Gonnard | 6801f39 | 2014-01-30 17:22:14 +0100 | [diff] [blame] | 179 | p_entropy.p = entropy; |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 180 | p_entropy.len = entropy_len; |
Manuel Pégourié-Gonnard | 6801f39 | 2014-01-30 17:22:14 +0100 | [diff] [blame] | 181 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 182 | md_info = mbedtls_md_info_from_type( md_alg ); |
Paul Bakker | 94b916c | 2014-04-17 16:07:20 +0200 | [diff] [blame] | 183 | TEST_ASSERT( md_info != NULL ); |
Manuel Pégourié-Gonnard | e6cdbbd | 2014-02-01 11:30:03 +0100 | [diff] [blame] | 184 | |
| 185 | /* Test the simplified buffer-based variant */ |
| 186 | memcpy( data, entropy, p_entropy.len ); |
| 187 | memcpy( data + p_entropy.len, custom, custom_len ); |
Manuel Pégourié-Gonnard | f9e9481 | 2015-04-28 22:07:14 +0200 | [diff] [blame] | 188 | TEST_ASSERT( mbedtls_hmac_drbg_seed_buf( &ctx, md_info, |
Manuel Pégourié-Gonnard | e6cdbbd | 2014-02-01 11:30:03 +0100 | [diff] [blame] | 189 | data, p_entropy.len + custom_len ) == 0 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 190 | TEST_ASSERT( mbedtls_hmac_drbg_random_with_add( &ctx, my_output, out_len, |
Manuel Pégourié-Gonnard | e6cdbbd | 2014-02-01 11:30:03 +0100 | [diff] [blame] | 191 | add1, add1_len ) == 0 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 192 | TEST_ASSERT( mbedtls_hmac_drbg_random_with_add( &ctx, my_output, out_len, |
Manuel Pégourié-Gonnard | e6cdbbd | 2014-02-01 11:30:03 +0100 | [diff] [blame] | 193 | add2, add2_len ) == 0 ); |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 194 | |
| 195 | /* clear for second run */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 196 | mbedtls_hmac_drbg_free( &ctx ); |
Manuel Pégourié-Gonnard | e6cdbbd | 2014-02-01 11:30:03 +0100 | [diff] [blame] | 197 | |
| 198 | TEST_ASSERT( memcmp( my_output, output, out_len ) == 0 ); |
| 199 | |
| 200 | /* And now the normal entropy-based variant */ |
Reut Caspi | e278b36 | 2017-10-19 08:49:19 +0100 | [diff] [blame] | 201 | TEST_ASSERT( mbedtls_hmac_drbg_seed( &ctx, md_info, mbedtls_test_entropy_func, &p_entropy, |
Manuel Pégourié-Gonnard | 6801f39 | 2014-01-30 17:22:14 +0100 | [diff] [blame] | 202 | custom, custom_len ) == 0 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 203 | TEST_ASSERT( mbedtls_hmac_drbg_random_with_add( &ctx, my_output, out_len, |
Manuel Pégourié-Gonnard | 6801f39 | 2014-01-30 17:22:14 +0100 | [diff] [blame] | 204 | add1, add1_len ) == 0 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 205 | TEST_ASSERT( mbedtls_hmac_drbg_random_with_add( &ctx, my_output, out_len, |
Manuel Pégourié-Gonnard | 6801f39 | 2014-01-30 17:22:14 +0100 | [diff] [blame] | 206 | add2, add2_len ) == 0 ); |
Manuel Pégourié-Gonnard | 24600b7 | 2014-01-31 09:54:14 +0100 | [diff] [blame] | 207 | TEST_ASSERT( memcmp( my_output, output, out_len ) == 0 ); |
Manuel Pégourié-Gonnard | e6cdbbd | 2014-02-01 11:30:03 +0100 | [diff] [blame] | 208 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 209 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 210 | mbedtls_hmac_drbg_free( &ctx ); |
Manuel Pégourié-Gonnard | 24600b7 | 2014-01-31 09:54:14 +0100 | [diff] [blame] | 211 | } |
| 212 | /* END_CASE */ |
| 213 | |
| 214 | /* BEGIN_CASE */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 215 | void hmac_drbg_nopr( int md_alg, uint8_t * entropy, uint32_t entropy_len, |
| 216 | uint8_t * custom, uint32_t custom_len, uint8_t * add1, |
| 217 | uint32_t add1_len, uint8_t * add2, uint32_t add2_len, |
| 218 | uint8_t * add3, uint32_t add3_len, uint8_t * output, |
| 219 | uint32_t out_len ) |
Manuel Pégourié-Gonnard | 24600b7 | 2014-01-31 09:54:14 +0100 | [diff] [blame] | 220 | { |
Manuel Pégourié-Gonnard | 24600b7 | 2014-01-31 09:54:14 +0100 | [diff] [blame] | 221 | unsigned char my_output[512]; |
Manuel Pégourié-Gonnard | 24600b7 | 2014-01-31 09:54:14 +0100 | [diff] [blame] | 222 | entropy_ctx p_entropy; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 223 | const mbedtls_md_info_t *md_info; |
| 224 | mbedtls_hmac_drbg_context ctx; |
Manuel Pégourié-Gonnard | 24600b7 | 2014-01-31 09:54:14 +0100 | [diff] [blame] | 225 | |
Manuel Pégourié-Gonnard | f9e9481 | 2015-04-28 22:07:14 +0200 | [diff] [blame] | 226 | mbedtls_hmac_drbg_init( &ctx ); |
Manuel Pégourié-Gonnard | 24600b7 | 2014-01-31 09:54:14 +0100 | [diff] [blame] | 227 | |
Manuel Pégourié-Gonnard | 24600b7 | 2014-01-31 09:54:14 +0100 | [diff] [blame] | 228 | p_entropy.p = entropy; |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 229 | p_entropy.len = entropy_len; |
Manuel Pégourié-Gonnard | 24600b7 | 2014-01-31 09:54:14 +0100 | [diff] [blame] | 230 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 231 | md_info = mbedtls_md_info_from_type( md_alg ); |
Paul Bakker | 94b916c | 2014-04-17 16:07:20 +0200 | [diff] [blame] | 232 | TEST_ASSERT( md_info != NULL ); |
| 233 | |
Reut Caspi | e278b36 | 2017-10-19 08:49:19 +0100 | [diff] [blame] | 234 | TEST_ASSERT( mbedtls_hmac_drbg_seed( &ctx, md_info, mbedtls_test_entropy_func, &p_entropy, |
Manuel Pégourié-Gonnard | 24600b7 | 2014-01-31 09:54:14 +0100 | [diff] [blame] | 235 | custom, custom_len ) == 0 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 236 | TEST_ASSERT( mbedtls_hmac_drbg_reseed( &ctx, add1, add1_len ) == 0 ); |
| 237 | TEST_ASSERT( mbedtls_hmac_drbg_random_with_add( &ctx, my_output, out_len, |
Manuel Pégourié-Gonnard | 24600b7 | 2014-01-31 09:54:14 +0100 | [diff] [blame] | 238 | add2, add2_len ) == 0 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 239 | TEST_ASSERT( mbedtls_hmac_drbg_random_with_add( &ctx, my_output, out_len, |
Manuel Pégourié-Gonnard | 24600b7 | 2014-01-31 09:54:14 +0100 | [diff] [blame] | 240 | add3, add3_len ) == 0 ); |
Manuel Pégourié-Gonnard | 24600b7 | 2014-01-31 09:54:14 +0100 | [diff] [blame] | 241 | |
Manuel Pégourié-Gonnard | 6801f39 | 2014-01-30 17:22:14 +0100 | [diff] [blame] | 242 | TEST_ASSERT( memcmp( my_output, output, out_len ) == 0 ); |
| 243 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 244 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 245 | mbedtls_hmac_drbg_free( &ctx ); |
Manuel Pégourié-Gonnard | 6801f39 | 2014-01-30 17:22:14 +0100 | [diff] [blame] | 246 | } |
| 247 | /* END_CASE */ |
| 248 | |
Manuel Pégourié-Gonnard | 62273b8 | 2014-01-31 10:16:57 +0100 | [diff] [blame] | 249 | /* BEGIN_CASE */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 250 | void hmac_drbg_pr( int md_alg, uint8_t * entropy, uint32_t entropy_len, |
| 251 | uint8_t * custom, uint32_t custom_len, uint8_t * add1, |
| 252 | uint32_t add1_len, uint8_t * add2, uint32_t add2_len, |
| 253 | uint8_t * output, uint32_t out_len ) |
Manuel Pégourié-Gonnard | 62273b8 | 2014-01-31 10:16:57 +0100 | [diff] [blame] | 254 | { |
Manuel Pégourié-Gonnard | 62273b8 | 2014-01-31 10:16:57 +0100 | [diff] [blame] | 255 | unsigned char my_output[512]; |
Manuel Pégourié-Gonnard | 62273b8 | 2014-01-31 10:16:57 +0100 | [diff] [blame] | 256 | entropy_ctx p_entropy; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 257 | const mbedtls_md_info_t *md_info; |
| 258 | mbedtls_hmac_drbg_context ctx; |
Manuel Pégourié-Gonnard | 62273b8 | 2014-01-31 10:16:57 +0100 | [diff] [blame] | 259 | |
Manuel Pégourié-Gonnard | f9e9481 | 2015-04-28 22:07:14 +0200 | [diff] [blame] | 260 | mbedtls_hmac_drbg_init( &ctx ); |
Manuel Pégourié-Gonnard | 62273b8 | 2014-01-31 10:16:57 +0100 | [diff] [blame] | 261 | |
Manuel Pégourié-Gonnard | 62273b8 | 2014-01-31 10:16:57 +0100 | [diff] [blame] | 262 | p_entropy.p = entropy; |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 263 | p_entropy.len = entropy_len; |
Manuel Pégourié-Gonnard | 62273b8 | 2014-01-31 10:16:57 +0100 | [diff] [blame] | 264 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 265 | md_info = mbedtls_md_info_from_type( md_alg ); |
Paul Bakker | 94b916c | 2014-04-17 16:07:20 +0200 | [diff] [blame] | 266 | TEST_ASSERT( md_info != NULL ); |
| 267 | |
Reut Caspi | e278b36 | 2017-10-19 08:49:19 +0100 | [diff] [blame] | 268 | TEST_ASSERT( mbedtls_hmac_drbg_seed( &ctx, md_info, mbedtls_test_entropy_func, &p_entropy, |
Manuel Pégourié-Gonnard | 62273b8 | 2014-01-31 10:16:57 +0100 | [diff] [blame] | 269 | custom, custom_len ) == 0 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 270 | mbedtls_hmac_drbg_set_prediction_resistance( &ctx, MBEDTLS_HMAC_DRBG_PR_ON ); |
| 271 | TEST_ASSERT( mbedtls_hmac_drbg_random_with_add( &ctx, my_output, out_len, |
Manuel Pégourié-Gonnard | 62273b8 | 2014-01-31 10:16:57 +0100 | [diff] [blame] | 272 | add1, add1_len ) == 0 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 273 | TEST_ASSERT( mbedtls_hmac_drbg_random_with_add( &ctx, my_output, out_len, |
Manuel Pégourié-Gonnard | 62273b8 | 2014-01-31 10:16:57 +0100 | [diff] [blame] | 274 | add2, add2_len ) == 0 ); |
Manuel Pégourié-Gonnard | 62273b8 | 2014-01-31 10:16:57 +0100 | [diff] [blame] | 275 | |
| 276 | TEST_ASSERT( memcmp( my_output, output, out_len ) == 0 ); |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 277 | |
| 278 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 279 | mbedtls_hmac_drbg_free( &ctx ); |
Manuel Pégourié-Gonnard | 62273b8 | 2014-01-31 10:16:57 +0100 | [diff] [blame] | 280 | } |
| 281 | /* END_CASE */ |
| 282 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 283 | /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 284 | void hmac_drbg_selftest( ) |
Manuel Pégourié-Gonnard | 79afaa0 | 2014-01-31 11:12:09 +0100 | [diff] [blame] | 285 | { |
Andres AG | 93012e8 | 2016-09-09 09:10:28 +0100 | [diff] [blame] | 286 | TEST_ASSERT( mbedtls_hmac_drbg_self_test( 1 ) == 0 ); |
Manuel Pégourié-Gonnard | 79afaa0 | 2014-01-31 11:12:09 +0100 | [diff] [blame] | 287 | } |
| 288 | /* END_CASE */ |