6 years, 11 months ago.

problem with aes-ctr in mbedtls

Hi everyone,

I'm trying to run aes-ctr in mbedtls on the development board nrf51dk in mbed but the there is something strange with the linking that I don't understand.

My guess is that is have to something with the ifguards in "mbedtls/aes.h" because I have no problem with mbedtls_aes_context, mbedtls_aes_init and mbedtls_aes_setkey_enc.

However I tried to define the macros in the user application as well without no progress, the code can be viewed below. Furthermore, when I use the the ifguards to program compiles and build successfully but the code inside the ifguards never get executed and if I define the macros I get linking errors.

include the mbed library with this snippet

#include "mbed.h"
#if DEBUG_LEVEL > 0
#include "mbedtls/debug.h"
#endif

#if !defined(MBEDTLS_CONFIG_FILE)
#include "mbedtls/config.h"
#else
#include MBEDTLS_CONFIG_FILE
#endif

#include "mbedtls/platform.h"

#include <string.h>

#include "mbedtls/md4.h"
#include "mbedtls/md5.h"
#include "mbedtls/ripemd160.h"
#include "mbedtls/sha1.h"
#include "mbedtls/sha256.h"
#include "mbedtls/sha512.h"
#include "mbedtls/arc4.h"
#include "mbedtls/des.h"
#include "mbedtls/aes.h"
#include "mbedtls/cmac.h"
#include "mbedtls/blowfish.h"
#include "mbedtls/camellia.h"
#include "mbedtls/gcm.h"
#include "mbedtls/ccm.h"
#include "mbedtls/havege.h"
#include "mbedtls/ctr_drbg.h"
#include "mbedtls/hmac_drbg.h"
#include "mbedtls/rsa.h"
#include "mbedtls/pk.h"
#include "mbedtls/dhm.h"
#include "mbedtls/ecdsa.h"
#include "mbedtls/ecdh.h"
#include "mbedtls/error.h"

#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
#include "mbedtls/memory_buffer_alloc.h"
#endif

#define MEM_BLOCK_OVERHEAD  ( 2 * sizeof( size_t ) )

/*
 * Size to use for the malloc buffer if MEMORY_BUFFER_ALLOC_C is defined.
 */
#define HEAP_SIZE       (1u << 16)  // 64k

#define BUFSIZE         1024
#define HEADER_FORMAT   "  %-24s :  "
#define TITLE_LEN       25

#define OPTIONS                                                           \
    "md4, md5, ripemd160, sha1, sha256, sha512,\r\n"                      \
    "arc4, camellia, blowfish,\r\n"                                       \
    "des3, des, aes_cmac, des3_cmac, aes_cbc, aes_gcm, aes_ccm, aes_ctr \r\n"      \
    "havege, ctr_drbg, hmac_drbg\r\n"                                     \
    "rsa, dhm, ecdsa, ecdh.\r\n"

#if defined(MBEDTLS_ERROR_C)
#define PRINT_ERROR                                            \
        mbedtls_strerror( ret, ( char * )tmp, sizeof( tmp ) ); \
        mbedtls_printf( "FAILED: %s\r\n", tmp );
#else
#define PRINT_ERROR                                    \
        mbedtls_printf( "FAILED: -0x%04x\r\n", -ret );
#endif

DigitalOut led1(LED1);
DigitalOut led4(LED4);

int main(void) {
    led1 = 0;
    led4 = 0;
#if defined(MBEDTLS_AES_C)
#if defined(MBEDTLS_CIPHER_MODE_CTR)
    led4 = 1;
    unsigned char key[16] = {
    0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7,
    0x15, 0x88,
    0x09, 0xcf, 0x4f, 0x3c
  };

    unsigned char ctr[16] = {
    0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9,
    0xfa, 0xfb,
    0xfc, 0xfd, 0xfe, 0xff
  };

  const unsigned char plaintext[64] = {
    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
  };

  size_t nc_off = 0;
  unsigned char stream_block[16] = {0};
  uint8_t enc_out[64];
  memset(enc_out, 0, sizeof(enc_out));

  mbedtls_aes_context aes;
  mbedtls_aes_init(&aes);
  mbedtls_aes_setkey_enc( &aes, key, 128);
  
  for(int i = 0; i < 10; i++) {
    mbedtls_aes_crypt_ctr(&aes, sizeof(plaintext), &nc_off, ctr, stream_block, plaintext, enc_out);
  }
  mbedtls_aes_free(&aes);
#endif
#endif
}

Grateful for help.

/Niklas A

Be the first to answer this question.