LCOV - code coverage report
Current view: top level - include/linux - random.h (source / functions) Hit Total Coverage
Test: Real Lines: 2 4 50.0 %
Date: 2020-10-17 15:46:16 Functions: 0 0 -
Legend: Neither, QEMU, Real, Both Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: GPL-2.0 */
       2                 :            : /*
       3                 :            :  * include/linux/random.h
       4                 :            :  *
       5                 :            :  * Include file for the random number generator.
       6                 :            :  */
       7                 :            : #ifndef _LINUX_RANDOM_H
       8                 :            : #define _LINUX_RANDOM_H
       9                 :            : 
      10                 :            : #include <linux/list.h>
      11                 :            : #include <linux/once.h>
      12                 :            : 
      13                 :            : #include <uapi/linux/random.h>
      14                 :            : 
      15                 :            : struct random_ready_callback {
      16                 :            :         struct list_head list;
      17                 :            :         void (*func)(struct random_ready_callback *rdy);
      18                 :            :         struct module *owner;
      19                 :            : };
      20                 :            : 
      21                 :            : extern void add_device_randomness(const void *, unsigned int);
      22                 :            : extern void add_bootloader_randomness(const void *, unsigned int);
      23                 :            : 
      24                 :            : #if defined(LATENT_ENTROPY_PLUGIN) && !defined(__CHECKER__)
      25                 :            : static inline void add_latent_entropy(void)
      26                 :            : {
      27                 :            :         add_device_randomness((const void *)&latent_entropy,
      28                 :            :                               sizeof(latent_entropy));
      29                 :            : }
      30                 :            : #else
      31                 :            : static inline void add_latent_entropy(void) {}
      32                 :            : #endif
      33                 :            : 
      34                 :            : extern void add_input_randomness(unsigned int type, unsigned int code,
      35                 :            :                                  unsigned int value) __latent_entropy;
      36                 :            : extern void add_interrupt_randomness(int irq, int irq_flags) __latent_entropy;
      37                 :            : 
      38                 :            : extern void get_random_bytes(void *buf, int nbytes);
      39                 :            : extern int wait_for_random_bytes(void);
      40                 :            : extern int __init rand_initialize(void);
      41                 :            : extern bool rng_is_initialized(void);
      42                 :            : extern int add_random_ready_callback(struct random_ready_callback *rdy);
      43                 :            : extern void del_random_ready_callback(struct random_ready_callback *rdy);
      44                 :            : extern int __must_check get_random_bytes_arch(void *buf, int nbytes);
      45                 :            : 
      46                 :            : #ifndef MODULE
      47                 :            : extern const struct file_operations random_fops, urandom_fops;
      48                 :            : #endif
      49                 :            : 
      50                 :            : u32 get_random_u32(void);
      51                 :            : u64 get_random_u64(void);
      52                 :            : static inline unsigned int get_random_int(void)
      53                 :            : {
      54                 :          3 :         return get_random_u32();
      55                 :            : }
      56                 :            : static inline unsigned long get_random_long(void)
      57                 :            : {
      58                 :            : #if BITS_PER_LONG == 64
      59                 :            :         return get_random_u64();
      60                 :            : #else
      61                 :          3 :         return get_random_u32();
      62                 :            : #endif
      63                 :            : }
      64                 :            : 
      65                 :            : /*
      66                 :            :  * On 64-bit architectures, protect against non-terminated C string overflows
      67                 :            :  * by zeroing out the first byte of the canary; this leaves 56 bits of entropy.
      68                 :            :  */
      69                 :            : #ifdef CONFIG_64BIT
      70                 :            : # ifdef __LITTLE_ENDIAN
      71                 :            : #  define CANARY_MASK 0xffffffffffffff00UL
      72                 :            : # else /* big endian, 64 bits: */
      73                 :            : #  define CANARY_MASK 0x00ffffffffffffffUL
      74                 :            : # endif
      75                 :            : #else /* 32 bits: */
      76                 :            : # define CANARY_MASK 0xffffffffUL
      77                 :            : #endif
      78                 :            : 
      79                 :            : static inline unsigned long get_random_canary(void)
      80                 :            : {
      81                 :            :         unsigned long val = get_random_long();
      82                 :            : 
      83                 :            :         return val & CANARY_MASK;
      84                 :            : }
      85                 :            : 
      86                 :            : /* Calls wait_for_random_bytes() and then calls get_random_bytes(buf, nbytes).
      87                 :            :  * Returns the result of the call to wait_for_random_bytes. */
      88                 :            : static inline int get_random_bytes_wait(void *buf, int nbytes)
      89                 :            : {
      90                 :          0 :         int ret = wait_for_random_bytes();
      91                 :          0 :         get_random_bytes(buf, nbytes);
      92                 :            :         return ret;
      93                 :            : }
      94                 :            : 
      95                 :            : #define declare_get_random_var_wait(var) \
      96                 :            :         static inline int get_random_ ## var ## _wait(var *out) { \
      97                 :            :                 int ret = wait_for_random_bytes(); \
      98                 :            :                 if (unlikely(ret)) \
      99                 :            :                         return ret; \
     100                 :            :                 *out = get_random_ ## var(); \
     101                 :            :                 return 0; \
     102                 :            :         }
     103                 :            : declare_get_random_var_wait(u32)
     104                 :            : declare_get_random_var_wait(u64)
     105                 :            : declare_get_random_var_wait(int)
     106                 :            : declare_get_random_var_wait(long)
     107                 :            : #undef declare_get_random_var
     108                 :            : 
     109                 :            : unsigned long randomize_page(unsigned long start, unsigned long range);
     110                 :            : 
     111                 :            : /*
     112                 :            :  * This is designed to be standalone for just prandom
     113                 :            :  * users, but for now we include it from <linux/random.h>
     114                 :            :  * for legacy reasons.
     115                 :            :  */
     116                 :            : #include <linux/prandom.h>
     117                 :            : 
     118                 :            : #ifdef CONFIG_ARCH_RANDOM
     119                 :            : # include <asm/archrandom.h>
     120                 :            : #else
     121                 :            : static inline bool arch_get_random_long(unsigned long *v)
     122                 :            : {
     123                 :            :         return 0;
     124                 :            : }
     125                 :            : static inline bool arch_get_random_int(unsigned int *v)
     126                 :            : {
     127                 :            :         return 0;
     128                 :            : }
     129                 :            : static inline bool arch_has_random(void)
     130                 :            : {
     131                 :            :         return 0;
     132                 :            : }
     133                 :            : static inline bool arch_get_random_seed_long(unsigned long *v)
     134                 :            : {
     135                 :            :         return 0;
     136                 :            : }
     137                 :            : static inline bool arch_get_random_seed_int(unsigned int *v)
     138                 :            : {
     139                 :            :         return 0;
     140                 :            : }
     141                 :            : static inline bool arch_has_random_seed(void)
     142                 :            : {
     143                 :            :         return 0;
     144                 :            : }
     145                 :            : #endif
     146                 :            : 
     147                 :            : #endif /* _LINUX_RANDOM_H */
    

Generated by: LCOV version 1.14