Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[baremetal] Add AES configuration for 128 bit keys #2890

Merged
merged 8 commits into from
Oct 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions configs/baremetal.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#define MBEDTLS_AES_C
#define MBEDTLS_AES_ROM_TABLES
#define MBEDTLS_AES_FEWER_TABLES
#define MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor - I think we could have had a shorter name here such as MBEDTLS_AES_128_ONLY, but not a big deal.

We should fix this in upstreaming.

#define MBEDTLS_CCM_C

/* Asymmetric crypto: Single-curve ECC only. */
Expand Down
4 changes: 4 additions & 0 deletions include/mbedtls/aes.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ typedef struct mbedtls_aes_context
{
int nr; /*!< The number of rounds. */
uint32_t *rk; /*!< AES round keys. */
#if defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH) && !defined(MBEDTLS_PADLOCK_C)
uint32_t buf[44]; /*!< Unaligned data buffer */
#else /* MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH */
uint32_t buf[68]; /*!< Unaligned data buffer. This buffer can
hold 32 extra Bytes, which can be used for
one of the following purposes:
Expand All @@ -95,6 +98,7 @@ typedef struct mbedtls_aes_context
<li>Simplifying key expansion in the 256-bit
case by generating an extra round key.
</li></ul> */
#endif /* MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH */
}
mbedtls_aes_context;

Expand Down
4 changes: 4 additions & 0 deletions include/mbedtls/check_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@
#error "MBEDTLS_CTR_DRBG_C defined, but not all prerequisites"
#endif

#if defined(MBEDTLS_CTR_DRBG_C) && defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH) && !defined(MBEDTLS_CTR_DRBG_USE_128_BIT_KEY)
#error "MBEDTLS_CTR_DRBG_C and MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH defined, but MBEDTLS_CTR_DRBG_USE_128_BIT_KEY is not defined"
#endif

#if defined(MBEDTLS_DHM_C) && !defined(MBEDTLS_BIGNUM_C)
#error "MBEDTLS_DHM_C defined, but not all prerequisites"
#endif
Expand Down
19 changes: 19 additions & 0 deletions include/mbedtls/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,25 @@
*/
//#define MBEDTLS_AES_FEWER_TABLES

/**
* \def MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH
*
* Use only 128-bit keys in AES operations to save ROM.
*
* Uncommenting this macro removes support for AES operations that are using 192
* or 256-bit keys.
*
* Tradeoff: Uncommenting this macro reduces ROM footprint by ~240 bytes.
*
* If uncommented, uncomment also MBEDTLS_CTR_DRBG_USE_128_BIT_KEY
*
* Module: library/aes.c
*
* Requires: MBEDTLS_AES_C
*
*/
//#define MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH

/**
* \def MBEDTLS_CAMELLIA_SMALL_MEMORY
*
Expand Down
44 changes: 43 additions & 1 deletion library/aes.c
Original file line number Diff line number Diff line change
Expand Up @@ -565,8 +565,10 @@ int mbedtls_aes_setkey_enc( mbedtls_aes_context *ctx, const unsigned char *key,
switch( keybits )
{
case 128: ctx->nr = 10; break;
#if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)
case 192: ctx->nr = 12; break;
case 256: ctx->nr = 14; break;
#endif /* !MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH */
default : return( MBEDTLS_ERR_AES_INVALID_KEY_LENGTH );
}

Expand Down Expand Up @@ -615,7 +617,7 @@ int mbedtls_aes_setkey_enc( mbedtls_aes_context *ctx, const unsigned char *key,
RK[7] = RK[3] ^ RK[6];
}
break;

#if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)
case 12:

for( i = 0; i < 8; i++, RK += 6 )
Expand Down Expand Up @@ -659,6 +661,7 @@ int mbedtls_aes_setkey_enc( mbedtls_aes_context *ctx, const unsigned char *key,
RK[15] = RK[7] ^ RK[14];
}
break;
#endif /* !MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH */
}

return( 0 );
Expand Down Expand Up @@ -1825,6 +1828,14 @@ int mbedtls_aes_self_test( int verbose )
mbedtls_printf( " AES-ECB-%3d (%s): ", keybits,
( mode == MBEDTLS_AES_DECRYPT ) ? "dec" : "enc" );

#if defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)
if( keybits > 128 )
{
mbedtls_printf( "skipped\n" );
continue;
}
#endif

mbedtls_platform_memset( buf, 0, 16 );

if( mode == MBEDTLS_AES_DECRYPT )
Expand Down Expand Up @@ -1887,6 +1898,14 @@ int mbedtls_aes_self_test( int verbose )
mbedtls_printf( " AES-CBC-%3d (%s): ", keybits,
( mode == MBEDTLS_AES_DECRYPT ) ? "dec" : "enc" );

#if defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)
if( keybits > 128 )
{
mbedtls_printf( "skipped\n" );
continue;
}
#endif

mbedtls_platform_memset( iv , 0, 16 );
mbedtls_platform_memset( prv, 0, 16 );
mbedtls_platform_memset( buf, 0, 16 );
Expand Down Expand Up @@ -1962,6 +1981,14 @@ int mbedtls_aes_self_test( int verbose )
mbedtls_printf( " AES-CFB128-%3d (%s): ", keybits,
( mode == MBEDTLS_AES_DECRYPT ) ? "dec" : "enc" );

#if defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)
if( keybits > 128 )
{
mbedtls_printf( "skipped\n" );
continue;
}
#endif

memcpy( iv, aes_test_cfb128_iv, 16 );
memcpy( key, aes_test_cfb128_key[u], keybits / 8 );

