“a9ed83a581d01b8330cd1fc867fd8a770342828f”上不存在“git@gitcode.net:openeuler/kernel.git”
ossl_rand.c 17.6 KB
Newer Older
1 2
/*
 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
B
Bodo Möller 已提交
3
 *
4 5 6 7
 * Licensed under the OpenSSL license (the "License").  You may not use
 * this file except in compliance with the License.  You can obtain a copy
 * in the file LICENSE in the source distribution or at
 * https://www.openssl.org/source/license.html
B
Bodo Möller 已提交
8
 */
9 10

#include <stdio.h>
B
Ben Laurie 已提交
11
#include <string.h>
12

13
#include "e_os.h"
14

15
#if !(defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_VXWORKS) || defined(OPENSSL_SYS_DSPBIOS))
16 17 18 19 20 21
# include <sys/time.h>
#endif
#if defined(OPENSSL_SYS_VXWORKS)
# include <time.h>
#endif

22
#include <openssl/opensslconf.h>
23
#include <openssl/crypto.h>
24
#include <openssl/rand.h>
25
#include <openssl/async.h>
26
#include <openssl/err.h>
27
#include <internal/thread_once.h>
R
Rich Salz 已提交
28
#include "rand_lcl.h"
29

30
#if defined(BN_DEBUG) || defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION)
R
Rich Salz 已提交
31
# define PREDICT 1
32 33
#endif

34 35
#define STATE_SIZE      1023

R
Rich Salz 已提交
36 37 38 39 40
typedef struct ossl_rand_state_st OSSL_RAND_STATE;

struct ossl_rand_state_st {
    size_t num;
    size_t index;
R
Rich Salz 已提交
41 42
    unsigned char state[STATE_SIZE + SHA_DIGEST_LENGTH];
    unsigned char md[SHA_DIGEST_LENGTH];
R
Rich Salz 已提交
43 44
    long md_count[2];
};
45

R
Rich Salz 已提交
46 47 48
static OSSL_RAND_STATE global_state;
static double randomness = 0;
static int initialized = 0;
49 50
static CRYPTO_RWLOCK *rand_lock = NULL;
static CRYPTO_RWLOCK *rand_tmp_lock = NULL;
R
Rich Salz 已提交
51 52
static CRYPTO_ONCE ossl_rand_init = CRYPTO_ONCE_STATIC_INIT;
static CRYPTO_THREAD_LOCAL key;
53 54 55

/* May be set only when a thread holds rand_lock (to prevent double locking) */
static unsigned int crypto_lock_rand = 0;
R
Rich Salz 已提交
56 57 58 59
/*
 * access to locking_threadid is synchronized by rand_tmp_lock;
 * valid iff crypto_lock_rand is set
 */
60
static CRYPTO_THREAD_ID locking_threadid;
B
Bodo Möller 已提交
61

62
#ifdef PREDICT
63
int rand_predictable = 0;
64 65
#endif

66
static int rand_hw_seed(EVP_MD_CTX *ctx);
67

R
Rich Salz 已提交
68 69 70
static void rand_thread_cleanup(void *arg)
{
    OSSL_RAND_STATE *sp = arg;
71

R
Rich Salz 已提交
72 73
    OPENSSL_clear_free(sp, sizeof(*sp));
}
74

R
Rich Salz 已提交
75
DEFINE_RUN_ONCE_STATIC(do_ossl_rand_init)
76
{
R
Rich Salz 已提交
77 78
    int ret = 1;

79
    OPENSSL_init_crypto(0, NULL);
80
    rand_lock = CRYPTO_THREAD_lock_new();
R
Rich Salz 已提交
81
    ret &= rand_lock != NULL;
82
    rand_tmp_lock = CRYPTO_THREAD_lock_new();
R
Rich Salz 已提交
83 84 85
    ret &= rand_tmp_lock != NULL;
    ret &= CRYPTO_THREAD_init_local(&key, rand_thread_cleanup) == 1;
    return ret;
86 87
}

88
RAND_METHOD *RAND_OpenSSL(void)
89
{
R
Rich Salz 已提交
90
    return &openssl_rand_meth;
91
}
92

93
static void rand_cleanup(void)
94
{
R
Rich Salz 已提交
95 96
    OPENSSL_cleanse(&global_state, sizeof(global_state));
    randomness = 0;
97
    initialized = 0;
98 99
    CRYPTO_THREAD_lock_free(rand_lock);
    CRYPTO_THREAD_lock_free(rand_tmp_lock);
100
}
101

