blob: 17a5b651baad69513b12633473535ba2bf991020 [file] [log] [blame]
Paul Bakker33b43f12013-08-20 11:48:36 +02001/* BEGIN_HEADER */
Paul Bakkera9379c02012-07-04 11:02:11 +00002#include "polarssl/blowfish.h"
Paul Bakker33b43f12013-08-20 11:48:36 +02003/* END_HEADER */
Paul Bakkera9379c02012-07-04 11:02:11 +00004
Paul Bakker33b43f12013-08-20 11:48:36 +02005/* BEGIN_DEPENDENCIES
6 * depends_on:POLARSSL_BLOWFISH_C
7 * END_DEPENDENCIES
8 */
Paul Bakkera9379c02012-07-04 11:02:11 +00009
Paul Bakker33b43f12013-08-20 11:48:36 +020010/* BEGIN_CASE */
11void blowfish_encrypt_ecb( char *hex_key_string, char *hex_src_string,
12 char *hex_dst_string, int setkey_result )
Paul Bakkera9379c02012-07-04 11:02:11 +000013{
14 unsigned char key_str[100];
15 unsigned char src_str[100];
16 unsigned char dst_str[100];
17 unsigned char output[100];
18 blowfish_context ctx;
19 int key_len;
20
21 memset(key_str, 0x00, 100);
22 memset(src_str, 0x00, 100);
23 memset(dst_str, 0x00, 100);
24 memset(output, 0x00, 100);
Paul Bakker8cfd9d82014-06-18 11:16:11 +020025 blowfish_init( &ctx );
Paul Bakkera9379c02012-07-04 11:02:11 +000026
Paul Bakker33b43f12013-08-20 11:48:36 +020027 key_len = unhexify( key_str, hex_key_string );
28 unhexify( src_str, hex_src_string );
Paul Bakkera9379c02012-07-04 11:02:11 +000029
Paul Bakker33b43f12013-08-20 11:48:36 +020030 TEST_ASSERT( blowfish_setkey( &ctx, key_str, key_len * 8 ) == setkey_result );
31 if( setkey_result == 0 )
Paul Bakkera9379c02012-07-04 11:02:11 +000032 {
33 TEST_ASSERT( blowfish_crypt_ecb( &ctx, BLOWFISH_ENCRYPT, src_str, output ) == 0 );
34 hexify( dst_str, output, 8 );
35
Paul Bakker33b43f12013-08-20 11:48:36 +020036 TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
Paul Bakkera9379c02012-07-04 11:02:11 +000037 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +020038
39 blowfish_free( &ctx );
Paul Bakkera9379c02012-07-04 11:02:11 +000040}
Paul Bakker33b43f12013-08-20 11:48:36 +020041/* END_CASE */
Paul Bakkera9379c02012-07-04 11:02:11 +000042
Paul Bakker33b43f12013-08-20 11:48:36 +020043/* BEGIN_CASE */
44void blowfish_decrypt_ecb( char *hex_key_string, char *hex_src_string,
45 char *hex_dst_string, int setkey_result )
Paul Bakkera9379c02012-07-04 11:02:11 +000046{
47 unsigned char key_str[100];
48 unsigned char src_str[100];
49 unsigned char dst_str[100];
50 unsigned char output[100];
51 blowfish_context ctx;
52 int key_len;
53
54 memset(key_str, 0x00, 100);
55 memset(src_str, 0x00, 100);
56 memset(dst_str, 0x00, 100);
57 memset(output, 0x00, 100);
Paul Bakker8cfd9d82014-06-18 11:16:11 +020058 blowfish_init( &ctx );
Paul Bakkera9379c02012-07-04 11:02:11 +000059
Paul Bakker33b43f12013-08-20 11:48:36 +020060 key_len = unhexify( key_str, hex_key_string );
61 unhexify( src_str, hex_src_string );
Paul Bakkera9379c02012-07-04 11:02:11 +000062
Paul Bakker33b43f12013-08-20 11:48:36 +020063 TEST_ASSERT( blowfish_setkey( &ctx, key_str, key_len * 8 ) == setkey_result );
64 if( setkey_result == 0 )
Paul Bakkera9379c02012-07-04 11:02:11 +000065 {
66 TEST_ASSERT( blowfish_crypt_ecb( &ctx, BLOWFISH_DECRYPT, src_str, output ) == 0 );
67 hexify( dst_str, output, 8 );
68
Paul Bakker33b43f12013-08-20 11:48:36 +020069 TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
Paul Bakkera9379c02012-07-04 11:02:11 +000070 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +020071
72 blowfish_free( &ctx );
Paul Bakkera9379c02012-07-04 11:02:11 +000073}
Paul Bakker33b43f12013-08-20 11:48:36 +020074/* END_CASE */
Paul Bakkera9379c02012-07-04 11:02:11 +000075
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +020076/* BEGIN_CASE depends_on:POLARSSL_CIPHER_MODE_CBC */
Paul Bakker33b43f12013-08-20 11:48:36 +020077void blowfish_encrypt_cbc( char *hex_key_string, char *hex_iv_string,
78 char *hex_src_string, char *hex_dst_string,
79 int cbc_result )
Paul Bakkera9379c02012-07-04 11:02:11 +000080{
81 unsigned char key_str[100];
82 unsigned char iv_str[100];
83 unsigned char src_str[100];
84 unsigned char dst_str[100];
85 unsigned char output[100];
86 blowfish_context ctx;
87 int key_len, data_len;
88
89 memset(key_str, 0x00, 100);
90 memset(iv_str, 0x00, 100);
91 memset(src_str, 0x00, 100);
92 memset(dst_str, 0x00, 100);
93 memset(output, 0x00, 100);
Paul Bakker8cfd9d82014-06-18 11:16:11 +020094 blowfish_init( &ctx );
Paul Bakkera9379c02012-07-04 11:02:11 +000095
Paul Bakker33b43f12013-08-20 11:48:36 +020096 key_len = unhexify( key_str, hex_key_string );
97 unhexify( iv_str, hex_iv_string );
98 data_len = unhexify( src_str, hex_src_string );
Paul Bakkera9379c02012-07-04 11:02:11 +000099
100 blowfish_setkey( &ctx, key_str, key_len * 8 );
101
Paul Bakker33b43f12013-08-20 11:48:36 +0200102 TEST_ASSERT( blowfish_crypt_cbc( &ctx, BLOWFISH_ENCRYPT, data_len , iv_str, src_str, output ) == cbc_result );
103 if( cbc_result == 0 )
Paul Bakkera9379c02012-07-04 11:02:11 +0000104 {
105 hexify( dst_str, output, data_len );
106
Paul Bakker33b43f12013-08-20 11:48:36 +0200107 TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
Paul Bakkera9379c02012-07-04 11:02:11 +0000108 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200109
110 blowfish_free( &ctx );
Paul Bakkera9379c02012-07-04 11:02:11 +0000111}
Paul Bakker33b43f12013-08-20 11:48:36 +0200112/* END_CASE */
Paul Bakkera9379c02012-07-04 11:02:11 +0000113
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200114/* BEGIN_CASE depends_on:POLARSSL_CIPHER_MODE_CBC */
Paul Bakker33b43f12013-08-20 11:48:36 +0200115void blowfish_decrypt_cbc( char *hex_key_string, char *hex_iv_string,
116 char *hex_src_string, char *hex_dst_string,
117 int cbc_result )
Paul Bakkera9379c02012-07-04 11:02:11 +0000118{
119 unsigned char key_str[100];
120 unsigned char iv_str[100];
121 unsigned char src_str[100];
122 unsigned char dst_str[100];
123 unsigned char output[100];
124 blowfish_context ctx;
125 int key_len, data_len;
126
127 memset(key_str, 0x00, 100);
128 memset(iv_str, 0x00, 100);
129 memset(src_str, 0x00, 100);
130 memset(dst_str, 0x00, 100);
131 memset(output, 0x00, 100);
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200132 blowfish_init( &ctx );
Paul Bakkera9379c02012-07-04 11:02:11 +0000133
Paul Bakker33b43f12013-08-20 11:48:36 +0200134 key_len = unhexify( key_str, hex_key_string );
135 unhexify( iv_str, hex_iv_string );
136 data_len = unhexify( src_str, hex_src_string );
Paul Bakkera9379c02012-07-04 11:02:11 +0000137
138 blowfish_setkey( &ctx, key_str, key_len * 8 );
Paul Bakker33b43f12013-08-20 11:48:36 +0200139 TEST_ASSERT( blowfish_crypt_cbc( &ctx, BLOWFISH_DECRYPT, data_len , iv_str, src_str, output ) == cbc_result );
140 if( cbc_result == 0)
Paul Bakkera9379c02012-07-04 11:02:11 +0000141 {
142 hexify( dst_str, output, data_len );
143
Paul Bakker33b43f12013-08-20 11:48:36 +0200144 TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
Paul Bakkera9379c02012-07-04 11:02:11 +0000145 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200146
147 blowfish_free( &ctx );
Paul Bakkera9379c02012-07-04 11:02:11 +0000148}
Paul Bakker33b43f12013-08-20 11:48:36 +0200149/* END_CASE */
Paul Bakkera9379c02012-07-04 11:02:11 +0000150
Manuel Pégourié-Gonnard29dcc0b2014-03-10 11:32:07 +0100151/* BEGIN_CASE depends_on:POLARSSL_CIPHER_MODE_CFB */
Paul Bakker33b43f12013-08-20 11:48:36 +0200152void blowfish_encrypt_cfb64( char *hex_key_string, char *hex_iv_string,
153 char *hex_src_string, char *hex_dst_string )
Paul Bakkera9379c02012-07-04 11:02:11 +0000154{
155 unsigned char key_str[100];
156 unsigned char iv_str[100];
157 unsigned char src_str[100];
158 unsigned char dst_str[100];
159 unsigned char output[100];
160 blowfish_context ctx;
161 size_t iv_offset = 0;
162 int key_len, src_len;
163
164 memset(key_str, 0x00, 100);
165 memset(iv_str, 0x00, 100);
166 memset(src_str, 0x00, 100);
167 memset(dst_str, 0x00, 100);
168 memset(output, 0x00, 100);
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200169 blowfish_init( &ctx );
Paul Bakkera9379c02012-07-04 11:02:11 +0000170
Paul Bakker33b43f12013-08-20 11:48:36 +0200171 key_len = unhexify( key_str, hex_key_string );
172 unhexify( iv_str, hex_iv_string );
173 src_len = unhexify( src_str, hex_src_string );
Paul Bakkera9379c02012-07-04 11:02:11 +0000174
175 blowfish_setkey( &ctx, key_str, key_len * 8 );
176 TEST_ASSERT( blowfish_crypt_cfb64( &ctx, BLOWFISH_ENCRYPT, src_len, &iv_offset, iv_str, src_str, output ) == 0 );
177 hexify( dst_str, output, src_len );
178
Paul Bakker33b43f12013-08-20 11:48:36 +0200179 TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200180
181 blowfish_free( &ctx );
Paul Bakkera9379c02012-07-04 11:02:11 +0000182}
Paul Bakker33b43f12013-08-20 11:48:36 +0200183/* END_CASE */
Paul Bakkera9379c02012-07-04 11:02:11 +0000184
Manuel Pégourié-Gonnard29dcc0b2014-03-10 11:32:07 +0100185/* BEGIN_CASE depends_on:POLARSSL_CIPHER_MODE_CFB */
Paul Bakker33b43f12013-08-20 11:48:36 +0200186void blowfish_decrypt_cfb64( char *hex_key_string, char *hex_iv_string,
187 char *hex_src_string, char *hex_dst_string )
Paul Bakkera9379c02012-07-04 11:02:11 +0000188{
189 unsigned char key_str[100];
190 unsigned char iv_str[100];
191 unsigned char src_str[100];
192 unsigned char dst_str[100];
193 unsigned char output[100];
194 blowfish_context ctx;
195 size_t iv_offset = 0;
196 int key_len, src_len;
197
198 memset(key_str, 0x00, 100);
199 memset(iv_str, 0x00, 100);
200 memset(src_str, 0x00, 100);
201 memset(dst_str, 0x00, 100);
202 memset(output, 0x00, 100);
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200203 blowfish_init( &ctx );
Paul Bakkera9379c02012-07-04 11:02:11 +0000204
Paul Bakker33b43f12013-08-20 11:48:36 +0200205 key_len = unhexify( key_str, hex_key_string );
206 unhexify( iv_str, hex_iv_string );
207 src_len = unhexify( src_str, hex_src_string );
Paul Bakkera9379c02012-07-04 11:02:11 +0000208
209 blowfish_setkey( &ctx, key_str, key_len * 8 );
210 TEST_ASSERT( blowfish_crypt_cfb64( &ctx, BLOWFISH_DECRYPT, src_len, &iv_offset, iv_str, src_str, output ) == 0 );
211 hexify( dst_str, output, src_len );
212
Paul Bakker33b43f12013-08-20 11:48:36 +0200213 TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200214
215 blowfish_free( &ctx );
Paul Bakkera9379c02012-07-04 11:02:11 +0000216}
Paul Bakker33b43f12013-08-20 11:48:36 +0200217/* END_CASE */
Paul Bakkera9379c02012-07-04 11:02:11 +0000218
Manuel Pégourié-Gonnard29dcc0b2014-03-10 11:32:07 +0100219/* BEGIN_CASE depends_on:POLARSSL_CIPHER_MODE_CTR */
Paul Bakker33b43f12013-08-20 11:48:36 +0200220void blowfish_encrypt_ctr( char *hex_key_string, char *hex_iv_string,
221 char *hex_src_string, char *hex_dst_string )
Paul Bakkera9379c02012-07-04 11:02:11 +0000222{
223 unsigned char key_str[100];
224 unsigned char iv_str[100];
225 unsigned char stream_str[100];
226 unsigned char src_str[100];
227 unsigned char dst_str[100];
228 unsigned char output[100];
229 blowfish_context ctx;
230 size_t iv_offset = 0;
231 int key_len, src_len;
232
233 memset(key_str, 0x00, 100);
234 memset(iv_str, 0x00, 100);
235 memset(stream_str, 0x00, 100);
236 memset(src_str, 0x00, 100);
237 memset(dst_str, 0x00, 100);
238 memset(output, 0x00, 100);
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200239 blowfish_init( &ctx );
Paul Bakkera9379c02012-07-04 11:02:11 +0000240
Paul Bakker33b43f12013-08-20 11:48:36 +0200241 key_len = unhexify( key_str, hex_key_string );
242 unhexify( iv_str, hex_iv_string );
243 src_len = unhexify( src_str, hex_src_string );
Paul Bakkera9379c02012-07-04 11:02:11 +0000244
245 blowfish_setkey( &ctx, key_str, key_len * 8 );
246 TEST_ASSERT( blowfish_crypt_ctr( &ctx, src_len, &iv_offset, iv_str, stream_str, src_str, output ) == 0 );
247 hexify( dst_str, output, src_len );
248
Paul Bakker33b43f12013-08-20 11:48:36 +0200249 TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200250
251 blowfish_free( &ctx );
Paul Bakkera9379c02012-07-04 11:02:11 +0000252}
Paul Bakker33b43f12013-08-20 11:48:36 +0200253/* END_CASE */