AES.Cryptors.swift 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. //
  2. // CryptoSwift
  3. //
  4. // Copyright (C) 2014-2017 Marcin Krzyżanowski <marcin@krzyzanowskim.com>
  5. // This software is provided 'as-is', without any express or implied warranty.
  6. //
  7. // In no event will the authors be held liable for any damages arising from the use of this software.
  8. //
  9. // 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:
  10. //
  11. // - 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.
  12. // - Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  13. // - This notice may not be removed or altered from any source or binary distribution.
  14. //
  15. // MARK: Cryptors
  16. extension AES: Cryptors {
  17. public func makeEncryptor() throws -> AES.Encryptor {
  18. return try AES.Encryptor(aes: self)
  19. }
  20. public func makeDecryptor() throws -> AES.Decryptor {
  21. return try AES.Decryptor(aes: self)
  22. }
  23. }
  24. // MARK: Encryptor
  25. extension AES {
  26. public struct Encryptor: Updatable {
  27. private var worker: BlockModeWorker
  28. private let padding: Padding
  29. // Accumulated bytes. Not all processed bytes.
  30. private var accumulated = Array<UInt8>()
  31. private var processedBytesTotalCount: Int = 0
  32. init(aes: AES) throws {
  33. padding = aes.padding
  34. worker = try aes.blockMode.worker(blockSize: AES.blockSize, cipherOperation: aes.encrypt)
  35. }
  36. public mutating func update(withBytes bytes: ArraySlice<UInt8>, isLast: Bool = false) throws -> Array<UInt8> {
  37. accumulated += bytes
  38. if isLast {
  39. accumulated = padding.add(to: accumulated, blockSize: AES.blockSize)
  40. }
  41. var processedBytes = 0
  42. var encrypted = Array<UInt8>(reserveCapacity: accumulated.count)
  43. for chunk in accumulated.batched(by: AES.blockSize) {
  44. if isLast || (accumulated.count - processedBytes) >= AES.blockSize {
  45. encrypted += worker.encrypt(chunk)
  46. processedBytes += chunk.count
  47. }
  48. }
  49. accumulated.removeFirst(processedBytes)
  50. processedBytesTotalCount += processedBytes
  51. if var finalizingWorker = worker as? BlockModeWorkerFinalizing, isLast == true {
  52. encrypted = try finalizingWorker.finalize(encrypt: encrypted.slice)
  53. }
  54. return encrypted
  55. }
  56. }
  57. }
  58. // MARK: Decryptor
  59. extension AES {
  60. public struct Decryptor: RandomAccessCryptor {
  61. private var worker: BlockModeWorker
  62. private let padding: Padding
  63. private var accumulated = Array<UInt8>()
  64. private var processedBytesTotalCount: Int = 0
  65. private var offset: Int = 0
  66. private var offsetToRemove: Int = 0
  67. init(aes: AES) throws {
  68. padding = aes.padding
  69. if aes.blockMode.options.contains(.useEncryptToDecrypt) {
  70. worker = try aes.blockMode.worker(blockSize: AES.blockSize, cipherOperation: aes.encrypt)
  71. } else {
  72. worker = try aes.blockMode.worker(blockSize: AES.blockSize, cipherOperation: aes.decrypt)
  73. }
  74. }
  75. public mutating func update(withBytes bytes: ArraySlice<UInt8>, isLast: Bool = false) throws -> Array<UInt8> {
  76. // prepend "offset" number of bytes at the beginning
  77. if offset > 0 {
  78. accumulated += Array<UInt8>(repeating: 0, count: offset) + bytes
  79. offsetToRemove = offset
  80. offset = 0
  81. } else {
  82. accumulated += bytes
  83. }
  84. var processedBytes = 0
  85. var plaintext = Array<UInt8>(reserveCapacity: accumulated.count)
  86. for chunk in accumulated.batched(by: AES.blockSize) {
  87. if isLast || (accumulated.count - processedBytes) >= AES.blockSize {
  88. plaintext += worker.decrypt(chunk)
  89. // remove "offset" from the beginning of first chunk
  90. if offsetToRemove > 0 {
  91. plaintext.removeFirst(offsetToRemove)
  92. offsetToRemove = 0
  93. }
  94. processedBytes += chunk.count
  95. }
  96. }
  97. accumulated.removeFirst(processedBytes)
  98. processedBytesTotalCount += processedBytes
  99. if isLast {
  100. plaintext = padding.remove(from: plaintext, blockSize: AES.blockSize)
  101. }
  102. if var finalizingWorker = worker as? BlockModeWorkerFinalizing, isLast == true {
  103. plaintext = try finalizingWorker.finalize(decrypt: plaintext.slice)
  104. }
  105. return plaintext
  106. }
  107. @discardableResult public mutating func seek(to position: Int) -> Bool {
  108. guard var worker = self.worker as? RandomAccessBlockModeWorker else {
  109. return false
  110. }
  111. worker.counter = UInt(position / AES.blockSize)
  112. self.worker = worker
  113. offset = position % AES.blockSize
  114. accumulated = []
  115. return true
  116. }
  117. }
  118. }