|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the SwiftCrypto open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2024 Apple Inc. and the SwiftCrypto project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.md for the list of SwiftCrypto project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +import Crypto |
| 16 | +@testable import _CryptoExtras |
| 17 | +import XCTest |
| 18 | + |
| 19 | +final class AES_CFBTests: XCTestCase { |
| 20 | + /// Test vectors from NIST, of the following form: |
| 21 | + /// ``` |
| 22 | + /// COUNT = 0 |
| 23 | + /// KEY = 00000000000000000000000000000000 |
| 24 | + /// IV = f34481ec3cc627bacd5dc3fb08f273e6 |
| 25 | + /// PLAINTEXT = 00000000000000000000000000000000 |
| 26 | + /// CIPHERTEXT = 0336763e966d92595a567cc9ce537f5e |
| 27 | + /// ``` |
| 28 | + /// —— source: https://csrc.nist.gov/projects/cryptographic-algorithm-validation-program/block-ciphers |
| 29 | + struct TestVector: Codable { |
| 30 | + var count: Int |
| 31 | + var key: [UInt8] |
| 32 | + var iv: [UInt8] |
| 33 | + var plaintext: [UInt8] |
| 34 | + var ciphertext: [UInt8] |
| 35 | + |
| 36 | + enum CodingKeys: String, CodingKey { |
| 37 | + case count = "COUNT" |
| 38 | + case key = "KEY" |
| 39 | + case iv = "IV" |
| 40 | + case plaintext = "PLAINTEXT" |
| 41 | + case ciphertext = "CIPHERTEXT" |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + func testVector(_ vector: TestVector) throws { |
| 46 | + let (contiguousPlaintextData, discontiguousPlaintextData) = vector.plaintext.asDataProtocols() |
| 47 | + for plaintextData in [contiguousPlaintextData as DataProtocol, discontiguousPlaintextData as DataProtocol] { |
| 48 | + let key = SymmetricKey(data: Data(vector.key)) |
| 49 | + let nonce = try AES._CFB.Nonce(nonceBytes: vector.iv) |
| 50 | + let ciphertext = try AES._CFB.encrypt(plaintextData, using: key, nonce: nonce) |
| 51 | + XCTAssertEqual(ciphertext, Data(vector.ciphertext)) |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + func testVectorsFrom(fileName: String) throws { |
| 56 | + var decoder = try RFCVectorDecoder(bundleType: self, fileName: fileName) |
| 57 | + let vectors = try decoder.decode([TestVector].self) |
| 58 | + for vector in vectors { |
| 59 | + try self.testVector(vector) |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + func testVectors() throws { |
| 64 | + try self.testVectorsFrom(fileName: "AESCFB128GFSbox128") |
| 65 | + try self.testVectorsFrom(fileName: "AESCFB128GFSbox128") |
| 66 | + try self.testVectorsFrom(fileName: "AESCFB128GFSbox128") |
| 67 | + try self.testVectorsFrom(fileName: "AESCFB128GFSbox192") |
| 68 | + try self.testVectorsFrom(fileName: "AESCFB128GFSbox256") |
| 69 | + try self.testVectorsFrom(fileName: "AESCFB128KeySbox128") |
| 70 | + try self.testVectorsFrom(fileName: "AESCFB128KeySbox192") |
| 71 | + try self.testVectorsFrom(fileName: "AESCFB128KeySbox256") |
| 72 | + try self.testVectorsFrom(fileName: "AESCFB128VarKey128") |
| 73 | + try self.testVectorsFrom(fileName: "AESCFB128VarKey192") |
| 74 | + try self.testVectorsFrom(fileName: "AESCFB128VarKey256") |
| 75 | + try self.testVectorsFrom(fileName: "AESCFB128VarTxt128") |
| 76 | + try self.testVectorsFrom(fileName: "AESCFB128VarTxt192") |
| 77 | + try self.testVectorsFrom(fileName: "AESCFB128VarTxt256") |
| 78 | + } |
| 79 | + |
| 80 | + func testRoundtrip() throws { |
| 81 | + let key = SymmetricKey(size: .bits128) |
| 82 | + let plaintext = Data(SystemRandomNumberGenerator.randomBytes(count: 1024)) |
| 83 | + let nonce = AES._CFB.Nonce() |
| 84 | + let ciphertext = try AES._CFB.encrypt(plaintext, using: key, nonce: nonce) |
| 85 | + XCTAssertEqual(try AES._CFB.decrypt(ciphertext, using: key, nonce: nonce), plaintext) |
| 86 | + } |
| 87 | + |
| 88 | + func testRejectsInvalidNonceSizes() throws { |
| 89 | + let someBytes = Array(repeating: UInt8(0), count: 24) |
| 90 | + |
| 91 | + for count in 0..<someBytes.count { |
| 92 | + let nonceBytes = someBytes.prefix(count) |
| 93 | + |
| 94 | + if count != 16 { |
| 95 | + XCTAssertThrowsError(try AES._CFB.Nonce(nonceBytes: nonceBytes)) |
| 96 | + } else { |
| 97 | + XCTAssertNoThrow(try AES._CFB.Nonce(nonceBytes: nonceBytes)) |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments