blob: 55ab619fccc744586ea40588791a7438f6e0e5cd [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 */
Azim Khanf1aaec92017-05-30 14:23:15 +010011void blowfish_encrypt_ecb( uint8_t * key_str, uint32_t key_len,
12 uint8_t * src_str, uint32_t src_str_len,
13 uint8_t * hex_dst_string,
14 uint32_t hex_dst_string_len, int setkey_result )
Paul Bakkera9379c02012-07-04 11:02:11 +000015{
Paul Bakkera9379c02012-07-04 11:02:11 +000016 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020017 mbedtls_blowfish_context ctx;
Paul Bakkera9379c02012-07-04 11:02:11 +000018
Paul Bakkera9379c02012-07-04 11:02:11 +000019 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020020 mbedtls_blowfish_init( &ctx );
Paul Bakkera9379c02012-07-04 11:02:11 +000021
Paul Bakkera9379c02012-07-04 11:02:11 +000022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023 TEST_ASSERT( mbedtls_blowfish_setkey( &ctx, key_str, key_len * 8 ) == setkey_result );
Paul Bakker33b43f12013-08-20 11:48:36 +020024 if( setkey_result == 0 )
Paul Bakkera9379c02012-07-04 11:02:11 +000025 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026 TEST_ASSERT( mbedtls_blowfish_crypt_ecb( &ctx, MBEDTLS_BLOWFISH_ENCRYPT, src_str, output ) == 0 );
Paul Bakkera9379c02012-07-04 11:02:11 +000027
Azim Khanf1aaec92017-05-30 14:23:15 +010028 TEST_ASSERT( hexcmp( output, hex_dst_string, 8, hex_dst_string_len ) == 0 );
Paul Bakkera9379c02012-07-04 11:02:11 +000029 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +020030
Paul Bakkerbd51b262014-07-10 15:26:12 +020031exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032 mbedtls_blowfish_free( &ctx );
Paul Bakkera9379c02012-07-04 11:02:11 +000033}
Paul Bakker33b43f12013-08-20 11:48:36 +020034/* END_CASE */
Paul Bakkera9379c02012-07-04 11:02:11 +000035
Paul Bakker33b43f12013-08-20 11:48:36 +020036/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +010037void blowfish_decrypt_ecb( uint8_t * key_str, uint32_t key_len,
38 uint8_t * src_str, uint32_t src_str_len,
39 uint8_t * hex_dst_string,
40 uint32_t hex_dst_string_len, int setkey_result )
Paul Bakkera9379c02012-07-04 11:02:11 +000041{
Paul Bakkera9379c02012-07-04 11:02:11 +000042 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043 mbedtls_blowfish_context ctx;
Paul Bakkera9379c02012-07-04 11:02:11 +000044
Paul Bakkera9379c02012-07-04 11:02:11 +000045 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046 mbedtls_blowfish_init( &ctx );
Paul Bakkera9379c02012-07-04 11:02:11 +000047
Paul Bakkera9379c02012-07-04 11:02:11 +000048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049 TEST_ASSERT( mbedtls_blowfish_setkey( &ctx, key_str, key_len * 8 ) == setkey_result );
Paul Bakker33b43f12013-08-20 11:48:36 +020050 if( setkey_result == 0 )
Paul Bakkera9379c02012-07-04 11:02:11 +000051 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052 TEST_ASSERT( mbedtls_blowfish_crypt_ecb( &ctx, MBEDTLS_BLOWFISH_DECRYPT, src_str, output ) == 0 );
Paul Bakkera9379c02012-07-04 11:02:11 +000053
Azim Khanf1aaec92017-05-30 14:23:15 +010054 TEST_ASSERT( hexcmp( output, hex_dst_string, 8, hex_dst_string_len ) == 0 );
Paul Bakkera9379c02012-07-04 11:02:11 +000055 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +020056
Paul Bakkerbd51b262014-07-10 15:26:12 +020057exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058 mbedtls_blowfish_free( &ctx );
Paul Bakkera9379c02012-07-04 11:02:11 +000059}
Paul Bakker33b43f12013-08-20 11:48:36 +020060/* END_CASE */
Paul Bakkera9379c02012-07-04 11:02:11 +000061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
Azim Khanf1aaec92017-05-30 14:23:15 +010063void blowfish_encrypt_cbc( uint8_t * key_str, uint32_t key_len,
64 uint8_t * iv_str, uint32_t iv_str_len,
65 uint8_t * src_str, uint32_t data_len,
66 uint8_t * hex_dst_string,
67 uint32_t hex_dst_string_len, int cbc_result )
Paul Bakkera9379c02012-07-04 11:02:11 +000068{
Paul Bakkera9379c02012-07-04 11:02:11 +000069 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070 mbedtls_blowfish_context ctx;
Paul Bakkera9379c02012-07-04 11:02:11 +000071
Paul Bakkera9379c02012-07-04 11:02:11 +000072 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073 mbedtls_blowfish_init( &ctx );
Paul Bakkera9379c02012-07-04 11:02:11 +000074
Paul Bakkera9379c02012-07-04 11:02:11 +000075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076 mbedtls_blowfish_setkey( &ctx, key_str, key_len * 8 );
Paul Bakkera9379c02012-07-04 11:02:11 +000077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078 TEST_ASSERT( mbedtls_blowfish_crypt_cbc( &ctx, MBEDTLS_BLOWFISH_ENCRYPT, data_len , iv_str, src_str, output ) == cbc_result );
Paul Bakker33b43f12013-08-20 11:48:36 +020079 if( cbc_result == 0 )
Paul Bakkera9379c02012-07-04 11:02:11 +000080 {
Paul Bakkera9379c02012-07-04 11:02:11 +000081
Azim Khanf1aaec92017-05-30 14:23:15 +010082 TEST_ASSERT( hexcmp( output, hex_dst_string, data_len, hex_dst_string_len ) == 0 );
Paul Bakkera9379c02012-07-04 11:02:11 +000083 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +020084
Paul Bakkerbd51b262014-07-10 15:26:12 +020085exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086 mbedtls_blowfish_free( &ctx );
Paul Bakkera9379c02012-07-04 11:02:11 +000087}
Paul Bakker33b43f12013-08-20 11:48:36 +020088/* END_CASE */
Paul Bakkera9379c02012-07-04 11:02:11 +000089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
Azim Khanf1aaec92017-05-30 14:23:15 +010091void blowfish_decrypt_cbc( uint8_t * key_str, uint32_t key_len,
92 uint8_t * iv_str, uint32_t iv_str_len,
93 uint8_t * src_str, uint32_t data_len,
94 uint8_t * hex_dst_string,
95 uint32_t hex_dst_string_len, int cbc_result )
Paul Bakkera9379c02012-07-04 11:02:11 +000096{
Paul Bakkera9379c02012-07-04 11:02:11 +000097 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098 mbedtls_blowfish_context ctx;
Paul Bakkera9379c02012-07-04 11:02:11 +000099
Paul Bakkera9379c02012-07-04 11:02:11 +0000100 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101 mbedtls_blowfish_init( &ctx );
Paul Bakkera9379c02012-07-04 11:02:11 +0000102
Paul Bakkera9379c02012-07-04 11:02:11 +0000103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104 mbedtls_blowfish_setkey( &ctx, key_str, key_len * 8 );
105 TEST_ASSERT( mbedtls_blowfish_crypt_cbc( &ctx, MBEDTLS_BLOWFISH_DECRYPT, data_len , iv_str, src_str, output ) == cbc_result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200106 if( cbc_result == 0)
Paul Bakkera9379c02012-07-04 11:02:11 +0000107 {
Paul Bakkera9379c02012-07-04 11:02:11 +0000108
Azim Khanf1aaec92017-05-30 14:23:15 +0100109 TEST_ASSERT( hexcmp( output, hex_dst_string, data_len, hex_dst_string_len ) == 0 );
Paul Bakkera9379c02012-07-04 11:02:11 +0000110 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200111
Paul Bakkerbd51b262014-07-10 15:26:12 +0200112exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113 mbedtls_blowfish_free( &ctx );
Paul Bakkera9379c02012-07-04 11:02:11 +0000114}
Paul Bakker33b43f12013-08-20 11:48:36 +0200115/* END_CASE */
Paul Bakkera9379c02012-07-04 11:02:11 +0000116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
Azim Khanf1aaec92017-05-30 14:23:15 +0100118void blowfish_encrypt_cfb64( uint8_t * key_str, uint32_t key_len,
119 uint8_t * iv_str, uint32_t iv_str_len,
120 uint8_t * src_str, uint32_t src_len,
121 uint8_t * hex_dst_string,
122 uint32_t hex_dst_string_len )
Paul Bakkera9379c02012-07-04 11:02:11 +0000123{
Paul Bakkera9379c02012-07-04 11:02:11 +0000124 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125 mbedtls_blowfish_context ctx;
Paul Bakkera9379c02012-07-04 11:02:11 +0000126 size_t iv_offset = 0;
Paul Bakkera9379c02012-07-04 11:02:11 +0000127
Paul Bakkera9379c02012-07-04 11:02:11 +0000128 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129 mbedtls_blowfish_init( &ctx );
Paul Bakkera9379c02012-07-04 11:02:11 +0000130
Paul Bakkera9379c02012-07-04 11:02:11 +0000131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132 mbedtls_blowfish_setkey( &ctx, key_str, key_len * 8 );
133 TEST_ASSERT( mbedtls_blowfish_crypt_cfb64( &ctx, MBEDTLS_BLOWFISH_ENCRYPT, src_len, &iv_offset, iv_str, src_str, output ) == 0 );
Paul Bakkera9379c02012-07-04 11:02:11 +0000134
Azim Khanf1aaec92017-05-30 14:23:15 +0100135 TEST_ASSERT( hexcmp( output, hex_dst_string, src_len, hex_dst_string_len ) == 0 );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200136
Paul Bakkerbd51b262014-07-10 15:26:12 +0200137exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138 mbedtls_blowfish_free( &ctx );
Paul Bakkera9379c02012-07-04 11:02:11 +0000139}
Paul Bakker33b43f12013-08-20 11:48:36 +0200140/* END_CASE */
Paul Bakkera9379c02012-07-04 11:02:11 +0000141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
Azim Khanf1aaec92017-05-30 14:23:15 +0100143void blowfish_decrypt_cfb64( uint8_t * key_str, uint32_t key_len,
144 uint8_t * iv_str, uint32_t iv_str_len,
145 uint8_t * src_str, uint32_t src_len,
146 uint8_t * hex_dst_string,
147 uint32_t hex_dst_string_len )
Paul Bakkera9379c02012-07-04 11:02:11 +0000148{
Paul Bakkera9379c02012-07-04 11:02:11 +0000149 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 mbedtls_blowfish_context ctx;
Paul Bakkera9379c02012-07-04 11:02:11 +0000151 size_t iv_offset = 0;
Paul Bakkera9379c02012-07-04 11:02:11 +0000152
Paul Bakkera9379c02012-07-04 11:02:11 +0000153 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154 mbedtls_blowfish_init( &ctx );
Paul Bakkera9379c02012-07-04 11:02:11 +0000155
Paul Bakkera9379c02012-07-04 11:02:11 +0000156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 mbedtls_blowfish_setkey( &ctx, key_str, key_len * 8 );
158 TEST_ASSERT( mbedtls_blowfish_crypt_cfb64( &ctx, MBEDTLS_BLOWFISH_DECRYPT, src_len, &iv_offset, iv_str, src_str, output ) == 0 );
Paul Bakkera9379c02012-07-04 11:02:11 +0000159
Azim Khanf1aaec92017-05-30 14:23:15 +0100160 TEST_ASSERT( hexcmp( output, hex_dst_string, src_len, hex_dst_string_len ) == 0 );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200161
Paul Bakkerbd51b262014-07-10 15:26:12 +0200162exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163 mbedtls_blowfish_free( &ctx );
Paul Bakkera9379c02012-07-04 11:02:11 +0000164}
Paul Bakker33b43f12013-08-20 11:48:36 +0200165/* END_CASE */
Paul Bakkera9379c02012-07-04 11:02:11 +0000166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CTR */
Azim Khanf1aaec92017-05-30 14:23:15 +0100168void blowfish_encrypt_ctr( uint8_t * key_str, uint32_t key_len,
169 uint8_t * iv_str, uint32_t iv_str_len,
170 uint8_t * src_str, uint32_t src_len,
171 uint8_t * hex_dst_string,
172 uint32_t hex_dst_string_len )
Paul Bakkera9379c02012-07-04 11:02:11 +0000173{
Paul Bakkera9379c02012-07-04 11:02:11 +0000174 unsigned char stream_str[100];
Paul Bakkera9379c02012-07-04 11:02:11 +0000175 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 mbedtls_blowfish_context ctx;
Paul Bakkera9379c02012-07-04 11:02:11 +0000177 size_t iv_offset = 0;
Paul Bakkera9379c02012-07-04 11:02:11 +0000178
Paul Bakkera9379c02012-07-04 11:02:11 +0000179 memset(stream_str, 0x00, 100);
Paul Bakkera9379c02012-07-04 11:02:11 +0000180 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181 mbedtls_blowfish_init( &ctx );
Paul Bakkera9379c02012-07-04 11:02:11 +0000182
Paul Bakkera9379c02012-07-04 11:02:11 +0000183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184 mbedtls_blowfish_setkey( &ctx, key_str, key_len * 8 );
185 TEST_ASSERT( mbedtls_blowfish_crypt_ctr( &ctx, src_len, &iv_offset, iv_str, stream_str, src_str, output ) == 0 );
Paul Bakkera9379c02012-07-04 11:02:11 +0000186
Azim Khanf1aaec92017-05-30 14:23:15 +0100187 TEST_ASSERT( hexcmp( output, hex_dst_string, src_len, hex_dst_string_len ) == 0 );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200188
Paul Bakkerbd51b262014-07-10 15:26:12 +0200189exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190 mbedtls_blowfish_free( &ctx );
Paul Bakkera9379c02012-07-04 11:02:11 +0000191}
Paul Bakker33b43f12013-08-20 11:48:36 +0200192/* END_CASE */