From 0bc03b0fd47a298ac17940aa7130f3010baccc79 Mon Sep 17 00:00:00 2001
From: Michael Kirk <michael@signal.org>
Date: Thu, 2 Aug 2018 17:21:01 -0600
Subject: [PATCH] Move seed

---
 Signal.xcodeproj/project.pbxproj              |  4 ----
 Signal/src/AppDelegate.m                      |  2 +-
 SignalMessaging/utils/OWSMath.h               |  2 --
 SignalMessaging/utils/OWSMath.m               | 19 -------------------
 SignalServiceKit/src/Util/Cryptography.h      |  3 +++
 SignalServiceKit/src/Util/Cryptography.m      | 15 +++++++++++++++
 .../ShareViewController.swift                 |  2 +-
 7 files changed, 20 insertions(+), 27 deletions(-)
 delete mode 100644 SignalMessaging/utils/OWSMath.m

diff --git a/Signal.xcodeproj/project.pbxproj b/Signal.xcodeproj/project.pbxproj
index e795afba6..35a953caa 100644
--- a/Signal.xcodeproj/project.pbxproj
+++ b/Signal.xcodeproj/project.pbxproj
@@ -99,7 +99,6 @@
 		3461284B1FD0B94000532771 /* SAELoadViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3461284A1FD0B93F00532771 /* SAELoadViewController.swift */; };
 		346129391FD1B47300532771 /* OWSPreferences.h in Headers */ = {isa = PBXBuildFile; fileRef = 346129371FD1B47200532771 /* OWSPreferences.h */; settings = {ATTRIBUTES = (Public, ); }; };
 		3461293A1FD1B47300532771 /* OWSPreferences.m in Sources */ = {isa = PBXBuildFile; fileRef = 346129381FD1B47200532771 /* OWSPreferences.m */; };
-		3461293C1FD1D46A00532771 /* OWSMath.m in Sources */ = {isa = PBXBuildFile; fileRef = 3461293B1FD1D46900532771 /* OWSMath.m */; };
 		3461293E1FD1D72B00532771 /* ExperienceUpgradeFinder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3461293D1FD1D72B00532771 /* ExperienceUpgradeFinder.swift */; };
 		3461295A1FD1D74C00532771 /* Environment.h in Headers */ = {isa = PBXBuildFile; fileRef = 346129401FD1D74B00532771 /* Environment.h */; settings = {ATTRIBUTES = (Public, ); }; };
 		3461295B1FD1D74C00532771 /* Environment.m in Sources */ = {isa = PBXBuildFile; fileRef = 346129411FD1D74B00532771 /* Environment.m */; };
@@ -734,7 +733,6 @@
 		3461284A1FD0B93F00532771 /* SAELoadViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SAELoadViewController.swift; sourceTree = "<group>"; };
 		346129371FD1B47200532771 /* OWSPreferences.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OWSPreferences.h; sourceTree = "<group>"; };
 		346129381FD1B47200532771 /* OWSPreferences.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OWSPreferences.m; sourceTree = "<group>"; };
-		3461293B1FD1D46900532771 /* OWSMath.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OWSMath.m; sourceTree = "<group>"; };
 		3461293D1FD1D72B00532771 /* ExperienceUpgradeFinder.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = ExperienceUpgradeFinder.swift; path = ExperienceUpgrades/ExperienceUpgradeFinder.swift; sourceTree = "<group>"; };
 		346129401FD1D74B00532771 /* Environment.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Environment.h; sourceTree = "<group>"; };
 		346129411FD1D74B00532771 /* Environment.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Environment.m; sourceTree = "<group>"; };
@@ -1548,7 +1546,6 @@
 				45666EC71D994C0D008FE134 /* OWSGroupAvatarBuilder.h */,
 				45666EC81D994C0D008FE134 /* OWSGroupAvatarBuilder.m */,
 				34480B481FD0A60200BC14EF /* OWSMath.h */,
-				3461293B1FD1D46900532771 /* OWSMath.m */,
 				346129371FD1B47200532771 /* OWSPreferences.h */,
 				346129381FD1B47200532771 /* OWSPreferences.m */,
 				34641E172088D7E900E2EDE5 /* OWSScreenLock.swift */,
@@ -3264,7 +3261,6 @@
 				34ABB2C42090C59700C727A6 /* OWSResaveCollectionDBMigration.m in Sources */,
 				4551DB5A205C562300C8AE75 /* Collection+OWS.swift in Sources */,
 				450C800E20AD1A6500F3A091 /* OWSNavigationBar.swift in Sources */,
-				3461293C1FD1D46A00532771 /* OWSMath.m in Sources */,
 				451F8A391FD711D6005CB9DA /* ContactsViewHelper.m in Sources */,
 				346129AF1FD1F5D900532771 /* SystemContactsFetcher.swift in Sources */,
 				344F248B20069F0600CFB4F4 /* ViewControllerUtils.m in Sources */,
