Skip to content

Commit 66d58bc

Browse files
committed
Code review fixes
1 parent 55e2421 commit 66d58bc

9 files changed

Lines changed: 29 additions & 34 deletions

File tree

‎Sources/nfclib/CardActions/CardCommands.swift‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public protocol CardCommands: Sendable {
125125
* - Throws: An error if the operation fails.
126126
* - Returns: The authentication response as `Data`.
127127
*/
128-
func authenticate(for hash: Data, withPin1 pin1: SecureData) async throws -> Data
128+
func authenticate(for hash: any DataType, withPin1 pin1: SecureData) async throws -> Data
129129

130130
/**
131131
* Calculates a digital signature for the given hash.
@@ -136,7 +136,7 @@ public protocol CardCommands: Sendable {
136136
* - Throws: An error if the operation fails.
137137
* - Returns: The signature as `Data`.
138138
*/
139-
func calculateSignature(for hash: Data, withPin2 pin2: SecureData) async throws -> Data
139+
func calculateSignature(for hash: any DataType, withPin2 pin2: SecureData) async throws -> Data
140140

141141
/**
142142
* Decrypts data using PIN 1.
@@ -147,5 +147,5 @@ public protocol CardCommands: Sendable {
147147
* - Throws: An error if the operation fails.
148148
* - Returns: The decrypted data.
149149
*/
150-
func decryptData(_ hash: Data, withPin1 pin1: SecureData) async throws -> Data
150+
func decryptData(_ hash: any DataType, withPin1 pin1: SecureData) async throws -> Data
151151
}

‎Sources/nfclib/CardActions/CardReader.swift‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
import Foundation
55

