]> git.proxmox.com Git - mirror_edk2.git/commitdiff
MdeModulePkg: Use monotonic count to initialize the NetLib random seed.
authorFu Siyuan <siyuan.fu@intel.com>
Fri, 7 Aug 2015 03:22:10 +0000 (03:22 +0000)
committersfu5 <sfu5@Edk2>
Fri, 7 Aug 2015 03:22:10 +0000 (03:22 +0000)
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 <siyuan.fu@intel.com>
Reviewed-by: Jiaxin Wu <jiaxin.wu@intel.com>
Reviewed-by: Ye Ting <ting.ye@intel.com>
git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@18185 6f19259b-4bc3-4df7-8a09-765794883524

MdeModulePkg/Include/Library/NetLib.h
MdeModulePkg/Library/DxeNetLib/DxeNetLib.c

index 7ad8dac446524a81bdd95a1830237a41779ba4db..280c51a01df2e2c4cfff67451058bd6f2623f052 100644 (file)
@@ -530,13 +530,13 @@ NetPutUint32 (
   );\r
 \r
 /**\r
   );\r
 \r
 /**\r
-  Initialize a random seed using current time.\r
+  Initialize a random seed using current time and monotonic count.\r
 \r
 \r
-  Get current time first. Then initialize a random seed based on some basic\r
-  mathematical operations on the hour, day, minute, second, nanosecond and year\r
-  of the current time.\r
+  Get current time and monotonic count first. Then initialize a random seed \r
+  based on some basic mathematics operation on the hour, day, minute, second,\r
+  nanosecond and year of the current time and the monotonic count value.\r
 \r
 \r
-  @return The random seed, initialized with current time.\r
+  @return The random seed initialized with current time.\r
 \r
 **/\r
 UINT32\r
 \r
 **/\r
 UINT32\r
index ce26b322bcb2cf619e705ae1e66af847fa019930..57e8f9f27b0dff0cf68a8dfac738800242180a1d 100644 (file)
@@ -853,11 +853,11 @@ Ip6Swap128 (
 }\r
 \r
 /**\r
 }\r
 \r
 /**\r
-  Initialize a random seed using current time.\r
+  Initialize a random seed using current time and monotonic count.\r
 \r
 \r
-  Get current time first. Then initialize a random seed based on some basic\r
-  mathematics operation on the hour, day, minute, second, nanosecond and year\r
-  of the current time.\r
+  Get current time and monotonic count first. Then initialize a random seed \r
+  based on some basic mathematics operation on the hour, day, minute, second,\r
+  nanosecond and year of the current time and the monotonic count value.\r
 \r
   @return The random seed initialized with current time.\r
 \r
 \r
   @return The random seed initialized with current time.\r
 \r
@@ -870,12 +870,16 @@ NetRandomInitSeed (
 {\r
   EFI_TIME                  Time;\r
   UINT32                    Seed;\r
 {\r
   EFI_TIME                  Time;\r
   UINT32                    Seed;\r
+  UINT64                    MonotonicCount;\r
 \r
   gRT->GetTime (&Time, NULL);\r
   Seed = (~Time.Hour << 24 | Time.Day << 16 | Time.Minute << 8 | Time.Second);\r
   Seed ^= Time.Nanosecond;\r
   Seed ^= Time.Year << 7;\r
 \r
 \r
   gRT->GetTime (&Time, NULL);\r
   Seed = (~Time.Hour << 24 | Time.Day << 16 | Time.Minute << 8 | Time.Second);\r
   Seed ^= Time.Nanosecond;\r
   Seed ^= Time.Year << 7;\r
 \r
+  gBS->GetNextMonotonicCount (&MonotonicCount);\r
+  Seed += (UINT32) MonotonicCount;\r
+\r
   return Seed;\r
 }\r
 \r
   return Seed;\r
 }\r
 \r