Arm Mbed and Pelion Device Management support forum

Trouble compiling

Hey guys,

I’m working on a project with mbedtls for the first time and need to make a file that can encrypt text using mbedtls.
I am using AES encryption with the gcm files (gcm.h and gcm.c files).
I am working on a Raspberry pi 4(the latest version with 4 cores).
Unfortunately, my code can’t compile :confused:
I haven’t found a problem with my code but when I try to compile it gives an error message that from my understanding means that the #include I added didn’t work:
Error Msg:

/usr/bin/ld: /tmp/ccaOvpzu.o: in function main': RoeeTestFile.c:(.text.startup+0x1c): undefined reference to mbedtls_gcm_init’
/usr/bin/ld: RoeeTestFile.c:(.text.startup+0x30): undefined reference to mbedtls_gcm_setkey' /usr/bin/ld: RoeeTestFile.c:(.text.startup+0x7c): undefined reference to mbedtls_gcm_crypt_and_tag’
/usr/bin/ld: RoeeTestFile.c:(.text.startup+0x8c): undefined reference to `mbedtls_gcm_free’
collect2: error: ld returned 1 exit status
make: *** [: test/RoeeTestFile] Error 1

My code:

#include <stdint.h>

#include “…/crypto/include/mbedtls/cipher.h”

#if !defined(MBEDTLS_CONFIG_FILE)

#include “mbedtls/config.h”

#else

#include MBEDTLS_CONFIG_FILE

#endif

#if defined(MBEDTLS_PLATFORM_C)

#include “mbedtls/platform.h”

#else

#include <stdio.h>

#define mbedtls_printf printf

#endif

#if defined(MBEDTLS_GCM_C)

#include “mbedtls/gcm.h”

#endif

#if defined(MBEDTLS_ENTROPY_C)

#include “mbedtls/entropy.h”

#endif

#if defined(MBEDTLS_SHA256_C)

#include “mbedtls/sha256.h”

#endif

#if defined(MBEDTLS_CTR_DRBG_C)

#include “mbedtls/ctr_drbg.h”

#endif

#include <string.h>

#include “mbedtls/error.h”

#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)

#include “mbedtls/memory_buffer_alloc.h”

#endif

/*

  • This file will Encrypt using the “gcm.h” file

*/

#define BUFSIZE 1024

#define keySize 128

#define tagSize 200

unsigned char bufInput[BUFSIZE] = “Hello World”;

unsigned char bufOutput[BUFSIZE];

unsigned char tag[tagSize] = “BCDEFGHIJKLMNOPQRSTUVWXYZBCDEFGHIJKLMNOPQRSTUVWXYZBCDEFGHIJKLMNOPQRSTUVWXYZBCDEFGHIJKLMNOPQRSTUVWXYZBCDEFGHIJKLMNOPQRSTUVWXYZBCD”;

int main() {

#if defined(MBEDTLS_GCM_C)

unsigned char ta[] = "123";

unsigned char* t = ta;

//BufInput = "Hello World";

//tag = "BCDEFGHIJKLMNOPQRSTUVWXYZBCDEFGHIJKLMNOPQRSTUVWXYZBCDEFGHIJKLMNOPQRSTUVWXYZBCDEFGHIJKLMNOPQRSTUVWXYZBCDEFGHIJKLMNOPQRSTUVWXYZBCD";

//int keySize = 128;

//key size

/*

 *need to initialise key size

 * temp value 128

*/

unsigned char key[128] = "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWX";

int ivLength = 200;

unsigned char iv[ivLength];

//declaring the context struct

mbedtls_gcm_context gcmContext;

//initializing the gcmContext

mbedtls_gcm_init(& gcmContext);

//associates the gcmContext with a cipher algorithm(AES) and a key

if(mbedtls_gcm_setkey(&gcmContext,  MBEDTLS_CIPHER_ID_AES, key/*insert the key here */, keySize) != 0) {

    printf( "setkey failed :(");

}

if(mbedtls_gcm_crypt_and_tag(&gcmContext, MBEDTLS_GCM_ENCRYPT, BUFSIZE, iv, ivLength/*iv length */, NULL /*add - idki what this is */, 0/*add length */, bufInput, bufOutput, tagSize, tag)!= 0) {

    printf("Encrypting failed :(");

}

//clearing the gcmContext

mbedtls_gcm_free(&gcmContext);

//printing Original text

printf("\t\tPrint Original: %s", bufInput);

//printing encrypted text

printf("\t\tPrint Encrypted: %s", bufOutput);

#endif

#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)

mbedtls_memory_buffer_alloc_free();

#endif

return( 0 );

}

I edited a makefile that compiled other files in the same directory as my current project file.
My cMake file:

# To compile on SunOS: add "-lsocket -lnsl" to LDFLAGS
# To compile with PKCS11: add "-lpkcs11-helper" to LDFLAGS

CFLAGS	?= -O2
WARNING_CFLAGS ?= -Wall -Wextra
WARNING_CXXFLAGS ?= -Wall -Wextra
LDFLAGS ?=

LOCAL_CFLAGS = $(WARNING_CFLAGS) -I../include -D_FILE_OFFSET_BITS=64
LOCAL_CXXFLAGS = $(WARNING_CXXFLAGS) -I../include -D_FILE_OFFSET_BITS=64
LOCAL_LDFLAGS = -L../library 			\
		-lmbedtls$(SHARED_SUFFIX)	\
		-lmbedx509$(SHARED_SUFFIX)	\
		-lmbedcrypto$(SHARED_SUFFIX)

LOCAL_LDFLAGS += -L../crypto/library
LOCAL_CFLAGS += -I../crypto/include
LOCAL_CXXFLAGS += -I../crypto/include

INCLUDING_FROM_MBEDTLS:=1
include ../crypto/3rdparty/Makefile.inc
LOCAL_CFLAGS += $(patsubst -I../3rdparty/%, -I../crypto/3rdparty/%, $(THIRDPARTY_INCLUDES))
LOCAL_CFLAGS += $(patsubst -I../3rdparty/%, -I../crypto/3rdparty/%, $(THIRDPARTY_INCLUDES))

ifdef DEBUG
LOCAL_CFLAGS += -g3
endif

# if we're running on Windows, build for Windows
ifdef WINDOWS
WINDOWS_BUILD=1
endif

ifdef WINDOWS_BUILD
DLEXT=dll
EXEXT=.exe
LOCAL_LDFLAGS += -lws2_32
ifdef SHARED
SHARED_SUFFIX=.$(DLEXT)
endif
else
DLEXT ?= so
EXEXT=
SHARED_SUFFIX=
endif

ifndef SHARED
DEP=../crypto/library/libmbedcrypto.a ../library/libmbedx509.a ../library/libmbedtls.a
else
DEP=../crypto/library/libmbedcrypto.$(DLEXT) ../library/libmbedx509.$(DLEXT) ../library/libmbedtls.$(DLEXT)
endif

# Zlib shared library extensions:
ifdef ZLIB
LOCAL_LDFLAGS += -lz
endif

APPS = \
	test/RoeeTestFile$(EXEXT) \
# End of APPS

ifdef PTHREAD
APPS +=	ssl/ssl_pthread_server$(EXEXT)
endif

ifdef TEST_CPP
APPS += test/cpp_dummy_build$(EXEXT)
endif

.SILENT:

.PHONY: all clean list fuzz

all: $(APPS)
ifndef WINDOWS
# APPS doesn't include the fuzzing programs, which aren't "normal"
# sample or test programs, and don't build with MSVC which is
# warning about fopen
all: fuzz
endif

fuzz:
	$(MAKE) -C fuzz THIRDPARTY_INCLUDES=$(THIRDPARTY_INCLUDES)

$(DEP):
	$(MAKE) -C ../library
/root/mbedtls/programs/test/RoeeTestFile$(EXEXT): /root/mbedtls/programs/test/RoeeTestFile.c$(DEP)
	echo "	CC	test/RoeeTestFile.c "
	$(CC) $(LOCAL_CFLAGS) $(CFLAGS) /root/mbedtls/programs/test/RoeeTestFile.c$(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
#EXAMPLE:
#./myfile$(EXEXT):  ./myfile.c $(DEP)
#	echo "	CC	./myfile.c"
#	$(CC) $(LOCAL_CFLAGS) $(CFLAGS)  ./myfile.c $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
# END OF EXAMPLE SECTION

clean:
ifndef WINDOWS
	rm -f $(APPS)
	-rm -f ssl/ssl_pthread_server$(EXEXT)
	-rm -f test/cpp_dummy_build$(EXEXT)
else
	if exist *.o del /Q /F *.o
	if exist *.exe del /Q /F *.exe
endif
	$(MAKE) -C fuzz clean

list:
	echo $(APPS)

Hi @phantomroe
As mentioned in this anouncement, Mbed TLS is now maintained under open governance at TrustedFirmware.org.
I would suggest you post your question there, as it doesn’t seem your question is Pelion related.

At a glance, your issue is not a compile time error, but a link time error, so this is not a missing #include in your code. This is because during link time, the gcm symbols were not compiled. Do you have MBEDTLS_GCM_C defined in your configuration?
Regards,
Mbed Support
Ron