StreamDecryptor.swift 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. final class StreamDecryptor: Cryptor, Updatable {
  15. private let blockSize: Int
  16. private var worker: CipherModeWorker
  17. private let padding: Padding
  18. private var accumulated = Array<UInt8>()
  19. private var lastBlockRemainder = 0
  20. init(blockSize: Int, padding: Padding, _ worker: CipherModeWorker) throws {
  21. self.blockSize = blockSize
  22. self.padding = padding
  23. self.worker = worker
  24. }
  25. // MARK: Updatable
  26. public func update(withBytes bytes: ArraySlice<UInt8>, isLast: Bool) throws -> Array<UInt8> {
  27. self.accumulated += bytes
  28. let toProcess = self.accumulated.prefix(max(self.accumulated.count - self.worker.additionalBufferSize, 0))
  29. if var finalizingWorker = worker as? FinalizingDecryptModeWorker, isLast == true {
  30. // will truncate suffix if needed
  31. try finalizingWorker.willDecryptLast(bytes: self.accumulated.slice)
  32. }
  33. var processedBytesCount = 0
  34. var plaintext = Array<UInt8>(reserveCapacity: bytes.count + self.worker.additionalBufferSize)
  35. for chunk in toProcess.batched(by: self.blockSize) {
  36. plaintext += self.worker.decrypt(block: chunk)
  37. processedBytesCount += chunk.count
  38. }
  39. if var finalizingWorker = worker as? FinalizingDecryptModeWorker, isLast == true {
  40. plaintext = Array(try finalizingWorker.didDecryptLast(bytes: plaintext.slice))
  41. }
  42. // omit unecessary calculation if not needed
  43. if self.padding != .noPadding {
  44. self.lastBlockRemainder = plaintext.count.quotientAndRemainder(dividingBy: self.blockSize).remainder
  45. }
  46. if isLast {
  47. // CTR doesn't need padding. Really. Add padding to the last block if really want. but... don't.
  48. plaintext = self.padding.remove(from: plaintext, blockSize: self.blockSize - self.lastBlockRemainder)
  49. }
  50. self.accumulated.removeFirst(processedBytesCount) // super-slow
  51. if var finalizingWorker = worker as? FinalizingDecryptModeWorker, isLast == true {
  52. plaintext = Array(try finalizingWorker.finalize(decrypt: plaintext.slice))
  53. }
  54. return plaintext
  55. }
  56. public func seek(to position: Int) throws {
  57. guard var worker = self.worker as? SeekableModeWorker else {
  58. fatalError("Not supported")
  59. }
  60. try worker.seek(to: position)
  61. self.worker = worker
  62. }
  63. }