Added initial support for signature
This commit is contained in:
@@ -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]
|
||||
)
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
} */
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
|
||||
public class Field {
|
||||
|
||||
public let key: String
|
||||
|
||||
4
Sources/IoT-Simulator-Core/Errors/SecurityErrors.swift
Normal file
4
Sources/IoT-Simulator-Core/Errors/SecurityErrors.swift
Normal file
@@ -0,0 +1,4 @@
|
||||
enum SecurityError: Error {
|
||||
case NotEncodableError
|
||||
case NotDecodableError
|
||||
}
|
||||
@@ -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
|
||||
|
||||
}
|
||||
55
Sources/IoT-Simulator-Core/Utils/Security.swift
Normal file
55
Sources/IoT-Simulator-Core/Utils/Security.swift
Normal file
@@ -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<Data>(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<Data>(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)
|
||||
}
|
||||
28
Sources/IoT-Simulator-Core/Utils/Statistics.swift
Normal file
28
Sources/IoT-Simulator-Core/Utils/Statistics.swift
Normal file
@@ -0,0 +1,28 @@
|
||||
import Foundation
|
||||
|
||||
public func meanValue<Numerical: FloatingPoint>(values: [Numerical]) -> Numerical{
|
||||
var avg : Numerical = 0
|
||||
|
||||
for i in 0..<values.count {
|
||||
avg += values[i]
|
||||
}
|
||||
|
||||
return avg / Numerical(values.count)
|
||||
}
|
||||
|
||||
// UGLY Find a way to implement power protocol, if issues with optimization
|
||||
public func standardDeviation<Numerical: FloatingPoint>(values: [Numerical]) -> Numerical{
|
||||
|
||||
let avg : Numerical = meanValue(values: values)
|
||||
var summatory: Numerical = 0
|
||||
|
||||
for i in 0..<values.count {
|
||||
let intermediate = (values[i] - avg)
|
||||
summatory += (intermediate * intermediate)
|
||||
}
|
||||
|
||||
return sqrt(
|
||||
summatory / Numerical(values.count)
|
||||
)
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user