| 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 |  | 
| Paul Bakker | d2a2d61 | 2014-07-01 15:45:49 +0200 | [diff] [blame] | 32 | cipher_init( &ctx ); | 
| Manuel Pégourié-Gonnard | 5e7693f | 2014-06-13 16:08:07 +0200 | [diff] [blame] | 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 |  | 
| Manuel Pégourié-Gonnard | 8f62563 | 2014-06-24 15:26:28 +0200 | [diff] [blame] | 63 | #if defined(POLARSSL_GCM_C) | 
| Manuel Pégourié-Gonnard | 5e7693f | 2014-06-13 16:08:07 +0200 | [diff] [blame] | 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 |  | 
| Manuel Pégourié-Gonnard | 8f62563 | 2014-06-24 15:26:28 +0200 | [diff] [blame] | 80 | #if defined(POLARSSL_GCM_C) | 
| Manuel Pégourié-Gonnard | 5e7693f | 2014-06-13 16:08:07 +0200 | [diff] [blame] | 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 | d2a2d61 | 2014-07-01 15:45:49 +0200 | [diff] [blame] | 114 | cipher_init( &ctx_dec ); | 
|  | 115 | cipher_init( &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 | 8f62563 | 2014-06-24 15:26:28 +0200 | [diff] [blame] | 160 | #if defined(POLARSSL_GCM_C) | 
| 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 | 8f62563 | 2014-06-24 15:26:28 +0200 | [diff] [blame] | 163 | #endif | 
| 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 | 8f62563 | 2014-06-24 15:26:28 +0200 | [diff] [blame] | 177 | #if defined(POLARSSL_GCM_C) | 
| 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 | 8f62563 | 2014-06-24 15:26:28 +0200 | [diff] [blame] | 179 | #endif | 
| 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 | 8f62563 | 2014-06-24 15:26:28 +0200 | [diff] [blame] | 198 | #if defined(POLARSSL_GCM_C) | 
| 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 | 8f62563 | 2014-06-24 15:26:28 +0200 | [diff] [blame] | 200 | #endif | 
| 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 | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 210 | exit: | 
| Paul Bakker | d2a2d61 | 2014-07-01 15:45:49 +0200 | [diff] [blame] | 211 | cipher_free( &ctx_dec ); | 
|  | 212 | cipher_free( &ctx_enc ); | 
| Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 213 | } | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 214 | /* END_CASE */ | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 215 |  | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 216 | /* BEGIN_CASE */ | 
|  | 217 | void enc_fail( int cipher_id, int pad_mode, int key_len, | 
|  | 218 | int length_val, int ret ) | 
| Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 219 | { | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 220 | size_t length = length_val; | 
| Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 221 | unsigned char key[32]; | 
|  | 222 | unsigned char iv[16]; | 
|  | 223 |  | 
|  | 224 | const cipher_info_t *cipher_info; | 
|  | 225 | cipher_context_t ctx; | 
|  | 226 |  | 
|  | 227 | unsigned char inbuf[64]; | 
|  | 228 | unsigned char encbuf[64]; | 
|  | 229 |  | 
|  | 230 | size_t outlen = 0; | 
|  | 231 |  | 
|  | 232 | memset( key, 0, 32 ); | 
|  | 233 | memset( iv , 0, 16 ); | 
|  | 234 |  | 
| Paul Bakker | d2a2d61 | 2014-07-01 15:45:49 +0200 | [diff] [blame] | 235 | cipher_init( &ctx ); | 
| Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 236 |  | 
|  | 237 | memset( inbuf, 5, 64 ); | 
|  | 238 | memset( encbuf, 0, 64 ); | 
|  | 239 |  | 
|  | 240 | /* Check and get info structures */ | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 241 | cipher_info = cipher_info_from_type( cipher_id ); | 
| Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 242 | TEST_ASSERT( NULL != cipher_info ); | 
|  | 243 |  | 
|  | 244 | /* Initialise context */ | 
|  | 245 | TEST_ASSERT( 0 == cipher_init_ctx( &ctx, cipher_info ) ); | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 246 | 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] | 247 | #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING) | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 248 | TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, pad_mode ) ); | 
| Manuel Pégourié-Gonnard | 989ed38 | 2013-09-13 14:41:45 +0200 | [diff] [blame] | 249 | #else | 
|  | 250 | (void) pad_mode; | 
|  | 251 | #endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */ | 
| Manuel Pégourié-Gonnard | 9c853b9 | 2013-09-03 13:04:44 +0200 | [diff] [blame] | 252 | TEST_ASSERT( 0 == cipher_set_iv( &ctx, iv, 16 ) ); | 
| Manuel Pégourié-Gonnard | 2adc40c | 2013-09-03 13:54:12 +0200 | [diff] [blame] | 253 | TEST_ASSERT( 0 == cipher_reset( &ctx ) ); | 
| Manuel Pégourié-Gonnard | 8f62563 | 2014-06-24 15:26:28 +0200 | [diff] [blame] | 254 | #if defined(POLARSSL_GCM_C) | 
| Manuel Pégourié-Gonnard | 2adc40c | 2013-09-03 13:54:12 +0200 | [diff] [blame] | 255 | TEST_ASSERT( 0 == cipher_update_ad( &ctx, NULL, 0 ) ); | 
| Manuel Pégourié-Gonnard | 8f62563 | 2014-06-24 15:26:28 +0200 | [diff] [blame] | 256 | #endif | 
| Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 257 |  | 
|  | 258 | /* encode length number of bytes from inbuf */ | 
|  | 259 | TEST_ASSERT( 0 == cipher_update( &ctx, inbuf, length, encbuf, &outlen ) ); | 
| Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 260 | TEST_ASSERT( ret == cipher_finish( &ctx, encbuf + outlen, &outlen ) ); | 
| Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 261 |  | 
|  | 262 | /* done */ | 
| Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 263 | exit: | 
| Paul Bakker | d2a2d61 | 2014-07-01 15:45:49 +0200 | [diff] [blame] | 264 | cipher_free( &ctx ); | 
| Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 265 | } | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 266 | /* END_CASE */ | 
| Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 267 |  | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 268 | /* BEGIN_CASE */ | 
|  | 269 | void dec_empty_buf() | 
| Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 270 | { | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 271 | unsigned char key[32]; | 
|  | 272 | unsigned char iv[16]; | 
|  | 273 |  | 
|  | 274 | cipher_context_t ctx_dec; | 
|  | 275 | const cipher_info_t *cipher_info; | 
|  | 276 |  | 
|  | 277 | unsigned char encbuf[64]; | 
|  | 278 | unsigned char decbuf[64]; | 
|  | 279 |  | 
| Paul Bakker | f4a3f30 | 2011-04-24 15:53:29 +0000 | [diff] [blame] | 280 | size_t outlen = 0; | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 281 |  | 
|  | 282 | memset( key, 0, 32 ); | 
|  | 283 | memset( iv , 0, 16 ); | 
| Paul Bakker | d2a2d61 | 2014-07-01 15:45:49 +0200 | [diff] [blame] | 284 |  | 
|  | 285 | cipher_init( &ctx_dec ); | 
|  | 286 |  | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 287 | memset( encbuf, 0, 64 ); | 
|  | 288 | memset( decbuf, 0, 64 ); | 
|  | 289 |  | 
| Manuel Pégourié-Gonnard | 9c853b9 | 2013-09-03 13:04:44 +0200 | [diff] [blame] | 290 | /* Initialise context */ | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 291 | cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CBC ); | 
|  | 292 | TEST_ASSERT( NULL != cipher_info); | 
| Paul Bakker | d2a2d61 | 2014-07-01 15:45:49 +0200 | [diff] [blame] | 293 |  | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 294 | TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); | 
|  | 295 |  | 
|  | 296 | TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); | 
|  | 297 |  | 
| Manuel Pégourié-Gonnard | 9c853b9 | 2013-09-03 13:04:44 +0200 | [diff] [blame] | 298 | TEST_ASSERT( 0 == cipher_set_iv( &ctx_dec, iv, 16 ) ); | 
|  | 299 |  | 
| Manuel Pégourié-Gonnard | 2adc40c | 2013-09-03 13:54:12 +0200 | [diff] [blame] | 300 | TEST_ASSERT( 0 == cipher_reset( &ctx_dec ) ); | 
|  | 301 |  | 
| Manuel Pégourié-Gonnard | 8f62563 | 2014-06-24 15:26:28 +0200 | [diff] [blame] | 302 | #if defined(POLARSSL_GCM_C) | 
| Manuel Pégourié-Gonnard | 2adc40c | 2013-09-03 13:54:12 +0200 | [diff] [blame] | 303 | TEST_ASSERT( 0 == cipher_update_ad( &ctx_dec, NULL, 0 ) ); | 
| Manuel Pégourié-Gonnard | 8f62563 | 2014-06-24 15:26:28 +0200 | [diff] [blame] | 304 | #endif | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 305 |  | 
|  | 306 | /* decode 0-byte string */ | 
|  | 307 | TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, 0, decbuf, &outlen ) ); | 
|  | 308 | TEST_ASSERT( 0 == outlen ); | 
| Manuel Pégourié-Gonnard | 9241be7 | 2013-08-31 17:31:03 +0200 | [diff] [blame] | 309 | TEST_ASSERT( POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED == cipher_finish( | 
| Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 310 | &ctx_dec, decbuf + outlen, &outlen ) ); | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 311 | TEST_ASSERT( 0 == outlen ); | 
|  | 312 |  | 
| Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 313 | exit: | 
| Paul Bakker | d2a2d61 | 2014-07-01 15:45:49 +0200 | [diff] [blame] | 314 | cipher_free( &ctx_dec ); | 
| Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 315 | } | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 316 | /* END_CASE */ | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 317 |  | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 318 | /* BEGIN_CASE */ | 
|  | 319 | void enc_dec_buf_multipart( int cipher_id, int key_len, int first_length_val, | 
|  | 320 | int second_length_val ) | 
| Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 321 | { | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 322 | size_t first_length = first_length_val; | 
|  | 323 | size_t second_length = second_length_val; | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 324 | size_t length = first_length + second_length; | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 325 | unsigned char key[32]; | 
|  | 326 | unsigned char iv[16]; | 
|  | 327 |  | 
|  | 328 | cipher_context_t ctx_dec; | 
|  | 329 | cipher_context_t ctx_enc; | 
|  | 330 | const cipher_info_t *cipher_info; | 
|  | 331 |  | 
|  | 332 | unsigned char inbuf[64]; | 
|  | 333 | unsigned char encbuf[64]; | 
|  | 334 | unsigned char decbuf[64]; | 
|  | 335 |  | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 336 | size_t outlen = 0; | 
|  | 337 | size_t totaloutlen = 0; | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 338 |  | 
|  | 339 | memset( key, 0, 32 ); | 
|  | 340 | memset( iv , 0, 16 ); | 
| Paul Bakker | d2a2d61 | 2014-07-01 15:45:49 +0200 | [diff] [blame] | 341 |  | 
|  | 342 | cipher_init( &ctx_dec ); | 
|  | 343 | cipher_init( &ctx_enc ); | 
|  | 344 |  | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 345 | memset( inbuf, 5, 64 ); | 
|  | 346 | memset( encbuf, 0, 64 ); | 
|  | 347 | memset( decbuf, 0, 64 ); | 
|  | 348 |  | 
|  | 349 | /* Initialise enc and dec contexts */ | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 350 | cipher_info = cipher_info_from_type( cipher_id ); | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 351 | TEST_ASSERT( NULL != cipher_info); | 
| Paul Bakker | d2a2d61 | 2014-07-01 15:45:49 +0200 | [diff] [blame] | 352 |  | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 353 | TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); | 
|  | 354 | TEST_ASSERT( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); | 
| Paul Bakker | d2a2d61 | 2014-07-01 15:45:49 +0200 | [diff] [blame] | 355 |  | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 356 | TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, key_len, POLARSSL_DECRYPT ) ); | 
|  | 357 | TEST_ASSERT( 0 == cipher_setkey( &ctx_enc, key, key_len, POLARSSL_ENCRYPT ) ); | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 358 |  | 
| Manuel Pégourié-Gonnard | 9c853b9 | 2013-09-03 13:04:44 +0200 | [diff] [blame] | 359 | TEST_ASSERT( 0 == cipher_set_iv( &ctx_dec, iv, 16 ) ); | 
|  | 360 | TEST_ASSERT( 0 == cipher_set_iv( &ctx_enc, iv, 16 ) ); | 
|  | 361 |  | 
| Manuel Pégourié-Gonnard | 2adc40c | 2013-09-03 13:54:12 +0200 | [diff] [blame] | 362 | TEST_ASSERT( 0 == cipher_reset( &ctx_dec ) ); | 
|  | 363 | TEST_ASSERT( 0 == cipher_reset( &ctx_enc ) ); | 
|  | 364 |  | 
| Manuel Pégourié-Gonnard | 8f62563 | 2014-06-24 15:26:28 +0200 | [diff] [blame] | 365 | #if defined(POLARSSL_GCM_C) | 
| Manuel Pégourié-Gonnard | 2adc40c | 2013-09-03 13:54:12 +0200 | [diff] [blame] | 366 | TEST_ASSERT( 0 == cipher_update_ad( &ctx_dec, NULL, 0 ) ); | 
|  | 367 | TEST_ASSERT( 0 == cipher_update_ad( &ctx_enc, NULL, 0 ) ); | 
| Manuel Pégourié-Gonnard | 8f62563 | 2014-06-24 15:26:28 +0200 | [diff] [blame] | 368 | #endif | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 369 |  | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 370 | /* encode length number of bytes from inbuf */ | 
|  | 371 | TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); | 
|  | 372 | totaloutlen = outlen; | 
|  | 373 | TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); | 
|  | 374 | totaloutlen += outlen; | 
| Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 375 | TEST_ASSERT( totaloutlen == length || | 
|  | 376 | ( totaloutlen % cipher_get_block_size( &ctx_enc ) == 0 && | 
|  | 377 | totaloutlen < length && | 
|  | 378 | totaloutlen + cipher_get_block_size( &ctx_enc ) > length ) ); | 
|  | 379 |  | 
| Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 380 | TEST_ASSERT( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 381 | totaloutlen += outlen; | 
| Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 382 | TEST_ASSERT( totaloutlen == length || | 
|  | 383 | ( totaloutlen % cipher_get_block_size( &ctx_enc ) == 0 && | 
|  | 384 | totaloutlen > length && | 
|  | 385 | totaloutlen <= length + cipher_get_block_size( &ctx_enc ) ) ); | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 386 |  | 
|  | 387 | /* decode the previously encoded string */ | 
| Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 388 | TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, totaloutlen, decbuf, &outlen ) ); | 
|  | 389 | totaloutlen = outlen; | 
|  | 390 |  | 
|  | 391 | TEST_ASSERT( totaloutlen == length || | 
|  | 392 | ( totaloutlen % cipher_get_block_size( &ctx_dec ) == 0 && | 
|  | 393 | totaloutlen < length && | 
| Manuel Pégourié-Gonnard | 07f8fa5 | 2013-08-30 18:34:08 +0200 | [diff] [blame] | 394 | totaloutlen + cipher_get_block_size( &ctx_dec ) >= length ) ); | 
| Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 395 |  | 
| Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 396 | TEST_ASSERT( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); | 
| Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 397 | totaloutlen += outlen; | 
|  | 398 |  | 
|  | 399 | TEST_ASSERT( totaloutlen == length ); | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 400 |  | 
|  | 401 | TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) ); | 
|  | 402 |  | 
| Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 403 | exit: | 
| Paul Bakker | d2a2d61 | 2014-07-01 15:45:49 +0200 | [diff] [blame] | 404 | cipher_free( &ctx_dec ); | 
|  | 405 | cipher_free( &ctx_enc ); | 
| Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 406 | } | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 407 | /* END_CASE */ | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 408 |  | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 409 | /* BEGIN_CASE */ | 
| Manuel Pégourié-Gonnard | 8eccab5 | 2013-09-03 18:31:25 +0200 | [diff] [blame] | 410 | void decrypt_test_vec( int cipher_id, int pad_mode, | 
|  | 411 | char *hex_key, char *hex_iv, | 
|  | 412 | char *hex_cipher, char *hex_clear, | 
|  | 413 | char *hex_ad, char *hex_tag, | 
|  | 414 | int finish_result, int tag_result ) | 
|  | 415 | { | 
| Manuel Pégourié-Gonnard | f7ce67f | 2013-09-03 20:17:35 +0200 | [diff] [blame] | 416 | unsigned char key[50]; | 
|  | 417 | unsigned char iv[50]; | 
|  | 418 | unsigned char cipher[200]; | 
|  | 419 | unsigned char clear[200]; | 
|  | 420 | unsigned char ad[200]; | 
|  | 421 | unsigned char tag[20]; | 
| Manuel Pégourié-Gonnard | a7496f0 | 2013-09-20 11:29:59 +0200 | [diff] [blame] | 422 | size_t key_len, iv_len, cipher_len, clear_len; | 
| Manuel Pégourié-Gonnard | 8f62563 | 2014-06-24 15:26:28 +0200 | [diff] [blame] | 423 | #if defined(POLARSSL_GCM_C) | 
| Manuel Pégourié-Gonnard | a7496f0 | 2013-09-20 11:29:59 +0200 | [diff] [blame] | 424 | size_t ad_len, tag_len; | 
|  | 425 | #endif | 
| Manuel Pégourié-Gonnard | 8eccab5 | 2013-09-03 18:31:25 +0200 | [diff] [blame] | 426 | cipher_context_t ctx; | 
| Manuel Pégourié-Gonnard | f7ce67f | 2013-09-03 20:17:35 +0200 | [diff] [blame] | 427 | unsigned char output[200]; | 
| Manuel Pégourié-Gonnard | 8eccab5 | 2013-09-03 18:31:25 +0200 | [diff] [blame] | 428 | size_t outlen, total_len; | 
|  | 429 |  | 
| Paul Bakker | d2a2d61 | 2014-07-01 15:45:49 +0200 | [diff] [blame] | 430 | cipher_init( &ctx ); | 
|  | 431 |  | 
| Manuel Pégourié-Gonnard | 8eccab5 | 2013-09-03 18:31:25 +0200 | [diff] [blame] | 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( output, 0x00, sizeof( output ) ); | 
|  | 439 |  | 
|  | 440 | key_len = unhexify( key, hex_key ); | 
|  | 441 | iv_len = unhexify( iv, hex_iv ); | 
|  | 442 | cipher_len = unhexify( cipher, hex_cipher ); | 
|  | 443 | clear_len = unhexify( clear, hex_clear ); | 
| Manuel Pégourié-Gonnard | 8f62563 | 2014-06-24 15:26:28 +0200 | [diff] [blame] | 444 | #if defined(POLARSSL_GCM_C) | 
| Manuel Pégourié-Gonnard | 8eccab5 | 2013-09-03 18:31:25 +0200 | [diff] [blame] | 445 | ad_len = unhexify( ad, hex_ad ); | 
|  | 446 | tag_len = unhexify( tag, hex_tag ); | 
| Manuel Pégourié-Gonnard | a7496f0 | 2013-09-20 11:29:59 +0200 | [diff] [blame] | 447 | #else | 
|  | 448 | ((void) hex_ad); | 
|  | 449 | ((void) hex_tag); | 
|  | 450 | #endif | 
| Manuel Pégourié-Gonnard | 8eccab5 | 2013-09-03 18:31:25 +0200 | [diff] [blame] | 451 |  | 
|  | 452 | /* Prepare context */ | 
|  | 453 | TEST_ASSERT( 0 == cipher_init_ctx( &ctx, | 
|  | 454 | cipher_info_from_type( cipher_id ) ) ); | 
|  | 455 | 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] | 456 | #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING) | 
| Manuel Pégourié-Gonnard | 8eccab5 | 2013-09-03 18:31:25 +0200 | [diff] [blame] | 457 | if( pad_mode != -1 ) | 
|  | 458 | TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, pad_mode ) ); | 
| Manuel Pégourié-Gonnard | 989ed38 | 2013-09-13 14:41:45 +0200 | [diff] [blame] | 459 | #else | 
|  | 460 | (void) pad_mode; | 
|  | 461 | #endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */ | 
| Manuel Pégourié-Gonnard | 8eccab5 | 2013-09-03 18:31:25 +0200 | [diff] [blame] | 462 | TEST_ASSERT( 0 == cipher_set_iv( &ctx, iv, iv_len ) ); | 
|  | 463 | TEST_ASSERT( 0 == cipher_reset( &ctx ) ); | 
| Manuel Pégourié-Gonnard | 8f62563 | 2014-06-24 15:26:28 +0200 | [diff] [blame] | 464 | #if defined(POLARSSL_GCM_C) | 
| Manuel Pégourié-Gonnard | 8eccab5 | 2013-09-03 18:31:25 +0200 | [diff] [blame] | 465 | TEST_ASSERT( 0 == cipher_update_ad( &ctx, ad, ad_len ) ); | 
| Manuel Pégourié-Gonnard | 8f62563 | 2014-06-24 15:26:28 +0200 | [diff] [blame] | 466 | #endif | 
| Manuel Pégourié-Gonnard | 8eccab5 | 2013-09-03 18:31:25 +0200 | [diff] [blame] | 467 |  | 
|  | 468 | /* decode buffer and check tag */ | 
|  | 469 | total_len = 0; | 
|  | 470 | TEST_ASSERT( 0 == cipher_update( &ctx, cipher, cipher_len, output, &outlen ) ); | 
|  | 471 | total_len += outlen; | 
|  | 472 | TEST_ASSERT( finish_result == cipher_finish( &ctx, output + outlen, | 
|  | 473 | &outlen ) ); | 
|  | 474 | total_len += outlen; | 
| Manuel Pégourié-Gonnard | 8f62563 | 2014-06-24 15:26:28 +0200 | [diff] [blame] | 475 | #if defined(POLARSSL_GCM_C) | 
| Manuel Pégourié-Gonnard | 8eccab5 | 2013-09-03 18:31:25 +0200 | [diff] [blame] | 476 | TEST_ASSERT( tag_result == cipher_check_tag( &ctx, tag, tag_len ) ); | 
| Manuel Pégourié-Gonnard | 8f62563 | 2014-06-24 15:26:28 +0200 | [diff] [blame] | 477 | #endif | 
| Manuel Pégourié-Gonnard | 8eccab5 | 2013-09-03 18:31:25 +0200 | [diff] [blame] | 478 |  | 
|  | 479 | /* check plaintext only if everything went fine */ | 
|  | 480 | if( 0 == finish_result && 0 == tag_result ) | 
|  | 481 | { | 
|  | 482 | TEST_ASSERT( total_len == clear_len ); | 
|  | 483 | TEST_ASSERT( 0 == memcmp( output, clear, clear_len ) ); | 
|  | 484 | } | 
|  | 485 |  | 
| Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 486 | exit: | 
| Paul Bakker | d2a2d61 | 2014-07-01 15:45:49 +0200 | [diff] [blame] | 487 | cipher_free( &ctx ); | 
| Manuel Pégourié-Gonnard | 8eccab5 | 2013-09-03 18:31:25 +0200 | [diff] [blame] | 488 | } | 
|  | 489 | /* END_CASE */ | 
|  | 490 |  | 
| Manuel Pégourié-Gonnard | 542eac5 | 2014-05-15 16:03:07 +0200 | [diff] [blame] | 491 | /* BEGIN_CASE depends_on:POLARSSL_CIPHER_MODE_AEAD */ | 
|  | 492 | void auth_crypt_tv( int cipher_id, char *hex_key, char *hex_iv, | 
|  | 493 | char *hex_ad, char *hex_cipher, | 
|  | 494 | char *hex_tag, char *hex_clear ) | 
|  | 495 | { | 
|  | 496 | int ret; | 
|  | 497 | unsigned char key[50]; | 
|  | 498 | unsigned char iv[50]; | 
|  | 499 | unsigned char cipher[200]; | 
|  | 500 | unsigned char clear[200]; | 
|  | 501 | unsigned char ad[200]; | 
|  | 502 | unsigned char tag[20]; | 
|  | 503 | unsigned char my_tag[20]; | 
|  | 504 | size_t key_len, iv_len, cipher_len, clear_len, ad_len, tag_len; | 
|  | 505 | cipher_context_t ctx; | 
|  | 506 | unsigned char output[200]; | 
|  | 507 | size_t outlen; | 
|  | 508 |  | 
| Paul Bakker | d2a2d61 | 2014-07-01 15:45:49 +0200 | [diff] [blame] | 509 | cipher_init( &ctx ); | 
|  | 510 |  | 
| Manuel Pégourié-Gonnard | 542eac5 | 2014-05-15 16:03:07 +0200 | [diff] [blame] | 511 | memset( key,    0x00, sizeof( key    ) ); | 
|  | 512 | memset( iv,     0x00, sizeof( iv     ) ); | 
|  | 513 | memset( cipher, 0x00, sizeof( cipher ) ); | 
|  | 514 | memset( clear,  0x00, sizeof( clear  ) ); | 
|  | 515 | memset( ad,     0x00, sizeof( ad     ) ); | 
|  | 516 | memset( tag,    0x00, sizeof( tag    ) ); | 
|  | 517 | memset( my_tag, 0xFF, sizeof( my_tag ) ); | 
|  | 518 | memset( output, 0xFF, sizeof( output ) ); | 
|  | 519 |  | 
|  | 520 | key_len     = unhexify( key,    hex_key     ); | 
|  | 521 | iv_len      = unhexify( iv,     hex_iv      ); | 
|  | 522 | cipher_len  = unhexify( cipher, hex_cipher  ); | 
|  | 523 | ad_len      = unhexify( ad,     hex_ad      ); | 
|  | 524 | tag_len     = unhexify( tag,    hex_tag     ); | 
|  | 525 |  | 
|  | 526 | /* Prepare context */ | 
|  | 527 | TEST_ASSERT( 0 == cipher_init_ctx( &ctx, | 
|  | 528 | cipher_info_from_type( cipher_id ) ) ); | 
|  | 529 | TEST_ASSERT( 0 == cipher_setkey( &ctx, key, 8 * key_len, POLARSSL_DECRYPT ) ); | 
|  | 530 |  | 
|  | 531 | /* decode buffer and check tag */ | 
|  | 532 | ret = cipher_auth_decrypt( &ctx, iv, iv_len, ad, ad_len, | 
|  | 533 | cipher, cipher_len, output, &outlen, | 
|  | 534 | tag, tag_len ); | 
|  | 535 |  | 
|  | 536 | /* make sure we didn't overwrite */ | 
|  | 537 | TEST_ASSERT( output[outlen + 0] == 0xFF ); | 
|  | 538 | TEST_ASSERT( output[outlen + 1] == 0xFF ); | 
|  | 539 |  | 
|  | 540 | /* make sure the message is rejected if it should be */ | 
|  | 541 | if( strcmp( hex_clear, "FAIL" ) == 0 ) | 
|  | 542 | { | 
|  | 543 | TEST_ASSERT( ret == POLARSSL_ERR_CIPHER_AUTH_FAILED ); | 
| Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 544 | goto exit; | 
| Manuel Pégourié-Gonnard | 542eac5 | 2014-05-15 16:03:07 +0200 | [diff] [blame] | 545 | } | 
|  | 546 |  | 
|  | 547 | /* otherwise, make sure it was decrypted properly */ | 
|  | 548 | TEST_ASSERT( ret == 0 ); | 
|  | 549 |  | 
|  | 550 | clear_len = unhexify( clear,  hex_clear   ); | 
|  | 551 | TEST_ASSERT( outlen == clear_len ); | 
|  | 552 | TEST_ASSERT( memcmp( output, clear, clear_len ) == 0 ); | 
|  | 553 |  | 
|  | 554 | /* then encrypt the clear and make sure we get the same ciphertext and tag */ | 
|  | 555 | memset( output, 0xFF, sizeof( output ) ); | 
|  | 556 | outlen = 0; | 
|  | 557 |  | 
|  | 558 | ret = cipher_auth_encrypt( &ctx, iv, iv_len, ad, ad_len, | 
|  | 559 | clear, clear_len, output, &outlen, | 
|  | 560 | my_tag, tag_len ); | 
|  | 561 | TEST_ASSERT( ret == 0 ); | 
|  | 562 |  | 
|  | 563 | TEST_ASSERT( outlen == clear_len ); | 
|  | 564 | TEST_ASSERT( memcmp( output, cipher, clear_len ) == 0 ); | 
|  | 565 | TEST_ASSERT( memcmp( my_tag, tag, tag_len ) == 0 ); | 
|  | 566 |  | 
|  | 567 | /* make sure we didn't overwrite */ | 
|  | 568 | TEST_ASSERT( output[outlen + 0] == 0xFF ); | 
|  | 569 | TEST_ASSERT( output[outlen + 1] == 0xFF ); | 
|  | 570 | TEST_ASSERT( my_tag[tag_len + 0] == 0xFF ); | 
|  | 571 | TEST_ASSERT( my_tag[tag_len + 1] == 0xFF ); | 
|  | 572 |  | 
|  | 573 |  | 
| Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 574 | exit: | 
| Paul Bakker | d2a2d61 | 2014-07-01 15:45:49 +0200 | [diff] [blame] | 575 | cipher_free( &ctx ); | 
| Manuel Pégourié-Gonnard | 542eac5 | 2014-05-15 16:03:07 +0200 | [diff] [blame] | 576 | } | 
|  | 577 | /* END_CASE */ | 
|  | 578 |  | 
| Manuel Pégourié-Gonnard | 8eccab5 | 2013-09-03 18:31:25 +0200 | [diff] [blame] | 579 | /* BEGIN_CASE */ | 
| Paul Bakker | 5e0efa7 | 2013-09-08 23:04:04 +0200 | [diff] [blame] | 580 | void test_vec_ecb( int cipher_id, int operation, char *hex_key, | 
|  | 581 | char *hex_input, char *hex_result, | 
|  | 582 | int finish_result ) | 
|  | 583 | { | 
|  | 584 | unsigned char key[50]; | 
|  | 585 | unsigned char input[16]; | 
|  | 586 | unsigned char result[16]; | 
|  | 587 | size_t key_len; | 
|  | 588 | cipher_context_t ctx; | 
|  | 589 | unsigned char output[32]; | 
|  | 590 | size_t outlen; | 
|  | 591 |  | 
| Paul Bakker | d2a2d61 | 2014-07-01 15:45:49 +0200 | [diff] [blame] | 592 | cipher_init( &ctx ); | 
|  | 593 |  | 
| Paul Bakker | 5e0efa7 | 2013-09-08 23:04:04 +0200 | [diff] [blame] | 594 | memset( key, 0x00, sizeof( key ) ); | 
|  | 595 | memset( input, 0x00, sizeof( input ) ); | 
|  | 596 | memset( result, 0x00, sizeof( result ) ); | 
|  | 597 | memset( output, 0x00, sizeof( output ) ); | 
|  | 598 |  | 
|  | 599 | /* Prepare context */ | 
|  | 600 | TEST_ASSERT( 0 == cipher_init_ctx( &ctx, | 
|  | 601 | cipher_info_from_type( cipher_id ) ) ); | 
|  | 602 |  | 
|  | 603 | key_len = unhexify( key, hex_key ); | 
|  | 604 | TEST_ASSERT( unhexify( input, hex_input ) == | 
|  | 605 | (int) cipher_get_block_size( &ctx ) ); | 
|  | 606 | TEST_ASSERT( unhexify( result, hex_result ) == | 
|  | 607 | (int) cipher_get_block_size( &ctx ) ); | 
|  | 608 |  | 
|  | 609 | TEST_ASSERT( 0 == cipher_setkey( &ctx, key, 8 * key_len, operation ) ); | 
|  | 610 |  | 
|  | 611 | TEST_ASSERT( 0 == cipher_update( &ctx, input, | 
|  | 612 | cipher_get_block_size( &ctx ), | 
|  | 613 | output, &outlen ) ); | 
|  | 614 | TEST_ASSERT( outlen == cipher_get_block_size( &ctx ) ); | 
|  | 615 | TEST_ASSERT( finish_result == cipher_finish( &ctx, output + outlen, | 
|  | 616 | &outlen ) ); | 
|  | 617 | TEST_ASSERT( 0 == outlen ); | 
|  | 618 |  | 
|  | 619 | /* check plaintext only if everything went fine */ | 
|  | 620 | if( 0 == finish_result ) | 
|  | 621 | TEST_ASSERT( 0 == memcmp( output, result, | 
|  | 622 | cipher_get_block_size( &ctx ) ) ); | 
|  | 623 |  | 
| Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 624 | exit: | 
| Paul Bakker | d2a2d61 | 2014-07-01 15:45:49 +0200 | [diff] [blame] | 625 | cipher_free( &ctx ); | 
| Paul Bakker | 5e0efa7 | 2013-09-08 23:04:04 +0200 | [diff] [blame] | 626 | } | 
|  | 627 | /* END_CASE */ | 
|  | 628 |  | 
| Manuel Pégourié-Gonnard | 989ed38 | 2013-09-13 14:41:45 +0200 | [diff] [blame] | 629 | /* BEGIN_CASE depends_on:POLARSSL_CIPHER_MODE_WITH_PADDING */ | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 630 | void set_padding( int cipher_id, int pad_mode, int ret ) | 
| Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 631 | { | 
| Manuel Pégourié-Gonnard | d5fdcaf | 2013-07-24 18:05:00 +0200 | [diff] [blame] | 632 | const cipher_info_t *cipher_info; | 
|  | 633 | cipher_context_t ctx; | 
|  | 634 |  | 
| Paul Bakker | d2a2d61 | 2014-07-01 15:45:49 +0200 | [diff] [blame] | 635 | cipher_init( &ctx ); | 
|  | 636 |  | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 637 | cipher_info = cipher_info_from_type( cipher_id ); | 
| Manuel Pégourié-Gonnard | d5fdcaf | 2013-07-24 18:05:00 +0200 | [diff] [blame] | 638 | TEST_ASSERT( NULL != cipher_info ); | 
|  | 639 | TEST_ASSERT( 0 == cipher_init_ctx( &ctx, cipher_info ) ); | 
|  | 640 |  | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 641 | TEST_ASSERT( ret == cipher_set_padding_mode( &ctx, pad_mode ) ); | 
| Manuel Pégourié-Gonnard | d5fdcaf | 2013-07-24 18:05:00 +0200 | [diff] [blame] | 642 |  | 
| Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 643 | exit: | 
| Paul Bakker | d2a2d61 | 2014-07-01 15:45:49 +0200 | [diff] [blame] | 644 | cipher_free( &ctx ); | 
| Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 645 | } | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 646 | /* END_CASE */ | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 647 |  | 
| Manuel Pégourié-Gonnard | 989ed38 | 2013-09-13 14:41:45 +0200 | [diff] [blame] | 648 | /* BEGIN_CASE depends_on:POLARSSL_CIPHER_MODE_CBC */ | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 649 | 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] | 650 | { | 
| Manuel Pégourié-Gonnard | a640849 | 2013-07-26 10:55:02 +0200 | [diff] [blame] | 651 | cipher_info_t cipher_info; | 
|  | 652 | cipher_context_t ctx; | 
|  | 653 | unsigned char input[16]; | 
|  | 654 | size_t ilen, dlen; | 
|  | 655 |  | 
|  | 656 | /* build a fake context just for getting access to get_padding */ | 
| Paul Bakker | d2a2d61 | 2014-07-01 15:45:49 +0200 | [diff] [blame] | 657 | cipher_init( &ctx ); | 
| Manuel Pégourié-Gonnard | a640849 | 2013-07-26 10:55:02 +0200 | [diff] [blame] | 658 | cipher_info.mode = POLARSSL_MODE_CBC; | 
|  | 659 | ctx.cipher_info = &cipher_info; | 
|  | 660 |  | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 661 | TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, pad_mode ) ); | 
| Manuel Pégourié-Gonnard | a640849 | 2013-07-26 10:55:02 +0200 | [diff] [blame] | 662 |  | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 663 | ilen = unhexify( input, input_str ); | 
| Manuel Pégourié-Gonnard | a640849 | 2013-07-26 10:55:02 +0200 | [diff] [blame] | 664 |  | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 665 | TEST_ASSERT( ret == ctx.get_padding( input, ilen, &dlen ) ); | 
|  | 666 | if( 0 == ret ) | 
|  | 667 | TEST_ASSERT( dlen == (size_t) dlen_check ); | 
| Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 668 | } | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 669 | /* END_CASE */ | 
| Manuel Pégourié-Gonnard | a640849 | 2013-07-26 10:55:02 +0200 | [diff] [blame] | 670 |  | 
| Manuel Pégourié-Gonnard | 2014016 | 2013-10-10 12:48:03 +0200 | [diff] [blame] | 671 | /* BEGIN_CASE depends_on:POLARSSL_SELF_TEST */ | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 672 | void cipher_selftest() | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 673 | { | 
|  | 674 | TEST_ASSERT( cipher_self_test( 0 ) == 0 ); | 
|  | 675 | } | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 676 | /* END_CASE */ |