BlockDecryptor.swift 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // CryptoSwift
  2. //
  3. // Copyright (C) 2014-2018 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. private let blockSize: Int
  16. private let padding: Padding
  17. private var worker: CipherModeWorker
  18. private var accumulated = Array<UInt8>()
  19. init(blockSize: Int, padding: Padding, _ worker: CipherModeWorker) throws {
  20. self.blockSize = blockSize
  21. self.padding = padding
  22. self.worker = worker
  23. }
  24. public func update(withBytes bytes: ArraySlice<UInt8>, isLast: Bool = false) throws -> Array<UInt8> {
  25. self.accumulated += bytes
  26. // If a worker (eg GCM) can combine ciphertext + tag
  27. // we need to remove tag from the ciphertext.
  28. if !isLast && self.accumulated.count < self.blockSize + self.worker.additionalBufferSize {
  29. return []
  30. }
  31. let accumulatedWithoutSuffix: Array<UInt8>
  32. if self.worker.additionalBufferSize > 0 {
  33. // FIXME: how slow is that?
  34. accumulatedWithoutSuffix = Array(self.accumulated.prefix(self.accumulated.count - self.worker.additionalBufferSize))
  35. } else {
  36. accumulatedWithoutSuffix = self.accumulated
  37. }
  38. var processedBytesCount = 0
  39. var plaintext = Array<UInt8>(reserveCapacity: accumulatedWithoutSuffix.count)
  40. // Processing in a block-size manner. It's good for block modes, but bad for stream modes.
  41. for var chunk in accumulatedWithoutSuffix.batched(by: self.blockSize) {
  42. if isLast || (accumulatedWithoutSuffix.count - processedBytesCount) >= blockSize {
  43. let isLastChunk = processedBytesCount + chunk.count == accumulatedWithoutSuffix.count
  44. if isLast, isLastChunk, var finalizingWorker = worker as? FinalizingDecryptModeWorker {
  45. chunk = try finalizingWorker.willDecryptLast(bytes: chunk + accumulated.suffix(worker.additionalBufferSize)) // tag size
  46. }
  47. if !chunk.isEmpty {
  48. plaintext += worker.decrypt(block: chunk)
  49. }
  50. if isLast, isLastChunk, var finalizingWorker = worker as? FinalizingDecryptModeWorker {
  51. plaintext = Array(try finalizingWorker.didDecryptLast(bytes: plaintext.slice))
  52. }
  53. processedBytesCount += chunk.count
  54. }
  55. }
  56. accumulated.removeFirst(processedBytesCount) // super-slow
  57. if isLast {
  58. plaintext = self.padding.remove(from: plaintext, blockSize: self.blockSize)
  59. }
  60. return plaintext
  61. }
  62. public func seek(to position: Int) throws {
  63. guard var worker = self.worker as? SeekableModeWorker else {
  64. fatalError("Not supported")
  65. }
  66. try worker.seek(to: position)
  67. self.worker = worker
  68. accumulated = []
  69. }
  70. }