blob: 486eacd8fe84c930f582cb7a329247cb4d8defec [file] [log] [blame]
Paul Bakker17373852011-01-06 14:20:01 +00001BEGIN_HEADER
2#include <polarssl/config.h>
3#include <polarssl/md.h>
4#include <polarssl/md2.h>
5#include <polarssl/md4.h>
6#include <polarssl/md5.h>
7#include <polarssl/sha1.h>
8#include <polarssl/sha2.h>
9#include <polarssl/sha4.h>
10END_HEADER
11
12BEGIN_CASE
13md_text:text_md_name:text_src_string:hex_hash_string
14{
15 char md_name[100];
16 unsigned char src_str[1000];
17 unsigned char hash_str[1000];
18 unsigned char output[100];
19 const md_info_t *md_info = NULL;
20
21 memset(md_name, 0x00, 100);
22 memset(src_str, 0x00, 1000);
23 memset(hash_str, 0x00, 1000);
24 memset(output, 0x00, 100);
25
26 strcpy( (char *) src_str, {text_src_string} );
27
28 strncpy( (char *) md_name, {text_md_name}, 100 );
29 md_info = md_info_from_string(md_name);
30 TEST_ASSERT( md_info != NULL );
31
32 TEST_ASSERT ( 0 == md( md_info, src_str, strlen( (char *) src_str ), output ) );
33 hexify( hash_str, output, md_get_size(md_info) );
34
35 TEST_ASSERT( strcmp( (char *) hash_str, {hex_hash_string} ) == 0 );
36}
37END_CASE
38
39BEGIN_CASE
40md_hex:text_md_name:hex_src_string:hex_hash_string
41{
42 char md_name[100];
43 unsigned char src_str[10000];
44 unsigned char hash_str[10000];
45 unsigned char output[100];
46 int src_len;
47 const md_info_t *md_info = NULL;
48
49 memset(md_name, 0x00, 100);
50 memset(src_str, 0x00, 10000);
51 memset(hash_str, 0x00, 10000);
52 memset(output, 0x00, 100);
53
54 strncpy( (char *) md_name, {text_md_name}, 100 );
55 md_info = md_info_from_string(md_name);
56 TEST_ASSERT( md_info != NULL );
57
58 src_len = unhexify( src_str, {hex_src_string} );
59 TEST_ASSERT ( 0 == md( md_info, src_str, src_len, output ) );
60
61 hexify( hash_str, output, md_get_size(md_info) );
62
63 TEST_ASSERT( strcmp( (char *) hash_str, {hex_hash_string} ) == 0 );
64}
65END_CASE
66
67BEGIN_CASE
68md_text_multi:text_md_name:text_src_string:hex_hash_string
69{
70 char md_name[100];
71 unsigned char src_str[1000];
72 unsigned char hash_str[1000];
73 unsigned char output[100];
74
75 const md_info_t *md_info = NULL;
76 md_context_t ctx = MD_CONTEXT_T_INIT;
77
78 memset(md_name, 0x00, 100);
79 memset(src_str, 0x00, 1000);
80 memset(hash_str, 0x00, 1000);
81 memset(output, 0x00, 100);
82
83 strcpy( (char *) src_str, {text_src_string} );
84
85 strncpy( (char *) md_name, {text_md_name}, 100 );
86 md_info = md_info_from_string(md_name);
87 TEST_ASSERT( md_info != NULL );
Paul Bakker562535d2011-01-20 16:42:01 +000088 TEST_ASSERT ( 0 == md_init_ctx( &ctx, md_info ) );
Paul Bakker17373852011-01-06 14:20:01 +000089
Paul Bakker562535d2011-01-20 16:42:01 +000090 TEST_ASSERT ( 0 == md_starts( &ctx ) );
Paul Bakker17373852011-01-06 14:20:01 +000091 TEST_ASSERT ( ctx.md_ctx != NULL );
92 TEST_ASSERT ( 0 == md_update( &ctx, src_str, strlen( (char *) src_str ) ) );
93 TEST_ASSERT ( 0 == md_finish( &ctx, output ) );
94 TEST_ASSERT ( 0 == md_free_ctx( &ctx ) );
95
96 hexify( hash_str, output, md_get_size(md_info) );
97
98 TEST_ASSERT( strcmp( (char *) hash_str, {hex_hash_string} ) == 0 );
99}
100END_CASE
101
102BEGIN_CASE
103md_hex_multi:text_md_name:hex_src_string:hex_hash_string
104{
105 char md_name[100];
106 unsigned char src_str[10000];
107 unsigned char hash_str[10000];
108 unsigned char output[100];
109 int src_len;
110 const md_info_t *md_info = NULL;
111 md_context_t ctx = MD_CONTEXT_T_INIT;
112
113 memset(md_name, 0x00, 100);
114 memset(src_str, 0x00, 10000);
115 memset(hash_str, 0x00, 10000);
116 memset(output, 0x00, 100);
117
118 strncpy( (char *) md_name, {text_md_name}, 100 );
119 md_info = md_info_from_string(md_name);
120 TEST_ASSERT( md_info != NULL );
Paul Bakker562535d2011-01-20 16:42:01 +0000121 TEST_ASSERT ( 0 == md_init_ctx( &ctx, md_info ) );
Paul Bakker17373852011-01-06 14:20:01 +0000122
123 src_len = unhexify( src_str, {hex_src_string} );
124
Paul Bakker562535d2011-01-20 16:42:01 +0000125 TEST_ASSERT ( 0 == md_starts( &ctx ) );
Paul Bakker17373852011-01-06 14:20:01 +0000126 TEST_ASSERT ( ctx.md_ctx != NULL );
127 TEST_ASSERT ( 0 == md_update( &ctx, src_str, src_len ) );
128 TEST_ASSERT ( 0 == md_finish( &ctx, output ) );
129 TEST_ASSERT ( 0 == md_free_ctx( &ctx ) );
130
131 hexify( hash_str, output, md_get_size(md_info) );
132
133 TEST_ASSERT( strcmp( (char *) hash_str, {hex_hash_string} ) == 0 );
134}
135END_CASE
136
137BEGIN_CASE
138md_hmac:text_md_name:trunc_size:hex_key_string:hex_src_string:hex_hash_string
139{
140 char md_name[100];
141 unsigned char src_str[10000];
142 unsigned char key_str[10000];
143 unsigned char hash_str[10000];
144 unsigned char output[100];
145 int key_len, src_len;
146 const md_info_t *md_info = NULL;
147
148 memset(md_name, 0x00, 100);
149 memset(src_str, 0x00, 10000);
150 memset(key_str, 0x00, 10000);
151 memset(hash_str, 0x00, 10000);
152 memset(output, 0x00, 100);
153
154 strncpy( (char *) md_name, {text_md_name}, 100 );
155 md_info = md_info_from_string( md_name );
156 TEST_ASSERT( md_info != NULL );
157
158 key_len = unhexify( key_str, {hex_key_string} );
159 src_len = unhexify( src_str, {hex_src_string} );
160
161 TEST_ASSERT ( md_hmac( md_info, key_str, key_len, src_str, src_len, output ) == 0 );
162 hexify( hash_str, output, md_get_size(md_info) );
163
164 TEST_ASSERT( strncmp( (char *) hash_str, {hex_hash_string}, {trunc_size} * 2 ) == 0 );
165}
166END_CASE
167
168BEGIN_CASE
169md_hmac_multi:text_md_name:trunc_size:hex_key_string:hex_src_string:hex_hash_string
170{
171 char md_name[100];
172 unsigned char src_str[10000];
173 unsigned char key_str[10000];
174 unsigned char hash_str[10000];
175 unsigned char output[100];
176 int key_len, src_len;
177 const md_info_t *md_info = NULL;
178 md_context_t ctx = MD_CONTEXT_T_INIT;
179
180 memset(md_name, 0x00, 100);
181 memset(src_str, 0x00, 10000);
182 memset(key_str, 0x00, 10000);
183 memset(hash_str, 0x00, 10000);
184 memset(output, 0x00, 100);
185
186 strncpy( (char *) md_name, {text_md_name}, 100 );
187 md_info = md_info_from_string( md_name );
188 TEST_ASSERT( md_info != NULL );
Paul Bakker562535d2011-01-20 16:42:01 +0000189 TEST_ASSERT ( 0 == md_init_ctx( &ctx, md_info ) );
Paul Bakker17373852011-01-06 14:20:01 +0000190
191 key_len = unhexify( key_str, {hex_key_string} );
192 src_len = unhexify( src_str, {hex_src_string} );
193
Paul Bakker562535d2011-01-20 16:42:01 +0000194 TEST_ASSERT ( 0 == md_hmac_starts( &ctx, key_str, key_len ) );
Paul Bakker17373852011-01-06 14:20:01 +0000195 TEST_ASSERT ( ctx.md_ctx != NULL );
196 TEST_ASSERT ( 0 == md_hmac_update( &ctx, src_str, src_len ) );
197 TEST_ASSERT ( 0 == md_hmac_finish( &ctx, output ) );
198 TEST_ASSERT ( 0 == md_free_ctx( &ctx ) );
199
200 hexify( hash_str, output, md_get_size(md_info) );
201
202 TEST_ASSERT( strncmp( (char *) hash_str, {hex_hash_string}, {trunc_size} * 2 ) == 0 );
203}
204END_CASE
205BEGIN_CASE
206md_file:text_md_name:filename:hex_hash_string
207{
208 char md_name[100];
209 unsigned char hash_str[1000];
210 unsigned char output[100];
211 const md_info_t *md_info = NULL;
212
213 memset(md_name, 0x00, 100);
214 memset(hash_str, 0x00, 1000);
215 memset(output, 0x00, 100);
216
217 strncpy( (char *) md_name, {text_md_name}, 100 );
218 md_info = md_info_from_string( md_name );
219 TEST_ASSERT( md_info != NULL );
220
221 md_file( md_info, {filename}, output);
222 hexify( hash_str, output, md_get_size(md_info) );
223
224 TEST_ASSERT( strcmp( (char *) hash_str, {hex_hash_string} ) == 0 );
225}
226END_CASE