Line data Source code
1 : /**
2 : * Copyright Notice:
3 : * Copyright 2021-2022 DMTF. All rights reserved.
4 : * License: BSD 3-Clause License. For full text see link: https://github.com/DMTF/libspdm/blob/main/LICENSE.md
5 : **/
6 :
7 : #include <base.h>
8 : #include <stdlib.h>
9 : #include "stdio.h"
10 : #include <unistd.h>
11 : #include <fcntl.h>
12 : #include <assert.h>
13 :
14 : /**
15 : * Generates a 64-bit random number.
16 : *
17 : * if rand is NULL, then LIBSPDM_ASSERT().
18 : *
19 : * @param[out] rand_data buffer pointer to store the 64-bit random value.
20 : *
21 : * @retval true Random number generated successfully.
22 : * @retval false Failed to generate the random number.
23 : *
24 : **/
25 13956 : bool libspdm_get_random_number_64(uint64_t *rand_data)
26 : {
27 : int fd;
28 :
29 13956 : assert(rand_data != NULL);
30 :
31 13956 : fd = open("/dev/urandom", O_RDONLY);
32 13956 : if (fd < 0) {
33 0 : printf("cannot open /dev/urandom\n");
34 0 : return false;
35 : }
36 13956 : if (read(fd, rand_data, sizeof(*rand_data)) != sizeof(*rand_data)) {
37 0 : printf("Cannot read /dev/urandom\n");
38 0 : close(fd);
39 0 : return false;
40 : }
41 13956 : close(fd);
42 :
43 13956 : return true;
44 : }
|