diff options
Diffstat (limited to 'src/lib/libcrypto/rand/rand_lib.c')
-rw-r--r-- | src/lib/libcrypto/rand/rand_lib.c | 104 |
1 files changed, 86 insertions, 18 deletions
diff --git a/src/lib/libcrypto/rand/rand_lib.c b/src/lib/libcrypto/rand/rand_lib.c index 34c6d5b968..5cf5dc1188 100644 --- a/src/lib/libcrypto/rand/rand_lib.c +++ b/src/lib/libcrypto/rand/rand_lib.c | |||
@@ -57,42 +57,110 @@ | |||
57 | */ | 57 | */ |
58 | 58 | ||
59 | #include <stdio.h> | 59 | #include <stdio.h> |
60 | #include <sys/types.h> | ||
61 | #include <time.h> | 60 | #include <time.h> |
61 | #include "cryptlib.h" | ||
62 | #include <openssl/rand.h> | 62 | #include <openssl/rand.h> |
63 | #include <openssl/engine.h> | ||
63 | 64 | ||
64 | #ifdef NO_RAND | 65 | /* non-NULL if default_RAND_meth is ENGINE-provided */ |
65 | static RAND_METHOD *rand_meth=NULL; | 66 | static ENGINE *funct_ref =NULL; |
66 | #else | 67 | static const RAND_METHOD *default_RAND_meth = NULL; |
67 | extern RAND_METHOD rand_ssleay_meth; | ||
68 | static RAND_METHOD *rand_meth= &rand_ssleay_meth; | ||
69 | #endif | ||
70 | 68 | ||
71 | void RAND_set_rand_method(RAND_METHOD *meth) | 69 | int RAND_set_rand_method(const RAND_METHOD *meth) |
72 | { | 70 | { |
73 | rand_meth=meth; | 71 | if(funct_ref) |
72 | { | ||
73 | ENGINE_finish(funct_ref); | ||
74 | funct_ref = NULL; | ||
75 | } | ||
76 | default_RAND_meth = meth; | ||
77 | return 1; | ||
74 | } | 78 | } |
75 | 79 | ||
76 | RAND_METHOD *RAND_get_rand_method(void) | 80 | const RAND_METHOD *RAND_get_rand_method(void) |
77 | { | 81 | { |
78 | return(rand_meth); | 82 | if (!default_RAND_meth) |
83 | { | ||
84 | ENGINE *e = ENGINE_get_default_RAND(); | ||
85 | if(e) | ||
86 | { | ||
87 | default_RAND_meth = ENGINE_get_RAND(e); | ||
88 | if(!default_RAND_meth) | ||
89 | { | ||
90 | ENGINE_finish(e); | ||
91 | e = NULL; | ||
92 | } | ||
93 | } | ||
94 | if(e) | ||
95 | funct_ref = e; | ||
96 | else | ||
97 | default_RAND_meth = RAND_SSLeay(); | ||
98 | } | ||
99 | return default_RAND_meth; | ||
100 | } | ||
101 | |||
102 | int RAND_set_rand_engine(ENGINE *engine) | ||
103 | { | ||
104 | const RAND_METHOD *tmp_meth = NULL; | ||
105 | if(engine) | ||
106 | { | ||
107 | if(!ENGINE_init(engine)) | ||
108 | return 0; | ||
109 | tmp_meth = ENGINE_get_RAND(engine); | ||
110 | if(!tmp_meth) | ||
111 | { | ||
112 | ENGINE_finish(engine); | ||
113 | return 0; | ||
114 | } | ||
115 | } | ||
116 | /* This function releases any prior ENGINE so call it first */ | ||
117 | RAND_set_rand_method(tmp_meth); | ||
118 | funct_ref = engine; | ||
119 | return 1; | ||
79 | } | 120 | } |
80 | 121 | ||
81 | void RAND_cleanup(void) | 122 | void RAND_cleanup(void) |
82 | { | 123 | { |
83 | if (rand_meth != NULL) | 124 | const RAND_METHOD *meth = RAND_get_rand_method(); |
84 | rand_meth->cleanup(); | 125 | if (meth && meth->cleanup) |
126 | meth->cleanup(); | ||
127 | RAND_set_rand_method(NULL); | ||
85 | } | 128 | } |
86 | 129 | ||
87 | void RAND_seed(const void *buf, int num) | 130 | void RAND_seed(const void *buf, int num) |
88 | { | 131 | { |
89 | if (rand_meth != NULL) | 132 | const RAND_METHOD *meth = RAND_get_rand_method(); |
90 | rand_meth->seed(buf,num); | 133 | if (meth && meth->seed) |
134 | meth->seed(buf,num); | ||
135 | } | ||
136 | |||
137 | void RAND_add(const void *buf, int num, double entropy) | ||
138 | { | ||
139 | const RAND_METHOD *meth = RAND_get_rand_method(); | ||
140 | if (meth && meth->add) | ||
141 | meth->add(buf,num,entropy); | ||
91 | } | 142 | } |
92 | 143 | ||
93 | void RAND_bytes(unsigned char *buf, int num) | 144 | int RAND_bytes(unsigned char *buf, int num) |
94 | { | 145 | { |
95 | if (rand_meth != NULL) | 146 | const RAND_METHOD *meth = RAND_get_rand_method(); |
96 | rand_meth->bytes(buf,num); | 147 | if (meth && meth->bytes) |
148 | return meth->bytes(buf,num); | ||
149 | return(-1); | ||
97 | } | 150 | } |
98 | 151 | ||
152 | int RAND_pseudo_bytes(unsigned char *buf, int num) | ||
153 | { | ||
154 | const RAND_METHOD *meth = RAND_get_rand_method(); | ||
155 | if (meth && meth->pseudorand) | ||
156 | return meth->pseudorand(buf,num); | ||
157 | return(-1); | ||
158 | } | ||
159 | |||
160 | int RAND_status(void) | ||
161 | { | ||
162 | const RAND_METHOD *meth = RAND_get_rand_method(); | ||
163 | if (meth && meth->status) | ||
164 | return meth->status(); | ||
165 | return 0; | ||
166 | } | ||