From 569b29c815791b65e28cb72496959865779b02bd Mon Sep 17 00:00:00 2001 From: Christian Risi <75698846+CnF-Gris@users.noreply.github.com> Date: Tue, 10 Dec 2024 17:58:01 +0100 Subject: [PATCH] V0.7.0 Arroyo Toad Added Factory to help the creation of EdgeDevices --- .../Classes/Devices/EdgeDevice.swift | 7 +- .../IoT-Simulator-Core/Enums/DataType.swift | 2 +- .../Errors/CoreErrors.swift | 4 + .../Factories/DeviceFactories.swift | 93 +++++++++++++++++++ .../Protocols/EdgeDeviceProtocol.swift | 1 + .../IoT-Simulator-Core/Utils/Security.swift | 82 ---------------- .../IoT-Simulator-Core/Utils/Statistics.swift | 28 ------ .../IoTCore-Tests.swift | 2 +- .../IoT_Simulator_CoreTests.swift | 63 ------------- 9 files changed, 106 insertions(+), 176 deletions(-) create mode 100644 Sources/IoT-Simulator-Core/Factories/DeviceFactories.swift delete mode 100644 Sources/IoT-Simulator-Core/Utils/Security.swift delete mode 100644 Sources/IoT-Simulator-Core/Utils/Statistics.swift delete mode 100644 Tests/IoT-Simulator-CoreTests/IoT_Simulator_CoreTests.swift diff --git a/Sources/IoT-Simulator-Core/Classes/Devices/EdgeDevice.swift b/Sources/IoT-Simulator-Core/Classes/Devices/EdgeDevice.swift index 792776d..6394619 100644 --- a/Sources/IoT-Simulator-Core/Classes/Devices/EdgeDevice.swift +++ b/Sources/IoT-Simulator-Core/Classes/Devices/EdgeDevice.swift @@ -15,12 +15,17 @@ public class EdgeDevice: EdgeDeviceP { public var dutyCicle: UInt public var sensors: [Int: Sensor] public var privateKey: P256.Signing.PrivateKey + public var publicKey: P256.Signing.PublicKey { + get{ + return self.privateKey.publicKey + } + } private var numberOfSensors: Int { return sensors.count } - public init( + internal init( deviceID: UInt128, dataType: DataType, disconnected: Bool, diff --git a/Sources/IoT-Simulator-Core/Enums/DataType.swift b/Sources/IoT-Simulator-Core/Enums/DataType.swift index 4dcd652..680d16e 100644 --- a/Sources/IoT-Simulator-Core/Enums/DataType.swift +++ b/Sources/IoT-Simulator-Core/Enums/DataType.swift @@ -1,4 +1,4 @@ -public enum DataType : Sendable{ +public enum DataType : String{ case Temperature case Humidity case Scan diff --git a/Sources/IoT-Simulator-Core/Errors/CoreErrors.swift b/Sources/IoT-Simulator-Core/Errors/CoreErrors.swift index 94fdad9..1cc3797 100644 --- a/Sources/IoT-Simulator-Core/Errors/CoreErrors.swift +++ b/Sources/IoT-Simulator-Core/Errors/CoreErrors.swift @@ -1,4 +1,8 @@ public enum CoreError : Error { case NoPhysicalDataAvailable( dataType: String) case NoEnvironment + case NoJSONDevice + case DeviceIDAlreadyExsitent + case InvalidDeviceID + case FinishedIDs } \ No newline at end of file diff --git a/Sources/IoT-Simulator-Core/Factories/DeviceFactories.swift b/Sources/IoT-Simulator-Core/Factories/DeviceFactories.swift new file mode 100644 index 0000000..df0ff2f --- /dev/null +++ b/Sources/IoT-Simulator-Core/Factories/DeviceFactories.swift @@ -0,0 +1,93 @@ +import Crypto +import MessageUtils + +public actor DeviceFactory { + + private static var setted: Bool = false + private static var availableIDs: Set = Set(0..<999) + + + public static func createEdgeDevice( + dataType: DataType, + privateKey: P256.Signing.PrivateKey + ) throws -> EdgeDevice{ + return try DeviceFactory.createEdgeDevice( + dataType: dataType, + privateKey: privateKey, + realSensor: false + ) + } + + public static func createEdgeDevice( + dataType: DataType, + privateKey: P256.Signing.PrivateKey, + realSensor: Bool + ) throws -> EdgeDevice{ + let deviceID = try DeviceFactory.getUnusedID() + + return try DeviceFactory.createEdgeDevice( + deviceID: deviceID, + dataType: dataType, + location: Location( + x: UInt64.random(in: 0...10000), + y: UInt64.random(in: 0...10000), + z: UInt64.random(in: 0...10000) + ), + dutyCicle: UInt.random(in: 0...5000), + privateKey: privateKey, + realSensor: realSensor + ) + } + + public static func createEdgeDevice( + deviceID: UInt128, + dataType: DataType, + location: Location, + dutyCicle: UInt, + privateKey: P256.Signing.PrivateKey, + realSensor: Bool + ) throws -> EdgeDevice { + + if !DeviceFactory.availableIDs.contains(deviceID) { + throw CoreError.InvalidDeviceID + } + + DeviceFactory.availableIDs.remove(deviceID) + + // Create sensors here + let sensors : [Int: Sensor] + + if !realSensor { + sensors = [ + 0: Sensor(id: 0, sensorType: dataType), + 1: Sensor(id: 1, sensorType: dataType), + 2: Sensor(id: 2, sensorType: dataType) + ] + } else { + sensors = [ + 0: RealSensor(sensorID: 0, sensorType: dataType, faulty: false, meanNoise: 0, stdNoise: 0.5, quantizationBits: 8), + 1: RealSensor(sensorID: 1, sensorType: dataType, faulty: false, meanNoise: 0, stdNoise: 0.5, quantizationBits: 8), + 2: RealSensor(sensorID: 2, sensorType: dataType, faulty: false, meanNoise: 0, stdNoise: 0.5, quantizationBits: 8) + ] + } + + return EdgeDevice( + deviceID: deviceID, + dataType: dataType, + disconnected: false, + location: location, + dutyCicle: dutyCicle, + sensors: sensors, + privateKey: privateKey) + } + + private static func getUnusedID() throws -> UInt128 { + + if DeviceFactory.availableIDs.count < 1 { + throw CoreError.FinishedIDs + } + + return DeviceFactory.availableIDs.removeFirst() + } + +} diff --git a/Sources/IoT-Simulator-Core/Protocols/EdgeDeviceProtocol.swift b/Sources/IoT-Simulator-Core/Protocols/EdgeDeviceProtocol.swift index 70eb00d..ee7ba1d 100644 --- a/Sources/IoT-Simulator-Core/Protocols/EdgeDeviceProtocol.swift +++ b/Sources/IoT-Simulator-Core/Protocols/EdgeDeviceProtocol.swift @@ -12,6 +12,7 @@ public protocol EdgeDeviceP { var compromised : Bool {get set} var dutyCicle : UInt {get set} var privateKey: P256.Signing.PrivateKey {get} + var publicKey: P256.Signing.PublicKey {get} func work(envrionment: PhysicalEnvironment) throws -> Data diff --git a/Sources/IoT-Simulator-Core/Utils/Security.swift b/Sources/IoT-Simulator-Core/Utils/Security.swift deleted file mode 100644 index a2000c9..0000000 --- a/Sources/IoT-Simulator-Core/Utils/Security.swift +++ /dev/null @@ -1,82 +0,0 @@ -import Crypto // Equivalent to CryptoKit (more or less) -import Foundation - - - -// ------------------ -// --- Sign --------- -// ------------------ - -public func sign(string: String, key: P521.Signing.PrivateKey) throws -> String { - let data = Data(string.utf8) - return try sign(object: data, key: key) - -} - -public func sign(object: Data, key: P521.Signing.PrivateKey)throws -> String { - return try key.signature(for: object).rawRepresentation.base64EncodedString() - -} -/* -public func sign(object: T, key: P521.Signing.PrivateKey) throws -> String { - - var _object = object - let data: Data = Data(bytes: &_object, count: MemoryLayout.stride) - -} */ - - - -// ------------------ -// --- Decrypt ------ -// ------------------ - -public func verifySignature(signature: String, string: String, key: P521.Signing.PublicKey) throws -> Bool { - let data = Data(string.utf8) - - let ecdsa: P521.Signing.ECDSASignature - do { - let bytes = Data(base64Encoded: signature)! - ecdsa = try P521.Signing.ECDSASignature(rawRepresentation: bytes) - } catch { - throw SecurityError.NotDecodableError - } - - return key.isValidSignature(ecdsa, for: data) -} - -public func verifySignature(signature: String, object: Data, key: P521.Signing.PublicKey) throws -> Bool { - - let ecdsa: P521.Signing.ECDSASignature - do { - let bytes = Data(base64Encoded: signature)! - ecdsa = try P521.Signing.ECDSASignature(rawRepresentation: bytes) - } catch { - throw SecurityError.NotDecodableError - } - - return key.isValidSignature(ecdsa, for: object) -} - - - -// ------------------ -// --- PEM 2 Key ---- -// ------------------ - -public func pem2key(filePath: String) throws -> P521.Signing.PrivateKey { - - let pemURL: URL = URL(filePath: filePath) - - return try pem2key(filePem: pemURL) -} - -public func pem2key(filePem: URL) throws -> P521.Signing.PrivateKey { - - let fileString: String = try String(contentsOf: filePem, encoding: String.Encoding.utf8) - return try pem2key(pemString: fileString) -} - -public func pem2key(pemString: String) throws -> P521.Signing.PrivateKey { - return try P521.Signing.PrivateKey(pemRepresentation: pemString) -} diff --git a/Sources/IoT-Simulator-Core/Utils/Statistics.swift b/Sources/IoT-Simulator-Core/Utils/Statistics.swift deleted file mode 100644 index c738fe3..0000000 --- a/Sources/IoT-Simulator-Core/Utils/Statistics.swift +++ /dev/null @@ -1,28 +0,0 @@ -import Foundation - -public func meanValue(values: [Numerical]) -> Numerical{ - var avg : Numerical = 0 - - for i in 0..(values: [Numerical]) -> Numerical{ - - let avg : Numerical = meanValue(values: values) - var summatory: Numerical = 0 - - for i in 0..