import Foundation
import CryptoKit
/// HKDF-SHA256 as the protocol uses it: `ikm` is the room secret (or an identity
/// seed), `salt` is the UTF-8 app salt, `info` is a short ASCII label.
enum KDF {
static func deriveBytes(ikm: Data, salt: Data, info: String, count: Int = 32) -> Data {
let key = HKDF<SHA256>.deriveKey(
inputKeyMaterial: SymmetricKey(data: ikm),
salt: salt,
info: Data(info.utf8),
outputByteCount: count
)
return key.withUnsafeBytes { Data($0) }
}
static func deriveKey(ikm: Data, salt: Data, info: String) -> SymmetricKey {
SymmetricKey(data: deriveBytes(ikm: ikm, salt: salt, info: info))
}
}