VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/posix/rand-posix.cpp@ 62514

Last change on this file since 62514 was 62477, checked in by vboxsync, 8 years ago

(C) 2016

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 4.3 KB
Line 
1/* $Id: rand-posix.cpp 62477 2016-07-22 18:27:37Z vboxsync $ */
2/** @file
3 * IPRT - Random Numbers and Byte Streams, POSIX.
4 */
5
6/*
7 * Copyright (C) 2006-2016 Oracle Corporation
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 (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#include <errno.h>
32#include <sys/stat.h>
33#include <sys/types.h>
34#include <sys/ioctl.h>
35#include <fcntl.h>
36#ifdef _MSC_VER
37# include <io.h>
38# include <stdio.h>
39#else
40# include <unistd.h>
41# include <sys/time.h>
42#endif
43
44#include <iprt/rand.h>
45#include <iprt/mem.h>
46#include <iprt/err.h>
47#include <iprt/assert.h>
48#include "internal/rand.h"
49#include "internal/magics.h"
50
51
52
53/** @copydoc RTRANDINT::pfnGetBytes */
54static DECLCALLBACK(void) rtRandAdvPosixGetBytes(PRTRANDINT pThis, uint8_t *pb, size_t cb)
55{
56 ssize_t cbRead = read(pThis->u.File.hFile, pb, cb);
57 if ((size_t)cbRead != cb)
58 {
59 /* S10 has been observed returning 1040 bytes at the time from /dev/urandom.
60 Which means we need to do than 256 rounds to reach 668171 bytes if
61 that's what demanded by the caller (like tstRTMemWipe.cpp). */
62 ssize_t cTries = RT_MAX(256, cb / 64);
63 do
64 {
65 if (cbRead > 0)
66 {
67 cb -= cbRead;
68 pb += cbRead;
69 }
70 cbRead = read(pThis->u.File.hFile, pb, cb);
71 } while ( (size_t)cbRead != cb
72 && cTries-- > 0);
73 AssertReleaseMsg((size_t)cbRead == cb, ("%zu != %zu, cTries=%zd errno=%d\n", cbRead, cb, cTries, errno));
74 }
75}
76
77
78/** @copydoc RTRANDINT::pfnDestroy */
79static DECLCALLBACK(int) rtRandAdvPosixDestroy(PRTRANDINT pThis)
80{
81 pThis->u32Magic = ~RTRANDINT_MAGIC;
82 int fd = pThis->u.File.hFile;
83 pThis->u.File.hFile = -1;
84 RTMemFree(pThis);
85 close(fd);
86 return VINF_SUCCESS;
87}
88
89
90static int rtRandAdvPosixCreateSystem(PRTRAND phRand, const char *pszDev) RT_NO_THROW_DEF
91{
92 /*
93 * Try open it first and then setup the handle structure.
94 */
95 int fd = open(pszDev, O_RDONLY);
96 if (fd < 0)
97 return RTErrConvertFromErrno(errno);
98 int rc;
99 if (fcntl(fd, F_SETFD, FD_CLOEXEC) != -1)
100 {
101 PRTRANDINT pThis = (PRTRANDINT)RTMemAlloc(sizeof(*pThis));
102 if (pThis)
103 {
104 pThis->u32Magic = RTRANDINT_MAGIC;
105 pThis->pfnGetBytes = rtRandAdvPosixGetBytes;
106 pThis->pfnGetU32 = rtRandAdvSynthesizeU32FromBytes;
107 pThis->pfnGetU64 = rtRandAdvSynthesizeU64FromBytes;
108 pThis->pfnSeed = rtRandAdvStubSeed;
109 pThis->pfnSaveState = rtRandAdvStubSaveState;
110 pThis->pfnRestoreState = rtRandAdvStubRestoreState;
111 pThis->pfnDestroy = rtRandAdvPosixDestroy;
112 pThis->u.File.hFile = fd;
113
114 *phRand = pThis;
115 return VINF_SUCCESS;
116 }
117
118 /* bail out */
119 rc = VERR_NO_MEMORY;
120 }
121 else
122 rc = RTErrConvertFromErrno(errno);
123 close(fd);
124 return rc;
125}
126
127
128RTDECL(int) RTRandAdvCreateSystemFaster(PRTRAND phRand) RT_NO_THROW_DEF
129{
130 return rtRandAdvPosixCreateSystem(phRand, "/dev/urandom");
131}
132
133
134RTDECL(int) RTRandAdvCreateSystemTruer(PRTRAND phRand) RT_NO_THROW_DEF
135{
136 return rtRandAdvPosixCreateSystem(phRand, "/dev/random");
137}
138
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette