RabbitTests.swift 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //
  2. // RabbitTests.swift
  3. // CryptoSwift
  4. //
  5. // Created by Dima Kalachov on 13/11/15.
  6. // Copyright © 2015 Marcin Krzyzanowski. All rights reserved.
  7. //
  8. import XCTest
  9. @testable import CryptoSwift
  10. class RabbitTests: XCTestCase {
  11. func testInitialization() {
  12. var key = Array<UInt8>(repeating: 0, count: Rabbit.keySize - 1)
  13. var iv: Array<UInt8>?
  14. XCTAssertThrowsError(try Rabbit(key: key, iv: iv))
  15. key = Array<UInt8>(repeating: 0, count: Rabbit.keySize + 1)
  16. XCTAssertThrowsError(try Rabbit(key: key, iv: iv))
  17. key = Array<UInt8>(repeating: 0, count: Rabbit.keySize)
  18. XCTAssertNotNil(try Rabbit(key: key, iv: iv))
  19. iv = Array<UInt8>(repeating: 0, count: Rabbit.ivSize - 1)
  20. XCTAssertThrowsError(try Rabbit(key: key, iv: iv))
  21. iv = Array<UInt8>(repeating: 0, count: Rabbit.ivSize)
  22. XCTAssertNotNil(try Rabbit(key: key, iv: iv))
  23. }
  24. func testRabbitWithoutIV() {
  25. // Examples from Appendix A: Test Vectors in http://tools.ietf.org/rfc/rfc4503.txt
  26. let cases: [(Array<UInt8>, Array<UInt8>)] = [ // First case
  27. (
  28. [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
  29. [
  30. 0xb1, 0x57, 0x54, 0xf0, 0x36, 0xa5, 0xd6, 0xec, 0xf5, 0x6b, 0x45, 0x26, 0x1c, 0x4a, 0xf7, 0x02,
  31. 0x88, 0xe8, 0xd8, 0x15, 0xc5, 0x9c, 0x0c, 0x39, 0x7b, 0x69, 0x6c, 0x47, 0x89, 0xc6, 0x8a, 0xa7,
  32. 0xf4, 0x16, 0xa1, 0xc3, 0x70, 0x0c, 0xd4, 0x51, 0xda, 0x68, 0xd1, 0x88, 0x16, 0x73, 0xd6, 0x96,
  33. ]
  34. ),
  35. // Second case
  36. (
  37. [0x91, 0x28, 0x13, 0x29, 0x2e, 0x3d, 0x36, 0xfe, 0x3b, 0xfc, 0x62, 0xf1, 0xdc, 0x51, 0xc3, 0xac],
  38. [
  39. 0x3d, 0x2d, 0xf3, 0xc8, 0x3e, 0xf6, 0x27, 0xa1, 0xe9, 0x7f, 0xc3, 0x84, 0x87, 0xe2, 0x51, 0x9c,
  40. 0xf5, 0x76, 0xcd, 0x61, 0xf4, 0x40, 0x5b, 0x88, 0x96, 0xbf, 0x53, 0xaa, 0x85, 0x54, 0xfc, 0x19,
  41. 0xe5, 0x54, 0x74, 0x73, 0xfb, 0xdb, 0x43, 0x50, 0x8a, 0xe5, 0x3b, 0x20, 0x20, 0x4d, 0x4c, 0x5e,
  42. ]
  43. ),
  44. // Third case
  45. (
  46. [0x83, 0x95, 0x74, 0x15, 0x87, 0xe0, 0xc7, 0x33, 0xe9, 0xe9, 0xab, 0x01, 0xc0, 0x9b, 0x00, 0x43],
  47. [
  48. 0x0c, 0xb1, 0x0d, 0xcd, 0xa0, 0x41, 0xcd, 0xac, 0x32, 0xeb, 0x5c, 0xfd, 0x02, 0xd0, 0x60, 0x9b,
  49. 0x95, 0xfc, 0x9f, 0xca, 0x0f, 0x17, 0x01, 0x5a, 0x7b, 0x70, 0x92, 0x11, 0x4c, 0xff, 0x3e, 0xad,
  50. 0x96, 0x49, 0xe5, 0xde, 0x8b, 0xfc, 0x7f, 0x3f, 0x92, 0x41, 0x47, 0xad, 0x3a, 0x94, 0x74, 0x28,
  51. ]
  52. ),
  53. ]
  54. let plainText = Array<UInt8>(repeating: 0, count: 48)
  55. for (key, expectedCipher) in cases {
  56. let rabbit = try! Rabbit(key: key)
  57. let cipherText = try! rabbit.encrypt(plainText)
  58. XCTAssertEqual(cipherText, expectedCipher)
  59. XCTAssertEqual(try! rabbit.decrypt(cipherText), plainText)
  60. }
  61. }
  62. func testRabbitWithIV() {
  63. // Examples from Appendix A: Test Vectors in http://tools.ietf.org/rfc/rfc4503.txt
  64. let key = Array<UInt8>(repeating: 0, count: Rabbit.keySize)
  65. let cases: [(Array<UInt8>, Array<UInt8>)] = [
  66. (
  67. [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
  68. [
  69. 0xc6, 0xa7, 0x27, 0x5e, 0xf8, 0x54, 0x95, 0xd8, 0x7c, 0xcd, 0x5d, 0x37, 0x67, 0x05, 0xb7, 0xed,
  70. 0x5f, 0x29, 0xa6, 0xac, 0x04, 0xf5, 0xef, 0xd4, 0x7b, 0x8f, 0x29, 0x32, 0x70, 0xdc, 0x4a, 0x8d,
  71. 0x2a, 0xde, 0x82, 0x2b, 0x29, 0xde, 0x6c, 0x1e, 0xe5, 0x2b, 0xdb, 0x8a, 0x47, 0xbf, 0x8f, 0x66,
  72. ]
  73. ),
  74. (
  75. [0xc3, 0x73, 0xf5, 0x75, 0xc1, 0x26, 0x7e, 0x59],
  76. [
  77. 0x1f, 0xcd, 0x4e, 0xb9, 0x58, 0x00, 0x12, 0xe2, 0xe0, 0xdc, 0xcc, 0x92, 0x22, 0x01, 0x7d, 0x6d,
  78. 0xa7, 0x5f, 0x4e, 0x10, 0xd1, 0x21, 0x25, 0x01, 0x7b, 0x24, 0x99, 0xff, 0xed, 0x93, 0x6f, 0x2e,
  79. 0xeb, 0xc1, 0x12, 0xc3, 0x93, 0xe7, 0x38, 0x39, 0x23, 0x56, 0xbd, 0xd0, 0x12, 0x02, 0x9b, 0xa7,
  80. ]
  81. ),
  82. (
  83. [0xa6, 0xeb, 0x56, 0x1a, 0xd2, 0xf4, 0x17, 0x27],
  84. [
  85. 0x44, 0x5a, 0xd8, 0xc8, 0x05, 0x85, 0x8d, 0xbf, 0x70, 0xb6, 0xaf, 0x23, 0xa1, 0x51, 0x10, 0x4d,
  86. 0x96, 0xc8, 0xf2, 0x79, 0x47, 0xf4, 0x2c, 0x5b, 0xae, 0xae, 0x67, 0xc6, 0xac, 0xc3, 0x5b, 0x03,
  87. 0x9f, 0xcb, 0xfc, 0x89, 0x5f, 0xa7, 0x1c, 0x17, 0x31, 0x3d, 0xf0, 0x34, 0xf0, 0x15, 0x51, 0xcb,
  88. ]
  89. ),
  90. ]
  91. let plainText = Array<UInt8>(repeating: 0, count: 48)
  92. for (iv, expectedCipher) in cases {
  93. let rabbit = try! Rabbit(key: key, iv: iv)
  94. let cipherText = try! rabbit.encrypt(plainText)
  95. XCTAssertEqual(cipherText, expectedCipher)
  96. XCTAssertEqual(try! rabbit.decrypt(cipherText), plainText)
  97. }
  98. }
  99. }
  100. #if !CI
  101. extension RabbitTests {
  102. func testRabbitPerformance() {
  103. let key: Array<UInt8> = Array<UInt8>(repeating: 0, count: Rabbit.keySize)
  104. let iv: Array<UInt8> = Array<UInt8>(repeating: 0, count: Rabbit.ivSize)
  105. let message = Array<UInt8>(repeating: 7, count: (1024 * 1024) * 1)
  106. measure {
  107. _ = try! Rabbit(key: key, iv: iv).encrypt(message)
  108. }
  109. }
  110. }
  111. #endif
  112. extension RabbitTests {
  113. static func allTests() -> [(String, (RabbitTests) -> () -> Void)] {
  114. var tests = [
  115. ("testInitialization", testInitialization),
  116. ("testRabbitWithoutIV", testRabbitWithoutIV),
  117. ("testRabbitWithIV", testRabbitWithIV),
  118. ]
  119. #if !CI
  120. tests += [("testRabbitPerformance", testRabbitPerformance)]
  121. #endif
  122. return tests
  123. }
  124. }