blob: 839fa97ea589c95c095729552f843341070395fa [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 );
23 des_crypt_ecb( &ctx, src_str, output );
24 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 );
48 des_crypt_ecb( &ctx, src_str, output );
49 hexify( dst_str, output, 8 );
50
51 TEST_ASSERT( strcasecmp( (char *) dst_str, {hex_dst_string} ) == 0 );
52}
53END_CASE
54
55BEGIN_CASE
56des_encrypt_cbc:hex_key_string:hex_iv_string:hex_src_string:hex_dst_string
57{
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 );
77 des_crypt_cbc( &ctx, DES_ENCRYPT, src_len, iv_str, src_str, output );
78 hexify( dst_str, output, src_len );
79
80 TEST_ASSERT( strcasecmp( (char *) dst_str, {hex_dst_string} ) == 0 );
81}
82END_CASE
83
84BEGIN_CASE
85des_decrypt_cbc:hex_key_string:hex_iv_string:hex_src_string:hex_dst_string
86{
87 unsigned char key_str[100];
88 unsigned char iv_str[100];
89 unsigned char src_str[100];
90 unsigned char dst_str[100];
91 unsigned char output[100];
92 des_context ctx;
Paul Bakker69998dd2009-07-11 19:15:20 +000093 int src_len;
Paul Bakkere896fea2009-07-06 06:40:23 +000094
95 memset(key_str, 0x00, 100);
96 memset(iv_str, 0x00, 100);
97 memset(src_str, 0x00, 100);
98 memset(dst_str, 0x00, 100);
99 memset(output, 0x00, 100);
100
101 unhexify( key_str, {hex_key_string} );
102 unhexify( iv_str, {hex_iv_string} );
Paul Bakker69998dd2009-07-11 19:15:20 +0000103 src_len = unhexify( src_str, {hex_src_string} );
Paul Bakkere896fea2009-07-06 06:40:23 +0000104
105 des_setkey_dec( &ctx, key_str );
106 des_crypt_cbc( &ctx, DES_DECRYPT, src_len, iv_str, src_str, output );
107 hexify( dst_str, output, src_len );
108
109 TEST_ASSERT( strcasecmp( (char *) dst_str, {hex_dst_string} ) == 0 );
110}
111END_CASE
112
113BEGIN_CASE
114des3_encrypt_ecb:key_count:hex_key_string:hex_src_string:hex_dst_string
115{
116 unsigned char key_str[100];
117 unsigned char src_str[100];
118 unsigned char dst_str[100];
119 unsigned char output[100];
120 des3_context ctx;
121
122 memset(key_str, 0x00, 100);
123 memset(src_str, 0x00, 100);
124 memset(dst_str, 0x00, 100);
125 memset(output, 0x00, 100);
126
127 unhexify( key_str, {hex_key_string} );
128 unhexify( src_str, {hex_src_string} );
129
130 if( {key_count} == 2 )
131 des3_set2key_enc( &ctx, key_str );
132 else if( {key_count} == 3 )
133 des3_set3key_enc( &ctx, key_str );
134 else
135 TEST_ASSERT( 0 );
136
137 des3_crypt_ecb( &ctx, src_str, output );
138 hexify( dst_str, output, 8 );
139
140 TEST_ASSERT( strcasecmp( (char *) dst_str, {hex_dst_string} ) == 0 );
141}
142END_CASE
143
144BEGIN_CASE
145des3_decrypt_ecb:key_count:hex_key_string:hex_src_string:hex_dst_string
146{
147 unsigned char key_str[100];
148 unsigned char src_str[100];
149 unsigned char dst_str[100];
150 unsigned char output[100];
151 des3_context ctx;
152
153 memset(key_str, 0x00, 100);
154 memset(src_str, 0x00, 100);
155 memset(dst_str, 0x00, 100);
156 memset(output, 0x00, 100);
157
158 unhexify( key_str, {hex_key_string} );
159 unhexify( src_str, {hex_src_string} );
160
161 if( {key_count} == 2 )
162 des3_set2key_dec( &ctx, key_str );
163 else if( {key_count} == 3 )
164 des3_set3key_dec( &ctx, key_str );
165 else
166 TEST_ASSERT( 0 );
167
168 des3_crypt_ecb( &ctx, src_str, output );
169 hexify( dst_str, output, 8 );
170
171 TEST_ASSERT( strcasecmp( (char *) dst_str, {hex_dst_string} ) == 0 );
172}
173END_CASE
174
175BEGIN_CASE
176des3_encrypt_cbc:key_count:hex_key_string:hex_iv_string:hex_src_string:hex_dst_string
177{
178 unsigned char key_str[100];
179 unsigned char iv_str[100];
180 unsigned char src_str[100];
181 unsigned char dst_str[100];
182 unsigned char output[100];
183 des3_context ctx;
Paul Bakker69998dd2009-07-11 19:15:20 +0000184 int src_len;
Paul Bakkere896fea2009-07-06 06:40:23 +0000185
186 memset(key_str, 0x00, 100);
187 memset(iv_str, 0x00, 100);
188 memset(src_str, 0x00, 100);
189 memset(dst_str, 0x00, 100);
190 memset(output, 0x00, 100);
191
192 unhexify( key_str, {hex_key_string} );
193 unhexify( iv_str, {hex_iv_string} );
Paul Bakker69998dd2009-07-11 19:15:20 +0000194 src_len = unhexify( src_str, {hex_src_string} );
Paul Bakkere896fea2009-07-06 06:40:23 +0000195
196 if( {key_count} == 2 )
197 des3_set2key_enc( &ctx, key_str );
198 else if( {key_count} == 3 )
199 des3_set3key_enc( &ctx, key_str );
200 else
201 TEST_ASSERT( 0 );
202
203 des3_crypt_cbc( &ctx, DES_ENCRYPT, src_len, iv_str, src_str, output );
204 hexify( dst_str, output, src_len );
205
206 TEST_ASSERT( strcasecmp( (char *) dst_str, {hex_dst_string} ) == 0 );
207}
208END_CASE
209
210BEGIN_CASE
211des3_decrypt_cbc:key_count:hex_key_string:hex_iv_string:hex_src_string:hex_dst_string
212{
213 unsigned char key_str[100];
214 unsigned char iv_str[100];
215 unsigned char src_str[100];
216 unsigned char dst_str[100];
217 unsigned char output[100];
218 des3_context ctx;
Paul Bakker69998dd2009-07-11 19:15:20 +0000219 int src_len;
Paul Bakkere896fea2009-07-06 06:40:23 +0000220
221 memset(key_str, 0x00, 100);
222 memset(iv_str, 0x00, 100);
223 memset(src_str, 0x00, 100);
224 memset(dst_str, 0x00, 100);
225 memset(output, 0x00, 100);
226
227 unhexify( key_str, {hex_key_string} );
228 unhexify( iv_str, {hex_iv_string} );
Paul Bakker69998dd2009-07-11 19:15:20 +0000229 src_len = unhexify( src_str, {hex_src_string} );
Paul Bakkere896fea2009-07-06 06:40:23 +0000230
231 if( {key_count} == 2 )
232 des3_set2key_dec( &ctx, key_str );
233 else if( {key_count} == 3 )
234 des3_set3key_dec( &ctx, key_str );
235 else
236 TEST_ASSERT( 0 );
237
238 des3_crypt_cbc( &ctx, DES_DECRYPT, src_len, iv_str, src_str, output );
239 hexify( dst_str, output, src_len );
240
241 TEST_ASSERT( strcasecmp( (char *) dst_str, {hex_dst_string} ) == 0 );
242}
243END_CASE
244
245BEGIN_CASE
246des_selftest:
247{
248 TEST_ASSERT( des_self_test( 0 ) == 0 );
249}
250END_CASE