From 36082a2fe36ecd800d784ae44c14f1f18c66a7e9 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 28 Apr 2024 09:33:12 +0200 Subject: Adding upstream version 3.7.9. Signed-off-by: Daniel Baumann --- lib/nettle/sysrng-bcrypt.c | 83 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 lib/nettle/sysrng-bcrypt.c (limited to 'lib/nettle/sysrng-bcrypt.c') diff --git a/lib/nettle/sysrng-bcrypt.c b/lib/nettle/sysrng-bcrypt.c new file mode 100644 index 0000000..a08e9de --- /dev/null +++ b/lib/nettle/sysrng-bcrypt.c @@ -0,0 +1,83 @@ +/* + * Copyright (C) 2010-2016 Free Software Foundation, Inc. + * Copyright (C) 2015-2016 Red Hat, Inc. + * Copyright (C) 2000, 2001, 2008 Niels Möller + * + * Author: Nikos Mavrogiannopoulos + * + * This file is part of GNUTLS. + * + * The GNUTLS library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see + * + */ + +/* Here are the common parts of the random generator layer. + * Some of this code was based on the LSH + * random generator (the trivia and device source functions for POSIX) + * and modified to fit gnutls' needs. Relicenced with permission. + * Original author Niels Möller. + */ + +#include "gnutls_int.h" +#include "errors.h" +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +/* The windows randomness gatherer. + */ + +#include +#include + +get_entropy_func _rnd_get_system_entropy = NULL; + +static BCRYPT_ALG_HANDLE device_fd = 0; + +static +int _rnd_get_system_entropy_win32(void* rnd, size_t size) +{ + NTSTATUS err = BCryptGenRandom(device_fd, rnd, size, 0); + if (!BCRYPT_SUCCESS(err)) { + _gnutls_debug_log("Error in BCryptGenRandom: %ld\n", err); + return GNUTLS_E_RANDOM_DEVICE_ERROR; + } + + return 0; +} + +int _rnd_system_entropy_init(void) +{ + NTSTATUS err = BCryptOpenAlgorithmProvider + (&device_fd, BCRYPT_RNG_ALGORITHM, NULL, 0); + if (!BCRYPT_SUCCESS(err)) { + _gnutls_debug_log("error in BCryptOpenAlgorithmProvider!\n"); + return GNUTLS_E_RANDOM_DEVICE_ERROR; + } + + _rnd_get_system_entropy = _rnd_get_system_entropy_win32; + return 0; +} + +void _rnd_system_entropy_deinit(void) +{ + BCryptCloseAlgorithmProvider(device_fd, 0); +} -- cgit v1.2.3