The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/libkern/arc4random.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 2017 The FreeBSD Foundation
    3  * All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer
   10  *    in this position and unchanged.
   11  * 2. Redistributions in binary form must reproduce the above copyright
   12  *    notice, this list of conditions and the following disclaimer in the
   13  *    documentation and/or other materials provided with the distribution.
   14  *
   15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
   19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   25  *
   26  */
   27 
   28 #include <sys/cdefs.h>
   29 __FBSDID("$FreeBSD$");
   30 
   31 #include <sys/types.h>
   32 #include <sys/param.h>
   33 #include <sys/kernel.h>
   34 #include <sys/libkern.h>
   35 #include <sys/linker.h>
   36 #include <sys/lock.h>
   37 #include <sys/malloc.h>
   38 #include <sys/mutex.h>
   39 #include <sys/random.h>
   40 #include <sys/smp.h>
   41 #include <sys/time.h>
   42 
   43 #include <crypto/chacha20/chacha.h>
   44 #include <crypto/sha2/sha256.h>
   45 #include <dev/random/randomdev.h>
   46 #include <machine/cpu.h>
   47 
   48 #define CHACHA20_RESEED_BYTES   65536
   49 #define CHACHA20_RESEED_SECONDS 300
   50 #define CHACHA20_KEYBYTES       32
   51 #define CHACHA20_BUFFER_SIZE    64
   52 
   53 CTASSERT(CHACHA20_KEYBYTES*8 >= CHACHA_MINKEYLEN);
   54 
   55 int arc4rand_iniseed_state = ARC4_ENTR_NONE;
   56 
   57 MALLOC_DEFINE(M_CHACHA20RANDOM, "chacha20random", "chacha20random structures");
   58 
   59 struct chacha20_s {
   60         struct mtx mtx;
   61         int numbytes;
   62         time_t t_reseed;
   63         u_int8_t m_buffer[CHACHA20_BUFFER_SIZE];
   64         struct chacha_ctx ctx;
   65 } __aligned(CACHE_LINE_SIZE);
   66 
   67 static struct chacha20_s *chacha20inst = NULL;
   68 
   69 #define CHACHA20_FOREACH(_chacha20) \
   70         for (_chacha20 = &chacha20inst[0]; \
   71              _chacha20 <= &chacha20inst[mp_maxid]; \
   72              _chacha20++)
   73 
   74 /*
   75  * Mix up the current context.
   76  */
   77 static void
   78 chacha20_randomstir(struct chacha20_s *chacha20)
   79 {
   80         struct timeval tv_now;
   81         u_int8_t key[CHACHA20_KEYBYTES];
   82 
   83         if (__predict_false(random_bypass_before_seeding && !is_random_seeded())) {
   84                 SHA256_CTX ctx;
   85                 uint64_t cc;
   86                 uint32_t fver;
   87 
   88                 if (!arc4random_bypassed_before_seeding) {
   89                         arc4random_bypassed_before_seeding = true;
   90                         if (!random_bypass_disable_warnings)
   91                                 printf("arc4random: WARNING: initial seeding "
   92                                     "bypassed the cryptographic random device "
   93                                     "because it was not yet seeded and the "
   94                                     "knob 'bypass_before_seeding' was "
   95                                     "enabled.\n");
   96                 }
   97 
   98                 /* Last ditch effort to inject something in a bad condition. */
   99                 cc = get_cyclecount();
  100                 SHA256_Init(&ctx);
  101                 SHA256_Update(&ctx, key, sizeof(key));
  102                 SHA256_Update(&ctx, &cc, sizeof(cc));
  103                 fver = __FreeBSD_version;
  104                 SHA256_Update(&ctx, &fver, sizeof(fver));
  105                 _Static_assert(sizeof(key) == SHA256_DIGEST_LENGTH,
  106                     "make sure 256 bits is still 256 bits");
  107                 SHA256_Final(key, &ctx);
  108         } else {
  109                 /*
  110                 * If the loader(8) did not have an entropy stash from the
  111                 * previous shutdown to load, then we will block.  The answer is
  112                 * to make sure there is an entropy stash at shutdown time.
  113                 *
  114                 * On the other hand, if the random_bypass_before_seeding knob
  115                 * was set and we landed in this branch, we know this won't
  116                 * block because we know the random device is seeded.
  117                 */
  118                 read_random(key, CHACHA20_KEYBYTES);
  119         }
  120         getmicrouptime(&tv_now);
  121         mtx_lock(&chacha20->mtx);
  122         chacha_keysetup(&chacha20->ctx, key, CHACHA20_KEYBYTES*8);
  123         chacha_ivsetup(&chacha20->ctx, (u_char *)&tv_now.tv_sec, (u_char *)&tv_now.tv_usec);
  124         /* Reset for next reseed cycle. */
  125         chacha20->t_reseed = tv_now.tv_sec + CHACHA20_RESEED_SECONDS;
  126         chacha20->numbytes = 0;
  127         mtx_unlock(&chacha20->mtx);
  128 }
  129 
  130 /*
  131  * Initialize the contexts.
  132  */
  133 static void
  134 chacha20_init(void)
  135 {
  136         struct chacha20_s *chacha20;
  137 
  138         chacha20inst = malloc((mp_maxid + 1) * sizeof(struct chacha20_s),
  139                         M_CHACHA20RANDOM, M_NOWAIT | M_ZERO);
  140         KASSERT(chacha20inst != NULL, ("chacha20_init: memory allocation error"));
  141 
  142         CHACHA20_FOREACH(chacha20) {
  143                 mtx_init(&chacha20->mtx, "chacha20_mtx", NULL, MTX_DEF);
  144                 chacha20->t_reseed = -1;
  145                 chacha20->numbytes = 0;
  146                 explicit_bzero(chacha20->m_buffer, CHACHA20_BUFFER_SIZE);
  147                 explicit_bzero(&chacha20->ctx, sizeof(chacha20->ctx));
  148         }
  149 }
  150 SYSINIT(chacha20, SI_SUB_LOCK, SI_ORDER_ANY, chacha20_init, NULL);
  151 
  152 
  153 static void
  154 chacha20_uninit(void)
  155 {
  156         struct chacha20_s *chacha20;
  157 
  158         CHACHA20_FOREACH(chacha20)
  159                 mtx_destroy(&chacha20->mtx);
  160         free(chacha20inst, M_CHACHA20RANDOM);
  161 }
  162 SYSUNINIT(chacha20, SI_SUB_LOCK, SI_ORDER_ANY, chacha20_uninit, NULL);
  163 
  164 
  165 /*
  166  * MPSAFE
  167  */
  168 void
  169 arc4rand(void *ptr, u_int len, int reseed)
  170 {
  171         struct chacha20_s *chacha20;
  172         struct timeval tv;
  173         u_int length;
  174         u_int8_t *p;
  175 
  176         if (reseed || atomic_cmpset_int(&arc4rand_iniseed_state, ARC4_ENTR_HAVE, ARC4_ENTR_SEED))
  177                 CHACHA20_FOREACH(chacha20)
  178                         chacha20_randomstir(chacha20);
  179 
  180         chacha20 = &chacha20inst[curcpu];
  181         getmicrouptime(&tv);
  182         /* We may get unlucky and be migrated off this CPU, but that is expected to be infrequent */
  183         if ((chacha20->numbytes > CHACHA20_RESEED_BYTES) || (tv.tv_sec > chacha20->t_reseed))
  184                 chacha20_randomstir(chacha20);
  185 
  186         mtx_lock(&chacha20->mtx);
  187         p = ptr;
  188         while (len) {
  189                 length = MIN(CHACHA20_BUFFER_SIZE, len);
  190                 chacha_encrypt_bytes(&chacha20->ctx, chacha20->m_buffer, p, length);
  191                 p += length;
  192                 len -= length;
  193                 chacha20->numbytes += length;
  194                 if (chacha20->numbytes > CHACHA20_RESEED_BYTES) {
  195                         mtx_unlock(&chacha20->mtx);
  196                         chacha20_randomstir(chacha20);
  197                         mtx_lock(&chacha20->mtx);
  198                 }
  199         }
  200         mtx_unlock(&chacha20->mtx);
  201 }
  202 
  203 uint32_t
  204 arc4random(void)
  205 {
  206         uint32_t ret;
  207 
  208         arc4rand(&ret, sizeof(ret), 0);
  209         return ret;
  210 }
  211 
  212 void
  213 arc4random_buf(void *ptr, size_t len)
  214 {
  215 
  216         arc4rand(ptr, len, 0);
  217 }

Cache object: a538ec6523a796a1d5b706961b937e6d


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.