Search code examples
c++cryptographystatic-librariesstatic-linking

Unable to link tomsfastmath into libtomcrypt


I am writing a secure instant messaging program in c++ using the libtomcrypt C library for its RSA and SPRNG functions. I got libtomcrypt compiled as a static library and I have been able to link to it and run the sprng functions and see and use the random data it is generating.

The problem I am having is trying to use the rsa_make_key() function which has dependencies to a linked math library to function.

In this case I am trying to use Tomsfastmath (tfm) which I am also trying to link as a static library. Both of these libraries reside in their own project folder one directory up from my project folder (ie ../libtomcrypt)

In my code when I try to access the tomsfast math descriptor "tfm_desc" I get the error test_crypt.cpp:8:11: error: 'tfm_desc' was not declared in this scope. Which makes me think that tfm is not getting correctly linked into libtomcrypt. I have read the documentation for both of these things its not very clear.

I am at wits end here. What I am doing wrong?

Here is my make file

 CC:=gcc #C Compiler
 CFLAGS:=-std=c99 -O0 -I/home/k3rb3ros/csci484-CMU-/libtomcrypt-1.17/src/headers -g -     Wall -Wextra#C Compiler flags
 CPP:=g++ #C++ Compiler
 CPPFLAGS:=-std=gnu++0x -O0 -I/home/k3rb3ros/csci484/csci484-CMU-/libtomcrypt-  1.17/src/headers -L. -g -Wall -Wextra#C++ Compiler flags
 #CPPFLAGS:=-std=gnu++0x -O0 -g -Wall -Wextra #C++ Compiler flags
 LDFLAGS:= -lSDL -lSDL_net -ltfm -ltomcrypt
 CSOURCES= #C files used in this program
 CPPSOURCES=connection.cpp chat.cpp test_crypt.cpp #CPP files used in this prgram
 #COBJECTS=$(CSOURCES:.c=.o)libtfm.a libtomcrypt.a
 COBJECTS=$(CSOURCES:.c=.o)
 CPPOBJECTS=$(CPPSOURCES:.cpp=.o)
 BINARY=down_low

 all: $(BINARY) $(COBJECTS) $(CPPOBJECTS)
 .c.o:
      $(CC) $(CFLAGS) -c $< -o $@

 .cpp.o:
      $(CPP) $(CPPFLAGS) -c $< -o $@

  $(BINARY): $(COBJETS) $(CPPOBJECTS)
      $(CPP) $(CPPFLAGS) $(COBJECTS) $(CPPOBJECTS) -o $@ $(LDFLAGS)

  clean:
     rm -rv $(BINARY) $(COBJECTS) $(CPPOBJECTS)

and here is my test_crypt function

#include "headers/test_crypt.h"
using namespace std;

void test_crypt()
{
     int err = 0;
     int rng_idx = -1; //rng index, not sure if I need this
     ltc_mp = tfm_desc; //tell tomcrypt to use toms fast math
     rsa_key pub_key;
     prng_state random_gen;

     if((err = sprng_start(&random_gen)) != CRYPT_OK) //start the rng and check for errors
     {
         cout << "start error " << error_to_string(err) << endl;
     }

     rng_idx = find_prng("sprng");
     if((err = sprng_ready(&random_gen)) != CRYPT_OK)
     {
         cout << "Ready error " << error_to_string(err) << endl;
     }

     //test toms fast math present and working
     //fp_int test;
     //fp_init(&test);

     //sprng_read(entropy, size, &random_gen);

     /*
     if((err = rsa_make_key(NULL,           //PRNG state
                            rng_idx,        //PRNG idx
                            1024/8,         //Size of key
                            65537,          //e
                            &pub_key)       //RSA key
                            ) != CRYPT_OK) //if conditon test
     {
         cout << "RSA Key Generation error " << error_to_string(err) << endl;
     }
     rsa_free(&pub_key); //free the key when we are done with it;
     */
     sprng_done(&random_gen); //done generating random numbers
}

Solution

  • Add -DTFM_DESC to your makefile's CFLAGS and CPPFLAGS variables so the headers will declare tfm_desc as an extern variable.

    The tfm_desc variable should then get pulled in from the library.