AESOCBTests.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 OCBTests: XCTestCase {
  18. struct TestFixture {
  19. let K: Array<UInt8>
  20. let N: Array<UInt8>
  21. let P: Array<UInt8>
  22. let C: Array<UInt8>
  23. }
  24. func testAESOCBWithRFC7253Tests() {
  25. var fixtures = [
  26. TestFixture(K: Array<UInt8>(hex: "000102030405060708090A0B0C0D0E0F"),
  27. N: Array<UInt8>(hex: "BBAA99887766554433221100"),
  28. P: Array<UInt8>(hex: ""),
  29. C: Array<UInt8>(hex: "785407BFFFC8AD9EDCC5520AC9111EE6")),
  30. TestFixture(K: Array<UInt8>(hex: "000102030405060708090A0B0C0D0E0F"),
  31. N: Array<UInt8>(hex: "BBAA99887766554433221103"),
  32. P: Array<UInt8>(hex: "0001020304050607"),
  33. C: Array<UInt8>(hex: "45DD69F8F5AAE72414054CD1F35D82760B2CD00D2F99BFA9")),
  34. TestFixture(K: Array<UInt8>(hex: "000102030405060708090A0B0C0D0E0F"),
  35. N: Array<UInt8>(hex: "BBAA99887766554433221106"),
  36. P: Array<UInt8>(hex: "000102030405060708090A0B0C0D0E0F"),
  37. C: Array<UInt8>(hex: "5CE88EC2E0692706A915C00AEB8B2396F40E1C743F52436BDF06D8FA1ECA343D")),
  38. TestFixture(K: Array<UInt8>(hex: "000102030405060708090A0B0C0D0E0F"),
  39. N: Array<UInt8>(hex: "BBAA99887766554433221109"),
  40. P: Array<UInt8>(hex: "000102030405060708090A0B0C0D0E0F1011121314151617"),
  41. C: Array<UInt8>(hex: "221BD0DE7FA6FE993ECCD769460A0AF2D6CDED0C395B1C3CE725F32494B9F914D85C0B1EB38357FF")),
  42. TestFixture(K: Array<UInt8>(hex: "000102030405060708090A0B0C0D0E0F"),
  43. N: Array<UInt8>(hex: "BBAA9988776655443322110C"),
  44. P: Array<UInt8>(hex: "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F"),
  45. C: Array<UInt8>(hex: "2942BFC773BDA23CABC6ACFD9BFD5835BD300F0973792EF46040C53F1432BCDFB5E1DDE3BC18A5F840B52E653444D5DF")),
  46. TestFixture(K: Array<UInt8>(hex: "000102030405060708090A0B0C0D0E0F"),
  47. N: Array<UInt8>(hex: "BBAA9988776655443322110F"),
  48. P: Array<UInt8>(hex: "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F2021222324252627"),
  49. C: Array<UInt8>(hex: "4412923493C57D5DE0D700F753CCE0D1D2D95060122E9F15A5DDBFC5787E50B5CC55EE507BCB084E479AD363AC366B95A98CA5F3000B1479")),
  50. ]
  51. func testEncrypt(fixture: TestFixture) -> Bool {
  52. let ocb = OCB(nonce: fixture.N, mode: .combined)
  53. let aes = try! AES(key: fixture.K, blockMode: ocb, padding: .noPadding)
  54. let encrypted = try! aes.encrypt(fixture.P)
  55. if encrypted != fixture.C {
  56. return false
  57. }
  58. return true
  59. }
  60. func testDecrypt(fixture: TestFixture) -> Bool {
  61. let ocb = OCB(nonce: fixture.N, mode: .combined)
  62. let aes = try! AES(key: fixture.K, blockMode: ocb, padding: .noPadding)
  63. let plaintext = try! aes.decrypt(fixture.C)
  64. if plaintext != fixture.P {
  65. return false
  66. }
  67. return true
  68. }
  69. func testInvalidTag(fixture: TestFixture) -> Bool {
  70. let ocb = OCB(nonce: fixture.N, mode: .combined)
  71. let aes = try! AES(key: fixture.K, blockMode: ocb, padding: .noPadding)
  72. var C_ = fixture.C.slice
  73. C_[C_.count - 1] ^= 0x01
  74. let plaintext = try? aes.decrypt(C_)
  75. return plaintext == nil
  76. }
  77. for (i, fixture) in fixtures.enumerated() {
  78. XCTAssertTrue(testEncrypt(fixture: fixture), "Encryption failed")
  79. XCTAssertTrue(testDecrypt(fixture: fixture), "(\(i) - Decryption failed.")
  80. XCTAssertTrue(testInvalidTag(fixture: fixture), "(\(i) - Invalid Tag verification failed.")
  81. }
  82. }
  83. static let allTests = [
  84. ("testAESOCBWithRFC7253Tests", testAESOCBWithRFC7253Tests),
  85. ]
  86. }