BlockDecryptor.swift 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // CryptoSwift
  2. //
  3. // Copyright (C) 2014-2025 Marcin Krzyżanowski <marcin@krzyzanowskim.com>
  4. // This software is provided 'as-is', without any express or implied warranty.
  5. //
  6. // In no event will the authors be held liable for any damages arising from the use of this software.
  7. //
  8. // Permission is granted to anyone to use this software for any purpose,including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:
  9. //
  10. // - The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation is required.
  11. // - Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  12. // - This notice may not be removed or altered from any source or binary distribution.
  13. //
  14. public class BlockDecryptor: Cryptor, Updatable {
  15. public enum Error: Swift.Error {
  16. case unsupported
  17. }
  18. @usableFromInline
  19. let blockSize: Int
  20. @usableFromInline
  21. let padding: Padding
  22. @usableFromInline
  23. var worker: CipherModeWorker
  24. @usableFromInline
  25. var accumulated = Array<UInt8>()
  26. @usableFromInline
  27. init(blockSize: Int, padding: Padding, _ worker: CipherModeWorker) throws {
  28. self.blockSize = blockSize
  29. self.padding = padding
  30. self.worker = worker
  31. }
  32. @inlinable
  33. public func update(withBytes bytes: ArraySlice<UInt8>, isLast: Bool = false) throws -> Array<UInt8> {
  34. self.accumulated += bytes
  35. // If a worker (eg GCM) can combine ciphertext + tag
  36. // we need to remove tag from the ciphertext.
  37. if !isLast && self.accumulated.count < self.blockSize + self.worker.additionalBufferSize {
  38. return []
  39. }
  40. let accumulatedWithoutSuffix: Array<UInt8>
  41. if self.worker.additionalBufferSize > 0 {
  42. // FIXME: how slow is that?
  43. accumulatedWithoutSuffix = Array(self.accumulated.prefix(self.accumulated.count - self.worker.additionalBufferSize))
  44. } else {
  45. accumulatedWithoutSuffix = self.accumulated
  46. }
  47. var processedBytesCount = 0
  48. var plaintext = Array<UInt8>(reserveCapacity: accumulatedWithoutSuffix.count)
  49. // Processing in a block-size manner. It's good for block modes, but bad for stream modes.
  50. for var chunk in accumulatedWithoutSuffix.batched(by: self.blockSize) {
  51. if isLast || (accumulatedWithoutSuffix.count - processedBytesCount) >= blockSize {
  52. let isLastChunk = processedBytesCount + chunk.count == accumulatedWithoutSuffix.count
  53. if isLast, isLastChunk, var finalizingWorker = worker as? FinalizingDecryptModeWorker {
  54. chunk = try finalizingWorker.willDecryptLast(bytes: chunk + accumulated.suffix(worker.additionalBufferSize)) // tag size
  55. }
  56. if !chunk.isEmpty {
  57. plaintext += worker.decrypt(block: chunk)
  58. }
  59. if isLast, isLastChunk, var finalizingWorker = worker as? FinalizingDecryptModeWorker {
  60. plaintext = Array(try finalizingWorker.didDecryptLast(bytes: plaintext.slice))
  61. }
  62. processedBytesCount += chunk.count
  63. }
  64. }
  65. accumulated.removeFirst(processedBytesCount) // super-slow
  66. if isLast {
  67. if accumulatedWithoutSuffix.isEmpty, var finalizingWorker = worker as? FinalizingDecryptModeWorker {
  68. try finalizingWorker.willDecryptLast(bytes: self.accumulated.suffix(self.worker.additionalBufferSize))
  69. plaintext = Array(try finalizingWorker.didDecryptLast(bytes: plaintext.slice))
  70. }
  71. plaintext = self.padding.remove(from: plaintext, blockSize: self.blockSize)
  72. }
  73. return plaintext
  74. }
  75. public func seek(to position: Int) throws {
  76. guard var worker = self.worker as? SeekableModeWorker else {
  77. throw Error.unsupported
  78. }
  79. try worker.seek(to: position)
  80. self.worker = worker
  81. accumulated = []
  82. }
  83. }