102
static int rand_add(const void *buf, int num, double add)
103 104 105
{
    int i, j, k, st_idx;
    long md_c[2];
R
Rich Salz 已提交
106
    unsigned char local_md[SHA_DIGEST_LENGTH];
107
    EVP_MD_CTX *m;
108 109
    int do_not_lock;
    int rv = 0;
R
Rich Salz 已提交
110
    OSSL_RAND_STATE *sp = &global_state;
111 112 113 114

    if (!num)
        return 1;

115 116 117 118 119
#ifdef PREDICT
    if (rand_predictable)
        return 1;
#endif

120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
    /*
     * (Based on the rand(3) manpage)
     *
     * The input is chopped up into units of 20 bytes (or less for
     * the last block).  Each of these blocks is run through the hash
     * function as follows:  The data passed to the hash function
     * is the current 'md', the same number of bytes from the 'state'
     * (the location determined by in incremented looping index) as
     * the current 'block', the new key data 'block', and 'count'
     * (which is incremented after each use).
     * The result of this is kept in 'md' and also xored into the
     * 'state' at the same locations that were used as input into the
     * hash function.
     */

135
    m = EVP_MD_CTX_new();
136 137 138
    if (m == NULL)
        goto err;

R
Rich Salz 已提交
139
    if (!RUN_ONCE(&ossl_rand_init, do_ossl_rand_init))
140
        goto err;
141

142 143
    /* check if we already have the lock */
    if (crypto_lock_rand) {
144 145 146 147
        CRYPTO_THREAD_ID cur = CRYPTO_THREAD_get_current_id();
        CRYPTO_THREAD_read_lock(rand_tmp_lock);
        do_not_lock = CRYPTO_THREAD_compare_id(locking_threadid, cur);
        CRYPTO_THREAD_unlock(rand_tmp_lock);
148 149 150 151
    } else
        do_not_lock = 0;

    if (!do_not_lock)
152
        CRYPTO_THREAD_write_lock(rand_lock);
R
Rich Salz 已提交
153
    st_idx = sp->index;
154 155 156 157 158 159

    /*
     * use our own copies of the counters so that even if a concurrent thread
     * seeds with exactly the same data and uses the same subarray there's
     * _some_ difference
     */
R
Rich Salz 已提交
160 161 162 163 164 165 166 167 168 169 170 171 172
    md_c[0] = sp->md_count[0];
    md_c[1] = sp->md_count[1];

    memcpy(local_md, sp->md, sizeof(sp->md));

    /* sp->index <= sp->num <= STATE_SIZE */
    sp->index += num;
    if (sp->index >= STATE_SIZE) {
        sp->index %= STATE_SIZE;
        sp->num = STATE_SIZE;
    } else if (sp->num < STATE_SIZE) {
        if (sp->index > sp->num)
            sp->num = sp->index;
173
    }
R
Rich Salz 已提交
174
    /* sp->index <= sp->num <= STATE_SIZE */
175 176 177 178 179 180

    /*
     * state[st_idx], ..., state[(st_idx + num - 1) % STATE_SIZE] are what we
     * will use now, but other threads may use them as well
     */

R
Rich Salz 已提交
181
    sp->md_count[1] += (num / SHA_DIGEST_LENGTH) + (num % SHA_DIGEST_LENGTH > 0);
182 183

    if (!do_not_lock)
184
        CRYPTO_THREAD_unlock(rand_lock);
185

R
Rich Salz 已提交
186
    for (i = 0; i < num; i += SHA_DIGEST_LENGTH) {
187
        j = (num - i);
R
Rich Salz 已提交
188
        j = (j > SHA_DIGEST_LENGTH) ? SHA_DIGEST_LENGTH : j;
189

R
Rich Salz 已提交
190
        if (!EVP_DigestInit_ex(m, EVP_sha1(), NULL))
191
            goto err;
R
Rich Salz 已提交
192
        if (!EVP_DigestUpdate(m, local_md, SHA_DIGEST_LENGTH))
193 194 195
            goto err;
        k = (st_idx + j) - STATE_SIZE;
        if (k > 0) {
R
Rich Salz 已提交
196
            if (!EVP_DigestUpdate(m, &sp->state[st_idx], j - k))
197
                goto err;
R
Rich Salz 已提交
198
            if (!EVP_DigestUpdate(m, &sp->state[0], k))
199
                goto err;
R
Rich Salz 已提交
200
        } else if (!EVP_DigestUpdate(m, &sp->state[st_idx], j))
201 202
            goto err;

R
Rich Salz 已提交
203 204
        /* DO NOT REMOVE THE FOLLOWING CALL TO EVP_DigestUpdate()! */
        if (!EVP_DigestUpdate(m, buf, j))
205 206 207 208 209 210 211 212 213
            goto err;
        /*
         * We know that line may cause programs such as purify and valgrind
         * to complain about use of uninitialized data.  The problem is not,
         * it's with the caller.  Removing that line will make sure you get
         * really bad randomness and thereby other problems such as very
         * insecure keys.
         */

R
Rich Salz 已提交
214
        if (!EVP_DigestUpdate(m, (unsigned char *)md_c, sizeof(md_c)))
215
            goto err;
R
Rich Salz 已提交
216
        if (!EVP_DigestFinal_ex(m, local_md, NULL))
217 218 219 220 221 222 223 224 225
            goto err;
        md_c[1]++;

        buf = (const char *)buf + j;

        for (k = 0; k < j; k++) {
            /*
             * Parallel threads may interfere with this, but always each byte
             * of the new state is the XOR of some previous value of its and
226
             * local_md (intermediate values may be lost). Alway using locking
227 228 229 230
             * could hurt performance more than necessary given that
             * conflicts occur only when the total seeding is longer than the
             * random state.
             */
R
Rich Salz 已提交
231
            sp->state[st_idx++] ^= local_md[k];
232 233 234 235 236 237
            if (st_idx >= STATE_SIZE)
                st_idx = 0;
        }
    }

    if (!do_not_lock)
238
        CRYPTO_THREAD_write_lock(rand_lock);
239 240 241
    /*
     * Don't just copy back local_md into md -- this could mean that other
     * thread's seeding remains without effect (except for the incremented
R
Rich Salz 已提交
242
     * counter).  By XORing it we keep at least as much randomness as fits into
243 244
     * md.
     */
R
Rich Salz 已提交
245 246
    for (k = 0; k < (int)sizeof(sp->md); k++) {
        sp->md[k] ^= local_md[k];
247
    }
R
Rich Salz 已提交
248 249
    if (randomness < RANDOMNESS_NEEDED) /* stop counting when we have enough */
        randomness += add;
250
    if (!do_not_lock)
251
        CRYPTO_THREAD_unlock(rand_lock);
252 253 254

    rv = 1;
 err:
255
    EVP_MD_CTX_free(m);
256 257
    return rv;
}
258

