Skip to content

Utils::Crypto::GetRandomUInt() is not random #1693

@ibc

Description

@ibc

Bug Report

This simple code:

for (int i = 0; i < 20; ++i)
{
  MS_DUMP("Utils::Crypto::GetRandomUInt(0, 65535): %u", Utils::Crypto::GetRandomUInt(0, 65535));
}

Always produces these results:

Utils::Crypto::GetRandomUInt(0, 65535): 990
Utils::Crypto::GetRandomUInt(0, 65535): 5392
Utils::Crypto::GetRandomUInt(0, 65535): 21307
Utils::Crypto::GetRandomUInt(0, 65535): 17272
Utils::Crypto::GetRandomUInt(0, 65535): 29313
Utils::Crypto::GetRandomUInt(0, 65535): 26150
Utils::Crypto::GetRandomUInt(0, 65535): 2297
Utils::Crypto::GetRandomUInt(0, 65535): 10049
Utils::Crypto::GetRandomUInt(0, 65535): 14377
Utils::Crypto::GetRandomUInt(0, 65535): 16944
Utils::Crypto::GetRandomUInt(0, 65535): 28122
Utils::Crypto::GetRandomUInt(0, 65535): 3485
Utils::Crypto::GetRandomUInt(0, 65535): 17940
Utils::Crypto::GetRandomUInt(0, 65535): 21870
Utils::Crypto::GetRandomUInt(0, 65535): 32031
Utils::Crypto::GetRandomUInt(0, 65535): 1229
Utils::Crypto::GetRandomUInt(0, 65535): 29059
Utils::Crypto::GetRandomUInt(0, 65535): 8225
Utils::Crypto::GetRandomUInt(0, 65535): 13832
Utils::Crypto::GetRandomUInt(0, 65535): 3857

NOTE: This is not entirely true. Results are the same if the test binary is not re-compiled and probably when it's executed by the same CPU core because Crypto::seed is a thread_local variable that the program may store in fixed memory locations. In that case, Crypto::seed is the same and hence it produces the very same results.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions