File tree Expand file tree Collapse file tree
CryptoLib.Tests/src/Security Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -217,10 +217,16 @@ procedure TTestSecureRandom.TestAESPRNG;
217217end ;
218218
219219procedure TTestSecureRandom.TestAESPRNGRandom ;
220+ const
221+ // For very small buffers two independent random draws can legitimately be
222+ // equal (e.g. a single byte collides with probability 1/256). Retrying a
223+ // few times distinguishes ordinary randomness (which diverges on retry)
224+ // from a genuinely stuck RNG (which keeps returning identical output).
225+ MaxCollisionRetries = Int32(8 );
220226var
221227 b1, b2: TBytes;
222228 a1, a2: IRandomNumberGenerator;
223- Idx: Int32;
229+ Idx, Retry : Int32;
224230begin
225231 // it is hard to validate randomness - we just test the feature set
226232 b1 := nil ;
@@ -248,6 +254,14 @@ procedure TTestSecureRandom.TestAESPRNGRandom;
248254 a1.GetBytes(b1);
249255 a2.GetBytes(b2);
250256
257+ Retry := 0 ;
258+ while AreEqual(b1, b2) and (Retry < MaxCollisionRetries) do
259+ begin
260+ a1.GetBytes(b1);
261+ a2.GetBytes(b2);
262+ System.Inc(Retry);
263+ end ;
264+
251265 CheckTrue(not AreEqual(b1, b2));
252266 end ;
253267
You can’t perform that action at this time.
0 commit comments