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