RabbitTests.swift 5.9 KB

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