Ignore:
Timestamp:
Apr 14, 2017, 9:41:36 PM (11 days ago)
Author:
brainslayer
Message:

kernel update

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/linux/universal/linux-4.9/drivers/char/random.c

    r31574 r31859  
    20962096#endif  /* CONFIG_SYSCTL */
    20972097
    2098 static u32 random_int_secret[MD5_MESSAGE_BYTES / 4] ____cacheline_aligned;
    2099 
    2100 int random_int_secret_init(void)
    2101 {
    2102         get_random_bytes(random_int_secret, sizeof(random_int_secret));
    2103         return 0;
    2104 }
    2105 
    2106 static DEFINE_PER_CPU(__u32 [MD5_DIGEST_WORDS], get_random_int_hash)
    2107                 __aligned(sizeof(unsigned long));
    2108 
    2109 /*
    2110  * Get a random word for internal kernel use only. Similar to urandom but
    2111  * with the goal of minimal entropy pool depletion. As a result, the random
    2112  * value is not cryptographically secure but for several uses the cost of
    2113  * depleting entropy is too high
    2114  */
     2098struct batched_entropy {
     2099        union {
     2100                unsigned long entropy_long[CHACHA20_BLOCK_SIZE / sizeof(unsigned long)];
     2101                unsigned int entropy_int[CHACHA20_BLOCK_SIZE / sizeof(unsigned int)];
     2102        };
     2103        unsigned int position;
     2104};
     2105
     2106/*
     2107 * Get a random word for internal kernel use only. The quality of the random
     2108 * number is either as good as RDRAND or as good as /dev/urandom, with the
     2109 * goal of being quite fast and not depleting entropy.
     2110 */
     2111static DEFINE_PER_CPU(struct batched_entropy, batched_entropy_long);
     2112unsigned long get_random_long(void)
     2113{
     2114        unsigned long ret;
     2115        struct batched_entropy *batch;
     2116
     2117        if (arch_get_random_long(&ret))
     2118                return ret;
     2119
     2120        batch = &get_cpu_var(batched_entropy_long);
     2121        if (batch->position % ARRAY_SIZE(batch->entropy_long) == 0) {
     2122                extract_crng((u8 *)batch->entropy_long);
     2123                batch->position = 0;
     2124        }
     2125        ret = batch->entropy_long[batch->position++];
     2126        put_cpu_var(batched_entropy_long);
     2127        return ret;
     2128}
     2129EXPORT_SYMBOL(get_random_long);
     2130
     2131#if BITS_PER_LONG == 32
    21152132unsigned int get_random_int(void)
    21162133{
    2117         __u32 *hash;
     2134        return get_random_long();
     2135}
     2136#else
     2137static DEFINE_PER_CPU(struct batched_entropy, batched_entropy_int);
     2138unsigned int get_random_int(void)
     2139{
    21182140        unsigned int ret;
     2141        struct batched_entropy *batch;
    21192142
    21202143        if (arch_get_random_int(&ret))
    21212144                return ret;
    21222145
    2123         hash = get_cpu_var(get_random_int_hash);
    2124 
    2125         hash[0] += current->pid + jiffies + random_get_entropy();
    2126         md5_transform(hash, random_int_secret);
    2127         ret = hash[0];
    2128         put_cpu_var(get_random_int_hash);
    2129 
     2146        batch = &get_cpu_var(batched_entropy_int);
     2147        if (batch->position % ARRAY_SIZE(batch->entropy_int) == 0) {
     2148                extract_crng((u8 *)batch->entropy_int);
     2149                batch->position = 0;
     2150        }
     2151        ret = batch->entropy_int[batch->position++];
     2152        put_cpu_var(batched_entropy_int);
    21302153        return ret;
    21312154}
     2155#endif
    21322156EXPORT_SYMBOL(get_random_int);
    2133 
    2134 /*
    2135  * Same as get_random_int(), but returns unsigned long.
    2136  */
    2137 unsigned long get_random_long(void)
    2138 {
    2139         __u32 *hash;
    2140         unsigned long ret;
    2141 
    2142         if (arch_get_random_long(&ret))
    2143                 return ret;
    2144 
    2145         hash = get_cpu_var(get_random_int_hash);
    2146 
    2147         hash[0] += current->pid + jiffies + random_get_entropy();
    2148         md5_transform(hash, random_int_secret);
    2149         ret = *(unsigned long *)hash;
    2150         put_cpu_var(get_random_int_hash);
    2151 
    2152         return ret;
    2153 }
    2154 EXPORT_SYMBOL(get_random_long);
    21552157
    21562158/**
Note: See TracChangeset for help on using the changeset viewer.