AES.Cryptors.swift 5.2 KB

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