1

I am need to calculate SHA3 in my program as well as use AES in my sockets and have decided to use crypto++ library.

I am new to Visual Studio environment with no prior experience on visual studios but have extensively worked on g++ (linux/mingw-w64). One of the third part library I am using recommends Visual Studios (causes linker error when used with g++).

I am getting an linker error. Error :

LNK2001 unresolved external symbol "public: virtual void __cdecl CryptoPP::SHA3::Update(unsigned char const *,unsigned __int64)" (?Update@SHA3@CryptoPP@@UEAAXPEBE_K@Z) ConsoleApplication2 C:\Users\Admin\source\repos\ConsoleApplication2\ConsoleApplication2\ConsoleApplication2.obj 1   

LNK2019 unresolved external symbol "public: virtual void __cdecl CryptoPP::SHA3::Restart(void)" (?Restart@SHA3@CryptoPP@@UEAAXXZ) referenced in function "public: __cdecl CryptoPP::SHA3::SHA3(unsigned int)" (??0SHA3@CryptoPP@@QEAA@I@Z)  ConsoleApplication2 C:\Users\Admin\source\repos\ConsoleApplication2\ConsoleApplication2\ConsoleApplication2.obj 1   

Error   LNK2001 unresolved external symbol "public: virtual void __cdecl CryptoPP::SHA3::TruncatedFinal(unsigned char *,unsigned __int64)" (?TruncatedFinal@SHA3@CryptoPP@@UEAAXPEAE_K@Z)   ConsoleApplication2 C:\Users\Admin\source\repos\ConsoleApplication2\ConsoleApplication2\ConsoleApplication2.obj 1

The same code works properly in g++(mingw-w64 - I am using dll created in mingw-w64)

OS : Windows 10 Pro 64 bit

Development Environment : Visual Studios 2017

Target : Debug x64

Source :

#include <sha3.h>
#include <filters.h>
#include <hex.h>
#include <string>
#include <iostream>
#include <vector>
#include <algorithm>
#include <iostream>
#include <base64.h>

int main()
{
    CryptoPP::SHA3_512 hash;

    std::cout << hash.AlgorithmName() << " Test." << std::endl;

    std::string in = "The quick brown fox jumps over the lazy dog";
    std::vector<CryptoPP::byte> out(hash.DigestSize());

    hash.CalculateTruncatedDigest(&out[0], hash.DigestSize(), reinterpret_cast<CryptoPP::byte*>(&in[0]), in.size());

    std::cout << in << std::endl;

    std::cout.setf(std::ios::hex, std::ios::basefield);

    for_each(out.begin(), out.end(), [](CryptoPP::byte i) {
        printf("%x", i);
    });

    std::cout << "Original String : " << str << std::endl;
    return 0;
}

Kindly advice me how to load (copy) all generated dll in some common folder just like the make install command (facility) in gcc.

Edit :

I have added cryptopp.lib in additional dependencies and added the folder containing both cryptopp.lib and cryptopp.dll in Additional Library Directory. I have added the directory in Path environment variable. I have selected Multi Threaded Debug Dll in Debug x64 configuration during dll generation in cryptolib project.

jww
  • 97,681
  • 90
  • 411
  • 885
Dark Sorrow
  • 1,681
  • 14
  • 37
  • You need to have .lib and .dll files. Go to your project-> right click properties-> Under linked section choose input-> here mention all the .lib of dll's you would like to use. Then provide the path of the dll's where you have placed them in Linker->Genera-> additional include directories – StraightCirle Jun 01 '18 at 12:18
  • @StraightCirle, done this still no success. Sorry, I should have mentioned in my original post. – Dark Sorrow Jun 01 '18 at 12:24
  • Also see [FIPS DLL](https://www.cryptopp.com/wiki/FIPS_DLL) on the Crypto++ wiki. The old/antiquated FIPS DLL does not include SHA3 because SHA3 predates the last FIPS validation. You probably want to avoid the FIPS DLL and just use the static library. The static library is called `cryptlib.lib`. Another page of interest on the wiki is [Visual Studio](https://www.cryptopp.com/wiki/Visual_Studio). – jww Jun 02 '18 at 03:39

2 Answers2

1

Paul Sanders is quite correct, the classes and functions you want to use from the crypto++ DLL must be qualified with _declspec(dllexport) / _declspec(dllimport).

Some of this has been done already, as described in Crypto++ wiki page for Fips dll. It works thru the preprocessor macro CRYPTOPP_DLL, which is defined in the crypto++ header config.h and used in fips140.h.

There is a problem though - not all the algorithms you want are in FIPS and, as you have found, are not exported for use in a DLL. You could modify other headers to add the DLL export/imports but note that the wiki page suggests : "If you really need a DLL, then provide a wrapper DLL that links against the static library.". Rather than wrap every component of every crypto++ algorithm you use, it would be more convenient to encapsulate entire operations into a high level class or function which is then exported/imported from your wrapper DLL.

GCC exports everything by default, which is why shared libraries worked for you on Linux.

stanthomas
  • 1,141
  • 10
  • 9
0

You need to link your app against (I believe) cryptopp.lib. This probably comes in separate versions for 32 bit and 64 bit (I believe the names in the import table differ) so make sure you use the right one.

You can tell Visual Studio to do this via Solution Explorer -> Right-click on project name -> Properties -> Linker -> Input -> Additional Dependencies. Then just build again.

Your app will need to be able to find the DLL at runtime, not at link time. If it can't, you will get an error message telling you so when you try to run it. Copying the DLL to the same directory as the executable is what people usually do (and I hope there's no issue using a DLL created by mingw in Visual Studio).

Paul Sanders
  • 24,133
  • 4
  • 26
  • 48
  • I have added .lib in additional dependencies "cryptopp.lib". I have selected Multi Threaded Debug Dll in Debug x64 configuration during dll generation in cryptolib project. – Dark Sorrow Jun 01 '18 at 12:23
  • And did it work? I was guessing at the name of the .lib file and I don't know where you'd find it on your disk if it arrived as part of the package. If you have useful additional information you can edit my post. OTOH, if you're building the cryptolib DLL yourself then you should ensure that Properties -> C++ -> Code Generation -> Runtime Library matches your app (and you will of course know where to find the .lib). – Paul Sanders Jun 01 '18 at 12:41
  • It didn't work. cryptopp.lib and cryptopp.dll were generated after I built cryptopp in Visual Studios 2017 under Debug x64 configuration and Runtime Library set to Multi Threaded Debug DLL (/MDd). I am building by application under the same Runtime Library Model. I have specified cryptopp.lib in additional libraries and also added necessary path. – Dark Sorrow Jun 01 '18 at 14:32
  • Same error messages? It might be easier to build `crypto++` as a static library, it doesn't really need to be a DLL. You'd need to create a new project file though (to build the static library, I mean). Where did the one you're using now come from? Was it supplied with the package? – Paul Sanders Jun 01 '18 at 14:40
  • They system architecture for this project requires me to use shared library. When using g++(mingw-w64) I had deployed the system with libcryptopp as shared library. If all other options are exhausted then I build a static library but till then I will have to try to solve the linker issue. – Dark Sorrow Jun 01 '18 at 15:21
  • OK, well next port of call is probably [dllimport and dllexport](https://msdn.microsoft.com/en-us/library/3y1sfaz2.aspx). Maybe the answer lies there. – Paul Sanders Jun 01 '18 at 16:59