aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJack Lloyd <[email protected]>2016-08-30 12:25:44 -0400
committerJack Lloyd <[email protected]>2016-08-30 12:25:44 -0400
commita98cdac63a9c88952b3b01875a414cac204deed5 (patch)
treef47c51b050bd9b5c86d473f75426d70777062c72 /src
parent36e84df0ab53861b9d764473f8ce85cb747a6d16 (diff)
Newhope is really limited by RNG speed.
4x-8x overall speedup switching from HMAC_DRBG to ChaCha20
Diffstat (limited to 'src')
-rw-r--r--src/cli/speed.cpp38
1 files changed, 33 insertions, 5 deletions
diff --git a/src/cli/speed.cpp b/src/cli/speed.cpp
index 651226d70..10408a5ae 100644
--- a/src/cli/speed.cpp
+++ b/src/cli/speed.cpp
@@ -83,8 +83,9 @@
#include <botan/mceliece.h>
#endif
-#if defined(BOTAN_HAS_NEWHOPE)
+#if defined(BOTAN_HAS_NEWHOPE) && defined(BOTAN_HAS_CHACHA)
#include <botan/newhope.h>
+ #include <botan/chacha.h>
#endif
namespace Botan_CLI {
@@ -401,7 +402,7 @@ class Speed final : public Command
bench_mceliece(provider, msec);
}
#endif
-#if defined(BOTAN_HAS_NEWHOPE)
+#if defined(BOTAN_HAS_NEWHOPE) && defined(BOTAN_HAS_CHACHA)
else if(algo == "NEWHOPE")
{
bench_newhope(provider, msec);
@@ -1089,7 +1090,7 @@ class Speed final : public Command
}
#endif
-#if defined(BOTAN_HAS_NEWHOPE)
+#if defined(BOTAN_HAS_NEWHOPE) && defined(BOTAN_HAS_CHACHA)
void bench_newhope(const std::string& provider,
std::chrono::milliseconds msec)
{
@@ -1099,6 +1100,33 @@ class Speed final : public Command
Timer shareda_timer(nm, "", "shareda");
Timer sharedb_timer(nm, "", "sharedb");
+ class ChaCha20_RNG : public Botan::RandomNumberGenerator
+ {
+ public:
+ std::string name() const override { return "ChaCha20_RNG"; }
+ void clear() override { /* ignored */ }
+
+ void randomize(uint8_t out[], size_t len) override
+ {
+ Botan::clear_mem(out, len);
+ m_chacha.cipher1(out, len);
+ }
+
+ bool is_seeded() const override { return true; }
+
+ void add_entropy(const uint8_t[], size_t) override { /* ignored */ }
+
+ ChaCha20_RNG(const Botan::secure_vector<uint8_t>& seed)
+ {
+ m_chacha.set_key(seed);
+ }
+
+ private:
+ Botan::ChaCha m_chacha;
+ };
+
+ ChaCha20_RNG nh_rng(rng().random_vec(32));
+
while(sharedb_timer.under(msec))
{
std::vector<uint8_t> send_a(NEWHOPE_SENDABYTES), send_b(NEWHOPE_SENDBBYTES);
@@ -1107,11 +1135,11 @@ class Speed final : public Command
Botan::newhope_poly sk_a;
keygen_timer.start();
- Botan::newhope_keygen(send_a.data(), &sk_a, rng());
+ Botan::newhope_keygen(send_a.data(), &sk_a, nh_rng);
keygen_timer.stop();
sharedb_timer.start();
- Botan::newhope_sharedb(shared_b.data(), send_b.data(), send_a.data(), rng());
+ Botan::newhope_sharedb(shared_b.data(), send_b.data(), send_a.data(), nh_rng);
sharedb_timer.stop();
shareda_timer.start();