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 */ |
| 15 | void enc_dec_buf( int cipher_id, char *cipher_string, int key_len, |
| 16 | int length_val, int pad_mode ) |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 17 | { |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 18 | size_t length = length_val; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 19 | unsigned char key[32]; |
| 20 | unsigned char iv[16]; |
Manuel Pégourié-Gonnard | 9241be7 | 2013-08-31 17:31:03 +0200 | [diff] [blame] | 21 | unsigned char ad[13]; |
| 22 | unsigned char tag[16]; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 23 | |
| 24 | const cipher_info_t *cipher_info; |
| 25 | cipher_context_t ctx_dec; |
| 26 | cipher_context_t ctx_enc; |
| 27 | |
| 28 | unsigned char inbuf[64]; |
| 29 | unsigned char encbuf[64]; |
| 30 | unsigned char decbuf[64]; |
| 31 | |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 32 | size_t outlen = 0; |
Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 33 | size_t total_len = 0; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 34 | |
| 35 | memset( key, 0, 32 ); |
| 36 | memset( iv , 0, 16 ); |
| 37 | |
| 38 | memset( &ctx_dec, 0, sizeof( ctx_dec ) ); |
| 39 | memset( &ctx_enc, 0, sizeof( ctx_enc ) ); |
| 40 | |
| 41 | memset( inbuf, 5, 64 ); |
| 42 | memset( encbuf, 0, 64 ); |
| 43 | memset( decbuf, 0, 64 ); |
Manuel Pégourié-Gonnard | 9241be7 | 2013-08-31 17:31:03 +0200 | [diff] [blame] | 44 | memset( tag, 0, 16 ); |
| 45 | memset( ad, 0x2a, 13 ); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 46 | |
| 47 | /* Check and get info structures */ |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 48 | cipher_info = cipher_info_from_type( cipher_id ); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 49 | TEST_ASSERT( NULL != cipher_info ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 50 | TEST_ASSERT( cipher_info_from_string( cipher_string ) == cipher_info ); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 51 | |
| 52 | /* Initialise enc and dec contexts */ |
| 53 | TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); |
| 54 | TEST_ASSERT( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); |
| 55 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 56 | TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, key_len, POLARSSL_DECRYPT ) ); |
| 57 | TEST_ASSERT( 0 == cipher_setkey( &ctx_enc, key, key_len, POLARSSL_ENCRYPT ) ); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 58 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 59 | if( -1 != pad_mode ) |
Manuel Pégourié-Gonnard | 6c97899 | 2013-07-26 13:20:42 +0200 | [diff] [blame] | 60 | { |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 61 | TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx_dec, pad_mode ) ); |
| 62 | 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] | 63 | } |
| 64 | |
Manuel Pégourié-Gonnard | 9c853b9 | 2013-09-03 13:04:44 +0200 | [diff] [blame] | 65 | TEST_ASSERT( 0 == cipher_set_iv( &ctx_dec, iv, 16 ) ); |
| 66 | TEST_ASSERT( 0 == cipher_set_iv( &ctx_enc, iv, 16 ) ); |
| 67 | |
Manuel Pégourié-Gonnard | 2adc40c | 2013-09-03 13:54:12 +0200 | [diff] [blame] | 68 | TEST_ASSERT( 0 == cipher_reset( &ctx_dec ) ); |
| 69 | TEST_ASSERT( 0 == cipher_reset( &ctx_enc ) ); |
| 70 | |
Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 71 | #if defined(POLARSSL_CIPHER_MODE_AEAD) |
Manuel Pégourié-Gonnard | 2adc40c | 2013-09-03 13:54:12 +0200 | [diff] [blame] | 72 | TEST_ASSERT( 0 == cipher_update_ad( &ctx_dec, ad, 13 ) ); |
| 73 | TEST_ASSERT( 0 == cipher_update_ad( &ctx_enc, ad, 13 ) ); |
Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 74 | #endif /* POLARSSL_CIPHER_MODE_AEAD */ |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 75 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 76 | /* encode length number of bytes from inbuf */ |
| 77 | 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] | 78 | total_len = outlen; |
| 79 | |
| 80 | TEST_ASSERT( total_len == length || |
| 81 | ( total_len % cipher_get_block_size( &ctx_enc ) == 0 && |
| 82 | total_len < length && |
| 83 | total_len + cipher_get_block_size( &ctx_enc ) > length ) ); |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 84 | |
Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 85 | TEST_ASSERT( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); |
Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 86 | total_len += outlen; |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 87 | |
Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 88 | #if defined(POLARSSL_CIPHER_MODE_AEAD) |
Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 89 | TEST_ASSERT( 0 == cipher_write_tag( &ctx_enc, tag, 16 ) ); |
Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 90 | #endif /* POLARSSL_CIPHER_MODE_AEAD */ |
Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 91 | |
Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 92 | TEST_ASSERT( total_len == length || |
| 93 | ( total_len % cipher_get_block_size( &ctx_enc ) == 0 && |
| 94 | total_len > length && |
| 95 | total_len <= length + cipher_get_block_size( &ctx_enc ) ) ); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 96 | |
| 97 | /* decode the previously encoded string */ |
Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 98 | TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, total_len, decbuf, &outlen ) ); |
| 99 | total_len = outlen; |
| 100 | |
| 101 | TEST_ASSERT( total_len == length || |
| 102 | ( total_len % cipher_get_block_size( &ctx_dec ) == 0 && |
| 103 | total_len < length && |
| 104 | total_len + cipher_get_block_size( &ctx_dec ) >= length ) ); |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 105 | |
Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 106 | TEST_ASSERT( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); |
Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 107 | total_len += outlen; |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 108 | |
Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 109 | #if defined(POLARSSL_CIPHER_MODE_AEAD) |
Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 110 | TEST_ASSERT( 0 == cipher_check_tag( &ctx_dec, tag, 16 ) ); |
Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 111 | #endif /* POLARSSL_CIPHER_MODE_AEAD */ |
Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 112 | |
Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 113 | TEST_ASSERT( total_len == length ); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 114 | |
| 115 | TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) ); |
| 116 | |
| 117 | TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) ); |
| 118 | TEST_ASSERT( 0 == cipher_free_ctx( &ctx_enc ) ); |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 119 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 120 | /* END_CASE */ |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 121 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 122 | /* BEGIN_CASE */ |
| 123 | void enc_fail( int cipher_id, int pad_mode, int key_len, |
| 124 | int length_val, int ret ) |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 125 | { |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 126 | size_t length = length_val; |
Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 127 | unsigned char key[32]; |
| 128 | unsigned char iv[16]; |
| 129 | |
| 130 | const cipher_info_t *cipher_info; |
| 131 | cipher_context_t ctx; |
| 132 | |
| 133 | unsigned char inbuf[64]; |
| 134 | unsigned char encbuf[64]; |
| 135 | |
| 136 | size_t outlen = 0; |
| 137 | |
| 138 | memset( key, 0, 32 ); |
| 139 | memset( iv , 0, 16 ); |
| 140 | |
| 141 | memset( &ctx, 0, sizeof( ctx ) ); |
| 142 | |
| 143 | memset( inbuf, 5, 64 ); |
| 144 | memset( encbuf, 0, 64 ); |
| 145 | |
| 146 | /* Check and get info structures */ |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 147 | cipher_info = cipher_info_from_type( cipher_id ); |
Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 148 | TEST_ASSERT( NULL != cipher_info ); |
| 149 | |
| 150 | /* Initialise context */ |
| 151 | TEST_ASSERT( 0 == cipher_init_ctx( &ctx, cipher_info ) ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 152 | TEST_ASSERT( 0 == cipher_setkey( &ctx, key, key_len, POLARSSL_ENCRYPT ) ); |
| 153 | TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, pad_mode ) ); |
Manuel Pégourié-Gonnard | 9c853b9 | 2013-09-03 13:04:44 +0200 | [diff] [blame] | 154 | TEST_ASSERT( 0 == cipher_set_iv( &ctx, iv, 16 ) ); |
Manuel Pégourié-Gonnard | 2adc40c | 2013-09-03 13:54:12 +0200 | [diff] [blame] | 155 | TEST_ASSERT( 0 == cipher_reset( &ctx ) ); |
Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 156 | #if defined(POLARSSL_CIPHER_MODE_AEAD) |
Manuel Pégourié-Gonnard | 2adc40c | 2013-09-03 13:54:12 +0200 | [diff] [blame] | 157 | TEST_ASSERT( 0 == cipher_update_ad( &ctx, NULL, 0 ) ); |
Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 158 | #endif /* POLARSSL_CIPHER_MODE_AEAD */ |
Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 159 | |
| 160 | /* encode length number of bytes from inbuf */ |
| 161 | TEST_ASSERT( 0 == cipher_update( &ctx, inbuf, length, encbuf, &outlen ) ); |
Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 162 | TEST_ASSERT( ret == cipher_finish( &ctx, encbuf + outlen, &outlen ) ); |
Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 163 | |
| 164 | /* done */ |
| 165 | TEST_ASSERT( 0 == cipher_free_ctx( &ctx ) ); |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 166 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 167 | /* END_CASE */ |
Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 168 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 169 | /* BEGIN_CASE */ |
| 170 | void dec_empty_buf() |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 171 | { |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 172 | unsigned char key[32]; |
| 173 | unsigned char iv[16]; |
| 174 | |
| 175 | cipher_context_t ctx_dec; |
| 176 | const cipher_info_t *cipher_info; |
| 177 | |
| 178 | unsigned char encbuf[64]; |
| 179 | unsigned char decbuf[64]; |
| 180 | |
Paul Bakker | f4a3f30 | 2011-04-24 15:53:29 +0000 | [diff] [blame] | 181 | size_t outlen = 0; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 182 | |
| 183 | memset( key, 0, 32 ); |
| 184 | memset( iv , 0, 16 ); |
| 185 | |
| 186 | memset( &ctx_dec, 0, sizeof( ctx_dec ) ); |
| 187 | |
| 188 | memset( encbuf, 0, 64 ); |
| 189 | memset( decbuf, 0, 64 ); |
| 190 | |
Manuel Pégourié-Gonnard | 9c853b9 | 2013-09-03 13:04:44 +0200 | [diff] [blame] | 191 | /* Initialise context */ |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 192 | cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CBC ); |
| 193 | TEST_ASSERT( NULL != cipher_info); |
| 194 | |
| 195 | TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); |
| 196 | |
| 197 | TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); |
| 198 | |
Manuel Pégourié-Gonnard | 9c853b9 | 2013-09-03 13:04:44 +0200 | [diff] [blame] | 199 | TEST_ASSERT( 0 == cipher_set_iv( &ctx_dec, iv, 16 ) ); |
| 200 | |
Manuel Pégourié-Gonnard | 2adc40c | 2013-09-03 13:54:12 +0200 | [diff] [blame] | 201 | TEST_ASSERT( 0 == cipher_reset( &ctx_dec ) ); |
| 202 | |
Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 203 | #if defined(POLARSSL_CIPHER_MODE_AEAD) |
Manuel Pégourié-Gonnard | 2adc40c | 2013-09-03 13:54:12 +0200 | [diff] [blame] | 204 | TEST_ASSERT( 0 == cipher_update_ad( &ctx_dec, NULL, 0 ) ); |
Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 205 | #endif /* POLARSSL_CIPHER_MODE_AEAD */ |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 206 | |
| 207 | /* decode 0-byte string */ |
| 208 | TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, 0, decbuf, &outlen ) ); |
| 209 | TEST_ASSERT( 0 == outlen ); |
Manuel Pégourié-Gonnard | 9241be7 | 2013-08-31 17:31:03 +0200 | [diff] [blame] | 210 | TEST_ASSERT( POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED == cipher_finish( |
Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 211 | &ctx_dec, decbuf + outlen, &outlen ) ); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 212 | TEST_ASSERT( 0 == outlen ); |
| 213 | |
| 214 | TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) ); |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 215 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 216 | /* END_CASE */ |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 217 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 218 | /* BEGIN_CASE */ |
| 219 | void enc_dec_buf_multipart( int cipher_id, int key_len, int first_length_val, |
| 220 | int second_length_val ) |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 221 | { |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 222 | size_t first_length = first_length_val; |
| 223 | size_t second_length = second_length_val; |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 224 | size_t length = first_length + second_length; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 225 | unsigned char key[32]; |
| 226 | unsigned char iv[16]; |
| 227 | |
| 228 | cipher_context_t ctx_dec; |
| 229 | cipher_context_t ctx_enc; |
| 230 | const cipher_info_t *cipher_info; |
| 231 | |
| 232 | unsigned char inbuf[64]; |
| 233 | unsigned char encbuf[64]; |
| 234 | unsigned char decbuf[64]; |
| 235 | |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 236 | size_t outlen = 0; |
| 237 | size_t totaloutlen = 0; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 238 | |
| 239 | memset( key, 0, 32 ); |
| 240 | memset( iv , 0, 16 ); |
| 241 | |
| 242 | memset( &ctx_dec, 0, sizeof( ctx_dec ) ); |
| 243 | memset( &ctx_enc, 0, sizeof( ctx_enc ) ); |
| 244 | |
| 245 | memset( inbuf, 5, 64 ); |
| 246 | memset( encbuf, 0, 64 ); |
| 247 | memset( decbuf, 0, 64 ); |
| 248 | |
| 249 | /* Initialise enc and dec contexts */ |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 250 | cipher_info = cipher_info_from_type( cipher_id ); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 251 | TEST_ASSERT( NULL != cipher_info); |
| 252 | |
| 253 | TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); |
| 254 | TEST_ASSERT( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); |
| 255 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 256 | TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, key_len, POLARSSL_DECRYPT ) ); |
| 257 | TEST_ASSERT( 0 == cipher_setkey( &ctx_enc, key, key_len, POLARSSL_ENCRYPT ) ); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 258 | |
Manuel Pégourié-Gonnard | 9c853b9 | 2013-09-03 13:04:44 +0200 | [diff] [blame] | 259 | TEST_ASSERT( 0 == cipher_set_iv( &ctx_dec, iv, 16 ) ); |
| 260 | TEST_ASSERT( 0 == cipher_set_iv( &ctx_enc, iv, 16 ) ); |
| 261 | |
Manuel Pégourié-Gonnard | 2adc40c | 2013-09-03 13:54:12 +0200 | [diff] [blame] | 262 | TEST_ASSERT( 0 == cipher_reset( &ctx_dec ) ); |
| 263 | TEST_ASSERT( 0 == cipher_reset( &ctx_enc ) ); |
| 264 | |
Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 265 | #if defined(POLARSSL_CIPHER_MODE_AEAD) |
Manuel Pégourié-Gonnard | 2adc40c | 2013-09-03 13:54:12 +0200 | [diff] [blame] | 266 | TEST_ASSERT( 0 == cipher_update_ad( &ctx_dec, NULL, 0 ) ); |
| 267 | TEST_ASSERT( 0 == cipher_update_ad( &ctx_enc, NULL, 0 ) ); |
Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 268 | #endif /* POLARSSL_CIPHER_MODE_AEAD */ |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 269 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 270 | /* encode length number of bytes from inbuf */ |
| 271 | TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); |
| 272 | totaloutlen = outlen; |
| 273 | TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); |
| 274 | totaloutlen += outlen; |
Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 275 | TEST_ASSERT( totaloutlen == length || |
| 276 | ( totaloutlen % cipher_get_block_size( &ctx_enc ) == 0 && |
| 277 | totaloutlen < length && |
| 278 | totaloutlen + cipher_get_block_size( &ctx_enc ) > length ) ); |
| 279 | |
Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 280 | TEST_ASSERT( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 281 | totaloutlen += outlen; |
Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 282 | TEST_ASSERT( totaloutlen == length || |
| 283 | ( totaloutlen % cipher_get_block_size( &ctx_enc ) == 0 && |
| 284 | totaloutlen > length && |
| 285 | totaloutlen <= length + cipher_get_block_size( &ctx_enc ) ) ); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 286 | |
| 287 | /* decode the previously encoded string */ |
Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 288 | TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, totaloutlen, decbuf, &outlen ) ); |
| 289 | totaloutlen = outlen; |
| 290 | |
| 291 | TEST_ASSERT( totaloutlen == length || |
| 292 | ( totaloutlen % cipher_get_block_size( &ctx_dec ) == 0 && |
| 293 | totaloutlen < length && |
Manuel Pégourié-Gonnard | 07f8fa5 | 2013-08-30 18:34:08 +0200 | [diff] [blame] | 294 | totaloutlen + cipher_get_block_size( &ctx_dec ) >= length ) ); |
Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 295 | |
Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 296 | TEST_ASSERT( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); |
Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 297 | totaloutlen += outlen; |
| 298 | |
| 299 | TEST_ASSERT( totaloutlen == length ); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 300 | |
| 301 | TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) ); |
| 302 | |
| 303 | TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) ); |
| 304 | TEST_ASSERT( 0 == cipher_free_ctx( &ctx_enc ) ); |
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 */ |
Manuel Pégourié-Gonnard | 8eccab5 | 2013-09-03 18:31:25 +0200 | [diff] [blame] | 309 | void decrypt_test_vec( int cipher_id, int pad_mode, |
| 310 | char *hex_key, char *hex_iv, |
| 311 | char *hex_cipher, char *hex_clear, |
| 312 | char *hex_ad, char *hex_tag, |
| 313 | int finish_result, int tag_result ) |
| 314 | { |
Manuel Pégourié-Gonnard | f7ce67f | 2013-09-03 20:17:35 +0200 | [diff] [blame^] | 315 | unsigned char key[50]; |
| 316 | unsigned char iv[50]; |
| 317 | unsigned char cipher[200]; |
| 318 | unsigned char clear[200]; |
| 319 | unsigned char ad[200]; |
| 320 | unsigned char tag[20]; |
Manuel Pégourié-Gonnard | 8eccab5 | 2013-09-03 18:31:25 +0200 | [diff] [blame] | 321 | size_t key_len, iv_len, cipher_len, clear_len, ad_len, tag_len; |
| 322 | cipher_context_t ctx; |
Manuel Pégourié-Gonnard | f7ce67f | 2013-09-03 20:17:35 +0200 | [diff] [blame^] | 323 | unsigned char output[200]; |
Manuel Pégourié-Gonnard | 8eccab5 | 2013-09-03 18:31:25 +0200 | [diff] [blame] | 324 | size_t outlen, total_len; |
| 325 | |
| 326 | memset( key, 0x00, sizeof( key ) ); |
| 327 | memset( iv, 0x00, sizeof( iv ) ); |
| 328 | memset( cipher, 0x00, sizeof( cipher ) ); |
| 329 | memset( clear, 0x00, sizeof( clear ) ); |
| 330 | memset( ad, 0x00, sizeof( ad ) ); |
| 331 | memset( tag, 0x00, sizeof( tag ) ); |
| 332 | memset( output, 0x00, sizeof( output ) ); |
| 333 | |
| 334 | key_len = unhexify( key, hex_key ); |
| 335 | iv_len = unhexify( iv, hex_iv ); |
| 336 | cipher_len = unhexify( cipher, hex_cipher ); |
| 337 | clear_len = unhexify( clear, hex_clear ); |
| 338 | ad_len = unhexify( ad, hex_ad ); |
| 339 | tag_len = unhexify( tag, hex_tag ); |
| 340 | |
| 341 | /* Prepare context */ |
| 342 | TEST_ASSERT( 0 == cipher_init_ctx( &ctx, |
| 343 | cipher_info_from_type( cipher_id ) ) ); |
| 344 | TEST_ASSERT( 0 == cipher_setkey( &ctx, key, 8 * key_len, POLARSSL_DECRYPT ) ); |
| 345 | if( pad_mode != -1 ) |
| 346 | TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, pad_mode ) ); |
| 347 | TEST_ASSERT( 0 == cipher_set_iv( &ctx, iv, iv_len ) ); |
| 348 | TEST_ASSERT( 0 == cipher_reset( &ctx ) ); |
| 349 | #if defined(POLARSSL_CIPHER_MODE_AEAD) |
| 350 | TEST_ASSERT( 0 == cipher_update_ad( &ctx, ad, ad_len ) ); |
| 351 | #endif /* POLARSSL_CIPHER_MODE_AEAD */ |
| 352 | |
| 353 | /* decode buffer and check tag */ |
| 354 | total_len = 0; |
| 355 | TEST_ASSERT( 0 == cipher_update( &ctx, cipher, cipher_len, output, &outlen ) ); |
| 356 | total_len += outlen; |
| 357 | TEST_ASSERT( finish_result == cipher_finish( &ctx, output + outlen, |
| 358 | &outlen ) ); |
| 359 | total_len += outlen; |
| 360 | #if defined(POLARSSL_CIPHER_MODE_AEAD) |
| 361 | TEST_ASSERT( tag_result == cipher_check_tag( &ctx, tag, tag_len ) ); |
| 362 | #endif /* POLARSSL_CIPHER_MODE_AEAD */ |
| 363 | |
| 364 | /* check plaintext only if everything went fine */ |
| 365 | if( 0 == finish_result && 0 == tag_result ) |
| 366 | { |
| 367 | TEST_ASSERT( total_len == clear_len ); |
| 368 | TEST_ASSERT( 0 == memcmp( output, clear, clear_len ) ); |
| 369 | } |
| 370 | |
| 371 | cipher_free_ctx( &ctx ); |
| 372 | } |
| 373 | /* END_CASE */ |
| 374 | |
| 375 | /* BEGIN_CASE */ |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 376 | void set_padding( int cipher_id, int pad_mode, int ret ) |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 377 | { |
Manuel Pégourié-Gonnard | d5fdcaf | 2013-07-24 18:05:00 +0200 | [diff] [blame] | 378 | const cipher_info_t *cipher_info; |
| 379 | cipher_context_t ctx; |
| 380 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 381 | cipher_info = cipher_info_from_type( cipher_id ); |
Manuel Pégourié-Gonnard | d5fdcaf | 2013-07-24 18:05:00 +0200 | [diff] [blame] | 382 | TEST_ASSERT( NULL != cipher_info ); |
| 383 | TEST_ASSERT( 0 == cipher_init_ctx( &ctx, cipher_info ) ); |
| 384 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 385 | TEST_ASSERT( ret == cipher_set_padding_mode( &ctx, pad_mode ) ); |
Manuel Pégourié-Gonnard | d5fdcaf | 2013-07-24 18:05:00 +0200 | [diff] [blame] | 386 | |
| 387 | TEST_ASSERT( 0 == cipher_free_ctx( &ctx ) ); |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 388 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 389 | /* END_CASE */ |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 390 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 391 | /* BEGIN_CASE */ |
| 392 | 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] | 393 | { |
Manuel Pégourié-Gonnard | a640849 | 2013-07-26 10:55:02 +0200 | [diff] [blame] | 394 | cipher_info_t cipher_info; |
| 395 | cipher_context_t ctx; |
| 396 | unsigned char input[16]; |
| 397 | size_t ilen, dlen; |
| 398 | |
| 399 | /* build a fake context just for getting access to get_padding */ |
| 400 | memset( &ctx, 0, sizeof( ctx ) ); |
| 401 | cipher_info.mode = POLARSSL_MODE_CBC; |
| 402 | ctx.cipher_info = &cipher_info; |
| 403 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 404 | TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, pad_mode ) ); |
Manuel Pégourié-Gonnard | a640849 | 2013-07-26 10:55:02 +0200 | [diff] [blame] | 405 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 406 | ilen = unhexify( input, input_str ); |
Manuel Pégourié-Gonnard | a640849 | 2013-07-26 10:55:02 +0200 | [diff] [blame] | 407 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 408 | TEST_ASSERT( ret == ctx.get_padding( input, ilen, &dlen ) ); |
| 409 | if( 0 == ret ) |
| 410 | TEST_ASSERT( dlen == (size_t) dlen_check ); |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 411 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 412 | /* END_CASE */ |
Manuel Pégourié-Gonnard | a640849 | 2013-07-26 10:55:02 +0200 | [diff] [blame] | 413 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 414 | /* BEGIN_CASE */ |
| 415 | void cipher_selftest() |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 416 | { |
| 417 | TEST_ASSERT( cipher_self_test( 0 ) == 0 ); |
| 418 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 419 | /* END_CASE */ |