From 040eee1aa49b49df4698d83a05af57c220127fd1 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 4 May 2024 13:36:04 +0200 Subject: Adding upstream version 2.2.0. Signed-off-by: Daniel Baumann --- src/lib/cryptolink/botan_link.cc | 84 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 src/lib/cryptolink/botan_link.cc (limited to 'src/lib/cryptolink/botan_link.cc') diff --git a/src/lib/cryptolink/botan_link.cc b/src/lib/cryptolink/botan_link.cc new file mode 100644 index 0000000..5eb0e34 --- /dev/null +++ b/src/lib/cryptolink/botan_link.cc @@ -0,0 +1,84 @@ +// Copyright (C) 2011-2022 Internet Systems Consortium, Inc. ("ISC") +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + +#include + +#include +#include +#include +#include + +#include +#include +#include + +namespace isc { +namespace cryptolink { + +// For Botan, we use the CryptoLink class object in RAII style +class CryptoLinkImpl { + // empty class +}; + +CryptoLink::~CryptoLink() { +} + +/// \brief Botan implementation of RNG. +class RNGImpl : public RNG { +public: + RNGImpl() { + rng.reset(new Botan::AutoSeeded_RNG()); + } + + ~RNGImpl() { + } + +private: + std::vector random(size_t len) { + std::vector data; + if (len > 0) { + data.resize(len); + try { + rng->randomize(&data[0], len); + } catch (const Botan::Exception& ex) { + isc_throw(isc::cryptolink::LibraryError, + "Botan error: " << ex.what()); + } + } + return (data); + } + + boost::shared_ptr rng; +}; + +void +CryptoLink::initialize(CryptoLink& c) { + if (!c.impl_) { + try { + c.impl_.reset(new CryptoLinkImpl()); + } catch (const Botan::Exception& ex) { + isc_throw(InitializationError, "Botan error: " << ex.what()); + } + } + if (!c.rng_) { + try { + c.rng_.reset(new RNGImpl()); + } catch (const Botan::Exception& ex) { + isc_throw(InitializationError, "Botan error: " << ex.what()); + } + } + // A not yet fixed bug makes RNG to be destroyed after memory pool... + atexit([]{ getCryptoLink().getRNG().reset(); }); +} + +std::string +CryptoLink::getVersion() { + return (Botan::version_string()); +} + +} // namespace cryptolink +} // namespace isc + -- cgit v1.2.3