This commit makes the ChaCha20 CSPRNG prefer using
OpenSSL's SIMD optimized ChaCha20 on platforms which
are compiled with such support.  This represents a
major improvement on performance, without impacting
the underlying CSPRNG functionality.

The EVP backend re-keys from the first bytes of every
refill, just as the built-in implementation does, so
that the fast key erasure - and thus the forward
secrecy - of the generator is the same for both.  The
built-in ChaCha20 implementation is always built so
that it can serve as a fallback when OpenSSL is unable
to provide ChaCha20, for example when it only offers
approved algorithms in FIPS mode.

Signed-off-by: Aaron Conole <[email protected]>
---

v3->v4:
  * Detect EVP_chacha20 at configure time (HAVE_CHACHA20_EVP) rather
    than relying on OPENSSL_NO_CHACHA.
  * Fall back to the built-in implementation with a warning when
    seeding fails, instead of aborting.
  * Reseed the built-in state from fresh entropy before falling back
    after a mid-stream EVP failure; previously it would refill a
    never-keyed state.
  * Documented that the maximum request size and IV consumption
    depend on configuration.

v2->v3:
  * Update the the csprng to provide local ChaCha20 when
    openssl isn't available for many reasons.
  * Updated the cs_random_bytes() max comment (since the OpenSSL
    version will shrink the max a bit more).
  * Updated the rekey to provide forward key erasure and forward
    secrecy.

lib/random.c      | 162 ++++++++++++++++++++++++++++++++++++++++++++--
 lib/random.h      |   3 +-
 m4/openvswitch.m4 |   7 ++
 3 files changed, 167 insertions(+), 5 deletions(-)

diff --git a/lib/random.c b/lib/random.c
index 9a16aea1ae..96d30c32cc 100644
--- a/lib/random.c
+++ b/lib/random.c
@@ -18,16 +18,24 @@
 #include "random.h"
 
 #include <errno.h>
+#include <stdbool.h>
 #include <stdlib.h>
 #include <sys/time.h>
 
+#ifdef HAVE_CHACHA20_EVP
+#include <openssl/evp.h>
+#endif
+
 #include "byte-order.h"
 #include "entropy.h"
 #include "hash.h"
+#include "openvswitch/vlog.h"
 #include "ovs-thread.h"
 #include "timeval.h"
 #include "util.h"
 
+VLOG_DEFINE_THIS_MODULE(random);
+
 /* This is the 32-bit PRNG recommended in G. Marsaglia, "Xorshift RNGs",
  * _Journal of Statistical Software_ 8:14 (July 2003).  According to the paper,
  * it has a period of 2**32 - 1 and passes almost all tests of randomness.
@@ -139,6 +147,14 @@ random_next(void)
  * the arc4random policy of periodic reseeding. */
 #define CSPRNG_RESEED   (1600 * 1024)
 
+#ifdef HAVE_CHACHA20_EVP
+#define CHACHA_IVLEN    16          /* ChaCha20 IV: counter and nonce. */
+/* The OpenSSL EVP backend consumes an IV in addition to a key. */
+#define CSPRNG_SECRETS  (CHACHA_KEYLEN + CHACHA_IVLEN)
+#else
+#define CSPRNG_SECRETS  CHACHA_KEYLEN
+#endif
+
 #define ROTL32(v, n)    (((v) << (n)) | ((v) >> (32 - (n))))
 
 #define QUARTERROUND(a, b, c, d)                \
@@ -148,6 +164,11 @@ random_next(void)
     c += d; b ^= c; b = ROTL32(b, 7)
 
 struct csprng {
+#ifdef HAVE_CHACHA20_EVP
+    EVP_CIPHER_CTX *ctx;            /* OpenSSL EVP state, once created. */
+    bool use_evp;                   /* Serve from OpenSSL rather than the
+                                     * built-in implementation. */
+#endif
     uint32_t state[16];             /* ChaCha20 state:
                                      * constant|key|counter|nonce. */
     uint8_t buf[CSPRNG_BUFSZ];      /* Buffered keystream. */
@@ -159,8 +180,10 @@ struct csprng {
 /* ChaCha20 constant: "expand 32-byte k".  Must be at least 16 bytes + NUL. */
 static const uint8_t chacha_sigma[] = "expand 32-byte k";
 
+#ifndef HAVE_CHACHA20_EVP
 /* Current random state. */
 DEFINE_STATIC_PER_THREAD_DATA(struct csprng, cs_state, { .initialized = 0 });
+#endif
 
 /* Produces one 64-byte ChaCha20 block from 'input' (16 words) into 'out'. */
 void
@@ -211,13 +234,129 @@ csprng_rekey(struct csprng *c, const uint8_t 
key[CHACHA_KEYLEN])
     }
 }
 
