V0.7.0 Arroyo Toad

Fixed support integrating MessageUtils Library
This commit is contained in:
Christian Risi 2024-12-09 12:58:26 +00:00
parent 2ebad7f68e
commit 5b358b8bef
16 changed files with 116 additions and 211 deletions

View File

@ -1,5 +1,5 @@
{
"originHash" : "a49eafc65c63e9300677cadad384d86aab484d1c10a415ec467d840aff87c075",
"originHash" : "5001bc8eac215b7cfcb9159953b561765b257824796f7e76cbc179ccc37c3e81",
"pins" : [
{
"identity" : "swift-asn1",
@ -18,6 +18,15 @@
"branch" : "main",
"revision" : "dc4c2c14e7ff95ee3aa8d3c2a217a248f51d3688"
}
},
{
"identity" : "swift-messageutils",
"kind" : "remoteSourceControl",
"location" : "https://repositories.communitynotfound.work/PoliBa-Software-Architecture/Swift-MessageUtils.git",
"state" : {
"branch" : "main",
"revision" : "8ae58929b01bf0b1a4a8c31b81dbbf18a3876feb"
}
}
],
"version" : 3

View File

@ -23,6 +23,7 @@ let package = Package(
],
dependencies: [
.package(url: "https://github.com/apple/swift-crypto.git", branch: "main"),
.package(url: "https://repositories.communitynotfound.work/PoliBa-Software-Architecture/Swift-MessageUtils.git", branch: "main")
],
targets: [
// Targets are the basic building blocks of a package, defining a module or a test suite.
@ -41,17 +42,17 @@ let package = Package(
"RandomCpp",
"DataAcquisition",
.product(name: "Crypto", package: "swift-crypto"),
.product(name: "MessageUtils", package: "swift-messageutils"),
],
swiftSettings: [
.interoperabilityMode(.Cxx),
.interoperabilityMode(.C)
]),
.testTarget(
name: "IoT-Simulator-CoreTests",
dependencies: ["IoT-Simulator-Core"],
swiftSettings: [
.interoperabilityMode(.C),
.interoperabilityMode(.Cxx),
]

View File

@ -1,16 +0,0 @@
-----BEGIN CERTIFICATE-----
MIICeTCCAdqgAwIBAgIUeKyiiDJdVGH3ParIry5vn/YGnaowCgYIKoZIzj0EAwIw
TjELMAkGA1UEBhMCSVQxDTALBgNVBAgMBEJhcmkxDTALBgNVBAcMBEJhcmkxITAf
BgNVBAoMGEludGVybmV0IFdpZGdpd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z9w==
-----END EC PRIVATE KEY-----
MHcCAQEEIIHjmZWSXyYrRusrK1z3TDaZY5mBeed3vODCxcwu0FsKoAoGCCqGSM49
AwEHoUQDQgAEsszGIDjEgu6k/MkW+p5Bf+UPEU/jF9bLykzEOzP3rD/HJ2AprRpV
m+PNIaLThIdUTPsO2BBBLH2CaAJ/1x65Wg==
-----END EC PRIVATE KEY-----

View File

@ -1,8 +0,0 @@
-----BEGIN PRIVATE KEY-----
MIHuAgEAMBAGByqGSM49AgEGBSuBBAAjBIHWMIHTAgEBBEIBAQAy+3ElWUTttb9x
xVDshJlGt/clGdhPkp76aJ3LJySugsnC8RROUracnWQi2A+XnEI1ZskzYAFUfh7G
5o5ViDyhgYkDgYYABAAAfWnGEUMElY/XIWUTPvX65HX3N5IkJKPdVFzDRtaTHRJK
nEEvU7Z5iLAT9NpbVfCabvQXKo7LD5sjoJ1ZpSVcogDgCFCopmVin2ZLs5lyMtae
tpVDH8m+AIlRQkkuGmkasM+OV62kzSoHl/CL4eNz1xXwqsPtoBgvPiRFxNIE/0dz
9w==
-----END PRIVATE KEY-----

View File

@ -1,34 +1,36 @@
import Foundation
import Crypto
import DataAcquisition
import Foundation
import MessageUtils
public class EdgeDevice: EdgeDeviceP {
public let deviceID: String
public let deviceID: UInt128
public let deviceType: DeviceType
public let dataType: DataType
public var disconnected: Bool
public var compromised: Bool
public var location: Location3D
public var location: Location
public var dutyCicle: UInt
public var sensors: [Int: Sensor]
public var privateKey: P521.Signing.PrivateKey
public var privateKey: P256.Signing.PrivateKey
private var numberOfSensors: Int {
return sensors.count
}
public init(
deviceID: String,
deviceID: UInt128,
dataType: DataType,
disconnected: Bool,
location: Location3D,
location: Location,
dutyCicle: UInt,
sensors: [Int: Sensor],
privateKey: P521.Signing.PrivateKey
privateKey: P256.Signing.PrivateKey
) {
self.deviceID = deviceID
self.deviceType = DeviceType.EdgeDevice
self.deviceType = DeviceType.EDGE_SENSOR
self.dataType = dataType
self.disconnected = disconnected
self.compromised = false
@ -46,7 +48,7 @@ public class EdgeDevice: EdgeDeviceP {
self.sensors.removeValue(forKey: id)
}
public func work(envrionment: PhysicalEnvironment) -> Message {
public func work(envrionment: PhysicalEnvironment) throws -> Data {
// UGLY: Declaring here some variables manually, remove them if you want to offere flexibility
let numberOfSamples: Int = 10
@ -70,11 +72,15 @@ public class EdgeDevice: EdgeDeviceP {
let metrics: Metrics = getMetrics(
rowPointer, Int32(self.sensors.count), Int32(numberOfSamples))
// Todo: END Add Vincenzo's implementation
// UGLY: Supposing everytihing is fine
let msg: Message = Message(
msgType: MessageType.Data,
version: 1,
messageType: MessageType.DATA,
devType: self.deviceType,
RESERVED: 0,
signType: SignType.P256,
timestamp: Date(),
deviceID: self.deviceID,
devID: self.deviceID,
location: self.location,
fields: [
Field(key: "data_type", value: "\(self.dataType)"),
@ -83,9 +89,14 @@ public class EdgeDevice: EdgeDeviceP {
]
)
msg.signMessage(key: self.privateKey)
var msgData = serializeV1(msg: msg)
let signature = try signMessage(msgData: msgData, signType: msg.signType, key: self.privateKey)
return msg
msgData.append(Data(signature))
return msgData
}
}

View File

@ -1,16 +0,0 @@
public class Field : Codable{
public let key: String
public let value: String
public init(key: String, value: String) {
self.key = key
self.value = value
}
public var description: String {
return "\(self.key): \t\(self.value)"
}
}

View File

@ -1,16 +0,0 @@
public class Location3D {
public let x : Float
public let y : Float
public let z : Float
public init(_ x: Float, _ y: Float, _ z : Float) {
self.x = x
self.y = y
self.z = z
}
public var description: String {
return "X: \(self.x)\tY: \(self.y)\tZ: \(self.z)"
}
}

View File

@ -1,68 +0,0 @@
import Crypto
import Foundation
public class Message {
public let messageType: MessageType
public let timestamp: Date
public let deviceID: String
public let location: Location3D
public let fields: [Field]
public var signature: String {
return self._signature != nil ? self._signature! : "##INVALID"
}
private var _signature: String? = nil
public init(
msgType: MessageType,
timestamp: Date,
deviceID: String,
location: Location3D,
fields: [Field]
) {
self.messageType = msgType
self.timestamp = timestamp
self.deviceID = deviceID
self.location = location
self.fields = fields
}
public func toDataCompatibleString() -> String {
var string: String = "\(self.messageType)"
string += "\(self.timestamp)"
string += "\(self.deviceID)"
string += "\(self.location.x)"
string += "\(self.location.y)"
string += "\(self.location.z)"
for field in self.fields {
string += "\(field.key)\(field.value)"
}
return string
}
public func signMessage(key: P521.Signing.PrivateKey) {
do {
self._signature = try sign(string: self.toDataCompatibleString(), key: key)
} catch {
// Do nothing
}
}
public var description: String {
var string: String = "MessageType: \t\(self.messageType)\n"
string += "Timestamp: \t\(self.timestamp)\n"
string += "DeviceID: \t\(self.deviceID)\n"
string += "Location: \t\(self.location.description)\n"
for field in self.fields {
string += "\(field.description)\n"
}
return string
}
}

View File

@ -1,4 +0,0 @@
public enum DeviceType : Sendable{
case AsyncEdgeDevice
case EdgeDevice
}

View File

@ -1,7 +0,0 @@
public enum MessageType {
case KeepAlive
case Data
case Info
case Warning
case Critical
}

View File

@ -7,7 +7,7 @@ public actor IoTSimulatorCore {
private static var enviroments: [String: PhysicalEnvironment] = Dictionary()
private static var devices: [String: EdgeDeviceP] = Dictionary()
private static var env_dev: [String: Set<String>] = Dictionary()
private static var env_dev: [String: Set<UInt128>] = Dictionary()
private static var dev_tasks: [String: Task<(), Never>] = Dictionary()
public static func addEnv(environment: PhysicalEnvironment) {
@ -16,7 +16,7 @@ public actor IoTSimulatorCore {
public static func addDevice(location: String, device: EdgeDeviceP) throws {
if let environment = IoTSimulatorCore.enviroments[location] {
IoTSimulatorCore.devices[device.deviceID] = device
IoTSimulatorCore.devices["\(device.deviceID)"] = device
if IoTSimulatorCore.env_dev[location] == nil {
IoTSimulatorCore.env_dev[location] = Set()
@ -26,12 +26,11 @@ public actor IoTSimulatorCore {
// schedule work
let task = IoTSimulatorCore.schedule(envID: environment.location, deviceID: device.deviceID) { msg in
print("\(msg.description)\n\n")
print("\(msg)\n\n")
} failure: {
print("Something is wrong")
}
IoTSimulatorCore.dev_tasks[device.deviceID] = task
IoTSimulatorCore.dev_tasks["\(device.deviceID)"] = task
} else {
throw CoreError.NoEnvironment
@ -43,6 +42,10 @@ public actor IoTSimulatorCore {
return IoTSimulatorCore.enviroments[name]
}
public static func getDev(devID: UInt128) -> EdgeDeviceP? {
return IoTSimulatorCore.devices["\(devID)"]
}
public static func getDev(devID: String) -> EdgeDeviceP? {
return IoTSimulatorCore.devices[devID]
}
@ -53,23 +56,27 @@ public actor IoTSimulatorCore {
}
}
private static func schedule(
envID: sending String,
deviceID: sending String,
success: sending @escaping (_ msg: Message) throws -> Void,
deviceID: UInt128,
success: sending @escaping (_ msg: Data) throws -> Void,
failure: sending @escaping () -> Void
) -> Task<(), Never> {
let _devID : String = "\(deviceID)"
return Task {
var notCancelled: Bool = true
let dev = IoTSimulatorCore.getDev(devID: deviceID)!
let dev = IoTSimulatorCore.getDev(devID: _devID)!
let env = IoTSimulatorCore.getEnv(name: envID)!
while notCancelled {
let message = dev.work(envrionment: env)
print("Device: \(dev.deviceID)")
do {
let message = try dev.work(envrionment: env)
try success(message)
} catch {
failure()

View File

@ -1,16 +1,18 @@
import Crypto
import Foundation
import Crypto
import MessageUtils
public protocol EdgeDeviceP {
var deviceID : String {get}
var deviceID : UInt128 {get}
var deviceType : DeviceType {get}
var dataType : DataType {get}
var location: Location3D {get set}
var location: Location{get set}
var disconnected : Bool {get set}
var compromised : Bool {get set}
var dutyCicle : UInt {get set}
var privateKey: P521.Signing.PrivateKey {get}
var privateKey: P256.Signing.PrivateKey {get}
func work(envrionment: PhysicalEnvironment) -> Message
func work(envrionment: PhysicalEnvironment) throws -> Data
}

View File

@ -0,0 +1,16 @@
import MessageUtils
extension Field {
public init(
key: String,
value: String
) {
self.init(
key: Array(key.utf8),
value: Array(value.utf8)
)
}
}

View File

@ -2,6 +2,7 @@ import Testing
import RandomCpp
import Foundation
import Crypto
import MessageUtils
@testable import IoT_Simulator_Core
@ -82,13 +83,13 @@ import Crypto
let signKeyPath = "./Private/privateKey.pem"
let privateKey = try pem2key(filePath: signKeyPath)
let privateKey = try pem2_P256key(filePath: signKeyPath)
let dev: EdgeDevice = EdgeDevice(
deviceID: "EDG-001",
deviceID: 1,
dataType: .Temperature,
disconnected: false,
location: Location3D(20, 10, 0),
location: Location(x: 20, y: 10, z: 0),
dutyCicle: 100098,
sensors: [
0: Sensor(id: 0, sensorType: DataType.Temperature),
@ -98,22 +99,19 @@ import Crypto
privateKey: privateKey
)
let message = dev.work(envrionment: env)
message.signMessage(key: dev.privateKey)
let message = try dev.work(envrionment: env)
let signedMessage = try deserializeV1(serializedData: message)
#expect(message != nil, "If this is nil, I don't knwo what's going on")
#expect(message.signature != nil, "If signature is nil, something is wrong")
print(signedMessage.toString())
print(signedMessage.signature)
#expect(signedMessage != nil, "If this is nil, I don't knwo what's going on")
#expect(signedMessage.signature != nil, "If signature is nil, something is wrong")
print(message.description)
print(message.signature)
#expect(
try verifySignature(
signature:message.signature,
string: message.toDataCompatibleString(),
key: dev.privateKey.publicKey
),
try verifyMessageSignature(message: signedMessage, key: privateKey.publicKey) ,
"Let's see that signatures match"
)
}
@ -127,13 +125,13 @@ import Crypto
let signKeyPath = "./Private/privateKey.pem"
let privateKey = try pem2key(filePath: signKeyPath)
let privateKey = try pem2_P256key(filePath: signKeyPath)
let dev: EdgeDevice = EdgeDevice(
deviceID: "EDG-001",
deviceID: 1,
dataType: .Temperature,
disconnected: false,
location: Location3D(20, 10, 0),
location: Location(x: 20, y: 10, z: 0),
dutyCicle: 100098,
sensors: [
0: RealSensor(sensorID: 0, sensorType: .Temperature, faulty: false, meanNoise: 1, stdNoise: 3, quantizationBits: 3),
@ -143,22 +141,19 @@ import Crypto
privateKey: privateKey
)
let message = dev.work(envrionment: env)
message.signMessage(key: dev.privateKey)
let message = try dev.work(envrionment: env)
let signedMessage = try deserializeV1(serializedData: message)
#expect(message != nil, "If this is nil, I don't knwo what's going on")
#expect(message.signature != nil, "If signature is nil, something is wrong")
print(signedMessage.toString())
print(signedMessage.signature)
#expect(signedMessage != nil, "If this is nil, I don't knwo what's going on")
#expect(signedMessage.signature != nil, "If signature is nil, something is wrong")
print(message.description)
print(message.signature)
#expect(
try verifySignature(
signature:message.signature,
string: message.toDataCompatibleString(),
key: dev.privateKey.publicKey
),
try verifyMessageSignature(message: signedMessage, key: privateKey.publicKey) ,
"Let's see that signatures match"
)
}

View File

@ -1,6 +1,7 @@
import DataAcquisition
import Foundation
import Testing
import MessageUtils
@testable import IoT_Simulator_Core
@ -15,13 +16,13 @@ import Testing
let signKeyPath = "./Private/privateKey.pem"
let privateKey = try pem2key(filePath: signKeyPath)
let privateKey = try pem2_P256key(filePath: signKeyPath)
let dev: EdgeDevice = EdgeDevice(
deviceID: "EDG-001",
deviceID: 1,
dataType: .Temperature,
disconnected: false,
location: Location3D(20, 10, 0),
location: Location(x: 20, y: 10, z: 0),
dutyCicle: 3000,
sensors: [
0: RealSensor(
@ -38,10 +39,10 @@ import Testing
)
let dev2: EdgeDevice = EdgeDevice(
deviceID: "EDG-002",
deviceID: 2,
dataType: .Temperature,
disconnected: false,
location: Location3D(20, 10, 0),
location: Location(x: 20, y: 10, z: 0),
dutyCicle: 1000,
sensors: [
0: RealSensor(
@ -66,7 +67,7 @@ import Testing
@Test func stressLoop1() async throws {
let devices: Int = 1000
let devices: UInt128 = 600
let env = PhysicalEnvironment("Delta")
let truth = PhysicalData(.Temperature, 22)
@ -77,15 +78,15 @@ import Testing
let signKeyPath = "./Private/privateKey.pem"
let privateKey = try pem2key(filePath: signKeyPath)
let privateKey = try pem2_P256key(filePath: signKeyPath)
for i in 0..<devices {
for i: UInt128 in 0..<devices {
let dev: EdgeDevice = EdgeDevice(
deviceID: "EDG-00\(i)",
deviceID: i,
dataType: .Temperature,
disconnected: false,
location: Location3D(20, 10, 0),
location: Location(x: 20, y: 10, z: 0),
dutyCicle: 200 + UInt.random(in: 1000...5000),
sensors: [
0: RealSensor(