p2p-swift / Sources / IrdP2PCrypto / HKDF.swift
91 words
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
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))
    }
}

static mirror of HEAD · about · clone: git clone https://ird.cod-sursa.ro/p2p-swift.git