1/* BEGIN_HEADER */
2#include "mbedtls/blowfish.h"
3/* END_HEADER */
4
5/* BEGIN_DEPENDENCIES
6 * depends_on:MBEDTLS_BLOWFISH_C
7 * END_DEPENDENCIES
8 */
9
10/* BEGIN_CASE */
11void blowfish_encrypt_ecb( char *hex_key_string, char *hex_src_string,
12                           char *hex_dst_string, int setkey_result )
13{
14    unsigned char key_str[100];
15    unsigned char src_str[100];
16    unsigned char dst_str[100];
17    unsigned char output[100];
18    mbedtls_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);
25    mbedtls_blowfish_init( &ctx );
26
27    key_len = unhexify( key_str, hex_key_string );
28    unhexify( src_str, hex_src_string );
29
30    TEST_ASSERT( mbedtls_blowfish_setkey( &ctx, key_str, key_len * 8 ) == setkey_result );
31    if( setkey_result == 0 )
32    {
33        TEST_ASSERT( mbedtls_blowfish_crypt_ecb( &ctx, MBEDTLS_BLOWFISH_ENCRYPT, src_str, output ) == 0 );
34        hexify( dst_str, output, 8 );
35
36        TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
37    }
38
39exit:
40    mbedtls_blowfish_free( &ctx );
41}
42/* END_CASE */
43
44/* BEGIN_CASE */
45void blowfish_decrypt_ecb( char *hex_key_string, char *hex_src_string,
46                           char *hex_dst_string, int setkey_result )
47{
48    unsigned char key_str[100];
49    unsigned char src_str[100];
50    unsigned char dst_str[100];
51    unsigned char output[100];
52    mbedtls_blowfish_context ctx;
53    int key_len;
54
55    memset(key_str, 0x00, 100);
56    memset(src_str, 0x00, 100);
57    memset(dst_str, 0x00, 100);
58    memset(output, 0x00, 100);
59    mbedtls_blowfish_init( &ctx );
60
61    key_len = unhexify( key_str, hex_key_string );
62    unhexify( src_str, hex_src_string );
63
64    TEST_ASSERT( mbedtls_blowfish_setkey( &ctx, key_str, key_len * 8 ) == setkey_result );
65    if( setkey_result == 0 )
66    {
67        TEST_ASSERT( mbedtls_blowfish_crypt_ecb( &ctx, MBEDTLS_BLOWFISH_DECRYPT, src_str, output ) == 0 );
68        hexify( dst_str, output, 8 );
69
70        TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
71    }
72
73exit:
74    mbedtls_blowfish_free( &ctx );
75}
76/* END_CASE */
77
78/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
79void blowfish_encrypt_cbc( char *hex_key_string, char *hex_iv_string,
80                           char *hex_src_string, char *hex_dst_string,
81                           int cbc_result )
82{
83    unsigned char key_str[100];
84    unsigned char iv_str[100];
85    unsigned char src_str[100];
86    unsigned char dst_str[100];
87    unsigned char output[100];
88    mbedtls_blowfish_context ctx;
89    int key_len, data_len;
90
91    memset(key_str, 0x00, 100);
92    memset(iv_str, 0x00, 100);
93    memset(src_str, 0x00, 100);
94    memset(dst_str, 0x00, 100);
95    memset(output, 0x00, 100);
96    mbedtls_blowfish_init( &ctx );
97
98    key_len = unhexify( key_str, hex_key_string );
99    unhexify( iv_str, hex_iv_string );
100    data_len = unhexify( src_str, hex_src_string );
101
102    mbedtls_blowfish_setkey( &ctx, key_str, key_len * 8 );
103
104    TEST_ASSERT( mbedtls_blowfish_crypt_cbc( &ctx, MBEDTLS_BLOWFISH_ENCRYPT, data_len , iv_str, src_str, output ) == cbc_result );
105    if( cbc_result == 0 )
106    {
107        hexify( dst_str, output, data_len );
108
109        TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
110    }
111
112exit:
113    mbedtls_blowfish_free( &ctx );
114}
115/* END_CASE */
116
117/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
118void blowfish_decrypt_cbc( char *hex_key_string, char *hex_iv_string,
119                           char *hex_src_string, char *hex_dst_string,
120                           int cbc_result )
121{
122    unsigned char key_str[100];
123    unsigned char iv_str[100];
124    unsigned char src_str[100];
125    unsigned char dst_str[100];
126    unsigned char output[100];
127    mbedtls_blowfish_context ctx;
128    int key_len, data_len;
129
130    memset(key_str, 0x00, 100);
131    memset(iv_str, 0x00, 100);
132    memset(src_str, 0x00, 100);
133    memset(dst_str, 0x00, 100);
134    memset(output, 0x00, 100);
135    mbedtls_blowfish_init( &ctx );
136
137    key_len = unhexify( key_str, hex_key_string );
138    unhexify( iv_str, hex_iv_string );
139    data_len = unhexify( src_str, hex_src_string );
140
141    mbedtls_blowfish_setkey( &ctx, key_str, key_len * 8 );
142    TEST_ASSERT( mbedtls_blowfish_crypt_cbc( &ctx, MBEDTLS_BLOWFISH_DECRYPT, data_len , iv_str, src_str, output ) == cbc_result );
143    if( cbc_result == 0)
144    {
145        hexify( dst_str, output, data_len );
146
147        TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
148    }
149
150exit:
151    mbedtls_blowfish_free( &ctx );
152}
153/* END_CASE */
154
155/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
156void blowfish_encrypt_cfb64( char *hex_key_string, char *hex_iv_string,
157                             char *hex_src_string, char *hex_dst_string )
158{
159    unsigned char key_str[100];
160    unsigned char iv_str[100];
161    unsigned char src_str[100];
162    unsigned char dst_str[100];
163    unsigned char output[100];
164    mbedtls_blowfish_context ctx;
165    size_t iv_offset = 0;
166    int key_len, src_len;
167
168    memset(key_str, 0x00, 100);
169    memset(iv_str, 0x00, 100);
170    memset(src_str, 0x00, 100);
171    memset(dst_str, 0x00, 100);
172    memset(output, 0x00, 100);
173    mbedtls_blowfish_init( &ctx );
174
175    key_len = unhexify( key_str, hex_key_string );
176    unhexify( iv_str, hex_iv_string );
177    src_len = unhexify( src_str, hex_src_string );
178
179    mbedtls_blowfish_setkey( &ctx, key_str, key_len * 8 );
180    TEST_ASSERT( mbedtls_blowfish_crypt_cfb64( &ctx, MBEDTLS_BLOWFISH_ENCRYPT, src_len, &iv_offset, iv_str, src_str, output ) == 0 );
181    hexify( dst_str, output, src_len );
182
183    TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
184
185exit:
186    mbedtls_blowfish_free( &ctx );
187}
188/* END_CASE */
189
190/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
191void blowfish_decrypt_cfb64( char *hex_key_string, char *hex_iv_string,
192                             char *hex_src_string, char *hex_dst_string )
193{
194    unsigned char key_str[100];
195    unsigned char iv_str[100];
196    unsigned char src_str[100];
197    unsigned char dst_str[100];
198    unsigned char output[100];
199    mbedtls_blowfish_context ctx;
200    size_t iv_offset = 0;
201    int key_len, src_len;
202
203    memset(key_str, 0x00, 100);
204    memset(iv_str, 0x00, 100);
205    memset(src_str, 0x00, 100);
206    memset(dst_str, 0x00, 100);
207    memset(output, 0x00, 100);
208    mbedtls_blowfish_init( &ctx );
209
210    key_len = unhexify( key_str, hex_key_string );
211    unhexify( iv_str, hex_iv_string );
212    src_len = unhexify( src_str, hex_src_string );
213
214    mbedtls_blowfish_setkey( &ctx, key_str, key_len * 8 );
215    TEST_ASSERT( mbedtls_blowfish_crypt_cfb64( &ctx, MBEDTLS_BLOWFISH_DECRYPT, src_len, &iv_offset, iv_str, src_str, output ) == 0 );
216    hexify( dst_str, output, src_len );
217
218    TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
219
220exit:
221    mbedtls_blowfish_free( &ctx );
222}
223/* END_CASE */
224
225/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CTR */
226void blowfish_encrypt_ctr( char *hex_key_string, char *hex_iv_string,
227                           char *hex_src_string, char *hex_dst_string )
228{
229    unsigned char key_str[100];
230    unsigned char iv_str[100];
231    unsigned char stream_str[100];
232    unsigned char src_str[100];
233    unsigned char dst_str[100];
234    unsigned char output[100];
235    mbedtls_blowfish_context ctx;
236    size_t iv_offset = 0;
237    int key_len, src_len;
238
239    memset(key_str, 0x00, 100);
240    memset(iv_str, 0x00, 100);
241    memset(stream_str, 0x00, 100);
242    memset(src_str, 0x00, 100);
243    memset(dst_str, 0x00, 100);
244    memset(output, 0x00, 100);
245    mbedtls_blowfish_init( &ctx );
246
247    key_len = unhexify( key_str, hex_key_string );
248    unhexify( iv_str, hex_iv_string );
249    src_len = unhexify( src_str, hex_src_string );
250
251    mbedtls_blowfish_setkey( &ctx, key_str, key_len * 8 );
252    TEST_ASSERT( mbedtls_blowfish_crypt_ctr( &ctx, src_len, &iv_offset, iv_str, stream_str, src_str, output ) == 0 );
253    hexify( dst_str, output, src_len );
254
255    TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
256
257exit:
258    mbedtls_blowfish_free( &ctx );
259}
260/* END_CASE */
261