blob: 5681a9e945b095896405c72323cf0258c3b80be5 [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
Paul Bakker33b43f12013-08-20 11:48:36 +020010/* BEGIN_CASE */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050011void blowfish_valid_param( )
12{
13 TEST_VALID_PARAM( mbedtls_blowfish_free( NULL ) );
14}
15/* END_CASE */
16
17/* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */
18void blowfish_invalid_param( )
19{
20 mbedtls_blowfish_context ctx;
21 unsigned char buf[16] = { 0 };
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050022 size_t invalid_mode = 42;
23 size_t off;
24 ((void) off);
25
Ronald Cron875b5fb2021-05-21 08:50:00 +020026 TEST_EQUAL( MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050027 mbedtls_blowfish_crypt_ecb( &ctx,
28 invalid_mode,
29 buf, buf ) );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050030
31#if defined(MBEDTLS_CIPHER_MODE_CBC)
Ronald Cron875b5fb2021-05-21 08:50:00 +020032 TEST_EQUAL( MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050033 mbedtls_blowfish_crypt_cbc( &ctx,
34 invalid_mode,
35 sizeof( buf ),
36 buf, buf, buf ) );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050037#endif /* MBEDTLS_CIPHER_MODE_CBC */
38
39#if defined(MBEDTLS_CIPHER_MODE_CFB)
Ronald Cron875b5fb2021-05-21 08:50:00 +020040 TEST_EQUAL( MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050041 mbedtls_blowfish_crypt_cfb64( &ctx,
42 invalid_mode,
43 sizeof( buf ),
44 &off, buf,
45 buf, buf ) );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050046#endif /* MBEDTLS_CIPHER_MODE_CFB */
47
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050048exit:
49 return;
50}
51/* END_CASE */
52
53/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +010054void blowfish_encrypt_ecb( data_t * key_str, data_t * src_str,
Ronald Cronac6ae352020-06-26 14:33:03 +020055 data_t * dst, int setkey_result )
Paul Bakkera9379c02012-07-04 11:02:11 +000056{
Paul Bakkera9379c02012-07-04 11:02:11 +000057 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058 mbedtls_blowfish_context ctx;
Paul Bakkera9379c02012-07-04 11:02:11 +000059
Paul Bakkera9379c02012-07-04 11:02:11 +000060 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061 mbedtls_blowfish_init( &ctx );
Paul Bakkera9379c02012-07-04 11:02:11 +000062
Paul Bakkera9379c02012-07-04 11:02:11 +000063
Azim Khand30ca132017-06-09 04:32:58 +010064 TEST_ASSERT( mbedtls_blowfish_setkey( &ctx, key_str->x, key_str->len * 8 ) == setkey_result );
Paul Bakker33b43f12013-08-20 11:48:36 +020065 if( setkey_result == 0 )
Paul Bakkera9379c02012-07-04 11:02:11 +000066 {
Azim Khand30ca132017-06-09 04:32:58 +010067 TEST_ASSERT( mbedtls_blowfish_crypt_ecb( &ctx, MBEDTLS_BLOWFISH_ENCRYPT, src_str->x, output ) == 0 );
Paul Bakkera9379c02012-07-04 11:02:11 +000068
Ronald Cronac6ae352020-06-26 14:33:03 +020069 TEST_ASSERT( mbedtls_test_hexcmp( output, dst->x, 8, dst->len ) == 0 );
Paul Bakkera9379c02012-07-04 11:02:11 +000070 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +020071
Paul Bakkerbd51b262014-07-10 15:26:12 +020072exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073 mbedtls_blowfish_free( &ctx );
Paul Bakkera9379c02012-07-04 11:02:11 +000074}
Paul Bakker33b43f12013-08-20 11:48:36 +020075/* END_CASE */
Paul Bakkera9379c02012-07-04 11:02:11 +000076
Paul Bakker33b43f12013-08-20 11:48:36 +020077/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +010078void blowfish_decrypt_ecb( data_t * key_str, data_t * src_str,
Ronald Cronac6ae352020-06-26 14:33:03 +020079 data_t * dst, int setkey_result )
Paul Bakkera9379c02012-07-04 11:02:11 +000080{
Paul Bakkera9379c02012-07-04 11:02:11 +000081 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082 mbedtls_blowfish_context ctx;
Paul Bakkera9379c02012-07-04 11:02:11 +000083
Paul Bakkera9379c02012-07-04 11:02:11 +000084 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085 mbedtls_blowfish_init( &ctx );
Paul Bakkera9379c02012-07-04 11:02:11 +000086
Paul Bakkera9379c02012-07-04 11:02:11 +000087
Azim Khand30ca132017-06-09 04:32:58 +010088 TEST_ASSERT( mbedtls_blowfish_setkey( &ctx, key_str->x, key_str->len * 8 ) == setkey_result );
Paul Bakker33b43f12013-08-20 11:48:36 +020089 if( setkey_result == 0 )
Paul Bakkera9379c02012-07-04 11:02:11 +000090 {
Azim Khand30ca132017-06-09 04:32:58 +010091 TEST_ASSERT( mbedtls_blowfish_crypt_ecb( &ctx, MBEDTLS_BLOWFISH_DECRYPT, src_str->x, output ) == 0 );
Paul Bakkera9379c02012-07-04 11:02:11 +000092
Ronald Cronac6ae352020-06-26 14:33:03 +020093 TEST_ASSERT( mbedtls_test_hexcmp( output, dst->x, 8, dst->len ) == 0 );
Paul Bakkera9379c02012-07-04 11:02:11 +000094 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +020095
Paul Bakkerbd51b262014-07-10 15:26:12 +020096exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097 mbedtls_blowfish_free( &ctx );
Paul Bakkera9379c02012-07-04 11:02:11 +000098}
Paul Bakker33b43f12013-08-20 11:48:36 +020099/* END_CASE */
Paul Bakkera9379c02012-07-04 11:02:11 +0000100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
Azim Khan5fcca462018-06-29 11:05:32 +0100102void blowfish_encrypt_cbc( data_t * key_str, data_t * iv_str,
Ronald Cronac6ae352020-06-26 14:33:03 +0200103 data_t * src_str, data_t * dst,
Azim Khand30ca132017-06-09 04:32:58 +0100104 int cbc_result )
Paul Bakkera9379c02012-07-04 11:02:11 +0000105{
Paul Bakkera9379c02012-07-04 11:02:11 +0000106 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107 mbedtls_blowfish_context ctx;
Paul Bakkera9379c02012-07-04 11:02:11 +0000108
Paul Bakkera9379c02012-07-04 11:02:11 +0000109 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110 mbedtls_blowfish_init( &ctx );
Paul Bakkera9379c02012-07-04 11:02:11 +0000111
Paul Bakkera9379c02012-07-04 11:02:11 +0000112
Azim Khand30ca132017-06-09 04:32:58 +0100113 mbedtls_blowfish_setkey( &ctx, key_str->x, key_str->len * 8 );
Paul Bakkera9379c02012-07-04 11:02:11 +0000114
Azim Khand30ca132017-06-09 04:32:58 +0100115 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 +0200116 if( cbc_result == 0 )
Paul Bakkera9379c02012-07-04 11:02:11 +0000117 {
Paul Bakkera9379c02012-07-04 11:02:11 +0000118
Ronald Cronac6ae352020-06-26 14:33:03 +0200119 TEST_ASSERT( mbedtls_test_hexcmp( output, dst->x,
120 src_str->len, dst->len ) == 0 );
Paul Bakkera9379c02012-07-04 11:02:11 +0000121 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200122
Paul Bakkerbd51b262014-07-10 15:26:12 +0200123exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124 mbedtls_blowfish_free( &ctx );
Paul Bakkera9379c02012-07-04 11:02:11 +0000125}
Paul Bakker33b43f12013-08-20 11:48:36 +0200126/* END_CASE */
Paul Bakkera9379c02012-07-04 11:02:11 +0000127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
Azim Khan5fcca462018-06-29 11:05:32 +0100129void blowfish_decrypt_cbc( data_t * key_str, data_t * iv_str,
Ronald Cronac6ae352020-06-26 14:33:03 +0200130 data_t * src_str, data_t * dst,
Azim Khand30ca132017-06-09 04:32:58 +0100131 int cbc_result )
Paul Bakkera9379c02012-07-04 11:02:11 +0000132{
Paul Bakkera9379c02012-07-04 11:02:11 +0000133 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 mbedtls_blowfish_context ctx;
Paul Bakkera9379c02012-07-04 11:02:11 +0000135
Paul Bakkera9379c02012-07-04 11:02:11 +0000136 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137 mbedtls_blowfish_init( &ctx );
Paul Bakkera9379c02012-07-04 11:02:11 +0000138
Paul Bakkera9379c02012-07-04 11:02:11 +0000139
Azim Khand30ca132017-06-09 04:32:58 +0100140 mbedtls_blowfish_setkey( &ctx, key_str->x, key_str->len * 8 );
141 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 +0200142 if( cbc_result == 0)
Paul Bakkera9379c02012-07-04 11:02:11 +0000143 {
Paul Bakkera9379c02012-07-04 11:02:11 +0000144
Ronald Cronac6ae352020-06-26 14:33:03 +0200145 TEST_ASSERT( mbedtls_test_hexcmp( output, dst->x, src_str->len,
146 dst->len ) == 0 );
Paul Bakkera9379c02012-07-04 11:02:11 +0000147 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200148
Paul Bakkerbd51b262014-07-10 15:26:12 +0200149exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 mbedtls_blowfish_free( &ctx );
Paul Bakkera9379c02012-07-04 11:02:11 +0000151}
Paul Bakker33b43f12013-08-20 11:48:36 +0200152/* END_CASE */
Paul Bakkera9379c02012-07-04 11:02:11 +0000153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
Azim Khan5fcca462018-06-29 11:05:32 +0100155void blowfish_encrypt_cfb64( data_t * key_str, data_t * iv_str,
Ronald Cronac6ae352020-06-26 14:33:03 +0200156 data_t * src_str, data_t * dst )
Paul Bakkera9379c02012-07-04 11:02:11 +0000157{
Paul Bakkera9379c02012-07-04 11:02:11 +0000158 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159 mbedtls_blowfish_context ctx;
Paul Bakkera9379c02012-07-04 11:02:11 +0000160 size_t iv_offset = 0;
Paul Bakkera9379c02012-07-04 11:02:11 +0000161
Paul Bakkera9379c02012-07-04 11:02:11 +0000162 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163 mbedtls_blowfish_init( &ctx );
Paul Bakkera9379c02012-07-04 11:02:11 +0000164
Paul Bakkera9379c02012-07-04 11:02:11 +0000165
Azim Khand30ca132017-06-09 04:32:58 +0100166 mbedtls_blowfish_setkey( &ctx, key_str->x, key_str->len * 8 );
167 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 +0000168
Ronald Cronac6ae352020-06-26 14:33:03 +0200169 TEST_ASSERT( mbedtls_test_hexcmp( output, dst->x, src_str->len,
170 dst->len ) == 0 );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200171
Paul Bakkerbd51b262014-07-10 15:26:12 +0200172exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173 mbedtls_blowfish_free( &ctx );
Paul Bakkera9379c02012-07-04 11:02:11 +0000174}
Paul Bakker33b43f12013-08-20 11:48:36 +0200175/* END_CASE */
Paul Bakkera9379c02012-07-04 11:02:11 +0000176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
Azim Khan5fcca462018-06-29 11:05:32 +0100178void blowfish_decrypt_cfb64( data_t * key_str, data_t * iv_str,
Ronald Cronac6ae352020-06-26 14:33:03 +0200179 data_t * src_str, data_t * dst )
Paul Bakkera9379c02012-07-04 11:02:11 +0000180{
Paul Bakkera9379c02012-07-04 11:02:11 +0000181 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182 mbedtls_blowfish_context ctx;
Paul Bakkera9379c02012-07-04 11:02:11 +0000183 size_t iv_offset = 0;
Paul Bakkera9379c02012-07-04 11:02:11 +0000184
Paul Bakkera9379c02012-07-04 11:02:11 +0000185 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186 mbedtls_blowfish_init( &ctx );
Paul Bakkera9379c02012-07-04 11:02:11 +0000187
Paul Bakkera9379c02012-07-04 11:02:11 +0000188
Azim Khand30ca132017-06-09 04:32:58 +0100189 mbedtls_blowfish_setkey( &ctx, key_str->x, key_str->len * 8 );
190 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 +0000191
Ronald Cronac6ae352020-06-26 14:33:03 +0200192 TEST_ASSERT( mbedtls_test_hexcmp( output, dst->x, src_str->len,
193 dst->len ) == 0 );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200194
Paul Bakkerbd51b262014-07-10 15:26:12 +0200195exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196 mbedtls_blowfish_free( &ctx );
Paul Bakkera9379c02012-07-04 11:02:11 +0000197}
Paul Bakker33b43f12013-08-20 11:48:36 +0200198/* END_CASE */
Paul Bakkera9379c02012-07-04 11:02:11 +0000199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CTR */
Azim Khan5fcca462018-06-29 11:05:32 +0100201void blowfish_encrypt_ctr( data_t * key_str, data_t * iv_str,
Ronald Cronac6ae352020-06-26 14:33:03 +0200202 data_t * src_str, data_t * dst )
Paul Bakkera9379c02012-07-04 11:02:11 +0000203{
Paul Bakkera9379c02012-07-04 11:02:11 +0000204 unsigned char stream_str[100];
Paul Bakkera9379c02012-07-04 11:02:11 +0000205 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206 mbedtls_blowfish_context ctx;
Paul Bakkera9379c02012-07-04 11:02:11 +0000207 size_t iv_offset = 0;
Paul Bakkera9379c02012-07-04 11:02:11 +0000208
Paul Bakkera9379c02012-07-04 11:02:11 +0000209 memset(stream_str, 0x00, 100);
Paul Bakkera9379c02012-07-04 11:02:11 +0000210 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 mbedtls_blowfish_init( &ctx );
Paul Bakkera9379c02012-07-04 11:02:11 +0000212
Paul Bakkera9379c02012-07-04 11:02:11 +0000213
Azim Khand30ca132017-06-09 04:32:58 +0100214 mbedtls_blowfish_setkey( &ctx, key_str->x, key_str->len * 8 );
215 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 +0000216
Ronald Cronac6ae352020-06-26 14:33:03 +0200217 TEST_ASSERT( mbedtls_test_hexcmp( output, dst->x, src_str->len,
218 dst->len ) == 0 );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200219
Paul Bakkerbd51b262014-07-10 15:26:12 +0200220exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221 mbedtls_blowfish_free( &ctx );
Paul Bakkera9379c02012-07-04 11:02:11 +0000222}
Paul Bakker33b43f12013-08-20 11:48:36 +0200223/* END_CASE */