Expand Down Expand Up @@ -2025,6 +2052,13 @@ int mbedtls_aes_self_test( int verbose )
mbedtls_printf( " AES-OFB-%3d (%s): ", keybits,
( mode == MBEDTLS_AES_DECRYPT ) ? "dec" : "enc" );

#if defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)
if( keybits > 128 )
{
mbedtls_printf( "skipped\n" );
continue;
}
#endif
memcpy( iv, aes_test_ofb_iv, 16 );
memcpy( key, aes_test_ofb_key[u], keybits / 8 );

Expand Down Expand Up @@ -2087,6 +2121,14 @@ int mbedtls_aes_self_test( int verbose )
mbedtls_printf( " AES-CTR-128 (%s): ",
( mode == MBEDTLS_AES_DECRYPT ) ? "dec" : "enc" );

#if defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)
if( keybits > 128 )
{
mbedtls_printf( "skipped\n" );
continue;
}
#endif

memcpy( nonce_counter, aes_test_ctr_nonce_counter[u], 16 );
memcpy( key, aes_test_ctr_key[u], 16 );

Expand Down
6 changes: 6 additions & 0 deletions library/aesni.c
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ static void aesni_setkey_enc_128( unsigned char *rk,
/*
* Key expansion, 192-bit case
*/
#if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)
static void aesni_setkey_enc_192( unsigned char *rk,
const unsigned char *key )
{
Expand Down Expand Up @@ -380,10 +381,12 @@ static void aesni_setkey_enc_192( unsigned char *rk,
: "r" (rk), "r" (key)
: "memory", "cc", "0" );
}
#endif /* !MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH */

/*
* Key expansion, 256-bit case
*/
#if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)
static void aesni_setkey_enc_256( unsigned char *rk,
const unsigned char *key )
{
Expand Down Expand Up @@ -446,6 +449,7 @@ static void aesni_setkey_enc_256( unsigned char *rk,
: "r" (rk), "r" (key)
: "memory", "cc", "0" );
}
#endif /* !MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH */

/*
* Key expansion, wrapper
Expand All @@ -457,8 +461,10 @@ int mbedtls_aesni_setkey_enc( unsigned char *rk,
switch( bits )
{
case 128: aesni_setkey_enc_128( rk, key ); break;
#if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)
case 192: aesni_setkey_enc_192( rk, key ); break;
case 256: aesni_setkey_enc_256( rk, key ); break;
#endif /* !MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH */
default : return( MBEDTLS_ERR_AES_INVALID_KEY_LENGTH );
}

Expand Down
8 changes: 8 additions & 0 deletions library/gcm.c
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,14 @@ int mbedtls_gcm_self_test( int verbose )
mbedtls_printf( " AES-GCM-%3d #%d (%s): ",
key_len, i, "enc" );

#if defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)
if( key_len > 128 )
{
mbedtls_printf( "skipped\n" );
continue;
}
#endif /* MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH */

ret = mbedtls_gcm_setkey( &ctx, cipher, key[key_index[i]],
key_len );
/*
Expand Down
3 changes: 3 additions & 0 deletions library/version_features.c
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,9 @@ static const char *features[] = {
#if defined(MBEDTLS_AES_FEWER_TABLES)
"MBEDTLS_AES_FEWER_TABLES",
#endif /* MBEDTLS_AES_FEWER_TABLES */
#if defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)
"MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH",
#endif /* MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH */
#if defined(MBEDTLS_CAMELLIA_SMALL_MEMORY)
"MBEDTLS_CAMELLIA_SMALL_MEMORY",
#endif /* MBEDTLS_CAMELLIA_SMALL_MEMORY */
Expand Down
8 changes: 8 additions & 0 deletions programs/ssl/query_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,14 @@ int query_config( const char *config )
}
#endif /* MBEDTLS_AES_FEWER_TABLES */

#if defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)
if( strcmp( "MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH", config ) == 0 )
{
MACRO_EXPANSION_TO_STR( MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH );
return( 0 );
}
#endif /* MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH */

#if defined(MBEDTLS_CAMELLIA_SMALL_MEMORY)
if( strcmp( "MBEDTLS_CAMELLIA_SMALL_MEMORY", config ) == 0 )
{
Expand Down
2 changes: 2 additions & 0 deletions scripts/config.pl
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
# MBEDTLS_PKCS11_C
# MBEDTLS_NO_UDBL_DIVISION
# MBEDTLS_NO_64BIT_MULTIPLICATION
# MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH
# and any symbol beginning _ALT
#

Expand Down Expand Up @@ -126,6 +127,7 @@
MBEDTLS_NO_UDBL_DIVISION
MBEDTLS_NO_64BIT_MULTIPLICATION
MBEDTLS_USE_TINYCRYPT
MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH
_ALT\s*$
);

Expand Down
12 changes: 12 additions & 0 deletions tests/scripts/all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1263,6 +1263,18 @@ component_test_aes_rom_tables () {
make test
}

component_test_aes_only_128_bit_keys () {
msg "build: default config with AES_ONLY_128_BIT_KEY_LENGTH enabled"
scripts/config.pl set MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH
scripts/config.pl set MBEDTLS_CTR_DRBG_USE_128_BIT_KEY
scripts/config.pl unset MBEDTLS_PADLOCK_C

make CC=gcc CFLAGS='-Werror -O1'

msg "test: AES_ONLY_128_BIT_KEY_LENGTH"
make test
}

component_test_aes_fewer_tables_and_rom_tables () {
msg "build: default config with AES_ROM_TABLES and AES_FEWER_TABLES enabled"
scripts/config.pl set MBEDTLS_AES_FEWER_TABLES
Expand Down
Loading