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 |
| 95 | dec_empty_buf: |
| 96 | unsigned char key[32]; |
| 97 | unsigned char iv[16]; |
| 98 | |
| 99 | cipher_context_t ctx_dec; |
| 100 | const cipher_info_t *cipher_info; |
| 101 | |
| 102 | unsigned char encbuf[64]; |
| 103 | unsigned char decbuf[64]; |
| 104 | |
Paul Bakker | f4a3f30 | 2011-04-24 15:53:29 +0000 | [diff] [blame] | 105 | size_t outlen = 0; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 106 | |
| 107 | memset( key, 0, 32 ); |
| 108 | memset( iv , 0, 16 ); |
| 109 | |
| 110 | memset( &ctx_dec, 0, sizeof( ctx_dec ) ); |
| 111 | |
| 112 | memset( encbuf, 0, 64 ); |
| 113 | memset( decbuf, 0, 64 ); |
| 114 | |
| 115 | /* Initialise enc and dec contexts */ |
| 116 | cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CBC ); |
| 117 | TEST_ASSERT( NULL != cipher_info); |
| 118 | |
| 119 | TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); |
| 120 | |
| 121 | TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); |
| 122 | |
| 123 | TEST_ASSERT( 0 == cipher_reset( &ctx_dec, iv ) ); |
| 124 | |
| 125 | /* decode 0-byte string */ |
| 126 | TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, 0, decbuf, &outlen ) ); |
| 127 | TEST_ASSERT( 0 == outlen ); |
Paul Bakker | c65ab34 | 2011-06-09 15:44:37 +0000 | [diff] [blame] | 128 | 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] | 129 | TEST_ASSERT( 0 == outlen ); |
| 130 | |
| 131 | TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) ); |
| 132 | END_CASE |
| 133 | |
| 134 | BEGIN_CASE |
| 135 | enc_dec_buf_multipart:cipher_id:key_len:first_length:second_length: |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 136 | size_t first_length = {first_length}; |
| 137 | size_t second_length = {second_length}; |
| 138 | size_t length = first_length + second_length; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 139 | unsigned char key[32]; |
| 140 | unsigned char iv[16]; |
| 141 | |
| 142 | cipher_context_t ctx_dec; |
| 143 | cipher_context_t ctx_enc; |
| 144 | const cipher_info_t *cipher_info; |
| 145 | |
| 146 | unsigned char inbuf[64]; |
| 147 | unsigned char encbuf[64]; |
| 148 | unsigned char decbuf[64]; |
| 149 | |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 150 | size_t outlen = 0; |
| 151 | size_t totaloutlen = 0; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 152 | |
| 153 | memset( key, 0, 32 ); |
| 154 | memset( iv , 0, 16 ); |
| 155 | |
| 156 | memset( &ctx_dec, 0, sizeof( ctx_dec ) ); |
| 157 | memset( &ctx_enc, 0, sizeof( ctx_enc ) ); |
| 158 | |
| 159 | memset( inbuf, 5, 64 ); |
| 160 | memset( encbuf, 0, 64 ); |
| 161 | memset( decbuf, 0, 64 ); |
| 162 | |
| 163 | /* Initialise enc and dec contexts */ |
| 164 | cipher_info = cipher_info_from_type( {cipher_id} ); |
| 165 | TEST_ASSERT( NULL != cipher_info); |
| 166 | |
| 167 | TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); |
| 168 | TEST_ASSERT( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); |
| 169 | |
| 170 | TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, {key_len}, POLARSSL_DECRYPT ) ); |
| 171 | TEST_ASSERT( 0 == cipher_setkey( &ctx_enc, key, {key_len}, POLARSSL_ENCRYPT ) ); |
| 172 | |
| 173 | TEST_ASSERT( 0 == cipher_reset( &ctx_dec, iv ) ); |
| 174 | TEST_ASSERT( 0 == cipher_reset( &ctx_enc, iv ) ); |
| 175 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 176 | /* encode length number of bytes from inbuf */ |
| 177 | TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); |
| 178 | totaloutlen = outlen; |
| 179 | TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); |
| 180 | totaloutlen += outlen; |
Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 181 | TEST_ASSERT( totaloutlen == length || |
| 182 | ( totaloutlen % cipher_get_block_size( &ctx_enc ) == 0 && |
| 183 | totaloutlen < length && |
| 184 | totaloutlen + cipher_get_block_size( &ctx_enc ) > length ) ); |
| 185 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 186 | TEST_ASSERT( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); |
| 187 | totaloutlen += outlen; |
Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 188 | TEST_ASSERT( totaloutlen == length || |
| 189 | ( totaloutlen % cipher_get_block_size( &ctx_enc ) == 0 && |
| 190 | totaloutlen > length && |
| 191 | totaloutlen <= length + cipher_get_block_size( &ctx_enc ) ) ); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 192 | |
| 193 | /* decode the previously encoded string */ |
Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 194 | TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, totaloutlen, decbuf, &outlen ) ); |
| 195 | totaloutlen = outlen; |
| 196 | |
| 197 | TEST_ASSERT( totaloutlen == length || |
| 198 | ( totaloutlen % cipher_get_block_size( &ctx_dec ) == 0 && |
| 199 | totaloutlen < length && |
| 200 | totaloutlen + cipher_get_block_size( &ctx_dec ) > length ) ); |
| 201 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 202 | TEST_ASSERT( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); |
Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 203 | totaloutlen += outlen; |
| 204 | |
| 205 | TEST_ASSERT( totaloutlen == length ); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 206 | |
| 207 | TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) ); |
| 208 | |
| 209 | TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) ); |
| 210 | TEST_ASSERT( 0 == cipher_free_ctx( &ctx_enc ) ); |
| 211 | END_CASE |
| 212 | |
Manuel Pégourié-Gonnard | d5fdcaf | 2013-07-24 18:05:00 +0200 | [diff] [blame] | 213 | BEGIN_CASE |
| 214 | set_padding:cipher_id:pad_mode:ret: |
| 215 | const cipher_info_t *cipher_info; |
| 216 | cipher_context_t ctx; |
| 217 | |
| 218 | cipher_info = cipher_info_from_type( {cipher_id} ); |
| 219 | TEST_ASSERT( NULL != cipher_info ); |
| 220 | TEST_ASSERT( 0 == cipher_init_ctx( &ctx, cipher_info ) ); |
| 221 | |
| 222 | TEST_ASSERT( {ret} == cipher_set_padding_mode( &ctx, {pad_mode} ) ); |
| 223 | |
| 224 | TEST_ASSERT( 0 == cipher_free_ctx( &ctx ) ); |
| 225 | END_CASE |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 226 | |
| 227 | BEGIN_CASE |
Manuel Pégourié-Gonnard | a640849 | 2013-07-26 10:55:02 +0200 | [diff] [blame] | 228 | check_padding:pad_mode:input:ret:dlen: |
| 229 | cipher_info_t cipher_info; |
| 230 | cipher_context_t ctx; |
| 231 | unsigned char input[16]; |
| 232 | size_t ilen, dlen; |
| 233 | |
| 234 | /* build a fake context just for getting access to get_padding */ |
| 235 | memset( &ctx, 0, sizeof( ctx ) ); |
| 236 | cipher_info.mode = POLARSSL_MODE_CBC; |
| 237 | ctx.cipher_info = &cipher_info; |
| 238 | |
| 239 | TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, {pad_mode} ) ); |
| 240 | |
| 241 | ilen = unhexify( input, {input} ); |
| 242 | |
| 243 | TEST_ASSERT( {ret} == ctx.get_padding( input, ilen, &dlen ) ); |
| 244 | if( 0 == {ret} ) |
| 245 | TEST_ASSERT( dlen == {dlen} ); |
| 246 | END_CASE |
| 247 | |
| 248 | BEGIN_CASE |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 249 | cipher_selftest: |
| 250 | { |
| 251 | TEST_ASSERT( cipher_self_test( 0 ) == 0 ); |
| 252 | } |
| 253 | END_CASE |