add an internal api to osl to collect 'native' random numbers
use /dev/urandom on unixes and rand_s() on windows Change-Id: Iccd6e01706f16d82f23cda0f0a410953c32fbc02 Reviewed-on: https://gerrit.libreoffice.org/15473 Reviewed-by: Michael Stahl <mstahl@redhat.com> Tested-by: Michael Stahl <mstahl@redhat.com>
This commit is contained in:
parent
04fb0ba0cc
commit
5ede1d017c
4 changed files with 136 additions and 0 deletions
|
@ -162,6 +162,7 @@ $(eval $(call gb_Library_add_exception_objects,sal,\
|
|||
sal/osl/unx/process \
|
||||
sal/osl/unx/process_impl \
|
||||
sal/osl/unx/profile \
|
||||
sal/osl/unx/random \
|
||||
sal/osl/unx/readwrite_helper \
|
||||
sal/osl/unx/security \
|
||||
sal/osl/unx/signal \
|
||||
|
@ -236,6 +237,7 @@ $(eval $(call gb_Library_add_cobjects,sal,\
|
|||
sal/osl/w32/mutex \
|
||||
sal/osl/w32/nlsupport \
|
||||
sal/osl/w32/pipe \
|
||||
sal/osl/w32/random \
|
||||
sal/osl/w32/security \
|
||||
sal/osl/w32/thread \
|
||||
sal/osl/w32/time \
|
||||
|
|
25
sal/inc/internal/oslrandom.h
Normal file
25
sal/inc/internal/oslrandom.h
Normal file
|
@ -0,0 +1,25 @@
|
|||
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
||||
/*
|
||||
* This file is part of the LibreOffice project.
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
#ifndef INCLUDED_SAL_INC_INTERNAL_OSLRANDOM_H
|
||||
#define INCLUDED_SAL_INC_INTERNAL_OSLRANDOM_H
|
||||
|
||||
#if defined __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
int osl_get_system_random_data(char* buffer, size_t desired_len);
|
||||
|
||||
#if defined __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|
48
sal/osl/unx/random.cxx
Normal file
48
sal/osl/unx/random.cxx
Normal file
|
@ -0,0 +1,48 @@
|
|||
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
||||
/*
|
||||
* This file is part of the LibreOffice project.
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
#include "system.hxx"
|
||||
#include "internal/oslrandom.h"
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
int osl_get_system_random_data(char* buffer, size_t desired_len)
|
||||
{
|
||||
ssize_t nb_read;
|
||||
int fd;
|
||||
|
||||
assert(buffer);
|
||||
fd = open("/dev/urandom", O_RDONLY);
|
||||
if(fd != -1)
|
||||
{
|
||||
while(desired_len)
|
||||
{
|
||||
if ((nb_read = read(fd, buffer, desired_len)) == -1)
|
||||
{
|
||||
if (errno != EINTR)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
buffer += nb_read;
|
||||
desired_len -= nb_read;
|
||||
}
|
||||
}
|
||||
close(fd);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|
61
sal/osl/w32/random.c
Normal file
61
sal/osl/w32/random.c
Normal file
|
@ -0,0 +1,61 @@
|
|||
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
||||
/*
|
||||
* This file is part of the LibreOffice project.
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
#define _CRT_RAND_S
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <memory.h>
|
||||
|
||||
int osl_get_system_random_data(char* buffer, size_t desired_len)
|
||||
{
|
||||
unsigned int val;
|
||||
|
||||
/* if unaligned fill to alignment */
|
||||
if((int)buffer & 3)
|
||||
{
|
||||
size_t len = 4 - ((size_t)(buffer) & 3);
|
||||
|
||||
if(len > desired_len)
|
||||
{
|
||||
len = desired_len;
|
||||
}
|
||||
if(rand_s(&val))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
memcpy(buffer, &val, len);
|
||||
buffer += len;
|
||||
desired_len -= len;
|
||||
}
|
||||
/* fill directly into the buffer as long as we can */
|
||||
while(desired_len >= 4)
|
||||
{
|
||||
if(rand_s((unsigned int*)buffer))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
buffer += 4;
|
||||
desired_len -= 4;
|
||||
}
|
||||
}
|
||||
/* deal with the partial int reminder to fill */
|
||||
if(desired_len)
|
||||
{
|
||||
if(rand_s(&val))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
memcpy(buffer, &val, desired_len);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|
Loading…
Reference in a new issue