RabbitTests.swift 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. //
  2. // CryptoSwift
  3. //
  4. // Copyright (C) 2014-2017 Marcin Krzyżanowski <marcin@krzyzanowskim.com>
  5. // This software is provided 'as-is', without any express or implied warranty.
  6. //
  7. // In no event will the authors be held liable for any damages arising from the use of this software.
  8. //
  9. // Permission is granted to anyone to use this software for any purpose,including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:
  10. //
  11. // - The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation is required.
  12. // - Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  13. // - This notice may not be removed or altered from any source or binary distribution.
  14. //
  15. @testable import CryptoSwift
  16. import XCTest
  17. class RabbitTests: XCTestCase {
  18. func testInitialization() {
  19. var key = Array<UInt8>(repeating: 0, count: Rabbit.keySize - 1)
  20. var iv: Array<UInt8>?
  21. XCTAssertThrowsError(try Rabbit(key: key, iv: iv))
  22. key = Array<UInt8>(repeating: 0, count: Rabbit.keySize + 1)
  23. XCTAssertThrowsError(try Rabbit(key: key, iv: iv))
  24. key = Array<UInt8>(repeating: 0, count: Rabbit.keySize)
  25. XCTAssertNotNil(try Rabbit(key: key, iv: iv))
  26. iv = Array<UInt8>(repeating: 0, count: Rabbit.ivSize - 1)
  27. XCTAssertThrowsError(try Rabbit(key: key, iv: iv))
  28. iv = Array<UInt8>(repeating: 0, count: Rabbit.ivSize)
  29. XCTAssertNotNil(try Rabbit(key: key, iv: iv))
  30. }
  31. func testRabbitWithoutIV() {
  32. // Examples from Appendix A: Test Vectors in http://tools.ietf.org/rfc/rfc4503.txt
  33. let cases: [(Array<UInt8>, Array<UInt8>)] = [ // First case
  34. (
  35. [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
  36. [
  37. 0xb1, 0x57, 0x54, 0xf0, 0x36, 0xa5, 0xd6, 0xec, 0xf5, 0x6b, 0x45, 0x26, 0x1c, 0x4a, 0xf7, 0x02,
  38. 0x88, 0xe8, 0xd8, 0x15, 0xc5, 0x9c, 0x0c, 0x39, 0x7b, 0x69, 0x6c, 0x47, 0x89, 0xc6, 0x8a, 0xa7,
  39. 0xf4, 0x16, 0xa1, 0xc3, 0x70, 0x0c, 0xd4, 0x51, 0xda, 0x68, 0xd1, 0x88, 0x16, 0x73, 0xd6, 0x96
  40. ]
  41. ),
  42. // Second case
  43. (
  44. [0x91, 0x28, 0x13, 0x29, 0x2e, 0x3d, 0x36, 0xfe, 0x3b, 0xfc, 0x62, 0xf1, 0xdc, 0x51, 0xc3, 0xac],
  45. [
  46. 0x3d, 0x2d, 0xf3, 0xc8, 0x3e, 0xf6, 0x27, 0xa1, 0xe9, 0x7f, 0xc3, 0x84, 0x87, 0xe2, 0x51, 0x9c,
  47. 0xf5, 0x76, 0xcd, 0x61, 0xf4, 0x40, 0x5b, 0x88, 0x96, 0xbf, 0x53, 0xaa, 0x85, 0x54, 0xfc, 0x19,
  48. 0xe5, 0x54, 0x74, 0x73, 0xfb, 0xdb, 0x43, 0x50, 0x8a, 0xe5, 0x3b, 0x20, 0x20, 0x4d, 0x4c, 0x5e
  49. ]
  50. ),
  51. // Third case
  52. (
  53. [0x83, 0x95, 0x74, 0x15, 0x87, 0xe0, 0xc7, 0x33, 0xe9, 0xe9, 0xab, 0x01, 0xc0, 0x9b, 0x00, 0x43],
  54. [
  55. 0x0c, 0xb1, 0x0d, 0xcd, 0xa0, 0x41, 0xcd, 0xac, 0x32, 0xeb, 0x5c, 0xfd, 0x02, 0xd0, 0x60, 0x9b,
  56. 0x95, 0xfc, 0x9f, 0xca, 0x0f, 0x17, 0x01, 0x5a, 0x7b, 0x70, 0x92, 0x11, 0x4c, 0xff, 0x3e, 0xad,
  57. 0x96, 0x49, 0xe5, 0xde, 0x8b, 0xfc, 0x7f, 0x3f, 0x92, 0x41, 0x47, 0xad, 0x3a, 0x94, 0x74, 0x28
  58. ]
  59. )
  60. ]
  61. let plainText = Array<UInt8>(repeating: 0, count: 48)
  62. for (key, expectedCipher) in cases {
  63. let rabbit = try! Rabbit(key: key)
  64. let cipherText = try! rabbit.encrypt(plainText)
  65. XCTAssertEqual(cipherText, expectedCipher)
  66. XCTAssertEqual(try! rabbit.decrypt(cipherText), plainText)
  67. }
  68. }
  69. func testRabbitWithIV() {
  70. // Examples from Appendix A: Test Vectors in http://tools.ietf.org/rfc/rfc4503.txt
  71. let key = Array<UInt8>(repeating: 0, count: Rabbit.keySize)
  72. let cases: [(Array<UInt8>, Array<UInt8>)] = [
  73. (
  74. [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
  75. [
  76. 0xc6, 0xa7, 0x27, 0x5e, 0xf8, 0x54, 0x95, 0xd8, 0x7c, 0xcd, 0x5d, 0x37, 0x67, 0x05, 0xb7, 0xed,
  77. 0x5f, 0x29, 0xa6, 0xac, 0x04, 0xf5, 0xef, 0xd4, 0x7b, 0x8f, 0x29, 0x32, 0x70, 0xdc, 0x4a, 0x8d,
  78. 0x2a, 0xde, 0x82, 0x2b, 0x29, 0xde, 0x6c, 0x1e, 0xe5, 0x2b, 0xdb, 0x8a, 0x47, 0xbf, 0x8f, 0x66
  79. ]
  80. ),
  81. (
  82. [0xc3, 0x73, 0xf5, 0x75, 0xc1, 0x26, 0x7e, 0x59],
  83. [
  84. 0x1f, 0xcd, 0x4e, 0xb9, 0x58, 0x00, 0x12, 0xe2, 0xe0, 0xdc, 0xcc, 0x92, 0x22, 0x01, 0x7d, 0x6d,
  85. 0xa7, 0x5f, 0x4e, 0x10, 0xd1, 0x21, 0x25, 0x01, 0x7b, 0x24, 0x99, 0xff, 0xed, 0x93, 0x6f, 0x2e,
  86. 0xeb, 0xc1, 0x12, 0xc3, 0x93, 0xe7, 0x38, 0x39, 0x23, 0x56, 0xbd, 0xd0, 0x12, 0x02, 0x9b, 0xa7
  87. ]
  88. ),
  89. (
  90. [0xa6, 0xeb, 0x56, 0x1a, 0xd2, 0xf4, 0x17, 0x27],
  91. [
  92. 0x44, 0x5a, 0xd8, 0xc8, 0x05, 0x85, 0x8d, 0xbf, 0x70, 0xb6, 0xaf, 0x23, 0xa1, 0x51, 0x10, 0x4d,
  93. 0x96, 0xc8, 0xf2, 0x79, 0x47, 0xf4, 0x2c, 0x5b, 0xae, 0xae, 0x67, 0xc6, 0xac, 0xc3, 0x5b, 0x03,
  94. 0x9f, 0xcb, 0xfc, 0x89, 0x5f, 0xa7, 0x1c, 0x17, 0x31, 0x3d, 0xf0, 0x34, 0xf0, 0x15, 0x51, 0xcb
  95. ]
  96. )
  97. ]
  98. let plainText = Array<UInt8>(repeating: 0, count: 48)
  99. for (iv, expectedCipher) in cases {
  100. let rabbit = try! Rabbit(key: key, iv: iv)
  101. let cipherText = try! rabbit.encrypt(plainText)
  102. XCTAssertEqual(cipherText, expectedCipher)
  103. XCTAssertEqual(try! rabbit.decrypt(cipherText), plainText)
  104. }
  105. }
  106. }
  107. extension RabbitTests {
  108. static func allTests() -> [(String, (RabbitTests) -> () -> Void)] {
  109. let tests = [
  110. ("testInitialization", testInitialization),
  111. ("testRabbitWithoutIV", testRabbitWithoutIV),
  112. ("testRabbitWithIV", testRabbitWithIV)
  113. ]
  114. return tests
  115. }
  116. }