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