]>
Commit | Line | Data |
---|---|---|
b1322259 RS |
1 | /* |
2 | * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. | |
dfeab068 | 3 | * |
b1322259 RS |
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 | |
dfeab068 RE |
8 | */ |
9 | ||
10 | #include <stdio.h> | |
dfeab068 | 11 | #include <time.h> |
b39fc560 | 12 | #include "internal/cryptlib.h" |
98186eb4 | 13 | #include <openssl/opensslconf.h> |
f3cd81d6 | 14 | #include "internal/rand.h" |
4ead4e52 | 15 | |
3c27208f | 16 | #include <openssl/engine.h> |
dfeab068 | 17 | |
0b13e9f0 | 18 | #ifndef OPENSSL_NO_ENGINE |
cb78486d | 19 | /* non-NULL if default_RAND_meth is ENGINE-provided */ |
0f113f3e | 20 | static ENGINE *funct_ref = NULL; |
0b13e9f0 | 21 | #endif |
cb78486d | 22 | static const RAND_METHOD *default_RAND_meth = NULL; |
dfeab068 | 23 | |
cb78486d | 24 | int RAND_set_rand_method(const RAND_METHOD *meth) |
0f113f3e | 25 | { |
0b13e9f0 | 26 | #ifndef OPENSSL_NO_ENGINE |
7c96dbcd RS |
27 | ENGINE_finish(funct_ref); |
28 | funct_ref = NULL; | |
0b13e9f0 | 29 | #endif |
0f113f3e MC |
30 | default_RAND_meth = meth; |
31 | return 1; | |
32 | } | |
dfeab068 | 33 | |
a4a9d97a | 34 | const RAND_METHOD *RAND_get_rand_method(void) |
0f113f3e MC |
35 | { |
36 | if (!default_RAND_meth) { | |
0b13e9f0 | 37 | #ifndef OPENSSL_NO_ENGINE |
0f113f3e MC |
38 | ENGINE *e = ENGINE_get_default_RAND(); |
39 | if (e) { | |
40 | default_RAND_meth = ENGINE_get_RAND(e); | |
7c96dbcd | 41 | if (default_RAND_meth == NULL) { |
0f113f3e MC |
42 | ENGINE_finish(e); |
43 | e = NULL; | |
44 | } | |
45 | } | |
46 | if (e) | |
47 | funct_ref = e; | |
48 | else | |
0b13e9f0 | 49 | #endif |
b0700d2c | 50 | default_RAND_meth = RAND_OpenSSL(); |
0f113f3e MC |
51 | } |
52 | return default_RAND_meth; | |
53 | } | |
cb78486d | 54 | |
0b13e9f0 | 55 | #ifndef OPENSSL_NO_ENGINE |
cb78486d | 56 | int RAND_set_rand_engine(ENGINE *engine) |
0f113f3e MC |
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); | |
7c96dbcd | 63 | if (tmp_meth == NULL) { |
0f113f3e MC |
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 | } | |
0b13e9f0 | 73 | #endif |
dfeab068 | 74 | |
b3599dbb | 75 | void rand_cleanup_int(void) |
0f113f3e | 76 | { |
5006b37b | 77 | const RAND_METHOD *meth = default_RAND_meth; |
0f113f3e MC |
78 | if (meth && meth->cleanup) |
79 | meth->cleanup(); | |
80 | RAND_set_rand_method(NULL); | |
81 | } | |
dfeab068 | 82 | |
6343829a | 83 | void RAND_seed(const void *buf, int num) |
0f113f3e MC |
84 | { |
85 | const RAND_METHOD *meth = RAND_get_rand_method(); | |
86 | if (meth && meth->seed) | |
87 | meth->seed(buf, num); | |
88 | } | |
dfeab068 | 89 | |
6343829a | 90 | void RAND_add(const void *buf, int num, double entropy) |
0f113f3e MC |
91 | { |
92 | const RAND_METHOD *meth = RAND_get_rand_method(); | |
93 | if (meth && meth->add) | |
94 | meth->add(buf, num, entropy); | |
95 | } | |
eb952088 | 96 | |
6343829a | 97 | int RAND_bytes(unsigned char *buf, int num) |
0f113f3e MC |
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 | } | |
dfeab068 | 104 | |
98186eb4 | 105 | #if OPENSSL_API_COMPAT < 0x10100000L |
6343829a | 106 | int RAND_pseudo_bytes(unsigned char *buf, int num) |
0f113f3e MC |
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 | } | |
302d38e3 | 113 | #endif |
5eb8ca4d BM |
114 | |
115 | int RAND_status(void) | |
0f113f3e MC |
116 | { |
117 | const RAND_METHOD *meth = RAND_get_rand_method(); | |
118 | if (meth && meth->status) | |
119 | return meth->status(); | |
120 | return 0; | |
121 | } |