2 #include "mbedtls/blowfish.h"
6 * depends_on:MBEDTLS_BLOWFISH_C
11 void blowfish_valid_param( )
13 TEST_VALID_PARAM( mbedtls_blowfish_free( NULL ) );
17 /* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */
18 void blowfish_invalid_param( )
20 mbedtls_blowfish_context ctx;
21 unsigned char buf[16] = { 0 };
22 size_t const valid_keylength = sizeof( buf ) * 8;
23 size_t valid_mode = MBEDTLS_BLOWFISH_ENCRYPT;
24 size_t invalid_mode = 42;
28 TEST_INVALID_PARAM( mbedtls_blowfish_init( NULL ) );
29 TEST_VALID_PARAM( mbedtls_blowfish_free( NULL ) );
31 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
32 mbedtls_blowfish_setkey( NULL,
35 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
36 mbedtls_blowfish_setkey( &ctx,
40 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
41 mbedtls_blowfish_crypt_ecb( NULL,
44 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
45 mbedtls_blowfish_crypt_ecb( &ctx,
48 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
49 mbedtls_blowfish_crypt_ecb( &ctx,
52 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
53 mbedtls_blowfish_crypt_ecb( &ctx,
57 #if defined(MBEDTLS_CIPHER_MODE_CBC)
58 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
59 mbedtls_blowfish_crypt_cbc( NULL,
63 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
64 mbedtls_blowfish_crypt_cbc( &ctx,
68 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
69 mbedtls_blowfish_crypt_cbc( &ctx,
73 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
74 mbedtls_blowfish_crypt_cbc( &ctx,
78 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
79 mbedtls_blowfish_crypt_cbc( &ctx,
83 #endif /* MBEDTLS_CIPHER_MODE_CBC */
85 #if defined(MBEDTLS_CIPHER_MODE_CFB)
86 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
87 mbedtls_blowfish_crypt_cfb64( NULL,
92 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
93 mbedtls_blowfish_crypt_cfb64( &ctx,
98 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
99 mbedtls_blowfish_crypt_cfb64( &ctx,
104 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
105 mbedtls_blowfish_crypt_cfb64( &ctx,
110 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
111 mbedtls_blowfish_crypt_cfb64( &ctx,
116 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
117 mbedtls_blowfish_crypt_cfb64( &ctx,
122 #endif /* MBEDTLS_CIPHER_MODE_CFB */
124 #if defined(MBEDTLS_CIPHER_MODE_CTR)
125 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
126 mbedtls_blowfish_crypt_ctr( NULL,
131 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
132 mbedtls_blowfish_crypt_ctr( &ctx,
137 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
138 mbedtls_blowfish_crypt_ctr( &ctx,
143 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
144 mbedtls_blowfish_crypt_ctr( &ctx,
149 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
150 mbedtls_blowfish_crypt_ctr( &ctx,
155 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
156 mbedtls_blowfish_crypt_ctr( &ctx,
161 #endif /* MBEDTLS_CIPHER_MODE_CTR */
169 void blowfish_encrypt_ecb( data_t * key_str, data_t * src_str,
170 data_t * hex_dst_string, int setkey_result )
172 unsigned char output[100];
173 mbedtls_blowfish_context ctx;
175 memset(output, 0x00, 100);
176 mbedtls_blowfish_init( &ctx );
179 TEST_ASSERT( mbedtls_blowfish_setkey( &ctx, key_str->x, key_str->len * 8 ) == setkey_result );
180 if( setkey_result == 0 )
182 TEST_ASSERT( mbedtls_blowfish_crypt_ecb( &ctx, MBEDTLS_BLOWFISH_ENCRYPT, src_str->x, output ) == 0 );
184 TEST_ASSERT( hexcmp( output, hex_dst_string->x, 8, hex_dst_string->len ) == 0 );
188 mbedtls_blowfish_free( &ctx );
193 void blowfish_decrypt_ecb( data_t * key_str, data_t * src_str,
194 data_t * hex_dst_string, int setkey_result )
196 unsigned char output[100];
197 mbedtls_blowfish_context ctx;
199 memset(output, 0x00, 100);
200 mbedtls_blowfish_init( &ctx );
203 TEST_ASSERT( mbedtls_blowfish_setkey( &ctx, key_str->x, key_str->len * 8 ) == setkey_result );
204 if( setkey_result == 0 )
206 TEST_ASSERT( mbedtls_blowfish_crypt_ecb( &ctx, MBEDTLS_BLOWFISH_DECRYPT, src_str->x, output ) == 0 );
208 TEST_ASSERT( hexcmp( output, hex_dst_string->x, 8, hex_dst_string->len ) == 0 );
212 mbedtls_blowfish_free( &ctx );
216 /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
217 void blowfish_encrypt_cbc( data_t * key_str, data_t * iv_str,
218 data_t * src_str, data_t * hex_dst_string,
221 unsigned char output[100];
222 mbedtls_blowfish_context ctx;
224 memset(output, 0x00, 100);
225 mbedtls_blowfish_init( &ctx );
228 mbedtls_blowfish_setkey( &ctx, key_str->x, key_str->len * 8 );
230 TEST_ASSERT( mbedtls_blowfish_crypt_cbc( &ctx, MBEDTLS_BLOWFISH_ENCRYPT, src_str->len , iv_str->x, src_str->x, output ) == cbc_result );
231 if( cbc_result == 0 )
234 TEST_ASSERT( hexcmp( output, hex_dst_string->x, src_str->len, hex_dst_string->len ) == 0 );
238 mbedtls_blowfish_free( &ctx );
242 /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
243 void blowfish_decrypt_cbc( data_t * key_str, data_t * iv_str,
244 data_t * src_str, data_t * hex_dst_string,
247 unsigned char output[100];
248 mbedtls_blowfish_context ctx;
250 memset(output, 0x00, 100);
251 mbedtls_blowfish_init( &ctx );
254 mbedtls_blowfish_setkey( &ctx, key_str->x, key_str->len * 8 );
255 TEST_ASSERT( mbedtls_blowfish_crypt_cbc( &ctx, MBEDTLS_BLOWFISH_DECRYPT, src_str->len , iv_str->x, src_str->x, output ) == cbc_result );
259 TEST_ASSERT( hexcmp( output, hex_dst_string->x, src_str->len, hex_dst_string->len ) == 0 );
263 mbedtls_blowfish_free( &ctx );
267 /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
268 void blowfish_encrypt_cfb64( data_t * key_str, data_t * iv_str,
269 data_t * src_str, data_t * hex_dst_string
272 unsigned char output[100];
273 mbedtls_blowfish_context ctx;
274 size_t iv_offset = 0;
276 memset(output, 0x00, 100);
277 mbedtls_blowfish_init( &ctx );
280 mbedtls_blowfish_setkey( &ctx, key_str->x, key_str->len * 8 );
281 TEST_ASSERT( mbedtls_blowfish_crypt_cfb64( &ctx, MBEDTLS_BLOWFISH_ENCRYPT, src_str->len, &iv_offset, iv_str->x, src_str->x, output ) == 0 );
283 TEST_ASSERT( hexcmp( output, hex_dst_string->x, src_str->len, hex_dst_string->len ) == 0 );
286 mbedtls_blowfish_free( &ctx );
290 /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
291 void blowfish_decrypt_cfb64( data_t * key_str, data_t * iv_str,
292 data_t * src_str, data_t * hex_dst_string
295 unsigned char output[100];
296 mbedtls_blowfish_context ctx;
297 size_t iv_offset = 0;
299 memset(output, 0x00, 100);
300 mbedtls_blowfish_init( &ctx );
303 mbedtls_blowfish_setkey( &ctx, key_str->x, key_str->len * 8 );
304 TEST_ASSERT( mbedtls_blowfish_crypt_cfb64( &ctx, MBEDTLS_BLOWFISH_DECRYPT, src_str->len, &iv_offset, iv_str->x, src_str->x, output ) == 0 );
306 TEST_ASSERT( hexcmp( output, hex_dst_string->x, src_str->len, hex_dst_string->len ) == 0 );
309 mbedtls_blowfish_free( &ctx );
313 /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CTR */
314 void blowfish_encrypt_ctr( data_t * key_str, data_t * iv_str,
315 data_t * src_str, data_t * hex_dst_string )
317 unsigned char stream_str[100];
318 unsigned char output[100];
319 mbedtls_blowfish_context ctx;
320 size_t iv_offset = 0;
322 memset(stream_str, 0x00, 100);
323 memset(output, 0x00, 100);
324 mbedtls_blowfish_init( &ctx );
327 mbedtls_blowfish_setkey( &ctx, key_str->x, key_str->len * 8 );
328 TEST_ASSERT( mbedtls_blowfish_crypt_ctr( &ctx, src_str->len, &iv_offset, iv_str->x, stream_str, src_str->x, output ) == 0 );
330 TEST_ASSERT( hexcmp( output, hex_dst_string->x, src_str->len, hex_dst_string->len ) == 0 );
333 mbedtls_blowfish_free( &ctx );