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