blob: 015ebc1a259f1eeccd3344ea306f5204e201904d [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;
64
65 memset(key_str, 0x00, 100);
66 memset(iv_str, 0x00, 100);
67 memset(src_str, 0x00, 100);
68 memset(dst_str, 0x00, 100);
69 memset(output, 0x00, 100);
70
71 unhexify( key_str, {hex_key_string} );
72 unhexify( iv_str, {hex_iv_string} );
73 int src_len = unhexify( src_str, {hex_src_string} );
74
75 des_setkey_enc( &ctx, key_str );
76 des_crypt_cbc( &ctx, DES_ENCRYPT, src_len, iv_str, src_str, output );
77 hexify( dst_str, output, src_len );
78
79 TEST_ASSERT( strcasecmp( (char *) dst_str, {hex_dst_string} ) == 0 );
80}
81END_CASE
82
83BEGIN_CASE
84des_decrypt_cbc:hex_key_string:hex_iv_string:hex_src_string:hex_dst_string
85{
86 unsigned char key_str[100];
87 unsigned char iv_str[100];
88 unsigned char src_str[100];
89 unsigned char dst_str[100];
90 unsigned char output[100];
91 des_context ctx;
92
93 memset(key_str, 0x00, 100);
94 memset(iv_str, 0x00, 100);
95 memset(src_str, 0x00, 100);
96 memset(dst_str, 0x00, 100);
97 memset(output, 0x00, 100);
98
99 unhexify( key_str, {hex_key_string} );
100 unhexify( iv_str, {hex_iv_string} );
101 int src_len = unhexify( src_str, {hex_src_string} );
102
103 des_setkey_dec( &ctx, key_str );
104 des_crypt_cbc( &ctx, DES_DECRYPT, src_len, iv_str, src_str, output );
105 hexify( dst_str, output, src_len );
106
107 TEST_ASSERT( strcasecmp( (char *) dst_str, {hex_dst_string} ) == 0 );
108}
109END_CASE
110
111BEGIN_CASE
112des3_encrypt_ecb:key_count:hex_key_string:hex_src_string:hex_dst_string
113{
114 unsigned char key_str[100];
115 unsigned char src_str[100];
116 unsigned char dst_str[100];
117 unsigned char output[100];
118 des3_context ctx;
119
120 memset(key_str, 0x00, 100);
121 memset(src_str, 0x00, 100);
122 memset(dst_str, 0x00, 100);
123 memset(output, 0x00, 100);
124
125 unhexify( key_str, {hex_key_string} );
126 unhexify( src_str, {hex_src_string} );
127
128 if( {key_count} == 2 )
129 des3_set2key_enc( &ctx, key_str );
130 else if( {key_count} == 3 )
131 des3_set3key_enc( &ctx, key_str );
132 else
133 TEST_ASSERT( 0 );
134
135 des3_crypt_ecb( &ctx, src_str, output );
136 hexify( dst_str, output, 8 );
137
138 TEST_ASSERT( strcasecmp( (char *) dst_str, {hex_dst_string} ) == 0 );
139}
140END_CASE
141
142BEGIN_CASE
143des3_decrypt_ecb:key_count:hex_key_string:hex_src_string:hex_dst_string
144{
145 unsigned char key_str[100];
146 unsigned char src_str[100];
147 unsigned char dst_str[100];
148 unsigned char output[100];
149 des3_context ctx;
150
151 memset(key_str, 0x00, 100);
152 memset(src_str, 0x00, 100);
153 memset(dst_str, 0x00, 100);
154 memset(output, 0x00, 100);
155
156 unhexify( key_str, {hex_key_string} );
157 unhexify( src_str, {hex_src_string} );
158
159 if( {key_count} == 2 )
160 des3_set2key_dec( &ctx, key_str );
161 else if( {key_count} == 3 )
162 des3_set3key_dec( &ctx, key_str );
163 else
164 TEST_ASSERT( 0 );
165
166 des3_crypt_ecb( &ctx, src_str, output );
167 hexify( dst_str, output, 8 );
168
169 TEST_ASSERT( strcasecmp( (char *) dst_str, {hex_dst_string} ) == 0 );
170}
171END_CASE
172
173BEGIN_CASE
174des3_encrypt_cbc:key_count:hex_key_string:hex_iv_string:hex_src_string:hex_dst_string
175{
176 unsigned char key_str[100];
177 unsigned char iv_str[100];
178 unsigned char src_str[100];
179 unsigned char dst_str[100];
180 unsigned char output[100];
181 des3_context ctx;
182
183 memset(key_str, 0x00, 100);
184 memset(iv_str, 0x00, 100);
185 memset(src_str, 0x00, 100);
186 memset(dst_str, 0x00, 100);
187 memset(output, 0x00, 100);
188
189 unhexify( key_str, {hex_key_string} );
190 unhexify( iv_str, {hex_iv_string} );
191 int src_len = unhexify( src_str, {hex_src_string} );
192
193 if( {key_count} == 2 )
194 des3_set2key_enc( &ctx, key_str );
195 else if( {key_count} == 3 )
196 des3_set3key_enc( &ctx, key_str );
197 else
198 TEST_ASSERT( 0 );
199
200 des3_crypt_cbc( &ctx, DES_ENCRYPT, src_len, iv_str, src_str, output );
201 hexify( dst_str, output, src_len );
202
203 TEST_ASSERT( strcasecmp( (char *) dst_str, {hex_dst_string} ) == 0 );
204}
205END_CASE
206
207BEGIN_CASE
208des3_decrypt_cbc:key_count:hex_key_string:hex_iv_string:hex_src_string:hex_dst_string
209{
210 unsigned char key_str[100];
211 unsigned char iv_str[100];
212 unsigned char src_str[100];
213 unsigned char dst_str[100];
214 unsigned char output[100];
215 des3_context ctx;
216
217 memset(key_str, 0x00, 100);
218 memset(iv_str, 0x00, 100);
219 memset(src_str, 0x00, 100);
220 memset(dst_str, 0x00, 100);
221 memset(output, 0x00, 100);
222
223 unhexify( key_str, {hex_key_string} );
224 unhexify( iv_str, {hex_iv_string} );
225 int src_len = unhexify( src_str, {hex_src_string} );
226
227 if( {key_count} == 2 )
228 des3_set2key_dec( &ctx, key_str );
229 else if( {key_count} == 3 )
230 des3_set3key_dec( &ctx, key_str );
231 else
232 TEST_ASSERT( 0 );
233
234 des3_crypt_cbc( &ctx, DES_DECRYPT, src_len, iv_str, src_str, output );
235 hexify( dst_str, output, src_len );
236
237 TEST_ASSERT( strcasecmp( (char *) dst_str, {hex_dst_string} ) == 0 );
238}
239END_CASE
240
241BEGIN_CASE
242des_selftest:
243{
244 TEST_ASSERT( des_self_test( 0 ) == 0 );
245}
246END_CASE