Blame lib/nettle/sysrng-bcrypt.c

Packit Service 991b93
/*
Packit Service 991b93
 * Copyright (C) 2010-2016 Free Software Foundation, Inc.
Packit Service 991b93
 * Copyright (C) 2015-2016 Red Hat, Inc.
Packit Service 991b93
 * Copyright (C) 2000, 2001, 2008 Niels Möller
Packit Service 991b93
 *
Packit Service 991b93
 * Author: Nikos Mavrogiannopoulos
Packit Service 991b93
 *
Packit Service 991b93
 * This file is part of GNUTLS.
Packit Service 991b93
 *
Packit Service 991b93
 * The GNUTLS library is free software; you can redistribute it and/or
Packit Service 991b93
 * modify it under the terms of the GNU Lesser General Public License
Packit Service 991b93
 * as published by the Free Software Foundation; either version 2.1 of
Packit Service 991b93
 * the License, or (at your option) any later version.
Packit Service 991b93
 *
Packit Service 991b93
 * This library is distributed in the hope that it will be useful, but
Packit Service 991b93
 * WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 991b93
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 991b93
 * Lesser General Public License for more details.
Packit Service 991b93
 *
Packit Service 991b93
 * You should have received a copy of the GNU Lesser General Public License
Packit Service 991b93
 * along with this program.  If not, see <https://www.gnu.org/licenses/>
Packit Service 991b93
 *
Packit Service 991b93
 */
Packit Service 991b93
Packit Service 991b93
/* Here are the common parts of the random generator layer. 
Packit Service 991b93
 * Some of this code was based on the LSH 
Packit Service 991b93
 * random generator (the trivia and device source functions for POSIX)
Packit Service 991b93
 * and modified to fit gnutls' needs. Relicenced with permission. 
Packit Service 991b93
 * Original author Niels Möller.
Packit Service 991b93
 */
Packit Service 991b93
Packit Service 991b93
#include "gnutls_int.h"
Packit Service 991b93
#include "errors.h"
Packit Service 991b93
#include <locks.h>
Packit Service 991b93
#include <num.h>
Packit Service 991b93
#include <nettle/yarrow.h>
Packit Service 991b93
#include <errno.h>
Packit Service 991b93
#include <rnd-common.h>
Packit Service 991b93
#include <hash-pjw-bare.h>
Packit Service 991b93
Packit Service 991b93
#include <sys/types.h>
Packit Service 991b93
#include <sys/stat.h>
Packit Service 991b93
#include <unistd.h>
Packit Service 991b93
Packit Service 991b93
/* The windows randomness gatherer.
Packit Service 991b93
 */
Packit Service 991b93
Packit Service 991b93
#include <windows.h>
Packit Service 991b93
#include <bcrypt.h>
Packit Service 991b93
Packit Service 991b93
get_entropy_func _rnd_get_system_entropy = NULL;
Packit Service 991b93
Packit Service 991b93
static BCRYPT_ALG_HANDLE device_fd = 0;
Packit Service 991b93
Packit Service 991b93
static
Packit Service 991b93
int _rnd_get_system_entropy_win32(void* rnd, size_t size)
Packit Service 991b93
{
Packit Service 991b93
	NTSTATUS err = BCryptGenRandom(device_fd, rnd, size, 0);
Packit Service 991b93
	if (!BCRYPT_SUCCESS(err)) {
Packit Service 991b93
		_gnutls_debug_log("Error in BCryptGenRandom: %ld\n", err);
Packit Service 991b93
		return GNUTLS_E_RANDOM_DEVICE_ERROR;
Packit Service 991b93
	}
Packit Service 991b93
Packit Service 991b93
	return 0;
Packit Service 991b93
}
Packit Service 991b93
Packit Service 991b93
int _rnd_system_entropy_check(void)
Packit Service 991b93
{
Packit Service 991b93
	return 0;
Packit Service 991b93
}
Packit Service 991b93
Packit Service 991b93
int _rnd_system_entropy_init(void)
Packit Service 991b93
{
Packit Service 991b93
	NTSTATUS err = BCryptOpenAlgorithmProvider
Packit Service 991b93
	    (&device_fd, BCRYPT_RNG_ALGORITHM, NULL, 0);
Packit Service 991b93
	if (!BCRYPT_SUCCESS(err)) {
Packit Service 991b93
		_gnutls_debug_log("error in BCryptOpenAlgorithmProvider!\n");
Packit Service 991b93
		return GNUTLS_E_RANDOM_DEVICE_ERROR;
Packit Service 991b93
	}
Packit Service 991b93
Packit Service 991b93
	_rnd_get_system_entropy = _rnd_get_system_entropy_win32;
Packit Service 991b93
	return 0;
Packit Service 991b93
}
Packit Service 991b93
Packit Service 991b93
void _rnd_system_entropy_deinit(void)
Packit Service 991b93
{
Packit Service 991b93
	BCryptCloseAlgorithmProvider(device_fd, 0);
Packit Service 991b93
}