Access.swift 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. import XCTest
  16. import Foundation
  17. // import without @testable to test public API
  18. import CryptoSwift
  19. class Access: XCTestCase {
  20. let cipher = try! AES(key: "secret0key000000", iv: "0123456789012345")
  21. let authenticator = HMAC(key: Array<UInt8>(hex: "b1b2b3b3b3b3b3b3b1b2b3b3b3b3b3b3"), variant: .sha1)
  22. func testChecksum() {
  23. _ = Checksum.crc32([1, 2, 3])
  24. _ = Checksum.crc16([1, 2, 3])
  25. }
  26. func testRandomIV() {
  27. _ = AES.randomIV(AES.blockSize)
  28. _ = ChaCha20.randomIV(ChaCha20.blockSize)
  29. }
  30. func testDigest() {
  31. _ = Digest.md5([1, 2, 3])
  32. _ = Digest.sha1([1, 2, 3])
  33. _ = Digest.sha224([1, 2, 3])
  34. _ = Digest.sha256([1, 2, 3])
  35. _ = Digest.sha384([1, 2, 3])
  36. _ = Digest.sha512([1, 2, 3])
  37. _ = Digest.sha3([1, 2, 3], variant: .sha224)
  38. _ = SHA1().calculate(for: [0])
  39. _ = SHA2(variant: .sha224).calculate(for: [0])
  40. _ = SHA3(variant: .sha256).calculate(for: [0])
  41. _ = MD5().calculate(for: [0])
  42. }
  43. func testArrayExtension() {
  44. let array = Array<UInt8>(hex: "b1b2b3b3b3b3b3b3b1b2b3b3b3b3b3b3")
  45. _ = array.toHexString()
  46. _ = array.md5()
  47. _ = array.sha1()
  48. _ = array.sha256()
  49. _ = array.sha384()
  50. _ = array.sha512()
  51. _ = array.sha2(.sha224)
  52. _ = array.sha3(.sha224)
  53. _ = array.crc32()
  54. _ = array.crc16()
  55. do {
  56. _ = try array.encrypt(cipher: cipher)
  57. _ = try array.decrypt(cipher: cipher)
  58. _ = try array.authenticate(with: authenticator)
  59. } catch {
  60. XCTFail(error.localizedDescription)
  61. }
  62. }
  63. func testCollectionExtension() {
  64. // nothing public
  65. }
  66. func testStringExtension() {
  67. let string = "foofoobarbar"
  68. _ = string.md5()
  69. _ = string.sha1()
  70. _ = string.sha224()
  71. _ = string.sha256()
  72. _ = string.sha384()
  73. _ = string.sha512()
  74. _ = string.sha3(.sha224)
  75. _ = string.crc16()
  76. _ = string.crc32()
  77. do {
  78. _ = try string.encrypt(cipher: cipher)
  79. _ = try string.authenticate(with: authenticator)
  80. } catch {
  81. XCTFail(error.localizedDescription)
  82. }
  83. }
  84. func testStringFoundationExtension() {
  85. let string = "aPf/i9th9iX+vf49eR7PYk2q7S5xmm3jkRLejgzHNJs="
  86. do {
  87. _ = try string.decryptBase64ToString(cipher: cipher)
  88. _ = try string.decryptBase64(cipher: cipher)
  89. } catch {
  90. XCTFail(error.localizedDescription)
  91. }
  92. }
  93. func testIntExtension() {
  94. // nothing public
  95. }
  96. func testUInt16Extension() {
  97. // nothing public
  98. }
  99. func testUInt32Extension() {
  100. // nothing public
  101. }
  102. func testUInt64Extension() {
  103. // nothing public
  104. }
  105. func testUInt8Extension() {
  106. // nothing public
  107. }
  108. func testDataExtension() {
  109. let data = Data(bytes: [1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8])
  110. _ = data.checksum()
  111. _ = data.md5()
  112. _ = data.sha1()
  113. _ = data.sha224()
  114. _ = data.sha256()
  115. _ = data.sha384()
  116. _ = data.sha512()
  117. _ = data.sha3(.sha224)
  118. _ = data.crc16()
  119. _ = data.crc32()
  120. _ = data.bytes
  121. _ = data.toHexString()
  122. do {
  123. _ = try data.encrypt(cipher: cipher)
  124. _ = try data.decrypt(cipher: cipher)
  125. _ = try data.authenticate(with: authenticator)
  126. } catch {
  127. XCTFail(error.localizedDescription)
  128. }
  129. }
  130. func testPadding() {
  131. // PKCS7
  132. _ = Padding.pkcs7.add(to: [1, 2, 3], blockSize: 16)
  133. _ = Padding.pkcs7.remove(from: [1, 2, 3], blockSize: 16)
  134. // PKCS5
  135. _ = Padding.pkcs5.add(to: [1, 2, 3], blockSize: 16)
  136. _ = Padding.pkcs5.remove(from: [1, 2, 3], blockSize: 16)
  137. // NoPadding
  138. _ = Padding.noPadding.add(to: [1, 2, 3], blockSize: 16)
  139. _ = Padding.noPadding.remove(from: [1, 2, 3], blockSize: 16)
  140. // ZeroPadding
  141. _ = Padding.zeroPadding.add(to: [1, 2, 3], blockSize: 16)
  142. _ = Padding.zeroPadding.remove(from: [1, 2, 3], blockSize: 16)
  143. }
  144. func testPBKDF() {
  145. do {
  146. _ = PKCS5.PBKDF1.Variant.md5
  147. _ = try PKCS5.PBKDF1(password: [1, 2, 3, 4, 5, 6, 7], salt: [1, 2, 3, 4, 5, 6, 7, 8]).calculate()
  148. _ = try PKCS5.PBKDF2(password: [1, 2, 3, 4, 5, 6, 7], salt: [1, 2, 3, 4]).calculate()
  149. } catch {
  150. XCTFail(error.localizedDescription)
  151. }
  152. }
  153. func testAuthenticators() {
  154. do {
  155. _ = try HMAC(key: Array<UInt8>(hex: "b1b2b3b3b3b3b3b3b1b2b3b3b3b3b3b3"), variant: .sha1).authenticate([1, 2, 3])
  156. _ = try Poly1305(key: [1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2]).authenticate([1, 2, 3])
  157. } catch {
  158. XCTFail(error.localizedDescription)
  159. }
  160. }
  161. func testAES() {
  162. do {
  163. let cipher = try AES(key: "secret0key000000", iv: "0123456789012345")
  164. var encryptor = try cipher.makeEncryptor()
  165. _ = try encryptor.update(withBytes: [1, 2, 3])
  166. _ = try encryptor.finish()
  167. var decryptor = try cipher.makeDecryptor()
  168. _ = try decryptor.update(withBytes: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16])
  169. _ = try decryptor.finish()
  170. let enc = try cipher.encrypt([1, 2, 3])
  171. _ = try cipher.decrypt(enc)
  172. _ = try AES(key: [1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6], blockMode: .CBC(iv: [1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6]), padding: .noPadding)
  173. _ = AES.Variant.aes128
  174. _ = AES.blockSize
  175. } catch {
  176. XCTFail("\(error)")
  177. }
  178. }
  179. func testBlowfish() {
  180. do {
  181. let cipher = try Blowfish(key: "secret0key000000", iv: "01234567")
  182. let enc = try cipher.encrypt([1, 2, 3])
  183. _ = try cipher.decrypt(enc)
  184. _ = try Blowfish(key: [1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6], padding: .noPadding)
  185. _ = Blowfish.blockSize
  186. } catch {
  187. XCTFail(error.localizedDescription)
  188. }
  189. }
  190. func testRabbit() {
  191. do {
  192. let rabbit = try Rabbit(key: [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])
  193. let enc = try rabbit.encrypt([1, 2, 3])
  194. _ = try rabbit.decrypt(enc)
  195. XCTAssertThrowsError(try Rabbit(key: "123"))
  196. _ = Rabbit.blockSize
  197. _ = Rabbit.keySize
  198. _ = Rabbit.ivSize
  199. } catch {
  200. XCTFail(error.localizedDescription)
  201. }
  202. }
  203. func testChaCha20() {
  204. let key: Array<UInt8> = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
  205. let iv: Array<UInt8> = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
  206. do {
  207. _ = ChaCha20.blockSize
  208. let chacha20 = try ChaCha20(key: key, iv: iv)
  209. let enc = try chacha20.encrypt([1, 3, 4])
  210. _ = try chacha20.decrypt(enc)
  211. XCTAssertThrowsError(try ChaCha20(key: "123", iv: "12345678"))
  212. _ = chacha20.makeEncryptor()
  213. _ = chacha20.makeDecryptor()
  214. } catch {
  215. XCTFail(error.localizedDescription)
  216. }
  217. }
  218. func testUpdatable() {
  219. // TODO:
  220. }
  221. static let allTests = [
  222. ("testChecksum", testChecksum),
  223. ("testDigest", testDigest),
  224. ("testArrayExtension", testArrayExtension),
  225. ("testCollectionExtension", testCollectionExtension),
  226. ("testStringExtension", testStringExtension),
  227. ("testStringFoundationExtension", testStringFoundationExtension),
  228. ("testIntExtension", testIntExtension),
  229. ("testUInt16Extension", testUInt16Extension),
  230. ("testUInt32Extension", testUInt32Extension),
  231. ("testUInt64Extension", testUInt64Extension),
  232. ("testUInt8Extension", testUInt8Extension),
  233. ("testDataExtension", testDataExtension),
  234. ("testPadding", testPadding),
  235. ("testPBKDF", testPBKDF),
  236. ("testAuthenticators", testAuthenticators),
  237. ("testAES", testAES),
  238. ("testBlowfish", testBlowfish),
  239. ("testRabbit", testRabbit),
  240. ("testChaCha20", testChaCha20),
  241. ("testUpdatable", testUpdatable),
  242. ("testRandomIV", testRandomIV),
  243. ]
  244. }