diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 341fe88..2f7b112 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -9,7 +9,9 @@ "customizations": { "vscode": { "extensions": [ - "sswg.swift-lang" + "sswg.swift-lang", + "fabiospampinato.vscode-highlight", + "fabiospampinato.vscode-todo-plus" ] } }, diff --git a/Package.resolved b/Package.resolved new file mode 100644 index 0000000..01475fa --- /dev/null +++ b/Package.resolved @@ -0,0 +1,24 @@ +{ + "originHash" : "a49eafc65c63e9300677cadad384d86aab484d1c10a415ec467d840aff87c075", + "pins" : [ + { + "identity" : "swift-asn1", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-asn1.git", + "state" : { + "revision" : "7faebca1ea4f9aaf0cda1cef7c43aecd2311ddf6", + "version" : "1.3.0" + } + }, + { + "identity" : "swift-crypto", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-crypto.git", + "state" : { + "branch" : "main", + "revision" : "dc4c2c14e7ff95ee3aa8d3c2a217a248f51d3688" + } + } + ], + "version" : 3 +} diff --git a/Package.swift b/Package.swift index 5a57155..5d3758d 100644 --- a/Package.swift +++ b/Package.swift @@ -10,20 +10,29 @@ let package = Package( .library( name: "IoT-Simulator-Core", targets: ["IoT-Simulator-Core"] - ), - .library(name: "RandomCpp", targets: ["RandomCpp"]) + ), + .library( + name: "RandomCpp", + targets: ["RandomCpp"] + ), + ], - dependencies: [], + dependencies: [ + .package(url: "https://github.com/apple/swift-crypto.git", branch: "main"), + ], targets: [ // Targets are the basic building blocks of a package, defining a module or a test suite. // Targets can depend on other targets in this package and products from dependencies. .target( name: "RandomCpp" ), + .target( name: "IoT-Simulator-Core", dependencies: [ "RandomCpp", + .product(name: "Crypto", package: "swift-crypto"), + ], swiftSettings: [ .interoperabilityMode(.Cxx) diff --git a/Private/cert.pem b/Private/cert.pem new file mode 100644 index 0000000..86323fe --- /dev/null +++ b/Private/cert.pem @@ -0,0 +1,16 @@ +-----BEGIN CERTIFICATE----- +MIICeTCCAdqgAwIBAgIUeKyiiDJdVGH3ParIry5vn/YGnaowCgYIKoZIzj0EAwIw +TjELMAkGA1UEBhMCSVQxDTALBgNVBAgMBEJhcmkxDTALBgNVBAcMBEJhcmkxITAf +BgNVBAoMGEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDAeFw0yNDEyMDIxNjM0NDVa +Fw0yNTAxMDExNjM0NDVaME4xCzAJBgNVBAYTAklUMQ0wCwYDVQQIDARCYXJpMQ0w +CwYDVQQHDARCYXJpMSEwHwYDVQQKDBhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQw +gZswEAYHKoZIzj0CAQYFK4EEACMDgYYABAAAfWnGEUMElY/XIWUTPvX65HX3N5Ik +JKPdVFzDRtaTHRJKnEEvU7Z5iLAT9NpbVfCabvQXKo7LD5sjoJ1ZpSVcogDgCFCo +pmVin2ZLs5lyMtaetpVDH8m+AIlRQkkuGmkasM+OV62kzSoHl/CL4eNz1xXwqsPt +oBgvPiRFxNIE/0dz96NTMFEwHQYDVR0OBBYEFKW5mYrSXJn68diXLDjhbiEGAxJu +MB8GA1UdIwQYMBaAFKW5mYrSXJn68diXLDjhbiEGAxJuMA8GA1UdEwEB/wQFMAMB +Af8wCgYIKoZIzj0EAwIDgYwAMIGIAkIAhVgtxgnZd6KeefLjZ6Mazgr5xLDcAHyI +NsKtTw3YzT/Pztnk2ccV+NyDZyoTG72lHoPMTiB5mRSUTqORg59XQTkCQgDqHRoN +tRQlPWY3abohilRRdvYZrsoPR8FzB/M4KxT0nk10jc1wtosQ7l/XZGcKe8/k+iVs +HC5CsESzsvnp+Qslyw== +-----END CERTIFICATE----- diff --git a/Private/privateKey.pem b/Private/privateKey.pem new file mode 100644 index 0000000..c2a1202 --- /dev/null +++ b/Private/privateKey.pem @@ -0,0 +1,7 @@ +-----BEGIN EC PRIVATE KEY----- +MIHcAgEBBEIBAQAy+3ElWUTttb9xxVDshJlGt/clGdhPkp76aJ3LJySugsnC8RRO +UracnWQi2A+XnEI1ZskzYAFUfh7G5o5ViDygBwYFK4EEACOhgYkDgYYABAAAfWnG +EUMElY/XIWUTPvX65HX3N5IkJKPdVFzDRtaTHRJKnEEvU7Z5iLAT9NpbVfCabvQX +Ko7LD5sjoJ1ZpSVcogDgCFCopmVin2ZLs5lyMtaetpVDH8m+AIlRQkkuGmkasM+O +V62kzSoHl/CL4eNz1xXwqsPtoBgvPiRFxNIE/0dz9w== +-----END EC PRIVATE KEY----- diff --git a/Private/privateKey_ASN1.pem b/Private/privateKey_ASN1.pem new file mode 100644 index 0000000..f1f5b78 --- /dev/null +++ b/Private/privateKey_ASN1.pem @@ -0,0 +1,8 @@ +-----BEGIN PRIVATE KEY----- +MIHuAgEAMBAGByqGSM49AgEGBSuBBAAjBIHWMIHTAgEBBEIBAQAy+3ElWUTttb9x +xVDshJlGt/clGdhPkp76aJ3LJySugsnC8RROUracnWQi2A+XnEI1ZskzYAFUfh7G +5o5ViDyhgYkDgYYABAAAfWnGEUMElY/XIWUTPvX65HX3N5IkJKPdVFzDRtaTHRJK +nEEvU7Z5iLAT9NpbVfCabvQXKo7LD5sjoJ1ZpSVcogDgCFCopmVin2ZLs5lyMtae +tpVDH8m+AIlRQkkuGmkasM+OV62kzSoHl/CL4eNz1xXwqsPtoBgvPiRFxNIE/0dz +9w== +-----END PRIVATE KEY----- \ No newline at end of file diff --git a/Scripts/openssl-ecdsa-certs.sh b/Scripts/openssl-ecdsa-certs.sh new file mode 100644 index 0000000..b478df1 --- /dev/null +++ b/Scripts/openssl-ecdsa-certs.sh @@ -0,0 +1,2 @@ +openssl ecparam -name secp521r1 -genkey -noout -out privateKey.pem +openssl req -x509 -sha256 -key privateKey.pem -out cert.pem -nodes \ No newline at end of file diff --git a/Sources/IoT-Simulator-Core/Classes/Devices/EdgeDevice.swift b/Sources/IoT-Simulator-Core/Classes/Devices/EdgeDevice.swift index e69de29..bf42e8c 100644 --- a/Sources/IoT-Simulator-Core/Classes/Devices/EdgeDevice.swift +++ b/Sources/IoT-Simulator-Core/Classes/Devices/EdgeDevice.swift @@ -0,0 +1,73 @@ +/* public class EdgeDevice : EdgeDeviceP { + + public let deviceID: String + public let deviceType: DeviceType + public let dataType: DataType + public var disconnected: Bool + public var compromised: Bool + public var dutyCicle: UInt + public var sensors: [Int: Sensor] + public var privateKey: [UInt8] + + private var numberOfSensors: Int { + get{ + return sensors.count + } + } + + public init( + deviceID: String, + dataType: DataType, + disconnected: Bool, + dutyCicle: UInt, + sensors: [Int: Sensor] + ) { + self.deviceID = deviceID + self.deviceType = DeviceType.EdgeDevice + self.dataType = dataType + self.disconnected = disconnected + self.compromised = false + self.dutyCicle = dutyCicle + self.sensors = sensors + } + + public func addSensor(sensor: Sensor) { + self.sensors[numberOfSensors + 1] = sensor + } + + public func removeSensor(id: Int) { + self.sensors.removeValue(forKey: id) + } + + public func work(envrionment: PhysicalEnvironment) { + + // UGLY: In case I have some optimization problems, fix here + var values: [Float] = [] + + for sensor in sensors { + values.append( + sensor.value.read(envrionment).value + ) + } + + // Todo: START Remove this and Add Vincenzo's implementation + let avg : Float = meanValue(values: values) + let std_dev : Float = standardDeviation(values: values) + + // Todo: END Add Vincenzo's implementation + return Message( + msgType: MessageType, + timestamp: Date, + deviceID: String, + location: Location3D, + fields: [Field], + signature: [UInt8] + ) + + } + + + + + +} */ \ No newline at end of file diff --git a/Sources/IoT-Simulator-Core/Classes/Devices/EdgeFactories.swift b/Sources/IoT-Simulator-Core/Classes/Devices/EdgeFactories.swift new file mode 100644 index 0000000..e69de29 diff --git a/Sources/IoT-Simulator-Core/Classes/Devices/Sensors.swift b/Sources/IoT-Simulator-Core/Classes/Devices/Sensors.swift index 1e85e2b..fec5446 100644 --- a/Sources/IoT-Simulator-Core/Classes/Devices/Sensors.swift +++ b/Sources/IoT-Simulator-Core/Classes/Devices/Sensors.swift @@ -59,6 +59,7 @@ public class RealSensor: Sensor { private var _meanNoise: Float private var _stdNoise: Float private let _quantizationBits: Int + // TODO: add a generator of GaussianRNG //private var gaussianNoise: public init( @@ -90,7 +91,7 @@ public class RealSensor: Sensor { override public func read(_ environment: PhysicalEnvironment) -> PhysicalData { let value: PhysicalData = super.read(environment) - + // TODO Add gaussian error here return value } diff --git a/Sources/IoT-Simulator-Core/Classes/Utils/Field.swift b/Sources/IoT-Simulator-Core/Classes/Utils/Field.swift index 81311ae..71a94dd 100644 --- a/Sources/IoT-Simulator-Core/Classes/Utils/Field.swift +++ b/Sources/IoT-Simulator-Core/Classes/Utils/Field.swift @@ -1,3 +1,4 @@ + public class Field { public let key: String diff --git a/Sources/IoT-Simulator-Core/Errors/SecurityErrors.swift b/Sources/IoT-Simulator-Core/Errors/SecurityErrors.swift new file mode 100644 index 0000000..bc6bdc5 --- /dev/null +++ b/Sources/IoT-Simulator-Core/Errors/SecurityErrors.swift @@ -0,0 +1,4 @@ +enum SecurityError: Error { + case NotEncodableError + case NotDecodableError +} \ No newline at end of file diff --git a/Sources/IoT-Simulator-Core/Protocols/EdgeDeviceProtocol.swift b/Sources/IoT-Simulator-Core/Protocols/EdgeDeviceProtocol.swift index 1435070..8b0f70c 100644 --- a/Sources/IoT-Simulator-Core/Protocols/EdgeDeviceProtocol.swift +++ b/Sources/IoT-Simulator-Core/Protocols/EdgeDeviceProtocol.swift @@ -1,11 +1,13 @@ -public protocol EdgeDevice { +public protocol EdgeDeviceP { + var deviceID : String {get} var deviceType : DeviceType {get} var dataType : DataType {get} var disconnected : Bool {get set} var compromised : Bool {get set} var dutyCicle : UInt {get set} + var privateKey: [UInt8] {get} - func work() -> Message + func work(envrionment: PhysicalEnvironment) -> Message } \ No newline at end of file diff --git a/Sources/IoT-Simulator-Core/Utils/Security.swift b/Sources/IoT-Simulator-Core/Utils/Security.swift new file mode 100644 index 0000000..cd42d73 --- /dev/null +++ b/Sources/IoT-Simulator-Core/Utils/Security.swift @@ -0,0 +1,55 @@ +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 key.signature(for: data).rawRepresentation + +} + + + +// ------------------ +// --- Decrypt ------ +// ------------------ + +public func verify(signature: String, string: String, key: P521.Signing.PublicKey) throws -> Bool { + let data = Data(string.utf8) + + let ecdsa: P521.Signing.ECDSASignature + do { + let bytes = ecdsa + ecdsa = try P521.Signing.ECDSASignature(rawRepresentation: signature) + } catch { + throw SecurityError.NotDecodableError + } + + return key.isValidSignature(ecdsa, for: data) +} + + + +// ------------------ +// --- 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 new file mode 100644 index 0000000..c738fe3 --- /dev/null +++ b/Sources/IoT-Simulator-Core/Utils/Statistics.swift @@ -0,0 +1,28 @@ +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..