1 | /* $Id: rand-posix.cpp 4071 2007-08-07 17:07:59Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * innotek Portable Runtime - Random Numbers and Byte Streams, POSIX.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 innotek GmbH
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License as published by the Free Software Foundation,
|
---|
13 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
14 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
15 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 |
|
---|
19 | /*******************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *******************************************************************************/
|
---|
22 | #include <errno.h>
|
---|
23 | #include <sys/stat.h>
|
---|
24 | #include <sys/types.h>
|
---|
25 | #include <sys/ioctl.h>
|
---|
26 | #include <sys/fcntl.h>
|
---|
27 | #include <fcntl.h>
|
---|
28 | #ifdef _MSC_VER
|
---|
29 | # include <io.h>
|
---|
30 | # include <stdio.h>
|
---|
31 | #else
|
---|
32 | # include <unistd.h>
|
---|
33 | # include <sys/time.h>
|
---|
34 | #endif
|
---|
35 |
|
---|
36 | #include <iprt/rand.h>
|
---|
37 | #include <iprt/err.h>
|
---|
38 | #include <iprt/assert.h>
|
---|
39 | #include "internal/rand.h"
|
---|
40 |
|
---|
41 |
|
---|
42 | /*******************************************************************************
|
---|
43 | * Global Variables *
|
---|
44 | *******************************************************************************/
|
---|
45 | /** File handle of /dev/random. */
|
---|
46 | static int g_fhDevRandom = -1;
|
---|
47 |
|
---|
48 |
|
---|
49 | void rtRandLazyInitNative(void)
|
---|
50 | {
|
---|
51 | if (g_fhDevRandom != -1)
|
---|
52 | return;
|
---|
53 |
|
---|
54 | int fh = open("/dev/urandom", O_RDONLY);
|
---|
55 | if (fh <= 0)
|
---|
56 | fh = open("/dev/random", O_RDONLY | O_NONBLOCK);
|
---|
57 | if (fh >= 0)
|
---|
58 | {
|
---|
59 | fcntl(fh, F_SETFD, FD_CLOEXEC);
|
---|
60 | g_fhDevRandom = fh;
|
---|
61 | }
|
---|
62 | }
|
---|
63 |
|
---|
64 |
|
---|
65 | int rtRandGenBytesNative(void *pv, size_t cb)
|
---|
66 | {
|
---|
67 | int fh = g_fhDevRandom;
|
---|
68 | if (fh == -1)
|
---|
69 | return VERR_NOT_SUPPORTED;
|
---|
70 |
|
---|
71 | ssize_t cbRead = read(fh, pv, cb);
|
---|
72 | if ((size_t)cbRead != cb)
|
---|
73 | {
|
---|
74 | /*
|
---|
75 | * Use the fallback for the remainder if /dev/urandom / /dev/random
|
---|
76 | * is out to lunch.
|
---|
77 | */
|
---|
78 | if (cbRead <= 0)
|
---|
79 | rtRandGenBytesFallback(pv, cb);
|
---|
80 | else
|
---|
81 | {
|
---|
82 | AssertRelease((size_t)cbRead < cb);
|
---|
83 | rtRandGenBytesFallback((uint8_t *)pv + cbRead, cb - cbRead);
|
---|
84 | }
|
---|
85 | }
|
---|
86 | return VINF_SUCCESS;
|
---|
87 | }
|
---|
88 |
|
---|