mbed TLS Build
tests/suites/test_suite_hmac_drbg.function@1:1a219dea6cb5, 2019-06-04 (annotated)
- Committer:
- williequesada
- Date:
- Tue Jun 04 16:03:38 2019 +0000
- Revision:
- 1:1a219dea6cb5
- Parent:
- 0:cdf462088d13
compartir a Pablo
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
markrad | 0:cdf462088d13 | 1 | /* BEGIN_HEADER */ |
markrad | 0:cdf462088d13 | 2 | #include "mbedtls/hmac_drbg.h" |
markrad | 0:cdf462088d13 | 3 | |
markrad | 0:cdf462088d13 | 4 | typedef struct |
markrad | 0:cdf462088d13 | 5 | { |
markrad | 0:cdf462088d13 | 6 | unsigned char *p; |
markrad | 0:cdf462088d13 | 7 | size_t len; |
markrad | 0:cdf462088d13 | 8 | } entropy_ctx; |
markrad | 0:cdf462088d13 | 9 | |
markrad | 0:cdf462088d13 | 10 | int mbedtls_entropy_func( void *data, unsigned char *buf, size_t len ) |
markrad | 0:cdf462088d13 | 11 | { |
markrad | 0:cdf462088d13 | 12 | entropy_ctx *ctx = (entropy_ctx *) data; |
markrad | 0:cdf462088d13 | 13 | |
markrad | 0:cdf462088d13 | 14 | if( len > ctx->len ) |
markrad | 0:cdf462088d13 | 15 | return( -1 ); |
markrad | 0:cdf462088d13 | 16 | |
markrad | 0:cdf462088d13 | 17 | memcpy( buf, ctx->p, len ); |
markrad | 0:cdf462088d13 | 18 | |
markrad | 0:cdf462088d13 | 19 | ctx->p += len; |
markrad | 0:cdf462088d13 | 20 | ctx->len -= len; |
markrad | 0:cdf462088d13 | 21 | |
markrad | 0:cdf462088d13 | 22 | return( 0 ); |
markrad | 0:cdf462088d13 | 23 | } |
markrad | 0:cdf462088d13 | 24 | /* END_HEADER */ |
markrad | 0:cdf462088d13 | 25 | |
markrad | 0:cdf462088d13 | 26 | /* BEGIN_DEPENDENCIES |
markrad | 0:cdf462088d13 | 27 | * depends_on:MBEDTLS_HMAC_DRBG_C |
markrad | 0:cdf462088d13 | 28 | * END_DEPENDENCIES |
markrad | 0:cdf462088d13 | 29 | */ |
markrad | 0:cdf462088d13 | 30 | |
markrad | 0:cdf462088d13 | 31 | /* BEGIN_CASE */ |
markrad | 0:cdf462088d13 | 32 | void hmac_drbg_entropy_usage( int md_alg ) |
markrad | 0:cdf462088d13 | 33 | { |
markrad | 0:cdf462088d13 | 34 | unsigned char out[16]; |
markrad | 0:cdf462088d13 | 35 | unsigned char buf[1024]; |
markrad | 0:cdf462088d13 | 36 | const mbedtls_md_info_t *md_info; |
markrad | 0:cdf462088d13 | 37 | mbedtls_hmac_drbg_context ctx; |
markrad | 0:cdf462088d13 | 38 | entropy_ctx entropy; |
markrad | 0:cdf462088d13 | 39 | size_t last_len, i, reps = 10; |
markrad | 0:cdf462088d13 | 40 | |
markrad | 0:cdf462088d13 | 41 | mbedtls_hmac_drbg_init( &ctx ); |
markrad | 0:cdf462088d13 | 42 | memset( buf, 0, sizeof( buf ) ); |
markrad | 0:cdf462088d13 | 43 | memset( out, 0, sizeof( out ) ); |
markrad | 0:cdf462088d13 | 44 | |
markrad | 0:cdf462088d13 | 45 | entropy.len = sizeof( buf ); |
markrad | 0:cdf462088d13 | 46 | entropy.p = buf; |
markrad | 0:cdf462088d13 | 47 | |
markrad | 0:cdf462088d13 | 48 | md_info = mbedtls_md_info_from_type( md_alg ); |
markrad | 0:cdf462088d13 | 49 | TEST_ASSERT( md_info != NULL ); |
markrad | 0:cdf462088d13 | 50 | |
markrad | 0:cdf462088d13 | 51 | /* Init must use entropy */ |
markrad | 0:cdf462088d13 | 52 | last_len = entropy.len; |
markrad | 0:cdf462088d13 | 53 | TEST_ASSERT( mbedtls_hmac_drbg_seed( &ctx, md_info, mbedtls_entropy_func, &entropy, |
markrad | 0:cdf462088d13 | 54 | NULL, 0 ) == 0 ); |
markrad | 0:cdf462088d13 | 55 | TEST_ASSERT( entropy.len < last_len ); |
markrad | 0:cdf462088d13 | 56 | |
markrad | 0:cdf462088d13 | 57 | /* By default, PR is off and reseed_interval is large, |
markrad | 0:cdf462088d13 | 58 | * so the next few calls should not use entropy */ |
markrad | 0:cdf462088d13 | 59 | last_len = entropy.len; |
markrad | 0:cdf462088d13 | 60 | for( i = 0; i < reps; i++ ) |
markrad | 0:cdf462088d13 | 61 | { |
markrad | 0:cdf462088d13 | 62 | TEST_ASSERT( mbedtls_hmac_drbg_random( &ctx, out, sizeof( out ) - 4 ) == 0 ); |
markrad | 0:cdf462088d13 | 63 | TEST_ASSERT( mbedtls_hmac_drbg_random_with_add( &ctx, out, sizeof( out ) - 4, |
markrad | 0:cdf462088d13 | 64 | buf, 16 ) == 0 ); |
markrad | 0:cdf462088d13 | 65 | } |
markrad | 0:cdf462088d13 | 66 | TEST_ASSERT( entropy.len == last_len ); |
markrad | 0:cdf462088d13 | 67 | |
markrad | 0:cdf462088d13 | 68 | /* While at it, make sure we didn't write past the requested length */ |
markrad | 0:cdf462088d13 | 69 | TEST_ASSERT( out[sizeof( out ) - 4] == 0 ); |
markrad | 0:cdf462088d13 | 70 | TEST_ASSERT( out[sizeof( out ) - 3] == 0 ); |
markrad | 0:cdf462088d13 | 71 | TEST_ASSERT( out[sizeof( out ) - 2] == 0 ); |
markrad | 0:cdf462088d13 | 72 | TEST_ASSERT( out[sizeof( out ) - 1] == 0 ); |
markrad | 0:cdf462088d13 | 73 | |
markrad | 0:cdf462088d13 | 74 | /* Set reseed_interval to the number of calls done, |
markrad | 0:cdf462088d13 | 75 | * so the next call should reseed */ |
markrad | 0:cdf462088d13 | 76 | mbedtls_hmac_drbg_set_reseed_interval( &ctx, 2 * reps ); |
markrad | 0:cdf462088d13 | 77 | TEST_ASSERT( mbedtls_hmac_drbg_random( &ctx, out, sizeof( out ) ) == 0 ); |
markrad | 0:cdf462088d13 | 78 | TEST_ASSERT( entropy.len < last_len ); |
markrad | 0:cdf462088d13 | 79 | |
markrad | 0:cdf462088d13 | 80 | /* The new few calls should not reseed */ |
markrad | 0:cdf462088d13 | 81 | last_len = entropy.len; |
markrad | 0:cdf462088d13 | 82 | for( i = 0; i < reps / 2; i++ ) |
markrad | 0:cdf462088d13 | 83 | { |
markrad | 0:cdf462088d13 | 84 | TEST_ASSERT( mbedtls_hmac_drbg_random( &ctx, out, sizeof( out ) ) == 0 ); |
markrad | 0:cdf462088d13 | 85 | TEST_ASSERT( mbedtls_hmac_drbg_random_with_add( &ctx, out, sizeof( out ) , |
markrad | 0:cdf462088d13 | 86 | buf, 16 ) == 0 ); |
markrad | 0:cdf462088d13 | 87 | } |
markrad | 0:cdf462088d13 | 88 | TEST_ASSERT( entropy.len == last_len ); |
markrad | 0:cdf462088d13 | 89 | |
markrad | 0:cdf462088d13 | 90 | /* Now enable PR, so the next few calls should all reseed */ |
markrad | 0:cdf462088d13 | 91 | mbedtls_hmac_drbg_set_prediction_resistance( &ctx, MBEDTLS_HMAC_DRBG_PR_ON ); |
markrad | 0:cdf462088d13 | 92 | TEST_ASSERT( mbedtls_hmac_drbg_random( &ctx, out, sizeof( out ) ) == 0 ); |
markrad | 0:cdf462088d13 | 93 | TEST_ASSERT( entropy.len < last_len ); |
markrad | 0:cdf462088d13 | 94 | |
markrad | 0:cdf462088d13 | 95 | /* Finally, check setting entropy_len */ |
markrad | 0:cdf462088d13 | 96 | mbedtls_hmac_drbg_set_entropy_len( &ctx, 42 ); |
markrad | 0:cdf462088d13 | 97 | last_len = entropy.len; |
markrad | 0:cdf462088d13 | 98 | TEST_ASSERT( mbedtls_hmac_drbg_random( &ctx, out, sizeof( out ) ) == 0 ); |
markrad | 0:cdf462088d13 | 99 | TEST_ASSERT( (int) last_len - entropy.len == 42 ); |
markrad | 0:cdf462088d13 | 100 | |
markrad | 0:cdf462088d13 | 101 | mbedtls_hmac_drbg_set_entropy_len( &ctx, 13 ); |
markrad | 0:cdf462088d13 | 102 | last_len = entropy.len; |
markrad | 0:cdf462088d13 | 103 | TEST_ASSERT( mbedtls_hmac_drbg_random( &ctx, out, sizeof( out ) ) == 0 ); |
markrad | 0:cdf462088d13 | 104 | TEST_ASSERT( (int) last_len - entropy.len == 13 ); |
markrad | 0:cdf462088d13 | 105 | |
markrad | 0:cdf462088d13 | 106 | exit: |
markrad | 0:cdf462088d13 | 107 | mbedtls_hmac_drbg_free( &ctx ); |
markrad | 0:cdf462088d13 | 108 | } |
markrad | 0:cdf462088d13 | 109 | /* END_CASE */ |
markrad | 0:cdf462088d13 | 110 | |
markrad | 0:cdf462088d13 | 111 | /* BEGIN_CASE depends_on:MBEDTLS_FS_IO */ |
markrad | 0:cdf462088d13 | 112 | void hmac_drbg_seed_file( int md_alg, char *path, int ret ) |
markrad | 0:cdf462088d13 | 113 | { |
markrad | 0:cdf462088d13 | 114 | const mbedtls_md_info_t *md_info; |
markrad | 0:cdf462088d13 | 115 | mbedtls_hmac_drbg_context ctx; |
markrad | 0:cdf462088d13 | 116 | |
markrad | 0:cdf462088d13 | 117 | mbedtls_hmac_drbg_init( &ctx ); |
markrad | 0:cdf462088d13 | 118 | |
markrad | 0:cdf462088d13 | 119 | md_info = mbedtls_md_info_from_type( md_alg ); |
markrad | 0:cdf462088d13 | 120 | TEST_ASSERT( md_info != NULL ); |
markrad | 0:cdf462088d13 | 121 | |
markrad | 0:cdf462088d13 | 122 | TEST_ASSERT( mbedtls_hmac_drbg_seed( &ctx, md_info, rnd_std_rand, NULL, |
markrad | 0:cdf462088d13 | 123 | NULL, 0 ) == 0 ); |
markrad | 0:cdf462088d13 | 124 | |
markrad | 0:cdf462088d13 | 125 | TEST_ASSERT( mbedtls_hmac_drbg_write_seed_file( &ctx, path ) == ret ); |
markrad | 0:cdf462088d13 | 126 | TEST_ASSERT( mbedtls_hmac_drbg_update_seed_file( &ctx, path ) == ret ); |
markrad | 0:cdf462088d13 | 127 | |
markrad | 0:cdf462088d13 | 128 | exit: |
markrad | 0:cdf462088d13 | 129 | mbedtls_hmac_drbg_free( &ctx ); |
markrad | 0:cdf462088d13 | 130 | } |
markrad | 0:cdf462088d13 | 131 | /* END_CASE */ |
markrad | 0:cdf462088d13 | 132 | |
markrad | 0:cdf462088d13 | 133 | /* BEGIN_CASE */ |
markrad | 0:cdf462088d13 | 134 | void hmac_drbg_buf( int md_alg ) |
markrad | 0:cdf462088d13 | 135 | { |
markrad | 0:cdf462088d13 | 136 | unsigned char out[16]; |
markrad | 0:cdf462088d13 | 137 | unsigned char buf[100]; |
markrad | 0:cdf462088d13 | 138 | const mbedtls_md_info_t *md_info; |
markrad | 0:cdf462088d13 | 139 | mbedtls_hmac_drbg_context ctx; |
markrad | 0:cdf462088d13 | 140 | size_t i; |
markrad | 0:cdf462088d13 | 141 | |
markrad | 0:cdf462088d13 | 142 | mbedtls_hmac_drbg_init( &ctx ); |
markrad | 0:cdf462088d13 | 143 | memset( buf, 0, sizeof( buf ) ); |
markrad | 0:cdf462088d13 | 144 | memset( out, 0, sizeof( out ) ); |
markrad | 0:cdf462088d13 | 145 | |
markrad | 0:cdf462088d13 | 146 | md_info = mbedtls_md_info_from_type( md_alg ); |
markrad | 0:cdf462088d13 | 147 | TEST_ASSERT( md_info != NULL ); |
markrad | 0:cdf462088d13 | 148 | TEST_ASSERT( mbedtls_hmac_drbg_seed_buf( &ctx, md_info, buf, sizeof( buf ) ) == 0 ); |
markrad | 0:cdf462088d13 | 149 | |
markrad | 0:cdf462088d13 | 150 | /* Make sure it never tries to reseed (would segfault otherwise) */ |
markrad | 0:cdf462088d13 | 151 | mbedtls_hmac_drbg_set_reseed_interval( &ctx, 3 ); |
markrad | 0:cdf462088d13 | 152 | mbedtls_hmac_drbg_set_prediction_resistance( &ctx, MBEDTLS_HMAC_DRBG_PR_ON ); |
markrad | 0:cdf462088d13 | 153 | |
markrad | 0:cdf462088d13 | 154 | for( i = 0; i < 30; i++ ) |
markrad | 0:cdf462088d13 | 155 | TEST_ASSERT( mbedtls_hmac_drbg_random( &ctx, out, sizeof( out ) ) == 0 ); |
markrad | 0:cdf462088d13 | 156 | |
markrad | 0:cdf462088d13 | 157 | exit: |
markrad | 0:cdf462088d13 | 158 | mbedtls_hmac_drbg_free( &ctx ); |
markrad | 0:cdf462088d13 | 159 | } |
markrad | 0:cdf462088d13 | 160 | /* END_CASE */ |
markrad | 0:cdf462088d13 | 161 | |
markrad | 0:cdf462088d13 | 162 | /* BEGIN_CASE */ |
markrad | 0:cdf462088d13 | 163 | void hmac_drbg_no_reseed( int md_alg, |
markrad | 0:cdf462088d13 | 164 | char *entropy_hex, char *custom_hex, |
markrad | 0:cdf462088d13 | 165 | char *add1_hex, char *add2_hex, |
markrad | 0:cdf462088d13 | 166 | char *output_hex ) |
markrad | 0:cdf462088d13 | 167 | { |
markrad | 0:cdf462088d13 | 168 | unsigned char data[1024]; |
markrad | 0:cdf462088d13 | 169 | unsigned char entropy[512]; |
markrad | 0:cdf462088d13 | 170 | unsigned char custom[512]; |
markrad | 0:cdf462088d13 | 171 | unsigned char add1[512]; |
markrad | 0:cdf462088d13 | 172 | unsigned char add2[512]; |
markrad | 0:cdf462088d13 | 173 | unsigned char output[512]; |
markrad | 0:cdf462088d13 | 174 | unsigned char my_output[512]; |
markrad | 0:cdf462088d13 | 175 | size_t custom_len, add1_len, add2_len, out_len; |
markrad | 0:cdf462088d13 | 176 | entropy_ctx p_entropy; |
markrad | 0:cdf462088d13 | 177 | const mbedtls_md_info_t *md_info; |
markrad | 0:cdf462088d13 | 178 | mbedtls_hmac_drbg_context ctx; |
markrad | 0:cdf462088d13 | 179 | |
markrad | 0:cdf462088d13 | 180 | mbedtls_hmac_drbg_init( &ctx ); |
markrad | 0:cdf462088d13 | 181 | memset( my_output, 0, sizeof my_output ); |
markrad | 0:cdf462088d13 | 182 | |
markrad | 0:cdf462088d13 | 183 | custom_len = unhexify( custom, custom_hex ); |
markrad | 0:cdf462088d13 | 184 | add1_len = unhexify( add1, add1_hex ); |
markrad | 0:cdf462088d13 | 185 | add2_len = unhexify( add2, add2_hex ); |
markrad | 0:cdf462088d13 | 186 | out_len = unhexify( output, output_hex ); |
markrad | 0:cdf462088d13 | 187 | p_entropy.len = unhexify( entropy, entropy_hex ); |
markrad | 0:cdf462088d13 | 188 | p_entropy.p = entropy; |
markrad | 0:cdf462088d13 | 189 | |
markrad | 0:cdf462088d13 | 190 | md_info = mbedtls_md_info_from_type( md_alg ); |
markrad | 0:cdf462088d13 | 191 | TEST_ASSERT( md_info != NULL ); |
markrad | 0:cdf462088d13 | 192 | |
markrad | 0:cdf462088d13 | 193 | /* Test the simplified buffer-based variant */ |
markrad | 0:cdf462088d13 | 194 | memcpy( data, entropy, p_entropy.len ); |
markrad | 0:cdf462088d13 | 195 | memcpy( data + p_entropy.len, custom, custom_len ); |
markrad | 0:cdf462088d13 | 196 | TEST_ASSERT( mbedtls_hmac_drbg_seed_buf( &ctx, md_info, |
markrad | 0:cdf462088d13 | 197 | data, p_entropy.len + custom_len ) == 0 ); |
markrad | 0:cdf462088d13 | 198 | TEST_ASSERT( mbedtls_hmac_drbg_random_with_add( &ctx, my_output, out_len, |
markrad | 0:cdf462088d13 | 199 | add1, add1_len ) == 0 ); |
markrad | 0:cdf462088d13 | 200 | TEST_ASSERT( mbedtls_hmac_drbg_random_with_add( &ctx, my_output, out_len, |
markrad | 0:cdf462088d13 | 201 | add2, add2_len ) == 0 ); |
markrad | 0:cdf462088d13 | 202 | |
markrad | 0:cdf462088d13 | 203 | /* clear for second run */ |
markrad | 0:cdf462088d13 | 204 | mbedtls_hmac_drbg_free( &ctx ); |
markrad | 0:cdf462088d13 | 205 | |
markrad | 0:cdf462088d13 | 206 | TEST_ASSERT( memcmp( my_output, output, out_len ) == 0 ); |
markrad | 0:cdf462088d13 | 207 | |
markrad | 0:cdf462088d13 | 208 | /* And now the normal entropy-based variant */ |
markrad | 0:cdf462088d13 | 209 | TEST_ASSERT( mbedtls_hmac_drbg_seed( &ctx, md_info, mbedtls_entropy_func, &p_entropy, |
markrad | 0:cdf462088d13 | 210 | custom, custom_len ) == 0 ); |
markrad | 0:cdf462088d13 | 211 | TEST_ASSERT( mbedtls_hmac_drbg_random_with_add( &ctx, my_output, out_len, |
markrad | 0:cdf462088d13 | 212 | add1, add1_len ) == 0 ); |
markrad | 0:cdf462088d13 | 213 | TEST_ASSERT( mbedtls_hmac_drbg_random_with_add( &ctx, my_output, out_len, |
markrad | 0:cdf462088d13 | 214 | add2, add2_len ) == 0 ); |
markrad | 0:cdf462088d13 | 215 | TEST_ASSERT( memcmp( my_output, output, out_len ) == 0 ); |
markrad | 0:cdf462088d13 | 216 | |
markrad | 0:cdf462088d13 | 217 | exit: |
markrad | 0:cdf462088d13 | 218 | mbedtls_hmac_drbg_free( &ctx ); |
markrad | 0:cdf462088d13 | 219 | } |
markrad | 0:cdf462088d13 | 220 | /* END_CASE */ |
markrad | 0:cdf462088d13 | 221 | |
markrad | 0:cdf462088d13 | 222 | /* BEGIN_CASE */ |
markrad | 0:cdf462088d13 | 223 | void hmac_drbg_nopr( int md_alg, |
markrad | 0:cdf462088d13 | 224 | char *entropy_hex, char *custom_hex, |
markrad | 0:cdf462088d13 | 225 | char *add1_hex, char *add2_hex, char *add3_hex, |
markrad | 0:cdf462088d13 | 226 | char *output_hex ) |
markrad | 0:cdf462088d13 | 227 | { |
markrad | 0:cdf462088d13 | 228 | unsigned char entropy[512]; |
markrad | 0:cdf462088d13 | 229 | unsigned char custom[512]; |
markrad | 0:cdf462088d13 | 230 | unsigned char add1[512]; |
markrad | 0:cdf462088d13 | 231 | unsigned char add2[512]; |
markrad | 0:cdf462088d13 | 232 | unsigned char add3[512]; |
markrad | 0:cdf462088d13 | 233 | unsigned char output[512]; |
markrad | 0:cdf462088d13 | 234 | unsigned char my_output[512]; |
markrad | 0:cdf462088d13 | 235 | size_t custom_len, add1_len, add2_len, add3_len, out_len; |
markrad | 0:cdf462088d13 | 236 | entropy_ctx p_entropy; |
markrad | 0:cdf462088d13 | 237 | const mbedtls_md_info_t *md_info; |
markrad | 0:cdf462088d13 | 238 | mbedtls_hmac_drbg_context ctx; |
markrad | 0:cdf462088d13 | 239 | |
markrad | 0:cdf462088d13 | 240 | mbedtls_hmac_drbg_init( &ctx ); |
markrad | 0:cdf462088d13 | 241 | memset( my_output, 0, sizeof my_output ); |
markrad | 0:cdf462088d13 | 242 | |
markrad | 0:cdf462088d13 | 243 | custom_len = unhexify( custom, custom_hex ); |
markrad | 0:cdf462088d13 | 244 | add1_len = unhexify( add1, add1_hex ); |
markrad | 0:cdf462088d13 | 245 | add2_len = unhexify( add2, add2_hex ); |
markrad | 0:cdf462088d13 | 246 | add3_len = unhexify( add3, add3_hex ); |
markrad | 0:cdf462088d13 | 247 | out_len = unhexify( output, output_hex ); |
markrad | 0:cdf462088d13 | 248 | p_entropy.len = unhexify( entropy, entropy_hex ); |
markrad | 0:cdf462088d13 | 249 | p_entropy.p = entropy; |
markrad | 0:cdf462088d13 | 250 | |
markrad | 0:cdf462088d13 | 251 | md_info = mbedtls_md_info_from_type( md_alg ); |
markrad | 0:cdf462088d13 | 252 | TEST_ASSERT( md_info != NULL ); |
markrad | 0:cdf462088d13 | 253 | |
markrad | 0:cdf462088d13 | 254 | TEST_ASSERT( mbedtls_hmac_drbg_seed( &ctx, md_info, mbedtls_entropy_func, &p_entropy, |
markrad | 0:cdf462088d13 | 255 | custom, custom_len ) == 0 ); |
markrad | 0:cdf462088d13 | 256 | TEST_ASSERT( mbedtls_hmac_drbg_reseed( &ctx, add1, add1_len ) == 0 ); |
markrad | 0:cdf462088d13 | 257 | TEST_ASSERT( mbedtls_hmac_drbg_random_with_add( &ctx, my_output, out_len, |
markrad | 0:cdf462088d13 | 258 | add2, add2_len ) == 0 ); |
markrad | 0:cdf462088d13 | 259 | TEST_ASSERT( mbedtls_hmac_drbg_random_with_add( &ctx, my_output, out_len, |
markrad | 0:cdf462088d13 | 260 | add3, add3_len ) == 0 ); |
markrad | 0:cdf462088d13 | 261 | |
markrad | 0:cdf462088d13 | 262 | TEST_ASSERT( memcmp( my_output, output, out_len ) == 0 ); |
markrad | 0:cdf462088d13 | 263 | |
markrad | 0:cdf462088d13 | 264 | exit: |
markrad | 0:cdf462088d13 | 265 | mbedtls_hmac_drbg_free( &ctx ); |
markrad | 0:cdf462088d13 | 266 | } |
markrad | 0:cdf462088d13 | 267 | /* END_CASE */ |
markrad | 0:cdf462088d13 | 268 | |
markrad | 0:cdf462088d13 | 269 | /* BEGIN_CASE */ |
markrad | 0:cdf462088d13 | 270 | void hmac_drbg_pr( int md_alg, |
markrad | 0:cdf462088d13 | 271 | char *entropy_hex, char *custom_hex, |
markrad | 0:cdf462088d13 | 272 | char *add1_hex, char *add2_hex, |
markrad | 0:cdf462088d13 | 273 | char *output_hex ) |
markrad | 0:cdf462088d13 | 274 | { |
markrad | 0:cdf462088d13 | 275 | unsigned char entropy[512]; |
markrad | 0:cdf462088d13 | 276 | unsigned char custom[512]; |
markrad | 0:cdf462088d13 | 277 | unsigned char add1[512]; |
markrad | 0:cdf462088d13 | 278 | unsigned char add2[512]; |
markrad | 0:cdf462088d13 | 279 | unsigned char output[512]; |
markrad | 0:cdf462088d13 | 280 | unsigned char my_output[512]; |
markrad | 0:cdf462088d13 | 281 | size_t custom_len, add1_len, add2_len, out_len; |
markrad | 0:cdf462088d13 | 282 | entropy_ctx p_entropy; |
markrad | 0:cdf462088d13 | 283 | const mbedtls_md_info_t *md_info; |
markrad | 0:cdf462088d13 | 284 | mbedtls_hmac_drbg_context ctx; |
markrad | 0:cdf462088d13 | 285 | |
markrad | 0:cdf462088d13 | 286 | mbedtls_hmac_drbg_init( &ctx ); |
markrad | 0:cdf462088d13 | 287 | memset( my_output, 0, sizeof my_output ); |
markrad | 0:cdf462088d13 | 288 | |
markrad | 0:cdf462088d13 | 289 | custom_len = unhexify( custom, custom_hex ); |
markrad | 0:cdf462088d13 | 290 | add1_len = unhexify( add1, add1_hex ); |
markrad | 0:cdf462088d13 | 291 | add2_len = unhexify( add2, add2_hex ); |
markrad | 0:cdf462088d13 | 292 | out_len = unhexify( output, output_hex ); |
markrad | 0:cdf462088d13 | 293 | p_entropy.len = unhexify( entropy, entropy_hex ); |
markrad | 0:cdf462088d13 | 294 | p_entropy.p = entropy; |
markrad | 0:cdf462088d13 | 295 | |
markrad | 0:cdf462088d13 | 296 | md_info = mbedtls_md_info_from_type( md_alg ); |
markrad | 0:cdf462088d13 | 297 | TEST_ASSERT( md_info != NULL ); |
markrad | 0:cdf462088d13 | 298 | |
markrad | 0:cdf462088d13 | 299 | TEST_ASSERT( mbedtls_hmac_drbg_seed( &ctx, md_info, mbedtls_entropy_func, &p_entropy, |
markrad | 0:cdf462088d13 | 300 | custom, custom_len ) == 0 ); |
markrad | 0:cdf462088d13 | 301 | mbedtls_hmac_drbg_set_prediction_resistance( &ctx, MBEDTLS_HMAC_DRBG_PR_ON ); |
markrad | 0:cdf462088d13 | 302 | TEST_ASSERT( mbedtls_hmac_drbg_random_with_add( &ctx, my_output, out_len, |
markrad | 0:cdf462088d13 | 303 | add1, add1_len ) == 0 ); |
markrad | 0:cdf462088d13 | 304 | TEST_ASSERT( mbedtls_hmac_drbg_random_with_add( &ctx, my_output, out_len, |
markrad | 0:cdf462088d13 | 305 | add2, add2_len ) == 0 ); |
markrad | 0:cdf462088d13 | 306 | |
markrad | 0:cdf462088d13 | 307 | TEST_ASSERT( memcmp( my_output, output, out_len ) == 0 ); |
markrad | 0:cdf462088d13 | 308 | |
markrad | 0:cdf462088d13 | 309 | exit: |
markrad | 0:cdf462088d13 | 310 | mbedtls_hmac_drbg_free( &ctx ); |
markrad | 0:cdf462088d13 | 311 | } |
markrad | 0:cdf462088d13 | 312 | /* END_CASE */ |
markrad | 0:cdf462088d13 | 313 | |
markrad | 0:cdf462088d13 | 314 | /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */ |
markrad | 0:cdf462088d13 | 315 | void hmac_drbg_selftest( ) |
markrad | 0:cdf462088d13 | 316 | { |
markrad | 0:cdf462088d13 | 317 | TEST_ASSERT( mbedtls_hmac_drbg_self_test( 1 ) == 0 ); |
markrad | 0:cdf462088d13 | 318 | } |
markrad | 0:cdf462088d13 | 319 | /* END_CASE */ |