mbed TLS Build
tests/suites/test_suite_cipher.function@1:1a219dea6cb5, 2019-06-04 (annotated)
- Committer:
- williequesada
- Date:
- Tue Jun 04 16:03:38 2019 +0000
- Revision:
- 1:1a219dea6cb5
- Parent:
- 0:cdf462088d13
compartir a Pablo
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
markrad | 0:cdf462088d13 | 1 | /* BEGIN_HEADER */ |
markrad | 0:cdf462088d13 | 2 | #include "mbedtls/cipher.h" |
markrad | 0:cdf462088d13 | 3 | |
markrad | 0:cdf462088d13 | 4 | #if defined(MBEDTLS_GCM_C) |
markrad | 0:cdf462088d13 | 5 | #include "mbedtls/gcm.h" |
markrad | 0:cdf462088d13 | 6 | #endif |
markrad | 0:cdf462088d13 | 7 | /* END_HEADER */ |
markrad | 0:cdf462088d13 | 8 | |
markrad | 0:cdf462088d13 | 9 | /* BEGIN_DEPENDENCIES |
markrad | 0:cdf462088d13 | 10 | * depends_on:MBEDTLS_CIPHER_C |
markrad | 0:cdf462088d13 | 11 | * END_DEPENDENCIES |
markrad | 0:cdf462088d13 | 12 | */ |
markrad | 0:cdf462088d13 | 13 | |
markrad | 0:cdf462088d13 | 14 | /* BEGIN_CASE */ |
markrad | 0:cdf462088d13 | 15 | void mbedtls_cipher_list( ) |
markrad | 0:cdf462088d13 | 16 | { |
markrad | 0:cdf462088d13 | 17 | const int *cipher_type; |
markrad | 0:cdf462088d13 | 18 | |
markrad | 0:cdf462088d13 | 19 | for( cipher_type = mbedtls_cipher_list(); *cipher_type != 0; cipher_type++ ) |
markrad | 0:cdf462088d13 | 20 | TEST_ASSERT( mbedtls_cipher_info_from_type( *cipher_type ) != NULL ); |
markrad | 0:cdf462088d13 | 21 | } |
markrad | 0:cdf462088d13 | 22 | /* END_CASE */ |
markrad | 0:cdf462088d13 | 23 | |
markrad | 0:cdf462088d13 | 24 | /* BEGIN_CASE */ |
markrad | 0:cdf462088d13 | 25 | void cipher_null_args( ) |
markrad | 0:cdf462088d13 | 26 | { |
markrad | 0:cdf462088d13 | 27 | mbedtls_cipher_context_t ctx; |
markrad | 0:cdf462088d13 | 28 | const mbedtls_cipher_info_t *info = mbedtls_cipher_info_from_type( *( mbedtls_cipher_list() ) ); |
markrad | 0:cdf462088d13 | 29 | unsigned char buf[1] = { 0 }; |
markrad | 0:cdf462088d13 | 30 | size_t olen; |
markrad | 0:cdf462088d13 | 31 | |
markrad | 0:cdf462088d13 | 32 | mbedtls_cipher_init( &ctx ); |
markrad | 0:cdf462088d13 | 33 | |
markrad | 0:cdf462088d13 | 34 | TEST_ASSERT( mbedtls_cipher_get_block_size( NULL ) == 0 ); |
markrad | 0:cdf462088d13 | 35 | TEST_ASSERT( mbedtls_cipher_get_block_size( &ctx ) == 0 ); |
markrad | 0:cdf462088d13 | 36 | |
markrad | 0:cdf462088d13 | 37 | TEST_ASSERT( mbedtls_cipher_get_cipher_mode( NULL ) == MBEDTLS_MODE_NONE ); |
markrad | 0:cdf462088d13 | 38 | TEST_ASSERT( mbedtls_cipher_get_cipher_mode( &ctx ) == MBEDTLS_MODE_NONE ); |
markrad | 0:cdf462088d13 | 39 | |
markrad | 0:cdf462088d13 | 40 | TEST_ASSERT( mbedtls_cipher_get_iv_size( NULL ) == 0 ); |
markrad | 0:cdf462088d13 | 41 | TEST_ASSERT( mbedtls_cipher_get_iv_size( &ctx ) == 0 ); |
markrad | 0:cdf462088d13 | 42 | |
markrad | 0:cdf462088d13 | 43 | TEST_ASSERT( mbedtls_cipher_info_from_string( NULL ) == NULL ); |
markrad | 0:cdf462088d13 | 44 | |
markrad | 0:cdf462088d13 | 45 | TEST_ASSERT( mbedtls_cipher_setup( &ctx, NULL ) |
markrad | 0:cdf462088d13 | 46 | == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); |
markrad | 0:cdf462088d13 | 47 | TEST_ASSERT( mbedtls_cipher_setup( NULL, info ) |
markrad | 0:cdf462088d13 | 48 | == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); |
markrad | 0:cdf462088d13 | 49 | |
markrad | 0:cdf462088d13 | 50 | TEST_ASSERT( mbedtls_cipher_setkey( NULL, buf, 0, MBEDTLS_ENCRYPT ) |
markrad | 0:cdf462088d13 | 51 | == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); |
markrad | 0:cdf462088d13 | 52 | TEST_ASSERT( mbedtls_cipher_setkey( &ctx, buf, 0, MBEDTLS_ENCRYPT ) |
markrad | 0:cdf462088d13 | 53 | == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); |
markrad | 0:cdf462088d13 | 54 | |
markrad | 0:cdf462088d13 | 55 | TEST_ASSERT( mbedtls_cipher_set_iv( NULL, buf, 0 ) |
markrad | 0:cdf462088d13 | 56 | == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); |
markrad | 0:cdf462088d13 | 57 | TEST_ASSERT( mbedtls_cipher_set_iv( &ctx, buf, 0 ) |
markrad | 0:cdf462088d13 | 58 | == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); |
markrad | 0:cdf462088d13 | 59 | |
markrad | 0:cdf462088d13 | 60 | TEST_ASSERT( mbedtls_cipher_reset( NULL ) == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); |
markrad | 0:cdf462088d13 | 61 | TEST_ASSERT( mbedtls_cipher_reset( &ctx ) == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); |
markrad | 0:cdf462088d13 | 62 | |
markrad | 0:cdf462088d13 | 63 | #if defined(MBEDTLS_GCM_C) |
markrad | 0:cdf462088d13 | 64 | TEST_ASSERT( mbedtls_cipher_update_ad( NULL, buf, 0 ) |
markrad | 0:cdf462088d13 | 65 | == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); |
markrad | 0:cdf462088d13 | 66 | TEST_ASSERT( mbedtls_cipher_update_ad( &ctx, buf, 0 ) |
markrad | 0:cdf462088d13 | 67 | == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); |
markrad | 0:cdf462088d13 | 68 | #endif |
markrad | 0:cdf462088d13 | 69 | |
markrad | 0:cdf462088d13 | 70 | TEST_ASSERT( mbedtls_cipher_update( NULL, buf, 0, buf, &olen ) |
markrad | 0:cdf462088d13 | 71 | == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); |
markrad | 0:cdf462088d13 | 72 | TEST_ASSERT( mbedtls_cipher_update( &ctx, buf, 0, buf, &olen ) |
markrad | 0:cdf462088d13 | 73 | == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); |
markrad | 0:cdf462088d13 | 74 | |
markrad | 0:cdf462088d13 | 75 | TEST_ASSERT( mbedtls_cipher_finish( NULL, buf, &olen ) |
markrad | 0:cdf462088d13 | 76 | == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); |
markrad | 0:cdf462088d13 | 77 | TEST_ASSERT( mbedtls_cipher_finish( &ctx, buf, &olen ) |
markrad | 0:cdf462088d13 | 78 | == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); |
markrad | 0:cdf462088d13 | 79 | |
markrad | 0:cdf462088d13 | 80 | #if defined(MBEDTLS_GCM_C) |
markrad | 0:cdf462088d13 | 81 | TEST_ASSERT( mbedtls_cipher_write_tag( NULL, buf, olen ) |
markrad | 0:cdf462088d13 | 82 | == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); |
markrad | 0:cdf462088d13 | 83 | TEST_ASSERT( mbedtls_cipher_write_tag( &ctx, buf, olen ) |
markrad | 0:cdf462088d13 | 84 | == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); |
markrad | 0:cdf462088d13 | 85 | |
markrad | 0:cdf462088d13 | 86 | TEST_ASSERT( mbedtls_cipher_check_tag( NULL, buf, olen ) |
markrad | 0:cdf462088d13 | 87 | == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); |
markrad | 0:cdf462088d13 | 88 | TEST_ASSERT( mbedtls_cipher_check_tag( &ctx, buf, olen ) |
markrad | 0:cdf462088d13 | 89 | == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); |
markrad | 0:cdf462088d13 | 90 | #endif |
markrad | 0:cdf462088d13 | 91 | } |
markrad | 0:cdf462088d13 | 92 | /* END_CASE */ |
markrad | 0:cdf462088d13 | 93 | |
markrad | 0:cdf462088d13 | 94 | /* BEGIN_CASE depends_on:MBEDTLS_AES_C */ |
markrad | 0:cdf462088d13 | 95 | void cipher_special_behaviours( ) |
markrad | 0:cdf462088d13 | 96 | { |
markrad | 0:cdf462088d13 | 97 | const mbedtls_cipher_info_t *cipher_info; |
markrad | 0:cdf462088d13 | 98 | mbedtls_cipher_context_t ctx; |
markrad | 0:cdf462088d13 | 99 | unsigned char input[32]; |
markrad | 0:cdf462088d13 | 100 | unsigned char output[32]; |
markrad | 0:cdf462088d13 | 101 | unsigned char iv[32]; |
markrad | 0:cdf462088d13 | 102 | size_t olen = 0; |
markrad | 0:cdf462088d13 | 103 | |
markrad | 0:cdf462088d13 | 104 | mbedtls_cipher_init( &ctx ); |
markrad | 0:cdf462088d13 | 105 | memset( input, 0, sizeof( input ) ); |
markrad | 0:cdf462088d13 | 106 | memset( output, 0, sizeof( output ) ); |
markrad | 0:cdf462088d13 | 107 | memset( iv, 0, sizeof( iv ) ); |
markrad | 0:cdf462088d13 | 108 | |
markrad | 0:cdf462088d13 | 109 | /* Check and get info structures */ |
markrad | 0:cdf462088d13 | 110 | cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_AES_128_ECB ); |
markrad | 0:cdf462088d13 | 111 | TEST_ASSERT( NULL != cipher_info ); |
markrad | 0:cdf462088d13 | 112 | |
markrad | 0:cdf462088d13 | 113 | TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) ); |
markrad | 0:cdf462088d13 | 114 | |
markrad | 0:cdf462088d13 | 115 | /* IV too big */ |
markrad | 0:cdf462088d13 | 116 | TEST_ASSERT( mbedtls_cipher_set_iv( &ctx, iv, MBEDTLS_MAX_IV_LENGTH + 1 ) |
markrad | 0:cdf462088d13 | 117 | == MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE ); |
markrad | 0:cdf462088d13 | 118 | |
markrad | 0:cdf462088d13 | 119 | /* IV too small */ |
markrad | 0:cdf462088d13 | 120 | TEST_ASSERT( mbedtls_cipher_set_iv( &ctx, iv, 0 ) |
markrad | 0:cdf462088d13 | 121 | == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); |
markrad | 0:cdf462088d13 | 122 | |
markrad | 0:cdf462088d13 | 123 | /* Update ECB with partial block */ |
markrad | 0:cdf462088d13 | 124 | TEST_ASSERT( mbedtls_cipher_update( &ctx, input, 1, output, &olen ) |
markrad | 0:cdf462088d13 | 125 | == MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED ); |
markrad | 0:cdf462088d13 | 126 | |
markrad | 0:cdf462088d13 | 127 | exit: |
markrad | 0:cdf462088d13 | 128 | mbedtls_cipher_free( &ctx ); |
markrad | 0:cdf462088d13 | 129 | } |
markrad | 0:cdf462088d13 | 130 | /* END_CASE */ |
markrad | 0:cdf462088d13 | 131 | |
markrad | 0:cdf462088d13 | 132 | /* BEGIN_CASE */ |
markrad | 0:cdf462088d13 | 133 | void enc_dec_buf( int cipher_id, char *cipher_string, int key_len, |
markrad | 0:cdf462088d13 | 134 | int length_val, int pad_mode ) |
markrad | 0:cdf462088d13 | 135 | { |
markrad | 0:cdf462088d13 | 136 | size_t length = length_val, outlen, total_len, i, block_size; |
markrad | 0:cdf462088d13 | 137 | unsigned char key[32]; |
markrad | 0:cdf462088d13 | 138 | unsigned char iv[16]; |
markrad | 0:cdf462088d13 | 139 | unsigned char ad[13]; |
markrad | 0:cdf462088d13 | 140 | unsigned char tag[16]; |
markrad | 0:cdf462088d13 | 141 | unsigned char inbuf[64]; |
markrad | 0:cdf462088d13 | 142 | unsigned char encbuf[64]; |
markrad | 0:cdf462088d13 | 143 | unsigned char decbuf[64]; |
markrad | 0:cdf462088d13 | 144 | |
markrad | 0:cdf462088d13 | 145 | const mbedtls_cipher_info_t *cipher_info; |
markrad | 0:cdf462088d13 | 146 | mbedtls_cipher_context_t ctx_dec; |
markrad | 0:cdf462088d13 | 147 | mbedtls_cipher_context_t ctx_enc; |
markrad | 0:cdf462088d13 | 148 | |
markrad | 0:cdf462088d13 | 149 | /* |
markrad | 0:cdf462088d13 | 150 | * Prepare contexts |
markrad | 0:cdf462088d13 | 151 | */ |
markrad | 0:cdf462088d13 | 152 | mbedtls_cipher_init( &ctx_dec ); |
markrad | 0:cdf462088d13 | 153 | mbedtls_cipher_init( &ctx_enc ); |
markrad | 0:cdf462088d13 | 154 | |
markrad | 0:cdf462088d13 | 155 | memset( key, 0x2a, sizeof( key ) ); |
markrad | 0:cdf462088d13 | 156 | |
markrad | 0:cdf462088d13 | 157 | /* Check and get info structures */ |
markrad | 0:cdf462088d13 | 158 | cipher_info = mbedtls_cipher_info_from_type( cipher_id ); |
markrad | 0:cdf462088d13 | 159 | TEST_ASSERT( NULL != cipher_info ); |
markrad | 0:cdf462088d13 | 160 | TEST_ASSERT( mbedtls_cipher_info_from_string( cipher_string ) == cipher_info ); |
markrad | 0:cdf462088d13 | 161 | |
markrad | 0:cdf462088d13 | 162 | /* Initialise enc and dec contexts */ |
markrad | 0:cdf462088d13 | 163 | TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_dec, cipher_info ) ); |
markrad | 0:cdf462088d13 | 164 | TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_enc, cipher_info ) ); |
markrad | 0:cdf462088d13 | 165 | |
markrad | 0:cdf462088d13 | 166 | TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_dec, key, key_len, MBEDTLS_DECRYPT ) ); |
markrad | 0:cdf462088d13 | 167 | TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_enc, key, key_len, MBEDTLS_ENCRYPT ) ); |
markrad | 0:cdf462088d13 | 168 | |
markrad | 0:cdf462088d13 | 169 | #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING) |
markrad | 0:cdf462088d13 | 170 | if( -1 != pad_mode ) |
markrad | 0:cdf462088d13 | 171 | { |
markrad | 0:cdf462088d13 | 172 | TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_dec, pad_mode ) ); |
markrad | 0:cdf462088d13 | 173 | TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_enc, pad_mode ) ); |
markrad | 0:cdf462088d13 | 174 | } |
markrad | 0:cdf462088d13 | 175 | #else |
markrad | 0:cdf462088d13 | 176 | (void) pad_mode; |
markrad | 0:cdf462088d13 | 177 | #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */ |
markrad | 0:cdf462088d13 | 178 | |
markrad | 0:cdf462088d13 | 179 | /* |
markrad | 0:cdf462088d13 | 180 | * Do a few encode/decode cycles |
markrad | 0:cdf462088d13 | 181 | */ |
markrad | 0:cdf462088d13 | 182 | for( i = 0; i < 3; i++ ) |
markrad | 0:cdf462088d13 | 183 | { |
markrad | 0:cdf462088d13 | 184 | memset( iv , 0x00 + i, sizeof( iv ) ); |
markrad | 0:cdf462088d13 | 185 | memset( ad, 0x10 + i, sizeof( ad ) ); |
markrad | 0:cdf462088d13 | 186 | memset( inbuf, 0x20 + i, sizeof( inbuf ) ); |
markrad | 0:cdf462088d13 | 187 | |
markrad | 0:cdf462088d13 | 188 | memset( encbuf, 0, sizeof( encbuf ) ); |
markrad | 0:cdf462088d13 | 189 | memset( decbuf, 0, sizeof( decbuf ) ); |
markrad | 0:cdf462088d13 | 190 | memset( tag, 0, sizeof( tag ) ); |
markrad | 0:cdf462088d13 | 191 | |
markrad | 0:cdf462088d13 | 192 | TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_dec, iv, sizeof( iv ) ) ); |
markrad | 0:cdf462088d13 | 193 | TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_enc, iv, sizeof( iv ) ) ); |
markrad | 0:cdf462088d13 | 194 | |
markrad | 0:cdf462088d13 | 195 | TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_dec ) ); |
markrad | 0:cdf462088d13 | 196 | TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_enc ) ); |
markrad | 0:cdf462088d13 | 197 | |
markrad | 0:cdf462088d13 | 198 | #if defined(MBEDTLS_GCM_C) |
markrad | 0:cdf462088d13 | 199 | TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_dec, ad, sizeof( ad ) - i ) ); |
markrad | 0:cdf462088d13 | 200 | TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_enc, ad, sizeof( ad ) - i ) ); |
markrad | 0:cdf462088d13 | 201 | #endif |
markrad | 0:cdf462088d13 | 202 | |
markrad | 0:cdf462088d13 | 203 | block_size = mbedtls_cipher_get_block_size( &ctx_enc ); |
markrad | 0:cdf462088d13 | 204 | TEST_ASSERT( block_size != 0 ); |
markrad | 0:cdf462088d13 | 205 | |
markrad | 0:cdf462088d13 | 206 | /* encode length number of bytes from inbuf */ |
markrad | 0:cdf462088d13 | 207 | TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); |
markrad | 0:cdf462088d13 | 208 | total_len = outlen; |
markrad | 0:cdf462088d13 | 209 | |
markrad | 0:cdf462088d13 | 210 | TEST_ASSERT( total_len == length || |
markrad | 0:cdf462088d13 | 211 | ( total_len % block_size == 0 && |
markrad | 0:cdf462088d13 | 212 | total_len < length && |
markrad | 0:cdf462088d13 | 213 | total_len + block_size > length ) ); |
markrad | 0:cdf462088d13 | 214 | |
markrad | 0:cdf462088d13 | 215 | TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); |
markrad | 0:cdf462088d13 | 216 | total_len += outlen; |
markrad | 0:cdf462088d13 | 217 | |
markrad | 0:cdf462088d13 | 218 | #if defined(MBEDTLS_GCM_C) |
markrad | 0:cdf462088d13 | 219 | TEST_ASSERT( 0 == mbedtls_cipher_write_tag( &ctx_enc, tag, sizeof( tag ) ) ); |
markrad | 0:cdf462088d13 | 220 | #endif |
markrad | 0:cdf462088d13 | 221 | |
markrad | 0:cdf462088d13 | 222 | TEST_ASSERT( total_len == length || |
markrad | 0:cdf462088d13 | 223 | ( total_len % block_size == 0 && |
markrad | 0:cdf462088d13 | 224 | total_len > length && |
markrad | 0:cdf462088d13 | 225 | total_len <= length + block_size ) ); |
markrad | 0:cdf462088d13 | 226 | |
markrad | 0:cdf462088d13 | 227 | /* decode the previously encoded string */ |
markrad | 0:cdf462088d13 | 228 | TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf, total_len, decbuf, &outlen ) ); |
markrad | 0:cdf462088d13 | 229 | total_len = outlen; |
markrad | 0:cdf462088d13 | 230 | |
markrad | 0:cdf462088d13 | 231 | TEST_ASSERT( total_len == length || |
markrad | 0:cdf462088d13 | 232 | ( total_len % block_size == 0 && |
markrad | 0:cdf462088d13 | 233 | total_len < length && |
markrad | 0:cdf462088d13 | 234 | total_len + block_size >= length ) ); |
markrad | 0:cdf462088d13 | 235 | |
markrad | 0:cdf462088d13 | 236 | TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); |
markrad | 0:cdf462088d13 | 237 | total_len += outlen; |
markrad | 0:cdf462088d13 | 238 | |
markrad | 0:cdf462088d13 | 239 | #if defined(MBEDTLS_GCM_C) |
markrad | 0:cdf462088d13 | 240 | TEST_ASSERT( 0 == mbedtls_cipher_check_tag( &ctx_dec, tag, sizeof( tag ) ) ); |
markrad | 0:cdf462088d13 | 241 | #endif |
markrad | 0:cdf462088d13 | 242 | |
markrad | 0:cdf462088d13 | 243 | /* check result */ |
markrad | 0:cdf462088d13 | 244 | TEST_ASSERT( total_len == length ); |
markrad | 0:cdf462088d13 | 245 | TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) ); |
markrad | 0:cdf462088d13 | 246 | } |
markrad | 0:cdf462088d13 | 247 | |
markrad | 0:cdf462088d13 | 248 | /* |
markrad | 0:cdf462088d13 | 249 | * Done |
markrad | 0:cdf462088d13 | 250 | */ |
markrad | 0:cdf462088d13 | 251 | exit: |
markrad | 0:cdf462088d13 | 252 | mbedtls_cipher_free( &ctx_dec ); |
markrad | 0:cdf462088d13 | 253 | mbedtls_cipher_free( &ctx_enc ); |
markrad | 0:cdf462088d13 | 254 | } |
markrad | 0:cdf462088d13 | 255 | /* END_CASE */ |
markrad | 0:cdf462088d13 | 256 | |
markrad | 0:cdf462088d13 | 257 | /* BEGIN_CASE */ |
markrad | 0:cdf462088d13 | 258 | void enc_fail( int cipher_id, int pad_mode, int key_len, |
markrad | 0:cdf462088d13 | 259 | int length_val, int ret ) |
markrad | 0:cdf462088d13 | 260 | { |
markrad | 0:cdf462088d13 | 261 | size_t length = length_val; |
markrad | 0:cdf462088d13 | 262 | unsigned char key[32]; |
markrad | 0:cdf462088d13 | 263 | unsigned char iv[16]; |
markrad | 0:cdf462088d13 | 264 | |
markrad | 0:cdf462088d13 | 265 | const mbedtls_cipher_info_t *cipher_info; |
markrad | 0:cdf462088d13 | 266 | mbedtls_cipher_context_t ctx; |
markrad | 0:cdf462088d13 | 267 | |
markrad | 0:cdf462088d13 | 268 | unsigned char inbuf[64]; |
markrad | 0:cdf462088d13 | 269 | unsigned char encbuf[64]; |
markrad | 0:cdf462088d13 | 270 | |
markrad | 0:cdf462088d13 | 271 | size_t outlen = 0; |
markrad | 0:cdf462088d13 | 272 | |
markrad | 0:cdf462088d13 | 273 | memset( key, 0, 32 ); |
markrad | 0:cdf462088d13 | 274 | memset( iv , 0, 16 ); |
markrad | 0:cdf462088d13 | 275 | |
markrad | 0:cdf462088d13 | 276 | mbedtls_cipher_init( &ctx ); |
markrad | 0:cdf462088d13 | 277 | |
markrad | 0:cdf462088d13 | 278 | memset( inbuf, 5, 64 ); |
markrad | 0:cdf462088d13 | 279 | memset( encbuf, 0, 64 ); |
markrad | 0:cdf462088d13 | 280 | |
markrad | 0:cdf462088d13 | 281 | /* Check and get info structures */ |
markrad | 0:cdf462088d13 | 282 | cipher_info = mbedtls_cipher_info_from_type( cipher_id ); |
markrad | 0:cdf462088d13 | 283 | TEST_ASSERT( NULL != cipher_info ); |
markrad | 0:cdf462088d13 | 284 | |
markrad | 0:cdf462088d13 | 285 | /* Initialise context */ |
markrad | 0:cdf462088d13 | 286 | TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) ); |
markrad | 0:cdf462088d13 | 287 | TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key, key_len, MBEDTLS_ENCRYPT ) ); |
markrad | 0:cdf462088d13 | 288 | #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING) |
markrad | 0:cdf462088d13 | 289 | TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) ); |
markrad | 0:cdf462088d13 | 290 | #else |
markrad | 0:cdf462088d13 | 291 | (void) pad_mode; |
markrad | 0:cdf462088d13 | 292 | #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */ |
markrad | 0:cdf462088d13 | 293 | TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx, iv, 16 ) ); |
markrad | 0:cdf462088d13 | 294 | TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx ) ); |
markrad | 0:cdf462088d13 | 295 | #if defined(MBEDTLS_GCM_C) |
markrad | 0:cdf462088d13 | 296 | TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx, NULL, 0 ) ); |
markrad | 0:cdf462088d13 | 297 | #endif |
markrad | 0:cdf462088d13 | 298 | |
markrad | 0:cdf462088d13 | 299 | /* encode length number of bytes from inbuf */ |
markrad | 0:cdf462088d13 | 300 | TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx, inbuf, length, encbuf, &outlen ) ); |
markrad | 0:cdf462088d13 | 301 | TEST_ASSERT( ret == mbedtls_cipher_finish( &ctx, encbuf + outlen, &outlen ) ); |
markrad | 0:cdf462088d13 | 302 | |
markrad | 0:cdf462088d13 | 303 | /* done */ |
markrad | 0:cdf462088d13 | 304 | exit: |
markrad | 0:cdf462088d13 | 305 | mbedtls_cipher_free( &ctx ); |
markrad | 0:cdf462088d13 | 306 | } |
markrad | 0:cdf462088d13 | 307 | /* END_CASE */ |
markrad | 0:cdf462088d13 | 308 | |
markrad | 0:cdf462088d13 | 309 | /* BEGIN_CASE */ |
markrad | 0:cdf462088d13 | 310 | void dec_empty_buf() |
markrad | 0:cdf462088d13 | 311 | { |
markrad | 0:cdf462088d13 | 312 | unsigned char key[32]; |
markrad | 0:cdf462088d13 | 313 | unsigned char iv[16]; |
markrad | 0:cdf462088d13 | 314 | |
markrad | 0:cdf462088d13 | 315 | mbedtls_cipher_context_t ctx_dec; |
markrad | 0:cdf462088d13 | 316 | const mbedtls_cipher_info_t *cipher_info; |
markrad | 0:cdf462088d13 | 317 | |
markrad | 0:cdf462088d13 | 318 | unsigned char encbuf[64]; |
markrad | 0:cdf462088d13 | 319 | unsigned char decbuf[64]; |
markrad | 0:cdf462088d13 | 320 | |
markrad | 0:cdf462088d13 | 321 | size_t outlen = 0; |
markrad | 0:cdf462088d13 | 322 | |
markrad | 0:cdf462088d13 | 323 | memset( key, 0, 32 ); |
markrad | 0:cdf462088d13 | 324 | memset( iv , 0, 16 ); |
markrad | 0:cdf462088d13 | 325 | |
markrad | 0:cdf462088d13 | 326 | mbedtls_cipher_init( &ctx_dec ); |
markrad | 0:cdf462088d13 | 327 | |
markrad | 0:cdf462088d13 | 328 | memset( encbuf, 0, 64 ); |
markrad | 0:cdf462088d13 | 329 | memset( decbuf, 0, 64 ); |
markrad | 0:cdf462088d13 | 330 | |
markrad | 0:cdf462088d13 | 331 | /* Initialise context */ |
markrad | 0:cdf462088d13 | 332 | cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_AES_128_CBC ); |
markrad | 0:cdf462088d13 | 333 | TEST_ASSERT( NULL != cipher_info); |
markrad | 0:cdf462088d13 | 334 | |
markrad | 0:cdf462088d13 | 335 | TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_dec, cipher_info ) ); |
markrad | 0:cdf462088d13 | 336 | |
markrad | 0:cdf462088d13 | 337 | TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_dec, key, 128, MBEDTLS_DECRYPT ) ); |
markrad | 0:cdf462088d13 | 338 | |
markrad | 0:cdf462088d13 | 339 | TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_dec, iv, 16 ) ); |
markrad | 0:cdf462088d13 | 340 | |
markrad | 0:cdf462088d13 | 341 | TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_dec ) ); |
markrad | 0:cdf462088d13 | 342 | |
markrad | 0:cdf462088d13 | 343 | #if defined(MBEDTLS_GCM_C) |
markrad | 0:cdf462088d13 | 344 | TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_dec, NULL, 0 ) ); |
markrad | 0:cdf462088d13 | 345 | #endif |
markrad | 0:cdf462088d13 | 346 | |
markrad | 0:cdf462088d13 | 347 | /* decode 0-byte string */ |
markrad | 0:cdf462088d13 | 348 | TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf, 0, decbuf, &outlen ) ); |
markrad | 0:cdf462088d13 | 349 | TEST_ASSERT( 0 == outlen ); |
markrad | 0:cdf462088d13 | 350 | TEST_ASSERT( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED == mbedtls_cipher_finish( |
markrad | 0:cdf462088d13 | 351 | &ctx_dec, decbuf + outlen, &outlen ) ); |
markrad | 0:cdf462088d13 | 352 | TEST_ASSERT( 0 == outlen ); |
markrad | 0:cdf462088d13 | 353 | |
markrad | 0:cdf462088d13 | 354 | exit: |
markrad | 0:cdf462088d13 | 355 | mbedtls_cipher_free( &ctx_dec ); |
markrad | 0:cdf462088d13 | 356 | } |
markrad | 0:cdf462088d13 | 357 | /* END_CASE */ |
markrad | 0:cdf462088d13 | 358 | |
markrad | 0:cdf462088d13 | 359 | /* BEGIN_CASE */ |
markrad | 0:cdf462088d13 | 360 | void enc_dec_buf_multipart( int cipher_id, int key_len, int first_length_val, |
markrad | 0:cdf462088d13 | 361 | int second_length_val ) |
markrad | 0:cdf462088d13 | 362 | { |
markrad | 0:cdf462088d13 | 363 | size_t first_length = first_length_val; |
markrad | 0:cdf462088d13 | 364 | size_t second_length = second_length_val; |
markrad | 0:cdf462088d13 | 365 | size_t length = first_length + second_length; |
markrad | 0:cdf462088d13 | 366 | size_t block_size; |
markrad | 0:cdf462088d13 | 367 | unsigned char key[32]; |
markrad | 0:cdf462088d13 | 368 | unsigned char iv[16]; |
markrad | 0:cdf462088d13 | 369 | |
markrad | 0:cdf462088d13 | 370 | mbedtls_cipher_context_t ctx_dec; |
markrad | 0:cdf462088d13 | 371 | mbedtls_cipher_context_t ctx_enc; |
markrad | 0:cdf462088d13 | 372 | const mbedtls_cipher_info_t *cipher_info; |
markrad | 0:cdf462088d13 | 373 | |
markrad | 0:cdf462088d13 | 374 | unsigned char inbuf[64]; |
markrad | 0:cdf462088d13 | 375 | unsigned char encbuf[64]; |
markrad | 0:cdf462088d13 | 376 | unsigned char decbuf[64]; |
markrad | 0:cdf462088d13 | 377 | |
markrad | 0:cdf462088d13 | 378 | size_t outlen = 0; |
markrad | 0:cdf462088d13 | 379 | size_t totaloutlen = 0; |
markrad | 0:cdf462088d13 | 380 | |
markrad | 0:cdf462088d13 | 381 | memset( key, 0, 32 ); |
markrad | 0:cdf462088d13 | 382 | memset( iv , 0, 16 ); |
markrad | 0:cdf462088d13 | 383 | |
markrad | 0:cdf462088d13 | 384 | mbedtls_cipher_init( &ctx_dec ); |
markrad | 0:cdf462088d13 | 385 | mbedtls_cipher_init( &ctx_enc ); |
markrad | 0:cdf462088d13 | 386 | |
markrad | 0:cdf462088d13 | 387 | memset( inbuf, 5, 64 ); |
markrad | 0:cdf462088d13 | 388 | memset( encbuf, 0, 64 ); |
markrad | 0:cdf462088d13 | 389 | memset( decbuf, 0, 64 ); |
markrad | 0:cdf462088d13 | 390 | |
markrad | 0:cdf462088d13 | 391 | /* Initialise enc and dec contexts */ |
markrad | 0:cdf462088d13 | 392 | cipher_info = mbedtls_cipher_info_from_type( cipher_id ); |
markrad | 0:cdf462088d13 | 393 | TEST_ASSERT( NULL != cipher_info); |
markrad | 0:cdf462088d13 | 394 | |
markrad | 0:cdf462088d13 | 395 | TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_dec, cipher_info ) ); |
markrad | 0:cdf462088d13 | 396 | TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_enc, cipher_info ) ); |
markrad | 0:cdf462088d13 | 397 | |
markrad | 0:cdf462088d13 | 398 | TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_dec, key, key_len, MBEDTLS_DECRYPT ) ); |
markrad | 0:cdf462088d13 | 399 | TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_enc, key, key_len, MBEDTLS_ENCRYPT ) ); |
markrad | 0:cdf462088d13 | 400 | |
markrad | 0:cdf462088d13 | 401 | TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_dec, iv, 16 ) ); |
markrad | 0:cdf462088d13 | 402 | TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_enc, iv, 16 ) ); |
markrad | 0:cdf462088d13 | 403 | |
markrad | 0:cdf462088d13 | 404 | TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_dec ) ); |
markrad | 0:cdf462088d13 | 405 | TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_enc ) ); |
markrad | 0:cdf462088d13 | 406 | |
markrad | 0:cdf462088d13 | 407 | #if defined(MBEDTLS_GCM_C) |
markrad | 0:cdf462088d13 | 408 | TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_dec, NULL, 0 ) ); |
markrad | 0:cdf462088d13 | 409 | TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_enc, NULL, 0 ) ); |
markrad | 0:cdf462088d13 | 410 | #endif |
markrad | 0:cdf462088d13 | 411 | |
markrad | 0:cdf462088d13 | 412 | block_size = mbedtls_cipher_get_block_size( &ctx_enc ); |
markrad | 0:cdf462088d13 | 413 | TEST_ASSERT( block_size != 0 ); |
markrad | 0:cdf462088d13 | 414 | |
markrad | 0:cdf462088d13 | 415 | /* encode length number of bytes from inbuf */ |
markrad | 0:cdf462088d13 | 416 | TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); |
markrad | 0:cdf462088d13 | 417 | totaloutlen = outlen; |
markrad | 0:cdf462088d13 | 418 | TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); |
markrad | 0:cdf462088d13 | 419 | totaloutlen += outlen; |
markrad | 0:cdf462088d13 | 420 | TEST_ASSERT( totaloutlen == length || |
markrad | 0:cdf462088d13 | 421 | ( totaloutlen % block_size == 0 && |
markrad | 0:cdf462088d13 | 422 | totaloutlen < length && |
markrad | 0:cdf462088d13 | 423 | totaloutlen + block_size > length ) ); |
markrad | 0:cdf462088d13 | 424 | |
markrad | 0:cdf462088d13 | 425 | TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); |
markrad | 0:cdf462088d13 | 426 | totaloutlen += outlen; |
markrad | 0:cdf462088d13 | 427 | TEST_ASSERT( totaloutlen == length || |
markrad | 0:cdf462088d13 | 428 | ( totaloutlen % block_size == 0 && |
markrad | 0:cdf462088d13 | 429 | totaloutlen > length && |
markrad | 0:cdf462088d13 | 430 | totaloutlen <= length + block_size ) ); |
markrad | 0:cdf462088d13 | 431 | |
markrad | 0:cdf462088d13 | 432 | /* decode the previously encoded string */ |
markrad | 0:cdf462088d13 | 433 | TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf, totaloutlen, decbuf, &outlen ) ); |
markrad | 0:cdf462088d13 | 434 | totaloutlen = outlen; |
markrad | 0:cdf462088d13 | 435 | |
markrad | 0:cdf462088d13 | 436 | TEST_ASSERT( totaloutlen == length || |
markrad | 0:cdf462088d13 | 437 | ( totaloutlen % block_size == 0 && |
markrad | 0:cdf462088d13 | 438 | totaloutlen < length && |
markrad | 0:cdf462088d13 | 439 | totaloutlen + block_size >= length ) ); |
markrad | 0:cdf462088d13 | 440 | |
markrad | 0:cdf462088d13 | 441 | TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); |
markrad | 0:cdf462088d13 | 442 | totaloutlen += outlen; |
markrad | 0:cdf462088d13 | 443 | |
markrad | 0:cdf462088d13 | 444 | TEST_ASSERT( totaloutlen == length ); |
markrad | 0:cdf462088d13 | 445 | |
markrad | 0:cdf462088d13 | 446 | TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) ); |
markrad | 0:cdf462088d13 | 447 | |
markrad | 0:cdf462088d13 | 448 | exit: |
markrad | 0:cdf462088d13 | 449 | mbedtls_cipher_free( &ctx_dec ); |
markrad | 0:cdf462088d13 | 450 | mbedtls_cipher_free( &ctx_enc ); |
markrad | 0:cdf462088d13 | 451 | } |
markrad | 0:cdf462088d13 | 452 | /* END_CASE */ |
markrad | 0:cdf462088d13 | 453 | |
markrad | 0:cdf462088d13 | 454 | /* BEGIN_CASE */ |
markrad | 0:cdf462088d13 | 455 | void decrypt_test_vec( int cipher_id, int pad_mode, |
markrad | 0:cdf462088d13 | 456 | char *hex_key, char *hex_iv, |
markrad | 0:cdf462088d13 | 457 | char *hex_cipher, char *hex_clear, |
markrad | 0:cdf462088d13 | 458 | char *hex_ad, char *hex_tag, |
markrad | 0:cdf462088d13 | 459 | int finish_result, int tag_result ) |
markrad | 0:cdf462088d13 | 460 | { |
markrad | 0:cdf462088d13 | 461 | unsigned char key[50]; |
markrad | 0:cdf462088d13 | 462 | unsigned char iv[50]; |
markrad | 0:cdf462088d13 | 463 | unsigned char cipher[200]; |
markrad | 0:cdf462088d13 | 464 | unsigned char clear[200]; |
markrad | 0:cdf462088d13 | 465 | unsigned char ad[200]; |
markrad | 0:cdf462088d13 | 466 | unsigned char tag[20]; |
markrad | 0:cdf462088d13 | 467 | size_t key_len, iv_len, cipher_len, clear_len; |
markrad | 0:cdf462088d13 | 468 | #if defined(MBEDTLS_GCM_C) |
markrad | 0:cdf462088d13 | 469 | size_t ad_len, tag_len; |
markrad | 0:cdf462088d13 | 470 | #endif |
markrad | 0:cdf462088d13 | 471 | mbedtls_cipher_context_t ctx; |
markrad | 0:cdf462088d13 | 472 | unsigned char output[200]; |
markrad | 0:cdf462088d13 | 473 | size_t outlen, total_len; |
markrad | 0:cdf462088d13 | 474 | |
markrad | 0:cdf462088d13 | 475 | mbedtls_cipher_init( &ctx ); |
markrad | 0:cdf462088d13 | 476 | |
markrad | 0:cdf462088d13 | 477 | memset( key, 0x00, sizeof( key ) ); |
markrad | 0:cdf462088d13 | 478 | memset( iv, 0x00, sizeof( iv ) ); |
markrad | 0:cdf462088d13 | 479 | memset( cipher, 0x00, sizeof( cipher ) ); |
markrad | 0:cdf462088d13 | 480 | memset( clear, 0x00, sizeof( clear ) ); |
markrad | 0:cdf462088d13 | 481 | memset( ad, 0x00, sizeof( ad ) ); |
markrad | 0:cdf462088d13 | 482 | memset( tag, 0x00, sizeof( tag ) ); |
markrad | 0:cdf462088d13 | 483 | memset( output, 0x00, sizeof( output ) ); |
markrad | 0:cdf462088d13 | 484 | |
markrad | 0:cdf462088d13 | 485 | key_len = unhexify( key, hex_key ); |
markrad | 0:cdf462088d13 | 486 | iv_len = unhexify( iv, hex_iv ); |
markrad | 0:cdf462088d13 | 487 | cipher_len = unhexify( cipher, hex_cipher ); |
markrad | 0:cdf462088d13 | 488 | clear_len = unhexify( clear, hex_clear ); |
markrad | 0:cdf462088d13 | 489 | #if defined(MBEDTLS_GCM_C) |
markrad | 0:cdf462088d13 | 490 | ad_len = unhexify( ad, hex_ad ); |
markrad | 0:cdf462088d13 | 491 | tag_len = unhexify( tag, hex_tag ); |
markrad | 0:cdf462088d13 | 492 | #else |
markrad | 0:cdf462088d13 | 493 | ((void) hex_ad); |
markrad | 0:cdf462088d13 | 494 | ((void) hex_tag); |
markrad | 0:cdf462088d13 | 495 | #endif |
markrad | 0:cdf462088d13 | 496 | |
markrad | 0:cdf462088d13 | 497 | /* Prepare context */ |
markrad | 0:cdf462088d13 | 498 | TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, |
markrad | 0:cdf462088d13 | 499 | mbedtls_cipher_info_from_type( cipher_id ) ) ); |
markrad | 0:cdf462088d13 | 500 | TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key, 8 * key_len, MBEDTLS_DECRYPT ) ); |
markrad | 0:cdf462088d13 | 501 | #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING) |
markrad | 0:cdf462088d13 | 502 | if( pad_mode != -1 ) |
markrad | 0:cdf462088d13 | 503 | TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) ); |
markrad | 0:cdf462088d13 | 504 | #else |
markrad | 0:cdf462088d13 | 505 | (void) pad_mode; |
markrad | 0:cdf462088d13 | 506 | #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */ |
markrad | 0:cdf462088d13 | 507 | TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx, iv, iv_len ) ); |
markrad | 0:cdf462088d13 | 508 | TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx ) ); |
markrad | 0:cdf462088d13 | 509 | #if defined(MBEDTLS_GCM_C) |
markrad | 0:cdf462088d13 | 510 | TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx, ad, ad_len ) ); |
markrad | 0:cdf462088d13 | 511 | #endif |
markrad | 0:cdf462088d13 | 512 | |
markrad | 0:cdf462088d13 | 513 | /* decode buffer and check tag */ |
markrad | 0:cdf462088d13 | 514 | total_len = 0; |
markrad | 0:cdf462088d13 | 515 | TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx, cipher, cipher_len, output, &outlen ) ); |
markrad | 0:cdf462088d13 | 516 | total_len += outlen; |
markrad | 0:cdf462088d13 | 517 | TEST_ASSERT( finish_result == mbedtls_cipher_finish( &ctx, output + outlen, |
markrad | 0:cdf462088d13 | 518 | &outlen ) ); |
markrad | 0:cdf462088d13 | 519 | total_len += outlen; |
markrad | 0:cdf462088d13 | 520 | #if defined(MBEDTLS_GCM_C) |
markrad | 0:cdf462088d13 | 521 | TEST_ASSERT( tag_result == mbedtls_cipher_check_tag( &ctx, tag, tag_len ) ); |
markrad | 0:cdf462088d13 | 522 | #endif |
markrad | 0:cdf462088d13 | 523 | |
markrad | 0:cdf462088d13 | 524 | /* check plaintext only if everything went fine */ |
markrad | 0:cdf462088d13 | 525 | if( 0 == finish_result && 0 == tag_result ) |
markrad | 0:cdf462088d13 | 526 | { |
markrad | 0:cdf462088d13 | 527 | TEST_ASSERT( total_len == clear_len ); |
markrad | 0:cdf462088d13 | 528 | TEST_ASSERT( 0 == memcmp( output, clear, clear_len ) ); |
markrad | 0:cdf462088d13 | 529 | } |
markrad | 0:cdf462088d13 | 530 | |
markrad | 0:cdf462088d13 | 531 | exit: |
markrad | 0:cdf462088d13 | 532 | mbedtls_cipher_free( &ctx ); |
markrad | 0:cdf462088d13 | 533 | } |
markrad | 0:cdf462088d13 | 534 | /* END_CASE */ |
markrad | 0:cdf462088d13 | 535 | |
markrad | 0:cdf462088d13 | 536 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_AEAD */ |
markrad | 0:cdf462088d13 | 537 | void auth_crypt_tv( int cipher_id, char *hex_key, char *hex_iv, |
markrad | 0:cdf462088d13 | 538 | char *hex_ad, char *hex_cipher, |
markrad | 0:cdf462088d13 | 539 | char *hex_tag, char *hex_clear ) |
markrad | 0:cdf462088d13 | 540 | { |
markrad | 0:cdf462088d13 | 541 | int ret; |
markrad | 0:cdf462088d13 | 542 | unsigned char key[50]; |
markrad | 0:cdf462088d13 | 543 | unsigned char iv[50]; |
markrad | 0:cdf462088d13 | 544 | unsigned char cipher[200]; |
markrad | 0:cdf462088d13 | 545 | unsigned char clear[200]; |
markrad | 0:cdf462088d13 | 546 | unsigned char ad[200]; |
markrad | 0:cdf462088d13 | 547 | unsigned char tag[20]; |
markrad | 0:cdf462088d13 | 548 | unsigned char my_tag[20]; |
markrad | 0:cdf462088d13 | 549 | size_t key_len, iv_len, cipher_len, clear_len, ad_len, tag_len; |
markrad | 0:cdf462088d13 | 550 | mbedtls_cipher_context_t ctx; |
markrad | 0:cdf462088d13 | 551 | unsigned char output[200]; |
markrad | 0:cdf462088d13 | 552 | size_t outlen; |
markrad | 0:cdf462088d13 | 553 | |
markrad | 0:cdf462088d13 | 554 | mbedtls_cipher_init( &ctx ); |
markrad | 0:cdf462088d13 | 555 | |
markrad | 0:cdf462088d13 | 556 | memset( key, 0x00, sizeof( key ) ); |
markrad | 0:cdf462088d13 | 557 | memset( iv, 0x00, sizeof( iv ) ); |
markrad | 0:cdf462088d13 | 558 | memset( cipher, 0x00, sizeof( cipher ) ); |
markrad | 0:cdf462088d13 | 559 | memset( clear, 0x00, sizeof( clear ) ); |
markrad | 0:cdf462088d13 | 560 | memset( ad, 0x00, sizeof( ad ) ); |
markrad | 0:cdf462088d13 | 561 | memset( tag, 0x00, sizeof( tag ) ); |
markrad | 0:cdf462088d13 | 562 | memset( my_tag, 0xFF, sizeof( my_tag ) ); |
markrad | 0:cdf462088d13 | 563 | memset( output, 0xFF, sizeof( output ) ); |
markrad | 0:cdf462088d13 | 564 | |
markrad | 0:cdf462088d13 | 565 | key_len = unhexify( key, hex_key ); |
markrad | 0:cdf462088d13 | 566 | iv_len = unhexify( iv, hex_iv ); |
markrad | 0:cdf462088d13 | 567 | cipher_len = unhexify( cipher, hex_cipher ); |
markrad | 0:cdf462088d13 | 568 | ad_len = unhexify( ad, hex_ad ); |
markrad | 0:cdf462088d13 | 569 | tag_len = unhexify( tag, hex_tag ); |
markrad | 0:cdf462088d13 | 570 | |
markrad | 0:cdf462088d13 | 571 | /* Prepare context */ |
markrad | 0:cdf462088d13 | 572 | TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, |
markrad | 0:cdf462088d13 | 573 | mbedtls_cipher_info_from_type( cipher_id ) ) ); |
markrad | 0:cdf462088d13 | 574 | TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key, 8 * key_len, MBEDTLS_DECRYPT ) ); |
markrad | 0:cdf462088d13 | 575 | |
markrad | 0:cdf462088d13 | 576 | /* decode buffer and check tag */ |
markrad | 0:cdf462088d13 | 577 | ret = mbedtls_cipher_auth_decrypt( &ctx, iv, iv_len, ad, ad_len, |
markrad | 0:cdf462088d13 | 578 | cipher, cipher_len, output, &outlen, |
markrad | 0:cdf462088d13 | 579 | tag, tag_len ); |
markrad | 0:cdf462088d13 | 580 | |
markrad | 0:cdf462088d13 | 581 | /* make sure we didn't overwrite */ |
markrad | 0:cdf462088d13 | 582 | TEST_ASSERT( output[outlen + 0] == 0xFF ); |
markrad | 0:cdf462088d13 | 583 | TEST_ASSERT( output[outlen + 1] == 0xFF ); |
markrad | 0:cdf462088d13 | 584 | |
markrad | 0:cdf462088d13 | 585 | /* make sure the message is rejected if it should be */ |
markrad | 0:cdf462088d13 | 586 | if( strcmp( hex_clear, "FAIL" ) == 0 ) |
markrad | 0:cdf462088d13 | 587 | { |
markrad | 0:cdf462088d13 | 588 | TEST_ASSERT( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED ); |
markrad | 0:cdf462088d13 | 589 | goto exit; |
markrad | 0:cdf462088d13 | 590 | } |
markrad | 0:cdf462088d13 | 591 | |
markrad | 0:cdf462088d13 | 592 | /* otherwise, make sure it was decrypted properly */ |
markrad | 0:cdf462088d13 | 593 | TEST_ASSERT( ret == 0 ); |
markrad | 0:cdf462088d13 | 594 | |
markrad | 0:cdf462088d13 | 595 | clear_len = unhexify( clear, hex_clear ); |
markrad | 0:cdf462088d13 | 596 | TEST_ASSERT( outlen == clear_len ); |
markrad | 0:cdf462088d13 | 597 | TEST_ASSERT( memcmp( output, clear, clear_len ) == 0 ); |
markrad | 0:cdf462088d13 | 598 | |
markrad | 0:cdf462088d13 | 599 | /* then encrypt the clear and make sure we get the same ciphertext and tag */ |
markrad | 0:cdf462088d13 | 600 | memset( output, 0xFF, sizeof( output ) ); |
markrad | 0:cdf462088d13 | 601 | outlen = 0; |
markrad | 0:cdf462088d13 | 602 | |
markrad | 0:cdf462088d13 | 603 | ret = mbedtls_cipher_auth_encrypt( &ctx, iv, iv_len, ad, ad_len, |
markrad | 0:cdf462088d13 | 604 | clear, clear_len, output, &outlen, |
markrad | 0:cdf462088d13 | 605 | my_tag, tag_len ); |
markrad | 0:cdf462088d13 | 606 | TEST_ASSERT( ret == 0 ); |
markrad | 0:cdf462088d13 | 607 | |
markrad | 0:cdf462088d13 | 608 | TEST_ASSERT( outlen == clear_len ); |
markrad | 0:cdf462088d13 | 609 | TEST_ASSERT( memcmp( output, cipher, clear_len ) == 0 ); |
markrad | 0:cdf462088d13 | 610 | TEST_ASSERT( memcmp( my_tag, tag, tag_len ) == 0 ); |
markrad | 0:cdf462088d13 | 611 | |
markrad | 0:cdf462088d13 | 612 | /* make sure we didn't overwrite */ |
markrad | 0:cdf462088d13 | 613 | TEST_ASSERT( output[outlen + 0] == 0xFF ); |
markrad | 0:cdf462088d13 | 614 | TEST_ASSERT( output[outlen + 1] == 0xFF ); |
markrad | 0:cdf462088d13 | 615 | TEST_ASSERT( my_tag[tag_len + 0] == 0xFF ); |
markrad | 0:cdf462088d13 | 616 | TEST_ASSERT( my_tag[tag_len + 1] == 0xFF ); |
markrad | 0:cdf462088d13 | 617 | |
markrad | 0:cdf462088d13 | 618 | |
markrad | 0:cdf462088d13 | 619 | exit: |
markrad | 0:cdf462088d13 | 620 | mbedtls_cipher_free( &ctx ); |
markrad | 0:cdf462088d13 | 621 | } |
markrad | 0:cdf462088d13 | 622 | /* END_CASE */ |
markrad | 0:cdf462088d13 | 623 | |
markrad | 0:cdf462088d13 | 624 | /* BEGIN_CASE */ |
markrad | 0:cdf462088d13 | 625 | void test_vec_ecb( int cipher_id, int operation, char *hex_key, |
markrad | 0:cdf462088d13 | 626 | char *hex_input, char *hex_result, |
markrad | 0:cdf462088d13 | 627 | int finish_result ) |
markrad | 0:cdf462088d13 | 628 | { |
markrad | 0:cdf462088d13 | 629 | unsigned char key[50]; |
markrad | 0:cdf462088d13 | 630 | unsigned char input[16]; |
markrad | 0:cdf462088d13 | 631 | unsigned char result[16]; |
markrad | 0:cdf462088d13 | 632 | size_t key_len; |
markrad | 0:cdf462088d13 | 633 | mbedtls_cipher_context_t ctx; |
markrad | 0:cdf462088d13 | 634 | unsigned char output[32]; |
markrad | 0:cdf462088d13 | 635 | size_t outlen; |
markrad | 0:cdf462088d13 | 636 | |
markrad | 0:cdf462088d13 | 637 | mbedtls_cipher_init( &ctx ); |
markrad | 0:cdf462088d13 | 638 | |
markrad | 0:cdf462088d13 | 639 | memset( key, 0x00, sizeof( key ) ); |
markrad | 0:cdf462088d13 | 640 | memset( input, 0x00, sizeof( input ) ); |
markrad | 0:cdf462088d13 | 641 | memset( result, 0x00, sizeof( result ) ); |
markrad | 0:cdf462088d13 | 642 | memset( output, 0x00, sizeof( output ) ); |
markrad | 0:cdf462088d13 | 643 | |
markrad | 0:cdf462088d13 | 644 | /* Prepare context */ |
markrad | 0:cdf462088d13 | 645 | TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, |
markrad | 0:cdf462088d13 | 646 | mbedtls_cipher_info_from_type( cipher_id ) ) ); |
markrad | 0:cdf462088d13 | 647 | |
markrad | 0:cdf462088d13 | 648 | key_len = unhexify( key, hex_key ); |
markrad | 0:cdf462088d13 | 649 | TEST_ASSERT( unhexify( input, hex_input ) == |
markrad | 0:cdf462088d13 | 650 | (int) mbedtls_cipher_get_block_size( &ctx ) ); |
markrad | 0:cdf462088d13 | 651 | TEST_ASSERT( unhexify( result, hex_result ) == |
markrad | 0:cdf462088d13 | 652 | (int) mbedtls_cipher_get_block_size( &ctx ) ); |
markrad | 0:cdf462088d13 | 653 | |
markrad | 0:cdf462088d13 | 654 | TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key, 8 * key_len, operation ) ); |
markrad | 0:cdf462088d13 | 655 | |
markrad | 0:cdf462088d13 | 656 | TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx, input, |
markrad | 0:cdf462088d13 | 657 | mbedtls_cipher_get_block_size( &ctx ), |
markrad | 0:cdf462088d13 | 658 | output, &outlen ) ); |
markrad | 0:cdf462088d13 | 659 | TEST_ASSERT( outlen == mbedtls_cipher_get_block_size( &ctx ) ); |
markrad | 0:cdf462088d13 | 660 | TEST_ASSERT( finish_result == mbedtls_cipher_finish( &ctx, output + outlen, |
markrad | 0:cdf462088d13 | 661 | &outlen ) ); |
markrad | 0:cdf462088d13 | 662 | TEST_ASSERT( 0 == outlen ); |
markrad | 0:cdf462088d13 | 663 | |
markrad | 0:cdf462088d13 | 664 | /* check plaintext only if everything went fine */ |
markrad | 0:cdf462088d13 | 665 | if( 0 == finish_result ) |
markrad | 0:cdf462088d13 | 666 | TEST_ASSERT( 0 == memcmp( output, result, |
markrad | 0:cdf462088d13 | 667 | mbedtls_cipher_get_block_size( &ctx ) ) ); |
markrad | 0:cdf462088d13 | 668 | |
markrad | 0:cdf462088d13 | 669 | exit: |
markrad | 0:cdf462088d13 | 670 | mbedtls_cipher_free( &ctx ); |
markrad | 0:cdf462088d13 | 671 | } |
markrad | 0:cdf462088d13 | 672 | /* END_CASE */ |
markrad | 0:cdf462088d13 | 673 | |
markrad | 0:cdf462088d13 | 674 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_WITH_PADDING */ |
markrad | 0:cdf462088d13 | 675 | void set_padding( int cipher_id, int pad_mode, int ret ) |
markrad | 0:cdf462088d13 | 676 | { |
markrad | 0:cdf462088d13 | 677 | const mbedtls_cipher_info_t *cipher_info; |
markrad | 0:cdf462088d13 | 678 | mbedtls_cipher_context_t ctx; |
markrad | 0:cdf462088d13 | 679 | |
markrad | 0:cdf462088d13 | 680 | mbedtls_cipher_init( &ctx ); |
markrad | 0:cdf462088d13 | 681 | |
markrad | 0:cdf462088d13 | 682 | cipher_info = mbedtls_cipher_info_from_type( cipher_id ); |
markrad | 0:cdf462088d13 | 683 | TEST_ASSERT( NULL != cipher_info ); |
markrad | 0:cdf462088d13 | 684 | TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) ); |
markrad | 0:cdf462088d13 | 685 | |
markrad | 0:cdf462088d13 | 686 | TEST_ASSERT( ret == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) ); |
markrad | 0:cdf462088d13 | 687 | |
markrad | 0:cdf462088d13 | 688 | exit: |
markrad | 0:cdf462088d13 | 689 | mbedtls_cipher_free( &ctx ); |
markrad | 0:cdf462088d13 | 690 | } |
markrad | 0:cdf462088d13 | 691 | /* END_CASE */ |
markrad | 0:cdf462088d13 | 692 | |
markrad | 0:cdf462088d13 | 693 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */ |
markrad | 0:cdf462088d13 | 694 | void check_padding( int pad_mode, char *input_str, int ret, int dlen_check ) |
markrad | 0:cdf462088d13 | 695 | { |
markrad | 0:cdf462088d13 | 696 | mbedtls_cipher_info_t cipher_info; |
markrad | 0:cdf462088d13 | 697 | mbedtls_cipher_context_t ctx; |
markrad | 0:cdf462088d13 | 698 | unsigned char input[16]; |
markrad | 0:cdf462088d13 | 699 | size_t ilen, dlen; |
markrad | 0:cdf462088d13 | 700 | |
markrad | 0:cdf462088d13 | 701 | /* build a fake context just for getting access to get_padding */ |
markrad | 0:cdf462088d13 | 702 | mbedtls_cipher_init( &ctx ); |
markrad | 0:cdf462088d13 | 703 | cipher_info.mode = MBEDTLS_MODE_CBC; |
markrad | 0:cdf462088d13 | 704 | ctx.cipher_info = &cipher_info; |
markrad | 0:cdf462088d13 | 705 | |
markrad | 0:cdf462088d13 | 706 | TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) ); |
markrad | 0:cdf462088d13 | 707 | |
markrad | 0:cdf462088d13 | 708 | ilen = unhexify( input, input_str ); |
markrad | 0:cdf462088d13 | 709 | |
markrad | 0:cdf462088d13 | 710 | TEST_ASSERT( ret == ctx.get_padding( input, ilen, &dlen ) ); |
markrad | 0:cdf462088d13 | 711 | if( 0 == ret ) |
markrad | 0:cdf462088d13 | 712 | TEST_ASSERT( dlen == (size_t) dlen_check ); |
markrad | 0:cdf462088d13 | 713 | } |
markrad | 0:cdf462088d13 | 714 | /* END_CASE */ |