rand_lib.c 4.1 KB
Newer Older
R
Rich Salz 已提交
1 2
/*
 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3
 *
R
Rich Salz 已提交
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
8 9 10 11
 */

#include <stdio.h>
#include <time.h>
12
#include "internal/cryptlib.h"
13
#include <openssl/opensslconf.h>
R
Rich Salz 已提交
14
#include "internal/rand_int.h"
R
Rich Salz 已提交
15
#include <openssl/engine.h>
16
#include "internal/thread_once.h"
R
Rich Salz 已提交
17
#include "rand_lcl.h"
18

19
#ifndef OPENSSL_NO_ENGINE
20
/* non-NULL if default_RAND_meth is ENGINE-provided */
R
Rich Salz 已提交
21 22
static ENGINE *funct_ref;
static CRYPTO_RWLOCK *rand_engine_lock;
23
#endif
R
Rich Salz 已提交
24 25 26
static CRYPTO_RWLOCK *rand_meth_lock;
static const RAND_METHOD *default_RAND_meth;
static CRYPTO_ONCE rand_init = CRYPTO_ONCE_STATIC_INIT;
27

R
Rich Salz 已提交
28 29

DEFINE_RUN_ONCE_STATIC(do_rand_init)
30
{
31
    int ret = 1;
32 33
#ifndef OPENSSL_NO_ENGINE
    rand_engine_lock = CRYPTO_THREAD_lock_new();
34
    ret &= rand_engine_lock != NULL;
35 36
#endif
    rand_meth_lock = CRYPTO_THREAD_lock_new();
37 38
    ret &= rand_meth_lock != NULL;
    return ret;
39
}
40

R
Rich Salz 已提交
41 42 43 44 45 46 47 48 49 50 51
void rand_cleanup_int(void)
{
    const RAND_METHOD *meth = default_RAND_meth;

    if (meth != NULL && meth->cleanup != NULL)
        meth->cleanup();
    RAND_set_rand_method(NULL);
#ifndef OPENSSL_NO_ENGINE
    CRYPTO_THREAD_lock_free(rand_engine_lock);
#endif
    CRYPTO_THREAD_lock_free(rand_meth_lock);
R
Rich Salz 已提交
52
    rand_drbg_cleanup();
R
Rich Salz 已提交
53 54
}

55
int RAND_set_rand_method(const RAND_METHOD *meth)
56
{
R
Rich Salz 已提交
57
    if (!RUN_ONCE(&rand_init, do_rand_init))
58 59 60
        return 0;

    CRYPTO_THREAD_write_lock(rand_meth_lock);
61
#ifndef OPENSSL_NO_ENGINE
R
Rich Salz 已提交
62 63
    ENGINE_finish(funct_ref);
    funct_ref = NULL;
64
#endif
65
    default_RAND_meth = meth;
66
    CRYPTO_THREAD_unlock(rand_meth_lock);
67 68
    return 1;
}
69

70
const RAND_METHOD *RAND_get_rand_method(void)
71
{
72 73
    const RAND_METHOD *tmp_meth = NULL;

R
Rich Salz 已提交
74
    if (!RUN_ONCE(&rand_init, do_rand_init))
75 76 77
        return NULL;

    CRYPTO_THREAD_write_lock(rand_meth_lock);
R
Rich Salz 已提交
78
    if (default_RAND_meth == NULL) {
79
#ifndef OPENSSL_NO_ENGINE
R
Rich Salz 已提交
80 81 82 83 84
        ENGINE *e;

        /* If we have an engine that can do RAND, use it. */
        if ((e = ENGINE_get_default_RAND()) != NULL
                && (tmp_meth = ENGINE_get_RAND(e)) != NULL) {
85
            funct_ref = e;
R
Rich Salz 已提交
86 87 88 89 90 91 92
            default_RAND_meth = tmp_meth;
        } else {
            ENGINE_finish(e);
            default_RAND_meth = &openssl_rand_meth;
        }
#else
        default_RAND_meth = &openssl_rand_meth;
93
#endif
94
    }
95 96 97
    tmp_meth = default_RAND_meth;
    CRYPTO_THREAD_unlock(rand_meth_lock);
    return tmp_meth;
98
}
99

100
#ifndef OPENSSL_NO_ENGINE
101
int RAND_set_rand_engine(ENGINE *engine)
102 103
{
    const RAND_METHOD *tmp_meth = NULL;
104

R
Rich Salz 已提交
105
    if (!RUN_ONCE(&rand_init, do_rand_init))
106 107
        return 0;

R
Rich Salz 已提交
108
    if (engine != NULL) {
109 110 111
        if (!ENGINE_init(engine))
            return 0;
        tmp_meth = ENGINE_get_RAND(engine);
R
Rich Salz 已提交
112
        if (tmp_meth == NULL) {
113 114 115 116
            ENGINE_finish(engine);
            return 0;
        }
    }
117
    CRYPTO_THREAD_write_lock(rand_engine_lock);
118 119 120
    /* This function releases any prior ENGINE so call it first */
    RAND_set_rand_method(tmp_meth);
    funct_ref = engine;
121
    CRYPTO_THREAD_unlock(rand_engine_lock);
122 123
    return 1;
}
124
#endif
125

126
void RAND_seed(const void *buf, int num)
127 128
{
    const RAND_METHOD *meth = RAND_get_rand_method();
R
Rich Salz 已提交
129 130

    if (meth->seed != NULL)
131 132
        meth->seed(buf, num);
}
133

R
Rich Salz 已提交
134
void RAND_add(const void *buf, int num, double randomness)
135 136
{
    const RAND_METHOD *meth = RAND_get_rand_method();
R
Rich Salz 已提交
137 138 139

    if (meth->add != NULL)
        meth->add(buf, num, randomness);
140
}
141

142
int RAND_bytes(unsigned char *buf, int num)
143 144
{
    const RAND_METHOD *meth = RAND_get_rand_method();
R
Rich Salz 已提交
145 146

    if (meth->bytes != NULL)
147
        return meth->bytes(buf, num);
R
Rich Salz 已提交
148
    RANDerr(RAND_F_RAND_BYTES, RAND_R_FUNC_NOT_IMPLEMENTED);
R
Rich Salz 已提交
149
    return -1;
150
}
151

152
#if OPENSSL_API_COMPAT < 0x10100000L
153
int RAND_pseudo_bytes(unsigned char *buf, int num)
154 155
{
    const RAND_METHOD *meth = RAND_get_rand_method();
R
Rich Salz 已提交
156 157

    if (meth->pseudorand != NULL)
158
        return meth->pseudorand(buf, num);
R
Rich Salz 已提交
159
    return -1;
160
}
M
Matt Caswell 已提交
161
#endif
162 163

int RAND_status(void)
164 165
{
    const RAND_METHOD *meth = RAND_get_rand_method();
R
Rich Salz 已提交
166 167

    if (meth->status != NULL)
168 169 170
        return meth->status();
    return 0;
}