blob: d88eac463b5499343e86c74ffce03c3afda2e038 [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 Khand30ca132017-06-09 04:32:58 +010011void blowfish_encrypt_ecb( HexParam_t * key_str, HexParam_t * src_str,
12 HexParam_t * hex_dst_string, int setkey_result )
Paul Bakkera9379c02012-07-04 11:02:11 +000013{
Paul Bakkera9379c02012-07-04 11:02:11 +000014 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020015 mbedtls_blowfish_context ctx;
Paul Bakkera9379c02012-07-04 11:02:11 +000016
Paul Bakkera9379c02012-07-04 11:02:11 +000017 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020018 mbedtls_blowfish_init( &ctx );
Paul Bakkera9379c02012-07-04 11:02:11 +000019
Paul Bakkera9379c02012-07-04 11:02:11 +000020
Azim Khand30ca132017-06-09 04:32:58 +010021 TEST_ASSERT( mbedtls_blowfish_setkey( &ctx, key_str->x, key_str->len * 8 ) == setkey_result );
Paul Bakker33b43f12013-08-20 11:48:36 +020022 if( setkey_result == 0 )
Paul Bakkera9379c02012-07-04 11:02:11 +000023 {
Azim Khand30ca132017-06-09 04:32:58 +010024 TEST_ASSERT( mbedtls_blowfish_crypt_ecb( &ctx, MBEDTLS_BLOWFISH_ENCRYPT, src_str->x, output ) == 0 );
Paul Bakkera9379c02012-07-04 11:02:11 +000025
Azim Khand30ca132017-06-09 04:32:58 +010026 TEST_ASSERT( hexcmp( output, hex_dst_string->x, 8, hex_dst_string->len ) == 0 );
Paul Bakkera9379c02012-07-04 11:02:11 +000027 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +020028
Paul Bakkerbd51b262014-07-10 15:26:12 +020029exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030 mbedtls_blowfish_free( &ctx );
Paul Bakkera9379c02012-07-04 11:02:11 +000031}
Paul Bakker33b43f12013-08-20 11:48:36 +020032/* END_CASE */
Paul Bakkera9379c02012-07-04 11:02:11 +000033
Paul Bakker33b43f12013-08-20 11:48:36 +020034/* BEGIN_CASE */
Azim Khand30ca132017-06-09 04:32:58 +010035void blowfish_decrypt_ecb( HexParam_t * key_str, HexParam_t * src_str,
36 HexParam_t * hex_dst_string, int setkey_result )
Paul Bakkera9379c02012-07-04 11:02:11 +000037{
Paul Bakkera9379c02012-07-04 11:02:11 +000038 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039 mbedtls_blowfish_context ctx;
Paul Bakkera9379c02012-07-04 11:02:11 +000040
Paul Bakkera9379c02012-07-04 11:02:11 +000041 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042 mbedtls_blowfish_init( &ctx );
Paul Bakkera9379c02012-07-04 11:02:11 +000043
Paul Bakkera9379c02012-07-04 11:02:11 +000044
Azim Khand30ca132017-06-09 04:32:58 +010045 TEST_ASSERT( mbedtls_blowfish_setkey( &ctx, key_str->x, key_str->len * 8 ) == setkey_result );
Paul Bakker33b43f12013-08-20 11:48:36 +020046 if( setkey_result == 0 )
Paul Bakkera9379c02012-07-04 11:02:11 +000047 {
Azim Khand30ca132017-06-09 04:32:58 +010048 TEST_ASSERT( mbedtls_blowfish_crypt_ecb( &ctx, MBEDTLS_BLOWFISH_DECRYPT, src_str->x, output ) == 0 );
Paul Bakkera9379c02012-07-04 11:02:11 +000049
Azim Khand30ca132017-06-09 04:32:58 +010050 TEST_ASSERT( hexcmp( output, hex_dst_string->x, 8, hex_dst_string->len ) == 0 );
Paul Bakkera9379c02012-07-04 11:02:11 +000051 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +020052
Paul Bakkerbd51b262014-07-10 15:26:12 +020053exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054 mbedtls_blowfish_free( &ctx );
Paul Bakkera9379c02012-07-04 11:02:11 +000055}
Paul Bakker33b43f12013-08-20 11:48:36 +020056/* END_CASE */
Paul Bakkera9379c02012-07-04 11:02:11 +000057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
Azim Khand30ca132017-06-09 04:32:58 +010059void blowfish_encrypt_cbc( HexParam_t * key_str, HexParam_t * iv_str,
60 HexParam_t * src_str, HexParam_t * hex_dst_string,
61 int cbc_result )
Paul Bakkera9379c02012-07-04 11:02:11 +000062{
Paul Bakkera9379c02012-07-04 11:02:11 +000063 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064 mbedtls_blowfish_context ctx;
Paul Bakkera9379c02012-07-04 11:02:11 +000065
Paul Bakkera9379c02012-07-04 11:02:11 +000066 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067 mbedtls_blowfish_init( &ctx );
Paul Bakkera9379c02012-07-04 11:02:11 +000068
Paul Bakkera9379c02012-07-04 11:02:11 +000069
Azim Khand30ca132017-06-09 04:32:58 +010070 mbedtls_blowfish_setkey( &ctx, key_str->x, key_str->len * 8 );
Paul Bakkera9379c02012-07-04 11:02:11 +000071
Azim Khand30ca132017-06-09 04:32:58 +010072 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 +020073 if( cbc_result == 0 )
Paul Bakkera9379c02012-07-04 11:02:11 +000074 {
Paul Bakkera9379c02012-07-04 11:02:11 +000075
Azim Khand30ca132017-06-09 04:32:58 +010076 TEST_ASSERT( hexcmp( output, hex_dst_string->x, src_str->len, hex_dst_string->len ) == 0 );
Paul Bakkera9379c02012-07-04 11:02:11 +000077 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +020078
Paul Bakkerbd51b262014-07-10 15:26:12 +020079exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080 mbedtls_blowfish_free( &ctx );
Paul Bakkera9379c02012-07-04 11:02:11 +000081}
Paul Bakker33b43f12013-08-20 11:48:36 +020082/* END_CASE */
Paul Bakkera9379c02012-07-04 11:02:11 +000083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
Azim Khand30ca132017-06-09 04:32:58 +010085void blowfish_decrypt_cbc( HexParam_t * key_str, HexParam_t * iv_str,
86 HexParam_t * src_str, HexParam_t * hex_dst_string,
87 int cbc_result )
Paul Bakkera9379c02012-07-04 11:02:11 +000088{
Paul Bakkera9379c02012-07-04 11:02:11 +000089 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090 mbedtls_blowfish_context ctx;
Paul Bakkera9379c02012-07-04 11:02:11 +000091
Paul Bakkera9379c02012-07-04 11:02:11 +000092 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093 mbedtls_blowfish_init( &ctx );
Paul Bakkera9379c02012-07-04 11:02:11 +000094
Paul Bakkera9379c02012-07-04 11:02:11 +000095
Azim Khand30ca132017-06-09 04:32:58 +010096 mbedtls_blowfish_setkey( &ctx, key_str->x, key_str->len * 8 );
97 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 +020098 if( cbc_result == 0)
Paul Bakkera9379c02012-07-04 11:02:11 +000099 {
Paul Bakkera9379c02012-07-04 11:02:11 +0000100
Azim Khand30ca132017-06-09 04:32:58 +0100101 TEST_ASSERT( hexcmp( output, hex_dst_string->x, src_str->len, hex_dst_string->len ) == 0 );
Paul Bakkera9379c02012-07-04 11:02:11 +0000102 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200103
Paul Bakkerbd51b262014-07-10 15:26:12 +0200104exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105 mbedtls_blowfish_free( &ctx );
Paul Bakkera9379c02012-07-04 11:02:11 +0000106}
Paul Bakker33b43f12013-08-20 11:48:36 +0200107/* END_CASE */
Paul Bakkera9379c02012-07-04 11:02:11 +0000108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
Azim Khand30ca132017-06-09 04:32:58 +0100110void blowfish_encrypt_cfb64( HexParam_t * key_str, HexParam_t * iv_str,
111 HexParam_t * src_str, HexParam_t * hex_dst_string
112 )
Paul Bakkera9379c02012-07-04 11:02:11 +0000113{
Paul Bakkera9379c02012-07-04 11:02:11 +0000114 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115 mbedtls_blowfish_context ctx;
Paul Bakkera9379c02012-07-04 11:02:11 +0000116 size_t iv_offset = 0;
Paul Bakkera9379c02012-07-04 11:02:11 +0000117
Paul Bakkera9379c02012-07-04 11:02:11 +0000118 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119 mbedtls_blowfish_init( &ctx );
Paul Bakkera9379c02012-07-04 11:02:11 +0000120
Paul Bakkera9379c02012-07-04 11:02:11 +0000121
Azim Khand30ca132017-06-09 04:32:58 +0100122 mbedtls_blowfish_setkey( &ctx, key_str->x, key_str->len * 8 );
123 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 +0000124
Azim Khand30ca132017-06-09 04:32:58 +0100125 TEST_ASSERT( hexcmp( output, hex_dst_string->x, src_str->len, hex_dst_string->len ) == 0 );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200126
Paul Bakkerbd51b262014-07-10 15:26:12 +0200127exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 mbedtls_blowfish_free( &ctx );
Paul Bakkera9379c02012-07-04 11:02:11 +0000129}
Paul Bakker33b43f12013-08-20 11:48:36 +0200130/* END_CASE */
Paul Bakkera9379c02012-07-04 11:02:11 +0000131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
Azim Khand30ca132017-06-09 04:32:58 +0100133void blowfish_decrypt_cfb64( HexParam_t * key_str, HexParam_t * iv_str,
134 HexParam_t * src_str, HexParam_t * hex_dst_string
135 )
Paul Bakkera9379c02012-07-04 11:02:11 +0000136{
Paul Bakkera9379c02012-07-04 11:02:11 +0000137 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138 mbedtls_blowfish_context ctx;
Paul Bakkera9379c02012-07-04 11:02:11 +0000139 size_t iv_offset = 0;
Paul Bakkera9379c02012-07-04 11:02:11 +0000140
Paul Bakkera9379c02012-07-04 11:02:11 +0000141 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142 mbedtls_blowfish_init( &ctx );
Paul Bakkera9379c02012-07-04 11:02:11 +0000143
Paul Bakkera9379c02012-07-04 11:02:11 +0000144
Azim Khand30ca132017-06-09 04:32:58 +0100145 mbedtls_blowfish_setkey( &ctx, key_str->x, key_str->len * 8 );
146 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 +0000147
Azim Khand30ca132017-06-09 04:32:58 +0100148 TEST_ASSERT( hexcmp( output, hex_dst_string->x, src_str->len, hex_dst_string->len ) == 0 );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200149
Paul Bakkerbd51b262014-07-10 15:26:12 +0200150exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 mbedtls_blowfish_free( &ctx );
Paul Bakkera9379c02012-07-04 11:02:11 +0000152}
Paul Bakker33b43f12013-08-20 11:48:36 +0200153/* END_CASE */
Paul Bakkera9379c02012-07-04 11:02:11 +0000154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CTR */
Azim Khand30ca132017-06-09 04:32:58 +0100156void blowfish_encrypt_ctr( HexParam_t * key_str, HexParam_t * iv_str,
157 HexParam_t * src_str, HexParam_t * hex_dst_string )
Paul Bakkera9379c02012-07-04 11:02:11 +0000158{
Paul Bakkera9379c02012-07-04 11:02:11 +0000159 unsigned char stream_str[100];
Paul Bakkera9379c02012-07-04 11:02:11 +0000160 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161 mbedtls_blowfish_context ctx;
Paul Bakkera9379c02012-07-04 11:02:11 +0000162 size_t iv_offset = 0;
Paul Bakkera9379c02012-07-04 11:02:11 +0000163
Paul Bakkera9379c02012-07-04 11:02:11 +0000164 memset(stream_str, 0x00, 100);
Paul Bakkera9379c02012-07-04 11:02:11 +0000165 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166 mbedtls_blowfish_init( &ctx );
Paul Bakkera9379c02012-07-04 11:02:11 +0000167
Paul Bakkera9379c02012-07-04 11:02:11 +0000168
Azim Khand30ca132017-06-09 04:32:58 +0100169 mbedtls_blowfish_setkey( &ctx, key_str->x, key_str->len * 8 );
170 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 +0000171
Azim Khand30ca132017-06-09 04:32:58 +0100172 TEST_ASSERT( hexcmp( output, hex_dst_string->x, src_str->len, hex_dst_string->len ) == 0 );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200173
Paul Bakkerbd51b262014-07-10 15:26:12 +0200174exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 mbedtls_blowfish_free( &ctx );
Paul Bakkera9379c02012-07-04 11:02:11 +0000176}
Paul Bakker33b43f12013-08-20 11:48:36 +0200177/* END_CASE */