Skip to content

Commit dfa9c7f

Browse files
committed
Updates mbed TLS to the current development head
1 parent 715dd5b commit dfa9c7f

File tree

11 files changed

+72
-43
lines changed

11 files changed

+72
-43
lines changed

core/mbedtls/VERSION.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
yotta-2.3.0-144-g9fa2e86d93b9b6e04c0a797b34aaf7b6066fbb25
1+
yotta-2.3.0-156-g184eea6aa06aa4fadd22b5eaad0aa729156d83d9

core/mbedtls/inc/mbedtls/cipher.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
#define MBEDTLS_ERR_CIPHER_INVALID_PADDING -0x6200 /**< Input data contains invalid padding and is rejected. */
5858
#define MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED -0x6280 /**< Decryption of block requires a full block. */
5959
#define MBEDTLS_ERR_CIPHER_AUTH_FAILED -0x6300 /**< Authentication failed (for AEAD modes). */
60+
#define MBEDTLS_ERR_CIPHER_INVALID_CONTEXT -0x6380 /**< The context is invalid, eg because it was free()ed. */
6061

6162
#define MBEDTLS_CIPHER_VARIABLE_IV_LEN 0x01 /**< Cipher accepts IVs of variable length */
6263
#define MBEDTLS_CIPHER_VARIABLE_KEY_LEN 0x02 /**< Cipher accepts keys of variable length */

