Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does C# .NET AES GenerateKey (RandomNumberGenerator) truly use /dev/urandom on Linux

Tags:

c#

.net

linux

aes

Generating a key and I have tried to understand how GenerateKey gets random in Linux.

Example: using System; using System.Security.Cryptography;

namespace AesEncryption
{
    class Program
    {
        static void Main(string[] args)
        {
            using (Aes aesAlgorithm = Aes.Create());
            aesAlgorithm.KeySize = 256;
            aesAlgorithm.GenerateKey(); // Where does it get its entropy?
            string keyBase64 = Convert.ToBase64String(aesAlgorithm.Key);
        }
    }
}

The method description can be found here: https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.symmetricalgorithm.generatekey?view=net-8.0#system-security-cryptography-symmetricalgorithm-generatekey

The source code for Aes.cs can be found here: https://github.com/dotnet/runtime/blob/5535e31a712343a63f5d7d796cd874e563e5ac14/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/Aes.cs

Firstly, how does the code decide which AesImplementation to instantiate (in Linux) as there are a few (Apple,Windows,OpenSsl and some others)?

[UnsupportedOSPlatform("browser")]
public static new Aes Create()
{
    return new AesImplementation();
}

Assuming it is AesImplementation.cs : https://github.com/dotnet/runtime/blob/main/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/AesImplementation.cs#L9

public sealed override void GenerateKey()
{
    Span<byte> key = stackalloc byte[KeySize / BitsPerByte];
    RandomNumberGenerator.Fill(key);
    SetKeyCore(key);
}

Of which I think leads to: https://github.com/dotnet/runtime/blob/main/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/RandomNumberGenerator.cs#L98

public static void Fill(Span<byte> data)
{
    RandomNumberGeneratorImplementation.FillSpan(data);
}

and then I am bit confused having seen all this where it gets random bytes from. I can't quite piece together how it uses /dev/urandom in Linux as I have been told it uses it but I need evidence of this.

internal static unsafe void FillSpan(Span<byte> data)
{
    if (data.Length > 0)
    {
        fixed (byte* ptr = data) GetBytes(ptr, data.Length);
    }
}
like image 423
PKCS12 Avatar asked Jan 19 '26 03:01

PKCS12


1 Answers

On Linux, .Net uses the RAND_bytes function of OpenSSL to generate random bytes, which uses /dev/urandom to seed the random generator, but does not use it for generating random bytes. Check Why OpenSSL can't use /dev/random directly?

The implementation is decided by the project file of the System.Security.Cryptography library. $(UseOpenSsl) is true on unix systems, so all the *.OpenSsl.cs files will be compiled into the assembly. The related file to this question is RandomNumberGeneratorImplementation.OpenSsl.cs

like image 63
shingo Avatar answered Jan 20 '26 19:01

shingo