在ns3中安装crypto++

希望在ns3仿真中使用一些密码学算法。以下为整理的安装过程,来源见参考文献。在ubuntu18.04可行。

安装crypto++

ubuntu使用的包管理系统apt中的crypto++版本比较旧了。不介意用旧版,或者是使用的是基于debian的发行版(yum)的话可以使用:

sudo apt-get install libcrypto++-dev libcrypto++-doc libcrypto++-utils

或者相应的

yum install cryptopp cryptopp-devel

最新版可以下载源码编译安装:

首先下载cryptopp的源码:https://www.cryptopp.com/#download,并解压到一个工作文件夹中。

比如:

mkdir cryptopp
cd cryptopp/
wget https://www.cryptopp.com/cryptopp820.zip
unzip -a cryptopp820.zip

其中cryptopp820.zip可以相应换成你希望的版本。

然后是build。如果想定制makefile flag可以参考crypto++的官方文档。执行:

make static dynamic cryptest.exe

build完可以执行

./cryptest.exe v
./cryptest.exe tv all

来测试build得到的库。

最后是安装库:

sudo make install PREFIX=/usr/local
sudo mkdir -p /usr/local/include/cryptopp
sudo cp *.h /usr/local/include/cryptopp
sudo chmod 755 /usr/local/include/cryptopp
sudo chmod 644 /usr/local/include/cryptopp/*.h
sudo mkdir -p /usr/local/lib
sudo cp libcryptopp.a /usr/local/lib
sudo chmod 644 /usr/local/lib/libcryptopp.a
sudo mkdir -p /usr/local/bin
sudo cp cryptest.exe /usr/local/bin
sudo chmod 755 /usr/local/bin/cryptest.exe
sudo mkdir -p /usr/local/share/cryptopp
sudo cp -r TestData /usr/local/share/cryptopp
sudo cp -r TestVectors /usr/local/share/cryptopp
sudo chmod 755 /usr/local/share/cryptopp
sudo chmod 755 /usr/local/share/cryptopp/TestData
sudo chmod 755 /usr/local/share/cryptopp/TestVectors
sudo chmod 644 /usr/local/share/cryptopp/TestData/*.dat
sudo chmod 644 /usr/local/share/cryptopp/TestVectors/*.txt

并配置shared library链接:

sudo ldconfig

修改wscript

打开ns3根目录(waf所在目录)下的wscript文件。

搜索

env = conf.env

并在下面添加:

conf.env['lpp'] 
crypto= conf.check(mandatory=True, lib='cryptopp', uselib_store='cryptopp')
conf.env.append_value('CXXDEFINES', 'ENABLE_CRYPTOPP')
conf.env.append_value('CCDEFINES', 'ENABLE_CRYPTOPP')

搜索

if program.env[‘ENABLE_STATIC_NS3’]:

找到代码

    if program.env['ENABLE_STATIC_NS3']:
        if sys.platform == 'darwin':
            program.env.STLIB_MARKER = '-Wl,-all_load'
        else:
            program.env.STLIB_MARKER = '-Wl,-Bstatic,--whole-archive'
            program.env.SHLIB_MARKER = '-Wl,-Bdynamic,--no-whole-archive'
    else:
        if program.env.DEST_BINFMT == 'elf':
            # All ELF platforms are impacted but only the gcc compiler has a flag to fix it.
            if 'gcc' in (program.env.CXX_NAME, program.env.CC_NAME): 
                program.env.append_value ('SHLIB_MARKER', '-Wl,--no-as-needed')

return program

并修改为:

    if program.env['ENABLE_STATIC_NS3']:
        if sys.platform == 'darwin':
            program.env.STLIB_MARKER = '-Wl,-all_load,-lcryptopp'
        else:
            program.env.STLIB_MARKER = '-Wl,-Bstatic,--whole-archive,-lcryptopp'
            program.env.SHLIB_MARKER = '-Wl,-Bdynamic,--no-whole-archive,-lcryptopp'
    else:
        if program.env.DEST_BINFMT == 'elf':
            # All ELF platforms are impacted but only the gcc compiler has a flag to fix it.
            if 'gcc' in (program.env.CXX_NAME, program.env.CC_NAME): 
                program.env.append_value ('SHLIB_MARKER', '-Wl,--no-as-needed,-lcryptopp')

    return program

搜索

obj.install_path = None

应有两个结果,在每一个后面都加上

obj.uselib = ‘CRYPTOPP'

修改完之后这段代码应形如:

            if os.path.isdir(os.path.join("scratch", filename)):
                obj = bld.create_ns3_program(filename, all_modules)
                obj.path = obj.path.find_dir('scratch').find_dir(filename)
                obj.source = obj.path.ant_glob('*.cc')
                obj.target = filename
                obj.name = obj.target
                obj.install_path = None
                obj.uselib = 'CRYPTOPP' 
            elif filename.endswith(".cc"):
                name = filename[:-len(".cc")]
                obj = bld.create_ns3_program(name, all_modules)
                obj.path = obj.path.find_dir('scratch')
                obj.source = filename
                obj.target = name
                obj.name = obj.target
                obj.install_path = None
                obj.uselib = 'CRYPTOPP' 

重新配置编译ns3

sudo ./waf distclean
sudo ./waf configure (你通常使用的配置参数)
sudo ./waf

此时你已成功的在ns3中安装了crypto++。比如如果需要SM3库,可以在你的ns3脚本中

#include ; //如果你通过apt-get安装crypto++,使用这个
#include ; //如果你按本文方法从源码安装crypto++,使用这个

来使用。

参考文献

https://www.mehic.info/2016/04/installing-and-crypto-libcryptopp-with-ns3/

https://www.cryptopp.com/

你可能感兴趣的:(在ns3中安装crypto++)