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