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> |
Manuel Pégourié-Gonnard | f7ce67f | 2013-09-03 20:17:35 +0200 | [diff] [blame] | 3 | |
| 4 | #if defined(POLARSSL_GCM_C) |
| 5 | #include <polarssl/gcm.h> |
| 6 | #endif |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 7 | /* END_HEADER */ |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 8 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 9 | /* BEGIN_DEPENDENCIES |
| 10 | * depends_on:POLARSSL_CIPHER_C |
| 11 | * END_DEPENDENCIES |
| 12 | */ |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 13 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 14 | /* BEGIN_CASE */ |
Manuel Pégourié-Gonnard | 66dfc5a | 2014-03-29 16:10:55 +0100 | [diff] [blame] | 15 | void cipher_list( ) |
| 16 | { |
| 17 | const int *cipher_type; |
| 18 | |
| 19 | for( cipher_type = cipher_list(); *cipher_type != 0; cipher_type++ ) |
| 20 | TEST_ASSERT( cipher_info_from_type( *cipher_type ) != NULL ); |
| 21 | } |
| 22 | /* END_CASE */ |
| 23 | |
| 24 | /* BEGIN_CASE */ |
Manuel Pégourié-Gonnard | 5e7693f | 2014-06-13 16:08:07 +0200 | [diff] [blame] | 25 | void cipher_null_args( ) |
| 26 | { |
| 27 | cipher_context_t ctx; |
| 28 | const cipher_info_t *info = cipher_info_from_type( *( cipher_list() ) ); |
| 29 | unsigned char buf[1] = { 0 }; |
| 30 | size_t olen; |
| 31 | |
| 32 | memset( &ctx, 0, sizeof( cipher_context_t ) ); |
| 33 | |
| 34 | TEST_ASSERT( cipher_get_block_size( NULL ) == 0 ); |
| 35 | TEST_ASSERT( cipher_get_block_size( &ctx ) == 0 ); |
| 36 | |
| 37 | TEST_ASSERT( cipher_get_cipher_mode( NULL ) == POLARSSL_MODE_NONE ); |
| 38 | TEST_ASSERT( cipher_get_cipher_mode( &ctx ) == POLARSSL_MODE_NONE ); |
| 39 | |
| 40 | TEST_ASSERT( cipher_get_iv_size( NULL ) == 0 ); |
| 41 | TEST_ASSERT( cipher_get_iv_size( &ctx ) == 0 ); |
| 42 | |
| 43 | TEST_ASSERT( cipher_info_from_string( NULL ) == NULL ); |
| 44 | |
| 45 | TEST_ASSERT( cipher_init_ctx( &ctx, NULL ) |
| 46 | == POLARSSL_ERR_CIPHER_BAD_INPUT_DATA ); |
| 47 | TEST_ASSERT( cipher_init_ctx( NULL, info ) |
| 48 | == POLARSSL_ERR_CIPHER_BAD_INPUT_DATA ); |
| 49 | |
| 50 | TEST_ASSERT( cipher_setkey( NULL, buf, 0, POLARSSL_ENCRYPT ) |
| 51 | == POLARSSL_ERR_CIPHER_BAD_INPUT_DATA ); |
| 52 | TEST_ASSERT( cipher_setkey( &ctx, buf, 0, POLARSSL_ENCRYPT ) |
| 53 | == POLARSSL_ERR_CIPHER_BAD_INPUT_DATA ); |
| 54 | |
| 55 | TEST_ASSERT( cipher_set_iv( NULL, buf, 0 ) |
| 56 | == POLARSSL_ERR_CIPHER_BAD_INPUT_DATA ); |
| 57 | TEST_ASSERT( cipher_set_iv( &ctx, buf, 0 ) |
| 58 | == POLARSSL_ERR_CIPHER_BAD_INPUT_DATA ); |
| 59 | |
| 60 | TEST_ASSERT( cipher_reset( NULL ) == POLARSSL_ERR_CIPHER_BAD_INPUT_DATA ); |
| 61 | TEST_ASSERT( cipher_reset( &ctx ) == POLARSSL_ERR_CIPHER_BAD_INPUT_DATA ); |
| 62 | |
| 63 | #if defined(POLARSSL_CIPHER_MODE_AEAD) |
| 64 | TEST_ASSERT( cipher_update_ad( NULL, buf, 0 ) |
| 65 | == POLARSSL_ERR_CIPHER_BAD_INPUT_DATA ); |
| 66 | TEST_ASSERT( cipher_update_ad( &ctx, buf, 0 ) |
| 67 | == POLARSSL_ERR_CIPHER_BAD_INPUT_DATA ); |
| 68 | #endif |
| 69 | |
| 70 | TEST_ASSERT( cipher_update( NULL, buf, 0, buf, &olen ) |
| 71 | == POLARSSL_ERR_CIPHER_BAD_INPUT_DATA ); |
| 72 | TEST_ASSERT( cipher_update( &ctx, buf, 0, buf, &olen ) |
| 73 | == POLARSSL_ERR_CIPHER_BAD_INPUT_DATA ); |
| 74 | |
| 75 | TEST_ASSERT( cipher_finish( NULL, buf, &olen ) |
| 76 | == POLARSSL_ERR_CIPHER_BAD_INPUT_DATA ); |
| 77 | TEST_ASSERT( cipher_finish( &ctx, buf, &olen ) |
| 78 | == POLARSSL_ERR_CIPHER_BAD_INPUT_DATA ); |
| 79 | |
| 80 | #if defined(POLARSSL_CIPHER_MODE_AEAD) |
| 81 | TEST_ASSERT( cipher_write_tag( NULL, buf, olen ) |
| 82 | == POLARSSL_ERR_CIPHER_BAD_INPUT_DATA ); |
| 83 | TEST_ASSERT( cipher_write_tag( &ctx, buf, olen ) |
| 84 | == POLARSSL_ERR_CIPHER_BAD_INPUT_DATA ); |
| 85 | |
| 86 | TEST_ASSERT( cipher_check_tag( NULL, buf, olen ) |
| 87 | == POLARSSL_ERR_CIPHER_BAD_INPUT_DATA ); |
| 88 | TEST_ASSERT( cipher_check_tag( &ctx, buf, olen ) |
| 89 | == POLARSSL_ERR_CIPHER_BAD_INPUT_DATA ); |
| 90 | #endif |
| 91 | } |
| 92 | /* END_CASE */ |
| 93 | |
| 94 | /* BEGIN_CASE */ |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 95 | void enc_dec_buf( int cipher_id, char *cipher_string, int key_len, |
| 96 | int length_val, int pad_mode ) |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 97 | { |
Manuel Pégourié-Gonnard | 1af50a2 | 2013-09-05 10:30:32 +0200 | [diff] [blame] | 98 | size_t length = length_val, outlen, total_len, i; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 99 | unsigned char key[32]; |
| 100 | unsigned char iv[16]; |
Manuel Pégourié-Gonnard | 9241be7 | 2013-08-31 17:31:03 +0200 | [diff] [blame] | 101 | unsigned char ad[13]; |
| 102 | unsigned char tag[16]; |
Manuel Pégourié-Gonnard | 1af50a2 | 2013-09-05 10:30:32 +0200 | [diff] [blame] | 103 | unsigned char inbuf[64]; |
| 104 | unsigned char encbuf[64]; |
| 105 | unsigned char decbuf[64]; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 106 | |
| 107 | const cipher_info_t *cipher_info; |
| 108 | cipher_context_t ctx_dec; |
| 109 | cipher_context_t ctx_enc; |
| 110 | |
Manuel Pégourié-Gonnard | 1af50a2 | 2013-09-05 10:30:32 +0200 | [diff] [blame] | 111 | /* |
| 112 | * Prepare contexts |
| 113 | */ |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 114 | memset( &ctx_dec, 0, sizeof( ctx_dec ) ); |
| 115 | memset( &ctx_enc, 0, sizeof( ctx_enc ) ); |
Manuel Pégourié-Gonnard | 1af50a2 | 2013-09-05 10:30:32 +0200 | [diff] [blame] | 116 | |
| 117 | memset( key, 0x2a, sizeof( key ) ); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 118 | |
| 119 | /* Check and get info structures */ |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 120 | cipher_info = cipher_info_from_type( cipher_id ); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 121 | TEST_ASSERT( NULL != cipher_info ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 122 | TEST_ASSERT( cipher_info_from_string( cipher_string ) == cipher_info ); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 123 | |
| 124 | /* Initialise enc and dec contexts */ |
| 125 | TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); |
| 126 | TEST_ASSERT( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); |
Manuel Pégourié-Gonnard | 1af50a2 | 2013-09-05 10:30:32 +0200 | [diff] [blame] | 127 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 128 | TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, key_len, POLARSSL_DECRYPT ) ); |
| 129 | TEST_ASSERT( 0 == cipher_setkey( &ctx_enc, key, key_len, POLARSSL_ENCRYPT ) ); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 130 | |
Manuel Pégourié-Gonnard | 989ed38 | 2013-09-13 14:41:45 +0200 | [diff] [blame] | 131 | #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING) |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 132 | if( -1 != pad_mode ) |
Manuel Pégourié-Gonnard | 6c97899 | 2013-07-26 13:20:42 +0200 | [diff] [blame] | 133 | { |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 134 | TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx_dec, pad_mode ) ); |
| 135 | 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] | 136 | } |
Manuel Pégourié-Gonnard | 989ed38 | 2013-09-13 14:41:45 +0200 | [diff] [blame] | 137 | #else |
| 138 | (void) pad_mode; |
| 139 | #endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */ |
Manuel Pégourié-Gonnard | 6c97899 | 2013-07-26 13:20:42 +0200 | [diff] [blame] | 140 | |
Manuel Pégourié-Gonnard | 1af50a2 | 2013-09-05 10:30:32 +0200 | [diff] [blame] | 141 | /* |
| 142 | * Do a few encode/decode cycles |
| 143 | */ |
| 144 | for( i = 0; i < 3; i++ ) |
| 145 | { |
| 146 | memset( iv , 0x00 + i, sizeof( iv ) ); |
| 147 | memset( ad, 0x10 + i, sizeof( ad ) ); |
| 148 | memset( inbuf, 0x20 + i, sizeof( inbuf ) ); |
| 149 | |
| 150 | memset( encbuf, 0, sizeof( encbuf ) ); |
| 151 | memset( decbuf, 0, sizeof( decbuf ) ); |
| 152 | memset( tag, 0, sizeof( tag ) ); |
| 153 | |
| 154 | TEST_ASSERT( 0 == cipher_set_iv( &ctx_dec, iv, sizeof( iv ) ) ); |
| 155 | TEST_ASSERT( 0 == cipher_set_iv( &ctx_enc, iv, sizeof( iv ) ) ); |
Manuel Pégourié-Gonnard | 9c853b9 | 2013-09-03 13:04:44 +0200 | [diff] [blame] | 156 | |
Manuel Pégourié-Gonnard | 2adc40c | 2013-09-03 13:54:12 +0200 | [diff] [blame] | 157 | TEST_ASSERT( 0 == cipher_reset( &ctx_dec ) ); |
| 158 | TEST_ASSERT( 0 == cipher_reset( &ctx_enc ) ); |
| 159 | |
Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 160 | #if defined(POLARSSL_CIPHER_MODE_AEAD) |
Manuel Pégourié-Gonnard | 1af50a2 | 2013-09-05 10:30:32 +0200 | [diff] [blame] | 161 | TEST_ASSERT( 0 == cipher_update_ad( &ctx_dec, ad, sizeof( ad ) - i ) ); |
| 162 | TEST_ASSERT( 0 == cipher_update_ad( &ctx_enc, ad, sizeof( ad ) - i ) ); |
Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 163 | #endif /* POLARSSL_CIPHER_MODE_AEAD */ |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 164 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 165 | /* encode length number of bytes from inbuf */ |
| 166 | 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] | 167 | total_len = outlen; |
| 168 | |
| 169 | TEST_ASSERT( total_len == length || |
| 170 | ( total_len % cipher_get_block_size( &ctx_enc ) == 0 && |
| 171 | total_len < length && |
| 172 | total_len + cipher_get_block_size( &ctx_enc ) > length ) ); |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 173 | |
Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 174 | TEST_ASSERT( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); |
Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 175 | total_len += outlen; |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 176 | |
Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 177 | #if defined(POLARSSL_CIPHER_MODE_AEAD) |
Manuel Pégourié-Gonnard | 1af50a2 | 2013-09-05 10:30:32 +0200 | [diff] [blame] | 178 | TEST_ASSERT( 0 == cipher_write_tag( &ctx_enc, tag, sizeof( tag ) ) ); |
Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 179 | #endif /* POLARSSL_CIPHER_MODE_AEAD */ |
Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 180 | |
Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 181 | TEST_ASSERT( total_len == length || |
| 182 | ( total_len % cipher_get_block_size( &ctx_enc ) == 0 && |
| 183 | total_len > length && |
| 184 | total_len <= length + cipher_get_block_size( &ctx_enc ) ) ); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 185 | |
| 186 | /* decode the previously encoded string */ |
Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 187 | TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, total_len, decbuf, &outlen ) ); |
| 188 | total_len = outlen; |
| 189 | |
| 190 | TEST_ASSERT( total_len == length || |
| 191 | ( total_len % cipher_get_block_size( &ctx_dec ) == 0 && |
| 192 | total_len < length && |
| 193 | total_len + cipher_get_block_size( &ctx_dec ) >= length ) ); |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 194 | |
Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 195 | TEST_ASSERT( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); |
Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 196 | total_len += outlen; |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 197 | |
Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 198 | #if defined(POLARSSL_CIPHER_MODE_AEAD) |
Manuel Pégourié-Gonnard | 1af50a2 | 2013-09-05 10:30:32 +0200 | [diff] [blame] | 199 | TEST_ASSERT( 0 == cipher_check_tag( &ctx_dec, tag, sizeof( tag ) ) ); |
Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 200 | #endif /* POLARSSL_CIPHER_MODE_AEAD */ |
Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 201 | |
Manuel Pégourié-Gonnard | 1af50a2 | 2013-09-05 10:30:32 +0200 | [diff] [blame] | 202 | /* check result */ |
Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 203 | TEST_ASSERT( total_len == length ); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 204 | TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) ); |
Manuel Pégourié-Gonnard | 1af50a2 | 2013-09-05 10:30:32 +0200 | [diff] [blame] | 205 | } |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 206 | |
Manuel Pégourié-Gonnard | 1af50a2 | 2013-09-05 10:30:32 +0200 | [diff] [blame] | 207 | /* |
| 208 | * Done |
| 209 | */ |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 210 | TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) ); |
| 211 | TEST_ASSERT( 0 == cipher_free_ctx( &ctx_enc ) ); |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 212 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 213 | /* END_CASE */ |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 214 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 215 | /* BEGIN_CASE */ |
| 216 | void enc_fail( int cipher_id, int pad_mode, int key_len, |
| 217 | int length_val, int ret ) |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 218 | { |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 219 | size_t length = length_val; |
Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 220 | unsigned char key[32]; |
| 221 | unsigned char iv[16]; |
| 222 | |
| 223 | const cipher_info_t *cipher_info; |
| 224 | cipher_context_t ctx; |
| 225 | |
| 226 | unsigned char inbuf[64]; |
| 227 | unsigned char encbuf[64]; |
| 228 | |
| 229 | size_t outlen = 0; |
| 230 | |
| 231 | memset( key, 0, 32 ); |
| 232 | memset( iv , 0, 16 ); |
| 233 | |
| 234 | memset( &ctx, 0, sizeof( ctx ) ); |
| 235 | |
| 236 | memset( inbuf, 5, 64 ); |
| 237 | memset( encbuf, 0, 64 ); |
| 238 | |
| 239 | /* Check and get info structures */ |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 240 | cipher_info = cipher_info_from_type( cipher_id ); |
Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 241 | TEST_ASSERT( NULL != cipher_info ); |
| 242 | |
| 243 | /* Initialise context */ |
| 244 | TEST_ASSERT( 0 == cipher_init_ctx( &ctx, cipher_info ) ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 245 | TEST_ASSERT( 0 == cipher_setkey( &ctx, key, key_len, POLARSSL_ENCRYPT ) ); |
Manuel Pégourié-Gonnard | 989ed38 | 2013-09-13 14:41:45 +0200 | [diff] [blame] | 246 | #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING) |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 247 | TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, pad_mode ) ); |
Manuel Pégourié-Gonnard | 989ed38 | 2013-09-13 14:41:45 +0200 | [diff] [blame] | 248 | #else |
| 249 | (void) pad_mode; |
| 250 | #endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */ |
Manuel Pégourié-Gonnard | 9c853b9 | 2013-09-03 13:04:44 +0200 | [diff] [blame] | 251 | TEST_ASSERT( 0 == cipher_set_iv( &ctx, iv, 16 ) ); |
Manuel Pégourié-Gonnard | 2adc40c | 2013-09-03 13:54:12 +0200 | [diff] [blame] | 252 | TEST_ASSERT( 0 == cipher_reset( &ctx ) ); |
Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 253 | #if defined(POLARSSL_CIPHER_MODE_AEAD) |
Manuel Pégourié-Gonnard | 2adc40c | 2013-09-03 13:54:12 +0200 | [diff] [blame] | 254 | TEST_ASSERT( 0 == cipher_update_ad( &ctx, NULL, 0 ) ); |
Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 255 | #endif /* POLARSSL_CIPHER_MODE_AEAD */ |
Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 256 | |
| 257 | /* encode length number of bytes from inbuf */ |
| 258 | TEST_ASSERT( 0 == cipher_update( &ctx, inbuf, length, encbuf, &outlen ) ); |
Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 259 | TEST_ASSERT( ret == cipher_finish( &ctx, encbuf + outlen, &outlen ) ); |
Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 260 | |
| 261 | /* done */ |
| 262 | TEST_ASSERT( 0 == cipher_free_ctx( &ctx ) ); |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 263 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 264 | /* END_CASE */ |
Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 265 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 266 | /* BEGIN_CASE */ |
| 267 | void dec_empty_buf() |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 268 | { |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 269 | unsigned char key[32]; |
| 270 | unsigned char iv[16]; |
| 271 | |
| 272 | cipher_context_t ctx_dec; |
| 273 | const cipher_info_t *cipher_info; |
| 274 | |
| 275 | unsigned char encbuf[64]; |
| 276 | unsigned char decbuf[64]; |
| 277 | |
Paul Bakker | f4a3f30 | 2011-04-24 15:53:29 +0000 | [diff] [blame] | 278 | size_t outlen = 0; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 279 | |
| 280 | memset( key, 0, 32 ); |
| 281 | memset( iv , 0, 16 ); |
| 282 | |
| 283 | memset( &ctx_dec, 0, sizeof( ctx_dec ) ); |
| 284 | |
| 285 | memset( encbuf, 0, 64 ); |
| 286 | memset( decbuf, 0, 64 ); |
| 287 | |
Manuel Pégourié-Gonnard | 9c853b9 | 2013-09-03 13:04:44 +0200 | [diff] [blame] | 288 | /* Initialise context */ |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 289 | cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CBC ); |
| 290 | TEST_ASSERT( NULL != cipher_info); |
| 291 | |
| 292 | TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); |
| 293 | |
| 294 | TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); |
| 295 | |
Manuel Pégourié-Gonnard | 9c853b9 | 2013-09-03 13:04:44 +0200 | [diff] [blame] | 296 | TEST_ASSERT( 0 == cipher_set_iv( &ctx_dec, iv, 16 ) ); |
| 297 | |
Manuel Pégourié-Gonnard | 2adc40c | 2013-09-03 13:54:12 +0200 | [diff] [blame] | 298 | TEST_ASSERT( 0 == cipher_reset( &ctx_dec ) ); |
| 299 | |
Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 300 | #if defined(POLARSSL_CIPHER_MODE_AEAD) |
Manuel Pégourié-Gonnard | 2adc40c | 2013-09-03 13:54:12 +0200 | [diff] [blame] | 301 | TEST_ASSERT( 0 == cipher_update_ad( &ctx_dec, NULL, 0 ) ); |
Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 302 | #endif /* POLARSSL_CIPHER_MODE_AEAD */ |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 303 | |
| 304 | /* decode 0-byte string */ |
| 305 | TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, 0, decbuf, &outlen ) ); |
| 306 | TEST_ASSERT( 0 == outlen ); |
Manuel Pégourié-Gonnard | 9241be7 | 2013-08-31 17:31:03 +0200 | [diff] [blame] | 307 | TEST_ASSERT( POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED == cipher_finish( |
Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 308 | &ctx_dec, decbuf + outlen, &outlen ) ); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 309 | TEST_ASSERT( 0 == outlen ); |
| 310 | |
| 311 | TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) ); |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 312 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 313 | /* END_CASE */ |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 314 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 315 | /* BEGIN_CASE */ |
| 316 | void enc_dec_buf_multipart( int cipher_id, int key_len, int first_length_val, |
| 317 | int second_length_val ) |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 318 | { |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 319 | size_t first_length = first_length_val; |
| 320 | size_t second_length = second_length_val; |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 321 | size_t length = first_length + second_length; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 322 | unsigned char key[32]; |
| 323 | unsigned char iv[16]; |
| 324 | |
| 325 | cipher_context_t ctx_dec; |
| 326 | cipher_context_t ctx_enc; |
| 327 | const cipher_info_t *cipher_info; |
| 328 | |
| 329 | unsigned char inbuf[64]; |
| 330 | unsigned char encbuf[64]; |
| 331 | unsigned char decbuf[64]; |
| 332 | |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 333 | size_t outlen = 0; |
| 334 | size_t totaloutlen = 0; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 335 | |
| 336 | memset( key, 0, 32 ); |
| 337 | memset( iv , 0, 16 ); |
| 338 | |
| 339 | memset( &ctx_dec, 0, sizeof( ctx_dec ) ); |
| 340 | memset( &ctx_enc, 0, sizeof( ctx_enc ) ); |
| 341 | |
| 342 | memset( inbuf, 5, 64 ); |
| 343 | memset( encbuf, 0, 64 ); |
| 344 | memset( decbuf, 0, 64 ); |
| 345 | |
| 346 | /* Initialise enc and dec contexts */ |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 347 | cipher_info = cipher_info_from_type( cipher_id ); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 348 | TEST_ASSERT( NULL != cipher_info); |
| 349 | |
| 350 | TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); |
| 351 | TEST_ASSERT( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); |
| 352 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 353 | TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, key_len, POLARSSL_DECRYPT ) ); |
| 354 | TEST_ASSERT( 0 == cipher_setkey( &ctx_enc, key, key_len, POLARSSL_ENCRYPT ) ); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 355 | |
Manuel Pégourié-Gonnard | 9c853b9 | 2013-09-03 13:04:44 +0200 | [diff] [blame] | 356 | TEST_ASSERT( 0 == cipher_set_iv( &ctx_dec, iv, 16 ) ); |
| 357 | TEST_ASSERT( 0 == cipher_set_iv( &ctx_enc, iv, 16 ) ); |
| 358 | |
Manuel Pégourié-Gonnard | 2adc40c | 2013-09-03 13:54:12 +0200 | [diff] [blame] | 359 | TEST_ASSERT( 0 == cipher_reset( &ctx_dec ) ); |
| 360 | TEST_ASSERT( 0 == cipher_reset( &ctx_enc ) ); |
| 361 | |
Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 362 | #if defined(POLARSSL_CIPHER_MODE_AEAD) |
Manuel Pégourié-Gonnard | 2adc40c | 2013-09-03 13:54:12 +0200 | [diff] [blame] | 363 | TEST_ASSERT( 0 == cipher_update_ad( &ctx_dec, NULL, 0 ) ); |
| 364 | TEST_ASSERT( 0 == cipher_update_ad( &ctx_enc, NULL, 0 ) ); |
Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 365 | #endif /* POLARSSL_CIPHER_MODE_AEAD */ |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 366 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 367 | /* encode length number of bytes from inbuf */ |
| 368 | TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); |
| 369 | totaloutlen = outlen; |
| 370 | TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); |
| 371 | totaloutlen += outlen; |
Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 372 | TEST_ASSERT( totaloutlen == length || |
| 373 | ( totaloutlen % cipher_get_block_size( &ctx_enc ) == 0 && |
| 374 | totaloutlen < length && |
| 375 | totaloutlen + cipher_get_block_size( &ctx_enc ) > length ) ); |
| 376 | |
Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 377 | TEST_ASSERT( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 378 | totaloutlen += outlen; |
Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 379 | TEST_ASSERT( totaloutlen == length || |
| 380 | ( totaloutlen % cipher_get_block_size( &ctx_enc ) == 0 && |
| 381 | totaloutlen > length && |
| 382 | totaloutlen <= length + cipher_get_block_size( &ctx_enc ) ) ); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 383 | |
| 384 | /* decode the previously encoded string */ |
Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 385 | TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, totaloutlen, decbuf, &outlen ) ); |
| 386 | totaloutlen = outlen; |
| 387 | |
| 388 | TEST_ASSERT( totaloutlen == length || |
| 389 | ( totaloutlen % cipher_get_block_size( &ctx_dec ) == 0 && |
| 390 | totaloutlen < length && |
Manuel Pégourié-Gonnard | 07f8fa5 | 2013-08-30 18:34:08 +0200 | [diff] [blame] | 391 | totaloutlen + cipher_get_block_size( &ctx_dec ) >= length ) ); |
Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 392 | |
Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 393 | TEST_ASSERT( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); |
Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 394 | totaloutlen += outlen; |
| 395 | |
| 396 | TEST_ASSERT( totaloutlen == length ); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 397 | |
| 398 | TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) ); |
| 399 | |
| 400 | TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) ); |
| 401 | TEST_ASSERT( 0 == cipher_free_ctx( &ctx_enc ) ); |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 402 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 403 | /* END_CASE */ |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 404 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 405 | /* BEGIN_CASE */ |
Manuel Pégourié-Gonnard | 8eccab5 | 2013-09-03 18:31:25 +0200 | [diff] [blame] | 406 | void decrypt_test_vec( int cipher_id, int pad_mode, |
| 407 | char *hex_key, char *hex_iv, |
| 408 | char *hex_cipher, char *hex_clear, |
| 409 | char *hex_ad, char *hex_tag, |
| 410 | int finish_result, int tag_result ) |
| 411 | { |
Manuel Pégourié-Gonnard | f7ce67f | 2013-09-03 20:17:35 +0200 | [diff] [blame] | 412 | unsigned char key[50]; |
| 413 | unsigned char iv[50]; |
| 414 | unsigned char cipher[200]; |
| 415 | unsigned char clear[200]; |
| 416 | unsigned char ad[200]; |
| 417 | unsigned char tag[20]; |
Manuel Pégourié-Gonnard | a7496f0 | 2013-09-20 11:29:59 +0200 | [diff] [blame] | 418 | size_t key_len, iv_len, cipher_len, clear_len; |
| 419 | #if defined(POLARSSL_CIPHER_MODE_AEAD) |
| 420 | size_t ad_len, tag_len; |
| 421 | #endif |
Manuel Pégourié-Gonnard | 8eccab5 | 2013-09-03 18:31:25 +0200 | [diff] [blame] | 422 | cipher_context_t ctx; |
Manuel Pégourié-Gonnard | f7ce67f | 2013-09-03 20:17:35 +0200 | [diff] [blame] | 423 | unsigned char output[200]; |
Manuel Pégourié-Gonnard | 8eccab5 | 2013-09-03 18:31:25 +0200 | [diff] [blame] | 424 | size_t outlen, total_len; |
| 425 | |
| 426 | memset( key, 0x00, sizeof( key ) ); |
| 427 | memset( iv, 0x00, sizeof( iv ) ); |
| 428 | memset( cipher, 0x00, sizeof( cipher ) ); |
| 429 | memset( clear, 0x00, sizeof( clear ) ); |
| 430 | memset( ad, 0x00, sizeof( ad ) ); |
| 431 | memset( tag, 0x00, sizeof( tag ) ); |
| 432 | memset( output, 0x00, sizeof( output ) ); |
| 433 | |
| 434 | key_len = unhexify( key, hex_key ); |
| 435 | iv_len = unhexify( iv, hex_iv ); |
| 436 | cipher_len = unhexify( cipher, hex_cipher ); |
| 437 | clear_len = unhexify( clear, hex_clear ); |
Manuel Pégourié-Gonnard | a7496f0 | 2013-09-20 11:29:59 +0200 | [diff] [blame] | 438 | #if defined(POLARSSL_CIPHER_MODE_AEAD) |
Manuel Pégourié-Gonnard | 8eccab5 | 2013-09-03 18:31:25 +0200 | [diff] [blame] | 439 | ad_len = unhexify( ad, hex_ad ); |
| 440 | tag_len = unhexify( tag, hex_tag ); |
Manuel Pégourié-Gonnard | a7496f0 | 2013-09-20 11:29:59 +0200 | [diff] [blame] | 441 | #else |
| 442 | ((void) hex_ad); |
| 443 | ((void) hex_tag); |
| 444 | #endif |
Manuel Pégourié-Gonnard | 8eccab5 | 2013-09-03 18:31:25 +0200 | [diff] [blame] | 445 | |
| 446 | /* Prepare context */ |
| 447 | TEST_ASSERT( 0 == cipher_init_ctx( &ctx, |
| 448 | cipher_info_from_type( cipher_id ) ) ); |
| 449 | TEST_ASSERT( 0 == cipher_setkey( &ctx, key, 8 * key_len, POLARSSL_DECRYPT ) ); |
Manuel Pégourié-Gonnard | 989ed38 | 2013-09-13 14:41:45 +0200 | [diff] [blame] | 450 | #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING) |
Manuel Pégourié-Gonnard | 8eccab5 | 2013-09-03 18:31:25 +0200 | [diff] [blame] | 451 | if( pad_mode != -1 ) |
| 452 | TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, pad_mode ) ); |
Manuel Pégourié-Gonnard | 989ed38 | 2013-09-13 14:41:45 +0200 | [diff] [blame] | 453 | #else |
| 454 | (void) pad_mode; |
| 455 | #endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */ |
Manuel Pégourié-Gonnard | 8eccab5 | 2013-09-03 18:31:25 +0200 | [diff] [blame] | 456 | TEST_ASSERT( 0 == cipher_set_iv( &ctx, iv, iv_len ) ); |
| 457 | TEST_ASSERT( 0 == cipher_reset( &ctx ) ); |
| 458 | #if defined(POLARSSL_CIPHER_MODE_AEAD) |
| 459 | TEST_ASSERT( 0 == cipher_update_ad( &ctx, ad, ad_len ) ); |
| 460 | #endif /* POLARSSL_CIPHER_MODE_AEAD */ |
| 461 | |
| 462 | /* decode buffer and check tag */ |
| 463 | total_len = 0; |
| 464 | TEST_ASSERT( 0 == cipher_update( &ctx, cipher, cipher_len, output, &outlen ) ); |
| 465 | total_len += outlen; |
| 466 | TEST_ASSERT( finish_result == cipher_finish( &ctx, output + outlen, |
| 467 | &outlen ) ); |
| 468 | total_len += outlen; |
| 469 | #if defined(POLARSSL_CIPHER_MODE_AEAD) |
| 470 | TEST_ASSERT( tag_result == cipher_check_tag( &ctx, tag, tag_len ) ); |
| 471 | #endif /* POLARSSL_CIPHER_MODE_AEAD */ |
| 472 | |
| 473 | /* check plaintext only if everything went fine */ |
| 474 | if( 0 == finish_result && 0 == tag_result ) |
| 475 | { |
| 476 | TEST_ASSERT( total_len == clear_len ); |
| 477 | TEST_ASSERT( 0 == memcmp( output, clear, clear_len ) ); |
| 478 | } |
| 479 | |
| 480 | cipher_free_ctx( &ctx ); |
| 481 | } |
| 482 | /* END_CASE */ |
| 483 | |
Manuel Pégourié-Gonnard | 542eac5 | 2014-05-15 16:03:07 +0200 | [diff] [blame] | 484 | /* BEGIN_CASE depends_on:POLARSSL_CIPHER_MODE_AEAD */ |
| 485 | void auth_crypt_tv( int cipher_id, char *hex_key, char *hex_iv, |
| 486 | char *hex_ad, char *hex_cipher, |
| 487 | char *hex_tag, char *hex_clear ) |
| 488 | { |
| 489 | int ret; |
| 490 | unsigned char key[50]; |
| 491 | unsigned char iv[50]; |
| 492 | unsigned char cipher[200]; |
| 493 | unsigned char clear[200]; |
| 494 | unsigned char ad[200]; |
| 495 | unsigned char tag[20]; |
| 496 | unsigned char my_tag[20]; |
| 497 | size_t key_len, iv_len, cipher_len, clear_len, ad_len, tag_len; |
| 498 | cipher_context_t ctx; |
| 499 | unsigned char output[200]; |
| 500 | size_t outlen; |
| 501 | |
| 502 | memset( key, 0x00, sizeof( key ) ); |
| 503 | memset( iv, 0x00, sizeof( iv ) ); |
| 504 | memset( cipher, 0x00, sizeof( cipher ) ); |
| 505 | memset( clear, 0x00, sizeof( clear ) ); |
| 506 | memset( ad, 0x00, sizeof( ad ) ); |
| 507 | memset( tag, 0x00, sizeof( tag ) ); |
| 508 | memset( my_tag, 0xFF, sizeof( my_tag ) ); |
| 509 | memset( output, 0xFF, sizeof( output ) ); |
| 510 | |
| 511 | key_len = unhexify( key, hex_key ); |
| 512 | iv_len = unhexify( iv, hex_iv ); |
| 513 | cipher_len = unhexify( cipher, hex_cipher ); |
| 514 | ad_len = unhexify( ad, hex_ad ); |
| 515 | tag_len = unhexify( tag, hex_tag ); |
| 516 | |
| 517 | /* Prepare context */ |
| 518 | TEST_ASSERT( 0 == cipher_init_ctx( &ctx, |
| 519 | cipher_info_from_type( cipher_id ) ) ); |
| 520 | TEST_ASSERT( 0 == cipher_setkey( &ctx, key, 8 * key_len, POLARSSL_DECRYPT ) ); |
| 521 | |
| 522 | /* decode buffer and check tag */ |
| 523 | ret = cipher_auth_decrypt( &ctx, iv, iv_len, ad, ad_len, |
| 524 | cipher, cipher_len, output, &outlen, |
| 525 | tag, tag_len ); |
| 526 | |
| 527 | /* make sure we didn't overwrite */ |
| 528 | TEST_ASSERT( output[outlen + 0] == 0xFF ); |
| 529 | TEST_ASSERT( output[outlen + 1] == 0xFF ); |
| 530 | |
| 531 | /* make sure the message is rejected if it should be */ |
| 532 | if( strcmp( hex_clear, "FAIL" ) == 0 ) |
| 533 | { |
| 534 | TEST_ASSERT( ret == POLARSSL_ERR_CIPHER_AUTH_FAILED ); |
| 535 | goto cleanup; |
| 536 | } |
| 537 | |
| 538 | /* otherwise, make sure it was decrypted properly */ |
| 539 | TEST_ASSERT( ret == 0 ); |
| 540 | |
| 541 | clear_len = unhexify( clear, hex_clear ); |
| 542 | TEST_ASSERT( outlen == clear_len ); |
| 543 | TEST_ASSERT( memcmp( output, clear, clear_len ) == 0 ); |
| 544 | |
| 545 | /* then encrypt the clear and make sure we get the same ciphertext and tag */ |
| 546 | memset( output, 0xFF, sizeof( output ) ); |
| 547 | outlen = 0; |
| 548 | |
| 549 | ret = cipher_auth_encrypt( &ctx, iv, iv_len, ad, ad_len, |
| 550 | clear, clear_len, output, &outlen, |
| 551 | my_tag, tag_len ); |
| 552 | TEST_ASSERT( ret == 0 ); |
| 553 | |
| 554 | TEST_ASSERT( outlen == clear_len ); |
| 555 | TEST_ASSERT( memcmp( output, cipher, clear_len ) == 0 ); |
| 556 | TEST_ASSERT( memcmp( my_tag, tag, tag_len ) == 0 ); |
| 557 | |
| 558 | /* make sure we didn't overwrite */ |
| 559 | TEST_ASSERT( output[outlen + 0] == 0xFF ); |
| 560 | TEST_ASSERT( output[outlen + 1] == 0xFF ); |
| 561 | TEST_ASSERT( my_tag[tag_len + 0] == 0xFF ); |
| 562 | TEST_ASSERT( my_tag[tag_len + 1] == 0xFF ); |
| 563 | |
| 564 | |
| 565 | cleanup: |
| 566 | cipher_free_ctx( &ctx ); |
| 567 | } |
| 568 | /* END_CASE */ |
| 569 | |
Manuel Pégourié-Gonnard | 8eccab5 | 2013-09-03 18:31:25 +0200 | [diff] [blame] | 570 | /* BEGIN_CASE */ |
Paul Bakker | 5e0efa7 | 2013-09-08 23:04:04 +0200 | [diff] [blame] | 571 | void test_vec_ecb( int cipher_id, int operation, char *hex_key, |
| 572 | char *hex_input, char *hex_result, |
| 573 | int finish_result ) |
| 574 | { |
| 575 | unsigned char key[50]; |
| 576 | unsigned char input[16]; |
| 577 | unsigned char result[16]; |
| 578 | size_t key_len; |
| 579 | cipher_context_t ctx; |
| 580 | unsigned char output[32]; |
| 581 | size_t outlen; |
| 582 | |
| 583 | memset( key, 0x00, sizeof( key ) ); |
| 584 | memset( input, 0x00, sizeof( input ) ); |
| 585 | memset( result, 0x00, sizeof( result ) ); |
| 586 | memset( output, 0x00, sizeof( output ) ); |
| 587 | |
| 588 | /* Prepare context */ |
| 589 | TEST_ASSERT( 0 == cipher_init_ctx( &ctx, |
| 590 | cipher_info_from_type( cipher_id ) ) ); |
| 591 | |
| 592 | key_len = unhexify( key, hex_key ); |
| 593 | TEST_ASSERT( unhexify( input, hex_input ) == |
| 594 | (int) cipher_get_block_size( &ctx ) ); |
| 595 | TEST_ASSERT( unhexify( result, hex_result ) == |
| 596 | (int) cipher_get_block_size( &ctx ) ); |
| 597 | |
| 598 | TEST_ASSERT( 0 == cipher_setkey( &ctx, key, 8 * key_len, operation ) ); |
| 599 | |
| 600 | TEST_ASSERT( 0 == cipher_update( &ctx, input, |
| 601 | cipher_get_block_size( &ctx ), |
| 602 | output, &outlen ) ); |
| 603 | TEST_ASSERT( outlen == cipher_get_block_size( &ctx ) ); |
| 604 | TEST_ASSERT( finish_result == cipher_finish( &ctx, output + outlen, |
| 605 | &outlen ) ); |
| 606 | TEST_ASSERT( 0 == outlen ); |
| 607 | |
| 608 | /* check plaintext only if everything went fine */ |
| 609 | if( 0 == finish_result ) |
| 610 | TEST_ASSERT( 0 == memcmp( output, result, |
| 611 | cipher_get_block_size( &ctx ) ) ); |
| 612 | |
| 613 | cipher_free_ctx( &ctx ); |
| 614 | } |
| 615 | /* END_CASE */ |
| 616 | |
Manuel Pégourié-Gonnard | 989ed38 | 2013-09-13 14:41:45 +0200 | [diff] [blame] | 617 | /* BEGIN_CASE depends_on:POLARSSL_CIPHER_MODE_WITH_PADDING */ |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 618 | void set_padding( int cipher_id, int pad_mode, int ret ) |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 619 | { |
Manuel Pégourié-Gonnard | d5fdcaf | 2013-07-24 18:05:00 +0200 | [diff] [blame] | 620 | const cipher_info_t *cipher_info; |
| 621 | cipher_context_t ctx; |
| 622 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 623 | cipher_info = cipher_info_from_type( cipher_id ); |
Manuel Pégourié-Gonnard | d5fdcaf | 2013-07-24 18:05:00 +0200 | [diff] [blame] | 624 | TEST_ASSERT( NULL != cipher_info ); |
| 625 | TEST_ASSERT( 0 == cipher_init_ctx( &ctx, cipher_info ) ); |
| 626 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 627 | TEST_ASSERT( ret == cipher_set_padding_mode( &ctx, pad_mode ) ); |
Manuel Pégourié-Gonnard | d5fdcaf | 2013-07-24 18:05:00 +0200 | [diff] [blame] | 628 | |
| 629 | TEST_ASSERT( 0 == cipher_free_ctx( &ctx ) ); |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 630 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 631 | /* END_CASE */ |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 632 | |
Manuel Pégourié-Gonnard | 989ed38 | 2013-09-13 14:41:45 +0200 | [diff] [blame] | 633 | /* BEGIN_CASE depends_on:POLARSSL_CIPHER_MODE_CBC */ |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 634 | 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] | 635 | { |
Manuel Pégourié-Gonnard | a640849 | 2013-07-26 10:55:02 +0200 | [diff] [blame] | 636 | cipher_info_t cipher_info; |
| 637 | cipher_context_t ctx; |
| 638 | unsigned char input[16]; |
| 639 | size_t ilen, dlen; |
| 640 | |
| 641 | /* build a fake context just for getting access to get_padding */ |
| 642 | memset( &ctx, 0, sizeof( ctx ) ); |
| 643 | cipher_info.mode = POLARSSL_MODE_CBC; |
| 644 | ctx.cipher_info = &cipher_info; |
| 645 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 646 | TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, pad_mode ) ); |
Manuel Pégourié-Gonnard | a640849 | 2013-07-26 10:55:02 +0200 | [diff] [blame] | 647 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 648 | ilen = unhexify( input, input_str ); |
Manuel Pégourié-Gonnard | a640849 | 2013-07-26 10:55:02 +0200 | [diff] [blame] | 649 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 650 | TEST_ASSERT( ret == ctx.get_padding( input, ilen, &dlen ) ); |
| 651 | if( 0 == ret ) |
| 652 | TEST_ASSERT( dlen == (size_t) dlen_check ); |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 653 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 654 | /* END_CASE */ |
Manuel Pégourié-Gonnard | a640849 | 2013-07-26 10:55:02 +0200 | [diff] [blame] | 655 | |
Manuel Pégourié-Gonnard | 2014016 | 2013-10-10 12:48:03 +0200 | [diff] [blame] | 656 | /* BEGIN_CASE depends_on:POLARSSL_SELF_TEST */ |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 657 | void cipher_selftest() |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 658 | { |
| 659 | TEST_ASSERT( cipher_self_test( 0 ) == 0 ); |
| 660 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 661 | /* END_CASE */ |