Removes libdrng (rdrand) (#139)

This commit is contained in:
Kamron Batman 2020-05-24 10:47:47 -07:00 • committed by GitHub
parent 739e77a973
commit 2770ab1d50
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
42 changed files with 64 additions and 4298 deletions

View file

@ -1,115 +0,0 @@
/*************************************************************************
* ModernUO *
* Copyright (C) 2019-2020 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: DRng64.cs - Created: 2019/12/30 - Updated: 2019/12/30 *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program 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 General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*************************************************************************/
using System;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using Server.Network;
namespace Server
{
public sealed class DRng64 : RandomNumberGenerator, IHardwareRNG
{
public enum RDRandError
{
Unknown = -4,
Unsupported = -3,
Supported = -2,
NotReady = -1,
Failure = 0,
Success = 1
}
public bool IsSupported()
{
ulong r = 0;
return SafeNativeMethods.rdrand_64(ref r, true) == RDRandError.Success;
}
private static unsafe void GetBytes(byte* pBuffer, int count)
{
SafeNativeMethods.rdrand_get_bytes(count, pBuffer);
}
public override void GetBytes(byte[] data)
{
if (data == null) throw new ArgumentNullException(nameof(data));
GetBytes(new Span<byte>(data));
}
public override void GetBytes(byte[] data, int offset, int count)
{
GetBytes(new Span<byte>(data, offset, count));
}
public override unsafe void GetBytes(Span<byte> data)
{
if (data.Length > 0)
fixed (byte* ptr = data)
{
GetBytes(ptr, data.Length);
}
}
public override void GetNonZeroBytes(byte[] data)
{
if (data == null) throw new ArgumentNullException(nameof(data));
GetNonZeroBytes(new Span<byte>(data));
}
public override void GetNonZeroBytes(Span<byte> data)
{
while (data.Length > 0)
{
// Fill the remaining portion of the span with random bytes.
GetBytes(data);
// Find the first zero in the remaining portion.
var indexOfFirst0Byte = data.Length;
for (var i = 0; i < data.Length; i++)
if (data[i] == 0)
{
indexOfFirst0Byte = i;
break;
}
// If there were any zeros, shift down all non-zeros.
for (var i = indexOfFirst0Byte + 1; i < data.Length; i++)
if (data[i] != 0)
data[indexOfFirst0Byte++] = data[i];
// Request new random bytes if necessary; dont re-use
// existing bytes since they were shifted down.
data = data.Slice(indexOfFirst0Byte);
}
}
internal class SafeNativeMethods
{
[DllImport("libdrng", CallingConvention = CallingConvention.Cdecl)]
internal static extern RDRandError rdrand_64(ref ulong rand, bool retry);
[DllImport("libdrng", CallingConvention = CallingConvention.Cdecl)]
internal static extern unsafe RDRandError rdrand_get_bytes(int n, byte* buffer);
}
}
}

View file

@ -0,0 +1,42 @@
/*************************************************************************
* ModernUO *
* Copyright (C) 2019-2020 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: RandomProviders.cs - Created: 2020/05/24 - Updated: 2020/05/24 *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program 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 General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*************************************************************************/
using System;
using System.Security.Cryptography;
namespace Server
{
public interface IRandomProvider
{
public uint Next(uint c);
public bool NextBool();
public void GetBytes(Span<byte> b);
public double NextDouble();
}
public static class RandomProviders
{
private static IRandomProvider m_Provider;
private static IRandomProvider m_SecureProvider;
public static IRandomProvider Provider => m_Provider ??= new Xoshiro256PlusPlus();
public static IRandomProvider SecureProvider => m_SecureProvider ??= new SecureRandom();
}
}

View file

@ -2,7 +2,7 @@
* ModernUO *
* Copyright (C) 2019-2020 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: Random.cs - Created: 2019/12/30 - Updated: 2019/01/05 *
* File: Random.cs - Created: 2019/12/30 - Updated: 2020/05/23 *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
@ -23,49 +23,18 @@ using System.Security.Cryptography;
namespace Server
{
/// <summary>
/// Handles random number generation.
/// </summary>
public static class RandomImpl
public class SecureRandom : RandomNumberGenerator, IRandomProvider
{
private static readonly Xoshiro256PlusPlus _Random = new Xoshiro256PlusPlus();
private readonly RandomNumberGenerator m_Random = new RNGCryptoServiceProvider();
public static uint Next(uint c) => _Random.Next(c);
public override void GetBytes(byte[] data) => m_Random.GetBytes(data);
public static bool NextBool() => _Random.NextBool();
public override void GetBytes(Span<byte> data) => m_Random.GetBytes(data);
public static void GetBytes(Span<byte> b) => _Random.GetBytes(b);
public uint Next(uint c) => throw new NotImplementedException();
public static double NextDouble() => _Random.NextDouble();
}
public bool NextBool() => throw new NotImplementedException();
public static class SecureRandomImpl
{
public static readonly RandomNumberGenerator _Random;
static SecureRandomImpl()
{
try
{
_Random = new DRng64();
if (_Random is IHardwareRNG rng && rng?.IsSupported() != true)
_Random = new RNGCryptoServiceProvider();
}
catch (Exception)
{
_Random = new RNGCryptoServiceProvider();
}
}
public static bool IsHardwareRNG => _Random is IHardwareRNG;
public static string Name => _Random.GetType().Name;
public static void GetBytes(Span<byte> buffer) => _Random.GetBytes(buffer);
}
public interface IHardwareRNG
{
bool IsSupported();
public double NextDouble() => throw new NotImplementedException();
}
}

View file

@ -875,7 +875,7 @@ namespace Server
var sides = numSides;
for (var i = 0; i < numDice; ++i)
total += (int)RandomImpl.Next(sides) + 1;
total += (int)RandomProviders.Provider.Next(sides) + 1;
return total + bonus;
}
@ -885,7 +885,7 @@ namespace Server
var count = list.Count;
for (var i = count - 1; i > 0; i--)
{
var r = (int)RandomImpl.Next((uint)count);
var r = (int)RandomProviders.Provider.Next((uint)count);
var swap = list[r];
list[r] = list[i];
list[i] = swap;
@ -896,7 +896,7 @@ namespace Server
public static T RandomList<T>(IList<T> list) => list[Random(list.Count)];
public static bool RandomBool() => RandomImpl.NextBool();
public static bool RandomBool() => RandomProviders.Provider.NextBool();
public static int RandomMinMax(int min, int max)
{
@ -911,27 +911,27 @@ namespace Server
return min;
}
return min + (int)RandomImpl.Next((uint)(max - min + 1));
return min + (int)RandomProviders.Provider.Next((uint)(max - min + 1));
}
public static int Random(int from, int count)
{
if (count == 0) return from;
if (count > 0) return (int)(from + RandomImpl.Next((uint)count));
if (count > 0) return (int)(from + RandomProviders.Provider.Next((uint)count));
return (int)(from - RandomImpl.Next((uint)-count));
return (int)(from - RandomProviders.Provider.Next((uint)-count));
}
public static int Random(int count) => (int)RandomImpl.Next((uint)count);
public static int Random(int count) => (int)RandomProviders.Provider.Next((uint)count);
public static uint Random(uint count) => RandomImpl.Next(count);
public static uint Random(uint count) => RandomProviders.Provider.Next(count);
public static void RandomBytes(byte[] buffer) => RandomImpl.GetBytes(buffer);
public static void RandomBytes(byte[] buffer) => RandomProviders.Provider.GetBytes(buffer);
public static void RandomBytes(Span<byte> buffer) => RandomImpl.GetBytes(buffer);
public static void RandomBytes(Span<byte> buffer) => RandomProviders.Provider.GetBytes(buffer);
public static double RandomDouble() => RandomImpl.NextDouble();
public static double RandomDouble() => RandomProviders.Provider.NextDouble();
/// <summary>
/// Random pink, blue, green, orange, red or yellow hue

View file

@ -3,7 +3,7 @@
* Copyright (C) 2019-2020 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: Xoshiro256PlusPlus.cs *
* Created: 2019/12/29 - Updated: 2019/12/30 *
* Created: 2019/12/29 - Updated: 2020/05/24 *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
@ -22,10 +22,11 @@
using System;
using System.Runtime.CompilerServices;
using System.Security.Cryptography;
using Server.Utilities;
namespace Server
{
public class Xoshiro256PlusPlus : RandomNumberGenerator
public class Xoshiro256PlusPlus : RandomNumberGenerator, IRandomProvider
{
private ulong _s0, _s1, _s2, _s3;