Skip to content

Commit 44d24be

Browse files
added readme
1 parent 347d9b3 commit 44d24be

File tree

5 files changed

+123
-33
lines changed

5 files changed

+123
-33
lines changed

HWRand.Net.sln

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test-CLI-App", "Test-CLI-Ap
1616
{9D45458B-701C-484E-85F7-9C7D7C0EF344} = {9D45458B-701C-484E-85F7-9C7D7C0EF344}
1717
EndProjectSection
1818
EndProject
19+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Projektmappenelemente", "Projektmappenelemente", "{C7F042B0-B6A6-4944-A92C-602582EA1415}"
20+
ProjectSection(SolutionItems) = preProject
21+
README.md = README.md
22+
EndProjectSection
23+
EndProject
1924
Global
2025
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2126
Debug|Any CPU = Debug|Any CPU

HWRand.Net/NextBytes.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,10 @@ public static Boolean NextBytes(Byte[] buffer, UInt64 offset, UInt64 count)
118118
/// <summary>
119119
/// Fills the in-buffer with RDRAND
120120
/// </summary>
121-
/// <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>
122122
/// <param name="buffer">array to operate on</param>
123123
/// <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>
124-
public static Boolean NextSpanBytes(Span<Byte> buffer) => NextSpanBytes(buffer, 0, buffer.Length);
124+
public static Boolean NextBytes(Span<Byte> buffer) => NextBytes(buffer, 0, buffer.Length);
125125
/// <summary>
126126
/// Fills the in-buffer with RDRAND
127127
/// </summary>
@@ -130,7 +130,7 @@ public static Boolean NextBytes(Byte[] buffer, UInt64 offset, UInt64 count)
130130
/// <param name="offset">offset</param>
131131
/// <param name="count">count</param>
132132
/// <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>
133-
public static Boolean NextSpanBytes(Span<Byte> buffer, Int32 offset, Int32 count)
133+
public static Boolean NextBytes(Span<Byte> buffer, Int32 offset, Int32 count)
134134
{
135135
if (offset + count > buffer.Length) return false;
136136

@@ -262,10 +262,10 @@ public static Boolean SeedNextBytes(Byte[] buffer, UInt64 offset, UInt64 count)
262262
/// <summary>
263263
/// Fills the in-buffer with RDSEED
264264
/// </summary>
265-
/// <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>
266266
/// <param name="buffer">array to operate on</param>
267267
/// <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>
268-
public static Boolean SeedNextSpanBytes(Span<Byte> buffer) => SeedNextSpanBytes(buffer, 0, buffer.Length);
268+
public static Boolean SeedNextBytes(Span<Byte> buffer) => SeedNextBytes(buffer, 0, buffer.Length);
269269
/// <summary>
270270
/// Fills the in-buffer with RDSEED
271271
/// </summary>
@@ -274,7 +274,7 @@ public static Boolean SeedNextBytes(Byte[] buffer, UInt64 offset, UInt64 count)
274274
/// <param name="offset">offset</param>
275275
/// <param name="count">count</param>
276276
/// <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>
277-
public static Boolean SeedNextSpanBytes(Span<Byte> buffer, Int32 offset, Int32 count)
277+
public static Boolean SeedNextBytes(Span<Byte> buffer, Int32 offset, Int32 count)
278278
{
279279
if (offset + count > buffer.Length) return false;
280280

README.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# HWRand.Net
2+
3+
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);
68+
```
69+
70+
#### To fill a buffer
71+
```
72+
Byte[] bytes = new Byte[254];
73+
74+
// use RDRAND to fill buffer
75+
HWRandom.NextBytes(bytes, 0, (UInt64)bytes.LongLength);
76+
77+
// use RDSEED to fill buffer
78+
HWRandom.SeedNextBytes(bytes.AsSpan());
79+
```
80+
---
81+
82+
[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)

Test-CLI-App/Program.cs

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,46 +5,49 @@ namespace Test_CLI_App
55
{
66
internal static class Program
77
{
8-
9-
10-
11-
12-
private unsafe static void Main()
8+
private static void Main()
139
{
1410
if (!HWRandom.HardwareRandomIsPresent())
1511
{
1612
Console.WriteLine("RDRAND/RDSEED instruction NOT available on CPU!");
1713
Environment.Exit(-1);
1814
}
1915

20-
UInt64 val64 = 0;
21-
UInt32 value32 = 0;
22-
UInt16 value16 = 0;
23-
Byte value8 = 0;
16+
// define values
17+
UInt64 _64RandomBits = 0;
18+
UInt32 _32RandomBits = 0;
19+
UInt16 _16RandomBits = 0;
20+
Byte _8RandomBits = 0;
21+
22+
// fill values
23+
HWRandom.ReadRandom64(_64RandomBits);
24+
HWRandom.ReadRandom32(_32RandomBits);
25+
HWRandom.ReadRandom16(_16RandomBits);
26+
HWRandom.ReadRandom8(_8RandomBits);
27+
28+
Console.WriteLine("rand on 64 bit register: " + _64RandomBits);
29+
Console.WriteLine("rand on 32 bit register: " + _32RandomBits);
30+
Console.WriteLine("rand on 16 bit register: " + _16RandomBits);
31+
Console.WriteLine("rand on 16 bit register (only 8 are returned by native function): " + _8RandomBits);
2432

25-
HWRandom.ReadRandom64(val64);
26-
HWRandom.ReadRandom32(value32);
27-
HWRandom.ReadRandom16(value16);
28-
HWRandom.ReadRandom8(value8);
33+
Console.WriteLine("\n\n");
2934

30-
Console.WriteLine("rand 64: " + val64);
31-
Console.WriteLine("rand 32: " + value32);
32-
Console.WriteLine("rand 16: " + value16);
33-
Console.WriteLine("rand 8: " + value8);
35+
HWRandom.ReadSeed64(_64RandomBits);
36+
HWRandom.ReadSeed32(_32RandomBits);
37+
HWRandom.ReadSeed16(_16RandomBits);
38+
HWRandom.ReadSeed8(_8RandomBits);
3439

35-
//
40+
Console.WriteLine("seed on 64 bit register: " + _64RandomBits);
41+
Console.WriteLine("seed on 32 bit register: " + _32RandomBits);
42+
Console.WriteLine("seed on 16 bit register: " + _16RandomBits);
43+
Console.WriteLine("seed on 16 bit register (only 8 are returned by native function): " + _8RandomBits);
3644

37-
HWRandom.ReadSeed64(val64);
38-
HWRandom.ReadSeed32(value32);
39-
HWRandom.ReadSeed16(value16);
40-
HWRandom.ReadSeed8(value8);
4145

42-
Console.WriteLine("seed 64: " + val64);
43-
Console.WriteLine("seed 32: " + value32);
44-
Console.WriteLine("seed 16: " + value16);
45-
Console.WriteLine("seed 8: " + value8);
46+
Byte[] bytes = new Byte[254];
4647

48+
HWRandom.NextBytes(bytes, 0, (UInt64)bytes.LongLength);
4749

50+
HWRandom.SeedNextBytes(bytes.AsSpan());
4851
}
4952
}
5053
}

native/native.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ __declspec(dllexport) bool HardwareRandomIsPresent()
4343

4444
__cpuid(&cpuID, 7);
4545

46-
if (!(cpuID.EBX & 0x40000) == 0x40000)
46+
if (!((cpuID.EBX & 0x40000) == 0x40000))
4747
{
4848
return 0;
4949
}

0 commit comments

Comments
 (0)