You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/// <remarks>Calls <see cref="NextSpanBytes(Span{Byte}, Int32, Int32)"/><br/><br/>Uses random bits from the pool, which is seeded by the conditioner. An upper bound of 511 128-bit samples will be generated per seed. That is, no more than 511*2=1022 sequential DRNG random numbers will be generated from the same seed value. - <see href="https://www.intel.com/content/www/us/en/developer/articles/guide/intel-digital-random-number-generator-drng-software-implementation-guide.html#inpage-nav-3-3">intel.com</see></remarks>
121
+
/// <remarks>Calls <see cref="NextBytes(Span{Byte}, Int32, Int32)"/><br/><br/>Uses random bits from the pool, which is seeded by the conditioner. An upper bound of 511 128-bit samples will be generated per seed. That is, no more than 511*2=1022 sequential DRNG random numbers will be generated from the same seed value. - <see href="https://www.intel.com/content/www/us/en/developer/articles/guide/intel-digital-random-number-generator-drng-software-implementation-guide.html#inpage-nav-3-3">intel.com</see></remarks>
122
122
/// <param name="buffer">array to operate on</param>
123
123
/// <returns>Indicated whether the operation succeeded or not (<see langword="bool"></see> success = <see langword="true"></see>) | will only return false if instruction fails 128 times in a row</returns>
/// <returns>Indicated whether the operation succeeded or not (<see langword="bool"></see> success = <see langword="true"></see>) | will only return false if instruction fails 128 times in a row</returns>
/// <remarks>Calls <see cref="SeedNextSpanBytes(Span{Byte}, Int32, Int32)"/><br/><br/>The seed values come directly from the entropy conditioner - <see href="https://www.intel.com/content/www/us/en/developer/articles/guide/intel-digital-random-number-generator-drng-software-implementation-guide.html#inpage-nav-5-8">intel.com</see></remarks>
265
+
/// <remarks>Calls <see cref="SeedNextBytes(Span{Byte}, Int32, Int32)"/><br/><br/>The seed values come directly from the entropy conditioner - <see href="https://www.intel.com/content/www/us/en/developer/articles/guide/intel-digital-random-number-generator-drng-software-implementation-guide.html#inpage-nav-5-8">intel.com</see></remarks>
266
266
/// <param name="buffer">array to operate on</param>
267
267
/// <returns>Indicated whether the operation succeeded or not (<see langword="bool"></see> success = <see langword="true"></see>) | will only return false if instruction fails 128 times in a row</returns>
/// <returns>Indicated whether the operation succeeded or not (<see langword="bool"></see> success = <see langword="true"></see>) | will only return false if instruction fails 128 times in a row</returns>
A small .Net Framework 4.8 library that allows you to *directly* call your CPUs RDRAND and RDSEED instructions, this is done by calling a C wrapper for x86-64 assembly with P/Invoke.
4
+
5
+
## Features
6
+
7
+
- No direct heap allocations (only static functions)
8
+
- easy to use
9
+
- Span support via `System.Memory`
10
+
- easy compatibility check via build-in function that uses `CPUID`
11
+
12
+
## How to Use
13
+
14
+
#### To check compatibility
15
+
```
16
+
if (HWRandom.HardwareRandomIsPresent())
17
+
{
18
+
Console.WriteLine("CPU compatible!");
19
+
Worker.Start();
20
+
21
+
Environment.Exit(0);
22
+
}
23
+
else
24
+
{
25
+
Console.WriteLine("RDRAND/RDSEED instruction NOT available on CPU!");
26
+
Environment.Exit(-1);
27
+
}
28
+
```
29
+
30
+
#### To get random numbers form the DRNG pool (RDRAND)
31
+
```
32
+
// define values
33
+
UInt64 _64RandomBits = 0;
34
+
UInt32 _32RandomBits = 0;
35
+
UInt16 _16RandomBits = 0;
36
+
Byte _8RandomBits = 0;
37
+
38
+
// fill values
39
+
HWRandom.ReadRandom64(_64RandomBits);
40
+
HWRandom.ReadRandom32(_32RandomBits);
41
+
HWRandom.ReadRandom16(_16RandomBits);
42
+
HWRandom.ReadRandom8(_8RandomBits);
43
+
44
+
Console.WriteLine("rand on 64 bit register: " + _64RandomBits);
45
+
Console.WriteLine("rand on 32 bit register: " + _32RandomBits);
46
+
Console.WriteLine("rand on 16 bit register: " + _16RandomBits);
47
+
Console.WriteLine("rand on 16 bit register (only 8 are returned by native function): " + _8RandomBits);
48
+
```
49
+
50
+
#### To get random numbers form the ENRNG (RDSEED)
51
+
```
52
+
// define values
53
+
UInt64 _64RandomBits = 0;
54
+
UInt32 _32RandomBits = 0;
55
+
UInt16 _16RandomBits = 0;
56
+
Byte _8RandomBits = 0;
57
+
58
+
// fill values
59
+
HWRandom.ReadSeed64(_64RandomBits);
60
+
HWRandom.ReadSeed32(_32RandomBits);
61
+
HWRandom.ReadSeed16(_16RandomBits);
62
+
HWRandom.ReadSeed8(_8RandomBits);
63
+
64
+
Console.WriteLine("seed on 64 bit register: " + _64RandomBits);
65
+
Console.WriteLine("seed on 32 bit register: " + _32RandomBits);
66
+
Console.WriteLine("seed on 16 bit register: " + _16RandomBits);
67
+
Console.WriteLine("seed on 16 bit register (only 8 are returned by native function): " + _8RandomBits);
[Intel® Digital Random Number Generator (DRNG) Software Implementation Guide](https://www.intel.com/content/www/us/en/developer/articles/guide/intel-digital-random-number-generator-drng-software-implementation-guide.html)
0 commit comments