259
static int rand_seed(const void *buf, int num)
260
{
261
    return rand_add(buf, num, (double)num);
262
}
263

R
Rich Salz 已提交
264
static int rand_bytes(unsigned char *buf, int num)
265 266
{
    static volatile int stirred_pool = 0;
R
Rich Salz 已提交
267 268
    int i, j, k;
    size_t num_ceil, st_idx, st_num;
269 270
    int ok;
    long md_c[2];
R
Rich Salz 已提交
271
    unsigned char local_md[SHA_DIGEST_LENGTH];
272
    EVP_MD_CTX *m;
R
Rich Salz 已提交
273
    OSSL_RAND_STATE *sp = &global_state;
A
Andy Polyakov 已提交
274
#ifndef GETPID_IS_MEANINGLESS
275
    pid_t curr_pid = getpid();
276
#endif
277 278
    time_t curr_time = time(NULL);
    int do_stir_pool = 0;
279 280
/* time value for various platforms */
#ifdef OPENSSL_SYS_WIN32
281
    FILETIME tv;
282
# ifdef _WIN32_WCE
283 284 285
    SYSTEMTIME t;
    GetSystemTime(&t);
    SystemTimeToFileTime(&t, &tv);
286
# else
287
    GetSystemTimeAsFileTime(&tv);
288 289
# endif
#elif defined(OPENSSL_SYS_VXWORKS)
290 291
    struct timespec tv;
    clock_gettime(CLOCK_REALTIME, &ts);
292
#elif defined(OPENSSL_SYS_DSPBIOS)
293 294
    unsigned long long tv, OPENSSL_rdtsc();
    tv = OPENSSL_rdtsc();
295
#else
296 297
    struct timeval tv;
    gettimeofday(&tv, NULL);
298
#endif
299

300
#ifdef PREDICT
301
    if (rand_predictable) {
302
        unsigned char val = 1;
303 304 305 306 307

        for (i = 0; i < num; i++)
            buf[i] = val++;
        return (1);
    }
308 309
#endif

310 311 312
    if (num <= 0)
        return 1;

313
    m = EVP_MD_CTX_new();
314 315 316
    if (m == NULL)
        goto err_mem;

R
Rich Salz 已提交
317
    /* round upwards to multiple of SHA_DIGEST_LENGTH/2 */
318
    num_ceil =
R
Rich Salz 已提交
319
        (1 + (num - 1) / (SHA_DIGEST_LENGTH / 2)) * (SHA_DIGEST_LENGTH / 2);
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338

    /*
     * (Based on the rand(3) manpage:)
     *
     * For each group of 10 bytes (or less), we do the following:
     *
     * Input into the hash function the local 'md' (which is initialized from
     * the global 'md' before any bytes are generated), the bytes that are to
     * be overwritten by the random bytes, and bytes from the 'state'
     * (incrementing looping index). From this digest output (which is kept
     * in 'md'), the top (up to) 10 bytes are returned to the caller and the
     * bottom 10 bytes are xored into the 'state'.
     *
     * Finally, after we have finished 'num' random bytes for the
     * caller, 'count' (which is incremented) and the local and global 'md'
     * are fed into the hash function and the results are kept in the
     * global 'md'.
     */

R
Rich Salz 已提交
339
    if (!RUN_ONCE(&ossl_rand_init, do_ossl_rand_init))
340 341
        goto err_mem;

342
    CRYPTO_THREAD_write_lock(rand_lock);
343 344 345 346 347
    /*
     * We could end up in an async engine while holding this lock so ensure
     * we don't pause and cause a deadlock
     */
    ASYNC_block_pause();
348

349
    /* prevent rand_bytes() from trying to obtain the lock again */
350 351 352
    CRYPTO_THREAD_write_lock(rand_tmp_lock);
    locking_threadid = CRYPTO_THREAD_get_current_id();
    CRYPTO_THREAD_unlock(rand_tmp_lock);
353 354 355 356 357 358 359 360 361 362
    crypto_lock_rand = 1;

    if (!initialized) {
        RAND_poll();
        initialized = 1;
    }

    if (!stirred_pool)
        do_stir_pool = 1;

R
Rich Salz 已提交
363
    ok = (randomness >= RANDOMNESS_NEEDED);
364 365 366 367
    if (!ok) {
        /*
         * If the PRNG state is not yet unpredictable, then seeing the PRNG
         * output may help attackers to determine the new state; thus we have
R
Rich Salz 已提交
368 369
         * to decrease the randomness estimate. Once we've had enough initial
         * seeding we don't bother to adjust the randomness count, though,
370 371
         * because we're not ambitious to provide *information-theoretic*
         * randomness. NOTE: This approach fails if the program forks before
R
Rich Salz 已提交
372 373 374
         * we have enough randomness. Randomness should be collected in a
         * separate input pool and be transferred to the output pool only
         * when the randomness limit has been reached.
375
         */
R
Rich Salz 已提交
376 377 378
        randomness -= num;
        if (randomness < 0)
            randomness = 0;
379 380 381 382 383
    }

    if (do_stir_pool) {
        /*
         * In the output function only half of 'md' remains secret, so we
R
Rich Salz 已提交
384
         * better make sure that the required randomness gets 'evenly
385
         * distributed' through 'state', our randomness pool. The input
386
         * function (rand_add) chains all of 'md', which makes it more
387 388 389 390 391
         * suitable for this purpose.
         */

        int n = STATE_SIZE;     /* so that the complete pool gets accessed */
        while (n > 0) {
R
Rich Salz 已提交
392
#if SHA_DIGEST_LENGTH > 20
B
Bodo Möller 已提交
393 394
# error "Please adjust DUMMY_SEED."
#endif
R
Rich Salz 已提交
395
#define DUMMY_SEED "...................." /* at least SHA_DIGEST_LENGTH */
396 397
            /*
             * Note that the seed does not matter, it's just that
398
             * rand_add expects to have something to hash.
399
             */
R
Rich Salz 已提交
400 401
            rand_add(DUMMY_SEED, SHA_DIGEST_LENGTH, 0.0);
            n -= SHA_DIGEST_LENGTH;
402 403 404 405 406
        }
        if (ok)
            stirred_pool = 1;
    }

R
Rich Salz 已提交
407 408 409 410 411
    st_idx = sp->index;
    st_num = sp->num;
    md_c[0] = sp->md_count[0];
    md_c[1] = sp->md_count[1];
    memcpy(local_md, sp->md, sizeof sp->md);
412

R
Rich Salz 已提交
413 414 415
    sp->index += num_ceil;
    if (sp->index > sp->num)
        sp->index %= sp->num;
416 417 418 419 420 421

    /*
     * state[st_idx], ..., state[(st_idx + num_ceil - 1) % st_num] are now
     * ours (but other threads may use them too)
     */

R
Rich Salz 已提交
422
    sp->md_count[0] += 1;
423 424 425

    /* before unlocking, we must clear 'crypto_lock_rand' */
    crypto_lock_rand = 0;
426
    ASYNC_unblock_pause();
427
    CRYPTO_THREAD_unlock(rand_lock);
428 429

    while (num > 0) {
R
Rich Salz 已提交
430 431
        /* num_ceil -= SHA_DIGEST_LENGTH / 2 */
        j = (num >= SHA_DIGEST_LENGTH / 2) ? SHA_DIGEST_LENGTH / 2 : num;
432
        num -= j;
R
Rich Salz 已提交
433
        if (!EVP_DigestInit_ex(m, EVP_sha1(), NULL))
434
            goto err;
A
Andy Polyakov 已提交
435
#ifndef GETPID_IS_MEANINGLESS
436
        if (curr_pid) {         /* just in the first iteration to save time */
R
Rich Salz 已提交
437
            if (!EVP_DigestUpdate(m, (unsigned char *)&curr_pid, sizeof curr_pid))
438 439 440
                goto err;
            curr_pid = 0;
        }
441
#endif
442
        if (curr_time) {        /* just in the first iteration to save time */
R
Rich Salz 已提交
443
            if (!EVP_DigestUpdate(m, (unsigned char *)&curr_time, sizeof curr_time))
444
                goto err;
R
Rich Salz 已提交
445
            if (!EVP_DigestUpdate(m, (unsigned char *)&tv, sizeof tv))
446 447
                goto err;
            curr_time = 0;
448 449
            if (!rand_hw_seed(m))
                goto err;
450
        }
R
Rich Salz 已提交
451
        if (!EVP_DigestUpdate(m, local_md, SHA_DIGEST_LENGTH))
452
            goto err;
R
Rich Salz 已提交
453
        if (!EVP_DigestUpdate(m, (unsigned char *)md_c, sizeof(md_c)))
454 455
            goto err;

R
Rich Salz 已提交
456
        k = (st_idx + SHA_DIGEST_LENGTH / 2) - st_num;
457
        if (k > 0) {
R
Rich Salz 已提交
458
            if (!EVP_DigestUpdate(m, &sp->state[st_idx], SHA_DIGEST_LENGTH / 2 - k))
459
                goto err;
R
Rich Salz 已提交
460
            if (!EVP_DigestUpdate(m, &sp->state[0], k))
461
                goto err;
R
Rich Salz 已提交
462
        } else if (!EVP_DigestUpdate(m, &sp->state[st_idx], SHA_DIGEST_LENGTH / 2))
463
            goto err;
R
Rich Salz 已提交
464
        if (!EVP_DigestFinal_ex(m, local_md, NULL))
465 466
            goto err;

R
Rich Salz 已提交
467
        for (i = 0; i < SHA_DIGEST_LENGTH / 2; i++) {
468
            /* may compete with other threads */
R
Rich Salz 已提交
469
            sp->state[st_idx++] ^= local_md[i];
470 471 472
            if (st_idx >= st_num)
                st_idx = 0;
            if (i < j)
R
Rich Salz 已提交
473
                *(buf++) = local_md[i + SHA_DIGEST_LENGTH / 2];
474 475 476
        }
    }

R
Rich Salz 已提交
477
    if (!EVP_DigestInit_ex(m, EVP_sha1(), NULL)
R
Rich Salz 已提交
478
        || !EVP_DigestUpdate(m, (unsigned char *)md_c, sizeof(md_c))
R
Rich Salz 已提交
479
        || !EVP_DigestUpdate(m, local_md, SHA_DIGEST_LENGTH))
480
        goto err;
481
    CRYPTO_THREAD_write_lock(rand_lock);
482 483 484 485
    /*
     * Prevent deadlocks if we end up in an async engine
     */
    ASYNC_block_pause();
R
Rich Salz 已提交
486 487
    if (!EVP_DigestUpdate(m, sp->md, sizeof(sp->md))
            || !EVP_DigestFinal_ex(m, sp->md, NULL)) {
488
        CRYPTO_THREAD_unlock(rand_lock);
489 490
        goto err;
    }
491
    ASYNC_unblock_pause();
492
    CRYPTO_THREAD_unlock(rand_lock);
493

494
    EVP_MD_CTX_free(m);
495 496
    if (ok)
        return (1);
R
Rich Salz 已提交
497 498 499 500
    RANDerr(RAND_F_RAND_BYTES, RAND_R_PRNG_NOT_SEEDED);
    ERR_add_error_data(1, "You need to read the OpenSSL FAQ, "
                       "https://www.openssl.org/docs/faq.html");
    return (0);
501
 err:
502
    RANDerr(RAND_F_RAND_BYTES, ERR_R_EVP_LIB);
503
    EVP_MD_CTX_free(m);
504 505 506
    return 0;
 err_mem:
    RANDerr(RAND_F_RAND_BYTES, ERR_R_MALLOC_FAILURE);
507
    EVP_MD_CTX_free(m);
508 509 510
    return 0;

}
511

