blob: 1fa59ee6e9df197a4be4d9eb105d22a53d40477c [file] [log] [blame]
Paul Bakker33b43f12013-08-20 11:48:36 +02001/* BEGIN_HEADER */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002#include "mbedtls/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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006 * depends_on:MBEDTLS_BLOWFISH_C
Paul Bakker33b43f12013-08-20 11:48:36 +02007 * END_DEPENDENCIES
8 */
Paul Bakkera9379c02012-07-04 11:02:11 +00009
TRodziewicz062f3532021-05-25 15:15:57 +020010/* BEGIN_CASE depends_on:NOT_DEFINED */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050011void blowfish_invalid_param( )
12{
13 mbedtls_blowfish_context ctx;
14 unsigned char buf[16] = { 0 };
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050015 size_t invalid_mode = 42;
16 size_t off;
17 ((void) off);
18
Ronald Cron875b5fb2021-05-21 08:50:00 +020019 TEST_EQUAL( MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050020 mbedtls_blowfish_crypt_ecb( &ctx,
21 invalid_mode,
22 buf, buf ) );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050023
24#if defined(MBEDTLS_CIPHER_MODE_CBC)
Ronald Cron875b5fb2021-05-21 08:50:00 +020025 TEST_EQUAL( MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050026 mbedtls_blowfish_crypt_cbc( &ctx,
27 invalid_mode,
28 sizeof( buf ),
29 buf, buf, buf ) );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050030#endif /* MBEDTLS_CIPHER_MODE_CBC */
31
32#if defined(MBEDTLS_CIPHER_MODE_CFB)
Ronald Cron875b5fb2021-05-21 08:50:00 +020033 TEST_EQUAL( MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050034 mbedtls_blowfish_crypt_cfb64( &ctx,
35 invalid_mode,
36 sizeof( buf ),
37 &off, buf,
38 buf, buf ) );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050039#endif /* MBEDTLS_CIPHER_MODE_CFB */
40
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050041exit:
42 return;
43}
44/* END_CASE */
45
46/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +010047void blowfish_encrypt_ecb( data_t * key_str, data_t * src_str,
Ronald Cronac6ae352020-06-26 14:33:03 +020048 data_t * dst, int setkey_result )
Paul Bakkera9379c02012-07-04 11:02:11 +000049{
Paul Bakkera9379c02012-07-04 11:02:11 +000050 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051 mbedtls_blowfish_context ctx;
Paul Bakkera9379c02012-07-04 11:02:11 +000052
Paul Bakkera9379c02012-07-04 11:02:11 +000053 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054 mbedtls_blowfish_init( &ctx );
Paul Bakkera9379c02012-07-04 11:02:11 +000055
Paul Bakkera9379c02012-07-04 11:02:11 +000056
Azim Khand30ca132017-06-09 04:32:58 +010057 TEST_ASSERT( mbedtls_blowfish_setkey( &ctx, key_str->x, key_str->len * 8 ) == setkey_result );
Paul Bakker33b43f12013-08-20 11:48:36 +020058 if( setkey_result == 0 )
Paul Bakkera9379c02012-07-04 11:02:11 +000059 {
Azim Khand30ca132017-06-09 04:32:58 +010060 TEST_ASSERT( mbedtls_blowfish_crypt_ecb( &ctx, MBEDTLS_BLOWFISH_ENCRYPT, src_str->x, output ) == 0 );
Paul Bakkera9379c02012-07-04 11:02:11 +000061
Ronald Cronac6ae352020-06-26 14:33:03 +020062 TEST_ASSERT( mbedtls_test_hexcmp( output, dst->x, 8, dst->len ) == 0 );
Paul Bakkera9379c02012-07-04 11:02:11 +000063 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +020064
Paul Bakkerbd51b262014-07-10 15:26:12 +020065exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066 mbedtls_blowfish_free( &ctx );
Paul Bakkera9379c02012-07-04 11:02:11 +000067}
Paul Bakker33b43f12013-08-20 11:48:36 +020068/* END_CASE */
Paul Bakkera9379c02012-07-04 11:02:11 +000069
Paul Bakker33b43f12013-08-20 11:48:36 +020070/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +010071void blowfish_decrypt_ecb( data_t * key_str, data_t * src_str,
Ronald Cronac6ae352020-06-26 14:33:03 +020072 data_t * dst, int setkey_result )
Paul Bakkera9379c02012-07-04 11:02:11 +000073{
Paul Bakkera9379c02012-07-04 11:02:11 +000074 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075 mbedtls_blowfish_context ctx;
Paul Bakkera9379c02012-07-04 11:02:11 +000076
Paul Bakkera9379c02012-07-04 11:02:11 +000077 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078 mbedtls_blowfish_init( &ctx );
Paul Bakkera9379c02012-07-04 11:02:11 +000079
Paul Bakkera9379c02012-07-04 11:02:11 +000080
Azim Khand30ca132017-06-09 04:32:58 +010081 TEST_ASSERT( mbedtls_blowfish_setkey( &ctx, key_str->x, key_str->len * 8 ) == setkey_result );
Paul Bakker33b43f12013-08-20 11:48:36 +020082 if( setkey_result == 0 )
Paul Bakkera9379c02012-07-04 11:02:11 +000083 {
Azim Khand30ca132017-06-09 04:32:58 +010084 TEST_ASSERT( mbedtls_blowfish_crypt_ecb( &ctx, MBEDTLS_BLOWFISH_DECRYPT, src_str->x, output ) == 0 );
Paul Bakkera9379c02012-07-04 11:02:11 +000085
Ronald Cronac6ae352020-06-26 14:33:03 +020086 TEST_ASSERT( mbedtls_test_hexcmp( output, dst->x, 8, dst->len ) == 0 );
Paul Bakkera9379c02012-07-04 11:02:11 +000087 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +020088
Paul Bakkerbd51b262014-07-10 15:26:12 +020089exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090 mbedtls_blowfish_free( &ctx );
Paul Bakkera9379c02012-07-04 11:02:11 +000091}
Paul Bakker33b43f12013-08-20 11:48:36 +020092/* END_CASE */
Paul Bakkera9379c02012-07-04 11:02:11 +000093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
Azim Khan5fcca462018-06-29 11:05:32 +010095void blowfish_encrypt_cbc( data_t * key_str, data_t * iv_str,
Ronald Cronac6ae352020-06-26 14:33:03 +020096 data_t * src_str, data_t * dst,
Azim Khand30ca132017-06-09 04:32:58 +010097 int cbc_result )
Paul Bakkera9379c02012-07-04 11:02:11 +000098{
Paul Bakkera9379c02012-07-04 11:02:11 +000099 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100 mbedtls_blowfish_context ctx;
Paul Bakkera9379c02012-07-04 11:02:11 +0000101
Paul Bakkera9379c02012-07-04 11:02:11 +0000102 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103 mbedtls_blowfish_init( &ctx );
Paul Bakkera9379c02012-07-04 11:02:11 +0000104
Paul Bakkera9379c02012-07-04 11:02:11 +0000105
Azim Khand30ca132017-06-09 04:32:58 +0100106 mbedtls_blowfish_setkey( &ctx, key_str->x, key_str->len * 8 );
Paul Bakkera9379c02012-07-04 11:02:11 +0000107
Azim Khand30ca132017-06-09 04:32:58 +0100108 TEST_ASSERT( mbedtls_blowfish_crypt_cbc( &ctx, MBEDTLS_BLOWFISH_ENCRYPT, src_str->len , iv_str->x, src_str->x, output ) == cbc_result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200109 if( cbc_result == 0 )
Paul Bakkera9379c02012-07-04 11:02:11 +0000110 {
Paul Bakkera9379c02012-07-04 11:02:11 +0000111
Ronald Cronac6ae352020-06-26 14:33:03 +0200112 TEST_ASSERT( mbedtls_test_hexcmp( output, dst->x,
113 src_str->len, dst->len ) == 0 );
Paul Bakkera9379c02012-07-04 11:02:11 +0000114 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200115
Paul Bakkerbd51b262014-07-10 15:26:12 +0200116exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117 mbedtls_blowfish_free( &ctx );
Paul Bakkera9379c02012-07-04 11:02:11 +0000118}
Paul Bakker33b43f12013-08-20 11:48:36 +0200119/* END_CASE */
Paul Bakkera9379c02012-07-04 11:02:11 +0000120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
Azim Khan5fcca462018-06-29 11:05:32 +0100122void blowfish_decrypt_cbc( data_t * key_str, data_t * iv_str,
Ronald Cronac6ae352020-06-26 14:33:03 +0200123 data_t * src_str, data_t * dst,
Azim Khand30ca132017-06-09 04:32:58 +0100124 int cbc_result )
Paul Bakkera9379c02012-07-04 11:02:11 +0000125{
Paul Bakkera9379c02012-07-04 11:02:11 +0000126 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127 mbedtls_blowfish_context ctx;
Paul Bakkera9379c02012-07-04 11:02:11 +0000128
Paul Bakkera9379c02012-07-04 11:02:11 +0000129 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130 mbedtls_blowfish_init( &ctx );
Paul Bakkera9379c02012-07-04 11:02:11 +0000131
Paul Bakkera9379c02012-07-04 11:02:11 +0000132
Azim Khand30ca132017-06-09 04:32:58 +0100133 mbedtls_blowfish_setkey( &ctx, key_str->x, key_str->len * 8 );
134 TEST_ASSERT( mbedtls_blowfish_crypt_cbc( &ctx, MBEDTLS_BLOWFISH_DECRYPT, src_str->len , iv_str->x, src_str->x, output ) == cbc_result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200135 if( cbc_result == 0)
Paul Bakkera9379c02012-07-04 11:02:11 +0000136 {
Paul Bakkera9379c02012-07-04 11:02:11 +0000137
Ronald Cronac6ae352020-06-26 14:33:03 +0200138 TEST_ASSERT( mbedtls_test_hexcmp( output, dst->x, src_str->len,
139 dst->len ) == 0 );
Paul Bakkera9379c02012-07-04 11:02:11 +0000140 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200141
Paul Bakkerbd51b262014-07-10 15:26:12 +0200142exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143 mbedtls_blowfish_free( &ctx );
Paul Bakkera9379c02012-07-04 11:02:11 +0000144}
Paul Bakker33b43f12013-08-20 11:48:36 +0200145/* END_CASE */
Paul Bakkera9379c02012-07-04 11:02:11 +0000146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
Azim Khan5fcca462018-06-29 11:05:32 +0100148void blowfish_encrypt_cfb64( data_t * key_str, data_t * iv_str,
Ronald Cronac6ae352020-06-26 14:33:03 +0200149 data_t * src_str, data_t * dst )
Paul Bakkera9379c02012-07-04 11:02:11 +0000150{
Paul Bakkera9379c02012-07-04 11:02:11 +0000151 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152 mbedtls_blowfish_context ctx;
Paul Bakkera9379c02012-07-04 11:02:11 +0000153 size_t iv_offset = 0;
Paul Bakkera9379c02012-07-04 11:02:11 +0000154
Paul Bakkera9379c02012-07-04 11:02:11 +0000155 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156 mbedtls_blowfish_init( &ctx );
Paul Bakkera9379c02012-07-04 11:02:11 +0000157
Paul Bakkera9379c02012-07-04 11:02:11 +0000158
Azim Khand30ca132017-06-09 04:32:58 +0100159 mbedtls_blowfish_setkey( &ctx, key_str->x, key_str->len * 8 );
160 TEST_ASSERT( mbedtls_blowfish_crypt_cfb64( &ctx, MBEDTLS_BLOWFISH_ENCRYPT, src_str->len, &iv_offset, iv_str->x, src_str->x, output ) == 0 );
Paul Bakkera9379c02012-07-04 11:02:11 +0000161
Ronald Cronac6ae352020-06-26 14:33:03 +0200162 TEST_ASSERT( mbedtls_test_hexcmp( output, dst->x, src_str->len,
163 dst->len ) == 0 );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200164
Paul Bakkerbd51b262014-07-10 15:26:12 +0200165exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166 mbedtls_blowfish_free( &ctx );
Paul Bakkera9379c02012-07-04 11:02:11 +0000167}
Paul Bakker33b43f12013-08-20 11:48:36 +0200168/* END_CASE */
Paul Bakkera9379c02012-07-04 11:02:11 +0000169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
Azim Khan5fcca462018-06-29 11:05:32 +0100171void blowfish_decrypt_cfb64( data_t * key_str, data_t * iv_str,
Ronald Cronac6ae352020-06-26 14:33:03 +0200172 data_t * src_str, data_t * dst )
Paul Bakkera9379c02012-07-04 11:02:11 +0000173{
Paul Bakkera9379c02012-07-04 11:02:11 +0000174 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 mbedtls_blowfish_context ctx;
Paul Bakkera9379c02012-07-04 11:02:11 +0000176 size_t iv_offset = 0;
Paul Bakkera9379c02012-07-04 11:02:11 +0000177
Paul Bakkera9379c02012-07-04 11:02:11 +0000178 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179 mbedtls_blowfish_init( &ctx );
Paul Bakkera9379c02012-07-04 11:02:11 +0000180
Paul Bakkera9379c02012-07-04 11:02:11 +0000181
Azim Khand30ca132017-06-09 04:32:58 +0100182 mbedtls_blowfish_setkey( &ctx, key_str->x, key_str->len * 8 );
183 TEST_ASSERT( mbedtls_blowfish_crypt_cfb64( &ctx, MBEDTLS_BLOWFISH_DECRYPT, src_str->len, &iv_offset, iv_str->x, src_str->x, output ) == 0 );
Paul Bakkera9379c02012-07-04 11:02:11 +0000184
Ronald Cronac6ae352020-06-26 14:33:03 +0200185 TEST_ASSERT( mbedtls_test_hexcmp( output, dst->x, src_str->len,
186 dst->len ) == 0 );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200187
Paul Bakkerbd51b262014-07-10 15:26:12 +0200188exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189 mbedtls_blowfish_free( &ctx );
Paul Bakkera9379c02012-07-04 11:02:11 +0000190}
Paul Bakker33b43f12013-08-20 11:48:36 +0200191/* END_CASE */
Paul Bakkera9379c02012-07-04 11:02:11 +0000192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CTR */
Azim Khan5fcca462018-06-29 11:05:32 +0100194void blowfish_encrypt_ctr( data_t * key_str, data_t * iv_str,
Ronald Cronac6ae352020-06-26 14:33:03 +0200195 data_t * src_str, data_t * dst )
Paul Bakkera9379c02012-07-04 11:02:11 +0000196{
Paul Bakkera9379c02012-07-04 11:02:11 +0000197 unsigned char stream_str[100];
Paul Bakkera9379c02012-07-04 11:02:11 +0000198 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199 mbedtls_blowfish_context ctx;
Paul Bakkera9379c02012-07-04 11:02:11 +0000200 size_t iv_offset = 0;
Paul Bakkera9379c02012-07-04 11:02:11 +0000201
Paul Bakkera9379c02012-07-04 11:02:11 +0000202 memset(stream_str, 0x00, 100);
Paul Bakkera9379c02012-07-04 11:02:11 +0000203 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204 mbedtls_blowfish_init( &ctx );
Paul Bakkera9379c02012-07-04 11:02:11 +0000205
Paul Bakkera9379c02012-07-04 11:02:11 +0000206
Azim Khand30ca132017-06-09 04:32:58 +0100207 mbedtls_blowfish_setkey( &ctx, key_str->x, key_str->len * 8 );
208 TEST_ASSERT( mbedtls_blowfish_crypt_ctr( &ctx, src_str->len, &iv_offset, iv_str->x, stream_str, src_str->x, output ) == 0 );
Paul Bakkera9379c02012-07-04 11:02:11 +0000209
Ronald Cronac6ae352020-06-26 14:33:03 +0200210 TEST_ASSERT( mbedtls_test_hexcmp( output, dst->x, src_str->len,
211 dst->len ) == 0 );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200212
Paul Bakkerbd51b262014-07-10 15:26:12 +0200213exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214 mbedtls_blowfish_free( &ctx );
Paul Bakkera9379c02012-07-04 11:02:11 +0000215}
Paul Bakker33b43f12013-08-20 11:48:36 +0200216/* END_CASE */