blob: 4b5921f7c5529f5682891ee30fd68081a78c29ae [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 Bakker33b43f12013-08-20 11:48:36 +020073 strcpy( (char *) src_str, text_src_string );
Paul Bakker17373852011-01-06 14:20:01 +000074
Paul Bakker33b43f12013-08-20 11:48:36 +020075 strncpy( (char *) md_name, text_md_name, 100 );
Paul Bakker17373852011-01-06 14:20:01 +000076 md_info = md_info_from_string(md_name);
77 TEST_ASSERT( md_info != NULL );
78
79 TEST_ASSERT ( 0 == md( md_info, src_str, strlen( (char *) src_str ), output ) );
80 hexify( hash_str, output, md_get_size(md_info) );
81
Paul Bakker33b43f12013-08-20 11:48:36 +020082 TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
Paul Bakker17373852011-01-06 14:20:01 +000083}
Paul Bakker33b43f12013-08-20 11:48:36 +020084/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +000085
Paul Bakker33b43f12013-08-20 11:48:36 +020086/* BEGIN_CASE */
87void md_hex( char *text_md_name, char *hex_src_string, char *hex_hash_string )
Paul Bakker17373852011-01-06 14:20:01 +000088{
89 char md_name[100];
90 unsigned char src_str[10000];
91 unsigned char hash_str[10000];
92 unsigned char output[100];
93 int src_len;
94 const md_info_t *md_info = NULL;
95
96 memset(md_name, 0x00, 100);
97 memset(src_str, 0x00, 10000);
98 memset(hash_str, 0x00, 10000);
99 memset(output, 0x00, 100);
100
Paul Bakker33b43f12013-08-20 11:48:36 +0200101 strncpy( (char *) md_name, text_md_name, 100 );
Paul Bakker17373852011-01-06 14:20:01 +0000102 md_info = md_info_from_string(md_name);
103 TEST_ASSERT( md_info != NULL );
104
Paul Bakker33b43f12013-08-20 11:48:36 +0200105 src_len = unhexify( src_str, hex_src_string );
Paul Bakker17373852011-01-06 14:20:01 +0000106 TEST_ASSERT ( 0 == md( md_info, src_str, src_len, output ) );
107
108 hexify( hash_str, output, md_get_size(md_info) );
109
Paul Bakker33b43f12013-08-20 11:48:36 +0200110 TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000111}
Paul Bakker33b43f12013-08-20 11:48:36 +0200112/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000113
Paul Bakker33b43f12013-08-20 11:48:36 +0200114/* BEGIN_CASE */
115void md_text_multi( char *text_md_name, char *text_src_string,
116 char *hex_hash_string )
Paul Bakker17373852011-01-06 14:20:01 +0000117{
118 char md_name[100];
119 unsigned char src_str[1000];
120 unsigned char hash_str[1000];
121 unsigned char output[100];
122
123 const md_info_t *md_info = NULL;
124 md_context_t ctx = MD_CONTEXT_T_INIT;
125
126 memset(md_name, 0x00, 100);
127 memset(src_str, 0x00, 1000);
128 memset(hash_str, 0x00, 1000);
129 memset(output, 0x00, 100);
130
Paul Bakker33b43f12013-08-20 11:48:36 +0200131 strcpy( (char *) src_str, text_src_string );
Paul Bakker17373852011-01-06 14:20:01 +0000132
Paul Bakker33b43f12013-08-20 11:48:36 +0200133 strncpy( (char *) md_name, text_md_name, 100 );
Paul Bakker17373852011-01-06 14:20:01 +0000134 md_info = md_info_from_string(md_name);
135 TEST_ASSERT( md_info != NULL );
Paul Bakker562535d2011-01-20 16:42:01 +0000136 TEST_ASSERT ( 0 == md_init_ctx( &ctx, md_info ) );
Paul Bakker17373852011-01-06 14:20:01 +0000137
Paul Bakker562535d2011-01-20 16:42:01 +0000138 TEST_ASSERT ( 0 == md_starts( &ctx ) );
Paul Bakker17373852011-01-06 14:20:01 +0000139 TEST_ASSERT ( ctx.md_ctx != NULL );
140 TEST_ASSERT ( 0 == md_update( &ctx, src_str, strlen( (char *) src_str ) ) );
141 TEST_ASSERT ( 0 == md_finish( &ctx, output ) );
142 TEST_ASSERT ( 0 == md_free_ctx( &ctx ) );
143
144 hexify( hash_str, output, md_get_size(md_info) );
145
Paul Bakker33b43f12013-08-20 11:48:36 +0200146 TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000147}
Paul Bakker33b43f12013-08-20 11:48:36 +0200148/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000149
Paul Bakker33b43f12013-08-20 11:48:36 +0200150/* BEGIN_CASE */
151void md_hex_multi( char *text_md_name, char *hex_src_string,
152 char *hex_hash_string )
Paul Bakker17373852011-01-06 14:20:01 +0000153{
154 char md_name[100];
155 unsigned char src_str[10000];
156 unsigned char hash_str[10000];
157 unsigned char output[100];
158 int src_len;
159 const md_info_t *md_info = NULL;
160 md_context_t ctx = MD_CONTEXT_T_INIT;
161
162 memset(md_name, 0x00, 100);
163 memset(src_str, 0x00, 10000);
164 memset(hash_str, 0x00, 10000);
165 memset(output, 0x00, 100);
166
Paul Bakker33b43f12013-08-20 11:48:36 +0200167 strncpy( (char *) md_name, text_md_name, 100 );
Paul Bakker17373852011-01-06 14:20:01 +0000168 md_info = md_info_from_string(md_name);
169 TEST_ASSERT( md_info != NULL );
Paul Bakker562535d2011-01-20 16:42:01 +0000170 TEST_ASSERT ( 0 == md_init_ctx( &ctx, md_info ) );
Paul Bakker17373852011-01-06 14:20:01 +0000171
Paul Bakker33b43f12013-08-20 11:48:36 +0200172 src_len = unhexify( src_str, hex_src_string );
Paul Bakker17373852011-01-06 14:20:01 +0000173
Paul Bakker562535d2011-01-20 16:42:01 +0000174 TEST_ASSERT ( 0 == md_starts( &ctx ) );
Paul Bakker17373852011-01-06 14:20:01 +0000175 TEST_ASSERT ( ctx.md_ctx != NULL );
176 TEST_ASSERT ( 0 == md_update( &ctx, src_str, src_len ) );
177 TEST_ASSERT ( 0 == md_finish( &ctx, output ) );
178 TEST_ASSERT ( 0 == md_free_ctx( &ctx ) );
179
180 hexify( hash_str, output, md_get_size(md_info) );
181
Paul Bakker33b43f12013-08-20 11:48:36 +0200182 TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000183}
Paul Bakker33b43f12013-08-20 11:48:36 +0200184/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000185
Paul Bakker33b43f12013-08-20 11:48:36 +0200186/* BEGIN_CASE */
187void md_hmac( char *text_md_name, int trunc_size, char *hex_key_string,
188 char *hex_src_string, char *hex_hash_string )
Paul Bakker17373852011-01-06 14:20:01 +0000189{
190 char md_name[100];
191 unsigned char src_str[10000];
192 unsigned char key_str[10000];
193 unsigned char hash_str[10000];
194 unsigned char output[100];
195 int key_len, src_len;
196 const md_info_t *md_info = NULL;
197
198 memset(md_name, 0x00, 100);
199 memset(src_str, 0x00, 10000);
200 memset(key_str, 0x00, 10000);
201 memset(hash_str, 0x00, 10000);
202 memset(output, 0x00, 100);
203
Paul Bakker33b43f12013-08-20 11:48:36 +0200204 strncpy( (char *) md_name, text_md_name, 100 );
Paul Bakker17373852011-01-06 14:20:01 +0000205 md_info = md_info_from_string( md_name );
206 TEST_ASSERT( md_info != NULL );
207
Paul Bakker33b43f12013-08-20 11:48:36 +0200208 key_len = unhexify( key_str, hex_key_string );
209 src_len = unhexify( src_str, hex_src_string );
Paul Bakker17373852011-01-06 14:20:01 +0000210
211 TEST_ASSERT ( md_hmac( md_info, key_str, key_len, src_str, src_len, output ) == 0 );
212 hexify( hash_str, output, md_get_size(md_info) );
213
Paul Bakker33b43f12013-08-20 11:48:36 +0200214 TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000215}
Paul Bakker33b43f12013-08-20 11:48:36 +0200216/* END_CASE */
Paul Bakker17373852011-01-06 14:20:01 +0000217
Paul Bakker33b43f12013-08-20 11:48:36 +0200218/* BEGIN_CASE */
219void md_hmac_multi( char *text_md_name, int trunc_size, char *hex_key_string,
220 char *hex_src_string, char *hex_hash_string )
Paul Bakker17373852011-01-06 14:20:01 +0000221{
222 char md_name[100];
223 unsigned char src_str[10000];
224 unsigned char key_str[10000];
225 unsigned char hash_str[10000];
226 unsigned char output[100];
227 int key_len, src_len;
228 const md_info_t *md_info = NULL;
229 md_context_t ctx = MD_CONTEXT_T_INIT;
230
231 memset(md_name, 0x00, 100);
232 memset(src_str, 0x00, 10000);
233 memset(key_str, 0x00, 10000);
234 memset(hash_str, 0x00, 10000);
235 memset(output, 0x00, 100);
236
Paul Bakker33b43f12013-08-20 11:48:36 +0200237 strncpy( (char *) md_name, text_md_name, 100 );
Paul Bakker17373852011-01-06 14:20:01 +0000238 md_info = md_info_from_string( md_name );
239 TEST_ASSERT( md_info != NULL );
Paul Bakker562535d2011-01-20 16:42:01 +0000240 TEST_ASSERT ( 0 == md_init_ctx( &ctx, md_info ) );
Paul Bakker17373852011-01-06 14:20:01 +0000241
Paul Bakker33b43f12013-08-20 11:48:36 +0200242 key_len = unhexify( key_str, hex_key_string );
243 src_len = unhexify( src_str, hex_src_string );
Paul Bakker17373852011-01-06 14:20:01 +0000244
Paul Bakker562535d2011-01-20 16:42:01 +0000245 TEST_ASSERT ( 0 == md_hmac_starts( &ctx, key_str, key_len ) );
Paul Bakker17373852011-01-06 14:20:01 +0000246 TEST_ASSERT ( ctx.md_ctx != NULL );
247 TEST_ASSERT ( 0 == md_hmac_update( &ctx, src_str, src_len ) );
248 TEST_ASSERT ( 0 == md_hmac_finish( &ctx, output ) );
Manuel Pégourié-Gonnard59ba4e92014-03-29 14:43:44 +0100249
250 hexify( hash_str, output, md_get_size(md_info) );
251 TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
252
253 /* Test again, for reset() */
254 memset(hash_str, 0x00, 10000);
255 memset(output, 0x00, 100);
256
257 TEST_ASSERT ( 0 == md_hmac_reset( &ctx ) );
258 TEST_ASSERT ( 0 == md_hmac_update( &ctx, src_str, src_len ) );
259 TEST_ASSERT ( 0 == md_hmac_finish( &ctx, output ) );
Paul Bakker17373852011-01-06 14:20:01 +0000260 TEST_ASSERT ( 0 == md_free_ctx( &ctx ) );
Paul Bakker33b43f12013-08-20 11:48:36 +0200261
Paul Bakker17373852011-01-06 14:20:01 +0000262 hexify( hash_str, output, md_get_size(md_info) );
Paul Bakker33b43f12013-08-20 11:48:36 +0200263 TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 );
Manuel Pégourié-Gonnard59ba4e92014-03-29 14:43:44 +0100264
265 TEST_ASSERT ( 0 == md_free_ctx( &ctx ) );
Paul Bakker17373852011-01-06 14:20:01 +0000266}
Paul Bakker33b43f12013-08-20 11:48:36 +0200267/* END_CASE */
Paul Bakker428b9ba2013-09-15 15:20:37 +0200268
269/* BEGIN_CASE depends_on:POLARSSL_FS_IO */
Paul Bakker33b43f12013-08-20 11:48:36 +0200270void md_file( char *text_md_name, char *filename, char *hex_hash_string )
Paul Bakker17373852011-01-06 14:20:01 +0000271{
272 char md_name[100];
273 unsigned char hash_str[1000];
274 unsigned char output[100];
275 const md_info_t *md_info = NULL;
276
277 memset(md_name, 0x00, 100);
278 memset(hash_str, 0x00, 1000);
279 memset(output, 0x00, 100);
280
Paul Bakker33b43f12013-08-20 11:48:36 +0200281 strncpy( (char *) md_name, text_md_name, 100 );
Paul Bakker17373852011-01-06 14:20:01 +0000282 md_info = md_info_from_string( md_name );
283 TEST_ASSERT( md_info != NULL );
284
Paul Bakker33b43f12013-08-20 11:48:36 +0200285 md_file( md_info, filename, output);
Paul Bakker17373852011-01-06 14:20:01 +0000286 hexify( hash_str, output, md_get_size(md_info) );
287
Paul Bakker33b43f12013-08-20 11:48:36 +0200288 TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 );
Paul Bakker17373852011-01-06 14:20:01 +0000289}
Paul Bakker33b43f12013-08-20 11:48:36 +0200290/* END_CASE */