VirtualBox

source: vbox/trunk/src/libs/openssl-1.1.0g/crypto/rand/rand_lib.c@ 69890

Last change on this file since 69890 was 69890, checked in by vboxsync, 7 years ago

Added OpenSSL 1.1.0g with unneeded files removed, otherwise unmodified.
bugref:8070: src/libs maintenance

  • Property svn:eol-style set to native
File size: 4.1 KB
Line 
1/*
2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10#include <stdio.h>
11#include <time.h>
12#include "internal/cryptlib.h"
13#include <openssl/opensslconf.h>
14#include "internal/rand.h"
15#include <openssl/engine.h>
16#include "internal/thread_once.h"
17
18#ifdef OPENSSL_FIPS
19# include <openssl/fips.h>
20# include <openssl/fips_rand.h>
21#endif
22
23#ifndef OPENSSL_NO_ENGINE
24/* non-NULL if default_RAND_meth is ENGINE-provided */
25static ENGINE *funct_ref = NULL;
26static CRYPTO_RWLOCK *rand_engine_lock = NULL;
27#endif
28static const RAND_METHOD *default_RAND_meth = NULL;
29static CRYPTO_RWLOCK *rand_meth_lock = NULL;
30static CRYPTO_ONCE rand_lock_init = CRYPTO_ONCE_STATIC_INIT;
31
32DEFINE_RUN_ONCE_STATIC(do_rand_lock_init)
33{
34 int ret = 1;
35#ifndef OPENSSL_NO_ENGINE
36 rand_engine_lock = CRYPTO_THREAD_lock_new();
37 ret &= rand_engine_lock != NULL;
38#endif
39 rand_meth_lock = CRYPTO_THREAD_lock_new();
40 ret &= rand_meth_lock != NULL;
41 return ret;
42}
43
44int RAND_set_rand_method(const RAND_METHOD *meth)
45{
46 if (!RUN_ONCE(&rand_lock_init, do_rand_lock_init))
47 return 0;
48
49 CRYPTO_THREAD_write_lock(rand_meth_lock);
50#ifndef OPENSSL_NO_ENGINE
51 ENGINE_finish(funct_ref);
52 funct_ref = NULL;
53#endif
54 default_RAND_meth = meth;
55 CRYPTO_THREAD_unlock(rand_meth_lock);
56 return 1;
57}
58
59const RAND_METHOD *RAND_get_rand_method(void)
60{
61 const RAND_METHOD *tmp_meth = NULL;
62
63 if (!RUN_ONCE(&rand_lock_init, do_rand_lock_init))
64 return NULL;
65
66 CRYPTO_THREAD_write_lock(rand_meth_lock);
67 if (!default_RAND_meth) {
68#ifndef OPENSSL_NO_ENGINE
69 ENGINE *e = ENGINE_get_default_RAND();
70 if (e) {
71 default_RAND_meth = ENGINE_get_RAND(e);
72 if (default_RAND_meth == NULL) {
73 ENGINE_finish(e);
74 e = NULL;
75 }
76 }
77 if (e)
78 funct_ref = e;
79 else
80#endif
81 default_RAND_meth = RAND_OpenSSL();
82 }
83 tmp_meth = default_RAND_meth;
84 CRYPTO_THREAD_unlock(rand_meth_lock);
85 return tmp_meth;
86}
87
88#ifndef OPENSSL_NO_ENGINE
89int RAND_set_rand_engine(ENGINE *engine)
90{
91 const RAND_METHOD *tmp_meth = NULL;
92
93 if (!RUN_ONCE(&rand_lock_init, do_rand_lock_init))
94 return 0;
95
96 if (engine) {
97 if (!ENGINE_init(engine))
98 return 0;
99 tmp_meth = ENGINE_get_RAND(engine);
100 if (tmp_meth == NULL) {
101 ENGINE_finish(engine);
102 return 0;
103 }
104 }
105 CRYPTO_THREAD_write_lock(rand_engine_lock);
106 /* This function releases any prior ENGINE so call it first */
107 RAND_set_rand_method(tmp_meth);
108 funct_ref = engine;
109 CRYPTO_THREAD_unlock(rand_engine_lock);
110 return 1;
111}
112#endif
113
114void rand_cleanup_int(void)
115{
116 const RAND_METHOD *meth = default_RAND_meth;
117 if (meth && meth->cleanup)
118 meth->cleanup();
119 RAND_set_rand_method(NULL);
120 CRYPTO_THREAD_lock_free(rand_meth_lock);
121#ifndef OPENSSL_NO_ENGINE
122 CRYPTO_THREAD_lock_free(rand_engine_lock);
123#endif
124}
125
126void RAND_seed(const void *buf, int num)
127{
128 const RAND_METHOD *meth = RAND_get_rand_method();
129 if (meth && meth->seed)
130 meth->seed(buf, num);
131}
132
133void RAND_add(const void *buf, int num, double entropy)
134{
135 const RAND_METHOD *meth = RAND_get_rand_method();
136 if (meth && meth->add)
137 meth->add(buf, num, entropy);
138}
139
140int RAND_bytes(unsigned char *buf, int num)
141{
142 const RAND_METHOD *meth = RAND_get_rand_method();
143 if (meth && meth->bytes)
144 return meth->bytes(buf, num);
145 return (-1);
146}
147
148#if OPENSSL_API_COMPAT < 0x10100000L
149int RAND_pseudo_bytes(unsigned char *buf, int num)
150{
151 const RAND_METHOD *meth = RAND_get_rand_method();
152 if (meth && meth->pseudorand)
153 return meth->pseudorand(buf, num);
154 return (-1);
155}
156#endif
157
158int RAND_status(void)
159{
160 const RAND_METHOD *meth = RAND_get_rand_method();
161 if (meth && meth->status)
162 return meth->status();
163 return 0;
164}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette