| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #if !defined(_DEFAULT_SOURCE) |
| 16 | #define _DEFAULT_SOURCE // Needed for getentropy on musl and glibc |
| 17 | #endif |
| 18 |
| 19 | #include <openssl/rand.h> |
| 20 |
| 21 | #include "../bcm_support.h" |
| 22 | #include "internal.h" |
| 23 |
| 24 | #if defined(OPENSSL_RAND_GETENTROPY) |
| 25 |
| 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <unistd.h> |
| 29 |
| 30 | #if defined(OPENSSL_MACOS) || defined(OPENSSL_FUCHSIA) |
| 31 | #include <sys/random.h> |
| 32 | #endif |
| 33 |
| 34 | void CRYPTO_init_sysrand(void) {} |
| 35 |
| 36 | // CRYPTO_sysrand puts |requested| random bytes into |out|. |
| 37 | void CRYPTO_sysrand(uint8_t *out, size_t requested) { |
| 38 | while (requested > 0) { |
| 39 | // |getentropy| can only request 256 bytes at a time. |
| 40 | size_t todo = requested <= 256 ? requested : 256; |
| 41 | if (getentropy(out, todo) != 0) { |
| 42 | perror("getentropy() failed"); |
| 43 | abort(); |
| 44 | } |
| 45 |
| 46 | out += todo; |
| 47 | requested -= todo; |
| 48 | } |
| 49 | } |
| 50 |
| 51 | int CRYPTO_sysrand_if_available(uint8_t *buf, size_t len) { |
| 52 | CRYPTO_sysrand(buf, len); |
| 53 | return 1; |
| 54 | } |
| 55 |
| 56 | void CRYPTO_sysrand_for_seed(uint8_t *out, size_t requested) { |
| 57 | CRYPTO_sysrand(out, requested); |
| 58 | } |
| 59 |
| 60 | #endif // OPENSSL_RAND_GETENTROPY |
| 61 |