Kaydet (Commit) 91457fb3 authored tarafından Norbert Thiebaud's avatar Norbert Thiebaud Kaydeden (comit) Michael Stahl

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/15474Reviewed-by: 's avatarMichael Stahl <mstahl@redhat.com>
Tested-by: 's avatarMichael Stahl <mstahl@redhat.com>
üst 5ede1d01
...@@ -24,7 +24,7 @@ ...@@ -24,7 +24,7 @@
#include <rtl/alloc.h> #include <rtl/alloc.h>
#include <rtl/digest.h> #include <rtl/digest.h>
#include <rtl/random.h> #include <rtl/random.h>
#include "internal/oslrandom.h"
/*======================================================================== /*========================================================================
* *
* rtlRandom internals. * rtlRandom internals.
...@@ -269,13 +269,19 @@ static void __rtl_random_readPool ( ...@@ -269,13 +269,19 @@ static void __rtl_random_readPool (
rtlRandomPool SAL_CALL rtl_random_createPool() SAL_THROW_EXTERN_C() rtlRandomPool SAL_CALL rtl_random_createPool() SAL_THROW_EXTERN_C()
{ {
RandomPool_Impl *pImpl = nullptr; 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))); pImpl = static_cast<RandomPool_Impl*>(rtl_allocateZeroMemory (sizeof(RandomPool_Impl)));
if (pImpl) if (pImpl)
{ {
if (!__rtl_random_initPool (pImpl)) if(!osl_get_system_random_data(sanity, 4))
{ {
rtl_freeZeroMemory (pImpl, sizeof(RandomPool_Impl)); if (!__rtl_random_initPool (pImpl))
pImpl = nullptr; {
rtl_freeZeroMemory (pImpl, sizeof(RandomPool_Impl));
pImpl = nullptr;
}
} }
} }
return static_cast<rtlRandomPool>(pImpl); return static_cast<rtlRandomPool>(pImpl);
...@@ -289,8 +295,11 @@ void SAL_CALL rtl_random_destroyPool (rtlRandomPool Pool) SAL_THROW_EXTERN_C() ...@@ -289,8 +295,11 @@ void SAL_CALL rtl_random_destroyPool (rtlRandomPool Pool) SAL_THROW_EXTERN_C()
RandomPool_Impl *pImpl = static_cast<RandomPool_Impl *>(Pool); RandomPool_Impl *pImpl = static_cast<RandomPool_Impl *>(Pool);
if (pImpl) if (pImpl)
{ {
rtl_digest_destroy (pImpl->m_hDigest); if(pImpl->m_hDigest)
rtl_freeZeroMemory (pImpl, sizeof (RandomPool_Impl)); {
rtl_digest_destroy (pImpl->m_hDigest);
rtl_freeZeroMemory (pImpl, sizeof (RandomPool_Impl));
}
} }
} }
...@@ -305,8 +314,10 @@ rtlRandomError SAL_CALL rtl_random_addBytes ( ...@@ -305,8 +314,10 @@ rtlRandomError SAL_CALL rtl_random_addBytes (
if ((pImpl == NULL) || (pBuffer == NULL)) if ((pImpl == NULL) || (pBuffer == NULL))
return rtl_Random_E_Argument; return rtl_Random_E_Argument;
if(pImpl->m_hDigest)
__rtl_random_seedPool (pImpl, pBuffer, Bytes); {
__rtl_random_seedPool (pImpl, pBuffer, Bytes);
}
return rtl_Random_E_None; return rtl_Random_E_None;
} }
...@@ -322,7 +333,17 @@ rtlRandomError SAL_CALL rtl_random_getBytes ( ...@@ -322,7 +333,17 @@ rtlRandomError SAL_CALL rtl_random_getBytes (
if ((pImpl == NULL) || (pBuffer == NULL)) if ((pImpl == NULL) || (pBuffer == NULL))
return rtl_Random_E_Argument; 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; return rtl_Random_E_None;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment