uuid: Use current time to compute sha.
authorGurucharan Shetty <gshetty@nicira.com>
Mon, 10 Mar 2014 16:18:50 +0000 (09:18 -0700)
committerGurucharan Shetty <gshetty@nicira.com>
Wed, 12 Mar 2014 16:15:23 +0000 (09:15 -0700)
Windows does not have the getppid(), getuid(), getgid() functions.
We do get a random seed from CryptGenRandom(). That seed along with
process id and current time hopefully is good enough.

Signed-off-by: Gurucharan Shetty <gshetty@nicira.com>
Acked-by: Ben Pfaff <blp@nicira.com>
lib/uuid.c

index 315c851..df1206e 100644 (file)
@@ -205,6 +205,12 @@ error:
     return false;
 }
 \f
+static void
+sha1_update_int(struct sha1_ctx *sha1_ctx, uintmax_t x)
+{
+   sha1_update(sha1_ctx, &x, sizeof x);
+}
+
 static void
 do_init(void)
 {
@@ -212,25 +218,21 @@ do_init(void)
     struct sha1_ctx sha1_ctx;
     uint8_t random_seed[16];
     struct timeval now;
-    pid_t pid, ppid;
-    uid_t uid;
-    gid_t gid;
 
     /* Get seed data. */
     get_entropy_or_die(random_seed, sizeof random_seed);
     xgettimeofday(&now);
-    pid = getpid();
-    ppid = getppid();
-    uid = getuid();
-    gid = getgid();
 
     /* Convert seed into key. */
     sha1_init(&sha1_ctx);
     sha1_update(&sha1_ctx, random_seed, sizeof random_seed);
-    sha1_update(&sha1_ctx, &pid, sizeof pid);
-    sha1_update(&sha1_ctx, &ppid, sizeof ppid);
-    sha1_update(&sha1_ctx, &uid, sizeof uid);
-    sha1_update(&sha1_ctx, &gid, sizeof gid);
+    sha1_update(&sha1_ctx, &now, sizeof now);
+    sha1_update_int(&sha1_ctx, getpid());
+#ifndef _WIN32
+    sha1_update_int(&sha1_ctx, getppid());
+    sha1_update_int(&sha1_ctx, getuid());
+    sha1_update_int(&sha1_ctx, getgid());
+#endif
     sha1_final(&sha1_ctx, sha1);
 
     /* Generate key. */