diff --git a/Signal/src/AppDelegate.m b/Signal/src/AppDelegate.m
index 45fd3174e..08de4d05c 100644
--- a/Signal/src/AppDelegate.m
+++ b/Signal/src/AppDelegate.m
@@ -123,7 +123,7 @@ static NSTimeInterval launchStartedAt;
 
     DDLogWarn(@"%@ application: didFinishLaunchingWithOptions.", self.logTag);
 
-    SetRandFunctionSeed();
+    [Cryptography seedRandom];
 
     // XXX - careful when moving this. It must happen before we initialize OWSPrimaryStorage.
     [self verifyDBKeysAvailableBeforeBackgroundLaunch];
diff --git a/SignalMessaging/utils/OWSMath.h b/SignalMessaging/utils/OWSMath.h
index e48779f71..ea8963d88 100644
--- a/SignalMessaging/utils/OWSMath.h
+++ b/SignalMessaging/utils/OWSMath.h
@@ -33,6 +33,4 @@ static inline CGFloat CeilEven(CGFloat value)
     return 2.f * (CGFloat)ceil(value * 0.5f);
 }
 
-void SetRandFunctionSeed(void);
-
 NS_ASSUME_NONNULL_END
diff --git a/SignalMessaging/utils/OWSMath.m b/SignalMessaging/utils/OWSMath.m
deleted file mode 100644
index 375a726af..000000000
--- a/SignalMessaging/utils/OWSMath.m
+++ /dev/null
@@ -1,19 +0,0 @@
-//
-//  Copyright (c) 2017 Open Whisper Systems. All rights reserved.
-//
-
-#import "OWSMath.h"
-
-NS_ASSUME_NONNULL_BEGIN
-
-void SetRandFunctionSeed(void)
-{
-    // Set the seed the generator for rand().
-    //
-    // We should always use arc4random() instead of rand(), but we
-    // still want to ensure that any third-party code that uses rand()
-    // gets random values.
-    srand((unsigned int)time(NULL));
-}
-
-NS_ASSUME_NONNULL_END
diff --git a/SignalServiceKit/src/Util/Cryptography.h b/SignalServiceKit/src/Util/Cryptography.h
index d88a0877c..5a8cba10d 100755
--- a/SignalServiceKit/src/Util/Cryptography.h
+++ b/SignalServiceKit/src/Util/Cryptography.h
@@ -55,6 +55,7 @@ typedef NS_ENUM(NSInteger, TSMACType) {
 
 + (uint32_t)randomUInt32;
 + (uint64_t)randomUInt64;
++ (unsigned)randomUnsigned;
 
 #pragma mark - SHA and HMAC methods
 
@@ -95,6 +96,8 @@ typedef NS_ENUM(NSInteger, TSMACType) {
 + (nullable NSData *)encryptAESGCMWithProfileData:(NSData *)plaintextData key:(OWSAES256Key *)key;
 + (nullable NSData *)decryptAESGCMWithProfileData:(NSData *)encryptedData key:(OWSAES256Key *)key;
 
++ (void)seedRandom;
+
 @end
 
 NS_ASSUME_NONNULL_END
diff --git a/SignalServiceKit/src/Util/Cryptography.m b/SignalServiceKit/src/Util/Cryptography.m
index f1fbc3767..86092a899 100755
--- a/SignalServiceKit/src/Util/Cryptography.m
+++ b/SignalServiceKit/src/Util/Cryptography.m
@@ -151,6 +151,15 @@ const NSUInteger kAES256_KeyByteLength = 32;
     return result;
 }
 
++ (unsigned)randomUnsigned
+{
+    size_t size = sizeof(unsigned);
+    NSData *data = [self generateRandomBytes:size];
+    unsigned result = 0;
+    [data getBytes:&result range:NSMakeRange(0, size)];
+    return result;
+}
+
 #pragma mark - SHA1
 
 // Used by TSContactManager to send hashed/truncated contact list to server.
@@ -777,6 +786,12 @@ const NSUInteger kAES256_KeyByteLength = 32;
                                                    key:key];
 }
 
++ (void)seedRandom
+{
+    unsigned seed = [Cryptography randomUnsigned];
+    srand(seed);
+}
+
 @end
 
 NS_ASSUME_NONNULL_END
diff --git a/SignalShareExtension/ShareViewController.swift b/SignalShareExtension/ShareViewController.swift
index 932aa22e3..07ba68ad6 100644
--- a/SignalShareExtension/ShareViewController.swift
+++ b/SignalShareExtension/ShareViewController.swift
@@ -48,7 +48,7 @@ public class ShareViewController: UIViewController, ShareViewDelegate, SAEFailed
 
         startupLogging()
 
-        SetRandFunctionSeed()
+        Cryptography.seedRandom()
 
         // We don't need to use DeviceSleepManager in the SAE.