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