LCOV - code coverage report
Current view: top level - basic - random-util.h (source / functions) Hit Total Coverage
Test: systemd_full.info Lines: 6 6 100.0 %
Date: 2019-08-23 13:36:53 Functions: 2 2 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: LGPL-2.1+ */
       2                 :            : #pragma once
       3                 :            : 
       4                 :            : #include <stdbool.h>
       5                 :            : #include <stddef.h>
       6                 :            : #include <stdint.h>
       7                 :            : 
       8                 :            : typedef enum RandomFlags {
       9                 :            :         RANDOM_EXTEND_WITH_PSEUDO = 1 << 0, /* If we can't get enough genuine randomness, but some, fill up the rest with pseudo-randomness */
      10                 :            :         RANDOM_BLOCK              = 1 << 1, /* Rather block than return crap randomness (only if the kernel supports that) */
      11                 :            :         RANDOM_MAY_FAIL           = 1 << 2, /* If we can't get any randomness at all, return early with -ENODATA */
      12                 :            :         RANDOM_ALLOW_RDRAND       = 1 << 3, /* Allow usage of the CPU RNG */
      13                 :            : } RandomFlags;
      14                 :            : 
      15                 :            : int genuine_random_bytes(void *p, size_t n, RandomFlags flags); /* returns "genuine" randomness, optionally filled up with pseudo random, if not enough is available */
      16                 :            : void pseudo_random_bytes(void *p, size_t n);                    /* returns only pseudo-randommess (but possibly seeded from something better) */
      17                 :            : void random_bytes(void *p, size_t n);                           /* returns genuine randomness if cheaply available, and pseudo randomness if not. */
      18                 :            : 
      19                 :            : void initialize_srand(void);
      20                 :            : 
      21                 :        180 : static inline uint64_t random_u64(void) {
      22                 :            :         uint64_t u;
      23                 :        180 :         random_bytes(&u, sizeof(u));
      24                 :        180 :         return u;
      25                 :            : }
      26                 :            : 
      27                 :         56 : static inline uint32_t random_u32(void) {
      28                 :            :         uint32_t u;
      29                 :         56 :         random_bytes(&u, sizeof(u));
      30                 :         56 :         return u;
      31                 :            : }
      32                 :            : 
      33                 :            : int rdrand(unsigned long *ret);
      34                 :            : 
      35                 :            : /* Some limits on the pool sizes when we deal with the kernel random pool */
      36                 :            : #define RANDOM_POOL_SIZE_MIN 512U
      37                 :            : #define RANDOM_POOL_SIZE_MAX (10U*1024U*1024U)
      38                 :            : 
      39                 :            : size_t random_pool_size(void);

Generated by: LCOV version 1.14