625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce/*
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce Authors:
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce Simo Sorce <ssorce@redhat.com>
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce Copyright (C) 2016 Red Hat
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce This program is free software; you can redistribute it and/or modify
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce it under the terms of the GNU General Public License as published by
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce the Free Software Foundation; either version 3 of the License, or
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce (at your option) any later version.
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce This program is distributed in the hope that it will be useful,
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce but WITHOUT ANY WARRANTY; without even the implied warranty of
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce GNU General Public License for more details.
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce You should have received a copy of the GNU General Public License
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce along with this program. If not, see <http://www.gnu.org/licenses/>.
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce*/
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce#include "config.h"
8890a30f5d054187fd7d5b50503f82a49cd025f0Lukas Slebodnik
8890a30f5d054187fd7d5b50503f82a49cd025f0Lukas Slebodnik#include <sys/stat.h>
8890a30f5d054187fd7d5b50503f82a49cd025f0Lukas Slebodnik#include <fcntl.h>
8890a30f5d054187fd7d5b50503f82a49cd025f0Lukas Slebodnik
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce#include "util/util.h"
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce#include "util/crypto/sss_crypto.h"
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorceint generate_csprng_buffer(uint8_t *buf, size_t size)
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce{
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce ssize_t rsize;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce int ret;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce int fd;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce fd = open("/dev/urandom", O_RDONLY);
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce if (fd == -1) return errno;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce
b3a22ee1d91aa4ed1544475be16ec2b7cf886180Jakub Hrozek rsize = sss_atomic_read_s(fd, buf, size);
b3a22ee1d91aa4ed1544475be16ec2b7cf886180Jakub Hrozek if (rsize == -1) {
b3a22ee1d91aa4ed1544475be16ec2b7cf886180Jakub Hrozek ret = errno;
b3a22ee1d91aa4ed1544475be16ec2b7cf886180Jakub Hrozek goto done;
b3a22ee1d91aa4ed1544475be16ec2b7cf886180Jakub Hrozek } else if (rsize != size) {
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce ret = EFAULT;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce goto done;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce }
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce ret = EOK;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorcedone:
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce close(fd);
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce return ret;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce}