CipherBlockMode.swift 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. //
  2. // CipherBlockMode.swift
  3. // CryptoSwift
  4. //
  5. // Created by Marcin Krzyzanowski on 27/12/14.
  6. // Copyright (c) 2014 Marcin Krzyzanowski. All rights reserved.
  7. //
  8. import Foundation
  9. // I have no better name for that
  10. typealias CipherOperationOnBlock = (block: [UInt8]) -> [UInt8]?
  11. private protocol BlockMode {
  12. var needIV:Bool { get }
  13. func encryptBlocks(blocks:[[UInt8]], iv:[UInt8]?, cipherOperation:CipherOperationOnBlock) -> [UInt8]?
  14. func decryptBlocks(blocks:[[UInt8]], iv:[UInt8]?, cipherOperation:CipherOperationOnBlock) -> [UInt8]?
  15. }
  16. public enum CipherBlockMode {
  17. case ECB, CBC, CFB
  18. private var mode:BlockMode {
  19. switch (self) {
  20. case CBC:
  21. return CBCMode()
  22. case CFB:
  23. return CFBMode()
  24. case ECB:
  25. return ECBMode()
  26. }
  27. }
  28. var needIV: Bool {
  29. return mode.needIV
  30. }
  31. /**
  32. Process input blocks with given block cipher mode. With fallback to plain mode.
  33. :param: blocks cipher block size blocks
  34. :param: iv IV
  35. :param: cipher single block encryption closure
  36. :returns: encrypted bytes
  37. */
  38. func encryptBlocks(blocks:[[UInt8]], iv:[UInt8]?, cipherOperation:CipherOperationOnBlock) -> [UInt8]? {
  39. // if IV is not available, fallback to plain
  40. var finalBlockMode:CipherBlockMode = self
  41. if (iv == nil) {
  42. finalBlockMode = .ECB
  43. }
  44. return finalBlockMode.mode.encryptBlocks(blocks, iv: iv, cipherOperation: cipherOperation)
  45. }
  46. func decryptBlocks(blocks:[[UInt8]], iv:[UInt8]?, cipherOperation:(block:[UInt8]) -> [UInt8]?) -> [UInt8]? {
  47. // if IV is not available, fallback to plain
  48. var finalBlockMode:CipherBlockMode = self
  49. if (iv == nil) {
  50. finalBlockMode = .ECB
  51. }
  52. return finalBlockMode.mode.decryptBlocks(blocks, iv: iv, cipherOperation: cipherOperation)
  53. }
  54. }
  55. /**
  56. * Cipher-block chaining (CBC)
  57. */
  58. private struct CBCMode: BlockMode {
  59. var needIV:Bool = true
  60. func encryptBlocks(blocks:[[UInt8]], iv:[UInt8]?, cipherOperation:CipherOperationOnBlock) -> [UInt8]? {
  61. precondition(blocks.count > 0)
  62. assert(iv != nil, "CFB require IV")
  63. if (iv == nil) {
  64. return nil;
  65. }
  66. var out:[UInt8] = [UInt8]()
  67. out.reserveCapacity(blocks.count * blocks[0].count)
  68. var prevCiphertext = iv! // for the first time prevCiphertext = iv
  69. for plaintext in blocks {
  70. if let encrypted = cipherOperation(block: xor(prevCiphertext, plaintext)) {
  71. out.extend(encrypted)
  72. prevCiphertext = encrypted
  73. }
  74. }
  75. return out;
  76. }
  77. func decryptBlocks(blocks:[[UInt8]], iv:[UInt8]?, cipherOperation:CipherOperationOnBlock) -> [UInt8]? {
  78. precondition(blocks.count > 0)
  79. assert(iv != nil, "CFB require IV")
  80. if (iv == nil) {
  81. return nil
  82. }
  83. var out:[UInt8] = [UInt8]()
  84. out.reserveCapacity(blocks.count * blocks[0].count)
  85. var prevCiphertext = iv! // for the first time prevCiphertext = iv
  86. for ciphertext in blocks {
  87. if let decrypted = cipherOperation(block: ciphertext) { // decrypt
  88. out.extend(xor(prevCiphertext, decrypted))
  89. }
  90. prevCiphertext = ciphertext
  91. }
  92. return out
  93. }
  94. }
  95. /**
  96. * Cipher feedback (CFB)
  97. */
  98. private struct CFBMode: BlockMode {
  99. var needIV:Bool = true
  100. func encryptBlocks(blocks:[[UInt8]], iv:[UInt8]?, cipherOperation:CipherOperationOnBlock) -> [UInt8]? {
  101. assert(iv != nil, "CFB require IV")
  102. if (iv == nil) {
  103. return nil
  104. }
  105. var out:[UInt8] = [UInt8]()
  106. out.reserveCapacity(blocks.count * blocks[0].count)
  107. var lastCiphertext = iv!
  108. for plaintext in blocks {
  109. if let encrypted = cipherOperation(block: lastCiphertext) {
  110. lastCiphertext = xor(plaintext,encrypted)
  111. out.extend(lastCiphertext)
  112. }
  113. }
  114. return out;
  115. }
  116. func decryptBlocks(blocks:[[UInt8]], iv:[UInt8]?, cipherOperation:CipherOperationOnBlock) -> [UInt8]? {
  117. return encryptBlocks(blocks, iv: iv, cipherOperation: cipherOperation)
  118. }
  119. }
  120. /**
  121. * Electronic codebook (ECB)
  122. */
  123. private struct ECBMode: BlockMode {
  124. var needIV:Bool = false
  125. func encryptBlocks(blocks:[[UInt8]], iv:[UInt8]?, cipherOperation:CipherOperationOnBlock) -> [UInt8]? {
  126. var out:[UInt8] = [UInt8]()
  127. out.reserveCapacity(blocks.count * blocks[0].count)
  128. for plaintext in blocks {
  129. if let encrypted = cipherOperation(block: plaintext) {
  130. out.extend(encrypted)
  131. }
  132. }
  133. return out
  134. }
  135. func decryptBlocks(blocks:[[UInt8]], iv:[UInt8]?, cipherOperation:CipherOperationOnBlock) -> [UInt8]? {
  136. return encryptBlocks(blocks, iv: iv, cipherOperation: cipherOperation)
  137. }
  138. }