blob: 41a115cc6b740e5d4e38ee2049368538880942b7 [file] [log] [blame]
Manuel Pégourié-Gonnard2c25eb02014-05-30 10:38:18 +02001/* BEGIN_HEADER */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002#include "mbedtls/entropy.h"
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +02003
4/*
5 * Number of calls made to entropy_dummy_source()
6 */
7static size_t entropy_dummy_calls;
8
9/*
10 * Dummy entropy source
11 *
12 * If data is NULL, write exactly the requested length.
13 * Otherwise, write the length indicated by data or error if negative
14 */
15static int entropy_dummy_source( void *data, unsigned char *output,
16 size_t len, size_t *olen )
17{
18 entropy_dummy_calls++;
19
20 if( data == NULL )
21 *olen = len;
22 else
23 {
24 int *d = (int *) data;
25
26 if( *d < 0 )
27 return( POLARSSL_ERR_ENTROPY_SOURCE_FAILED );
28 else
29 *olen = *d;
30 }
31
32 memset( output, 0x2a, *olen );
33
34 return( 0 );
35}
Manuel Pégourié-Gonnard2c25eb02014-05-30 10:38:18 +020036/* END_HEADER */
37
38/* BEGIN_DEPENDENCIES
39 * depends_on:POLARSSL_ENTROPY_C
40 * END_DEPENDENCIES
41 */
42
43/* BEGIN_CASE depends_on:POLARSSL_FS_IO */
44void entropy_seed_file( char *path, int ret )
45{
46 entropy_context ctx;
47
48 entropy_init( &ctx );
49
50 TEST_ASSERT( entropy_write_seed_file( &ctx, path ) == ret );
51 TEST_ASSERT( entropy_update_seed_file( &ctx, path ) == ret );
52
Paul Bakkerbd51b262014-07-10 15:26:12 +020053exit:
Manuel Pégourié-Gonnard2c25eb02014-05-30 10:38:18 +020054 entropy_free( &ctx );
55}
56/* END_CASE */
57
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +020058/* BEGIN_CASE */
59void entropy_too_many_sources( )
60{
61 entropy_context ctx;
62 size_t i;
63
64 entropy_init( &ctx );
65
66 /*
67 * It's hard to tell precisely when the error will occur,
68 * since we don't know how many sources were automatically added.
69 */
70 for( i = 0; i < ENTROPY_MAX_SOURCES; i++ )
71 (void) entropy_add_source( &ctx, entropy_dummy_source, NULL, 16 );
72
73 TEST_ASSERT( entropy_add_source( &ctx, entropy_dummy_source, NULL, 16 )
74 == POLARSSL_ERR_ENTROPY_MAX_SOURCES );
75
Paul Bakkerbd51b262014-07-10 15:26:12 +020076exit:
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +020077 entropy_free( &ctx );
78}
79/* END_CASE */
80
81/* BEGIN_CASE */
82void entropy_func_len( int len, int ret )
83{
84 entropy_context ctx;
85 unsigned char buf[ENTROPY_BLOCK_SIZE + 10] = { 0 };
86 unsigned char acc[ENTROPY_BLOCK_SIZE + 10] = { 0 };
87 size_t i, j;
88
89 entropy_init( &ctx );
90
91 /*
92 * See comments in entropy_self_test()
93 */
94 for( i = 0; i < 8; i++ )
95 {
96 TEST_ASSERT( entropy_func( &ctx, buf, len ) == ret );
97 for( j = 0; j < sizeof( buf ); j++ )
98 acc[j] |= buf[j];
99 }
100
101 if( ret == 0 )
102 for( j = 0; j < (size_t) len; j++ )
103 TEST_ASSERT( acc[j] != 0 );
104
105 for( j = len; j < sizeof( buf ); j++ )
106 TEST_ASSERT( acc[j] == 0 );
107}
108/* END_CASE */
109
110/* BEGIN_CASE */
111void entropy_source_fail( char *path )
112{
113 entropy_context ctx;
114 int fail = -1;
115 unsigned char buf[16];
116
117 entropy_init( &ctx );
118
119 TEST_ASSERT( entropy_add_source( &ctx, entropy_dummy_source, &fail, 16 )
120 == 0 );
121
122 TEST_ASSERT( entropy_func( &ctx, buf, sizeof( buf ) )
123 == POLARSSL_ERR_ENTROPY_SOURCE_FAILED );
124 TEST_ASSERT( entropy_gather( &ctx )
125 == POLARSSL_ERR_ENTROPY_SOURCE_FAILED );
126#if defined(POLARSSL_FS_IO)
127 TEST_ASSERT( entropy_write_seed_file( &ctx, path )
128 == POLARSSL_ERR_ENTROPY_SOURCE_FAILED );
129 TEST_ASSERT( entropy_update_seed_file( &ctx, path )
130 == POLARSSL_ERR_ENTROPY_SOURCE_FAILED );
131#else
132 ((void) path);
133#endif
134
Paul Bakkerbd51b262014-07-10 15:26:12 +0200135exit:
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200136 entropy_free( &ctx );
137}
138/* END_CASE */
139
140/* BEGIN_CASE */
141void entropy_threshold( int threshold, int chunk_size, int result )
142{
143 entropy_context ctx;
144 unsigned char buf[ENTROPY_BLOCK_SIZE] = { 0 };
145 int ret;
146
147 entropy_init( &ctx );
148
149 TEST_ASSERT( entropy_add_source( &ctx, entropy_dummy_source,
150 &chunk_size, threshold ) == 0 );
151
152 entropy_dummy_calls = 0;
153 ret = entropy_func( &ctx, buf, sizeof( buf ) );
154
155 if( result >= 0 )
156 {
157 TEST_ASSERT( ret == 0 );
158 TEST_ASSERT( entropy_dummy_calls == (size_t) result );
159 }
160 else
161 {
162 TEST_ASSERT( ret == result );
163 }
164
Paul Bakkerbd51b262014-07-10 15:26:12 +0200165exit:
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200166 entropy_free( &ctx );
167}
168/* END_CASE */
169
Manuel Pégourié-Gonnard2c25eb02014-05-30 10:38:18 +0200170/* BEGIN_CASE depends_on:POLARSSL_SELF_TEST */
171void entropy_selftest( )
172{
173 TEST_ASSERT( entropy_self_test( 0 ) == 0 );
174}
175/* END_CASE */