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