简体   繁体   中英

Seg fault when using Botan

I'm just getting started with Botan. I have included the botan_all.h in my code file and am linking to the libbotan-2.a library when building.

Here is the relevant part of main.cpp:

#include "botan_all.h"

int main(int argc, char *argv[])
{
    const std::vector<uint8_t> key = Botan::hex_decode("2B7E151628AED2A6ABF7158809CF4F3C");
    std::unique_ptr<Botan::Cipher_Mode> enc = Botan::Cipher_Mode::create("AES-128/CBC/PKCS7", Botan::ENCRYPTION);    
    enc->set_key(key);
}

The enc->set_key(key) causes a seg fault. What am I missing?

This is likely caused by your Botan version not being build with support for the specified algorithm. The method CipherMode::create returns a nullptr if the specified algorithm is not found ( reference ). You would then have to check the result, eg:

    if (enc == nullptr) {
      throw std::runtime_error{"Cipher not supported!"};
    }

Alternatively, you could use CipherMode::create_or_throw ( reference ):

Botan::Cipher_Mode::create_or_throw("AES-128/CBC/PKCS7", Botan::ENCRYPTION);

You can also check your Botan headers for support for these specific algorithms, by checking if BOTAN_HAS_AES and BOTAN_HAS_MODE_CBC are defined. If these are not available, you would have to ensure that when you compile botan, you include AES and CBC support, eg with:

./configure.py --enable-modules=aes,cbc,...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM