Sesame-iOS/Sesame/API/Message.swift

180 lines
4.9 KiB
Swift
Raw Normal View History

2022-04-13 14:55:22 +02:00
import Foundation
import NIOCore
2022-04-28 23:19:44 +02:00
#if canImport(CryptoKit)
import CryptoKit
#else
import Crypto
#endif
2022-04-13 14:55:22 +02:00
/**
An authenticated message to or from the device.
*/
struct Message: Equatable, Hashable {
2022-04-28 23:19:44 +02:00
/// The message authentication code for the message (32 bytes)
let mac: Data
/// The message content
let content: Content
/**
Create an authenticated message
- Parameter mac: The message authentication code
- Parameter content: The message content
*/
init(mac: Data, content: Content) {
self.mac = mac
self.content = content
}
}
extension Message: Codable {
enum CodingKeys: Int, CodingKey {
case mac = 1
case content = 2
}
}
2022-04-28 23:19:44 +02:00
extension Message {
2022-04-13 14:55:22 +02:00
/**
The message content without authentication.
*/
struct Content: Equatable, Hashable {
/// The time of message creation, in UNIX time (seconds since 1970)
let time: UInt32
/// The counter of the message (for freshness)
let id: UInt32
2023-08-07 15:47:40 +02:00
let deviceId: UInt8?
2022-04-13 14:55:22 +02:00
/**
Create new message content.
- Parameter time: The time of message creation,
- Parameter id: The counter of the message
*/
2023-08-07 15:47:40 +02:00
init(time: UInt32, id: UInt32, device: UInt8) {
2022-04-13 14:55:22 +02:00
self.time = time
self.id = id
2023-08-07 15:47:40 +02:00
self.deviceId = device
2022-04-13 14:55:22 +02:00
}
/**
Decode message content from data.
2022-04-28 23:19:44 +02:00
The data consists of two `UInt32` encoded in little endian format
2022-04-13 14:55:22 +02:00
- Warning: The sequence must contain at least 8 bytes, or the function will crash.
- Parameter data: The sequence containing the bytes.
*/
init<T: Sequence>(decodeFrom data: T) where T.Element == UInt8 {
2022-04-28 23:19:44 +02:00
self.time = UInt32(data: Data(data.prefix(MemoryLayout<UInt32>.size)))
2023-08-07 15:47:40 +02:00
self.id = UInt32(data: Data(data.dropLast().suffix(MemoryLayout<UInt32>.size)))
self.deviceId = data.suffix(1).last!
2022-04-13 14:55:22 +02:00
}
/// The byte length of an encoded message content
static var length: Int {
2023-08-07 15:47:40 +02:00
MemoryLayout<UInt32>.size * 2 + 1
2022-04-13 14:55:22 +02:00
}
/// The message content encoded to data
var encoded: Data {
2023-08-07 15:47:40 +02:00
time.encoded + id.encoded + Data([deviceId ?? 0])
2022-04-13 14:55:22 +02:00
}
}
}
2022-04-13 14:55:22 +02:00
extension Message.Content: Codable {
2023-08-07 15:47:40 +02:00
enum CodingKeys: Int, CodingKey {
case time = 1
case id = 2
2023-08-07 15:47:40 +02:00
case deviceId = 3
}
2022-04-28 23:19:44 +02:00
}
2022-04-13 14:55:22 +02:00
2022-04-28 23:19:44 +02:00
extension Message {
2022-04-13 14:55:22 +02:00
2022-04-28 23:19:44 +02:00
/// The length of a message in bytes
static var length: Int {
SHA256.byteCount + Content.length
2022-04-13 14:55:22 +02:00
}
/**
Decode a message from a byte buffer.
The buffer must contain at least `Message.length` bytes, or it will return `nil`.
- Parameter buffer: The buffer containing the bytes.
*/
init?(decodeFrom buffer: ByteBuffer) {
guard let data = buffer.getBytes(at: 0, length: Message.length) else {
return nil
}
self.init(decodeFrom: data)
}
init?(decodeFrom data: Data, index: inout Int) {
guard index + Message.length <= data.count else {
return nil
}
self.init(decodeFrom: data.advanced(by: index))
index += Message.length
}
2022-04-13 14:55:22 +02:00
/// The message encoded to data
var encoded: Data {
mac + content.encoded
}
var bytes: [UInt8] {
Array(encoded)
}
2022-04-28 23:19:44 +02:00
/**
Create a message from received bytes.
- Parameter data: The sequence of bytes
- Note: The sequence must contain at least `Message.length` bytes, or the function will crash.
*/
init<T: Sequence>(decodeFrom data: T) where T.Element == UInt8 {
let count = SHA256.byteCount
self.mac = Data(data.prefix(count))
self.content = .init(decodeFrom: Array(data.dropFirst(count)))
}
/**
Check if the message contains a valid authentication code
- Parameter key: The key used to sign the message.
- Returns: `true`, if the message is valid.
*/
func isValid(using key: SymmetricKey) -> Bool {
HMAC<SHA256>.isValidAuthenticationCode(mac, authenticating: content.encoded, using: key)
2022-04-13 14:55:22 +02:00
}
}
2022-04-28 23:19:44 +02:00
extension Message.Content {
/**
Calculate an authentication code for the message content.
- Parameter key: The key to use to sign the content.
- Returns: The new message signed with the key.
*/
func authenticate(using key: SymmetricKey) -> Message {
let mac = HMAC<SHA256>.authenticationCode(for: encoded, using: key)
return .init(mac: Data(mac.map { $0 }), content: self)
}
/**
Calculate an authentication code for the message content and convert everything to data.
- Parameter key: The key to use to sign the content.
- Returns: The new message signed with the key, serialized to bytes.
*/
func authenticateAndSerialize(using key: SymmetricKey) -> Data {
let encoded = self.encoded
let mac = HMAC<SHA256>.authenticationCode(for: encoded, using: key)
return Data(mac.map { $0 }) + encoded
}
2022-04-13 14:55:22 +02:00
}