From 8be0f14aa92a31e3bbd1231e8f1957dc0bd1c483 Mon Sep 17 00:00:00 2001 From: Davide De Rosa Date: Sun, 19 May 2019 15:52:55 +0200 Subject: [PATCH] Move PRNG initialization to namespace level --- .../AppExtension/TunnelKitProvider.swift | 2 +- .../Sources/OpenVPN/EncryptionBridge.swift | 19 +------------------ TunnelKit/Sources/OpenVPN/OpenVPN.swift | 18 ++++++++++++++++++ 3 files changed, 20 insertions(+), 19 deletions(-) diff --git a/TunnelKit/Sources/AppExtension/TunnelKitProvider.swift b/TunnelKit/Sources/AppExtension/TunnelKitProvider.swift index 3996077..0444d9d 100644 --- a/TunnelKit/Sources/AppExtension/TunnelKitProvider.swift +++ b/TunnelKit/Sources/AppExtension/TunnelKitProvider.swift @@ -205,7 +205,7 @@ open class TunnelKitProvider: NEPacketTunnelProvider { log.info("Starting tunnel...") cfg.clearLastError(in: appGroup) - guard OpenVPN.EncryptionBridge.prepareRandomNumberGenerator(seedLength: prngSeedLength) else { + guard OpenVPN.prepareRandomNumberGenerator(seedLength: prngSeedLength) else { completionHandler(ProviderConfigurationError.prngInitialization) return } diff --git a/TunnelKit/Sources/OpenVPN/EncryptionBridge.swift b/TunnelKit/Sources/OpenVPN/EncryptionBridge.swift index efb1f64..91f1078 100644 --- a/TunnelKit/Sources/OpenVPN/EncryptionBridge.swift +++ b/TunnelKit/Sources/OpenVPN/EncryptionBridge.swift @@ -39,28 +39,11 @@ import __TunnelKitCore import __TunnelKitOpenVPN extension OpenVPN { - - /// Bridges native encryption for high-level operations. - public class EncryptionBridge { + class EncryptionBridge { private static let maxHmacLength = 100 private let box: CryptoBox - /** - Initializes the PRNG. Must be issued before using `OpenVPNSession`. - - - Parameter seedLength: The length in bytes of the pseudorandom seed that will feed the PRNG. - */ - public static func prepareRandomNumberGenerator(seedLength: Int) -> Bool { - let seed: ZeroingData - do { - seed = try SecureRandom.safeData(length: seedLength) - } catch { - return false - } - return CryptoBox.preparePRNG(withSeed: seed.bytes, length: seed.count) - } - // Ruby: keys_prf private static func keysPRF( _ label: String, diff --git a/TunnelKit/Sources/OpenVPN/OpenVPN.swift b/TunnelKit/Sources/OpenVPN/OpenVPN.swift index 500d042..f545e31 100644 --- a/TunnelKit/Sources/OpenVPN/OpenVPN.swift +++ b/TunnelKit/Sources/OpenVPN/OpenVPN.swift @@ -24,7 +24,25 @@ // import Foundation +import __TunnelKitCore +import __TunnelKitOpenVPN /// Container for OpenVPN classes. public class OpenVPN { + + /** + Initializes the PRNG. Must be issued before using `OpenVPNSession`. + + - Parameter seedLength: The length in bytes of the pseudorandom seed that will feed the PRNG. + */ + public static func prepareRandomNumberGenerator(seedLength: Int) -> Bool { + let seed: ZeroingData + do { + seed = try SecureRandom.safeData(length: seedLength) + } catch { + return false + } + return CryptoBox.preparePRNG(withSeed: seed.bytes, length: seed.count) + } + }