Access.swift 8.6 KB

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