512
static int rand_status(void)
513
{
514
    CRYPTO_THREAD_ID cur;
515 516 517
    int ret;
    int do_not_lock;

R
Rich Salz 已提交
518
    if (!RUN_ONCE(&ossl_rand_init, do_ossl_rand_init))
519 520
        return 0;

521
    cur = CRYPTO_THREAD_get_current_id();
522 523 524 525 526
    /*
     * check if we already have the lock (could happen if a RAND_poll()
     * implementation calls RAND_status())
     */
    if (crypto_lock_rand) {
527 528 529
        CRYPTO_THREAD_read_lock(rand_tmp_lock);
        do_not_lock = CRYPTO_THREAD_compare_id(locking_threadid, cur);
        CRYPTO_THREAD_unlock(rand_tmp_lock);
530 531 532 533
    } else
        do_not_lock = 0;

    if (!do_not_lock) {
534
        CRYPTO_THREAD_write_lock(rand_lock);
535 536 537 538
        /*
         * Prevent deadlocks in case we end up in an async engine
         */
        ASYNC_block_pause();
539 540

        /*
541
         * prevent rand_bytes() from trying to obtain the lock again
542
         */
543 544 545
        CRYPTO_THREAD_write_lock(rand_tmp_lock);
        locking_threadid = cur;
        CRYPTO_THREAD_unlock(rand_tmp_lock);
546 547 548 549 550 551 552 553
        crypto_lock_rand = 1;
    }

    if (!initialized) {
        RAND_poll();
        initialized = 1;
    }

R
Rich Salz 已提交
554
    ret = randomness >= RANDOMNESS_NEEDED;
555 556 557 558 559

    if (!do_not_lock) {
        /* before unlocking, we must clear 'crypto_lock_rand' */
        crypto_lock_rand = 0;

560
        ASYNC_unblock_pause();
561
        CRYPTO_THREAD_unlock(rand_lock);
562 563 564 565 566 567 568 569
    }

    return ret;
}

