StreamDecryptor.swift 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. accumulated += bytes
  28. let toProcess = accumulated.prefix(max(accumulated.count - worker.additionalBufferSize, 0))
  29. if var finalizingWorker = worker as? FinalizingDecryptModeWorker, isLast == true {
  30. // will truncate suffix if needed
  31. try finalizingWorker.willDecryptLast(bytes: accumulated.slice)
  32. }
  33. var processedBytesCount = 0
  34. var plaintext = Array<UInt8>(reserveCapacity: bytes.count + worker.additionalBufferSize)
  35. for chunk in toProcess.batched(by: blockSize) {
  36. plaintext += 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 padding != .noPadding {
  44. lastBlockRemainder = plaintext.count.quotientAndRemainder(dividingBy: 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 = padding.remove(from: plaintext, blockSize: blockSize - lastBlockRemainder)
  49. }
  50. 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. }