Michael220
Michael220

Reputation: 313

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?

Upvotes: 1

Views: 155

Answers (1)

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, e.g.:

    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, e.g. with:

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

Upvotes: 0

Related Questions