238712671ba3d4ba568e0ef1ab0c5e5210dfae00
[openssl.git] / crypto / rand / rand_lib.c
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
16 #include <openssl/engine.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 */
25 static ENGINE *funct_ref = NULL;
26 #endif
27 static const RAND_METHOD *default_RAND_meth = NULL;
28
29 int RAND_set_rand_method(const RAND_METHOD *meth)
30 {
31 #ifndef OPENSSL_NO_ENGINE
32     ENGINE_finish(funct_ref);
33     funct_ref = NULL;
34 #endif
35     default_RAND_meth = meth;
36     return 1;
37 }
38
39 const RAND_METHOD *RAND_get_rand_method(void)
40 {
41     if (!default_RAND_meth) {
42 #ifndef OPENSSL_NO_ENGINE
43         ENGINE *e = ENGINE_get_default_RAND();
44         if (e) {
45             default_RAND_meth = ENGINE_get_RAND(e);
46             if (default_RAND_meth == NULL) {
47                 ENGINE_finish(e);
48                 e = NULL;
49             }
50         }
51         if (e)
52             funct_ref = e;
53         else
54 #endif
55             default_RAND_meth = RAND_OpenSSL();
56     }
57     return default_RAND_meth;
58 }
59
60 #ifndef OPENSSL_NO_ENGINE
61 int RAND_set_rand_engine(ENGINE *engine)
62 {
63     const RAND_METHOD *tmp_meth = NULL;
64     if (engine) {
65         if (!ENGINE_init(engine))
66             return 0;
67         tmp_meth = ENGINE_get_RAND(engine);
68         if (tmp_meth == NULL) {
69             ENGINE_finish(engine);
70             return 0;
71         }
72     }
73     /* This function releases any prior ENGINE so call it first */
74     RAND_set_rand_method(tmp_meth);
75     funct_ref = engine;
76     return 1;
77 }
78 #endif
79
80 void rand_cleanup_int(void)
81 {
82     const RAND_METHOD *meth = RAND_get_rand_method();
83     if (meth && meth->cleanup)
84         meth->cleanup();
85     RAND_set_rand_method(NULL);
86 }
87
88 void RAND_seed(const void *buf, int num)
89 {
90     const RAND_METHOD *meth = RAND_get_rand_method();
91     if (meth && meth->seed)
92         meth->seed(buf, num);
93 }
94
95 void RAND_add(const void *buf, int num, double entropy)
96 {
97     const RAND_METHOD *meth = RAND_get_rand_method();
98     if (meth && meth->add)
99         meth->add(buf, num, entropy);
100 }
101
102 int RAND_bytes(unsigned char *buf, int num)
103 {
104     const RAND_METHOD *meth = RAND_get_rand_method();
105     if (meth && meth->bytes)
106         return meth->bytes(buf, num);
107     return (-1);
108 }
109
110 #if OPENSSL_API_COMPAT < 0x10100000L
111 int RAND_pseudo_bytes(unsigned char *buf, int num)
112 {
113     const RAND_METHOD *meth = RAND_get_rand_method();
114     if (meth && meth->pseudorand)
115         return meth->pseudorand(buf, num);
116     return (-1);
117 }
118 #endif
119
120 int RAND_status(void)
121 {
122     const RAND_METHOD *meth = RAND_get_rand_method();
123     if (meth && meth->status)
124         return meth->status();
125     return 0;
126 }