Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/crypto/chacha Simplify ChaCha selection and allow it to ...



details:   https://anonhg.NetBSD.org/src/rev/ca6aba94acb9
branches:  trunk
changeset: 936477:ca6aba94acb9
user:      riastradh <riastradh%NetBSD.org@localhost>
date:      Mon Jul 27 20:49:10 2020 +0000

description:
Simplify ChaCha selection and allow it to be used much earlier.

This way we can use it for cprng_fast early on.  ChaCha is easy
because there's no data formats that must be preserved from call to
call but vary from implementation to implementation -- we could even
make it a sysctl knob to dynamically select it with negligible cost.

(In contrast, different AES implementations use different expanded
key formats which must be preserved from aes_setenckey to aes_enc,
for example, which means a considerably greater burden on dynamic
selection that's not really worth it.)

diffstat:

 sys/crypto/chacha/chacha_impl.c |  41 +++--------------------------------------
 1 files changed, 3 insertions(+), 38 deletions(-)

diffs (129 lines):

diff -r a285cc0013a5 -r ca6aba94acb9 sys/crypto/chacha/chacha_impl.c
--- a/sys/crypto/chacha/chacha_impl.c   Mon Jul 27 20:48:18 2020 +0000
+++ b/sys/crypto/chacha/chacha_impl.c   Mon Jul 27 20:49:10 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: chacha_impl.c,v 1.2 2020/07/27 20:45:15 riastradh Exp $        */
+/*     $NetBSD: chacha_impl.c,v 1.3 2020/07/27 20:49:10 riastradh Exp $        */
 
 /*-
  * Copyright (c) 2020 The NetBSD Foundation, Inc.
@@ -38,17 +38,14 @@
 #include "chacha.h"
 #include "chacha_ref.h"
 
-static const struct chacha_impl        *chacha_md_impl __read_mostly;
-static const struct chacha_impl        *chacha_impl    __read_mostly;
+static const struct chacha_impl        *chacha_md_impl __read_mostly;
+static const struct chacha_impl        *chacha_impl __read_mostly = &chacha_ref_impl;
 
 static int
 sysctl_kern_crypto_chacha_selected(SYSCTLFN_ARGS)
 {
        struct sysctlnode node;
 
-       KASSERTMSG(chacha_impl != NULL,
-           "sysctl ran before ChaCha implementation was selected");
-
        node = *rnode;
        node.sysctl_data = __UNCONST(chacha_impl->ci_name);
        node.sysctl_size = strlen(chacha_impl->ci_name) + 1;
@@ -79,8 +76,6 @@
 chacha_select(void)
 {
 
-       KASSERT(chacha_impl == NULL);
-
        if (chacha_md_impl) {
                if (chacha_selftest(chacha_md_impl))
                        aprint_error("chacha: self-test failed: %s\n",
@@ -88,15 +83,6 @@
                else
                        chacha_impl = chacha_md_impl;
        }
-       if (chacha_impl == NULL) {
-               if (chacha_selftest(&chacha_ref_impl))
-                       aprint_error("chacha: self-test failed: %s\n",
-                           chacha_ref_impl.ci_name);
-               else
-                       chacha_impl = &chacha_ref_impl;
-       }
-       if (chacha_impl == NULL)
-               panic("ChaCha self-tests failed");
 
        aprint_verbose("chacha: %s\n", chacha_impl->ci_name);
        return 0;
@@ -118,26 +104,11 @@
        }
 }
 
-static void
-chacha_guarantee_selected(void)
-{
-#if 0
-       static once_t once;
-       int error;
-
-       error = RUN_ONCE(&once, chacha_select);
-       KASSERT(error == 0);
-#endif
-}
-
 void
 chacha_md_init(const struct chacha_impl *impl)
 {
 
        KASSERT(cold);
-       KASSERTMSG(chacha_impl == NULL,
-           "ChaCha implementation `%s' already chosen, can't offer `%s'",
-           chacha_impl->ci_name, impl->ci_name);
        KASSERTMSG(chacha_md_impl == NULL,
            "ChaCha implementation `%s' already offered, can't offer `%s'",
            chacha_md_impl->ci_name, impl->ci_name);
@@ -153,7 +124,6 @@
     unsigned nr)
 {
 
-       chacha_guarantee_selected();
        (*chacha_impl->ci_chacha_core)(out, in, k, c, nr);
 }
 
@@ -165,7 +135,6 @@
     unsigned nr)
 {
 
-       chacha_guarantee_selected();
        (*chacha_impl->ci_hchacha)(out, in, k, c, nr);
 }
 
@@ -176,7 +145,6 @@
     unsigned nr)
 {
 
-       chacha_guarantee_selected();
        (*chacha_impl->ci_chacha_stream)(s, nbytes, blkno, nonce, key, nr);
 }
 
@@ -187,7 +155,6 @@
     unsigned nr)
 {
 
-       chacha_guarantee_selected();
        (*chacha_impl->ci_chacha_stream_xor)(c, p, nbytes, blkno, nonce, key,
            nr);
 }
@@ -199,7 +166,6 @@
     unsigned nr)
 {
 
-       chacha_guarantee_selected();
        (*chacha_impl->ci_xchacha_stream)(s, nbytes, blkno, nonce, key, nr);
 }
 
@@ -210,7 +176,6 @@
     unsigned nr)
 {
 
-       chacha_guarantee_selected();
        (*chacha_impl->ci_xchacha_stream_xor)(c, p, nbytes, blkno, nonce, key,
            nr);
 }



Home | Main Index | Thread Index | Old Index