AESOCBTests.swift 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 Foundation
  16. import XCTest
  17. @testable import CryptoSwift
  18. final class OCBTests: XCTestCase {
  19. struct TestFixture {
  20. let N: Array<UInt8>
  21. let A: Array<UInt8>
  22. let P: Array<UInt8>
  23. let C: Array<UInt8>
  24. }
  25. func testAESOCBWithRFC7253Tests() {
  26. let K = Array<UInt8>(hex: "000102030405060708090A0B0C0D0E0F")
  27. let fixtures = [
  28. TestFixture(N: Array<UInt8>(hex: "BBAA99887766554433221100"),
  29. A: Array<UInt8>(hex: ""),
  30. P: Array<UInt8>(hex: ""),
  31. C: Array<UInt8>(hex: "785407BFFFC8AD9EDCC5520AC9111EE6")),
  32. TestFixture(N: Array<UInt8>(hex: "BBAA99887766554433221101"),
  33. A: Array<UInt8>(hex: "0001020304050607"),
  34. P: Array<UInt8>(hex: "0001020304050607"),
  35. C: Array<UInt8>(hex: "6820B3657B6F615A5725BDA0D3B4EB3A257C9AF1F8F03009")),
  36. TestFixture(N: Array<UInt8>(hex: "BBAA99887766554433221102"),
  37. A: Array<UInt8>(hex: "0001020304050607"),
  38. P: Array<UInt8>(hex: ""),
  39. C: Array<UInt8>(hex: "81017F8203F081277152FADE694A0A00")),
  40. TestFixture(N: Array<UInt8>(hex: "BBAA99887766554433221103"),
  41. A: Array<UInt8>(hex: ""),
  42. P: Array<UInt8>(hex: "0001020304050607"),
  43. C: Array<UInt8>(hex: "45DD69F8F5AAE72414054CD1F35D82760B2CD00D2F99BFA9")),
  44. TestFixture(N: Array<UInt8>(hex: "BBAA99887766554433221104"),
  45. A: Array<UInt8>(hex: "000102030405060708090A0B0C0D0E0F"),
  46. P: Array<UInt8>(hex: "000102030405060708090A0B0C0D0E0F"),
  47. C: Array<UInt8>(hex: "571D535B60B277188BE5147170A9A22C3AD7A4FF3835B8C5701C1CCEC8FC3358")),
  48. TestFixture(N: Array<UInt8>(hex: "BBAA99887766554433221105"),
  49. A: Array<UInt8>(hex: "000102030405060708090A0B0C0D0E0F"),
  50. P: Array<UInt8>(hex: ""),
  51. C: Array<UInt8>(hex: "8CF761B6902EF764462AD86498CA6B97")),
  52. TestFixture(N: Array<UInt8>(hex: "BBAA99887766554433221106"),
  53. A: Array<UInt8>(hex: ""),
  54. P: Array<UInt8>(hex: "000102030405060708090A0B0C0D0E0F"),
  55. C: Array<UInt8>(hex: "5CE88EC2E0692706A915C00AEB8B2396F40E1C743F52436BDF06D8FA1ECA343D")),
  56. TestFixture(N: Array<UInt8>(hex: "BBAA99887766554433221107"),
  57. A: Array<UInt8>(hex: "000102030405060708090A0B0C0D0E0F1011121314151617"),
  58. P: Array<UInt8>(hex: "000102030405060708090A0B0C0D0E0F1011121314151617"),
  59. C: Array<UInt8>(hex: "1CA2207308C87C010756104D8840CE1952F09673A448A122C92C62241051F57356D7F3C90BB0E07F")),
  60. TestFixture(N: Array<UInt8>(hex: "BBAA99887766554433221108"),
  61. A: Array<UInt8>(hex: "000102030405060708090A0B0C0D0E0F1011121314151617"),
  62. P: Array<UInt8>(hex: ""),
  63. C: Array<UInt8>(hex: "6DC225A071FC1B9F7C69F93B0F1E10DE")),
  64. TestFixture(N: Array<UInt8>(hex: "BBAA99887766554433221109"),
  65. A: Array<UInt8>(hex: ""),
  66. P: Array<UInt8>(hex: "000102030405060708090A0B0C0D0E0F1011121314151617"),
  67. C: Array<UInt8>(hex: "221BD0DE7FA6FE993ECCD769460A0AF2D6CDED0C395B1C3CE725F32494B9F914D85C0B1EB38357FF")),
  68. TestFixture(N: Array<UInt8>(hex: "BBAA9988776655443322110A"),
  69. A: Array<UInt8>(hex: "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F"),
  70. P: Array<UInt8>(hex: "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F"),
  71. C: Array<UInt8>(hex: "BD6F6C496201C69296C11EFD138A467ABD3C707924B964DEAFFC40319AF5A48540FBBA186C5553C68AD9F592A79A4240")),
  72. TestFixture(N: Array<UInt8>(hex: "BBAA9988776655443322110B"),
  73. A: Array<UInt8>(hex: "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F"),
  74. P: Array<UInt8>(hex: ""),
  75. C: Array<UInt8>(hex: "FE80690BEE8A485D11F32965BC9D2A32")),
  76. TestFixture(N: Array<UInt8>(hex: "BBAA9988776655443322110C"),
  77. A: Array<UInt8>(hex: ""),
  78. P: Array<UInt8>(hex: "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F"),
  79. C: Array<UInt8>(hex: "2942BFC773BDA23CABC6ACFD9BFD5835BD300F0973792EF46040C53F1432BCDFB5E1DDE3BC18A5F840B52E653444D5DF")),
  80. TestFixture(N: Array<UInt8>(hex: "BBAA9988776655443322110D"),
  81. A: Array<UInt8>(hex: "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F2021222324252627"),
  82. P: Array<UInt8>(hex: "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F2021222324252627"),
  83. C: Array<UInt8>(hex: "D5CA91748410C1751FF8A2F618255B68A0A12E093FF454606E59F9C1D0DDC54B65E8628E568BAD7AED07BA06A4A69483A7035490C5769E60")),
  84. TestFixture(N: Array<UInt8>(hex: "BBAA9988776655443322110E"),
  85. A: Array<UInt8>(hex: "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F2021222324252627"),
  86. P: Array<UInt8>(hex: ""),
  87. C: Array<UInt8>(hex: "C5CD9D1850C141E358649994EE701B68")),
  88. TestFixture(N: Array<UInt8>(hex: "BBAA9988776655443322110F"),
  89. A: Array<UInt8>(hex: ""),
  90. P: Array<UInt8>(hex: "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F2021222324252627"),
  91. C: Array<UInt8>(hex: "4412923493C57D5DE0D700F753CCE0D1D2D95060122E9F15A5DDBFC5787E50B5CC55EE507BCB084E479AD363AC366B95A98CA5F3000B1479")),
  92. ]
  93. func testEncrypt(fixture: TestFixture) -> Bool {
  94. let ocb = OCB(nonce: fixture.N, additionalAuthenticatedData: fixture.A, mode: .combined)
  95. let aes = try! AES(key: K, blockMode: ocb, padding: .noPadding)
  96. let encrypted = try! aes.encrypt(fixture.P)
  97. if encrypted != fixture.C {
  98. return false
  99. }
  100. return true
  101. }
  102. func testDecrypt(fixture: TestFixture) -> Bool {
  103. let ocb = OCB(nonce: fixture.N, additionalAuthenticatedData: fixture.A, mode: .combined)
  104. let aes = try! AES(key: K, blockMode: ocb, padding: .noPadding)
  105. let plaintext = try! aes.decrypt(fixture.C)
  106. if plaintext != fixture.P {
  107. return false
  108. }
  109. return true
  110. }
  111. func testInvalidTag(fixture: TestFixture) -> Bool {
  112. let ocb = OCB(nonce: fixture.N, additionalAuthenticatedData: fixture.A, mode: .combined)
  113. let aes = try! AES(key: K, blockMode: ocb, padding: .noPadding)
  114. var C_ = fixture.C.slice
  115. C_[C_.count - 1] ^= 0x01
  116. let plaintext = try? aes.decrypt(C_)
  117. return plaintext == nil
  118. }
  119. for (i, fixture) in fixtures.enumerated() {
  120. XCTAssertTrue(testEncrypt(fixture: fixture), "Encryption failed")
  121. XCTAssertTrue(testDecrypt(fixture: fixture), "(\(i) - Decryption failed.")
  122. XCTAssertTrue(testInvalidTag(fixture: fixture), "(\(i) - Invalid Tag verification failed.")
  123. }
  124. }
  125. static let allTests = [
  126. ("testAESOCBWithRFC7253Tests", testAESOCBWithRFC7253Tests),
  127. ]
  128. }