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