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