DES.swift 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //
  2. // DES.swift
  3. // CryptoSwift
  4. //
  5. // Created by Marcin Krzyzanowski on 21/01/2017.
  6. // Copyright © 2017 Marcin Krzyzanowski. All rights reserved.
  7. //
  8. // http://csrc.nist.gov/publications/fips/fips46-3/fips46-3.pdf
  9. //
  10. /// Data Encryption Standard (DES)
  11. public final class DES: BlockCipher {
  12. public static let blockSize: Int = 8
  13. private let permutedChoice1: Array<UInt8> = [7, 15, 23, 31, 39, 47, 55, 63,
  14. 6, 14, 22, 30, 38, 46, 54, 62,
  15. 5, 13, 21, 29, 37, 45, 53, 61,
  16. 4, 12, 20, 28, 1, 9, 17, 25,
  17. 33, 41, 49, 57, 2, 10, 18, 26,
  18. 34, 42, 50, 58, 3, 11, 19, 27,
  19. 35, 43, 51, 59, 36, 44, 52, 60]
  20. private let permutedChoice2: Array<UInt8> = [42, 39, 45, 32, 55, 51, 53, 28,
  21. 41, 50, 35, 46, 33, 37, 44, 52,
  22. 30, 48, 40, 49, 29, 36, 43, 54,
  23. 15, 4, 25, 19, 9, 1, 26, 16,
  24. 5, 11, 23, 8, 12, 7, 17, 0,
  25. 22, 3, 10, 14, 6, 20, 27, 24]
  26. private let ksRotations: Array<UInt8> = [1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1]
  27. private var subkeys = Array<UInt64>()
  28. public init(key: Array<UInt8>) throws {
  29. self.subkeys = self.generateSubkeys(key: key)
  30. }
  31. /// The 64 bits of the input block to be enciphered are first subjected to the following permutation, called the initial permutation.
  32. ///
  33. /// - Parameter block: 8 bytes
  34. /// - Returns: block
  35. fileprivate func initialPermuation(block: inout UInt64) {
  36. // block = b7 b6 b5 b4 b3 b2 b1 b0
  37. var b1 = block >> 48
  38. var b2 = block << 48
  39. block ^= b1 ^ b2 ^ b1 << 48 ^ b2 >> 48
  40. // block = b1 b0 b5 b4 b3 b2 b7 b6
  41. b1 = block >> 32 & 0xff00ff
  42. b2 = (block & 0xff00ff00)
  43. block ^= b1 << 32 ^ b2 ^ b1 << 8 ^ b2 << 24 // exchange b0 b4 with b3 b7
  44. // exchange 4,5,6,7 with 32,33,34,35 etc.
  45. b1 = block & 0x0f0f00000f0f0000
  46. b2 = block & 0x0000f0f00000f0f0
  47. block ^= b1 ^ b2 ^ b1 >> 12 ^ b2 << 12
  48. // exchange 0,1,4,5 with 18,19,22,23
  49. b1 = block & 0x3300330033003300
  50. b2 = block & 0x00cc00cc00cc00cc
  51. block ^= b1 ^ b2 ^ b1 >> 6 ^ b2 << 6
  52. // exchange 0,2,4,6 with 9,11,13,15
  53. b1 = block & 0xaaaaaaaa55555555
  54. block ^= b1 ^ b1 >> 33 ^ b1 << 33
  55. }
  56. /// Expands an input block of 32 bits, producing an output block of 48 bits.
  57. fileprivate func expand(src: UInt32) -> UInt64 {
  58. var src = (src << 5) | (src >> 27)
  59. var result: UInt64 = 0
  60. for _ in 0 ..< 8 {
  61. result <<= 6
  62. result |= UInt64(src) & (1 << 6 - 1)
  63. src = (src << 4) | (src >> 28)
  64. }
  65. return result
  66. }
  67. /// General purpose function to perform block permutations
  68. fileprivate func permute(block: UInt64, permutation: Array<UInt8>) -> UInt64 {
  69. var result: UInt64 = 0
  70. for (idx,value) in permutation.enumerated() {
  71. let bit = (block >> UInt64(value)) & 1
  72. result |= bit << UInt64(permutation.count - 1 - idx)
  73. }
  74. return result
  75. }
  76. // 16 28-bit blocks rotated according to the rotation ksRotations schedule
  77. fileprivate func ksRotate(_ value: UInt32) -> Array<UInt32> {
  78. var result = Array<UInt32>(repeating: 0, count: 16)
  79. var last = value
  80. for i in 0 ..< 16 {
  81. let left = (last << UInt32(4 + ksRotations[i])) >> 4
  82. let right = (last << 4) >> 32 - UInt32(ksRotations[i])
  83. result[i] = left | right
  84. last = result[i]
  85. }
  86. return result
  87. }
  88. fileprivate func generateSubkeys(key: Array<UInt8>) -> Array<UInt64> {
  89. //TODO: check endianess of UInt64
  90. var subkeys = Array<UInt64>(repeating: 0, count: 16)
  91. let permutedKey = self.permute(block: UInt64(bytes: key), permutation: permutedChoice1)
  92. // rotate halves of permuted key
  93. let leftRotations = ksRotate(UInt32(permutedKey >> 28))
  94. let rightRotations = ksRotate(UInt32(permutedKey << 4) >> 4)
  95. for i in 0 ..< 16 {
  96. let pc2Input = UInt64(leftRotations[i])<<28 | uint64(rightRotations[i])
  97. // apply PC2 permutation to 7 byte input
  98. subkeys[i] = self.permute(block: pc2Input, permutation: permutedChoice2)
  99. }
  100. return subkeys
  101. }
  102. }
  103. extension DES: Cipher {
  104. public func encrypt<C: Collection>(_ bytes: C) throws -> Array<UInt8> where C.Iterator.Element == UInt8, C.IndexDistance == Int, C.Index == Int, C.SubSequence: Collection, C.SubSequence.Iterator.Element == C.Iterator.Element, C.SubSequence.Index == C.Index, C.SubSequence.IndexDistance == C.IndexDistance {
  105. for chunk in bytes.batched(by: DES.blockSize) {
  106. var b = UInt64(bytes: chunk) //TODO: check endianess
  107. self.initialPermuation(block: &b)
  108. let left = UInt32(b >> 32)
  109. let right = UInt32(truncatingBitPattern: b)
  110. //TODO: more to do
  111. }
  112. return []
  113. }
  114. public func decrypt<C: Collection>(_ bytes: C) throws -> Array<UInt8> where C.Iterator.Element == UInt8, C.IndexDistance == Int, C.Index == Int {
  115. return []
  116. }
  117. }