Access.swift 8.7 KB

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