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