PolarSSL is now part of ARM Official announcement and rebranded as mbed TLS.

Minimal config for GCM AES decode - how to exclude encrypt functions?


Sep 27, 2017 18:11
Matthew

Hello. First off, hats off to the excellent library and documentation. I love the modular and configurable design.

I'm working with a super minimal config for use by a bootloader firmware updater... e.g. bootloader receives encrypted firmware image, decrypts it, and applies to the system.

I have the following in config.h :

#define MBEDTLS_PLATFORM_C
#define MBEDTLS_AES_C
#define MBEDTLS_GCM_C
#define MBEDTLS_CIPHER_C
#define MBEDTLS_MD_C
#define MBEDTLS_MD5_C

In this case, no encryption functions are needed. However, some are being compiled. mbedtls_aes_encrypt is the largest offender, to the tune of ~1.7k bytes, which is relatively significant here.

What would be the recommended approach to excluding any encrypt functionality?

 
Oct 1, 2017 11:17
Ron Eldor

Hi Matthew,
Unfortunately, it is not possible to opt out as part of the compilation the encryption part of the GCM.
However, you can add compilation and link flags to tell the linker to remove any unused function. If in fact mbedtls_aes_encrypt is not called in your binary, than the linker should remove it from the final image.
On GCC:

CFLAGS += -fdata-sections -ffunction-sections
LDFLAGS += -Wl,--gc-sections

Regards,
Mbed TLS Team member
Ron

 
Oct 2, 2017 21:34
Matthew

Hello Ron,

Thank you for the reply.

Unfortunately, in this case the encrypt/decrypt interface function(s) such as mbedtls_aes_crypt_ecb contian calls to both the encrypt or decrypt function, which are executed depending on the passed mode parameter, so the unused garbage-collect approach does not do the job.

    if( mode == MBEDTLS_AES_ENCRYPT )
        mbedtls_aes_encrypt( ctx, input, output );
    else
        mbedtls_aes_decrypt( ctx, input, output );

    return( 0 );
 
Oct 3, 2017 10:17
Ron Eldor

HI Matthew,
You are correct, confused between GCM encrypt (mbedtls_gcm_crypt_and_tag) , and the internal mbedtls_aes_encrypt functionality. However, note that GCM uses mbedtls_aes_encrypt so you will not be able to remove it.
Unfortunately, at the moment it is not possible to disable mbedtls_aes_decrypt functionality.
Regards,
Mbed TLS Team member
Ron