core/mbedtls/src/base64.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ int mbedtls_base64_encode( unsigned char *dst, size_t dlen, size_t *olen,
9797

9898
n *= 4;
9999

100-
if( dlen < n + 1 )
100+
if( ( dlen < n + 1 ) || ( NULL == dst ) )
101101
{
102102
*olen = n + 1;
103103
return( MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL );

core/mbedtls/src/camellia.c

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -963,38 +963,38 @@ int mbedtls_camellia_self_test( int verbose )
963963
mbedtls_printf( " CAMELLIA-CBC-%3d (%s): ", 128 + u * 64,
964964
( v == MBEDTLS_CAMELLIA_DECRYPT ) ? "dec" : "enc" );
965965

966-
memcpy( src, camellia_test_cbc_iv, 16 );
967-
memcpy( dst, camellia_test_cbc_iv, 16 );
968-
memcpy( key, camellia_test_cbc_key[u], 16 + 8 * u );
969-
970-
if( v == MBEDTLS_CAMELLIA_DECRYPT ) {
971-
mbedtls_camellia_setkey_dec( &ctx, key, 128 + u * 64 );
972-
} else {
973-
mbedtls_camellia_setkey_enc( &ctx, key, 128 + u * 64 );
974-
}
975-
976-
for( i = 0; i < CAMELLIA_TESTS_CBC; i++ ) {
966+
memcpy( src, camellia_test_cbc_iv, 16 );
967+
memcpy( dst, camellia_test_cbc_iv, 16 );
968+
memcpy( key, camellia_test_cbc_key[u], 16 + 8 * u );
977969

978970
if( v == MBEDTLS_CAMELLIA_DECRYPT ) {
979-
memcpy( iv , src, 16 );
980-
memcpy( src, camellia_test_cbc_cipher[u][i], 16 );
981-
memcpy( dst, camellia_test_cbc_plain[i], 16 );
982-
} else { /* MBEDTLS_CAMELLIA_ENCRYPT */
983-
memcpy( iv , dst, 16 );
984-
memcpy( src, camellia_test_cbc_plain[i], 16 );
985-
memcpy( dst, camellia_test_cbc_cipher[u][i], 16 );
971+
mbedtls_camellia_setkey_dec( &ctx, key, 128 + u * 64 );
972+
} else {
973+
mbedtls_camellia_setkey_enc( &ctx, key, 128 + u * 64 );
986974
}
987975

988-
mbedtls_camellia_crypt_cbc( &ctx, v, 16, iv, src, buf );
976+
for( i = 0; i < CAMELLIA_TESTS_CBC; i++ ) {
989977

990-
if( memcmp( buf, dst, 16 ) != 0 )
991-
{
992-
if( verbose != 0 )
993-
mbedtls_printf( "failed\n" );
978+
if( v == MBEDTLS_CAMELLIA_DECRYPT ) {
979+
memcpy( iv , src, 16 );
980+
memcpy( src, camellia_test_cbc_cipher[u][i], 16 );
981+
memcpy( dst, camellia_test_cbc_plain[i], 16 );
982+
} else { /* MBEDTLS_CAMELLIA_ENCRYPT */
983+
memcpy( iv , dst, 16 );
984+
memcpy( src, camellia_test_cbc_plain[i], 16 );
985+
memcpy( dst, camellia_test_cbc_cipher[u][i], 16 );
986+
}
994987

995-
return( 1 );
988+
mbedtls_camellia_crypt_cbc( &ctx, v, 16, iv, src, buf );
989+
990+
if( memcmp( buf, dst, 16 ) != 0 )
991+
{
992+
if( verbose != 0 )
993+
mbedtls_printf( "failed\n" );
994+
995+
return( 1 );
996+
}
996997
}
997-
}
998998

999999
if( verbose != 0 )
10001000
mbedtls_printf( "passed\n" );

core/mbedtls/src/cipher.c

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -252,17 +252,19 @@ int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *i
252252
size_t ilen, unsigned char *output, size_t *olen )
253253
{
254254
int ret;
255+
size_t block_size = 0;
255256

256257
if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
257258
{
258259
return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
259260
}
260261

261262
*olen = 0;
263+
block_size = mbedtls_cipher_get_block_size( ctx );
262264

263265
if( ctx->cipher_info->mode == MBEDTLS_MODE_ECB )
264266
{
265-
if( ilen != mbedtls_cipher_get_block_size( ctx ) )
267+
if( ilen != block_size )
266268
return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
267269

268270
*olen = ilen;
@@ -285,8 +287,13 @@ int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *i
285287
}
286288
#endif
287289

290+
if ( 0 == block_size )
291+
{
292+
return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
293+
}
294+
288295
if( input == output &&
289-
( ctx->unprocessed_len != 0 || ilen % mbedtls_cipher_get_block_size( ctx ) ) )
296+
( ctx->unprocessed_len != 0 || ilen % block_size ) )
290297
{
291298
return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
292299
}
@@ -300,9 +307,9 @@ int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *i
300307
* If there is not enough data for a full block, cache it.
301308
*/
302309
if( ( ctx->operation == MBEDTLS_DECRYPT &&
303-
ilen + ctx->unprocessed_len <= mbedtls_cipher_get_block_size( ctx ) ) ||
310+
ilen + ctx->unprocessed_len <= block_size ) ||
304311
( ctx->operation == MBEDTLS_ENCRYPT &&
305-
ilen + ctx->unprocessed_len < mbedtls_cipher_get_block_size( ctx ) ) )
312+
ilen + ctx->unprocessed_len < block_size ) )
306313
{
307314
memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
308315
ilen );
@@ -314,22 +321,22 @@ int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *i
314321
/*
315322
* Process cached data first
316323
*/
317-
if( ctx->unprocessed_len != 0 )
324+
if( 0 != ctx->unprocessed_len )
318325
{
319-
copy_len = mbedtls_cipher_get_block_size( ctx ) - ctx->unprocessed_len;
326+
copy_len = block_size - ctx->unprocessed_len;
320327

321328
memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
322329
copy_len );
323330

324331
if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
325-
ctx->operation, mbedtls_cipher_get_block_size( ctx ), ctx->iv,
332+
ctx->operation, block_size, ctx->iv,
326333
ctx->unprocessed_data, output ) ) )
327334
{
328335
return( ret );
329336
}
330337

331-
*olen += mbedtls_cipher_get_block_size( ctx );
332-
output += mbedtls_cipher_get_block_size( ctx );
338+
*olen += block_size;
339+
output += block_size;
333340
ctx->unprocessed_len = 0;
334341

335342
input += copy_len;
@@ -341,9 +348,14 @@ int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *i
341348
*/
342349
if( 0 != ilen )
343350
{
344-
copy_len = ilen % mbedtls_cipher_get_block_size( ctx );
351+
if( 0 == block_size )
352+
{
353+
return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
354+
}
355+
356+
copy_len = ilen % block_size;
345357
if( copy_len == 0 && ctx->operation == MBEDTLS_DECRYPT )
346-
copy_len = mbedtls_cipher_get_block_size( ctx );
358+
copy_len = block_size;
347359

348360
memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
349361
copy_len );

