|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the SwiftCrypto open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2023 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 | +import Foundation |
| 15 | +import XCTest |
| 16 | +import Crypto |
| 17 | +import _CryptoExtras |
| 18 | + |
| 19 | +class ChaCha20CTRTests: XCTestCase { |
| 20 | + |
| 21 | + /// Test Vector - https://datatracker.ietf.org/doc/html/rfc9001#name-chacha20-poly1305-short-hea |
| 22 | + func testChaCha20CTR_v1() throws { |
| 23 | + let hpKey = try Array(hexString: "25a282b9e82f06f21f488917a4fc8f1b73573685608597d0efcb076b0ab7a7a4") |
| 24 | + /// Sample = 0x5e5cd55c41f69080575d7999c25a5bfb |
| 25 | + let counterAsData = try Array(hexString: "5e5cd55c") |
| 26 | + let counterAsUInt32 = UInt32(bigEndian: 0x5e5cd55c) |
| 27 | + let iv = try Array(hexString: "41f69080575d7999c25a5bfb") |
| 28 | + |
| 29 | + let mask: Data = try Insecure.ChaCha20CTR.encrypt(Array<UInt8>(repeating: 0, count: 5), using: SymmetricKey(data: hpKey), counter: Insecure.ChaCha20CTR.Counter(data: counterAsData), nonce: Insecure.ChaCha20CTR.Nonce(data: iv)) |
| 30 | + let mask2: Data = try Insecure.ChaCha20CTR.encrypt(Array<UInt8>(repeating: 0, count: 5), using: SymmetricKey(data: hpKey), counter: Insecure.ChaCha20CTR.Counter(offset: counterAsUInt32), nonce: Insecure.ChaCha20CTR.Nonce(data: iv)) |
| 31 | + |
| 32 | + XCTAssertEqual(mask, try Data(hexString: "aefefe7d03")) |
| 33 | + XCTAssertEqual(mask, mask2) |
| 34 | + } |
| 35 | + |
| 36 | + /// Test Vector - https://www.ietf.org/archive/id/draft-ietf-quic-v2-10.html#name-chacha20-poly1305-short-head |
| 37 | + func testChaCha20CTR_v2() throws { |
| 38 | + let hpKey = try Array(hexString: "d659760d2ba434a226fd37b35c69e2da8211d10c4f12538787d65645d5d1b8e2") |
| 39 | + /// Sample = 0xe7b6b932bc27d786f4bc2bb20f2162ba |
| 40 | + let counterAsData = try Array(hexString: "e7b6b932") |
| 41 | + let counterAsUInt32 = UInt32(bigEndian: 0xe7b6b932) |
| 42 | + let iv = try Array(hexString: "bc27d786f4bc2bb20f2162ba") |
| 43 | + |
| 44 | + let mask: Data = try Insecure.ChaCha20CTR.encrypt(Array<UInt8>(repeating: 0, count: 5), using: SymmetricKey(data: hpKey), counter: Insecure.ChaCha20CTR.Counter(data: counterAsData), nonce: Insecure.ChaCha20CTR.Nonce(data: iv)) |
| 45 | + let mask2: Data = try Insecure.ChaCha20CTR.encrypt(Array<UInt8>(repeating: 0, count: 5), using: SymmetricKey(data: hpKey), counter: Insecure.ChaCha20CTR.Counter(offset: counterAsUInt32), nonce: Insecure.ChaCha20CTR.Nonce(data: iv)) |
| 46 | + |
| 47 | + XCTAssertEqual(mask, try Data(hexString: "97580e32bf")) |
| 48 | + XCTAssertEqual(mask, mask2) |
| 49 | + } |
| 50 | + |
| 51 | + func testChaCha20CTR_InvalidParameters() throws { |
| 52 | + let keyTooLong: SymmetricKey = SymmetricKey(data: [214, 89, 118, 13, 43, 164, 52, 162, 38, 253, 55, 179, 92, 105, 226, 218, 130, 17, 209, 12, 79, 18, 83, 135, 135, 214, 86, 69, 213, 209, 184, 226, 22]) |
| 53 | + XCTAssertThrowsError(try Insecure.ChaCha20CTR.encrypt(Array<UInt8>(repeating: 0, count: 5), using: keyTooLong, nonce: Insecure.ChaCha20CTR.Nonce())) { error in |
| 54 | + guard case CryptoKitError.incorrectKeySize = error else { return XCTFail("Error thrown was of unexpected type: \(error)") } |
| 55 | + } |
| 56 | + |
| 57 | + let keyTooShort: SymmetricKey = SymmetricKey(data: [214, 89, 118, 13, 43, 164, 52, 162, 38, 253, 55, 179, 92, 105, 226, 218, 130, 17, 209, 12, 79, 18, 83, 135, 135, 214, 86, 69, 213, 209, 184]) |
| 58 | + XCTAssertThrowsError(try Insecure.ChaCha20CTR.encrypt(Array<UInt8>(repeating: 0, count: 5), using: keyTooShort, nonce: Insecure.ChaCha20CTR.Nonce())) { error in |
| 59 | + guard case CryptoKitError.incorrectKeySize = error else { return XCTFail("Error thrown was of unexpected type: \(error)") } |
| 60 | + } |
| 61 | + |
| 62 | + let nonceTooLong: [UInt8] = [188, 39, 215, 134, 244, 188, 43, 178, 15, 33, 98, 186, 14] |
| 63 | + XCTAssertThrowsError(try Insecure.ChaCha20CTR.Nonce(data: nonceTooLong)) { error in |
| 64 | + guard case CryptoKitError.incorrectParameterSize = error else { return XCTFail("Error thrown was of unexpected type: \(error)") } |
| 65 | + } |
| 66 | + |
| 67 | + let nonceTooShort: [UInt8] = [188, 39, 215, 134, 244, 188, 43, 178, 15, 33, 98] |
| 68 | + XCTAssertThrowsError(try Insecure.ChaCha20CTR.Nonce(data: nonceTooShort)) { error in |
| 69 | + guard case CryptoKitError.incorrectParameterSize = error else { return XCTFail("Error thrown was of unexpected type: \(error)") } |
| 70 | + } |
| 71 | + |
| 72 | + let counterTooLong: [UInt8] = [231, 182, 185, 50, 82] |
| 73 | + XCTAssertThrowsError(try Insecure.ChaCha20CTR.Counter(data: counterTooLong)) { error in |
| 74 | + guard case CryptoKitError.incorrectParameterSize = error else { return XCTFail("Error thrown was of unexpected type: \(error)") } |
| 75 | + } |
| 76 | + |
| 77 | + let counterTooShort: [UInt8] = [231, 182, 185] |
| 78 | + XCTAssertThrowsError(try Insecure.ChaCha20CTR.Counter(data: counterTooShort)) { error in |
| 79 | + guard case CryptoKitError.incorrectParameterSize = error else { return XCTFail("Error thrown was of unexpected type: \(error)") } |
| 80 | + } |
| 81 | + |
| 82 | + let key: SymmetricKey = SymmetricKey(data: [214, 89, 118, 13, 43, 164, 52, 162, 38, 253, 55, 179, 92, 105, 226, 218, 130, 17, 209, 12, 79, 18, 83, 135, 135, 214, 86, 69, 213, 209, 184, 226]) |
| 83 | + |
| 84 | + // Ensure UInt32.max Counter Supported |
| 85 | + XCTAssertNoThrow(try Insecure.ChaCha20CTR.encrypt(Array<UInt8>(repeating: 0, count: 5), using: key, counter: Insecure.ChaCha20CTR.Counter(offset: UInt32.max), nonce: Insecure.ChaCha20CTR.Nonce())) |
| 86 | + |
| 87 | + // Assert that two calls with the same Counter + Nonce params results in the same output |
| 88 | + let nonce = Insecure.ChaCha20CTR.Nonce() |
| 89 | + let counter = Insecure.ChaCha20CTR.Counter() |
| 90 | + let ciphertext1 = try Insecure.ChaCha20CTR.encrypt(Array<UInt8>(repeating: 0, count: 5), using: key, counter: counter, nonce: nonce) |
| 91 | + let ciphertext2 = try Insecure.ChaCha20CTR.encrypt(Array<UInt8>(repeating: 0, count: 5), using: key, counter: counter, nonce: nonce) |
| 92 | + XCTAssertEqual(ciphertext1, ciphertext2) |
| 93 | + |
| 94 | + // Assert that two calls with different Nonce params results in different output |
| 95 | + let ciphertext3 = try Insecure.ChaCha20CTR.encrypt(Array<UInt8>(repeating: 0, count: 5), using: key, counter: counter, nonce: Insecure.ChaCha20CTR.Nonce()) |
| 96 | + let ciphertext4 = try Insecure.ChaCha20CTR.encrypt(Array<UInt8>(repeating: 0, count: 5), using: key, counter: counter, nonce: Insecure.ChaCha20CTR.Nonce()) |
| 97 | + XCTAssertNotEqual(ciphertext3, ciphertext4) |
| 98 | + } |
| 99 | +} |
0 commit comments