2

I try to implement AES-CMAC using mbedTLS. I get some errors:

undefined reference to mbedtls_cipher_cmac_starts, undefined reference to mbedtls_cipher_cmac_update, undefined reference to mbedtls_cipher_cmac_finish,

Why could these functions not be resolved, even though mbedtls_cipher_init and mbedtls_cipher_setup could?

BTW. I implemented AES under the same project using mbedTLS with no problem. I use Eclipse Nano.

Here is my code:

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "openssl/evp.h"
#include "openssl/cmac.h"
#include "mbedtls/cmac.h"
#include "mbedtls/cipher.h"
using namespace std;
unsigned char key[16]={0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};

int main()
{
    unsigned char m[100],m_len=32;
    unsigned char out[16],out1[16],out2[16];
    size_t d_len;

    int i,ret;
    mbedtls_cipher_context_t m_ctx;
    const mbedtls_cipher_info_t *cipher_info;
    cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_AES_128_ECB );
    if(cipher_info==NULL)
        printf("\nmbedtls_cipher_info_from_type failed");

    mbedtls_cipher_init(&m_ctx);

    ret=mbedtls_cipher_setup( &m_ctx, cipher_info );
    printf("\n mbedtls_cipher_setup returned %d %d",ret,     m_ctx.cipher_info->type);



    ret=mbedtls_cipher_cmac_starts(&m_ctx,key,128);
    printf("\n mbedtls_cipher_cmac_starts returned %d",ret);

    ret= mbedtls_cipher_cmac_update(&m_ctx, m,m_len);
    printf("\n mbedtls_cipher_cmac_update returned %d",ret);

    ret=mbedtls_cipher_cmac_finish(&m_ctx,out1);
    printf("\n mbedtls_cipher_cmac_starts returned %d",ret);
    d_len=16;
    printf("\nLength is %d\n",(int)d_len);
    for(i=0;i<d_len;i++)
    {
        printf("%x ",out1[i]);
    }


    return 0;

}
evildead
  • 4,607
  • 4
  • 25
  • 49
malievci
  • 369
  • 1
  • 2
  • 6
  • Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Biffen Feb 01 '17 at 06:17
  • CAUTION: I corrected an error here ```cipher_info = mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_128_ECB);``` has to be ECB mode and _not_ CBC mode, c.f. https://tls.mbed.org/api/cmac_8h.html#ae5835d528bbfec2ae2452ba4617469b8 – evildead Jun 27 '19 at 10:09

1 Answers1

1

For whatever reason, CMAC is disabled in the default configuration. If other crypto functions are found, but not the CMAC functions, this must be because the CMAC functions weren't included in your build.

Edit config.h to uncomment #define MBEDTLS_CMAC_C and rebuild the library.

Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254