+#ifdef HAVE_CHACHA20_EVP
+/* This is the EVP based CSPRNG using the OpenSSL EVP framework to create a
+ * long stream of bytes, and pull those.  The OpenSSL EVP manages the
+ * underlying bytes, all this does is manage the policy.  It re-keys from the
+ * beginning of every refill, as the built-in implementation below does, so
+ * that both provide the same fast key erasure.
+ */
+
+static void
+cs_state_destroy(void *ctx)
+{
+    struct csprng *csp = ctx;
+
+    if (csp->ctx) {
+        EVP_CIPHER_CTX_free(csp->ctx);
+        csp->ctx = NULL;
+    }
+
+    free(csp);
+}
+
+static struct csprng *
+cs_state_get(void)
+{
+    static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
+    static ovsthread_key_t cskey;
+    struct csprng *csp;
+
+    if (ovsthread_once_start(&once)) {
+        ovsthread_key_create(&cskey, cs_state_destroy);
+        ovsthread_once_done(&once);
+    }
+
+    csp = ovsthread_getspecific(cskey);
+    if (!csp) {
+        csp = xzalloc(sizeof *csp);
+        csp->use_evp = true;
+        ovsthread_setspecific(cskey, csp);
+    }
+    return csp;
+}
+
+/* (Re)keys the OpenSSL EVP state from 'key' (32 bytes) and 'iv' (16 bytes). */
+static bool
+csprng_evp_rekey(struct csprng *c, const uint8_t key[CHACHA_KEYLEN],
+                 const uint8_t iv[CHACHA_IVLEN])
+{
+    return EVP_EncryptInit_ex(c->ctx, EVP_chacha20(), NULL, key, iv) == 1;
+}
+
+/* Seeds the OpenSSL EVP state from fresh entropy, returning false if OpenSSL
+ * cannot provide ChaCha20. */
+static bool
+csprng_evp_seed(struct csprng *c, const uint8_t key[CHACHA_KEYLEN])
+{
+    uint8_t iv[CHACHA_IVLEN];
+    bool ok;
+
+    if (!c->ctx) {
+        c->ctx = EVP_CIPHER_CTX_new();
+        if (!c->ctx) {
+            VLOG_WARN_ONCE("cs_random: cannot create cipher context.");
+            return false;
+        }
+    }
+
+    get_entropy_or_die(iv, sizeof iv);
+    ok = csprng_evp_rekey(c, key, iv);
+    ovs_memzero(iv, sizeof iv);
+
+    if (!ok) {
+        /* OpenSSL may not provide ChaCha20 at all, for example when it only
+         * offers approved algorithms in FIPS mode.  Fall back to the built-in
+         * implementation rather than failing to provide randomness. */
+        VLOG_WARN_ONCE("cs_random: OpenSSL ChaCha20 unavailable, using the "
+                       "built-in implementation.");
+    }
+    return ok;
+}
+
+/* Fills 'c' with fresh keystream from OpenSSL, reserving the first bytes to
+ * re-key the generator (fast key erasure / forward secrecy).  Returns false
+ * if OpenSSL fails to produce the keystream or to re-key, in which case the
+ * caller should fall back to the built-in implementation. */
+static bool
+csprng_evp_refill(struct csprng *c)
+{
+    static const uint8_t zero[CSPRNG_BUFSZ];
+    int outl;
+
+    if (EVP_EncryptUpdate(c->ctx, c->buf, &outl, zero, sizeof c->buf) != 1 ||
+        outl != sizeof c->buf ||
+        !csprng_evp_rekey(c, c->buf, c->buf + CHACHA_KEYLEN)) {
+        VLOG_WARN_ONCE("cs_random: OpenSSL EVP failure, using the built-in "
+                       "implementation.");
+        ovs_memzero(c->buf, sizeof c->buf);
+        return false;
+    }
+
+    ovs_memzero(c->buf, CSPRNG_SECRETS);
+    c->pos = CSPRNG_SECRETS;
+    return true;
+}
+#endif /* HAVE_CHACHA20_EVP */
+
 static void
 csprng_seed(struct csprng *c)
 {
     uint8_t key[CHACHA_KEYLEN];
 
     get_entropy_or_die(key, sizeof key);
+
+#ifdef HAVE_CHACHA20_EVP
+    if (c->use_evp && !csprng_evp_seed(c, key)) {
+        c->use_evp = false;
+    }
+    if (!c->use_evp) {
+        csprng_rekey(c, key);
+    }
+#else
     csprng_rekey(c, key);
+#endif
+
     ovs_memzero(key, sizeof key);
 
     c->pos = sizeof c->buf;
@@ -256,10 +395,11 @@ cs_random_bytes(void *b, size_t l)
      * anyone using this interface forks via xfork(); if child processes
      * ever need fresh state, xfork() should be taught to clear it. */
 
-    /* The first 32 bytes of every refill are consumed as the next key, so
-     * they are not available as output.  An over-sized request would read
-     * past 'c->buf', so abort on it in all builds, not just debug ones. */
-    if (l > sizeof c->buf - CHACHA_KEYLEN) {
+    /* The first bytes of every refill are consumed as the next key, and as
+     * the next IV when serving from OpenSSL, so they are not available as
+     * output.  An over-sized request would read past 'c->buf', so abort on
+     * it in all builds, not just debug ones. */
+    if (l > sizeof c->buf - CSPRNG_SECRETS) {
         OVS_NOT_REACHED();
     }
 
@@ -268,7 +408,21 @@ cs_random_bytes(void *b, size_t l)
     }
 
     if (c->pos + l > sizeof c->buf) {
+#ifdef HAVE_CHACHA20_EVP
+        if (c->use_evp && !csprng_evp_refill(c)) {
+            /* The built-in state has never been keyed when serving from
+             * OpenSSL, so seed it from fresh entropy before falling back;
+             * refilling the unkeyed state would produce deterministic
+             * output. */
+            c->use_evp = false;
+            csprng_seed(c);
+        }
+        if (!c->use_evp) {
+            csprng_refill(c);
+        }
+#else
         csprng_refill(c);
+#endif
     }
 
     memcpy(b, c->buf + c->pos, l);
diff --git a/lib/random.h b/lib/random.h
index 77792e28f1..e62268a669 100644
--- a/lib/random.h
+++ b/lib/random.h
@@ -53,7 +53,8 @@ void chacha20_block(const uint32_t input[CHACHA_K_INPUT],
                     uint8_t out[CHACHA_BLOCK]);
 
 /* Fills 'b' with 'l' cryptographically secure random bytes.  'l' must not be
- * larger than 4064; callers needing more bytes should make multiple calls. */
+ * larger than 4048 (4064 in builds without OpenSSL EVP ChaCha20 support);
+ * callers needing more bytes should make multiple calls. */
 void cs_random_bytes(void *, size_t);
 uint32_t cs_random_uint32(void);
 
diff --git a/m4/openvswitch.m4 b/m4/openvswitch.m4
index 979349e6b4..c1b74361a7 100644
--- a/m4/openvswitch.m4
+++ b/m4/openvswitch.m4
@@ -170,6 +170,13 @@ OpenFlow connections over SSL/TLS will not be supported.
    AM_CONDITIONAL([HAVE_OPENSSL], [test "$HAVE_OPENSSL" = yes])
    if test "$HAVE_OPENSSL" = yes; then
       AC_DEFINE([HAVE_OPENSSL], [1], [Define to 1 if OpenSSL is installed.])
+      dnl OpenSSL may be built without ChaCha20 support, e.g. when it only
+      dnl offers approved algorithms in FIPS mode, so detect it separately.
+      AC_CHECK_DECLS([EVP_chacha20],
+                     [AC_DEFINE([HAVE_CHACHA20_EVP], [1],
+                                [Define to 1 if OpenSSL provides ChaCha20.])],
+                     [],
+                     [[#include <openssl/evp.h>]])
    fi
 ])
 
-- 
2.56.0

_______________________________________________
dev mailing list
[email protected]
https://mail.openvswitch.org/mailman/listinfo/ovs-dev

Reply via email to