mbed TLS Build
Dependents: Encrypt_Decrypt1 mbed_blink_tls encrypt encrypt
tests/suites/test_suite_blowfish.function@0:cdf462088d13, 2017-01-05 (annotated)
- Committer:
- markrad
- Date:
- Thu Jan 05 00:18:44 2017 +0000
- Revision:
- 0:cdf462088d13
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
markrad | 0:cdf462088d13 | 1 | /* BEGIN_HEADER */ |
markrad | 0:cdf462088d13 | 2 | #include "mbedtls/blowfish.h" |
markrad | 0:cdf462088d13 | 3 | /* END_HEADER */ |
markrad | 0:cdf462088d13 | 4 | |
markrad | 0:cdf462088d13 | 5 | /* BEGIN_DEPENDENCIES |
markrad | 0:cdf462088d13 | 6 | * depends_on:MBEDTLS_BLOWFISH_C |
markrad | 0:cdf462088d13 | 7 | * END_DEPENDENCIES |
markrad | 0:cdf462088d13 | 8 | */ |
markrad | 0:cdf462088d13 | 9 | |
markrad | 0:cdf462088d13 | 10 | /* BEGIN_CASE */ |
markrad | 0:cdf462088d13 | 11 | void blowfish_encrypt_ecb( char *hex_key_string, char *hex_src_string, |
markrad | 0:cdf462088d13 | 12 | char *hex_dst_string, int setkey_result ) |
markrad | 0:cdf462088d13 | 13 | { |
markrad | 0:cdf462088d13 | 14 | unsigned char key_str[100]; |
markrad | 0:cdf462088d13 | 15 | unsigned char src_str[100]; |
markrad | 0:cdf462088d13 | 16 | unsigned char dst_str[100]; |
markrad | 0:cdf462088d13 | 17 | unsigned char output[100]; |
markrad | 0:cdf462088d13 | 18 | mbedtls_blowfish_context ctx; |
markrad | 0:cdf462088d13 | 19 | int key_len; |
markrad | 0:cdf462088d13 | 20 | |
markrad | 0:cdf462088d13 | 21 | memset(key_str, 0x00, 100); |
markrad | 0:cdf462088d13 | 22 | memset(src_str, 0x00, 100); |
markrad | 0:cdf462088d13 | 23 | memset(dst_str, 0x00, 100); |
markrad | 0:cdf462088d13 | 24 | memset(output, 0x00, 100); |
markrad | 0:cdf462088d13 | 25 | mbedtls_blowfish_init( &ctx ); |
markrad | 0:cdf462088d13 | 26 | |
markrad | 0:cdf462088d13 | 27 | key_len = unhexify( key_str, hex_key_string ); |
markrad | 0:cdf462088d13 | 28 | unhexify( src_str, hex_src_string ); |
markrad | 0:cdf462088d13 | 29 | |
markrad | 0:cdf462088d13 | 30 | TEST_ASSERT( mbedtls_blowfish_setkey( &ctx, key_str, key_len * 8 ) == setkey_result ); |
markrad | 0:cdf462088d13 | 31 | if( setkey_result == 0 ) |
markrad | 0:cdf462088d13 | 32 | { |
markrad | 0:cdf462088d13 | 33 | TEST_ASSERT( mbedtls_blowfish_crypt_ecb( &ctx, MBEDTLS_BLOWFISH_ENCRYPT, src_str, output ) == 0 ); |
markrad | 0:cdf462088d13 | 34 | hexify( dst_str, output, 8 ); |
markrad | 0:cdf462088d13 | 35 | |
markrad | 0:cdf462088d13 | 36 | TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 ); |
markrad | 0:cdf462088d13 | 37 | } |
markrad | 0:cdf462088d13 | 38 | |
markrad | 0:cdf462088d13 | 39 | exit: |
markrad | 0:cdf462088d13 | 40 | mbedtls_blowfish_free( &ctx ); |
markrad | 0:cdf462088d13 | 41 | } |
markrad | 0:cdf462088d13 | 42 | /* END_CASE */ |
markrad | 0:cdf462088d13 | 43 | |
markrad | 0:cdf462088d13 | 44 | /* BEGIN_CASE */ |
markrad | 0:cdf462088d13 | 45 | void blowfish_decrypt_ecb( char *hex_key_string, char *hex_src_string, |
markrad | 0:cdf462088d13 | 46 | char *hex_dst_string, int setkey_result ) |
markrad | 0:cdf462088d13 | 47 | { |
markrad | 0:cdf462088d13 | 48 | unsigned char key_str[100]; |
markrad | 0:cdf462088d13 | 49 | unsigned char src_str[100]; |
markrad | 0:cdf462088d13 | 50 | unsigned char dst_str[100]; |
markrad | 0:cdf462088d13 | 51 | unsigned char output[100]; |
markrad | 0:cdf462088d13 | 52 | mbedtls_blowfish_context ctx; |
markrad | 0:cdf462088d13 | 53 | int key_len; |
markrad | 0:cdf462088d13 | 54 | |
markrad | 0:cdf462088d13 | 55 | memset(key_str, 0x00, 100); |
markrad | 0:cdf462088d13 | 56 | memset(src_str, 0x00, 100); |
markrad | 0:cdf462088d13 | 57 | memset(dst_str, 0x00, 100); |
markrad | 0:cdf462088d13 | 58 | memset(output, 0x00, 100); |
markrad | 0:cdf462088d13 | 59 | mbedtls_blowfish_init( &ctx ); |
markrad | 0:cdf462088d13 | 60 | |
markrad | 0:cdf462088d13 | 61 | key_len = unhexify( key_str, hex_key_string ); |
markrad | 0:cdf462088d13 | 62 | unhexify( src_str, hex_src_string ); |
markrad | 0:cdf462088d13 | 63 | |
markrad | 0:cdf462088d13 | 64 | TEST_ASSERT( mbedtls_blowfish_setkey( &ctx, key_str, key_len * 8 ) == setkey_result ); |
markrad | 0:cdf462088d13 | 65 | if( setkey_result == 0 ) |
markrad | 0:cdf462088d13 | 66 | { |
markrad | 0:cdf462088d13 | 67 | TEST_ASSERT( mbedtls_blowfish_crypt_ecb( &ctx, MBEDTLS_BLOWFISH_DECRYPT, src_str, output ) == 0 ); |
markrad | 0:cdf462088d13 | 68 | hexify( dst_str, output, 8 ); |
markrad | 0:cdf462088d13 | 69 | |
markrad | 0:cdf462088d13 | 70 | TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 ); |
markrad | 0:cdf462088d13 | 71 | } |
markrad | 0:cdf462088d13 | 72 | |
markrad | 0:cdf462088d13 | 73 | exit: |
markrad | 0:cdf462088d13 | 74 | mbedtls_blowfish_free( &ctx ); |
markrad | 0:cdf462088d13 | 75 | } |
markrad | 0:cdf462088d13 | 76 | /* END_CASE */ |
markrad | 0:cdf462088d13 | 77 | |
markrad | 0:cdf462088d13 | 78 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */ |
markrad | 0:cdf462088d13 | 79 | void blowfish_encrypt_cbc( char *hex_key_string, char *hex_iv_string, |
markrad | 0:cdf462088d13 | 80 | char *hex_src_string, char *hex_dst_string, |
markrad | 0:cdf462088d13 | 81 | int cbc_result ) |
markrad | 0:cdf462088d13 | 82 | { |
markrad | 0:cdf462088d13 | 83 | unsigned char key_str[100]; |
markrad | 0:cdf462088d13 | 84 | unsigned char iv_str[100]; |
markrad | 0:cdf462088d13 | 85 | unsigned char src_str[100]; |
markrad | 0:cdf462088d13 | 86 | unsigned char dst_str[100]; |
markrad | 0:cdf462088d13 | 87 | unsigned char output[100]; |
markrad | 0:cdf462088d13 | 88 | mbedtls_blowfish_context ctx; |
markrad | 0:cdf462088d13 | 89 | int key_len, data_len; |
markrad | 0:cdf462088d13 | 90 | |
markrad | 0:cdf462088d13 | 91 | memset(key_str, 0x00, 100); |
markrad | 0:cdf462088d13 | 92 | memset(iv_str, 0x00, 100); |
markrad | 0:cdf462088d13 | 93 | memset(src_str, 0x00, 100); |
markrad | 0:cdf462088d13 | 94 | memset(dst_str, 0x00, 100); |
markrad | 0:cdf462088d13 | 95 | memset(output, 0x00, 100); |
markrad | 0:cdf462088d13 | 96 | mbedtls_blowfish_init( &ctx ); |
markrad | 0:cdf462088d13 | 97 | |
markrad | 0:cdf462088d13 | 98 | key_len = unhexify( key_str, hex_key_string ); |
markrad | 0:cdf462088d13 | 99 | unhexify( iv_str, hex_iv_string ); |
markrad | 0:cdf462088d13 | 100 | data_len = unhexify( src_str, hex_src_string ); |
markrad | 0:cdf462088d13 | 101 | |
markrad | 0:cdf462088d13 | 102 | mbedtls_blowfish_setkey( &ctx, key_str, key_len * 8 ); |
markrad | 0:cdf462088d13 | 103 | |
markrad | 0:cdf462088d13 | 104 | TEST_ASSERT( mbedtls_blowfish_crypt_cbc( &ctx, MBEDTLS_BLOWFISH_ENCRYPT, data_len , iv_str, src_str, output ) == cbc_result ); |
markrad | 0:cdf462088d13 | 105 | if( cbc_result == 0 ) |
markrad | 0:cdf462088d13 | 106 | { |
markrad | 0:cdf462088d13 | 107 | hexify( dst_str, output, data_len ); |
markrad | 0:cdf462088d13 | 108 | |
markrad | 0:cdf462088d13 | 109 | TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 ); |
markrad | 0:cdf462088d13 | 110 | } |
markrad | 0:cdf462088d13 | 111 | |
markrad | 0:cdf462088d13 | 112 | exit: |
markrad | 0:cdf462088d13 | 113 | mbedtls_blowfish_free( &ctx ); |
markrad | 0:cdf462088d13 | 114 | } |
markrad | 0:cdf462088d13 | 115 | /* END_CASE */ |
markrad | 0:cdf462088d13 | 116 | |
markrad | 0:cdf462088d13 | 117 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */ |
markrad | 0:cdf462088d13 | 118 | void blowfish_decrypt_cbc( char *hex_key_string, char *hex_iv_string, |
markrad | 0:cdf462088d13 | 119 | char *hex_src_string, char *hex_dst_string, |
markrad | 0:cdf462088d13 | 120 | int cbc_result ) |
markrad | 0:cdf462088d13 | 121 | { |
markrad | 0:cdf462088d13 | 122 | unsigned char key_str[100]; |
markrad | 0:cdf462088d13 | 123 | unsigned char iv_str[100]; |
markrad | 0:cdf462088d13 | 124 | unsigned char src_str[100]; |
markrad | 0:cdf462088d13 | 125 | unsigned char dst_str[100]; |
markrad | 0:cdf462088d13 | 126 | unsigned char output[100]; |
markrad | 0:cdf462088d13 | 127 | mbedtls_blowfish_context ctx; |
markrad | 0:cdf462088d13 | 128 | int key_len, data_len; |
markrad | 0:cdf462088d13 | 129 | |
markrad | 0:cdf462088d13 | 130 | memset(key_str, 0x00, 100); |
markrad | 0:cdf462088d13 | 131 | memset(iv_str, 0x00, 100); |
markrad | 0:cdf462088d13 | 132 | memset(src_str, 0x00, 100); |
markrad | 0:cdf462088d13 | 133 | memset(dst_str, 0x00, 100); |
markrad | 0:cdf462088d13 | 134 | memset(output, 0x00, 100); |
markrad | 0:cdf462088d13 | 135 | mbedtls_blowfish_init( &ctx ); |
markrad | 0:cdf462088d13 | 136 | |
markrad | 0:cdf462088d13 | 137 | key_len = unhexify( key_str, hex_key_string ); |
markrad | 0:cdf462088d13 | 138 | unhexify( iv_str, hex_iv_string ); |
markrad | 0:cdf462088d13 | 139 | data_len = unhexify( src_str, hex_src_string ); |
markrad | 0:cdf462088d13 | 140 | |
markrad | 0:cdf462088d13 | 141 | mbedtls_blowfish_setkey( &ctx, key_str, key_len * 8 ); |
markrad | 0:cdf462088d13 | 142 | TEST_ASSERT( mbedtls_blowfish_crypt_cbc( &ctx, MBEDTLS_BLOWFISH_DECRYPT, data_len , iv_str, src_str, output ) == cbc_result ); |
markrad | 0:cdf462088d13 | 143 | if( cbc_result == 0) |
markrad | 0:cdf462088d13 | 144 | { |
markrad | 0:cdf462088d13 | 145 | hexify( dst_str, output, data_len ); |
markrad | 0:cdf462088d13 | 146 | |
markrad | 0:cdf462088d13 | 147 | TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 ); |
markrad | 0:cdf462088d13 | 148 | } |
markrad | 0:cdf462088d13 | 149 | |
markrad | 0:cdf462088d13 | 150 | exit: |
markrad | 0:cdf462088d13 | 151 | mbedtls_blowfish_free( &ctx ); |
markrad | 0:cdf462088d13 | 152 | } |
markrad | 0:cdf462088d13 | 153 | /* END_CASE */ |
markrad | 0:cdf462088d13 | 154 | |
markrad | 0:cdf462088d13 | 155 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */ |
markrad | 0:cdf462088d13 | 156 | void blowfish_encrypt_cfb64( char *hex_key_string, char *hex_iv_string, |
markrad | 0:cdf462088d13 | 157 | char *hex_src_string, char *hex_dst_string ) |
markrad | 0:cdf462088d13 | 158 | { |
markrad | 0:cdf462088d13 | 159 | unsigned char key_str[100]; |
markrad | 0:cdf462088d13 | 160 | unsigned char iv_str[100]; |
markrad | 0:cdf462088d13 | 161 | unsigned char src_str[100]; |
markrad | 0:cdf462088d13 | 162 | unsigned char dst_str[100]; |
markrad | 0:cdf462088d13 | 163 | unsigned char output[100]; |
markrad | 0:cdf462088d13 | 164 | mbedtls_blowfish_context ctx; |
markrad | 0:cdf462088d13 | 165 | size_t iv_offset = 0; |
markrad | 0:cdf462088d13 | 166 | int key_len, src_len; |
markrad | 0:cdf462088d13 | 167 | |
markrad | 0:cdf462088d13 | 168 | memset(key_str, 0x00, 100); |
markrad | 0:cdf462088d13 | 169 | memset(iv_str, 0x00, 100); |
markrad | 0:cdf462088d13 | 170 | memset(src_str, 0x00, 100); |
markrad | 0:cdf462088d13 | 171 | memset(dst_str, 0x00, 100); |
markrad | 0:cdf462088d13 | 172 | memset(output, 0x00, 100); |
markrad | 0:cdf462088d13 | 173 | mbedtls_blowfish_init( &ctx ); |
markrad | 0:cdf462088d13 | 174 | |
markrad | 0:cdf462088d13 | 175 | key_len = unhexify( key_str, hex_key_string ); |
markrad | 0:cdf462088d13 | 176 | unhexify( iv_str, hex_iv_string ); |
markrad | 0:cdf462088d13 | 177 | src_len = unhexify( src_str, hex_src_string ); |
markrad | 0:cdf462088d13 | 178 | |
markrad | 0:cdf462088d13 | 179 | mbedtls_blowfish_setkey( &ctx, key_str, key_len * 8 ); |
markrad | 0:cdf462088d13 | 180 | TEST_ASSERT( mbedtls_blowfish_crypt_cfb64( &ctx, MBEDTLS_BLOWFISH_ENCRYPT, src_len, &iv_offset, iv_str, src_str, output ) == 0 ); |
markrad | 0:cdf462088d13 | 181 | hexify( dst_str, output, src_len ); |
markrad | 0:cdf462088d13 | 182 | |
markrad | 0:cdf462088d13 | 183 | TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 ); |
markrad | 0:cdf462088d13 | 184 | |
markrad | 0:cdf462088d13 | 185 | exit: |
markrad | 0:cdf462088d13 | 186 | mbedtls_blowfish_free( &ctx ); |
markrad | 0:cdf462088d13 | 187 | } |
markrad | 0:cdf462088d13 | 188 | /* END_CASE */ |
markrad | 0:cdf462088d13 | 189 | |
markrad | 0:cdf462088d13 | 190 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */ |
markrad | 0:cdf462088d13 | 191 | void blowfish_decrypt_cfb64( char *hex_key_string, char *hex_iv_string, |
markrad | 0:cdf462088d13 | 192 | char *hex_src_string, char *hex_dst_string ) |
markrad | 0:cdf462088d13 | 193 | { |
markrad | 0:cdf462088d13 | 194 | unsigned char key_str[100]; |
markrad | 0:cdf462088d13 | 195 | unsigned char iv_str[100]; |
markrad | 0:cdf462088d13 | 196 | unsigned char src_str[100]; |
markrad | 0:cdf462088d13 | 197 | unsigned char dst_str[100]; |
markrad | 0:cdf462088d13 | 198 | unsigned char output[100]; |
markrad | 0:cdf462088d13 | 199 | mbedtls_blowfish_context ctx; |
markrad | 0:cdf462088d13 | 200 | size_t iv_offset = 0; |
markrad | 0:cdf462088d13 | 201 | int key_len, src_len; |
markrad | 0:cdf462088d13 | 202 | |
markrad | 0:cdf462088d13 | 203 | memset(key_str, 0x00, 100); |
markrad | 0:cdf462088d13 | 204 | memset(iv_str, 0x00, 100); |
markrad | 0:cdf462088d13 | 205 | memset(src_str, 0x00, 100); |
markrad | 0:cdf462088d13 | 206 | memset(dst_str, 0x00, 100); |
markrad | 0:cdf462088d13 | 207 | memset(output, 0x00, 100); |
markrad | 0:cdf462088d13 | 208 | mbedtls_blowfish_init( &ctx ); |
markrad | 0:cdf462088d13 | 209 | |
markrad | 0:cdf462088d13 | 210 | key_len = unhexify( key_str, hex_key_string ); |
markrad | 0:cdf462088d13 | 211 | unhexify( iv_str, hex_iv_string ); |
markrad | 0:cdf462088d13 | 212 | src_len = unhexify( src_str, hex_src_string ); |
markrad | 0:cdf462088d13 | 213 | |
markrad | 0:cdf462088d13 | 214 | mbedtls_blowfish_setkey( &ctx, key_str, key_len * 8 ); |
markrad | 0:cdf462088d13 | 215 | TEST_ASSERT( mbedtls_blowfish_crypt_cfb64( &ctx, MBEDTLS_BLOWFISH_DECRYPT, src_len, &iv_offset, iv_str, src_str, output ) == 0 ); |
markrad | 0:cdf462088d13 | 216 | hexify( dst_str, output, src_len ); |
markrad | 0:cdf462088d13 | 217 | |
markrad | 0:cdf462088d13 | 218 | TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 ); |
markrad | 0:cdf462088d13 | 219 | |
markrad | 0:cdf462088d13 | 220 | exit: |
markrad | 0:cdf462088d13 | 221 | mbedtls_blowfish_free( &ctx ); |
markrad | 0:cdf462088d13 | 222 | } |
markrad | 0:cdf462088d13 | 223 | /* END_CASE */ |
markrad | 0:cdf462088d13 | 224 | |
markrad | 0:cdf462088d13 | 225 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CTR */ |
markrad | 0:cdf462088d13 | 226 | void blowfish_encrypt_ctr( char *hex_key_string, char *hex_iv_string, |
markrad | 0:cdf462088d13 | 227 | char *hex_src_string, char *hex_dst_string ) |
markrad | 0:cdf462088d13 | 228 | { |
markrad | 0:cdf462088d13 | 229 | unsigned char key_str[100]; |
markrad | 0:cdf462088d13 | 230 | unsigned char iv_str[100]; |
markrad | 0:cdf462088d13 | 231 | unsigned char stream_str[100]; |
markrad | 0:cdf462088d13 | 232 | unsigned char src_str[100]; |
markrad | 0:cdf462088d13 | 233 | unsigned char dst_str[100]; |
markrad | 0:cdf462088d13 | 234 | unsigned char output[100]; |
markrad | 0:cdf462088d13 | 235 | mbedtls_blowfish_context ctx; |
markrad | 0:cdf462088d13 | 236 | size_t iv_offset = 0; |
markrad | 0:cdf462088d13 | 237 | int key_len, src_len; |
markrad | 0:cdf462088d13 | 238 | |
markrad | 0:cdf462088d13 | 239 | memset(key_str, 0x00, 100); |
markrad | 0:cdf462088d13 | 240 | memset(iv_str, 0x00, 100); |
markrad | 0:cdf462088d13 | 241 | memset(stream_str, 0x00, 100); |
markrad | 0:cdf462088d13 | 242 | memset(src_str, 0x00, 100); |
markrad | 0:cdf462088d13 | 243 | memset(dst_str, 0x00, 100); |
markrad | 0:cdf462088d13 | 244 | memset(output, 0x00, 100); |
markrad | 0:cdf462088d13 | 245 | mbedtls_blowfish_init( &ctx ); |
markrad | 0:cdf462088d13 | 246 | |
markrad | 0:cdf462088d13 | 247 | key_len = unhexify( key_str, hex_key_string ); |
markrad | 0:cdf462088d13 | 248 | unhexify( iv_str, hex_iv_string ); |
markrad | 0:cdf462088d13 | 249 | src_len = unhexify( src_str, hex_src_string ); |
markrad | 0:cdf462088d13 | 250 | |
markrad | 0:cdf462088d13 | 251 | mbedtls_blowfish_setkey( &ctx, key_str, key_len * 8 ); |
markrad | 0:cdf462088d13 | 252 | TEST_ASSERT( mbedtls_blowfish_crypt_ctr( &ctx, src_len, &iv_offset, iv_str, stream_str, src_str, output ) == 0 ); |
markrad | 0:cdf462088d13 | 253 | hexify( dst_str, output, src_len ); |
markrad | 0:cdf462088d13 | 254 | |
markrad | 0:cdf462088d13 | 255 | TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 ); |
markrad | 0:cdf462088d13 | 256 | |
markrad | 0:cdf462088d13 | 257 | exit: |
markrad | 0:cdf462088d13 | 258 | mbedtls_blowfish_free( &ctx ); |
markrad | 0:cdf462088d13 | 259 | } |
markrad | 0:cdf462088d13 | 260 | /* END_CASE */ |