From ee0460cff538a3de7ca89fb54d37215757659a42 Mon Sep 17 00:00:00 2001 From: Daniel Seither Date: Fri, 25 Sep 2015 14:10:01 +0200 Subject: Add the Darwin_SecRandom entropy source It uses the SecRandomCopyBytes function from the Security framework of OS X and iOS. We need this because it is the official way to get cryptographically secure random numbers on iOS, where /dev/random is not accessible due to sandboxing. --- .../entropy/darwin_secrandom/darwin_secrandom.cpp | 28 +++++++++++++++++++ .../entropy/darwin_secrandom/darwin_secrandom.h | 31 ++++++++++++++++++++++ src/lib/entropy/darwin_secrandom/info.txt | 17 ++++++++++++ src/lib/entropy/entropy_srcs.cpp | 8 ++++++ 4 files changed, 84 insertions(+) create mode 100644 src/lib/entropy/darwin_secrandom/darwin_secrandom.cpp create mode 100644 src/lib/entropy/darwin_secrandom/darwin_secrandom.h create mode 100644 src/lib/entropy/darwin_secrandom/info.txt (limited to 'src/lib/entropy') diff --git a/src/lib/entropy/darwin_secrandom/darwin_secrandom.cpp b/src/lib/entropy/darwin_secrandom/darwin_secrandom.cpp new file mode 100644 index 000000000..f04b75a12 --- /dev/null +++ b/src/lib/entropy/darwin_secrandom/darwin_secrandom.cpp @@ -0,0 +1,28 @@ +/* +* Darwin SecRandomCopyBytes EntropySource +* (C) 2015 Daniel Seither (Kullo GmbH) +* +* Botan is released under the Simplified BSD License (see license.txt) +*/ + +#include +#include + +namespace Botan { + +/** +* Gather entropy from SecRandomCopyBytes +*/ +void Darwin_SecRandom::poll(Entropy_Accumulator& accum) + { + const size_t ENTROPY_BITS_PER_BYTE = 8; + const size_t BUF_SIZE = 256; + + m_buf.resize(BUF_SIZE); + if (0 == SecRandomCopyBytes(kSecRandomDefault, m_buf.size(), m_buf.data())) + { + accum.add(m_buf.data(), m_buf.size(), ENTROPY_BITS_PER_BYTE); + } + } + +} diff --git a/src/lib/entropy/darwin_secrandom/darwin_secrandom.h b/src/lib/entropy/darwin_secrandom/darwin_secrandom.h new file mode 100644 index 000000000..504d5cc64 --- /dev/null +++ b/src/lib/entropy/darwin_secrandom/darwin_secrandom.h @@ -0,0 +1,31 @@ +/* +* Darwin SecRandomCopyBytes EntropySource +* (C) 2015 Daniel Seither (Kullo GmbH) +* +* Botan is released under the Simplified BSD License (see license.txt) +*/ + +#ifndef BOTAN_ENTROPY_SRC_DARWIN_SECRANDOM_H__ +#define BOTAN_ENTROPY_SRC_DARWIN_SECRANDOM_H__ + +#include + +namespace Botan { + +/** +* Entropy source using SecRandomCopyBytes from Darwin's Security.framework +*/ +class Darwin_SecRandom : public EntropySource + { + public: + std::string name() const override { return "Darwin SecRandomCopyBytes"; } + + void poll(Entropy_Accumulator& accum) override; + + private: + secure_vector m_buf; + }; + +} + +#endif diff --git a/src/lib/entropy/darwin_secrandom/info.txt b/src/lib/entropy/darwin_secrandom/info.txt new file mode 100644 index 000000000..e12c341fd --- /dev/null +++ b/src/lib/entropy/darwin_secrandom/info.txt @@ -0,0 +1,17 @@ +define ENTROPY_SRC_DARWIN_SECRANDOM 20150925 + + +darwin_secrandom.cpp + + + +darwin_secrandom.h + + + +darwin + + + +darwin -> Security + \ No newline at end of file diff --git a/src/lib/entropy/entropy_srcs.cpp b/src/lib/entropy/entropy_srcs.cpp index d44ab8c92..d57160c88 100644 --- a/src/lib/entropy/entropy_srcs.cpp +++ b/src/lib/entropy/entropy_srcs.cpp @@ -43,6 +43,10 @@ #include #endif +#if defined(BOTAN_HAS_ENTROPY_SRC_DARWIN_SECRANDOM) + #include +#endif + namespace Botan { namespace { @@ -97,6 +101,10 @@ std::vector> get_default_entropy_sources() )); #endif +#if defined(BOTAN_HAS_ENTROPY_SRC_DARWIN_SECRANDOM) + sources.push_back(std::unique_ptr(new Darwin_SecRandom)); +#endif + return sources; } -- cgit v1.2.3