summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNorbert Thiebaud <nthiebaud@gmail.com>2015-04-21 20:55:15 -0500
committerMichael Stahl <mstahl@redhat.com>2015-07-20 21:52:34 +0000
commit91457fb326dda7bd1fc6d9e1b3afe0667425121c (patch)
tree935572434278cf9bad8f8e0beb18e78e6c59dd1f
parent5ede1d017cb6e602db699a1b2364f9708de441c8 (diff)
use osl_get_system_random data in rtlRamdomPool
substitute as much as possible getting directly random data from the system rather than mixing our own pseudo-random numbers Fall back on the home-grown method if for some reason system random does not work. (on windows rand_s() is said to be able to return errors, beyond EINVAL, but they are just not listed.. so who knows) Change-Id: I71e88e097a9f3587086a710e9a785d61c560785e Reviewed-on: https://gerrit.libreoffice.org/15474 Reviewed-by: Michael Stahl <mstahl@redhat.com> Tested-by: Michael Stahl <mstahl@redhat.com>
-rw-r--r--sal/rtl/random.cxx39
1 files changed, 30 insertions, 9 deletions
diff --git a/sal/rtl/random.cxx b/sal/rtl/random.cxx
index 1f6d2b42d4cd..f4637131de45 100644
--- a/sal/rtl/random.cxx
+++ b/sal/rtl/random.cxx
@@ -24,7 +24,7 @@
#include <rtl/alloc.h>
#include <rtl/digest.h>
#include <rtl/random.h>
-
+#include "internal/oslrandom.h"
/*========================================================================
*
* rtlRandom internals.
@@ -269,13 +269,19 @@ static void __rtl_random_readPool (
rtlRandomPool SAL_CALL rtl_random_createPool() SAL_THROW_EXTERN_C()
{
RandomPool_Impl *pImpl = nullptr;
+ char sanity[4];
+
+ /* try to get system random number, if it fail fall back on own pool */
pImpl = static_cast<RandomPool_Impl*>(rtl_allocateZeroMemory (sizeof(RandomPool_Impl)));
if (pImpl)
{
- if (!__rtl_random_initPool (pImpl))
+ if(!osl_get_system_random_data(sanity, 4))
{
- rtl_freeZeroMemory (pImpl, sizeof(RandomPool_Impl));
- pImpl = nullptr;
+ if (!__rtl_random_initPool (pImpl))
+ {
+ rtl_freeZeroMemory (pImpl, sizeof(RandomPool_Impl));
+ pImpl = nullptr;
+ }
}
}
return static_cast<rtlRandomPool>(pImpl);
@@ -289,8 +295,11 @@ void SAL_CALL rtl_random_destroyPool (rtlRandomPool Pool) SAL_THROW_EXTERN_C()
RandomPool_Impl *pImpl = static_cast<RandomPool_Impl *>(Pool);
if (pImpl)
{
- rtl_digest_destroy (pImpl->m_hDigest);
- rtl_freeZeroMemory (pImpl, sizeof (RandomPool_Impl));
+ if(pImpl->m_hDigest)
+ {
+ rtl_digest_destroy (pImpl->m_hDigest);
+ rtl_freeZeroMemory (pImpl, sizeof (RandomPool_Impl));
+ }
}
}
@@ -305,8 +314,10 @@ rtlRandomError SAL_CALL rtl_random_addBytes (
if ((pImpl == NULL) || (pBuffer == NULL))
return rtl_Random_E_Argument;
-
- __rtl_random_seedPool (pImpl, pBuffer, Bytes);
+ if(pImpl->m_hDigest)
+ {
+ __rtl_random_seedPool (pImpl, pBuffer, Bytes);
+ }
return rtl_Random_E_None;
}
@@ -322,7 +333,17 @@ rtlRandomError SAL_CALL rtl_random_getBytes (
if ((pImpl == NULL) || (pBuffer == NULL))
return rtl_Random_E_Argument;
- __rtl_random_readPool (pImpl, pBuffer, Bytes);
+ if(pImpl->m_hDigest || !osl_get_system_random_data((char*)Buffer, Bytes))
+ {
+ if(!pImpl->m_hDigest)
+ {
+ if (!__rtl_random_initPool (pImpl))
+ {
+ return rtl_Random_E_Unknown;
+ }
+ }
+ __rtl_random_readPool (pImpl, pBuffer, Bytes);
+ }
return rtl_Random_E_None;
}