summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/rand/rand_lib.c
diff options
context:
space:
mode:
authormarkus <>2002-09-05 12:51:50 +0000
committermarkus <>2002-09-05 12:51:50 +0000
commit15b5d84f9da2ce4bfae8580e56e34a859f74ad71 (patch)
treebf939e82d7fd73cc8a01cf6959002209972091bc /src/lib/libcrypto/rand/rand_lib.c
parent027351f729b9e837200dae6e1520cda6577ab930 (diff)
downloadopenbsd-15b5d84f9da2ce4bfae8580e56e34a859f74ad71.tar.gz
openbsd-15b5d84f9da2ce4bfae8580e56e34a859f74ad71.tar.bz2
openbsd-15b5d84f9da2ce4bfae8580e56e34a859f74ad71.zip
import openssl-0.9.7-beta1
Diffstat (limited to 'src/lib/libcrypto/rand/rand_lib.c')
-rw-r--r--src/lib/libcrypto/rand/rand_lib.c104
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 */
65static RAND_METHOD *rand_meth=NULL; 66static ENGINE *funct_ref =NULL;
66#else 67static const RAND_METHOD *default_RAND_meth = NULL;
67extern RAND_METHOD rand_ssleay_meth;
68static RAND_METHOD *rand_meth= &rand_ssleay_meth;
69#endif
70 68
71void RAND_set_rand_method(RAND_METHOD *meth) 69int 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
76RAND_METHOD *RAND_get_rand_method(void) 80const 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
102int 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
81void RAND_cleanup(void) 122void 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
87void RAND_seed(const void *buf, int num) 130void 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
137void 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
93void RAND_bytes(unsigned char *buf, int num) 144int 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
152int 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
160int 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 }