From 2bd25290f3841c135342737c01a7e4336b72b271 Mon Sep 17 00:00:00 2001 From: Fu Siyuan Date: Fri, 7 Aug 2015 03:22:10 +0000 Subject: [PATCH] MdeModulePkg: Use monotonic count to initialize the NetLib random seed. NetRandomInitSeed() function use current time to initialize the random seed, while in some platform the time service is not accuracy that make the random seed collision. This patch add the monotonic count to the seed to avoid this. Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Fu Siyuan Reviewed-by: Jiaxin Wu Reviewed-by: Ye Ting git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@18185 6f19259b-4bc3-4df7-8a09-765794883524 --- MdeModulePkg/Include/Library/NetLib.h | 10 +++++----- MdeModulePkg/Library/DxeNetLib/DxeNetLib.c | 12 ++++++++---- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/MdeModulePkg/Include/Library/NetLib.h b/MdeModulePkg/Include/Library/NetLib.h index 7ad8dac446..280c51a01d 100644 --- a/MdeModulePkg/Include/Library/NetLib.h +++ b/MdeModulePkg/Include/Library/NetLib.h @@ -530,13 +530,13 @@ NetPutUint32 ( ); /** - Initialize a random seed using current time. + Initialize a random seed using current time and monotonic count. - Get current time first. Then initialize a random seed based on some basic - mathematical operations on the hour, day, minute, second, nanosecond and year - of the current time. + Get current time and monotonic count first. Then initialize a random seed + based on some basic mathematics operation on the hour, day, minute, second, + nanosecond and year of the current time and the monotonic count value. - @return The random seed, initialized with current time. + @return The random seed initialized with current time. **/ UINT32 diff --git a/MdeModulePkg/Library/DxeNetLib/DxeNetLib.c b/MdeModulePkg/Library/DxeNetLib/DxeNetLib.c index ce26b322bc..57e8f9f27b 100644 --- a/MdeModulePkg/Library/DxeNetLib/DxeNetLib.c +++ b/MdeModulePkg/Library/DxeNetLib/DxeNetLib.c @@ -853,11 +853,11 @@ Ip6Swap128 ( } /** - Initialize a random seed using current time. + Initialize a random seed using current time and monotonic count. - Get current time first. Then initialize a random seed based on some basic - mathematics operation on the hour, day, minute, second, nanosecond and year - of the current time. + Get current time and monotonic count first. Then initialize a random seed + based on some basic mathematics operation on the hour, day, minute, second, + nanosecond and year of the current time and the monotonic count value. @return The random seed initialized with current time. @@ -870,12 +870,16 @@ NetRandomInitSeed ( { EFI_TIME Time; UINT32 Seed; + UINT64 MonotonicCount; gRT->GetTime (&Time, NULL); Seed = (~Time.Hour << 24 | Time.Day << 16 | Time.Minute << 8 | Time.Second); Seed ^= Time.Nanosecond; Seed ^= Time.Year << 7; + gBS->GetNextMonotonicCount (&MonotonicCount); + Seed += (UINT32) MonotonicCount; + return Seed; } -- 2.39.2