XChaCha20Poly1305Tests.swift 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. //
  2. // CryptoSwift
  3. //
  4. // Copyright (C) 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. import XCTest
  16. @testable import CryptoSwift
  17. class XChaCha20Poly1305Tests: XCTestCase {
  18. /// See: https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-xchacha#appendix-A.3.1
  19. func testRoundTrip() {
  20. do {
  21. let plaintext: Array<UInt8> = .init(
  22. hex: """
  23. 4c616469657320616e642047656e746c656d656e206f662074686520636c6173
  24. 73206f66202739393a204966204920636f756c64206f6666657220796f75206f
  25. 6e6c79206f6e652074697020666f7220746865206675747572652c2073756e73
  26. 637265656e20776f756c642062652069742e
  27. """.replacingOccurrences(of: "\n", with: "")
  28. )
  29. let aad: Array<UInt8> = .init(
  30. hex: "50515253c0c1c2c3c4c5c6c7")
  31. let key: Array<UInt8> = .init(
  32. hex: "808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9f")
  33. let iv: Array<UInt8> = .init(
  34. hex: "404142434445464748494a4b4c4d4e4f5051525354555657")
  35. let encryptResult = try AEADXChaCha20Poly1305.encrypt(
  36. plaintext,
  37. key: key,
  38. iv: iv,
  39. authenticationHeader: aad
  40. )
  41. XCTAssertEqual(
  42. encryptResult.cipherText.toHexString(),
  43. """
  44. bd6d179d3e83d43b9576579493c0e939572a1700252bfaccbed2902c21396cbb
  45. 731c7f1b0b4aa6440bf3a82f4eda7e39ae64c6708c54c216cb96b72e1213b452
  46. 2f8c9ba40db5d945b11b69b982c1bb9e3f3fac2bc369488f76b2383565d3fff9
  47. 21f9664c97637da9768812f615c68b13b52e
  48. """.replacingOccurrences(of: "\n", with: "")
  49. )
  50. XCTAssertEqual(
  51. encryptResult.authenticationTag.toHexString(),
  52. "c0875924c1c7987947deafd8780acf49"
  53. )
  54. let decryptResult = try AEADXChaCha20Poly1305.decrypt(
  55. encryptResult.cipherText,
  56. key: key,
  57. iv: iv,
  58. authenticationHeader: aad,
  59. authenticationTag: encryptResult.authenticationTag
  60. )
  61. XCTAssertEqual(decryptResult.plainText, plaintext)
  62. } catch {
  63. XCTFail(error.localizedDescription)
  64. }
  65. }
  66. static let allTests = [
  67. ("Test Vector for AEAD_XCHACHA20_POLY1305", testRoundTrip)
  68. ]
  69. }