Files
swift-crypto/Tests/CryptoTests/Digests/DigestsTests.swift
T
+6 431013afaf Update to WWDC25 release (#417)
This change updates Crypto to WWDC25 equivalent code.

Each of these changes were individually merged to the WWDC25 branch.
This includes:

- #359
- #363
- #365
- #367
- #368
- #397
- #403
- #410
- #407
- #401
- #415

---------

Co-authored-by: YourMJK <37852512+YourMJK@users.noreply.github.com>
Co-authored-by: Si Beaumont <beaumont@apple.com>
Co-authored-by: Rick Newton-Rogers <rnro@apple.com>
Co-authored-by: Raphael <josephnoir@users.noreply.github.com>
Co-authored-by: Evan Wilde <ewilde@apple.com>
Co-authored-by: Tim Condon <0xTim@users.noreply.github.com>
Co-authored-by: Gus Cairo <gus.cairo@apple.com>
Co-authored-by: George Barnett <gbarnett@apple.com>
Co-authored-by: Fabrice de Gans <Steelskin@users.noreply.github.com>
Co-authored-by: Wojciech Nagrodzki <278594+wnagrodzki@users.noreply.github.com>
Co-authored-by: Jesse L. Zamora <xtremekforever@gmail.com>
Co-authored-by: aryan-25 <aryan_shah@apple.com>
Co-authored-by: Gwynne Raskind <gwynne@vapor.codes>
Co-authored-by: 3405691582 <dsk@google.com>
Co-authored-by: Paul Toffoloni <69189821+ptoffy@users.noreply.github.com>
2025-09-26 11:55:11 +01:00

181 lines
8.2 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftCrypto open source project
//
// Copyright (c) 2019-2020 Apple Inc. and the SwiftCrypto project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftCrypto project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import XCTest
#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
import Crypto
#elseif !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
import CryptoKit
#else
import Crypto
#endif
enum TestError: Error {
case unhandled
}
func nullTestVectorForAlgorithm<H: HashFunction>(hashFunction: H.Type) throws -> String {
#if !CRYPTOKIT_IN_SEP
#if !CRYPTO_IN_SWIFTPM || canImport(CryptoKit, _version: 324.0.4)
if #available(iOS 19.0, macOS 16.0, watchOS 12.0, tvOS 19.0, macCatalyst 19.0, visionOS 2.0, *) {
switch H.self {
case is SHA3_256.Type: return "a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a"
case is SHA3_384.Type: return "0c63a75b845e4f7d01107d852e4c2485c51a50aaaa94fc61995e71bbee983a2ac3713831264adb47fb6bd1e058d5f004"
case is SHA3_512.Type: return "a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a615b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26"
default:
break
}
}
#endif // !CRYPTO_IN_SWIFTPM || canImport(CryptoKit, _version: 324.0.4)
#endif // !CRYPTOKIT_IN_SEP
switch H.self {
case is Insecure.SHA1.Type: return "da39a3ee5e6b4b0d3255bfef95601890afd80709"
case is SHA256.Type: return "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
case is SHA384.Type: return "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b"
case is SHA512.Type: return "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e"
default:
XCTFail("Unhandled type: \(H.self)")
throw TestError.unhandled
}
}
func testVectorForAlgorithm<H: HashFunction>(hashFunction: H.Type) throws -> String {
#if !CRYPTOKIT_IN_SEP
#if !CRYPTO_IN_SWIFTPM || canImport(CryptoKit, _version: 324.0.4)
if #available(iOS 19.0, macOS 16.0, watchOS 12.0, tvOS 19.0, macCatalyst 19.0, visionOS 2.0, *) {
switch H.self {
case is SHA3_256.Type: return "916f6061fe879741ca6469b43971dfdb28b1a32dc36cb3254e812be27aad1d18"
case is SHA3_384.Type: return "79407d3b5916b59c3e30b09822974791c313fb9ecc849e406f23592d04f625dc8c709b98b43b3852b337216179aa7fc7"
case is SHA3_512.Type: return "afebb2ef542e6579c50cad06d2e578f9f8dd6881d7dc824d26360feebf18a4fa73e3261122948efcfd492e74e82e2189ed0fb440d187f382270cb455f21dd185"
default:
break
}
}
#endif // !CRYPTO_IN_SWIFTPM || canImport(CryptoKit, _version: 324.0.4)
#endif // !CRYPTOKIT_IN_SEP
switch H.self {
case is Insecure.SHA1.Type: return "a49b2446a02c645bf419f995b67091253a04a259"
case is SHA256.Type: return "cf5b16a778af8380036ce59e7b0492370b249b11e8f07a51afac45037afee9d1"
case is SHA384.Type: return "09330c33f71147e83d192fc782cd1b4753111b173b3b05d22fa08086e3b0f712fcc7c71a557e2db966c3e9fa91746039"
case is SHA512.Type: return "8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909"
default:
XCTFail("Unhandled type: \(H.self)")
throw TestError.unhandled
}
}
class DigestsTests: XCTestCase {
func assertHashFunctionWithVector<H: HashFunction>(hf: H.Type, data: Data, testVector: String, file: StaticString = (#filePath), line: UInt = #line) throws {
var h = hf.init()
h.update(data: data)
let result = h.finalize()
let testBytes = try orFail(file: file, line: line) { try Array(hexString: testVector) }
XCTAssertEqual(testBytes, Array(result), file: file, line: line)
XCTAssertEqual(Array(H.hash(data: data)), testBytes, file: file, line: line)
let (contiguousResult, discontiguousResult) = testBytes.asDataProtocols()
XCTAssert(result == contiguousResult, file: file, line: line)
XCTAssert(result == discontiguousResult, file: file, line: line)
XCTAssertFalse(result == DispatchData.empty, file: file, line: line)
}
func testMD5() throws {
XCTAssertEqual(Data(Insecure.MD5.hash(data: Data())).count, Insecure.MD5.byteCount)
XCTAssertEqual(
Data(Insecure.MD5.hash(data: Data())),
try Data(hexString: "d41d8cd98f00b204e9800998ecf8427e")
)
XCTAssertEqual(
Data(Insecure.MD5.hash(data: Data("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq".utf8))),
try Data(hexString: "8215ef0796a20bcaaae116d3876c664a")
)
}
func testHashFunction<H: HashFunction>(hf: H.Type) throws {
let data = ("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu".data(using: String.Encoding.ascii)!)
try orFail { try assertHashFunctionWithVector(hf: hf, data: data, testVector: try testVectorForAlgorithm(hashFunction: hf)) }
try orFail { try assertHashFunctionWithVector(hf: hf, data: Data(repeating: 0, count: 0), testVector: try nullTestVectorForAlgorithm(hashFunction: hf)) }
}
func testHashFunctions() throws {
try orFail { try testHashFunction(hf: Insecure.SHA1.self) }
try orFail { try testHashFunction(hf: SHA256.self) }
try orFail { try testHashFunction(hf: SHA384.self) }
try orFail { try testHashFunction(hf: SHA512.self) }
#if !CRYPTOKIT_IN_SEP
#if !CRYPTO_IN_SWIFTPM || canImport(CryptoKit, _version: 324.0.4)
if #available(iOS 19.0, macOS 16.0, watchOS 12.0, tvOS 19.0, macCatalyst 19.0, visionOS 2.0, *) {
try orFail { try testHashFunction(hf: SHA3_256.self) }
try orFail { try testHashFunction(hf: SHA3_384.self) }
try orFail { try testHashFunction(hf: SHA3_512.self) }
}
#endif // !CRYPTO_IN_SWIFTPM || canImport(CryptoKit, _version: 324.0.4)
#endif // !CRYPTOKIT_IN_SEP
}
func testHashFunctionImplementsCoW<H: HashFunction>(hf: H.Type) throws {
var hf = H()
hf.update(data: [1, 2, 3, 4])
var hfCopy = hf
hf.update(data: [5, 6, 7, 8])
let digest = hf.finalize()
hfCopy.update(data: [5, 6, 7, 8])
let copyDigest = hfCopy.finalize()
XCTAssertEqual(digest, copyDigest)
}
func testHashFunctionsImplementCow() throws {
try orFail { try testHashFunctionImplementsCoW(hf: Insecure.MD5.self) }
try orFail { try testHashFunctionImplementsCoW(hf: Insecure.SHA1.self) }
try orFail { try testHashFunctionImplementsCoW(hf: SHA256.self) }
try orFail { try testHashFunctionImplementsCoW(hf: SHA384.self) }
try orFail { try testHashFunctionImplementsCoW(hf: SHA512.self) }
#if !CRYPTOKIT_IN_SEP
#if !CRYPTO_IN_SWIFTPM || canImport(CryptoKit, _version: 324.0.4)
if #available(iOS 19.0, macOS 16.0, watchOS 12.0, tvOS 19.0, macCatalyst 19.0, visionOS 2.0, *) {
try orFail { try testHashFunctionImplementsCoW(hf: SHA3_256.self) }
try orFail { try testHashFunctionImplementsCoW(hf: SHA3_384.self) }
try orFail { try testHashFunctionImplementsCoW(hf: SHA3_512.self) }
}
#endif // !CRYPTO_IN_SWIFTPM || canImport(CryptoKit, _version: 324.0.4)
#endif // !CRYPTOKIT_IN_SEP
}
func testBlockSizes() {
XCTAssertEqual(Insecure.MD5.blockByteCount, 64)
XCTAssertEqual(Insecure.SHA1.blockByteCount, 64)
XCTAssertEqual(SHA256.blockByteCount, 64)
XCTAssertEqual(SHA384.blockByteCount, 128)
XCTAssertEqual(SHA512.blockByteCount, 128)
#if !CRYPTOKIT_IN_SEP
#if !CRYPTO_IN_SWIFTPM || canImport(CryptoKit, _version: 324.0.4)
if #available(iOS 19.0, macOS 16.0, watchOS 12.0, tvOS 19.0, macCatalyst 19.0, visionOS 2.0, *) {
XCTAssertEqual(SHA3_256.blockByteCount, 136)
XCTAssertEqual(SHA3_384.blockByteCount, 104)
XCTAssertEqual(SHA3_512.blockByteCount, 72)
}
#endif // !CRYPTO_IN_SWIFTPM || canImport(CryptoKit, _version: 324.0.4)
#endif // !CRYPTOKIT_IN_SEP
}
}