core/mbedtls/src/debug.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ void mbedtls_debug_print_msg( const mbedtls_ssl_context *ssl, int level,
8686
char str[DEBUG_BUF_SIZE];
8787
int ret;
8888

89-
if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold )
89+
if( NULL == ssl || NULL == ssl->conf || NULL == ssl->conf->f_dbg || level > debug_threshold )
9090
return;
9191

9292
va_start( argp, format );

core/mbedtls/src/ecp.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1827,7 +1827,9 @@ int mbedtls_ecp_gen_keypair_base( mbedtls_ecp_group *grp,
18271827
/* [M225] page 5 */
18281828
size_t b;
18291829

1830-
MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( d, n_size, f_rng, p_rng ) );
1830+
do {
1831+
MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( d, n_size, f_rng, p_rng ) );
1832+
} while( mbedtls_mpi_bitlen( d ) == 0);
18311833

18321834
/* Make sure the most significant bit is nbits */
18331835
b = mbedtls_mpi_bitlen( d ) - 1; /* mbedtls_mpi_bitlen is one-based */

core/mbedtls/src/error.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,8 @@ void mbedtls_strerror( int ret, char *buf, size_t buflen )
183183
mbedtls_snprintf( buf, buflen, "CIPHER - Decryption of block requires a full block" );
184184
if( use_ret == -(MBEDTLS_ERR_CIPHER_AUTH_FAILED) )
185185
mbedtls_snprintf( buf, buflen, "CIPHER - Authentication failed (for AEAD modes)" );
186+
if( use_ret == -(MBEDTLS_ERR_CIPHER_INVALID_CONTEXT) )
187+
mbedtls_snprintf( buf, buflen, "CIPHER - The context is invalid, eg because it was free()ed" );
186188
#endif /* MBEDTLS_CIPHER_C */
187189

188190
#if defined(MBEDTLS_DHM_C)

core/mbedtls/src/rsa.c

100755100644
File mode changed.

core/mbedtls/src/ssl_tls.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5773,7 +5773,7 @@ int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
57735773
{
57745774
mbedtls_ecjpake_role role;
57755775

5776-
if( ssl->handshake == NULL && ssl->conf == NULL )
5776+
if( ssl->handshake == NULL || ssl->conf == NULL )
57775777
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
57785778

57795779
if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )

core/mbedtls/src/x509_crt.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -970,7 +970,9 @@ int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain, const unsigned char *bu
970970
int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen )
971971
{
972972
int success = 0, first_error = 0, total_failed = 0;
973+
#if defined(MBEDTLS_PEM_PARSE_C)
973974
int buf_format = MBEDTLS_X509_FORMAT_DER;
975+
#endif
974976

975977
/*
976978
* Check for valid input
@@ -988,10 +990,12 @@ int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain, const unsigned char *buf, s
988990
{
989991
buf_format = MBEDTLS_X509_FORMAT_PEM;
990992
}
991-
#endif
992993

993994
if( buf_format == MBEDTLS_X509_FORMAT_DER )
994995
return mbedtls_x509_crt_parse_der( chain, buf, buflen );
996+
#else
997+
return mbedtls_x509_crt_parse_der( chain, buf, buflen );
998+
#endif
995999

9961000
#if defined(MBEDTLS_PEM_PARSE_C)
9971001
if( buf_format == MBEDTLS_X509_FORMAT_PEM )
@@ -1064,14 +1068,14 @@ int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain, const unsigned char *buf, s
10641068
success = 1;
10651069
}
10661070
}
1067-
#endif /* MBEDTLS_PEM_PARSE_C */
10681071

10691072
if( success )
10701073
return( total_failed );
10711074
else if( first_error )
10721075
return( first_error );
10731076
else
10741077
return( MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT );
1078+
#endif /* MBEDTLS_PEM_PARSE_C */
10751079
}
10761080

10771081
#if defined(MBEDTLS_FS_IO)
@@ -1353,6 +1357,14 @@ int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
13531357
p = buf;
13541358
n = size;
13551359

1360+
if( NULL == crt )
1361+
{
1362+
ret = mbedtls_snprintf( p, n, "\nCertificate is uninitialised!\n" );
1363+
MBEDTLS_X509_SAFE_SNPRINTF;
1364+
1365+
return( (int) ( size - n ) );
1366+
}
1367+
13561368
ret = mbedtls_snprintf( p, n, "%scert. version : %d\n",
13571369
prefix, crt->version );
13581370
MBEDTLS_X509_SAFE_SNPRINTF;

0 commit comments

Comments
 (0)