-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
IOS-8008 Sign hashes with different sizes (#382)
- Loading branch information
Showing
7 changed files
with
376 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// | ||
// ChunkHashesUtil.swift | ||
// TangemSdk | ||
// | ||
// Created by Alexander Osokin on 31.10.2024. | ||
// Copyright © 2024 Tangem AG. All rights reserved. | ||
// | ||
|
||
struct ChunkHashesUtil { | ||
func chunkHashes(_ hashes: [Data]) -> [Chunk] { | ||
let hashes = hashes.enumerated().map { Hash(index: $0.offset, data: $0.element) } | ||
let hashesBySize = Dictionary(grouping: hashes, by: { $0.data.count }) | ||
|
||
let chunks = hashesBySize.flatMap { hashesGroup in | ||
let hashSize = hashesGroup.key | ||
let chunkSize = getChunkSize(for: hashSize) | ||
|
||
let chunkedHashes = hashesGroup.value.chunked(into: chunkSize) | ||
let chunks = chunkedHashes.map { Chunk(hashSize: hashSize, hashes: $0) } | ||
|
||
return chunks | ||
} | ||
|
||
return chunks | ||
} | ||
|
||
func getChunkSize(for hashSize: Int) -> Int { | ||
/// These devices are not able to sign long hashes. | ||
if NFCUtils.isPoorNfcQualityDevice { | ||
return Constants.maxChunkSizePoorNfcQualityDevice | ||
} | ||
|
||
guard hashSize > 0 else { | ||
return Constants.maxChunkSize | ||
} | ||
|
||
let estimatedChunkSize = Constants.packageSize / hashSize | ||
let chunkSize = max(1, min(estimatedChunkSize, Constants.maxChunkSize)) | ||
return chunkSize | ||
} | ||
} | ||
|
||
// MARK: - Constants | ||
|
||
private extension ChunkHashesUtil { | ||
enum Constants { | ||
/// The max answer is 1152 bytes (unencrypted) and 1120 (encrypted). The worst case is 8 hashes * 64 bytes for ed + 512 bytes of signatures + cardId, SignedHashes + TLV + SW is ok. | ||
static let packageSize = 512 | ||
|
||
/// Card limitation | ||
static let maxChunkSize = 10 | ||
|
||
/// Empirical value | ||
static let maxChunkSizePoorNfcQualityDevice = 2 | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
TangemSdk/TangemSdk/Operations/Sign/ChunkedHashesContainer.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// | ||
// ChunkedHashesContainer.swift | ||
// TangemSdk | ||
// | ||
// Created by Alexander Osokin on 31.10.2024. | ||
// Copyright © 2024 Tangem AG. All rights reserved. | ||
// | ||
|
||
|
||
import Foundation | ||
|
||
struct ChunkedHashesContainer { | ||
var isEmpty: Bool { chunks.isEmpty } | ||
let chunksCount: Int | ||
|
||
private(set) var currentChunkIndex: Int = 0 | ||
|
||
private let chunks: [Chunk] | ||
private var signedChunks: [SignedChunk] = [] | ||
|
||
init(hashes: [Data]) { | ||
self.chunks = ChunkHashesUtil().chunkHashes(hashes) | ||
self.chunksCount = chunks.count | ||
} | ||
|
||
func getCurrentChunk() throws -> Chunk { | ||
guard currentChunkIndex < chunks.count else { | ||
throw ChunkedHashesContainerError.processingError | ||
} | ||
|
||
return chunks[currentChunkIndex] | ||
} | ||
|
||
mutating func addSignedChunk(_ signedChunk: SignedChunk) { | ||
signedChunks.append(signedChunk) | ||
currentChunkIndex += 1 | ||
} | ||
|
||
func getSignatures() -> [Data] { | ||
let signedHashes = signedChunks.flatMap { $0.signedHashes }.sorted() | ||
let signatures = signedHashes.map { $0.signature } | ||
return signatures | ||
} | ||
} | ||
|
||
// MARK: - ChunkedHashesContainerError | ||
|
||
enum ChunkedHashesContainerError: Error { | ||
case processingError | ||
} | ||
|
Oops, something went wrong.