blob: 844eb96a98fbbd02cd7a1031ea01bc38b1b72c93 [file] [log] [blame]
Manuel Pégourié-Gonnard2c25eb02014-05-30 10:38:18 +02001/* BEGIN_HEADER */
2#include <polarssl/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
53 entropy_free( &ctx );
54}
55/* END_CASE */
56
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +020057/* BEGIN_CASE */
58void entropy_too_many_sources( )
59{
60 entropy_context ctx;
61 size_t i;
62
63 entropy_init( &ctx );
64
65 /*
66 * It's hard to tell precisely when the error will occur,
67 * since we don't know how many sources were automatically added.
68 */
69 for( i = 0; i < ENTROPY_MAX_SOURCES; i++ )
70 (void) entropy_add_source( &ctx, entropy_dummy_source, NULL, 16 );
71
72 TEST_ASSERT( entropy_add_source( &ctx, entropy_dummy_source, NULL, 16 )
73 == POLARSSL_ERR_ENTROPY_MAX_SOURCES );
74
75 entropy_free( &ctx );
76}
77/* END_CASE */
78
79/* BEGIN_CASE */
80void entropy_func_len( int len, int ret )
81{
82 entropy_context ctx;
83 unsigned char buf[ENTROPY_BLOCK_SIZE + 10] = { 0 };
84 unsigned char acc[ENTROPY_BLOCK_SIZE + 10] = { 0 };
85 size_t i, j;
86
87 entropy_init( &ctx );
88
89 /*
90 * See comments in entropy_self_test()
91 */
92 for( i = 0; i < 8; i++ )
93 {
94 TEST_ASSERT( entropy_func( &ctx, buf, len ) == ret );
95 for( j = 0; j < sizeof( buf ); j++ )
96 acc[j] |= buf[j];
97 }
98
99 if( ret == 0 )
100 for( j = 0; j < (size_t) len; j++ )
101 TEST_ASSERT( acc[j] != 0 );
102
103 for( j = len; j < sizeof( buf ); j++ )
104 TEST_ASSERT( acc[j] == 0 );
105}
106/* END_CASE */
107
108/* BEGIN_CASE */
109void entropy_source_fail( char *path )
110{
111 entropy_context ctx;
112 int fail = -1;
113 unsigned char buf[16];
114
115 entropy_init( &ctx );
116
117 TEST_ASSERT( entropy_add_source( &ctx, entropy_dummy_source, &fail, 16 )
118 == 0 );
119
120 TEST_ASSERT( entropy_func( &ctx, buf, sizeof( buf ) )
121 == POLARSSL_ERR_ENTROPY_SOURCE_FAILED );
122 TEST_ASSERT( entropy_gather( &ctx )
123 == POLARSSL_ERR_ENTROPY_SOURCE_FAILED );
124#if defined(POLARSSL_FS_IO)
125 TEST_ASSERT( entropy_write_seed_file( &ctx, path )
126 == POLARSSL_ERR_ENTROPY_SOURCE_FAILED );
127 TEST_ASSERT( entropy_update_seed_file( &ctx, path )
128 == POLARSSL_ERR_ENTROPY_SOURCE_FAILED );
129#else
130 ((void) path);
131#endif
132
133 entropy_free( &ctx );
134}
135/* END_CASE */
136
137/* BEGIN_CASE */
138void entropy_threshold( int threshold, int chunk_size, int result )
139{
140 entropy_context ctx;
141 unsigned char buf[ENTROPY_BLOCK_SIZE] = { 0 };
142 int ret;
143
144 entropy_init( &ctx );
145
146 TEST_ASSERT( entropy_add_source( &ctx, entropy_dummy_source,
147 &chunk_size, threshold ) == 0 );
148
149 entropy_dummy_calls = 0;
150 ret = entropy_func( &ctx, buf, sizeof( buf ) );
151
152 if( result >= 0 )
153 {
154 TEST_ASSERT( ret == 0 );
155 TEST_ASSERT( entropy_dummy_calls == (size_t) result );
156 }
157 else
158 {
159 TEST_ASSERT( ret == result );
160 }
161
162 entropy_free( &ctx );
163}
164/* END_CASE */
165
Manuel Pégourié-Gonnard2c25eb02014-05-30 10:38:18 +0200166/* BEGIN_CASE depends_on:POLARSSL_SELF_TEST */
167void entropy_selftest( )
168{
169 TEST_ASSERT( entropy_self_test( 0 ) == 0 );
170}
171/* END_CASE */