BlockDecryptor.swift 3.4 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. 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. accumulated += bytes
  26. // If a worker (eg GCM) can combine ciphertext + tag
  27. // we need to remove tag from the ciphertext.
  28. if !isLast && accumulated.count < blockSize + worker.additionalBufferSize {
  29. return []
  30. }
  31. let accumulatedWithoutSuffix: Array<UInt8>
  32. if worker.additionalBufferSize > 0 {
  33. // FIXME: how slow is that?
  34. accumulatedWithoutSuffix = Array(accumulated.prefix(accumulated.count - worker.additionalBufferSize))
  35. } else {
  36. accumulatedWithoutSuffix = 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: blockSize) {
  42. if isLast || (accumulatedWithoutSuffix.count - processedBytesCount) >= blockSize {
  43. if isLast, var finalizingWorker = worker as? FinalizingDecryptModeWorker {
  44. chunk = try finalizingWorker.willDecryptLast(bytes: chunk + accumulated.suffix(worker.additionalBufferSize)) // tag size
  45. }
  46. if !chunk.isEmpty {
  47. plaintext += worker.decrypt(block: chunk)
  48. }
  49. if var finalizingWorker = worker as? FinalizingDecryptModeWorker, isLast == true {
  50. plaintext = Array(try finalizingWorker.didDecryptLast(bytes: plaintext.slice))
  51. }
  52. processedBytesCount += chunk.count
  53. }
  54. }
  55. accumulated.removeFirst(processedBytesCount) // super-slow
  56. if isLast {
  57. plaintext = padding.remove(from: plaintext, blockSize: blockSize)
  58. }
  59. return plaintext
  60. }
  61. public func seek(to position: Int) throws {
  62. guard var worker = self.worker as? SeekableModeWorker else {
  63. fatalError("Not supported")
  64. }
  65. try worker.seek(to: position)
  66. self.worker = worker
  67. accumulated = []
  68. }
  69. }