CFB.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. // Cipher feedback (CFB)
  16. //
  17. public struct CFB: BlockMode {
  18. public enum Error: Swift.Error {
  19. /// Invalid IV
  20. case invalidInitializationVector
  21. }
  22. public enum SegmentSize: Int {
  23. case cfb8 = 1 // Encrypt byte per byte
  24. case cfb128 = 16 // Encrypt 16 bytes per 16 bytes (default)
  25. }
  26. public let options: BlockModeOption = [.initializationVectorRequired, .useEncryptToDecrypt]
  27. private let iv: Array<UInt8>
  28. private let segmentSize: SegmentSize
  29. public let customBlockSize: Int?
  30. public init(iv: Array<UInt8>, segmentSize: SegmentSize = .cfb128) {
  31. self.iv = iv
  32. self.segmentSize = segmentSize
  33. self.customBlockSize = segmentSize.rawValue
  34. }
  35. public func worker(blockSize: Int, cipherOperation: @escaping CipherOperationOnBlock, encryptionOperation: @escaping CipherOperationOnBlock) throws -> CipherModeWorker {
  36. if !(self.iv.count == blockSize || (segmentSize == .cfb8 && self.iv.count == AES.blockSize)) {
  37. throw Error.invalidInitializationVector
  38. }
  39. return CFBModeWorker(blockSize: blockSize, iv: self.iv.slice, segmentSize: segmentSize, cipherOperation: cipherOperation)
  40. }
  41. }
  42. struct CFBModeWorker: BlockModeWorker {
  43. let cipherOperation: CipherOperationOnBlock
  44. let blockSize: Int
  45. let additionalBufferSize: Int = 0
  46. private let iv: ArraySlice<UInt8>
  47. private let segmentSize: CFB.SegmentSize
  48. private var prev: ArraySlice<UInt8>?
  49. init(blockSize: Int, iv: ArraySlice<UInt8>, segmentSize: CFB.SegmentSize, cipherOperation: @escaping CipherOperationOnBlock) {
  50. self.blockSize = blockSize
  51. self.iv = iv
  52. self.segmentSize = segmentSize
  53. self.cipherOperation = cipherOperation
  54. }
  55. @inlinable
  56. mutating func encrypt(block plaintext: ArraySlice<UInt8>) -> Array<UInt8> {
  57. switch segmentSize {
  58. case .cfb128:
  59. guard let ciphertext = cipherOperation(prev ?? iv) else {
  60. return Array(plaintext)
  61. }
  62. self.prev = xor(plaintext, ciphertext.slice)
  63. return Array(self.prev ?? [])
  64. case .cfb8:
  65. guard let ciphertext = cipherOperation(prev ?? iv) else {
  66. return Array(plaintext)
  67. }
  68. let result = [Array(plaintext)[0] ^ Array(ciphertext)[0]]
  69. self.prev = Array((prev ?? iv).dropFirst()) + [result[0]]
  70. return result
  71. }
  72. }
  73. @inlinable
  74. mutating func decrypt(block ciphertext: ArraySlice<UInt8>) -> Array<UInt8> {
  75. switch segmentSize {
  76. case .cfb128:
  77. guard let plaintext = cipherOperation(prev ?? iv) else {
  78. return Array(ciphertext)
  79. }
  80. let result: Array<UInt8> = xor(plaintext, ciphertext)
  81. prev = ciphertext
  82. return result
  83. case .cfb8:
  84. guard let plaintext = cipherOperation(prev ?? iv) else {
  85. return Array(ciphertext)
  86. }
  87. self.prev = Array((prev ?? iv).dropFirst()) + [Array(ciphertext)[0]]
  88. return [Array(ciphertext)[0] ^ Array(plaintext)[0]]
  89. }
  90. }
  91. }