/*
 * rand_hw_seed: get seed data from any available hardware RNG. only
 * currently supports rdrand.
570 571 572
 */
#if (defined(__i386)   || defined(__i386__)   || defined(_M_IX86) || \
     defined(__x86_64) || defined(__x86_64__) || \
573 574
     defined(_M_AMD64) || defined (_M_X64)) && defined(OPENSSL_CPUID_OBJ) \
     && !defined(OPENSSL_NO_RDRAND)
575

576
# define RDRAND_CALLS    4
577 578 579 580

size_t OPENSSL_ia32_rdrand(void);
extern unsigned int OPENSSL_ia32cap_P[];

581
static int rand_hw_seed(EVP_MD_CTX *ctx)
582 583 584
{
    int i;
    if (!(OPENSSL_ia32cap_P[1] & (1 << (62 - 32))))
585
        return 1;
586 587 588 589
    for (i = 0; i < RDRAND_CALLS; i++) {
        size_t rnd;
        rnd = OPENSSL_ia32_rdrand();
        if (rnd == 0)
590
            return 1;
R
Rich Salz 已提交
591
        if (!EVP_DigestUpdate(ctx, (unsigned char *)&rnd, sizeof(size_t)))
592
            return 0;
593
    }
594
    return 1;
595
}
596 597 598

#else

599
static int rand_hw_seed(EVP_MD_CTX *ctx)
600
{
601
    return 1;
602
}
603 604

#endif
R
Rich Salz 已提交
605 606 607 608 609 610 611 612 613 614


RAND_METHOD openssl_rand_meth = {
    rand_seed,
    rand_bytes,
    rand_cleanup,
    rand_add,
    rand_bytes,
    rand_status
};
新手
引导
客服 返回
顶部