0

I'm trying to write a LoRaWAN library from scratch. Using the function mbedtls_cipher_cmac from mbedtls/cmac.h to generate the Join-Request frame. I get the error

undefined reference to `mbedtls_cipher_cmac'

I have attempted the solution in this post: AES-CMAC using mbedtls: undefined reference error by defining CONFIG_MBEDTLS_CMAC_C in my main.cpp file.
When I look in esp_config.h, the inactive region colorization (VSC feature) confirms that MBEDTLS_CMAC_C is now defined, however a clean build still turns up the undefined reference error.


I am aware that the linker cannot find the corresponding .cpp file that goes along with cmac.h, but here I am lost as I cannot find aes.cpp either, however it builds just fine. I have no idea where VSC stores the .cpp files. They appear to be built in, but a search in the PlatformIO libraries tab returns nothing. Here is my code (with fake appkey):

#define CONFIG_MBEDTLS_CMAC_C
#include <Arduino.h>
#include <mbedtls/cmac.h>

void setup() {
  static const uint8_t APPKEY[] = {0xB6, 0xB5, 0x3F, 0x4A, 0x16, 0x8A, 0x7A, 0x88, 0xBD, 0xF7, 0xEA, 0x13, 0x5C, 0xE9, 0xCF, 0xCA};
  uint8_t testPHYPayload[] = {0x00, 0xDC, 0x00, 0x00, 0xD0, 0x7E, 0xD5, 0xB3, 0x70, 0x1E, 0x6F, 0xED, 0xF5, 0x7C, 0xEE, 0xAF, 0x00, 0xC8, 0x86, 0x03, 0x0A, 0xF2, 0xC9};
  unsigned char cmac[100] = {0};
  const mbedtls_cipher_info_t* cipher_info;
  cipher_info = mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_128_ECB);
  mbedtls_cipher_cmac(cipher_info, APPKEY, 128, testPHYPayload, 19, cmac);
}

void loop() {
  
}
  • You need to define `MBEDTLS_CMAC_C` when building mbedtls, not just when building your own source file. You should specify what development kit you're using, then maybe someone who knows that development kit can help you. By the way, there's no `cmac.cpp`: it's `cmac.c`. – Gilles 'SO- stop being evil' Dec 12 '21 at 21:31
  • Thanks @Gilles'SO-stopbeingevil'. I am using VS Code with PlatformIO extension, programming an ESP32 DevModule using the Arduino framework. I did manage to find a workaround by copying the cmac.c file from mbedtls into my project, and adding `build_flags = -DCONFIG_MBEDTLS_CMAC_C` in the platformio.ini file. It seems that most of mbedtls is included in the Arduino framework, but not cmac.c. Here is a [link](https://community.platformio.org/t/mbedtls-linker-issues/16034) to the discussion in platformio forum. – Michael Ward Dec 14 '21 at 07:44

0 Answers0