Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1 | /* BEGIN_HEADER */ |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 2 | #include "polarssl/blowfish.h" |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 3 | /* END_HEADER */ |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 4 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 5 | /* BEGIN_DEPENDENCIES |
| 6 | * depends_on:POLARSSL_BLOWFISH_C |
| 7 | * END_DEPENDENCIES |
| 8 | */ |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 9 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 10 | /* BEGIN_CASE */ |
| 11 | void blowfish_encrypt_ecb( char *hex_key_string, char *hex_src_string, |
| 12 | char *hex_dst_string, int setkey_result ) |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 13 | { |
| 14 | unsigned char key_str[100]; |
| 15 | unsigned char src_str[100]; |
| 16 | unsigned char dst_str[100]; |
| 17 | unsigned char output[100]; |
| 18 | blowfish_context ctx; |
| 19 | int key_len; |
| 20 | |
| 21 | memset(key_str, 0x00, 100); |
| 22 | memset(src_str, 0x00, 100); |
| 23 | memset(dst_str, 0x00, 100); |
| 24 | memset(output, 0x00, 100); |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame^] | 25 | blowfish_init( &ctx ); |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 26 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 27 | key_len = unhexify( key_str, hex_key_string ); |
| 28 | unhexify( src_str, hex_src_string ); |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 29 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 30 | TEST_ASSERT( blowfish_setkey( &ctx, key_str, key_len * 8 ) == setkey_result ); |
| 31 | if( setkey_result == 0 ) |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 32 | { |
| 33 | TEST_ASSERT( blowfish_crypt_ecb( &ctx, BLOWFISH_ENCRYPT, src_str, output ) == 0 ); |
| 34 | hexify( dst_str, output, 8 ); |
| 35 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 36 | TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 ); |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 37 | } |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame^] | 38 | |
| 39 | blowfish_free( &ctx ); |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 40 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 41 | /* END_CASE */ |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 42 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 43 | /* BEGIN_CASE */ |
| 44 | void blowfish_decrypt_ecb( char *hex_key_string, char *hex_src_string, |
| 45 | char *hex_dst_string, int setkey_result ) |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 46 | { |
| 47 | unsigned char key_str[100]; |
| 48 | unsigned char src_str[100]; |
| 49 | unsigned char dst_str[100]; |
| 50 | unsigned char output[100]; |
| 51 | blowfish_context ctx; |
| 52 | int key_len; |
| 53 | |
| 54 | memset(key_str, 0x00, 100); |
| 55 | memset(src_str, 0x00, 100); |
| 56 | memset(dst_str, 0x00, 100); |
| 57 | memset(output, 0x00, 100); |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame^] | 58 | blowfish_init( &ctx ); |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 59 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 60 | key_len = unhexify( key_str, hex_key_string ); |
| 61 | unhexify( src_str, hex_src_string ); |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 62 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 63 | TEST_ASSERT( blowfish_setkey( &ctx, key_str, key_len * 8 ) == setkey_result ); |
| 64 | if( setkey_result == 0 ) |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 65 | { |
| 66 | TEST_ASSERT( blowfish_crypt_ecb( &ctx, BLOWFISH_DECRYPT, src_str, output ) == 0 ); |
| 67 | hexify( dst_str, output, 8 ); |
| 68 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 69 | TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 ); |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 70 | } |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame^] | 71 | |
| 72 | blowfish_free( &ctx ); |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 73 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 74 | /* END_CASE */ |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 75 | |
Manuel Pégourié-Gonnard | 92cb1d3 | 2013-09-13 16:24:20 +0200 | [diff] [blame] | 76 | /* BEGIN_CASE depends_on:POLARSSL_CIPHER_MODE_CBC */ |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 77 | void blowfish_encrypt_cbc( char *hex_key_string, char *hex_iv_string, |
| 78 | char *hex_src_string, char *hex_dst_string, |
| 79 | int cbc_result ) |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 80 | { |
| 81 | unsigned char key_str[100]; |
| 82 | unsigned char iv_str[100]; |
| 83 | unsigned char src_str[100]; |
| 84 | unsigned char dst_str[100]; |
| 85 | unsigned char output[100]; |
| 86 | blowfish_context ctx; |
| 87 | int key_len, data_len; |
| 88 | |
| 89 | memset(key_str, 0x00, 100); |
| 90 | memset(iv_str, 0x00, 100); |
| 91 | memset(src_str, 0x00, 100); |
| 92 | memset(dst_str, 0x00, 100); |
| 93 | memset(output, 0x00, 100); |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame^] | 94 | blowfish_init( &ctx ); |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 95 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 96 | key_len = unhexify( key_str, hex_key_string ); |
| 97 | unhexify( iv_str, hex_iv_string ); |
| 98 | data_len = unhexify( src_str, hex_src_string ); |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 99 | |
| 100 | blowfish_setkey( &ctx, key_str, key_len * 8 ); |
| 101 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 102 | TEST_ASSERT( blowfish_crypt_cbc( &ctx, BLOWFISH_ENCRYPT, data_len , iv_str, src_str, output ) == cbc_result ); |
| 103 | if( cbc_result == 0 ) |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 104 | { |
| 105 | hexify( dst_str, output, data_len ); |
| 106 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 107 | TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 ); |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 108 | } |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame^] | 109 | |
| 110 | blowfish_free( &ctx ); |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 111 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 112 | /* END_CASE */ |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 113 | |
Manuel Pégourié-Gonnard | 92cb1d3 | 2013-09-13 16:24:20 +0200 | [diff] [blame] | 114 | /* BEGIN_CASE depends_on:POLARSSL_CIPHER_MODE_CBC */ |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 115 | void blowfish_decrypt_cbc( char *hex_key_string, char *hex_iv_string, |
| 116 | char *hex_src_string, char *hex_dst_string, |
| 117 | int cbc_result ) |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 118 | { |
| 119 | unsigned char key_str[100]; |
| 120 | unsigned char iv_str[100]; |
| 121 | unsigned char src_str[100]; |
| 122 | unsigned char dst_str[100]; |
| 123 | unsigned char output[100]; |
| 124 | blowfish_context ctx; |
| 125 | int key_len, data_len; |
| 126 | |
| 127 | memset(key_str, 0x00, 100); |
| 128 | memset(iv_str, 0x00, 100); |
| 129 | memset(src_str, 0x00, 100); |
| 130 | memset(dst_str, 0x00, 100); |
| 131 | memset(output, 0x00, 100); |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame^] | 132 | blowfish_init( &ctx ); |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 133 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 134 | key_len = unhexify( key_str, hex_key_string ); |
| 135 | unhexify( iv_str, hex_iv_string ); |
| 136 | data_len = unhexify( src_str, hex_src_string ); |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 137 | |
| 138 | blowfish_setkey( &ctx, key_str, key_len * 8 ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 139 | TEST_ASSERT( blowfish_crypt_cbc( &ctx, BLOWFISH_DECRYPT, data_len , iv_str, src_str, output ) == cbc_result ); |
| 140 | if( cbc_result == 0) |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 141 | { |
| 142 | hexify( dst_str, output, data_len ); |
| 143 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 144 | TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 ); |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 145 | } |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame^] | 146 | |
| 147 | blowfish_free( &ctx ); |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 148 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 149 | /* END_CASE */ |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 150 | |
Manuel Pégourié-Gonnard | 29dcc0b | 2014-03-10 11:32:07 +0100 | [diff] [blame] | 151 | /* BEGIN_CASE depends_on:POLARSSL_CIPHER_MODE_CFB */ |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 152 | void blowfish_encrypt_cfb64( char *hex_key_string, char *hex_iv_string, |
| 153 | char *hex_src_string, char *hex_dst_string ) |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 154 | { |
| 155 | unsigned char key_str[100]; |
| 156 | unsigned char iv_str[100]; |
| 157 | unsigned char src_str[100]; |
| 158 | unsigned char dst_str[100]; |
| 159 | unsigned char output[100]; |
| 160 | blowfish_context ctx; |
| 161 | size_t iv_offset = 0; |
| 162 | int key_len, src_len; |
| 163 | |
| 164 | memset(key_str, 0x00, 100); |
| 165 | memset(iv_str, 0x00, 100); |
| 166 | memset(src_str, 0x00, 100); |
| 167 | memset(dst_str, 0x00, 100); |
| 168 | memset(output, 0x00, 100); |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame^] | 169 | blowfish_init( &ctx ); |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 170 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 171 | key_len = unhexify( key_str, hex_key_string ); |
| 172 | unhexify( iv_str, hex_iv_string ); |
| 173 | src_len = unhexify( src_str, hex_src_string ); |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 174 | |
| 175 | blowfish_setkey( &ctx, key_str, key_len * 8 ); |
| 176 | TEST_ASSERT( blowfish_crypt_cfb64( &ctx, BLOWFISH_ENCRYPT, src_len, &iv_offset, iv_str, src_str, output ) == 0 ); |
| 177 | hexify( dst_str, output, src_len ); |
| 178 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 179 | TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 ); |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame^] | 180 | |
| 181 | blowfish_free( &ctx ); |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 182 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 183 | /* END_CASE */ |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 184 | |
Manuel Pégourié-Gonnard | 29dcc0b | 2014-03-10 11:32:07 +0100 | [diff] [blame] | 185 | /* BEGIN_CASE depends_on:POLARSSL_CIPHER_MODE_CFB */ |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 186 | void blowfish_decrypt_cfb64( char *hex_key_string, char *hex_iv_string, |
| 187 | char *hex_src_string, char *hex_dst_string ) |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 188 | { |
| 189 | unsigned char key_str[100]; |
| 190 | unsigned char iv_str[100]; |
| 191 | unsigned char src_str[100]; |
| 192 | unsigned char dst_str[100]; |
| 193 | unsigned char output[100]; |
| 194 | blowfish_context ctx; |
| 195 | size_t iv_offset = 0; |
| 196 | int key_len, src_len; |
| 197 | |
| 198 | memset(key_str, 0x00, 100); |
| 199 | memset(iv_str, 0x00, 100); |
| 200 | memset(src_str, 0x00, 100); |
| 201 | memset(dst_str, 0x00, 100); |
| 202 | memset(output, 0x00, 100); |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame^] | 203 | blowfish_init( &ctx ); |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 204 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 205 | key_len = unhexify( key_str, hex_key_string ); |
| 206 | unhexify( iv_str, hex_iv_string ); |
| 207 | src_len = unhexify( src_str, hex_src_string ); |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 208 | |
| 209 | blowfish_setkey( &ctx, key_str, key_len * 8 ); |
| 210 | TEST_ASSERT( blowfish_crypt_cfb64( &ctx, BLOWFISH_DECRYPT, src_len, &iv_offset, iv_str, src_str, output ) == 0 ); |
| 211 | hexify( dst_str, output, src_len ); |
| 212 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 213 | TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 ); |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame^] | 214 | |
| 215 | blowfish_free( &ctx ); |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 216 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 217 | /* END_CASE */ |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 218 | |
Manuel Pégourié-Gonnard | 29dcc0b | 2014-03-10 11:32:07 +0100 | [diff] [blame] | 219 | /* BEGIN_CASE depends_on:POLARSSL_CIPHER_MODE_CTR */ |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 220 | void blowfish_encrypt_ctr( char *hex_key_string, char *hex_iv_string, |
| 221 | char *hex_src_string, char *hex_dst_string ) |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 222 | { |
| 223 | unsigned char key_str[100]; |
| 224 | unsigned char iv_str[100]; |
| 225 | unsigned char stream_str[100]; |
| 226 | unsigned char src_str[100]; |
| 227 | unsigned char dst_str[100]; |
| 228 | unsigned char output[100]; |
| 229 | blowfish_context ctx; |
| 230 | size_t iv_offset = 0; |
| 231 | int key_len, src_len; |
| 232 | |
| 233 | memset(key_str, 0x00, 100); |
| 234 | memset(iv_str, 0x00, 100); |
| 235 | memset(stream_str, 0x00, 100); |
| 236 | memset(src_str, 0x00, 100); |
| 237 | memset(dst_str, 0x00, 100); |
| 238 | memset(output, 0x00, 100); |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame^] | 239 | blowfish_init( &ctx ); |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 240 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 241 | key_len = unhexify( key_str, hex_key_string ); |
| 242 | unhexify( iv_str, hex_iv_string ); |
| 243 | src_len = unhexify( src_str, hex_src_string ); |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 244 | |
| 245 | blowfish_setkey( &ctx, key_str, key_len * 8 ); |
| 246 | TEST_ASSERT( blowfish_crypt_ctr( &ctx, src_len, &iv_offset, iv_str, stream_str, src_str, output ) == 0 ); |
| 247 | hexify( dst_str, output, src_len ); |
| 248 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 249 | TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 ); |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame^] | 250 | |
| 251 | blowfish_free( &ctx ); |
Paul Bakker | a9379c0 | 2012-07-04 11:02:11 +0000 | [diff] [blame] | 252 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 253 | /* END_CASE */ |