6-
typealias Bytes = [UInt8]
7-
86
protocol CardReader: Sendable {
97
/**
108
* Sends an APDU (Application Protocol Data Unit) command to the smart card and retrieves the response.

‎Sources/nfclib/CardActions/CardReaderNFC.swift‎

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
import OSLog
55
import CoreNFC
6-
import CommonCrypto
76
import CryptoTokenKit
87
internal import SwiftECC
98
internal import BigInt
@@ -308,8 +307,8 @@ class CardReaderNFC: CardReader {
308307
// MARK: - Utils
309308
// swiftlint:disable force_unwrapping
310309
static private func pseudoRandomNumberMappingAES(
311-
sVal: any AES.DataType,
312-
tVal: any AES.DataType,
310+
sVal: any DataType,
311+
tVal: any DataType,
313312
domain: Domain
314313
) throws -> BInt {
315314
let lVal = sVal.count * 8
@@ -383,7 +382,7 @@ class CardReaderNFC: CardReader {
383382
Point(x3Val, (AVal * UVal).mod(pVal))
384383
}
385384

386-
static private func decryptNonce<T: AES.DataType>(CAN: String, encryptedNonce: T) throws -> Bytes {
385+
static private func decryptNonce<T: DataType>(CAN: String, encryptedNonce: T) throws -> Bytes {
387386
let decryptionKey = KDF(key: Bytes(CAN.utf8), counter: 3)
388387
let cipher = AES.CBC(key: decryptionKey)
389388
return try cipher.decrypt(encryptedNonce)
@@ -392,14 +391,7 @@ class CardReaderNFC: CardReader {
392391
static private func KDF(key: Bytes, counter: UInt8) -> Bytes {
393392
var keydata = key + Bytes(repeating: 0x00, count: 4)
394393
keydata[keydata.count - 1] = counter
395-
return SHA256(data: keydata)
396-
}
397-
398-
static private func SHA256(data: Bytes) -> Bytes {
399-
Bytes(unsafeUninitializedCapacity: Int(CC_SHA256_DIGEST_LENGTH)) { buffer, initializedCount in
400-
CC_SHA256(data, CC_LONG(data.count), buffer.baseAddress)
401-
initializedCount = Int(CC_SHA256_DIGEST_LENGTH)
402-
}
394+
return sha(hashLength: .bits256, data: keydata)
403395
}
404396

405397
static private func random(count: Int) throws -> Data {

‎Sources/nfclib/CardActions/Idemia.swift‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,25 +132,25 @@ final class Idemia: CardCommandsInternal {
132132

133133
// MARK: - Authentication & Signing
134134

135-
func authenticate(for hash: Data, withPin1 pin1: SecureData) async throws -> Data {
135+
func authenticate(for hash: any DataType, withPin1 pin1: SecureData) async throws -> Data {
136136
_ = try await select(file: kAIDOberthur)
137137
try await verifyCode(.pin1, code: pin1)
138138
try await setSecEnv(mode: 0xA4, algo: [0xFF, 0x20, 0x08, 0x00], keyRef: AUTHKEY)
139139
var paddedHash = Data(repeating: 0x00, count: max(48, hash.count) - hash.count)
140-
paddedHash.append(hash)
140+
paddedHash.append(contentsOf: hash)
141141
return try await reader.sendAPDU(ins: 0x88, data: paddedHash, leByte: 0x00)
142142
}
143143

144-
func calculateSignature(for hash: Data, withPin2 pin2: SecureData) async throws -> Data {
144+
func calculateSignature(for hash: any DataType, withPin2 pin2: SecureData) async throws -> Data {
145145
_ = try await select(file: kAIDQSCD)
146146
try await verifyCode(.pin2, code: pin2)
147147
try await setSecEnv(mode: 0xB6, algo: [0xFF, 0x15, 0x08, 0x00], keyRef: SIGNKEY)
148148
var paddedHash = Data(repeating: 0x00, count: max(48, hash.count) - hash.count)
149-
paddedHash.append(hash)
149+
paddedHash.append(contentsOf: hash)
150150
return try await reader.sendAPDU(ins: 0x2A, p1Byte: 0x9E, p2Byte: 0x9A, data: paddedHash, leByte: 0x00)
151151
}
152152

153-
func decryptData(_ hash: Data, withPin1 pin1: SecureData) async throws -> Data {
153+
func decryptData(_ hash: any DataType, withPin1 pin1: SecureData) async throws -> Data {
154154
_ = try await select(file: kAIDOberthur)
155155
try await verifyCode(.pin1, code: pin1)
156156
try await setSecEnv(mode: 0xB8, algo: [0xFF, 0x30, 0x04, 0x00], keyRef: AUTHKEY)

‎Sources/nfclib/CardActions/Thales.swift‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ final class Thales: CardCommandsInternal {
129129

130130
// MARK: - Authentication & Signing
131131

132-
private func sign(type: CodeType, pin: SecureData, keyRef: UInt8, hash: Data) async throws -> Data {
132+
private func sign(type: CodeType, pin: SecureData, keyRef: UInt8, hash: any DataType) async throws -> Data {
133133
try await verifyCode(type, code: pin)
134134
try await setSecEnv(mode: 0xB6, algo: [0x24 + UInt8(hash.count)], keyRef: keyRef)
135135
_ = try await reader
@@ -147,20 +147,20 @@ final class Thales: CardCommandsInternal {
147147
return try await reader.sendAPDU(ins: 0x2A, p1Byte: 0x9E, p2Byte: 0x9A, leByte: 0x00)
148148
}
149149

150-
func authenticate(for hash: Data, withPin1 pin1: SecureData) async throws -> Data {
150+
func authenticate(for hash: any DataType, withPin1 pin1: SecureData) async throws -> Data {
151151
_ = try await select(file: Thales.kAID)
152152
return try await sign(type: .pin1, pin: pin1, keyRef: Thales.AUTHKEY, hash: hash)
153153
}
154154

155-
func calculateSignature(for hash: Data, withPin2 pin2: SecureData) async throws -> Data {
155+
func calculateSignature(for hash: any DataType, withPin2 pin2: SecureData) async throws -> Data {
156156
let (_, pin2Changed) = try await readCodeTryCounterRecord(.pin2)
157157
guard pin2Changed else { throw IdCardInternalError.notActivated }
158158

159159
_ = try await select(file: Thales.kAID)
160160
return try await sign(type: .pin2, pin: pin2, keyRef: Thales.SIGNKEY, hash: hash)
161161
}
162162

163-
func decryptData(_ hash: Data, withPin1 pin1: SecureData) async throws -> Data {
163+
func decryptData(_ hash: any DataType, withPin1 pin1: SecureData) async throws -> Data {
164164
_ = try await select(file: Thales.kAID)
165165
try await verifyCode(.pin1, code: pin1)
166166
try await setSecEnv(mode: 0xB8, keyRef: Thales.AUTHKEY)

‎Sources/nfclib/Utilities/AES.swift‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import CommonCrypto
55
import Foundation
66

77
class AES {
8-
typealias DataType = DataProtocol & ContiguousBytes
98
static let BlockSize: Int = kCCBlockSizeAES128
109
static let Zero = Bytes(repeating: 0x00, count: BlockSize)
1110

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ enum HashLength {
1010
case bits512
1111
}
1212

13-
func sha(hashLength: HashLength, data: Data) -> Data {
13+
func sha(hashLength: HashLength, data: some DataType) -> Bytes {
1414
let digestLength: Int
1515
let hashFunction: (UnsafeRawPointer?, CC_LONG, UnsafeMutablePointer<UInt8>?) -> UnsafeMutablePointer<UInt8>?
1616

@@ -26,12 +26,10 @@ func sha(hashLength: HashLength, data: Data) -> Data {
2626
hashFunction = CC_SHA512
2727
}
2828

29-
let hashBytes = Bytes(unsafeUninitializedCapacity: digestLength) { buffer, initializedCount in
29+
return Bytes(unsafeUninitializedCapacity: digestLength) { buffer, initializedCount in
3030
data.withUnsafeBytes { dataBytes in
31-
_ = hashFunction(dataBytes.baseAddress, CC_LONG(data.count), buffer.baseAddress)
31+
_ = hashFunction(dataBytes.baseAddress, CC_LONG(dataBytes.count), buffer.baseAddress)
3232
}
3333
initializedCount = digestLength
3434
}
35-
36-
return Data(hashBytes)
3735
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// SPDX-FileCopyrightText: Estonian Information System Authority
2+
// SPDX-License-Identifier: LGPL-2.1-or-later
3+
4+
import Foundation
5+
6+
typealias Bytes = [UInt8]
7+
8+
public typealias DataType = DataProtocol & ContiguousBytes

‎Sources/nfclib/WebEID/OperationAuthenticateWithWebEID.swift‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ extension OperationAuthenticateWithWebEID: @MainActor NFCTagReaderSessionDelegat
115115
let algorithm = try WebEidSignatureAlgorithm.forAuthenticationKey(publicKey)
116116
let hashLength = algorithm.hashLength
117117

118-
let originHash = sha(hashLength: hashLength, data: Data(origin.utf8))
119-
let challengeHash = sha(hashLength: hashLength, data: Data(challenge.utf8))
118+
let originHash = sha(hashLength: hashLength, data: Bytes(origin.utf8))
119+
let challengeHash = sha(hashLength: hashLength, data: Bytes(challenge.utf8))
120120
let webEidHash = sha(hashLength: hashLength, data: originHash + challengeHash)
121121

122122
updateAlertMessage(step: 4)

0 commit comments

Comments
 (0)