Access.swift 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 _ = MD5()
  32. }
  33. func testArrayExtension() {
  34. let array = Array<UInt8>(hex: "b1b2b3b3b3b3b3b3b1b2b3b3b3b3b3b3")
  35. let _ = array.toHexString()
  36. let _ = array.md5()
  37. let _ = array.sha1()
  38. let _ = array.sha256()
  39. let _ = array.sha384()
  40. let _ = array.sha512()
  41. let _ = array.sha3(.sha224)
  42. let _ = array.crc32()
  43. let _ = array.crc16()
  44. do {
  45. let _ = try array.encrypt(cipher: cipher)
  46. let _ = try array.decrypt(cipher: cipher)
  47. let _ = try array.authenticate(with: authenticator)
  48. } catch {
  49. XCTFail(error.localizedDescription)
  50. }
  51. }
  52. func testCollectionExtension() {
  53. // nothing public
  54. }
  55. func testStringExtension() {
  56. let string = "foofoobarbar"
  57. let _ = string.md5()
  58. let _ = string.sha1()
  59. let _ = string.sha224()
  60. let _ = string.sha256()
  61. let _ = string.sha384()
  62. let _ = string.sha512()
  63. let _ = string.sha3(.sha224)
  64. let _ = string.crc16()
  65. let _ = string.crc32()
  66. do {
  67. let _ = try string.encrypt(cipher: cipher)
  68. let _ = try string.authenticate(with: authenticator)
  69. } catch {
  70. XCTFail(error.localizedDescription)
  71. }
  72. }
  73. func testStringFoundationExtension() {
  74. let string = "aPf/i9th9iX+vf49eR7PYk2q7S5xmm3jkRLejgzHNJs="
  75. do {
  76. let _ = try string.decryptBase64ToString(cipher: cipher)
  77. let _ = try string.decryptBase64(cipher: cipher)
  78. } catch {
  79. XCTFail(error.localizedDescription)
  80. }
  81. }
  82. func testIntExtension() {
  83. // nothing public
  84. }
  85. func testUInt16Extension() {
  86. // nothing public
  87. }
  88. func testUInt32Extension() {
  89. // nothing public
  90. }
  91. func testUInt64Extension() {
  92. // nothing public
  93. }
  94. func testUInt8Extension() {
  95. // nothing public
  96. }
  97. func testDataExtension() {
  98. let data = Data(bytes: [1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8])
  99. let _ = data.checksum()
  100. let _ = data.md5()
  101. let _ = data.sha1()
  102. let _ = data.sha224()
  103. let _ = data.sha256()
  104. let _ = data.sha384()
  105. let _ = data.sha512()
  106. let _ = data.sha3(.sha224)
  107. let _ = data.crc16()
  108. let _ = data.crc32()
  109. let _ = data.bytes
  110. let _ = data.toHexString()
  111. do {
  112. let _ = try data.encrypt(cipher: cipher)
  113. let _ = try data.decrypt(cipher: cipher)
  114. let _ = try data.authenticate(with: authenticator)
  115. } catch {
  116. XCTFail(error.localizedDescription)
  117. }
  118. }
  119. func testPadding() {
  120. // PKCS7
  121. let _ = PKCS7().add(to: [1,2,3], blockSize: 16)
  122. let _ = PKCS7().remove(from: [1,2,3], blockSize: 16)
  123. // NoPadding
  124. let _ = NoPadding().add(to: [1,2,3], blockSize: 16)
  125. let _ = NoPadding().remove(from: [1,2,3], blockSize: 16)
  126. // ZeroPadding
  127. let _ = ZeroPadding().add(to: [1,2,3], blockSize: 16)
  128. let _ = ZeroPadding().remove(from: [1,2,3], blockSize: 16)
  129. }
  130. func testPBKDF() {
  131. do {
  132. let _ = PKCS5.PBKDF1.Variant.md5
  133. let _ = try PKCS5.PBKDF1(password: [1,2,3,4,5,6,7], salt: [1,2,3,4,5,6,7,8]).calculate()
  134. let _ = try PKCS5.PBKDF2(password: [1,2,3,4,5,6,7], salt: [1,2,3,4]).calculate()
  135. } catch {
  136. XCTFail(error.localizedDescription)
  137. }
  138. }
  139. func testAuthenticators() {
  140. do {
  141. let _ = try HMAC(key: Array<UInt8>(hex: "b1b2b3b3b3b3b3b3b1b2b3b3b3b3b3b3"), variant: .sha1).authenticate([1,2,3])
  142. 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])
  143. } catch {
  144. XCTFail(error.localizedDescription)
  145. }
  146. }
  147. func testAES() {
  148. do {
  149. let aes = try AES(key: "secret0key000000", iv: "0123456789012345")
  150. var encryptor = aes.makeEncryptor()
  151. let _ = try encryptor.update(withBytes: [1,2,3])
  152. let _ = try encryptor.finish()
  153. var decryptor = aes.makeDecryptor()
  154. let _ = try decryptor.update(withBytes: [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16])
  155. let _ = try decryptor.finish()
  156. let enc = try aes.encrypt([1,2,3])
  157. let _ = try aes.decrypt(enc)
  158. 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())
  159. let _ = AES.Variant.aes128
  160. let _ = AES.blockSize
  161. } catch {
  162. XCTFail(error.localizedDescription)
  163. }
  164. }
  165. func testRabbit() {
  166. do {
  167. let rabbit = try Rabbit(key: [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])
  168. let enc = rabbit.encrypt([1,2,3])
  169. let _ = rabbit.decrypt(enc)
  170. XCTAssertThrowsError(try Rabbit(key: "123"))
  171. let _ = Rabbit.blockSize
  172. let _ = Rabbit.keySize
  173. let _ = Rabbit.ivSize
  174. } catch {
  175. XCTFail(error.localizedDescription)
  176. }
  177. }
  178. func testChaCha20() {
  179. let key:Array<UInt8> = [0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c];
  180. let iv:Array<UInt8> = [0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F]
  181. do {
  182. let _ = ChaCha20.blockSize
  183. let chacha20 = try ChaCha20(key: key, iv: iv)
  184. let enc = try chacha20.encrypt([1,3,4])
  185. let _ = try chacha20.decrypt(enc)
  186. XCTAssertThrowsError(try ChaCha20(key: "123", iv: "12345678"))
  187. let _ = chacha20.makeEncryptor()
  188. let _ = chacha20.makeDecryptor()
  189. } catch {
  190. XCTFail(error.localizedDescription)
  191. }
  192. }
  193. func testUpdatable() {
  194. // TODO
  195. }
  196. static let allTests = [
  197. ("testChecksum", testChecksum),
  198. ("testDigest", testDigest),
  199. ("testArrayExtension", testArrayExtension),
  200. ("testCollectionExtension", testCollectionExtension),
  201. ("testStringExtension", testStringExtension),
  202. ("testStringFoundationExtension", testStringFoundationExtension),
  203. ("testIntExtension", testIntExtension),
  204. ("testUInt16Extension", testUInt16Extension),
  205. ("testUInt32Extension", testUInt32Extension),
  206. ("testUInt64Extension", testUInt64Extension),
  207. ("testUInt8Extension", testUInt8Extension),
  208. ("testDataExtension", testDataExtension),
  209. ("testPadding", testPadding),
  210. ("testPBKDF", testPBKDF),
  211. ("testAuthenticators", testAuthenticators),
  212. ("testAES", testAES),
  213. ("testRabbit", testRabbit),
  214. ("testChaCha20", testChaCha20),
  215. ("testUpdatable", testUpdatable),
  216. ("testRandomIV", testRandomIV)
  217. ]
  218. }