Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 1 | BEGIN_HEADER |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 2 | #include <polarssl/cipher.h> |
| 3 | END_HEADER |
| 4 | |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 5 | BEGIN_DEPENDENCIES |
| 6 | depends_on:POLARSSL_CIPHER_C |
| 7 | END_DEPENDENCIES |
| 8 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 9 | BEGIN_CASE |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame^] | 10 | enc_dec_buf:#cipher_id:cipher_string:#key_len:#length_val:#pad_mode |
| 11 | { |
| 12 | size_t length = {length_val}; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 13 | unsigned char key[32]; |
| 14 | unsigned char iv[16]; |
| 15 | |
| 16 | const cipher_info_t *cipher_info; |
| 17 | cipher_context_t ctx_dec; |
| 18 | cipher_context_t ctx_enc; |
| 19 | |
| 20 | unsigned char inbuf[64]; |
| 21 | unsigned char encbuf[64]; |
| 22 | unsigned char decbuf[64]; |
| 23 | |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 24 | size_t outlen = 0; |
Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 25 | size_t total_len = 0; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 26 | |
| 27 | memset( key, 0, 32 ); |
| 28 | memset( iv , 0, 16 ); |
| 29 | |
| 30 | memset( &ctx_dec, 0, sizeof( ctx_dec ) ); |
| 31 | memset( &ctx_enc, 0, sizeof( ctx_enc ) ); |
| 32 | |
| 33 | memset( inbuf, 5, 64 ); |
| 34 | memset( encbuf, 0, 64 ); |
| 35 | memset( decbuf, 0, 64 ); |
| 36 | |
| 37 | /* Check and get info structures */ |
| 38 | cipher_info = cipher_info_from_type( {cipher_id} ); |
| 39 | TEST_ASSERT( NULL != cipher_info ); |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame^] | 40 | TEST_ASSERT( cipher_info_from_string( {cipher_string} ) == cipher_info ); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 41 | |
| 42 | /* Initialise enc and dec contexts */ |
| 43 | TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); |
| 44 | TEST_ASSERT( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); |
| 45 | |
| 46 | TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, {key_len}, POLARSSL_DECRYPT ) ); |
| 47 | TEST_ASSERT( 0 == cipher_setkey( &ctx_enc, key, {key_len}, POLARSSL_ENCRYPT ) ); |
| 48 | |
Manuel Pégourié-Gonnard | 6c97899 | 2013-07-26 13:20:42 +0200 | [diff] [blame] | 49 | if( -1 != {pad_mode} ) |
| 50 | { |
| 51 | TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx_dec, {pad_mode} ) ); |
| 52 | TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx_enc, {pad_mode} ) ); |
| 53 | } |
| 54 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 55 | TEST_ASSERT( 0 == cipher_reset( &ctx_dec, iv ) ); |
| 56 | TEST_ASSERT( 0 == cipher_reset( &ctx_enc, iv ) ); |
| 57 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 58 | /* encode length number of bytes from inbuf */ |
| 59 | TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); |
Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 60 | total_len = outlen; |
| 61 | |
| 62 | TEST_ASSERT( total_len == length || |
| 63 | ( total_len % cipher_get_block_size( &ctx_enc ) == 0 && |
| 64 | total_len < length && |
| 65 | total_len + cipher_get_block_size( &ctx_enc ) > length ) ); |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 66 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 67 | TEST_ASSERT( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); |
Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 68 | total_len += outlen; |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 69 | |
Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 70 | TEST_ASSERT( total_len == length || |
| 71 | ( total_len % cipher_get_block_size( &ctx_enc ) == 0 && |
| 72 | total_len > length && |
| 73 | total_len <= length + cipher_get_block_size( &ctx_enc ) ) ); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 74 | |
| 75 | /* decode the previously encoded string */ |
Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 76 | TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, total_len, decbuf, &outlen ) ); |
| 77 | total_len = outlen; |
| 78 | |
| 79 | TEST_ASSERT( total_len == length || |
| 80 | ( total_len % cipher_get_block_size( &ctx_dec ) == 0 && |
| 81 | total_len < length && |
| 82 | total_len + cipher_get_block_size( &ctx_dec ) >= length ) ); |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 83 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 84 | TEST_ASSERT( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); |
Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 85 | total_len += outlen; |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 86 | |
Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 87 | TEST_ASSERT( total_len == length ); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 88 | |
| 89 | TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) ); |
| 90 | |
| 91 | TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) ); |
| 92 | TEST_ASSERT( 0 == cipher_free_ctx( &ctx_enc ) ); |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame^] | 93 | } |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 94 | END_CASE |
| 95 | |
| 96 | BEGIN_CASE |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame^] | 97 | enc_fail:#cipher_id:#pad_mode:#key_len:#length_val:#ret |
| 98 | { |
| 99 | size_t length = {length_val}; |
Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 100 | unsigned char key[32]; |
| 101 | unsigned char iv[16]; |
| 102 | |
| 103 | const cipher_info_t *cipher_info; |
| 104 | cipher_context_t ctx; |
| 105 | |
| 106 | unsigned char inbuf[64]; |
| 107 | unsigned char encbuf[64]; |
| 108 | |
| 109 | size_t outlen = 0; |
| 110 | |
| 111 | memset( key, 0, 32 ); |
| 112 | memset( iv , 0, 16 ); |
| 113 | |
| 114 | memset( &ctx, 0, sizeof( ctx ) ); |
| 115 | |
| 116 | memset( inbuf, 5, 64 ); |
| 117 | memset( encbuf, 0, 64 ); |
| 118 | |
| 119 | /* Check and get info structures */ |
| 120 | cipher_info = cipher_info_from_type( {cipher_id} ); |
| 121 | TEST_ASSERT( NULL != cipher_info ); |
| 122 | |
| 123 | /* Initialise context */ |
| 124 | TEST_ASSERT( 0 == cipher_init_ctx( &ctx, cipher_info ) ); |
| 125 | TEST_ASSERT( 0 == cipher_setkey( &ctx, key, {key_len}, POLARSSL_ENCRYPT ) ); |
| 126 | TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, {pad_mode} ) ); |
| 127 | TEST_ASSERT( 0 == cipher_reset( &ctx, iv ) ); |
| 128 | |
| 129 | /* encode length number of bytes from inbuf */ |
| 130 | TEST_ASSERT( 0 == cipher_update( &ctx, inbuf, length, encbuf, &outlen ) ); |
| 131 | TEST_ASSERT( {ret} == cipher_finish( &ctx, encbuf + outlen, &outlen ) ); |
| 132 | |
| 133 | /* done */ |
| 134 | TEST_ASSERT( 0 == cipher_free_ctx( &ctx ) ); |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame^] | 135 | } |
Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 136 | END_CASE |
| 137 | |
| 138 | BEGIN_CASE |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 139 | dec_empty_buf: |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame^] | 140 | { |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 141 | unsigned char key[32]; |
| 142 | unsigned char iv[16]; |
| 143 | |
| 144 | cipher_context_t ctx_dec; |
| 145 | const cipher_info_t *cipher_info; |
| 146 | |
| 147 | unsigned char encbuf[64]; |
| 148 | unsigned char decbuf[64]; |
| 149 | |
Paul Bakker | f4a3f30 | 2011-04-24 15:53:29 +0000 | [diff] [blame] | 150 | size_t outlen = 0; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 151 | |
| 152 | memset( key, 0, 32 ); |
| 153 | memset( iv , 0, 16 ); |
| 154 | |
| 155 | memset( &ctx_dec, 0, sizeof( ctx_dec ) ); |
| 156 | |
| 157 | memset( encbuf, 0, 64 ); |
| 158 | memset( decbuf, 0, 64 ); |
| 159 | |
| 160 | /* Initialise enc and dec contexts */ |
| 161 | cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CBC ); |
| 162 | TEST_ASSERT( NULL != cipher_info); |
| 163 | |
| 164 | TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); |
| 165 | |
| 166 | TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); |
| 167 | |
| 168 | TEST_ASSERT( 0 == cipher_reset( &ctx_dec, iv ) ); |
| 169 | |
| 170 | /* decode 0-byte string */ |
| 171 | TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, 0, decbuf, &outlen ) ); |
| 172 | TEST_ASSERT( 0 == outlen ); |
Paul Bakker | c65ab34 | 2011-06-09 15:44:37 +0000 | [diff] [blame] | 173 | TEST_ASSERT( POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 174 | TEST_ASSERT( 0 == outlen ); |
| 175 | |
| 176 | TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) ); |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame^] | 177 | } |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 178 | END_CASE |
| 179 | |
| 180 | BEGIN_CASE |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame^] | 181 | enc_dec_buf_multipart:#cipher_id:#key_len:#first_length_val:#second_length_val |
| 182 | { |
| 183 | size_t first_length = {first_length_val}; |
| 184 | size_t second_length = {second_length_val}; |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 185 | size_t length = first_length + second_length; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 186 | unsigned char key[32]; |
| 187 | unsigned char iv[16]; |
| 188 | |
| 189 | cipher_context_t ctx_dec; |
| 190 | cipher_context_t ctx_enc; |
| 191 | const cipher_info_t *cipher_info; |
| 192 | |
| 193 | unsigned char inbuf[64]; |
| 194 | unsigned char encbuf[64]; |
| 195 | unsigned char decbuf[64]; |
| 196 | |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 197 | size_t outlen = 0; |
| 198 | size_t totaloutlen = 0; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 199 | |
| 200 | memset( key, 0, 32 ); |
| 201 | memset( iv , 0, 16 ); |
| 202 | |
| 203 | memset( &ctx_dec, 0, sizeof( ctx_dec ) ); |
| 204 | memset( &ctx_enc, 0, sizeof( ctx_enc ) ); |
| 205 | |
| 206 | memset( inbuf, 5, 64 ); |
| 207 | memset( encbuf, 0, 64 ); |
| 208 | memset( decbuf, 0, 64 ); |
| 209 | |
| 210 | /* Initialise enc and dec contexts */ |
| 211 | cipher_info = cipher_info_from_type( {cipher_id} ); |
| 212 | TEST_ASSERT( NULL != cipher_info); |
| 213 | |
| 214 | TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); |
| 215 | TEST_ASSERT( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); |
| 216 | |
| 217 | TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, {key_len}, POLARSSL_DECRYPT ) ); |
| 218 | TEST_ASSERT( 0 == cipher_setkey( &ctx_enc, key, {key_len}, POLARSSL_ENCRYPT ) ); |
| 219 | |
| 220 | TEST_ASSERT( 0 == cipher_reset( &ctx_dec, iv ) ); |
| 221 | TEST_ASSERT( 0 == cipher_reset( &ctx_enc, iv ) ); |
| 222 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 223 | /* encode length number of bytes from inbuf */ |
| 224 | TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); |
| 225 | totaloutlen = outlen; |
| 226 | TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); |
| 227 | totaloutlen += outlen; |
Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 228 | TEST_ASSERT( totaloutlen == length || |
| 229 | ( totaloutlen % cipher_get_block_size( &ctx_enc ) == 0 && |
| 230 | totaloutlen < length && |
| 231 | totaloutlen + cipher_get_block_size( &ctx_enc ) > length ) ); |
| 232 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 233 | TEST_ASSERT( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); |
| 234 | totaloutlen += outlen; |
Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 235 | TEST_ASSERT( totaloutlen == length || |
| 236 | ( totaloutlen % cipher_get_block_size( &ctx_enc ) == 0 && |
| 237 | totaloutlen > length && |
| 238 | totaloutlen <= length + cipher_get_block_size( &ctx_enc ) ) ); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 239 | |
| 240 | /* decode the previously encoded string */ |
Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 241 | TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, totaloutlen, decbuf, &outlen ) ); |
| 242 | totaloutlen = outlen; |
| 243 | |
| 244 | TEST_ASSERT( totaloutlen == length || |
| 245 | ( totaloutlen % cipher_get_block_size( &ctx_dec ) == 0 && |
| 246 | totaloutlen < length && |
| 247 | totaloutlen + cipher_get_block_size( &ctx_dec ) > length ) ); |
| 248 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 249 | TEST_ASSERT( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); |
Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 250 | totaloutlen += outlen; |
| 251 | |
| 252 | TEST_ASSERT( totaloutlen == length ); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 253 | |
| 254 | TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) ); |
| 255 | |
| 256 | TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) ); |
| 257 | TEST_ASSERT( 0 == cipher_free_ctx( &ctx_enc ) ); |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame^] | 258 | } |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 259 | END_CASE |
| 260 | |
Manuel Pégourié-Gonnard | d5fdcaf | 2013-07-24 18:05:00 +0200 | [diff] [blame] | 261 | BEGIN_CASE |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame^] | 262 | set_padding:#cipher_id:#pad_mode:#ret |
| 263 | { |
Manuel Pégourié-Gonnard | d5fdcaf | 2013-07-24 18:05:00 +0200 | [diff] [blame] | 264 | const cipher_info_t *cipher_info; |
| 265 | cipher_context_t ctx; |
| 266 | |
| 267 | cipher_info = cipher_info_from_type( {cipher_id} ); |
| 268 | TEST_ASSERT( NULL != cipher_info ); |
| 269 | TEST_ASSERT( 0 == cipher_init_ctx( &ctx, cipher_info ) ); |
| 270 | |
| 271 | TEST_ASSERT( {ret} == cipher_set_padding_mode( &ctx, {pad_mode} ) ); |
| 272 | |
| 273 | TEST_ASSERT( 0 == cipher_free_ctx( &ctx ) ); |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame^] | 274 | } |
Manuel Pégourié-Gonnard | d5fdcaf | 2013-07-24 18:05:00 +0200 | [diff] [blame] | 275 | END_CASE |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 276 | |
| 277 | BEGIN_CASE |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame^] | 278 | check_padding:#pad_mode:input_str:#ret:#dlen_check |
| 279 | { |
Manuel Pégourié-Gonnard | a640849 | 2013-07-26 10:55:02 +0200 | [diff] [blame] | 280 | cipher_info_t cipher_info; |
| 281 | cipher_context_t ctx; |
| 282 | unsigned char input[16]; |
| 283 | size_t ilen, dlen; |
| 284 | |
| 285 | /* build a fake context just for getting access to get_padding */ |
| 286 | memset( &ctx, 0, sizeof( ctx ) ); |
| 287 | cipher_info.mode = POLARSSL_MODE_CBC; |
| 288 | ctx.cipher_info = &cipher_info; |
| 289 | |
| 290 | TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, {pad_mode} ) ); |
| 291 | |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame^] | 292 | ilen = unhexify( input, {input_str} ); |
Manuel Pégourié-Gonnard | a640849 | 2013-07-26 10:55:02 +0200 | [diff] [blame] | 293 | |
| 294 | TEST_ASSERT( {ret} == ctx.get_padding( input, ilen, &dlen ) ); |
| 295 | if( 0 == {ret} ) |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame^] | 296 | TEST_ASSERT( dlen == (size_t) {dlen_check} ); |
| 297 | } |
Manuel Pégourié-Gonnard | a640849 | 2013-07-26 10:55:02 +0200 | [diff] [blame] | 298 | END_CASE |
| 299 | |
| 300 | BEGIN_CASE |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 301 | cipher_selftest: |
| 302 | { |
| 303 | TEST_ASSERT( cipher_self_test( 0 ) == 0 ); |
| 304 | } |
| 305 | END_CASE |