StreamDecryptor.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. // TODO: accumulate `worker.additionalBufferSize`
  28. // and pass it to willDecrypt(), most likely it will contains MAC
  29. accumulated += bytes
  30. // If a worker (eg CCM) can combine ciphertext + tag
  31. // we need to remove tag from the ciphertext.
  32. if !isLast && accumulated.count < worker.additionalBufferSize {
  33. return []
  34. }
  35. if var finalizingWorker = worker as? FinalizingDecryptModeWorker, isLast == true {
  36. // will truncate suffix if needed
  37. accumulated = Array(try finalizingWorker.willDecryptLast(bytes: accumulated.slice))
  38. }
  39. var processedBytesCount = 0
  40. var plaintext = Array<UInt8>(reserveCapacity: bytes.count + worker.additionalBufferSize)
  41. for chunk in accumulated.batched(by: blockSize) {
  42. plaintext += worker.decrypt(block: chunk)
  43. processedBytesCount += chunk.count
  44. }
  45. if var finalizingWorker = worker as? FinalizingDecryptModeWorker, isLast == true {
  46. plaintext = Array(try finalizingWorker.didDecryptLast(bytes: plaintext.slice))
  47. }
  48. // omit unecessary calculation if not needed
  49. if padding != .noPadding {
  50. lastBlockRemainder = plaintext.count.quotientAndRemainder(dividingBy: blockSize).remainder
  51. }
  52. if isLast {
  53. // CTR doesn't need padding. Really. Add padding to the last block if really want. but... don't.
  54. plaintext = padding.remove(from: plaintext, blockSize: blockSize - lastBlockRemainder)
  55. }
  56. accumulated.removeFirst(processedBytesCount) // super-slow
  57. if var finalizingWorker = worker as? FinalizingDecryptModeWorker, isLast == true {
  58. plaintext = Array(try finalizingWorker.finalize(decrypt: plaintext.slice))
  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. }
  69. }