mirror of
https://git.proxmox.com/git/mirror_zfs.git
synced 2026-03-11 04:46:18 +03:00
85 lines
2.0 KiB
C
85 lines
2.0 KiB
C
|
|
// SPDX-License-Identifier: CDDL-1.0
|
||
|
|
/*
|
||
|
|
* CDDL HEADER START
|
||
|
|
*
|
||
|
|
* The contents of this file are subject to the terms of the
|
||
|
|
* Common Development and Distribution License (the "License").
|
||
|
|
* You may not use this file except in compliance with the License.
|
||
|
|
*
|
||
|
|
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
|
||
|
|
* or https://opensource.org/licenses/CDDL-1.0.
|
||
|
|
* See the License for the specific language governing permissions
|
||
|
|
* and limitations under the License.
|
||
|
|
*
|
||
|
|
* When distributing Covered Code, include this CDDL HEADER in each
|
||
|
|
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
|
||
|
|
* If applicable, add the following below this CDDL HEADER, with the
|
||
|
|
* fields enclosed by brackets "[]" replaced with your own identifying
|
||
|
|
* information: Portions Copyright [yyyy] [name of copyright owner]
|
||
|
|
*
|
||
|
|
* CDDL HEADER END
|
||
|
|
*/
|
||
|
|
/*
|
||
|
|
* Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
|
||
|
|
* Copyright (c) 2012, 2018 by Delphix. All rights reserved.
|
||
|
|
* Copyright (c) 2016 Actifio, Inc. All rights reserved.
|
||
|
|
* Copyright (c) 2025, Klara, Inc.
|
||
|
|
*/
|
||
|
|
|
||
|
|
#include <stdint.h>
|
||
|
|
#include <fcntl.h>
|
||
|
|
#include <assert.h>
|
||
|
|
#include <sys/random.h>
|
||
|
|
#include "libspl_impl.h"
|
||
|
|
|
||
|
|
const char *random_path = "/dev/random";
|
||
|
|
const char *urandom_path = "/dev/urandom";
|
||
|
|
static int random_fd = -1, urandom_fd = -1;
|
||
|
|
|
||
|
|
void
|
||
|
|
random_init(void)
|
||
|
|
{
|
||
|
|
VERIFY((random_fd = open(random_path, O_RDONLY | O_CLOEXEC)) != -1);
|
||
|
|
VERIFY((urandom_fd = open(urandom_path, O_RDONLY | O_CLOEXEC)) != -1);
|
||
|
|
}
|
||
|
|
|
||
|
|
void
|
||
|
|
random_fini(void)
|
||
|
|
{
|
||
|
|
close(random_fd);
|
||
|
|
close(urandom_fd);
|
||
|
|
|
||
|
|
random_fd = -1;
|
||
|
|
urandom_fd = -1;
|
||
|
|
}
|
||
|
|
|
||
|
|
static int
|
||
|
|
random_get_bytes_common(uint8_t *ptr, size_t len, int fd)
|
||
|
|
{
|
||
|
|
size_t resid = len;
|
||
|
|
ssize_t bytes;
|
||
|
|
|
||
|
|
ASSERT(fd != -1);
|
||
|
|
|
||
|
|
while (resid != 0) {
|
||
|
|
bytes = read(fd, ptr, resid);
|
||
|
|
ASSERT3S(bytes, >=, 0);
|
||
|
|
ptr += bytes;
|
||
|
|
resid -= bytes;
|
||
|
|
}
|
||
|
|
|
||
|
|
return (0);
|
||
|
|
}
|
||
|
|
|
||
|
|
int
|
||
|
|
random_get_bytes(uint8_t *ptr, size_t len)
|
||
|
|
{
|
||
|
|
return (random_get_bytes_common(ptr, len, random_fd));
|
||
|
|
}
|
||
|
|
|
||
|
|
int
|
||
|
|
random_get_pseudo_bytes(uint8_t *ptr, size_t len)
|
||
|
|
{
|
||
|
|
return (random_get_bytes_common(ptr, len, urandom_fd));
|
||
|
|
}
|