blob: 59458b86e8f79454e70b3a59d9654cf8c97677d7 [file] [log] [blame]
Paul Bakkere896fea2009-07-06 06:40:23 +00001BEGIN_HEADER
2#include <polarssl/des.h>
3END_HEADER
4
5BEGIN_CASE
6des_encrypt_ecb:hex_key_string:hex_src_string:hex_dst_string
7{
8 unsigned char key_str[100];
9 unsigned char src_str[100];
10 unsigned char dst_str[100];
11 unsigned char output[100];
12 des_context ctx;
13
14 memset(key_str, 0x00, 100);
15 memset(src_str, 0x00, 100);
16 memset(dst_str, 0x00, 100);
17 memset(output, 0x00, 100);
18
19 unhexify( key_str, {hex_key_string} );
20 unhexify( src_str, {hex_src_string} );
21
22 des_setkey_enc( &ctx, key_str );
Paul Bakkerf3ccc682010-03-18 21:21:02 +000023 TEST_ASSERT( des_crypt_ecb( &ctx, src_str, output ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +000024 hexify( dst_str, output, 8 );
25
26 TEST_ASSERT( strcasecmp( (char *) dst_str, {hex_dst_string} ) == 0 );
27}
28END_CASE
29
30BEGIN_CASE
31des_decrypt_ecb:hex_key_string:hex_src_string:hex_dst_string
32{
33 unsigned char key_str[100];
34 unsigned char src_str[100];
35 unsigned char dst_str[100];
36 unsigned char output[100];
37 des_context ctx;
38
39 memset(key_str, 0x00, 100);
40 memset(src_str, 0x00, 100);
41 memset(dst_str, 0x00, 100);
42 memset(output, 0x00, 100);
43
44 unhexify( key_str, {hex_key_string} );
45 unhexify( src_str, {hex_src_string} );
46
47 des_setkey_dec( &ctx, key_str );
Paul Bakkerf3ccc682010-03-18 21:21:02 +000048 TEST_ASSERT( des_crypt_ecb( &ctx, src_str, output ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +000049 hexify( dst_str, output, 8 );
50
51 TEST_ASSERT( strcasecmp( (char *) dst_str, {hex_dst_string} ) == 0 );
52}
53END_CASE
54
55BEGIN_CASE
Paul Bakkerf3ccc682010-03-18 21:21:02 +000056des_encrypt_cbc:hex_key_string:hex_iv_string:hex_src_string:hex_dst_string:cbc_result
Paul Bakkere896fea2009-07-06 06:40:23 +000057{
58 unsigned char key_str[100];
59 unsigned char iv_str[100];
60 unsigned char src_str[100];
61 unsigned char dst_str[100];
62 unsigned char output[100];
63 des_context ctx;
Paul Bakker69998dd2009-07-11 19:15:20 +000064 int src_len;
Paul Bakkere896fea2009-07-06 06:40:23 +000065
66 memset(key_str, 0x00, 100);
67 memset(iv_str, 0x00, 100);
68 memset(src_str, 0x00, 100);
69 memset(dst_str, 0x00, 100);
70 memset(output, 0x00, 100);
71
72 unhexify( key_str, {hex_key_string} );
73 unhexify( iv_str, {hex_iv_string} );
Paul Bakker69998dd2009-07-11 19:15:20 +000074 src_len = unhexify( src_str, {hex_src_string} );
Paul Bakkere896fea2009-07-06 06:40:23 +000075
76 des_setkey_enc( &ctx, key_str );
Paul Bakkerf3ccc682010-03-18 21:21:02 +000077 TEST_ASSERT( des_crypt_cbc( &ctx, DES_ENCRYPT, src_len, iv_str, src_str, output ) == {cbc_result} );
78 if( {cbc_result} == 0 )
79 {
80 hexify( dst_str, output, src_len );
Paul Bakkere896fea2009-07-06 06:40:23 +000081
Paul Bakkerf3ccc682010-03-18 21:21:02 +000082 TEST_ASSERT( strcasecmp( (char *) dst_str, {hex_dst_string} ) == 0 );
83 }
Paul Bakkere896fea2009-07-06 06:40:23 +000084}
85END_CASE
86
87BEGIN_CASE
Paul Bakkerf3ccc682010-03-18 21:21:02 +000088des_decrypt_cbc:hex_key_string:hex_iv_string:hex_src_string:hex_dst_string:cbc_result
Paul Bakkere896fea2009-07-06 06:40:23 +000089{
90 unsigned char key_str[100];
91 unsigned char iv_str[100];
92 unsigned char src_str[100];
93 unsigned char dst_str[100];
94 unsigned char output[100];
95 des_context ctx;
Paul Bakker69998dd2009-07-11 19:15:20 +000096 int src_len;
Paul Bakkere896fea2009-07-06 06:40:23 +000097
98 memset(key_str, 0x00, 100);
99 memset(iv_str, 0x00, 100);
100 memset(src_str, 0x00, 100);
101 memset(dst_str, 0x00, 100);
102 memset(output, 0x00, 100);
103
104 unhexify( key_str, {hex_key_string} );
105 unhexify( iv_str, {hex_iv_string} );
Paul Bakker69998dd2009-07-11 19:15:20 +0000106 src_len = unhexify( src_str, {hex_src_string} );
Paul Bakkere896fea2009-07-06 06:40:23 +0000107
108 des_setkey_dec( &ctx, key_str );
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000109 TEST_ASSERT( des_crypt_cbc( &ctx, DES_DECRYPT, src_len, iv_str, src_str, output ) == {cbc_result} );
110 if( {cbc_result} == 0 )
111 {
112 hexify( dst_str, output, src_len );
Paul Bakkere896fea2009-07-06 06:40:23 +0000113
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000114 TEST_ASSERT( strcasecmp( (char *) dst_str, {hex_dst_string} ) == 0 );
115 }
Paul Bakkere896fea2009-07-06 06:40:23 +0000116}
117END_CASE
118
119BEGIN_CASE
120des3_encrypt_ecb:key_count:hex_key_string:hex_src_string:hex_dst_string
121{
122 unsigned char key_str[100];
123 unsigned char src_str[100];
124 unsigned char dst_str[100];
125 unsigned char output[100];
126 des3_context ctx;
127
128 memset(key_str, 0x00, 100);
129 memset(src_str, 0x00, 100);
130 memset(dst_str, 0x00, 100);
131 memset(output, 0x00, 100);
132
133 unhexify( key_str, {hex_key_string} );
134 unhexify( src_str, {hex_src_string} );
135
136 if( {key_count} == 2 )
137 des3_set2key_enc( &ctx, key_str );
138 else if( {key_count} == 3 )
139 des3_set3key_enc( &ctx, key_str );
140 else
141 TEST_ASSERT( 0 );
142
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000143 TEST_ASSERT( des3_crypt_ecb( &ctx, src_str, output ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000144 hexify( dst_str, output, 8 );
145
146 TEST_ASSERT( strcasecmp( (char *) dst_str, {hex_dst_string} ) == 0 );
147}
148END_CASE
149
150BEGIN_CASE
151des3_decrypt_ecb:key_count:hex_key_string:hex_src_string:hex_dst_string
152{
153 unsigned char key_str[100];
154 unsigned char src_str[100];
155 unsigned char dst_str[100];
156 unsigned char output[100];
157 des3_context ctx;
158
159 memset(key_str, 0x00, 100);
160 memset(src_str, 0x00, 100);
161 memset(dst_str, 0x00, 100);
162 memset(output, 0x00, 100);
163
164 unhexify( key_str, {hex_key_string} );
165 unhexify( src_str, {hex_src_string} );
166
167 if( {key_count} == 2 )
168 des3_set2key_dec( &ctx, key_str );
169 else if( {key_count} == 3 )
170 des3_set3key_dec( &ctx, key_str );
171 else
172 TEST_ASSERT( 0 );
173
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000174 TEST_ASSERT( des3_crypt_ecb( &ctx, src_str, output ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000175 hexify( dst_str, output, 8 );
176
177 TEST_ASSERT( strcasecmp( (char *) dst_str, {hex_dst_string} ) == 0 );
178}
179END_CASE
180
181BEGIN_CASE
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000182des3_encrypt_cbc:key_count:hex_key_string:hex_iv_string:hex_src_string:hex_dst_string:cbc_result
Paul Bakkere896fea2009-07-06 06:40:23 +0000183{
184 unsigned char key_str[100];
185 unsigned char iv_str[100];
186 unsigned char src_str[100];
187 unsigned char dst_str[100];
188 unsigned char output[100];
189 des3_context ctx;
Paul Bakker69998dd2009-07-11 19:15:20 +0000190 int src_len;
Paul Bakkere896fea2009-07-06 06:40:23 +0000191
192 memset(key_str, 0x00, 100);
193 memset(iv_str, 0x00, 100);
194 memset(src_str, 0x00, 100);
195 memset(dst_str, 0x00, 100);
196 memset(output, 0x00, 100);
197
198 unhexify( key_str, {hex_key_string} );
199 unhexify( iv_str, {hex_iv_string} );
Paul Bakker69998dd2009-07-11 19:15:20 +0000200 src_len = unhexify( src_str, {hex_src_string} );
Paul Bakkere896fea2009-07-06 06:40:23 +0000201
202 if( {key_count} == 2 )
203 des3_set2key_enc( &ctx, key_str );
204 else if( {key_count} == 3 )
205 des3_set3key_enc( &ctx, key_str );
206 else
207 TEST_ASSERT( 0 );
208
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000209 TEST_ASSERT( des3_crypt_cbc( &ctx, DES_ENCRYPT, src_len, iv_str, src_str, output ) == {cbc_result} );
210 if( {cbc_result} == 0 )
211 {
212 hexify( dst_str, output, src_len );
Paul Bakkere896fea2009-07-06 06:40:23 +0000213
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000214 TEST_ASSERT( strcasecmp( (char *) dst_str, {hex_dst_string} ) == 0 );
215 }
Paul Bakkere896fea2009-07-06 06:40:23 +0000216}
217END_CASE
218
219BEGIN_CASE
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000220des3_decrypt_cbc:key_count:hex_key_string:hex_iv_string:hex_src_string:hex_dst_string:cbc_result
Paul Bakkere896fea2009-07-06 06:40:23 +0000221{
222 unsigned char key_str[100];
223 unsigned char iv_str[100];
224 unsigned char src_str[100];
225 unsigned char dst_str[100];
226 unsigned char output[100];
227 des3_context ctx;
Paul Bakker69998dd2009-07-11 19:15:20 +0000228 int src_len;
Paul Bakkere896fea2009-07-06 06:40:23 +0000229
230 memset(key_str, 0x00, 100);
231 memset(iv_str, 0x00, 100);
232 memset(src_str, 0x00, 100);
233 memset(dst_str, 0x00, 100);
234 memset(output, 0x00, 100);
235
236 unhexify( key_str, {hex_key_string} );
237 unhexify( iv_str, {hex_iv_string} );
Paul Bakker69998dd2009-07-11 19:15:20 +0000238 src_len = unhexify( src_str, {hex_src_string} );
Paul Bakkere896fea2009-07-06 06:40:23 +0000239
240 if( {key_count} == 2 )
241 des3_set2key_dec( &ctx, key_str );
242 else if( {key_count} == 3 )
243 des3_set3key_dec( &ctx, key_str );
244 else
245 TEST_ASSERT( 0 );
246
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000247 TEST_ASSERT( des3_crypt_cbc( &ctx, DES_DECRYPT, src_len, iv_str, src_str, output ) == {cbc_result} )
248 if( {cbc_result} == 0 )
249 {
250 hexify( dst_str, output, src_len );
Paul Bakkere896fea2009-07-06 06:40:23 +0000251
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000252 TEST_ASSERT( strcasecmp( (char *) dst_str, {hex_dst_string} ) == 0 );
253 }
Paul Bakkere896fea2009-07-06 06:40:23 +0000254}
255END_CASE
256
257BEGIN_CASE
258des_selftest:
259{
260 TEST_ASSERT( des_self_test( 0 ) == 0 );
261}
262END_CASE