mirror of
https://github.com/apple/swift-crypto
synced 2026-11-22 04:30:39 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e15b6234fe | ||
|
|
3280a5f728 | ||
|
|
1fa43495bc | ||
|
|
9f96b82711 | ||
|
|
281a92df6c | ||
|
|
32adcc5038 | ||
|
|
1586608eee | ||
|
|
f8fad2f95a |
No files matched your search
@@ -48,6 +48,10 @@ jobs:
|
||||
|
||||
cmake-checks:
|
||||
name: CMake checks
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
swift_version: ["6.2", "6.3", "6.4"]
|
||||
uses: apple/swift-nio/.github/workflows/cmake_tests.yml@main
|
||||
with:
|
||||
update_cmake_lists_config: >-
|
||||
@@ -62,7 +66,7 @@ jobs:
|
||||
{ "name": "CCryptoBoringSSL", "type": "assembly", "exceptions": [ "*/AES/*.swift" ] }
|
||||
]
|
||||
}
|
||||
image: swift:6.2
|
||||
image: "swift:${{ matrix.swift_version }}"
|
||||
|
||||
macos-tests:
|
||||
name: macOS tests
|
||||
|
||||
+6
-8
@@ -12,7 +12,7 @@
|
||||
##
|
||||
##===----------------------------------------------------------------------===##
|
||||
|
||||
cmake_minimum_required(VERSION 3.15.1)
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
|
||||
if(POLICY CMP0157)
|
||||
cmake_policy(SET CMP0157 NEW)
|
||||
@@ -25,13 +25,8 @@ list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules)
|
||||
|
||||
include(SwiftSupport)
|
||||
|
||||
if(CMAKE_VERSION VERSION_LESS 3.16 AND CMAKE_SYSTEM_NAME STREQUAL Windows)
|
||||
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
|
||||
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
|
||||
else()
|
||||
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
|
||||
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
|
||||
endif()
|
||||
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
|
||||
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
|
||||
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
|
||||
set(CMAKE_Swift_MODULE_DIRECTORY ${CMAKE_BINARY_DIR}/swift)
|
||||
|
||||
@@ -79,5 +74,8 @@ if(NOT SwiftASN1_FOUND)
|
||||
FetchContent_MakeAvailable(ASN1)
|
||||
endif()
|
||||
|
||||
# Set after vending swift-asn1 so its targets keep their own language mode.
|
||||
set(CMAKE_Swift_LANGUAGE_VERSION 6)
|
||||
|
||||
add_subdirectory(Sources)
|
||||
add_subdirectory(cmake/modules)
|
||||
+2
-3
@@ -49,9 +49,8 @@ var swiftSettings: [SwiftSetting] = [
|
||||
swiftSettings.append(.enableExperimentalFeature("CheckImplementationOnly"))
|
||||
#endif
|
||||
|
||||
// This doesn't work when cross-compiling: the privacy manifest will be included in the Bundle and
|
||||
// Foundation will be linked. This is, however, strictly better than unconditionally adding the
|
||||
// resource.
|
||||
// A resource makes SwiftPM generate a `Bundle.module` accessor that imports Foundation, not just FoundationEssentials.
|
||||
// `canImport(Darwin)` checks the host, not the target, so cross-compiling from Darwin still adds the resource.
|
||||
#if canImport(Darwin)
|
||||
let privacyManifestExclude: [String] = []
|
||||
let privacyManifestResource: [PackageDescription.Resource] = [.copy("PrivacyInfo.xcprivacy")]
|
||||
|
||||
@@ -72,7 +72,7 @@ extension AES {
|
||||
/// - nonce: The nonce the sealing process requires.
|
||||
/// - authenticatedData: Additional data to be authenticated, if provided.
|
||||
/// - tag: receives the 16-byte authentication tag
|
||||
#if swift(<6.3)
|
||||
#if compiler(<6.3)
|
||||
@_lifetime(message: copy message)
|
||||
#endif
|
||||
public static func seal(
|
||||
@@ -127,7 +127,7 @@ extension AES {
|
||||
/// - authenticatedData: Additional data that was authenticated.
|
||||
///
|
||||
/// The call throws an error if decryption or authentication fail.
|
||||
#if swift(<6.3)
|
||||
#if compiler(<6.3)
|
||||
@_lifetime(message: copy message)
|
||||
#endif
|
||||
public static func open(
|
||||
|
||||
@@ -59,7 +59,7 @@ enum OpenSSLAESGCMImpl {
|
||||
return AES.GCM.SealedBox(combined: combined, nonceByteCount: nonce.count)
|
||||
}
|
||||
|
||||
#if swift(<6.3)
|
||||
#if compiler(<6.3)
|
||||
@_lifetime(message: copy message)
|
||||
#endif
|
||||
@inlinable
|
||||
@@ -119,7 +119,7 @@ enum OpenSSLAESGCMImpl {
|
||||
}
|
||||
|
||||
/// Open a given message in place.
|
||||
#if swift(<6.3)
|
||||
#if compiler(<6.3)
|
||||
@_lifetime(message: copy message)
|
||||
#endif
|
||||
@inlinable
|
||||
|
||||
@@ -48,7 +48,7 @@ extension BoringSSLAEAD {
|
||||
}
|
||||
|
||||
/// Seal a given message in place
|
||||
#if swift(<6.3)
|
||||
#if compiler(<6.3)
|
||||
@_lifetime(message: copy message)
|
||||
#endif
|
||||
func seal(
|
||||
@@ -93,7 +93,7 @@ extension BoringSSLAEAD {
|
||||
}
|
||||
|
||||
/// Open a given message in place.
|
||||
#if swift(<6.3)
|
||||
#if compiler(<6.3)
|
||||
@_lifetime(message: copy message)
|
||||
#endif
|
||||
public func open(
|
||||
@@ -150,7 +150,7 @@ enum OpenSSLChaChaPolyImpl {
|
||||
return ChaChaPoly.SealedBox(combined: combined, nonceByteCount: nonce.count)
|
||||
}
|
||||
|
||||
#if swift(<6.3)
|
||||
#if compiler(<6.3)
|
||||
@_lifetime(message: copy message)
|
||||
#endif
|
||||
static func encrypt(
|
||||
@@ -211,7 +211,7 @@ enum OpenSSLChaChaPolyImpl {
|
||||
}
|
||||
}
|
||||
|
||||
#if swift(<6.3)
|
||||
#if compiler(<6.3)
|
||||
@_lifetime(message: copy message)
|
||||
#endif
|
||||
static func decrypt(
|
||||
|
||||
@@ -70,7 +70,7 @@ public enum ChaChaPoly: Cipher, Sendable {
|
||||
/// - nonce: The nonce the sealing process requires.
|
||||
/// - authenticatedData: Additional data to be authenticated.
|
||||
/// - tag: Will be updated with the 16-byte authentication tag.
|
||||
#if swift(<6.3)
|
||||
#if compiler(<6.3)
|
||||
@_lifetime(message: copy message)
|
||||
#endif
|
||||
public static func seal(
|
||||
@@ -124,7 +124,7 @@ public enum ChaChaPoly: Cipher, Sendable {
|
||||
/// - authenticatedData: Additional data that was authenticated.
|
||||
///
|
||||
/// The call throws an error if decryption or authentication fail.
|
||||
#if swift(<6.3)
|
||||
#if compiler(<6.3)
|
||||
@_lifetime(message: copy message)
|
||||
#endif
|
||||
public static func open(
|
||||
|
||||
@@ -48,7 +48,7 @@ public struct HKDF<H: HashFunction>: Sendable {
|
||||
/// - info: The shared information to use for key derivation.
|
||||
/// - outputKey: An output span that will be populated with the derived
|
||||
/// symmetric key.
|
||||
#if swift(<6.3)
|
||||
#if compiler(<6.3)
|
||||
@_lifetime(outputKey: copy outputKey)
|
||||
#endif
|
||||
public static func deriveKey(inputKeyMaterial: SymmetricKey,
|
||||
@@ -258,7 +258,7 @@ public struct HKDF<H: HashFunction>: Sendable {
|
||||
/// - outputByteCount: The length in bytes of the resulting symmetric key.
|
||||
///
|
||||
/// - Returns: The derived symmetric key.
|
||||
#if swift(<6.3)
|
||||
#if compiler(<6.3)
|
||||
@_lifetime(output: copy output)
|
||||
#endif
|
||||
public static func expand(pseudoRandomKey prk: RawSpan, info: RawSpan?, into output: inout OutputRawSpan) {
|
||||
|
||||
@@ -121,10 +121,10 @@ extension Curve25519.KeyAgreement {
|
||||
) throws
|
||||
-> SharedSecret
|
||||
{
|
||||
let sharedSecret = SecureBytes(
|
||||
let sharedSecret = try SecureBytes(
|
||||
unsafeUninitializedCapacity: Curve25519.KeyAgreement.keySizeBytes
|
||||
) { secretPointer, secretSize in
|
||||
self.key.withUnsafeBytes { privateKeyPointer in
|
||||
try self.key.withUnsafeBytes { privateKeyPointer in
|
||||
// We precondition on all of these sizes because bounds checking is cool.
|
||||
// These are fatal instead of guards because we allocated the secret (so it must be right),
|
||||
// we either allocated the private key or validated it on construction (so it must be right),
|
||||
@@ -133,16 +133,17 @@ extension Curve25519.KeyAgreement {
|
||||
precondition(privateKeyPointer.count == Curve25519.KeyAgreement.keySizeBytes)
|
||||
precondition(publicKeyShare.keyBytes.count == Curve25519.KeyAgreement.keySizeBytes)
|
||||
|
||||
// We don't check the return code here. This return code only validates that the generated secret is not the weak all-zero
|
||||
// secret (it is not possible for BoringSSL's X25519 implementation to fail, which is nice). There is currently what I would
|
||||
// politely describe as a "lack of consensus" as to whether crypto libraries should reject this secret. CryptoKit on Apple
|
||||
// platforms currently does not, so for the sake of conformance with our peer implementation I will also refuse to check it.
|
||||
// We may elect to revisit this decision if the security best-practice thinking changes.
|
||||
CCryptoBoringSSL_X25519(
|
||||
secretPointer.baseAddress,
|
||||
privateKeyPointer.baseAddress,
|
||||
publicKeyShare.keyBytes
|
||||
)
|
||||
// BoringSSL returns 0 when the peer's public key has low order, which makes the shared secret
|
||||
// the weak all-zero value. CryptoKit rejects that key agreement, so we do too.
|
||||
guard
|
||||
CCryptoBoringSSL_X25519(
|
||||
secretPointer.baseAddress,
|
||||
privateKeyPointer.baseAddress,
|
||||
publicKeyShare.keyBytes
|
||||
) == 1
|
||||
else {
|
||||
throw CryptoKitError.internalBoringSSLError()
|
||||
}
|
||||
}
|
||||
|
||||
secretSize = Curve25519.KeyAgreement.keySizeBytes // We always use all of it.
|
||||
|
||||
@@ -57,7 +57,7 @@ extension ContiguousArray where Element == UInt8 {
|
||||
|
||||
extension OutputRawSpan {
|
||||
/// Append the contents of the given raw span to this output span.
|
||||
#if swift(<6.3)
|
||||
#if compiler(<6.3)
|
||||
@_lifetime(self: copy self)
|
||||
#endif
|
||||
mutating func append(contentsOf bytes: RawSpan) {
|
||||
@@ -69,7 +69,7 @@ extension OutputRawSpan {
|
||||
|
||||
extension OutputSpan<UInt8> {
|
||||
/// Append the contents of the given raw span to this output span.
|
||||
#if swift(<6.3)
|
||||
#if compiler(<6.3)
|
||||
@_lifetime(self: copy self)
|
||||
#endif
|
||||
mutating func append(contentsOf bytes: RawSpan) {
|
||||
|
||||
@@ -18,29 +18,20 @@ import FoundationEssentials
|
||||
import Foundation
|
||||
#endif
|
||||
|
||||
#if swift(<6.4)
|
||||
#if compiler(<6.4)
|
||||
@available(macOS 10.14.4, iOS 12.2, watchOS 5.2, tvOS 12.2, visionOS 1.0, *)
|
||||
extension ContiguousBytes {
|
||||
func withBytes<R>(_ body: (RawSpan) throws -> R) rethrows -> R {
|
||||
try withUnsafeBytes { try body(RawSpan(_unsafeBytes: $0)) }
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#if swift(<6.4)
|
||||
@available(macOS 10.14.4, iOS 12.2, watchOS 5.2, tvOS 12.2, visionOS 1.0, *)
|
||||
extension MutableRawSpan {
|
||||
#if swift(>=6.3)
|
||||
subscript(_ byteOffset: Int) -> UInt8 {
|
||||
get { unsafeLoad(fromByteOffset: byteOffset, as: UInt8.self) }
|
||||
@_lifetime(self: copy self)
|
||||
set { storeBytes(of: newValue, toByteOffset: byteOffset, as: UInt8.self) }
|
||||
}
|
||||
#elseif swift(<6.3)
|
||||
subscript(_ byteOffset: Int) -> UInt8 {
|
||||
get { unsafeLoad(fromByteOffset: byteOffset, as: UInt8.self) }
|
||||
@_lifetime(&self)
|
||||
set { storeBytes(of: newValue, toByteOffset: byteOffset, as: UInt8.self) }
|
||||
}
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
@@ -45,7 +45,7 @@ extension UnsafeMutableRawBufferPointer {
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension MutableRawSpan {
|
||||
#if swift(<6.3)
|
||||
#if compiler(<6.3)
|
||||
@_lifetime(self: copy self)
|
||||
#endif
|
||||
mutating func initializeWithRandomBytes(count: Int) {
|
||||
@@ -55,7 +55,7 @@ extension MutableRawSpan {
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension OutputRawSpan {
|
||||
#if swift(<6.3)
|
||||
#if compiler(<6.3)
|
||||
@_lifetime(self: copy self)
|
||||
#endif
|
||||
mutating func appendingRandomBytes(count: Int) {
|
||||
|
||||
@@ -153,7 +153,7 @@ extension BoringSSLAEAD.AEADContext {
|
||||
|
||||
/// Lowest level seal operation that calls into BoringSSL directly and
|
||||
/// operates on already-allocated memory.
|
||||
#if swift(<6.3)
|
||||
#if compiler(<6.3)
|
||||
@_lifetime(tag: copy tag)
|
||||
#endif
|
||||
public func seal(
|
||||
@@ -303,7 +303,7 @@ extension BoringSSLAEAD.AEADContext {
|
||||
}
|
||||
|
||||
/// Lowest level call into BoringSSL that decrypts in place.
|
||||
#if swift(<6.3)
|
||||
#if compiler(<6.3)
|
||||
@_lifetime(message: copy message)
|
||||
#endif
|
||||
public func open(
|
||||
|
||||
@@ -47,7 +47,7 @@ extension UnsafeMutableRawBufferPointer {
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension OutputRawSpan {
|
||||
#if swift(<6.3)
|
||||
#if compiler(<6.3)
|
||||
@_lifetime(self: copy self)
|
||||
#endif
|
||||
package mutating func appendingRandomBytes(count: Int) {
|
||||
|
||||
@@ -85,9 +85,21 @@ internal struct BoringSSLScrypt {
|
||||
var derivedKeyData = Data(count: outputByteCount)
|
||||
defer { derivedKeyData.withUnsafeMutableBytes { $0.zeroize() } }
|
||||
|
||||
// This computes the maximum amount of memory that will be used by the scrypt algorithm with an additional memory page to spare. This value will be used by the BoringSSL as the memory limit for the algorithm.
|
||||
// An additional memory page is added to the computed value (using POSIX specification) to ensure that the memory limit is not too tight.
|
||||
let maxMemory = maxMemory ?? (128 * rounds * blockSize * parallelism + getPageSize())
|
||||
let memoryLimit: Int
|
||||
if let maxMemory {
|
||||
memoryLimit = maxMemory
|
||||
} else {
|
||||
// This computes the maximum amount of memory that will be used by the scrypt algorithm with an additional memory page to spare. This value will be used by the BoringSSL as the memory limit for the algorithm.
|
||||
// An additional memory page is added to the computed value (using POSIX specification) to ensure that the memory limit is not too tight.
|
||||
let (blockBytes, blockBytesOverflowed) = 128.multipliedReportingOverflow(by: blockSize)
|
||||
let (laneBytes, laneBytesOverflowed) = blockBytes.multipliedReportingOverflow(by: rounds)
|
||||
let (stateBytes, stateBytesOverflowed) = laneBytes.multipliedReportingOverflow(by: parallelism)
|
||||
let (computedLimit, computedLimitOverflowed) = stateBytes.addingReportingOverflow(getPageSize())
|
||||
guard !blockBytesOverflowed, !laneBytesOverflowed, !stateBytesOverflowed, !computedLimitOverflowed else {
|
||||
throw CryptoKitError.incorrectParameterSize
|
||||
}
|
||||
memoryLimit = computedLimit
|
||||
}
|
||||
|
||||
let result = derivedKeyData.withUnsafeMutableBytes { derivedKeyBytes -> Int32 in
|
||||
let saltBytes: ContiguousBytes = salt.regions.count == 1 ? salt.regions.first! : Array(salt)
|
||||
@@ -103,7 +115,7 @@ internal struct BoringSSLScrypt {
|
||||
UInt64(rounds),
|
||||
UInt64(blockSize),
|
||||
UInt64(parallelism),
|
||||
maxMemory,
|
||||
memoryLimit,
|
||||
derivedKeyBytes.baseAddress!,
|
||||
derivedKeyBytes.count
|
||||
)
|
||||
|
||||
@@ -35,10 +35,16 @@ extension KDF {
|
||||
/// - rounds: The number of rounds which should be used to perform key derivation. Must be a power of 2 less than `2^(128 * blockSize / 8)`.
|
||||
/// - blockSize: The block size to use for key derivation.
|
||||
/// - parallelism: The parallelism factor to use for key derivation. Must be a positive integer less than or equal to `((2^32 - 1) * 32) / (128 * blockSize)`.
|
||||
/// - maxMemory: The maximum amount of memory allowed to use for key derivation. If not provided, the default value is computed for the provided parameters.
|
||||
/// - maxMemory: The maximum amount of memory allowed to use for key derivation. Must be positive. If not provided, the default value is computed for the provided parameters.
|
||||
/// - Returns: The derived symmetric key.
|
||||
public static func deriveKey<Passphrase: DataProtocol, Salt: DataProtocol>(from password: Passphrase, salt: Salt, outputByteCount: Int, rounds: Int, blockSize: Int, parallelism: Int, maxMemory: Int? = nil) throws -> SymmetricKey {
|
||||
return try BackingScrypt.deriveKey(from: password, salt: salt, outputByteCount: outputByteCount, rounds: rounds, blockSize: blockSize, parallelism: parallelism)
|
||||
guard rounds > 0, blockSize > 0, parallelism > 0, outputByteCount >= 0 else {
|
||||
throw CryptoKitError.incorrectParameterSize
|
||||
}
|
||||
if let maxMemory, maxMemory <= 0 {
|
||||
throw CryptoKitError.incorrectParameterSize
|
||||
}
|
||||
return try BackingScrypt.deriveKey(from: password, salt: salt, outputByteCount: outputByteCount, rounds: rounds, blockSize: blockSize, parallelism: parallelism, maxMemory: maxMemory)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -84,4 +84,70 @@ class ScryptTests: XCTestCase {
|
||||
try orFail { try self.testRFCVector(vector) }
|
||||
}
|
||||
}
|
||||
|
||||
func testDeriveKeyWithMaxMemoryBelowRequirementThrows() throws {
|
||||
let password = Array("password".utf8)
|
||||
let salt = Array("salt".utf8)
|
||||
|
||||
// These parameters need 128 * blockSize * (rounds + parallelism + 1) bytes, just over 1 MiB.
|
||||
// BoringSSL reads a limit of zero as "use the default limit", and a negative limit as a huge size_t.
|
||||
for maxMemory in [1024, 0, -1] {
|
||||
XCTAssertThrowsError(
|
||||
try KDF.Scrypt.deriveKey(
|
||||
from: password,
|
||||
salt: salt,
|
||||
outputByteCount: 32,
|
||||
rounds: 1024,
|
||||
blockSize: 8,
|
||||
parallelism: 1,
|
||||
maxMemory: maxMemory
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
let bounded = try KDF.Scrypt.deriveKey(
|
||||
from: password,
|
||||
salt: salt,
|
||||
outputByteCount: 32,
|
||||
rounds: 1024,
|
||||
blockSize: 8,
|
||||
parallelism: 1,
|
||||
maxMemory: 2 << 20
|
||||
)
|
||||
let unbounded = try KDF.Scrypt.deriveKey(
|
||||
from: password,
|
||||
salt: salt,
|
||||
outputByteCount: 32,
|
||||
rounds: 1024,
|
||||
blockSize: 8,
|
||||
parallelism: 1
|
||||
)
|
||||
XCTAssertEqual(bounded, unbounded)
|
||||
}
|
||||
|
||||
func testDeriveKeyWithOutOfRangeParametersThrows() throws {
|
||||
let password = Array("password".utf8)
|
||||
let salt = Array("salt".utf8)
|
||||
|
||||
for (rounds, blockSize, parallelism, outputByteCount) in [
|
||||
// Negative values reach UInt64 conversions and Data(count:), both of which trap.
|
||||
(-1, 8, 1, 32),
|
||||
(1024, -1, 1, 32),
|
||||
(1024, 8, -1, 32),
|
||||
(1024, 8, 1, -1),
|
||||
// 128 * blockSize * rounds * parallelism overflows Int.
|
||||
(1 << 32, 1 << 30, 1, 32),
|
||||
] {
|
||||
XCTAssertThrowsError(
|
||||
try KDF.Scrypt.deriveKey(
|
||||
from: password,
|
||||
salt: salt,
|
||||
outputByteCount: outputByteCount,
|
||||
rounds: rounds,
|
||||
blockSize: blockSize,
|
||||
parallelism: parallelism
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the SwiftCrypto open source project
|
||||
//
|
||||
// Copyright (c) 2026 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 canImport(CryptoKit)
|
||||
// Skip tests that require @testable imports of CryptoKit.
|
||||
#else
|
||||
@testable import Crypto
|
||||
|
||||
extension X25519Tests {
|
||||
func testKeyAgreementWithLowOrderPublicKeyThrows() throws {
|
||||
// The vectors whose shared secret is all-zero: the seven low-order u-coordinates, which are
|
||||
// 0, 1, both points of order 8, p-1, p and p+1, plus a non-canonical spelling of each.
|
||||
try wycheproofTest(
|
||||
bundleType: self,
|
||||
jsonName: "x25519_test",
|
||||
testFunction: { (group: ECDHTestGroup) in
|
||||
let zeroSharedSecretTests = group.tests.filter { $0.flags.contains("ZeroSharedSecret") }
|
||||
XCTAssert(!zeroSharedSecretTests.isEmpty)
|
||||
|
||||
for testVector in zeroSharedSecretTests {
|
||||
// The public key itself is accepted, as it is by CryptoKit; only the agreement fails.
|
||||
let publicKey = try Curve25519.KeyAgreement.PublicKey(
|
||||
rawRepresentation: Array(hexString: testVector.publicKey)
|
||||
)
|
||||
let privateKey = try Curve25519.KeyAgreement.PrivateKey(
|
||||
rawRepresentation: Array(hexString: testVector.privateKey)
|
||||
)
|
||||
XCTAssertThrowsError(
|
||||
try privateKey.sharedSecretFromKeyAgreement(with: publicKey),
|
||||
"tcId \(testVector.tcId)"
|
||||
)
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#endif // canImport(CryptoKit)
|
||||
Reference in new issue
Block a user