Mbed TLS is now part of TrustedFirmware.org.

Errors while trying to compile mbedtls for LPC1768


Nov 11, 2015 04:08
Richard Glover

My platform is the mbed LPC1768 and i am using the online mbed compiler to compile the source code. I'm working from: generate-an-aes-key and encrypt-with-aes-cbc

My goal is to generate an AES256 key and encrypt/decrypt my data. Then output the encrypted data and decrypted data to a serial terminal such as termite or teraterm. Eventually I would like to add HMAC, but for right now, I am just trying to get the hang of the basics.

Errors: Error: Too many arguments in function call "if( ( ret = mbedtls_ctr_drbg_init( &ctr_drbg, mbedtls_entropy_func, &entropy,"

Error: A value of type "void" cannot be assigned to an entity of type "int" "if( ( ret = mbedtls_ctr_drbg_init( &ctr_drbg, mbedtls_entropy_func, &entropy,"

Error: Identifier "AES_ENCRYPT" is undefined "mbedtls_aes_crypt_cbc( &aes, AES_ENCRYPT, 24, iv, input, output );"

Code:

#include "mbed.h"
#include "mbedtls/config.h"
#include "mbedtls/platform.h"
#include "mbedtls/aes.h"
#include "mbedtls/entropy.h"
#include "mbedtls/ctr_drbg.h"

DigitalOut myled(LED1);

mbedtls_aes_context aes;
mbedtls_ctr_drbg_context ctr_drbg;
mbedtls_entropy_context entropy;

//aes_init(&aes);

Serial pc(USBTX, USBRX);


unsigned char key[32];

unsigned char iv[16]  = { 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31,
                          0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31 };

unsigned char input[128] = { 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
                             0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
                             0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
                             0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
                             0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11,
                             0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
                             0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17,
                             0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10 };
unsigned char output[128];

size_t input_len = 40;
size_t output_len = 0;

int main() {
    pc.baud(115200);

    char *pers = "aes generate key";
    int ret;

    mbedtls_entropy_init( &entropy );
    if( ( ret = mbedtls_ctr_drbg_init( &ctr_drbg, mbedtls_entropy_func, &entropy,
        (unsigned char *) pers, strlen( pers ) ) ) != 0 )
    {
        printf( " failed\n ! mbedtls_ctr_drbg_init returned -0x%04x\n", -ret );
    }

    if( ( ret = mbedtls_ctr_drbg_random( &ctr_drbg, key, 32 ) ) != 0 )
    {
        printf( " failed\n ! mbedtls_ctr_drbg_random returned -0x%04x\n", -ret );
    }

    mbedtls_aes_setkey_enc( &aes, key, 256 );
    mbedtls_aes_crypt_cbc( &aes, AES_ENCRYPT, 24, iv, input, output );

    pc.printf("\r\n\r\nFirst run\r\n");
}
 
Apr 9, 2017 18:29
Jose Henrique Pimentel

Did you solve the problem?

 
Apr 12, 2017 13:28
Ron Eldor

Hi Richard and Jose,
The link regarding random generation is out of date, and we will fix it.
Please note the signature of mbedtls_ctr_drbg_init:

void mbedtls_ctr_drbg_init( mbedtls_ctr_drbg_context *ctx );

and of mbedtls_aes_crypt_cbc:

int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx,
                    int mode,
                    size_t length,
                    unsigned char iv[16],
                    const unsigned char *input,
                    unsigned char *output );

AES_ENCRYPT has been changed to MBEDTLS_AES_ENCRYPT , and CTR DRBG should be handled a bit different. Please look at the following example:

 mbedtls_ctr_drbg_init( &ctr_drbg );
ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy, (const unsigned char *) "RANDOM_GEN", 10 );
    if( ret != 0 )
    {
        mbedtls_printf( "failed in mbedtls_ctr_drbg_seed: %d\n", ret );
        goto cleanup;
    }
mbedtls_ctr_drbg_set_prediction_resistance( &ctr_drbg, MBEDTLS_CTR_DRBG_PR_OFF );
 ret = mbedtls_ctr_drbg_random( &ctr_drbg, buf, sizeof( buf ) );

of course, "RANDOM_GEN" should be replaced with your additional string (pers)
I apologize for mistake in knowledge base
I hope this answers your questions
Regards,
mbed TLS Team member
Ron