BlowfishTests.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. //
  2. // CryptoSwift
  3. //
  4. // Copyright (C) 2014-2025 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. // Test vector from http://www.schneier.com/code/vectors.txt
  16. import XCTest
  17. @testable import CryptoSwift
  18. class BlowfishTests: XCTestCase {
  19. struct TestData {
  20. let key: Array<UInt8>
  21. let input: Array<UInt8>
  22. let output: Array<UInt8>
  23. }
  24. let tests = [
  25. TestData(key: [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
  26. input: [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
  27. output: [0x4e, 0xf9, 0x97, 0x45, 0x61, 0x98, 0xdd, 0x78]),
  28. TestData(key: [0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff],
  29. input: [0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff],
  30. output: [0x51, 0x86, 0x6f, 0xd5, 0xb8, 0x5e, 0xcb, 0x8a]),
  31. TestData(key: [0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
  32. input: [0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01],
  33. output: [0x7d, 0x85, 0x6f, 0x9a, 0x61, 0x30, 0x63, 0xf2]),
  34. TestData(key: [0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11],
  35. input: [0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11],
  36. output: [0x24, 0x66, 0xdd, 0x87, 0x8b, 0x96, 0x3c, 0x9d]),
  37. TestData(key: [0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef],
  38. input: [0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11],
  39. output: [0x61, 0xf9, 0xc3, 0x80, 0x22, 0x81, 0xb0, 0x96]),
  40. TestData(key: [0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11],
  41. input: [0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef],
  42. output: [0x7d, 0x0c, 0xc6, 0x30, 0xaf, 0xda, 0x1e, 0xc7]),
  43. TestData(key: [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
  44. input: [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
  45. output: [0x4e, 0xf9, 0x97, 0x45, 0x61, 0x98, 0xdd, 0x78]),
  46. TestData(key: [0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10],
  47. input: [0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef],
  48. output: [0x0a, 0xce, 0xab, 0x0f, 0xc6, 0xa0, 0xa2, 0x8d]),
  49. TestData(key: [0x7c, 0xa1, 0x10, 0x45, 0x4a, 0x1a, 0x6e, 0x57],
  50. input: [0x01, 0xa1, 0xd6, 0xd0, 0x39, 0x77, 0x67, 0x42],
  51. output: [0x59, 0xc6, 0x82, 0x45, 0xeb, 0x05, 0x28, 0x2b]),
  52. TestData(key: [0x01, 0x31, 0xd9, 0x61, 0x9d, 0xc1, 0x37, 0x6e],
  53. input: [0x5c, 0xd5, 0x4c, 0xa8, 0x3d, 0xef, 0x57, 0xda],
  54. output: [0xb1, 0xb8, 0xcc, 0x0b, 0x25, 0x0f, 0x09, 0xa0]),
  55. TestData(key: [0x07, 0xa1, 0x13, 0x3e, 0x4a, 0x0b, 0x26, 0x86],
  56. input: [0x02, 0x48, 0xd4, 0x38, 0x06, 0xf6, 0x71, 0x72],
  57. output: [0x17, 0x30, 0xe5, 0x77, 0x8b, 0xea, 0x1d, 0xa4]),
  58. TestData(key: [0x38, 0x49, 0x67, 0x4c, 0x26, 0x02, 0x31, 0x9e],
  59. input: [0x51, 0x45, 0x4b, 0x58, 0x2d, 0xdf, 0x44, 0x0a],
  60. output: [0xa2, 0x5e, 0x78, 0x56, 0xcf, 0x26, 0x51, 0xeb]),
  61. TestData(key: [0x04, 0xb9, 0x15, 0xba, 0x43, 0xfe, 0xb5, 0xb6],
  62. input: [0x42, 0xfd, 0x44, 0x30, 0x59, 0x57, 0x7f, 0xa2],
  63. output: [0x35, 0x38, 0x82, 0xb1, 0x09, 0xce, 0x8f, 0x1a]),
  64. TestData(key: [0x01, 0x13, 0xb9, 0x70, 0xfd, 0x34, 0xf2, 0xce],
  65. input: [0x05, 0x9b, 0x5e, 0x08, 0x51, 0xcf, 0x14, 0x3a],
  66. output: [0x48, 0xf4, 0xd0, 0x88, 0x4c, 0x37, 0x99, 0x18]),
  67. TestData(key: [0x01, 0x70, 0xf1, 0x75, 0x46, 0x8f, 0xb5, 0xe6],
  68. input: [0x07, 0x56, 0xd8, 0xe0, 0x77, 0x47, 0x61, 0xd2],
  69. output: [0x43, 0x21, 0x93, 0xb7, 0x89, 0x51, 0xfc, 0x98]),
  70. TestData(key: [0x43, 0x29, 0x7f, 0xad, 0x38, 0xe3, 0x73, 0xfe],
  71. input: [0x76, 0x25, 0x14, 0xb8, 0x29, 0xbf, 0x48, 0x6a],
  72. output: [0x13, 0xf0, 0x41, 0x54, 0xd6, 0x9d, 0x1a, 0xe5]),
  73. TestData(key: [0x07, 0xa7, 0x13, 0x70, 0x45, 0xda, 0x2a, 0x16],
  74. input: [0x3b, 0xdd, 0x11, 0x90, 0x49, 0x37, 0x28, 0x02],
  75. output: [0x2e, 0xed, 0xda, 0x93, 0xff, 0xd3, 0x9c, 0x79]),
  76. TestData(key: [0x04, 0x68, 0x91, 0x04, 0xc2, 0xfd, 0x3b, 0x2f],
  77. input: [0x26, 0x95, 0x5f, 0x68, 0x35, 0xaf, 0x60, 0x9a],
  78. output: [0xd8, 0x87, 0xe0, 0x39, 0x3c, 0x2d, 0xa6, 0xe3]),
  79. TestData(key: [0x37, 0xd0, 0x6b, 0xb5, 0x16, 0xcb, 0x75, 0x46],
  80. input: [0x16, 0x4d, 0x5e, 0x40, 0x4f, 0x27, 0x52, 0x32],
  81. output: [0x5f, 0x99, 0xd0, 0x4f, 0x5b, 0x16, 0x39, 0x69]),
  82. TestData(key: [0x1f, 0x08, 0x26, 0x0d, 0x1a, 0xc2, 0x46, 0x5e],
  83. input: [0x6b, 0x05, 0x6e, 0x18, 0x75, 0x9f, 0x5c, 0xca],
  84. output: [0x4a, 0x05, 0x7a, 0x3b, 0x24, 0xd3, 0x97, 0x7b]),
  85. TestData(key: [0x58, 0x40, 0x23, 0x64, 0x1a, 0xba, 0x61, 0x76],
  86. input: [0x00, 0x4b, 0xd6, 0xef, 0x09, 0x17, 0x60, 0x62],
  87. output: [0x45, 0x20, 0x31, 0xc1, 0xe4, 0xfa, 0xda, 0x8e]),
  88. TestData(key: [0x02, 0x58, 0x16, 0x16, 0x46, 0x29, 0xb0, 0x07],
  89. input: [0x48, 0x0d, 0x39, 0x00, 0x6e, 0xe7, 0x62, 0xf2],
  90. output: [0x75, 0x55, 0xae, 0x39, 0xf5, 0x9b, 0x87, 0xbd]),
  91. TestData(key: [0x49, 0x79, 0x3e, 0xbc, 0x79, 0xb3, 0x25, 0x8f],
  92. input: [0x43, 0x75, 0x40, 0xc8, 0x69, 0x8f, 0x3c, 0xfa],
  93. output: [0x53, 0xc5, 0x5f, 0x9c, 0xb4, 0x9f, 0xc0, 0x19]),
  94. TestData(key: [0x4f, 0xb0, 0x5e, 0x15, 0x15, 0xab, 0x73, 0xa7],
  95. input: [0x07, 0x2d, 0x43, 0xa0, 0x77, 0x07, 0x52, 0x92],
  96. output: [0x7a, 0x8e, 0x7b, 0xfa, 0x93, 0x7e, 0x89, 0xa3]),
  97. TestData(key: [0x49, 0xe9, 0x5d, 0x6d, 0x4c, 0xa2, 0x29, 0xbf],
  98. input: [0x02, 0xfe, 0x55, 0x77, 0x81, 0x17, 0xf1, 0x2a],
  99. output: [0xcf, 0x9c, 0x5d, 0x7a, 0x49, 0x86, 0xad, 0xb5]),
  100. TestData(key: [0x01, 0x83, 0x10, 0xdc, 0x40, 0x9b, 0x26, 0xd6],
  101. input: [0x1d, 0x9d, 0x5c, 0x50, 0x18, 0xf7, 0x28, 0xc2],
  102. output: [0xd1, 0xab, 0xb2, 0x90, 0x65, 0x8b, 0xc7, 0x78]),
  103. TestData(key: [0x1c, 0x58, 0x7f, 0x1c, 0x13, 0x92, 0x4f, 0xef],
  104. input: [0x30, 0x55, 0x32, 0x28, 0x6d, 0x6f, 0x29, 0x5a],
  105. output: [0x55, 0xcb, 0x37, 0x74, 0xd1, 0x3e, 0xf2, 0x01]),
  106. TestData(key: [0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01],
  107. input: [0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef],
  108. output: [0xfa, 0x34, 0xec, 0x48, 0x47, 0xb2, 0x68, 0xb2]),
  109. TestData(key: [0x1f, 0x1f, 0x1f, 0x1f, 0x0e, 0x0e, 0x0e, 0x0e],
  110. input: [0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef],
  111. output: [0xa7, 0x90, 0x79, 0x51, 0x08, 0xea, 0x3c, 0xae]),
  112. TestData(key: [0xe0, 0xfe, 0xe0, 0xfe, 0xf1, 0xfe, 0xf1, 0xfe],
  113. input: [0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef],
  114. output: [0xc3, 0x9e, 0x07, 0x2d, 0x9f, 0xac, 0x63, 0x1d]),
  115. TestData(key: [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
  116. input: [0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff],
  117. output: [0x01, 0x49, 0x33, 0xe0, 0xcd, 0xaf, 0xf6, 0xe4]),
  118. TestData(key: [0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff],
  119. input: [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
  120. output: [0xf2, 0x1e, 0x9a, 0x77, 0xb7, 0x1c, 0x49, 0xbc]),
  121. TestData(key: [0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef],
  122. input: [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
  123. output: [0x24, 0x59, 0x46, 0x88, 0x57, 0x54, 0x36, 0x9a]),
  124. TestData(key: [0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10],
  125. input: [0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff],
  126. output: [0x6b, 0x5c, 0x5a, 0x9c, 0x5d, 0x9e, 0x0a, 0x5a])
  127. ]
  128. func testEncrypt() {
  129. for test in self.tests {
  130. XCTAssertEqual(try Blowfish(key: test.key, blockMode: ECB(), padding: .noPadding).encrypt(test.input), test.output)
  131. }
  132. }
  133. func testDecrypt() {
  134. for test in self.tests {
  135. XCTAssertEqual(try Blowfish(key: test.key, blockMode: ECB(), padding: .noPadding).decrypt(test.output), test.input)
  136. }
  137. }
  138. func testCBCZeroPadding() {
  139. let key = Array<UInt8>(hex: "0123456789ABCDEFF0E1D2C3B4A59687")
  140. let iv = Array<UInt8>(hex: "FEDCBA9876543210")
  141. let input = Array<UInt8>(hex: "37363534333231204E6F77206973207468652074696D6520666F722000")
  142. XCTAssertEqual(try Blowfish(key: key, blockMode: CBC(iv: iv), padding: .zeroPadding).encrypt(input), Array<UInt8>(hex: "6B77B4D63006DEE605B156E27403979358DEB9E7154616D959F1652BD5FF92CC"))
  143. }
  144. func testEncryptDecrypt() {
  145. let key = Array<UInt8>.init(hex: "0123456789ABCDEFF0E1D2C3B4A59687")
  146. let iv = Array<UInt8>.init(hex: "FEDCBA9876543210")
  147. let input = Array<UInt8>.init(hex: "37363534333231204E6F77206973207468652074696D6520666F722000")
  148. do {
  149. let cipher = try Blowfish(key: key, blockMode: CBC(iv: iv), padding: .pkcs7)
  150. let ciphertext = try cipher.encrypt(input)
  151. let plaintext = try cipher.decrypt(ciphertext)
  152. XCTAssertEqual(plaintext, input)
  153. } catch {
  154. XCTFail(error.localizedDescription)
  155. }
  156. }
  157. // https://github.com/krzyzanowskim/CryptoSwift/issues/415
  158. func testDecryptCFB415() {
  159. do {
  160. let plaintext = "secret12".bytes
  161. let key = "passwordpassword".bytes
  162. let iv = "12345678".bytes
  163. let encrypted = try Blowfish(key: key, blockMode: CFB(iv: iv), padding: .noPadding).encrypt(plaintext)
  164. let decrypted = try Blowfish(key: key, blockMode: CFB(iv: iv), padding: .noPadding).decrypt(encrypted)
  165. XCTAssertEqual(plaintext, decrypted)
  166. } catch {
  167. XCTFail(error.localizedDescription)
  168. }
  169. }
  170. }
  171. extension BlowfishTests {
  172. static func allTests() -> [(String, (BlowfishTests) -> () -> Void)] {
  173. let tests = [
  174. ("testEncrypt", testEncrypt),
  175. ("testDecrypt", testDecrypt),
  176. ("testCBCZeroPadding", testCBCZeroPadding),
  177. ("testEncryptDecrypt", testEncryptDecrypt),
  178. ("testDecryptCFB415", testDecryptCFB415)
  179. ]
  180. return tests
  181. }
  182. }