Access.swift 8.8 KB

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