blob: 7b6f9e5b188ce917da38fb4bf0393685071ea86e [file] [log] [blame]
Paul Bakker33b43f12013-08-20 11:48:36 +02001/* BEGIN_HEADER */
Paul Bakker17373852011-01-06 14:20:01 +00002#include <polarssl/md.h>
Paul Bakker33b43f12013-08-20 11:48:36 +02003/* END_HEADER */
Paul Bakker17373852011-01-06 14:20:01 +00004
Paul Bakker33b43f12013-08-20 11:48:36 +02005/* BEGIN_DEPENDENCIES
6 * depends_on:POLARSSL_MD_C
7 * END_DEPENDENCIES
8 */
Paul Bakker5690efc2011-05-26 13:16:06 +00009
Paul Bakker33b43f12013-08-20 11:48:36 +020010/* BEGIN_CASE */
Manuel Pégourié-Gonnardedb242f2014-04-02 17:52:04 +020011void md_process( )
Manuel Pégourié-Gonnardf3013832014-03-29 15:54:50 +010012{
13 const int *md_type_ptr;
Manuel Pégourié-Gonnardedb242f2014-04-02 17:52:04 +020014 const md_info_t *info;
15 md_context_t ctx;
16 unsigned char buf[150];
Manuel Pégourié-Gonnardf3013832014-03-29 15:54:50 +010017
Manuel Pégourié-Gonnardedb242f2014-04-02 17:52:04 +020018 memset( &ctx, 0, sizeof ctx );
19
20 /*
21 * Very minimal testing of md_process, just make sure the various
22 * xxx_process_wrap() function pointers are valid. (Testing that they
23 * indeed do the right thing whould require messing with the internal
24 * state of the underlying md/sha context.)
25 *
26 * Also tests that md_list() only returns valid MDs.
27 */
Manuel Pégourié-Gonnardf3013832014-03-29 15:54:50 +010028 for( md_type_ptr = md_list(); *md_type_ptr != 0; md_type_ptr++ )
Manuel Pégourié-Gonnardedb242f2014-04-02 17:52:04 +020029 {
30 TEST_ASSERT( ( info = md_info_from_type( *md_type_ptr ) ) != NULL );
31 TEST_ASSERT( md_init_ctx( &ctx, info ) == 0 );
32 TEST_ASSERT( md_process( &ctx, buf ) == 0 );
33 TEST_ASSERT( md_free_ctx( &ctx ) == 0 );
34 }
Manuel Pégourié-Gonnardf3013832014-03-29 15:54:50 +010035}
36/* END_CASE */
37
38/* BEGIN_CASE */
39void md_info( int md_type, char *md_name, int md_size )
40{
41 const md_info_t *md_info;
42 const int *md_type_ptr;
43 int found;
44
45 TEST_ASSERT( ( md_info = md_info_from_type( md_type ) ) != NULL );
46 TEST_ASSERT( md_info == md_info_from_string( md_name ) );
47
48 TEST_ASSERT( md_get_type( md_info ) == (md_type_t) md_type );
49 TEST_ASSERT( md_get_size( md_info ) == (unsigned char) md_size );
50
51 found = 0;
52 for( md_type_ptr = md_list(); *md_type_ptr != 0; md_type_ptr++ )
53 if( *md_type_ptr == md_type )
54 found = 1;
55 TEST_ASSERT( found == 1 );
56}
57/* END_CASE */
58
59/* BEGIN_CASE */
Paul Bakker33b43f12013-08-20 11:48:36 +020060void md_text( char *text_md_name, char *text_src_string, char *hex_hash_string )
Paul Bakker17373852011-01-06 14:20:01 +000061{
62 char md_name[100];
63 unsigned char src_str[1000];
64 unsigned char hash_str[1000];
65 unsigned char output[100];
66 const md_info_t *md_info = NULL;
67
68 memset(md_name, 0x00, 100);
69 memset(src_str, 0x00, 1000);
70 memset(hash_str, 0x00, 1000);
71 memset(output, 0x00, 100);
72
Paul Bakkerdd0aae92014-04-17 16:06:37 +020073 strncpy( (char *) src_str, text_src_string, sizeof(src_str) - 1 );
74 strncpy( (char *) md_name, text_md_name, sizeof(md_name) - 1 );
Paul Bakker17373852011-01-06 14:20:01 +000075 md_info = md_info_from_string(md_name);
76 TEST_ASSERT( md_info != NULL );
77
78 TEST_ASSERT ( 0 == md( md_info, src_str, strlen( (char *) src_str ), output ) );
79 hexify( hash_str, output, md_get_size(md_info) );
80
Paul Bakker33b43f12013-08-20 11:48:36 +020081 TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
Paul Bakker17373852011-01-06 14:20:01 +000082}
Paul Bakker33b43f12013-08-20 11:48:36 +020083/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +000084
Paul Bakker33b43f12013-08-20 11:48:36 +020085/* BEGIN_CASE */
86void md_hex( char *text_md_name, char *hex_src_string, char *hex_hash_string )
Paul Bakker17373852011-01-06 14:20:01 +000087{
88 char md_name[100];
89 unsigned char src_str[10000];
90 unsigned char hash_str[10000];
91 unsigned char output[100];
92 int src_len;
93 const md_info_t *md_info = NULL;
94
95 memset(md_name, 0x00, 100);
96 memset(src_str, 0x00, 10000);
97 memset(hash_str, 0x00, 10000);
98 memset(output, 0x00, 100);
99
Paul Bakkerdd0aae92014-04-17 16:06:37 +0200100 strncpy( (char *) md_name, text_md_name, sizeof(md_name) - 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000101 md_info = md_info_from_string(md_name);
102 TEST_ASSERT( md_info != NULL );
103
Paul Bakker33b43f12013-08-20 11:48:36 +0200104 src_len = unhexify( src_str, hex_src_string );
Paul Bakker17373852011-01-06 14:20:01 +0000105 TEST_ASSERT ( 0 == md( md_info, src_str, src_len, output ) );
106
107 hexify( hash_str, output, md_get_size(md_info) );
108
Paul Bakker33b43f12013-08-20 11:48:36 +0200109 TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000110}
Paul Bakker33b43f12013-08-20 11:48:36 +0200111/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000112
Paul Bakker33b43f12013-08-20 11:48:36 +0200113/* BEGIN_CASE */
114void md_text_multi( char *text_md_name, char *text_src_string,
115 char *hex_hash_string )
Paul Bakker17373852011-01-06 14:20:01 +0000116{
117 char md_name[100];
118 unsigned char src_str[1000];
119 unsigned char hash_str[1000];
120 unsigned char output[100];
121
122 const md_info_t *md_info = NULL;
123 md_context_t ctx = MD_CONTEXT_T_INIT;
124
125 memset(md_name, 0x00, 100);
126 memset(src_str, 0x00, 1000);
127 memset(hash_str, 0x00, 1000);
128 memset(output, 0x00, 100);
129
Paul Bakkerdd0aae92014-04-17 16:06:37 +0200130 strncpy( (char *) src_str, text_src_string, sizeof(src_str) - 1 );
131 strncpy( (char *) md_name, text_md_name, sizeof(md_name) - 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000132 md_info = md_info_from_string(md_name);
133 TEST_ASSERT( md_info != NULL );
Paul Bakker562535d2011-01-20 16:42:01 +0000134 TEST_ASSERT ( 0 == md_init_ctx( &ctx, md_info ) );
Paul Bakker17373852011-01-06 14:20:01 +0000135
Paul Bakker562535d2011-01-20 16:42:01 +0000136 TEST_ASSERT ( 0 == md_starts( &ctx ) );
Paul Bakker17373852011-01-06 14:20:01 +0000137 TEST_ASSERT ( ctx.md_ctx != NULL );
138 TEST_ASSERT ( 0 == md_update( &ctx, src_str, strlen( (char *) src_str ) ) );
139 TEST_ASSERT ( 0 == md_finish( &ctx, output ) );
140 TEST_ASSERT ( 0 == md_free_ctx( &ctx ) );
141
142 hexify( hash_str, output, md_get_size(md_info) );
143
Paul Bakker33b43f12013-08-20 11:48:36 +0200144 TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000145}
Paul Bakker33b43f12013-08-20 11:48:36 +0200146/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000147
Paul Bakker33b43f12013-08-20 11:48:36 +0200148/* BEGIN_CASE */
149void md_hex_multi( char *text_md_name, char *hex_src_string,
150 char *hex_hash_string )
Paul Bakker17373852011-01-06 14:20:01 +0000151{
152 char md_name[100];
153 unsigned char src_str[10000];
154 unsigned char hash_str[10000];
155 unsigned char output[100];
156 int src_len;
157 const md_info_t *md_info = NULL;
158 md_context_t ctx = MD_CONTEXT_T_INIT;
159
160 memset(md_name, 0x00, 100);
161 memset(src_str, 0x00, 10000);
162 memset(hash_str, 0x00, 10000);
163 memset(output, 0x00, 100);
164
Paul Bakkerdd0aae92014-04-17 16:06:37 +0200165 strncpy( (char *) md_name, text_md_name, sizeof(md_name) - 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000166 md_info = md_info_from_string(md_name);
167 TEST_ASSERT( md_info != NULL );
Paul Bakker562535d2011-01-20 16:42:01 +0000168 TEST_ASSERT ( 0 == md_init_ctx( &ctx, md_info ) );
Paul Bakker17373852011-01-06 14:20:01 +0000169
Paul Bakker33b43f12013-08-20 11:48:36 +0200170 src_len = unhexify( src_str, hex_src_string );
Paul Bakker17373852011-01-06 14:20:01 +0000171
Paul Bakker562535d2011-01-20 16:42:01 +0000172 TEST_ASSERT ( 0 == md_starts( &ctx ) );
Paul Bakker17373852011-01-06 14:20:01 +0000173 TEST_ASSERT ( ctx.md_ctx != NULL );
174 TEST_ASSERT ( 0 == md_update( &ctx, src_str, src_len ) );
175 TEST_ASSERT ( 0 == md_finish( &ctx, output ) );
176 TEST_ASSERT ( 0 == md_free_ctx( &ctx ) );
177
178 hexify( hash_str, output, md_get_size(md_info) );
179
Paul Bakker33b43f12013-08-20 11:48:36 +0200180 TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000181}
Paul Bakker33b43f12013-08-20 11:48:36 +0200182/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000183
Paul Bakker33b43f12013-08-20 11:48:36 +0200184/* BEGIN_CASE */
185void md_hmac( char *text_md_name, int trunc_size, char *hex_key_string,
186 char *hex_src_string, char *hex_hash_string )
Paul Bakker17373852011-01-06 14:20:01 +0000187{
188 char md_name[100];
189 unsigned char src_str[10000];
190 unsigned char key_str[10000];
191 unsigned char hash_str[10000];
192 unsigned char output[100];
193 int key_len, src_len;
194 const md_info_t *md_info = NULL;
195
196 memset(md_name, 0x00, 100);
197 memset(src_str, 0x00, 10000);
198 memset(key_str, 0x00, 10000);
199 memset(hash_str, 0x00, 10000);
200 memset(output, 0x00, 100);
201
Paul Bakkerdd0aae92014-04-17 16:06:37 +0200202 strncpy( (char *) md_name, text_md_name, sizeof(md_name) - 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000203 md_info = md_info_from_string( md_name );
204 TEST_ASSERT( md_info != NULL );
205
Paul Bakker33b43f12013-08-20 11:48:36 +0200206 key_len = unhexify( key_str, hex_key_string );
207 src_len = unhexify( src_str, hex_src_string );
Paul Bakker17373852011-01-06 14:20:01 +0000208
209 TEST_ASSERT ( md_hmac( md_info, key_str, key_len, src_str, src_len, output ) == 0 );
210 hexify( hash_str, output, md_get_size(md_info) );
211
Paul Bakker33b43f12013-08-20 11:48:36 +0200212 TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000213}
Paul Bakker33b43f12013-08-20 11:48:36 +0200214/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000215
Paul Bakker33b43f12013-08-20 11:48:36 +0200216/* BEGIN_CASE */
217void md_hmac_multi( char *text_md_name, int trunc_size, char *hex_key_string,
218 char *hex_src_string, char *hex_hash_string )
Paul Bakker17373852011-01-06 14:20:01 +0000219{
220 char md_name[100];
221 unsigned char src_str[10000];
222 unsigned char key_str[10000];
223 unsigned char hash_str[10000];
224 unsigned char output[100];
225 int key_len, src_len;
226 const md_info_t *md_info = NULL;
227 md_context_t ctx = MD_CONTEXT_T_INIT;
228
229 memset(md_name, 0x00, 100);
230 memset(src_str, 0x00, 10000);
231 memset(key_str, 0x00, 10000);
232 memset(hash_str, 0x00, 10000);
233 memset(output, 0x00, 100);
234
Paul Bakkerdd0aae92014-04-17 16:06:37 +0200235 strncpy( (char *) md_name, text_md_name, sizeof(md_name) - 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000236 md_info = md_info_from_string( md_name );
237 TEST_ASSERT( md_info != NULL );
Paul Bakker562535d2011-01-20 16:42:01 +0000238 TEST_ASSERT ( 0 == md_init_ctx( &ctx, md_info ) );
Paul Bakker17373852011-01-06 14:20:01 +0000239
Paul Bakker33b43f12013-08-20 11:48:36 +0200240 key_len = unhexify( key_str, hex_key_string );
241 src_len = unhexify( src_str, hex_src_string );
Paul Bakker17373852011-01-06 14:20:01 +0000242
Paul Bakker562535d2011-01-20 16:42:01 +0000243 TEST_ASSERT ( 0 == md_hmac_starts( &ctx, key_str, key_len ) );
Paul Bakker17373852011-01-06 14:20:01 +0000244 TEST_ASSERT ( ctx.md_ctx != NULL );
245 TEST_ASSERT ( 0 == md_hmac_update( &ctx, src_str, src_len ) );
246 TEST_ASSERT ( 0 == md_hmac_finish( &ctx, output ) );
Manuel Pégourié-Gonnard59ba4e92014-03-29 14:43:44 +0100247
248 hexify( hash_str, output, md_get_size(md_info) );
249 TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
250
251 /* Test again, for reset() */
252 memset(hash_str, 0x00, 10000);
253 memset(output, 0x00, 100);
254
255 TEST_ASSERT ( 0 == md_hmac_reset( &ctx ) );
256 TEST_ASSERT ( 0 == md_hmac_update( &ctx, src_str, src_len ) );
257 TEST_ASSERT ( 0 == md_hmac_finish( &ctx, output ) );
Paul Bakker17373852011-01-06 14:20:01 +0000258 TEST_ASSERT ( 0 == md_free_ctx( &ctx ) );
Paul Bakker33b43f12013-08-20 11:48:36 +0200259
Paul Bakker17373852011-01-06 14:20:01 +0000260 hexify( hash_str, output, md_get_size(md_info) );
Paul Bakker33b43f12013-08-20 11:48:36 +0200261 TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
Manuel Pégourié-Gonnard59ba4e92014-03-29 14:43:44 +0100262
263 TEST_ASSERT ( 0 == md_free_ctx( &ctx ) );
Paul Bakker17373852011-01-06 14:20:01 +0000264}
Paul Bakker33b43f12013-08-20 11:48:36 +0200265/* END_CASE */
Paul Bakker428b9ba2013-09-15 15:20:37 +0200266
267/* BEGIN_CASE depends_on:POLARSSL_FS_IO */
Paul Bakker33b43f12013-08-20 11:48:36 +0200268void md_file( char *text_md_name, char *filename, char *hex_hash_string )
Paul Bakker17373852011-01-06 14:20:01 +0000269{
270 char md_name[100];
271 unsigned char hash_str[1000];
272 unsigned char output[100];
273 const md_info_t *md_info = NULL;
274
275 memset(md_name, 0x00, 100);
276 memset(hash_str, 0x00, 1000);
277 memset(output, 0x00, 100);
278
Paul Bakkerdd0aae92014-04-17 16:06:37 +0200279 strncpy( (char *) md_name, text_md_name, sizeof(md_name) - 1 );
Paul Bakker17373852011-01-06 14:20:01 +0000280 md_info = md_info_from_string( md_name );
281 TEST_ASSERT( md_info != NULL );
282
Paul Bakker33b43f12013-08-20 11:48:36 +0200283 md_file( md_info, filename, output);
Paul Bakker17373852011-01-06 14:20:01 +0000284 hexify( hash_str, output, md_get_size(md_info) );
285
Paul Bakker33b43f12013-08-20 11:48:36 +0200286 TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000287}
Paul Bakker33b43f12013-08-20 11:48:36 +0200288/* END_CASE */