Rabbit.swift 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. public final class Rabbit: BlockCipher {
  16. public enum Error: Swift.Error {
  17. case invalidKeyOrInitializationVector
  18. }
  19. /// Size of IV in bytes
  20. public static let ivSize = 64 / 8
  21. /// Size of key in bytes
  22. public static let keySize = 128 / 8
  23. /// Size of block in bytes
  24. public static let blockSize = 128 / 8
  25. public var keySize: Int {
  26. return key.count
  27. }
  28. /// Key
  29. private let key: Key
  30. /// IV (optional)
  31. private let iv: Array<UInt8>?
  32. /// State variables
  33. private var x = Array<UInt32>(repeating: 0, count: 8)
  34. /// Counter variables
  35. private var c = Array<UInt32>(repeating: 0, count: 8)
  36. /// Counter carry
  37. private var p7: UInt32 = 0
  38. /// 'a' constants
  39. private var a: Array<UInt32> = [
  40. 0x4d34d34d,
  41. 0xd34d34d3,
  42. 0x34d34d34,
  43. 0x4d34d34d,
  44. 0xd34d34d3,
  45. 0x34d34d34,
  46. 0x4d34d34d,
  47. 0xd34d34d3,
  48. ]
  49. // MARK: - Initializers
  50. public convenience init(key: Array<UInt8>) throws {
  51. try self.init(key: key, iv: nil)
  52. }
  53. public init(key: Array<UInt8>, iv: Array<UInt8>?) throws {
  54. self.key = Key(bytes: key)
  55. self.iv = iv
  56. guard key.count == Rabbit.keySize && (iv == nil || iv!.count == Rabbit.ivSize) else {
  57. throw Error.invalidKeyOrInitializationVector
  58. }
  59. }
  60. // MARK: -
  61. fileprivate func setup() {
  62. p7 = 0
  63. // Key divided into 8 subkeys
  64. var k = Array<UInt32>(repeating: 0, count: 8)
  65. for j in 0..<8 {
  66. k[j] = UInt32(key[Rabbit.blockSize - (2 * j + 1)]) | (UInt32(key[Rabbit.blockSize - (2 * j + 2)]) << 8)
  67. }
  68. // Initialize state and counter variables from subkeys
  69. for j in 0..<8 {
  70. if j % 2 == 0 {
  71. x[j] = (k[(j + 1) % 8] << 16) | k[j]
  72. c[j] = (k[(j + 4) % 8] << 16) | k[(j + 5) % 8]
  73. } else {
  74. x[j] = (k[(j + 5) % 8] << 16) | k[(j + 4) % 8]
  75. c[j] = (k[j] << 16) | k[(j + 1) % 8]
  76. }
  77. }
  78. // Iterate system four times
  79. nextState()
  80. nextState()
  81. nextState()
  82. nextState()
  83. // Reinitialize counter variables
  84. for j in 0..<8 {
  85. c[j] = c[j] ^ x[(j + 4) % 8]
  86. }
  87. if let iv = iv {
  88. setupIV(iv)
  89. }
  90. }
  91. private func setupIV(_ iv: Array<UInt8>) {
  92. // 63...56 55...48 47...40 39...32 31...24 23...16 15...8 7...0 IV bits
  93. // 0 1 2 3 4 5 6 7 IV bytes in array
  94. let iv0 = UInt32(bytes: [iv[4], iv[5], iv[6], iv[7]])
  95. let iv1 = UInt32(bytes: [iv[0], iv[1], iv[4], iv[5]])
  96. let iv2 = UInt32(bytes: [iv[0], iv[1], iv[2], iv[3]])
  97. let iv3 = UInt32(bytes: [iv[2], iv[3], iv[6], iv[7]])
  98. // Modify the counter state as function of the IV
  99. c[0] = c[0] ^ iv0
  100. c[1] = c[1] ^ iv1
  101. c[2] = c[2] ^ iv2
  102. c[3] = c[3] ^ iv3
  103. c[4] = c[4] ^ iv0
  104. c[5] = c[5] ^ iv1
  105. c[6] = c[6] ^ iv2
  106. c[7] = c[7] ^ iv3
  107. // Iterate system four times
  108. nextState()
  109. nextState()
  110. nextState()
  111. nextState()
  112. }
  113. private func nextState() {
  114. // Before an iteration the counters are incremented
  115. var carry = p7
  116. for j in 0..<8 {
  117. let prev = c[j]
  118. c[j] = prev &+ a[j] &+ carry
  119. carry = prev > c[j] ? 1 : 0 // detect overflow
  120. }
  121. p7 = carry // save last carry bit
  122. // Iteration of the system
  123. var newX = Array<UInt32>(repeating: 0, count: 8)
  124. newX[0] = g(0) &+ rotateLeft(g(7), by: 16) &+ rotateLeft(g(6), by: 16)
  125. newX[1] = g(1) &+ rotateLeft(g(0), by: 8) &+ g(7)
  126. newX[2] = g(2) &+ rotateLeft(g(1), by: 16) &+ rotateLeft(g(0), by: 16)
  127. newX[3] = g(3) &+ rotateLeft(g(2), by: 8) &+ g(1)
  128. newX[4] = g(4) &+ rotateLeft(g(3), by: 16) &+ rotateLeft(g(2), by: 16)
  129. newX[5] = g(5) &+ rotateLeft(g(4), by: 8) &+ g(3)
  130. newX[6] = g(6) &+ rotateLeft(g(5), by: 16) &+ rotateLeft(g(4), by: 16)
  131. newX[7] = g(7) &+ rotateLeft(g(6), by: 8) &+ g(5)
  132. x = newX
  133. }
  134. private func g(_ j: Int) -> UInt32 {
  135. let sum = x[j] &+ c[j]
  136. let square = UInt64(sum) * UInt64(sum)
  137. return UInt32(truncatingIfNeeded: square ^ (square >> 32))
  138. }
  139. fileprivate func nextOutput() -> Array<UInt8> {
  140. nextState()
  141. var output16 = Array<UInt16>(repeating: 0, count: Rabbit.blockSize / 2)
  142. output16[7] = UInt16(truncatingIfNeeded: x[0]) ^ UInt16(truncatingIfNeeded: x[5] >> 16)
  143. output16[6] = UInt16(truncatingIfNeeded: x[0] >> 16) ^ UInt16(truncatingIfNeeded: x[3])
  144. output16[5] = UInt16(truncatingIfNeeded: x[2]) ^ UInt16(truncatingIfNeeded: x[7] >> 16)
  145. output16[4] = UInt16(truncatingIfNeeded: x[2] >> 16) ^ UInt16(truncatingIfNeeded: x[5])
  146. output16[3] = UInt16(truncatingIfNeeded: x[4]) ^ UInt16(truncatingIfNeeded: x[1] >> 16)
  147. output16[2] = UInt16(truncatingIfNeeded: x[4] >> 16) ^ UInt16(truncatingIfNeeded: x[7])
  148. output16[1] = UInt16(truncatingIfNeeded: x[6]) ^ UInt16(truncatingIfNeeded: x[3] >> 16)
  149. output16[0] = UInt16(truncatingIfNeeded: x[6] >> 16) ^ UInt16(truncatingIfNeeded: x[1])
  150. var output8 = Array<UInt8>(repeating: 0, count: Rabbit.blockSize)
  151. for j in 0..<output16.count {
  152. output8[j * 2] = UInt8(truncatingIfNeeded: output16[j] >> 8)
  153. output8[j * 2 + 1] = UInt8(truncatingIfNeeded: output16[j])
  154. }
  155. return output8
  156. }
  157. }
  158. // MARK: Cipher
  159. extension Rabbit: Cipher {
  160. public func encrypt(_ bytes: ArraySlice<UInt8>) throws -> Array<UInt8> {
  161. setup()
  162. var result = Array<UInt8>(repeating: 0, count: bytes.count)
  163. var output = nextOutput()
  164. var byteIdx = 0
  165. var outputIdx = 0
  166. while byteIdx < bytes.count {
  167. if outputIdx == Rabbit.blockSize {
  168. output = nextOutput()
  169. outputIdx = 0
  170. }
  171. result[byteIdx] = bytes[byteIdx] ^ output[outputIdx]
  172. byteIdx += 1
  173. outputIdx += 1
  174. }
  175. return result
  176. }
  177. public func decrypt(_ bytes: ArraySlice<UInt8>) throws -> Array<UInt8> {
  178. return try encrypt(bytes)
  179. }
  180. }