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