add pairing tests
This commit is contained in:
parent
62c3a921fe
commit
05bf145328
|
@ -19,6 +19,12 @@ struct APDUResponse {
|
||||||
self.data = rawData.count > 2 ? Array(rawData[0..<(rawData.count - 3)]) : []
|
self.data = rawData.count > 2 ? Array(rawData[0..<(rawData.count - 3)]) : []
|
||||||
}
|
}
|
||||||
|
|
||||||
|
init(sw1: UInt8, sw2: UInt8, data: [UInt8]) {
|
||||||
|
self.sw1 = sw1
|
||||||
|
self.sw2 = sw2
|
||||||
|
self.data = data
|
||||||
|
}
|
||||||
|
|
||||||
func checkOK() throws -> APDUResponse {
|
func checkOK() throws -> APDUResponse {
|
||||||
try checkSW(StatusWord.ok)
|
try checkSW(StatusWord.ok)
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,4 +6,5 @@ enum CardError: Error {
|
||||||
case pinBlocked
|
case pinBlocked
|
||||||
case invalidAuthData
|
case invalidAuthData
|
||||||
case invalidMac
|
case invalidMac
|
||||||
|
case communicationError
|
||||||
}
|
}
|
||||||
|
|
|
@ -108,6 +108,30 @@ final class KeycardTests: XCTestCase {
|
||||||
let secureChannel = SecureChannel()
|
let secureChannel = SecureChannel()
|
||||||
secureChannel.generateSecret(pubKey: pub)
|
secureChannel.generateSecret(pubKey: pub)
|
||||||
XCTAssertEqual(secureChannel.secret!, Crypto.shared.secp256k1ECDH(privKey: priv, pubKey: secureChannel.publicKey!))
|
XCTAssertEqual(secureChannel.secret!, Crypto.shared.secp256k1ECDH(privKey: priv, pubKey: secureChannel.publicKey!))
|
||||||
|
|
||||||
|
var clientChallenge: [UInt8]? = nil
|
||||||
|
var pairing: [UInt8] = []
|
||||||
|
let sharedSecret = Crypto.shared.random(count: 32)
|
||||||
|
|
||||||
|
let testChannel = TestCardChannel()
|
||||||
|
testChannel.callback = { (cmd) in
|
||||||
|
if clientChallenge == nil {
|
||||||
|
XCTAssertEqual(cmd.ins, SecureChannelINS.pair.rawValue)
|
||||||
|
XCTAssertEqual(cmd.p1, PairP1.firstStep.rawValue)
|
||||||
|
let cryptogram = Crypto.shared.sha256(sharedSecret + cmd.data)
|
||||||
|
clientChallenge = Crypto.shared.random(count: 32)
|
||||||
|
return APDUResponse(sw1: 0x90, sw2: 0x00, data: cryptogram + clientChallenge!)
|
||||||
|
} else {
|
||||||
|
XCTAssertEqual(Crypto.shared.sha256(sharedSecret + clientChallenge!), cmd.data)
|
||||||
|
let salt = Crypto.shared.random(count: 32)
|
||||||
|
pairing = Crypto.shared.sha256(sharedSecret + salt)
|
||||||
|
return APDUResponse(sw1: 0x90, sw2: 0x00, data: [0x03] + salt)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
XCTAssertNoThrow(try secureChannel.autoPair(channel: testChannel, sharedSecret: sharedSecret))
|
||||||
|
XCTAssertEqual(secureChannel.pairing!.pairingIndex, 3)
|
||||||
|
XCTAssertEqual(secureChannel.pairing!.pairingKey, pairing)
|
||||||
}
|
}
|
||||||
|
|
||||||
static var allTests = [
|
static var allTests = [
|
||||||
|
|
|
@ -0,0 +1,14 @@
|
||||||
|
@testable import Keycard
|
||||||
|
|
||||||
|
class TestCardChannel: CardChannel {
|
||||||
|
var callback: ((APDUCommand) throws -> APDUResponse)?
|
||||||
|
var connected: Bool { get { true } }
|
||||||
|
|
||||||
|
func send(_ cmd: APDUCommand) throws -> APDUResponse {
|
||||||
|
if callback != nil {
|
||||||
|
return try callback!(cmd)
|
||||||
|
} else {
|
||||||
|
throw CardError.communicationError
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue