Rabbit.swift 6.3 KB

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