mirror of
https://github.com/apple/swift-crypto
synced 2026-11-26 04:47:49 +00:00
Update to RC SDK (#410)
This patch brings us up-to-date with the RC SDK. There are a couple of tweaks to the MLDSA code and the XWing code, mostly a few minor interface changes. I've adopted those in the backing code, and also wired up the SHA3 integrity checks.
This commit is contained in:
1 parent
890595d10e
commit
ea7f8fbce1
15 files changed
+355
-327
No files matched your search
@@ -102,17 +102,26 @@ extension MLKEM768.InternalPrivateKey: BoringSSLBackedMLKEMPrivateKey {
|
||||
}
|
||||
|
||||
init<Bytes>(seedRepresentation: Bytes, publicKeyRawRepresentation: Data?) throws where Bytes: DataProtocol {
|
||||
precondition(publicKeyRawRepresentation == nil)
|
||||
self = try .init(seedRepresentation: seedRepresentation)
|
||||
let publicKeyHash = publicKeyRawRepresentation.map {
|
||||
SHA3_256.hash(data: $0)
|
||||
}
|
||||
self = try .init(seedRepresentation: seedRepresentation, publicKeyHash: publicKeyHash)
|
||||
}
|
||||
|
||||
init<Bytes: DataProtocol>(seedRepresentation: Bytes, publicKeyHash: SHA3_256Digest?) throws {
|
||||
precondition(publicKeyHash == nil)
|
||||
self = try .init(seedRepresentation: seedRepresentation)
|
||||
let generatedHash = SHA3_256.hash(data: self.publicKey.rawRepresentation)
|
||||
if let publicKeyHash, generatedHash != publicKeyHash {
|
||||
throw KEM.Errors.publicKeyMismatchDuringInitialization
|
||||
}
|
||||
}
|
||||
|
||||
var integrityCheckedRepresentation: Data {
|
||||
fatalError()
|
||||
var representation = self.seedRepresentation
|
||||
SHA3_256.hash(data: self.publicKey.rawRepresentation).withUnsafeBytes {
|
||||
representation.append(contentsOf: $0)
|
||||
}
|
||||
return representation
|
||||
}
|
||||
|
||||
var interiorPublicKey: MLKEM768.InternalPublicKey {
|
||||
@@ -155,17 +164,26 @@ extension MLKEM1024.InternalPrivateKey: BoringSSLBackedMLKEMPrivateKey {
|
||||
}
|
||||
|
||||
init<Bytes>(seedRepresentation: Bytes, publicKeyRawRepresentation: Data?) throws where Bytes: DataProtocol {
|
||||
precondition(publicKeyRawRepresentation == nil)
|
||||
self = try .init(seedRepresentation: seedRepresentation)
|
||||
let publicKeyHash = publicKeyRawRepresentation.map {
|
||||
SHA3_256.hash(data: $0)
|
||||
}
|
||||
self = try .init(seedRepresentation: seedRepresentation, publicKeyHash: publicKeyHash)
|
||||
}
|
||||
|
||||
init<Bytes: DataProtocol>(seedRepresentation: Bytes, publicKeyHash: SHA3_256Digest?) throws {
|
||||
precondition(publicKeyHash == nil)
|
||||
self = try .init(seedRepresentation: seedRepresentation)
|
||||
let generatedHash = SHA3_256.hash(data: self.publicKey.rawRepresentation)
|
||||
if let publicKeyHash, generatedHash != publicKeyHash {
|
||||
throw KEM.Errors.publicKeyMismatchDuringInitialization
|
||||
}
|
||||
}
|
||||
|
||||
var integrityCheckedRepresentation: Data {
|
||||
fatalError()
|
||||
var representation = self.seedRepresentation
|
||||
SHA3_256.hash(data: self.publicKey.rawRepresentation).withUnsafeBytes {
|
||||
representation.append(contentsOf: $0)
|
||||
}
|
||||
return representation
|
||||
}
|
||||
|
||||
var interiorPublicKey: MLKEM1024.InternalPublicKey {
|
||||
|
||||
@@ -30,8 +30,8 @@ struct OpenSSLXWingPublicKeyImpl: Sendable {
|
||||
self.publicKeyBytes = publicKeyBytes
|
||||
}
|
||||
|
||||
init<D: ContiguousBytes>(dataRepresentation: D) throws {
|
||||
self.publicKeyBytes = try dataRepresentation.withUnsafeBytes {
|
||||
init<D: ContiguousBytes>(rawRepresentation: D) throws {
|
||||
self.publicKeyBytes = try rawRepresentation.withUnsafeBytes {
|
||||
guard $0.count == XWING_PUBLIC_KEY_BYTES else {
|
||||
throw CryptoKitError.incorrectKeySize
|
||||
}
|
||||
@@ -164,7 +164,7 @@ extension OpenSSLXWingPrivateKeyImpl {
|
||||
}
|
||||
|
||||
// Matching CryptoKit, we only care that this _is_ a public key, not that it matches.
|
||||
let _ = try OpenSSLXWingPublicKeyImpl(dataRepresentation: publicKeyBytes)
|
||||
let _ = try OpenSSLXWingPublicKeyImpl(rawRepresentation: publicKeyBytes)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -187,6 +187,10 @@ extension OpenSSLXWingPrivateKeyImpl {
|
||||
throw CryptoKitError.internalBoringSSLError()
|
||||
}
|
||||
}
|
||||
|
||||
if let publicKeyHash, publicKeyHash != self.publicKeyDigest {
|
||||
throw KEM.Errors.publicKeyMismatchDuringInitialization
|
||||
}
|
||||
}
|
||||
|
||||
var seedRepresentation: Data {
|
||||
@@ -200,7 +204,11 @@ extension OpenSSLXWingPrivateKeyImpl {
|
||||
}
|
||||
|
||||
var integrityCheckedRepresentation: Data {
|
||||
fatalError()
|
||||
var representation = self.seedRepresentation
|
||||
self.publicKeyDigest.withUnsafeBytes {
|
||||
representation.append(contentsOf: $0)
|
||||
}
|
||||
return representation
|
||||
}
|
||||
|
||||
var dataRepresentation: Data {
|
||||
@@ -215,6 +223,14 @@ extension OpenSSLXWingPrivateKeyImpl {
|
||||
}
|
||||
}
|
||||
|
||||
private var publicKeyDigest: SHA3_256Digest {
|
||||
withUnsafeTemporaryAllocation(byteCount: Int(XWING_PUBLIC_KEY_BYTES), alignment: 1) {
|
||||
let rc = CCryptoBoringSSL_XWING_public_from_private($0.baseAddress, &self.privateKey)
|
||||
precondition(rc == 1)
|
||||
return SHA3_256.hash(bufferPointer: UnsafeRawBufferPointer($0))
|
||||
}
|
||||
}
|
||||
|
||||
static func generate() throws -> Self {
|
||||
try Self()
|
||||
}
|
||||
|
||||
@@ -14,25 +14,12 @@
|
||||
#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
|
||||
@_exported import CryptoKit
|
||||
#else
|
||||
#if canImport(FoundationEssentials)
|
||||
public import FoundationEssentials
|
||||
#else
|
||||
public import Foundation
|
||||
#endif
|
||||
|
||||
#if _runtime(_ObjC) && CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
|
||||
@_exported import CryptoKit
|
||||
#else
|
||||
|
||||
#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION
|
||||
import SwiftSystem
|
||||
#else
|
||||
#if canImport(FoundationEssentials)
|
||||
import FoundationEssentials
|
||||
#else
|
||||
import Foundation
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if (!CRYPTO_IN_SWIFTPM_FORCE_BUILD_API) || CRYPTOKIT_NO_ACCESS_TO_FOUNDATION
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
@@ -61,8 +48,8 @@ extension XWingMLKEM768X25519 {
|
||||
self.impl = impl
|
||||
}
|
||||
|
||||
public init<D: ContiguousBytes>(dataRepresentation: D) throws {
|
||||
self.impl = try .init(dataRepresentation: dataRepresentation)
|
||||
public init<D: ContiguousBytes>(rawRepresentation: D) throws {
|
||||
self.impl = try .init(rawRepresentation: rawRepresentation)
|
||||
}
|
||||
|
||||
public var rawRepresentation: Data {
|
||||
@@ -96,20 +83,10 @@ extension XWingMLKEM768X25519 {
|
||||
self.impl = impl
|
||||
}
|
||||
|
||||
public init<D: ContiguousBytes>(bytes: D) throws {
|
||||
self.impl = try .init(bytes: bytes)
|
||||
}
|
||||
|
||||
internal init<D: DataProtocol>(seedRepresentation: D, publicKeyHash: SHA3_256Digest?) throws {
|
||||
self.impl = try .init(seedRepresentation: seedRepresentation, publicKeyHash: publicKeyHash)
|
||||
}
|
||||
|
||||
public var dataRepresentation: Data {
|
||||
get {
|
||||
self.impl.dataRepresentation
|
||||
}
|
||||
}
|
||||
|
||||
public static func generate() throws -> XWingMLKEM768X25519.PrivateKey {
|
||||
return try Self(impl: XWingPrivateKeyImpl.generate())
|
||||
}
|
||||
@@ -133,17 +110,12 @@ extension XWingMLKEM768X25519.PrivateKey: HPKEKEMPrivateKeyGeneration {
|
||||
}
|
||||
|
||||
public init<D: DataProtocol>(seedRepresentation: D, publicKey: XWingMLKEM768X25519.PublicKey?) throws {
|
||||
#if false
|
||||
var publicKeyHash: SHA3_256Digest? = nil
|
||||
if publicKey != nil {
|
||||
publicKeyHash = SHA3_256.hash(data: publicKey!.rawRepresentation)
|
||||
}
|
||||
|
||||
self = try XWingMLKEM768X25519.PrivateKey.init(seedRepresentation: seedRepresentation, publicKeyHash: publicKeyHash)
|
||||
#else
|
||||
precondition(publicKey == nil)
|
||||
self = try XWingMLKEM768X25519.PrivateKey.init(seedRepresentation: seedRepresentation, publicKeyHash: nil)
|
||||
#endif
|
||||
}
|
||||
|
||||
public init<D: DataProtocol>(integrityCheckedRepresentation: D) throws {
|
||||
@@ -178,7 +150,7 @@ extension XWingMLKEM768X25519.PublicKey: HPKEKEMPublicKey {
|
||||
/// - Throws: ``CryptoKit/HPKE/Errors/inconsistentCiphersuiteAndKey`` if the key encapsulation mechanism requested is incompatible with this public key.
|
||||
public init<D>(_ serialization: D, kem: HPKE.KEM) throws where D: ContiguousBytes {
|
||||
try Self.validateCiphersuite(kem)
|
||||
try self.init(dataRepresentation: serialization)
|
||||
try self.init(rawRepresentation: serialization)
|
||||
}
|
||||
|
||||
/// Creates a serialized representation of the public key.
|
||||
@@ -197,5 +169,4 @@ extension XWingMLKEM768X25519.PublicKey: HPKEKEMPublicKey {
|
||||
/// The type of the ephemeral private key associated with this public key.
|
||||
public typealias HPKEEphemeralPrivateKey = XWingMLKEM768X25519.PrivateKey
|
||||
}
|
||||
#endif
|
||||
#endif // Linux or !SwiftPM
|
||||
@@ -639,7 +639,7 @@ extension MLDSA87 {
|
||||
|
||||
enum MLDSA {
|
||||
/// The size of the seed in bytes.
|
||||
fileprivate static let seedByteCount = 32
|
||||
static let seedByteCount = 32
|
||||
}
|
||||
|
||||
#endif // CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
|
||||
@@ -342,7 +342,7 @@ extension MLDSA${parameter_set} {
|
||||
|
||||
enum MLDSA {
|
||||
/// The size of the seed in bytes.
|
||||
fileprivate static let seedByteCount = 32
|
||||
static let seedByteCount = 32
|
||||
}
|
||||
|
||||
#endif // CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
|
||||
@@ -84,18 +84,29 @@ extension MLDSA87.InternalPublicKey: BoringSSLBackedMLDSAPublicKey {}
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
struct OpenSSLMLDSAPrivateKeyImpl<Parameters: BoringSSLBackedMLDSAParameters> {
|
||||
private var backing: Parameters.BackingPrivateKey
|
||||
private let publicKeyHash: SHA3_256Digest
|
||||
|
||||
init() throws {
|
||||
self.backing = try .init()
|
||||
self.publicKeyHash = SHA3_256.hash(data: self.backing.publicKey.rawRepresentation)
|
||||
}
|
||||
|
||||
init<D: DataProtocol>(seedRepresentation: D, publicKey: OpenSSLMLDSAPublicKeyImpl<Parameters>?) throws {
|
||||
precondition(publicKey == nil)
|
||||
init<D: DataProtocol>(seedRepresentation: D, publicKeyRawRepresentation: Data?) throws {
|
||||
let publicKeyHash = publicKeyRawRepresentation.map {
|
||||
SHA3_256.hash(data: $0)
|
||||
}
|
||||
self = try Self(seedRepresentation: seedRepresentation, publicKeyHash: publicKeyHash)
|
||||
}
|
||||
|
||||
init<D: DataProtocol>(seedRepresentation: D, publicKeyHash: SHA3_256Digest?) throws {
|
||||
self.backing = try .init(seedRepresentation: seedRepresentation)
|
||||
}
|
||||
let generatedHash = SHA3_256.hash(data: self.backing.publicKey.rawRepresentation)
|
||||
|
||||
init<D: DataProtocol>(integrityCheckedRepresentation: D) throws {
|
||||
fatalError()
|
||||
if let publicKeyHash, generatedHash != publicKeyHash {
|
||||
throw CryptoKitError.unwrapFailure
|
||||
}
|
||||
|
||||
self.publicKeyHash = generatedHash
|
||||
}
|
||||
|
||||
func signature<D: DataProtocol>(for data: D) throws -> Data {
|
||||
@@ -115,7 +126,16 @@ struct OpenSSLMLDSAPrivateKeyImpl<Parameters: BoringSSLBackedMLDSAParameters> {
|
||||
}
|
||||
|
||||
var integrityCheckedRepresentation: Data {
|
||||
fatalError()
|
||||
var representation = self.seedRepresentation
|
||||
representation.reserveCapacity(SHA3_256Digest.byteCount)
|
||||
self.publicKeyHash.withUnsafeBytes {
|
||||
representation.append(contentsOf: $0)
|
||||
}
|
||||
return representation
|
||||
}
|
||||
|
||||
static var seedSize: Int {
|
||||
MLDSA.seedByteCount
|
||||
}
|
||||
}
|
||||
|
||||
@@ -132,14 +152,14 @@ struct OpenSSLMLDSAPublicKeyImpl<Parameters: BoringSSLBackedMLDSAParameters> {
|
||||
}
|
||||
|
||||
func isValidSignature<S: DataProtocol, D: DataProtocol>(
|
||||
signature: S,
|
||||
_ signature: S,
|
||||
for data: D
|
||||
) -> Bool {
|
||||
self.backing.isValidSignature(signature, for: data)
|
||||
}
|
||||
|
||||
func isValidSignature<S: DataProtocol, D: DataProtocol, C: DataProtocol>(
|
||||
signature: S,
|
||||
_ signature: S,
|
||||
for data: D,
|
||||
context: C
|
||||
) -> Bool {
|
||||
|
||||
@@ -21,8 +21,10 @@ public import Foundation
|
||||
#endif
|
||||
|
||||
#if (!CRYPTO_IN_SWIFTPM_FORCE_BUILD_API) || CRYPTOKIT_NO_ACCESS_TO_FOUNDATION
|
||||
typealias MLDSAPublicKeyImpl = CoreCryptoMLDSAPublicKeyImpl
|
||||
typealias MLDSAPrivateKeyImpl = CoreCryptoMLDSAPrivateKeyImpl
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
typealias MLDSAPublicKeyImpl = CorecryptoMLDSAPublicKeyImpl
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
typealias MLDSAPrivateKeyImpl = CorecryptoMLDSAPrivateKeyImpl
|
||||
#else
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
typealias MLDSAPublicKeyImpl = OpenSSLMLDSAPublicKeyImpl
|
||||
@@ -38,16 +40,38 @@ public enum MLDSA65: Sendable {}
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension MLDSA65 {
|
||||
/// The public key for MLDSA65.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public struct PublicKey: Sendable {
|
||||
var impl: MLDSAPublicKeyImpl<MLDSA65>
|
||||
|
||||
internal init(_ impl: MLDSAPublicKeyImpl<MLDSA65>) {
|
||||
self.impl = impl
|
||||
}
|
||||
|
||||
/// Parses a public key from a serialized representation.
|
||||
///
|
||||
/// - Parameter rawRepresentation: The public key, in the FIPS 204 standard serialization format.
|
||||
/// - Returns: The deserialized public key.
|
||||
public init<D: DataProtocol>(rawRepresentation: D) throws {
|
||||
self.impl = try MLDSAPublicKeyImpl(rawRepresentation: rawRepresentation)
|
||||
}
|
||||
|
||||
/// A serialized representation of the public key.
|
||||
///
|
||||
/// This property provides a representation of the public key in the FIPS 204 standard serialization format.
|
||||
public var rawRepresentation: Data {
|
||||
get {
|
||||
return self.impl.rawRepresentation
|
||||
}
|
||||
}
|
||||
|
||||
/// Verifies a MLDSA65 signature.
|
||||
/// - Parameters:
|
||||
/// - signature: The MLDSA65 signature to verify.
|
||||
/// - data: The signed data.
|
||||
/// - Returns: `true` if the signature is valid, `false` otherwise.
|
||||
public func isValidSignature<S: DataProtocol, D: DataProtocol>(signature: S, for data: D) -> Bool {
|
||||
self.impl.isValidSignature(signature: signature, for: data)
|
||||
public func isValidSignature<S: DataProtocol, D: DataProtocol>(_ signature: S, for data: D) -> Bool {
|
||||
return self.impl.isValidSignature(signature, for: data)
|
||||
}
|
||||
|
||||
/// Verifies a MLDSA65 signature, in a specific context.
|
||||
@@ -56,65 +80,24 @@ extension MLDSA65 {
|
||||
/// - data: The signed data.
|
||||
/// - context: Context for the signature.
|
||||
/// - Returns: `true` if the signature is valid in the specified context, `false` otherwise.
|
||||
public func isValidSignature<S: DataProtocol, D: DataProtocol, C: DataProtocol>(signature: S, for data: D, context: C) -> Bool {
|
||||
self.impl.isValidSignature(signature: signature, for: data, context: context)
|
||||
}
|
||||
|
||||
/// Parses a public key from a serialized representation.
|
||||
///
|
||||
/// - Parameter rawRepresentation: The public key, in the FIPS 204 standard serialization format.
|
||||
/// - Returns: The deserialized public key.
|
||||
public init<D: DataProtocol>(rawRepresentation: D) throws {
|
||||
self.impl = try .init(rawRepresentation: rawRepresentation)
|
||||
}
|
||||
|
||||
/// A serialized representation of the public key.
|
||||
///
|
||||
/// This property provides a representation of the public key in the FIPS 204 standard serialization format.
|
||||
public var rawRepresentation: Data {
|
||||
get {
|
||||
self.impl.rawRepresentation
|
||||
}
|
||||
}
|
||||
|
||||
fileprivate init(impl: MLDSAPublicKeyImpl<MLDSA65>) {
|
||||
self.impl = impl
|
||||
public func isValidSignature<S: DataProtocol, D: DataProtocol, C: DataProtocol>(_ signature: S, for data: D, context: C) -> Bool {
|
||||
return self.impl.isValidSignature(signature, for: data, context: context)
|
||||
}
|
||||
}
|
||||
|
||||
/// The private key for MLDSA65.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public struct PrivateKey: Signer, Sendable {
|
||||
var impl: MLDSAPrivateKeyImpl<MLDSA65>
|
||||
|
||||
/// Generates a MLDSA65 signature.
|
||||
/// - Parameters:
|
||||
/// - data: The data to sign.
|
||||
/// - Returns: The MLDSA65 signature.
|
||||
/// This method throws if CryptoKit encounters an error producing the signature.
|
||||
public func signature<D: DataProtocol>(for data: D) throws -> Data {
|
||||
try self.impl.signature(for: data)
|
||||
internal let impl: MLDSAPrivateKeyImpl<MLDSA65>
|
||||
|
||||
internal init(_ impl: MLDSAPrivateKeyImpl<MLDSA65>) {
|
||||
self.impl = impl
|
||||
}
|
||||
|
||||
/// Generates a MLDSA65 signature, with context.
|
||||
/// - Parameters:
|
||||
/// - data: The data to sign.
|
||||
/// - context: Context for the signature.
|
||||
/// - Returns: The MLDSA65 signature.
|
||||
/// This method throws if CryptoKit encounters an error producing the signature.
|
||||
public func signature<D: DataProtocol, C: DataProtocol>(for data: D, context: C) throws -> Data {
|
||||
try self.impl.signature(for: data, context: context)
|
||||
}
|
||||
|
||||
/// The associated public key.
|
||||
public var publicKey: PublicKey {
|
||||
get {
|
||||
PublicKey(impl: self.impl.publicKey)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// Initializes a new random private key.
|
||||
public init() throws {
|
||||
self.impl = try .init()
|
||||
let impl = try MLDSAPrivateKeyImpl<MLDSA65>()
|
||||
self = PrivateKey(impl)
|
||||
}
|
||||
|
||||
/// Initializes a private key from the seed representation.
|
||||
@@ -126,9 +109,13 @@ extension MLDSA65 {
|
||||
///
|
||||
/// If a public key is provided, a consistency check is performed between it and the derived public key.
|
||||
public init<D: DataProtocol>(seedRepresentation: D, publicKey: MLDSA65.PublicKey?) throws {
|
||||
self.impl = try .init(seedRepresentation: seedRepresentation, publicKey: publicKey?.impl)
|
||||
var publicKeyRawRepresentation: Data? = nil
|
||||
if publicKey != nil {
|
||||
publicKeyRawRepresentation = publicKey!.rawRepresentation
|
||||
}
|
||||
self.impl = try MLDSAPrivateKeyImpl<MLDSA65>(seedRepresentation: seedRepresentation, publicKeyRawRepresentation: publicKeyRawRepresentation)
|
||||
}
|
||||
|
||||
|
||||
/// The seed representation of the private key.
|
||||
///
|
||||
/// The seed representation is 32 bytes long, and is the parameter
|
||||
@@ -139,12 +126,47 @@ extension MLDSA65 {
|
||||
}
|
||||
}
|
||||
|
||||
/// Generates a MLDSA65 signature.
|
||||
/// - Parameters:
|
||||
/// - data: The data to sign.
|
||||
/// - Returns: The MLDSA65 signature.
|
||||
/// This method throws if CryptoKit encounters an error producing the signature.
|
||||
public func signature<D: DataProtocol>(for data: D) throws -> Data {
|
||||
return try impl.signature(for: data)
|
||||
}
|
||||
|
||||
/// Generates a MLDSA65 signature, with context.
|
||||
/// - Parameters:
|
||||
/// - data: The data to sign.
|
||||
/// - context: Context for the signature.
|
||||
/// - Returns: The MLDSA65 signature.
|
||||
/// This method throws if CryptoKit encounters an error producing the signature.
|
||||
public func signature<D: DataProtocol, C: DataProtocol>(for data: D, context: C) throws -> Data {
|
||||
return try impl.signature(for: data, context: context)
|
||||
}
|
||||
|
||||
/// The associated public key.
|
||||
public var publicKey: PublicKey {
|
||||
get {
|
||||
PublicKey(impl.publicKey)
|
||||
}
|
||||
}
|
||||
|
||||
/// Initializes a private key from an integrity-checked data representation.
|
||||
///
|
||||
/// - Parameter integrityCheckedRepresentation: The integrity-checked data representation of the private key.
|
||||
/// The parameter needs to be 64 bytes long, and contain the seed and a hash of the public key.
|
||||
public init<D: DataProtocol>(integrityCheckedRepresentation: D) throws {
|
||||
self.impl = try .init(integrityCheckedRepresentation: integrityCheckedRepresentation)
|
||||
let seedSize = MLDSAPrivateKeyImpl<MLDSA65>.seedSize
|
||||
guard integrityCheckedRepresentation.count == seedSize + 32 else {
|
||||
throw CryptoKitError.incorrectParameterSize
|
||||
}
|
||||
|
||||
let seed = Data(integrityCheckedRepresentation).subdata(in: 0..<seedSize)
|
||||
let publicKeyHashData = Data(integrityCheckedRepresentation).subdata(in: seedSize..<integrityCheckedRepresentation.count)
|
||||
let publicKeyHash = SHA3_256Digest(bytes: [UInt8](publicKeyHashData))
|
||||
|
||||
self.impl = try MLDSAPrivateKeyImpl<MLDSA65>(seedRepresentation: seed, publicKeyHash: publicKeyHash)
|
||||
}
|
||||
|
||||
/// The integrity-checked data representation of the private key.
|
||||
@@ -166,16 +188,38 @@ public enum MLDSA87: Sendable {}
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension MLDSA87 {
|
||||
/// The public key for MLDSA87.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public struct PublicKey: Sendable {
|
||||
var impl: MLDSAPublicKeyImpl<MLDSA87>
|
||||
|
||||
internal init(_ impl: MLDSAPublicKeyImpl<MLDSA87>) {
|
||||
self.impl = impl
|
||||
}
|
||||
|
||||
/// Parses a public key from a serialized representation.
|
||||
///
|
||||
/// - Parameter rawRepresentation: The public key, in the FIPS 204 standard serialization format.
|
||||
/// - Returns: The deserialized public key.
|
||||
public init<D: DataProtocol>(rawRepresentation: D) throws {
|
||||
self.impl = try MLDSAPublicKeyImpl(rawRepresentation: rawRepresentation)
|
||||
}
|
||||
|
||||
/// A serialized representation of the public key.
|
||||
///
|
||||
/// This property provides a representation of the public key in the FIPS 204 standard serialization format.
|
||||
public var rawRepresentation: Data {
|
||||
get {
|
||||
return self.impl.rawRepresentation
|
||||
}
|
||||
}
|
||||
|
||||
/// Verifies a MLDSA87 signature.
|
||||
/// - Parameters:
|
||||
/// - signature: The MLDSA87 signature to verify.
|
||||
/// - data: The signed data.
|
||||
/// - Returns: `true` if the signature is valid, `false` otherwise.
|
||||
public func isValidSignature<S: DataProtocol, D: DataProtocol>(signature: S, for data: D) -> Bool {
|
||||
self.impl.isValidSignature(signature: signature, for: data)
|
||||
public func isValidSignature<S: DataProtocol, D: DataProtocol>(_ signature: S, for data: D) -> Bool {
|
||||
return self.impl.isValidSignature(signature, for: data)
|
||||
}
|
||||
|
||||
/// Verifies a MLDSA87 signature, in a specific context.
|
||||
@@ -184,65 +228,24 @@ extension MLDSA87 {
|
||||
/// - data: The signed data.
|
||||
/// - context: Context for the signature.
|
||||
/// - Returns: `true` if the signature is valid in the specified context, `false` otherwise.
|
||||
public func isValidSignature<S: DataProtocol, D: DataProtocol, C: DataProtocol>(signature: S, for data: D, context: C) -> Bool {
|
||||
self.impl.isValidSignature(signature: signature, for: data, context: context)
|
||||
}
|
||||
|
||||
/// Parses a public key from a serialized representation.
|
||||
///
|
||||
/// - Parameter rawRepresentation: The public key, in the FIPS 204 standard serialization format.
|
||||
/// - Returns: The deserialized public key.
|
||||
public init<D: DataProtocol>(rawRepresentation: D) throws {
|
||||
self.impl = try .init(rawRepresentation: rawRepresentation)
|
||||
}
|
||||
|
||||
/// A serialized representation of the public key.
|
||||
///
|
||||
/// This property provides a representation of the public key in the FIPS 204 standard serialization format.
|
||||
public var rawRepresentation: Data {
|
||||
get {
|
||||
self.impl.rawRepresentation
|
||||
}
|
||||
}
|
||||
|
||||
fileprivate init(impl: MLDSAPublicKeyImpl<MLDSA87>) {
|
||||
self.impl = impl
|
||||
public func isValidSignature<S: DataProtocol, D: DataProtocol, C: DataProtocol>(_ signature: S, for data: D, context: C) -> Bool {
|
||||
return self.impl.isValidSignature(signature, for: data, context: context)
|
||||
}
|
||||
}
|
||||
|
||||
/// The private key for MLDSA87.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public struct PrivateKey: Signer, Sendable {
|
||||
var impl: MLDSAPrivateKeyImpl<MLDSA87>
|
||||
|
||||
/// Generates a MLDSA87 signature.
|
||||
/// - Parameters:
|
||||
/// - data: The data to sign.
|
||||
/// - Returns: The MLDSA87 signature.
|
||||
/// This method throws if CryptoKit encounters an error producing the signature.
|
||||
public func signature<D: DataProtocol>(for data: D) throws -> Data {
|
||||
try self.impl.signature(for: data)
|
||||
internal let impl: MLDSAPrivateKeyImpl<MLDSA87>
|
||||
|
||||
internal init(_ impl: MLDSAPrivateKeyImpl<MLDSA87>) {
|
||||
self.impl = impl
|
||||
}
|
||||
|
||||
/// Generates a MLDSA87 signature, with context.
|
||||
/// - Parameters:
|
||||
/// - data: The data to sign.
|
||||
/// - context: Context for the signature.
|
||||
/// - Returns: The MLDSA87 signature.
|
||||
/// This method throws if CryptoKit encounters an error producing the signature.
|
||||
public func signature<D: DataProtocol, C: DataProtocol>(for data: D, context: C) throws -> Data {
|
||||
try self.impl.signature(for: data, context: context)
|
||||
}
|
||||
|
||||
/// The associated public key.
|
||||
public var publicKey: PublicKey {
|
||||
get {
|
||||
PublicKey(impl: self.impl.publicKey)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// Initializes a new random private key.
|
||||
public init() throws {
|
||||
self.impl = try .init()
|
||||
let impl = try MLDSAPrivateKeyImpl<MLDSA87>()
|
||||
self = PrivateKey(impl)
|
||||
}
|
||||
|
||||
/// Initializes a private key from the seed representation.
|
||||
@@ -254,9 +257,13 @@ extension MLDSA87 {
|
||||
///
|
||||
/// If a public key is provided, a consistency check is performed between it and the derived public key.
|
||||
public init<D: DataProtocol>(seedRepresentation: D, publicKey: MLDSA87.PublicKey?) throws {
|
||||
self.impl = try .init(seedRepresentation: seedRepresentation, publicKey: publicKey?.impl)
|
||||
var publicKeyRawRepresentation: Data? = nil
|
||||
if publicKey != nil {
|
||||
publicKeyRawRepresentation = publicKey!.rawRepresentation
|
||||
}
|
||||
self.impl = try MLDSAPrivateKeyImpl<MLDSA87>(seedRepresentation: seedRepresentation, publicKeyRawRepresentation: publicKeyRawRepresentation)
|
||||
}
|
||||
|
||||
|
||||
/// The seed representation of the private key.
|
||||
///
|
||||
/// The seed representation is 32 bytes long, and is the parameter
|
||||
@@ -267,12 +274,47 @@ extension MLDSA87 {
|
||||
}
|
||||
}
|
||||
|
||||
/// Generates a MLDSA87 signature.
|
||||
/// - Parameters:
|
||||
/// - data: The data to sign.
|
||||
/// - Returns: The MLDSA87 signature.
|
||||
/// This method throws if CryptoKit encounters an error producing the signature.
|
||||
public func signature<D: DataProtocol>(for data: D) throws -> Data {
|
||||
return try impl.signature(for: data)
|
||||
}
|
||||
|
||||
/// Generates a MLDSA87 signature, with context.
|
||||
/// - Parameters:
|
||||
/// - data: The data to sign.
|
||||
/// - context: Context for the signature.
|
||||
/// - Returns: The MLDSA87 signature.
|
||||
/// This method throws if CryptoKit encounters an error producing the signature.
|
||||
public func signature<D: DataProtocol, C: DataProtocol>(for data: D, context: C) throws -> Data {
|
||||
return try impl.signature(for: data, context: context)
|
||||
}
|
||||
|
||||
/// The associated public key.
|
||||
public var publicKey: PublicKey {
|
||||
get {
|
||||
PublicKey(impl.publicKey)
|
||||
}
|
||||
}
|
||||
|
||||
/// Initializes a private key from an integrity-checked data representation.
|
||||
///
|
||||
/// - Parameter integrityCheckedRepresentation: The integrity-checked data representation of the private key.
|
||||
/// The parameter needs to be 64 bytes long, and contain the seed and a hash of the public key.
|
||||
public init<D: DataProtocol>(integrityCheckedRepresentation: D) throws {
|
||||
self.impl = try .init(integrityCheckedRepresentation: integrityCheckedRepresentation)
|
||||
let seedSize = MLDSAPrivateKeyImpl<MLDSA87>.seedSize
|
||||
guard integrityCheckedRepresentation.count == seedSize + 32 else {
|
||||
throw CryptoKitError.incorrectParameterSize
|
||||
}
|
||||
|
||||
let seed = Data(integrityCheckedRepresentation).subdata(in: 0..<seedSize)
|
||||
let publicKeyHashData = Data(integrityCheckedRepresentation).subdata(in: seedSize..<integrityCheckedRepresentation.count)
|
||||
let publicKeyHash = SHA3_256Digest(bytes: [UInt8](publicKeyHashData))
|
||||
|
||||
self.impl = try MLDSAPrivateKeyImpl<MLDSA87>(seedRepresentation: seed, publicKeyHash: publicKeyHash)
|
||||
}
|
||||
|
||||
/// The integrity-checked data representation of the private key.
|
||||
|
||||
@@ -19,22 +19,21 @@ public import FoundationEssentials
|
||||
#else
|
||||
public import Foundation
|
||||
#endif
|
||||
%{
|
||||
MLDSA_VARIANTS = [{"name": "MLDSA65"}, {"name": "MLDSA87"}]
|
||||
}%
|
||||
|
||||
#if (!CRYPTO_IN_SWIFTPM_FORCE_BUILD_API) || CRYPTOKIT_NO_ACCESS_TO_FOUNDATION
|
||||
typealias MLDSAPublicKeyImpl = CoreCryptoMLDSAPublicKeyImpl
|
||||
typealias MLDSAPrivateKeyImpl = CoreCryptoMLDSAPrivateKeyImpl
|
||||
typealias MLDSAPublicKeyImpl = CorecryptoMLDSAPublicKeyImpl
|
||||
typealias MLDSAPrivateKeyImpl = CorecryptoMLDSAPrivateKeyImpl
|
||||
#else
|
||||
typealias MLDSAPublicKeyImpl = OpenSSLMLDSAPublicKeyImpl
|
||||
typealias MLDSAPrivateKeyImpl = OpenSSLMLDSAPrivateKeyImpl
|
||||
#endif
|
||||
|
||||
%{
|
||||
MLDSA_VARIANTS = [{"name": "MLDSA65", "ccinfo": "ccmldsa65()!"}, {"name": "MLDSA87", "ccinfo": "ccmldsa87()!"}]
|
||||
}%
|
||||
% for MLDSA_VARIANT in MLDSA_VARIANTS:
|
||||
%{
|
||||
NAME = MLDSA_VARIANT["name"]
|
||||
INFO = MLDSA_VARIANT["ccinfo"]
|
||||
}%
|
||||
|
||||
/// The ${NAME} Digital Signature Algorithm
|
||||
@@ -44,14 +43,31 @@ extension ${NAME} {
|
||||
/// The public key for ${NAME}.
|
||||
public struct PublicKey: Sendable {
|
||||
var impl: MLDSAPublicKeyImpl<${NAME}>
|
||||
|
||||
/// Parses a public key from a serialized representation.
|
||||
///
|
||||
/// - Parameter rawRepresentation: The public key, in the FIPS 204 standard serialization format.
|
||||
/// - Returns: The deserialized public key.
|
||||
public init<D: DataProtocol>(rawRepresentation: D) throws {
|
||||
self.impl = try MLDSAPublicKeyImpl(rawRepresentation: rawRepresentation)
|
||||
}
|
||||
|
||||
/// A serialized representation of the public key.
|
||||
///
|
||||
/// This property provides a representation of the public key in the FIPS 204 standard serialization format.
|
||||
public var rawRepresentation: Data {
|
||||
get {
|
||||
return self.impl.rawRepresentation
|
||||
}
|
||||
}
|
||||
|
||||
/// Verifies a ${NAME} signature.
|
||||
/// - Parameters:
|
||||
/// - signature: The ${NAME} signature to verify.
|
||||
/// - data: The signed data.
|
||||
/// - Returns: `true` if the signature is valid, `false` otherwise.
|
||||
public func isValidSignature<S: DataProtocol, D: DataProtocol>(signature: S, for data: D) -> Bool {
|
||||
self.impl.isValidSignature(signature: signature, for: data)
|
||||
public func isValidSignature<S: DataProtocol, D: DataProtocol>(_ signature: S, for data: D) -> Bool {
|
||||
return self.impl.isValidSignature(signature, for: data)
|
||||
}
|
||||
|
||||
/// Verifies a ${NAME} signature, in a specific context.
|
||||
@@ -60,65 +76,23 @@ extension ${NAME} {
|
||||
/// - data: The signed data.
|
||||
/// - context: Context for the signature.
|
||||
/// - Returns: `true` if the signature is valid in the specified context, `false` otherwise.
|
||||
public func isValidSignature<S: DataProtocol, D: DataProtocol, C: DataProtocol>(signature: S, for data: D, context: C) -> Bool {
|
||||
self.impl.isValidSignature(signature: signature, for: data, context: context)
|
||||
}
|
||||
|
||||
/// Parses a public key from a serialized representation.
|
||||
///
|
||||
/// - Parameter rawRepresentation: The public key, in the FIPS 204 standard serialization format.
|
||||
/// - Returns: The deserialized public key.
|
||||
public init<D: DataProtocol>(rawRepresentation: D) throws {
|
||||
self.impl = try .init(rawRepresentation: rawRepresentation)
|
||||
}
|
||||
|
||||
/// A serialized representation of the public key.
|
||||
///
|
||||
/// This property provides a representation of the public key in the FIPS 204 standard serialization format.
|
||||
public var rawRepresentation: Data {
|
||||
get {
|
||||
self.impl.rawRepresentation
|
||||
}
|
||||
}
|
||||
|
||||
fileprivate init(impl: MLDSAPublicKeyImpl<${NAME}>) {
|
||||
self.impl = impl
|
||||
public func isValidSignature<S: DataProtocol, D: DataProtocol, C: DataProtocol>(_ signature: S, for data: D, context: C) -> Bool {
|
||||
return self.impl.isValidSignature(signature, for: data, context: context)
|
||||
}
|
||||
}
|
||||
|
||||
/// The private key for ${NAME}.
|
||||
public struct PrivateKey: Signer, Sendable {
|
||||
var impl: MLDSAPrivateKeyImpl<${NAME}>
|
||||
|
||||
/// Generates a ${NAME} signature.
|
||||
/// - Parameters:
|
||||
/// - data: The data to sign.
|
||||
/// - Returns: The ${NAME} signature.
|
||||
/// This method throws if CryptoKit encounters an error producing the signature.
|
||||
public func signature<D: DataProtocol>(for data: D) throws -> Data {
|
||||
try self.impl.signature(for: data)
|
||||
internal let impl: MLDSAPrivateKeyImpl<${NAME}>
|
||||
|
||||
internal init(_ impl: MLDSAPrivateKeyImpl<${NAME}>) {
|
||||
self.impl = impl
|
||||
}
|
||||
|
||||
/// Generates a ${NAME} signature, with context.
|
||||
/// - Parameters:
|
||||
/// - data: The data to sign.
|
||||
/// - context: Context for the signature.
|
||||
/// - Returns: The ${NAME} signature.
|
||||
/// This method throws if CryptoKit encounters an error producing the signature.
|
||||
public func signature<D: DataProtocol, C: DataProtocol>(for data: D, context: C) throws -> Data {
|
||||
try self.impl.signature(for: data, context: context)
|
||||
}
|
||||
|
||||
/// The associated public key.
|
||||
public var publicKey: PublicKey {
|
||||
get {
|
||||
PublicKey(impl: self.impl.publicKey)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// Initializes a new random private key.
|
||||
public init() throws {
|
||||
self.impl = try .init()
|
||||
let impl = try MLDSAPrivateKeyImpl<${NAME}>()
|
||||
self = PrivateKey(impl)
|
||||
}
|
||||
|
||||
/// Initializes a private key from the seed representation.
|
||||
@@ -130,9 +104,13 @@ extension ${NAME} {
|
||||
///
|
||||
/// If a public key is provided, a consistency check is performed between it and the derived public key.
|
||||
public init<D: DataProtocol>(seedRepresentation: D, publicKey: ${NAME}.PublicKey?) throws {
|
||||
self.impl = try .init(seedRepresentation: seedRepresentation, publicKey: publicKey?.impl)
|
||||
var publicKeyRawRepresentation: Data? = nil
|
||||
if publicKey != nil {
|
||||
publicKeyRawRepresentation = publicKey!.rawRepresentation
|
||||
}
|
||||
self.impl = try MLDSAPrivateKeyImpl<${NAME}>(seedRepresentation: seedRepresentation, publicKeyRawRepresentation: publicKeyRawRepresentation)
|
||||
}
|
||||
|
||||
|
||||
/// The seed representation of the private key.
|
||||
///
|
||||
/// The seed representation is 32 bytes long, and is the parameter
|
||||
@@ -143,12 +121,47 @@ extension ${NAME} {
|
||||
}
|
||||
}
|
||||
|
||||
/// Generates a ${NAME} signature.
|
||||
/// - Parameters:
|
||||
/// - data: The data to sign.
|
||||
/// - Returns: The ${NAME} signature.
|
||||
/// This method throws if CryptoKit encounters an error producing the signature.
|
||||
public func signature<D: DataProtocol>(for data: D) throws -> Data {
|
||||
return try impl.signature(for: data)
|
||||
}
|
||||
|
||||
/// Generates a ${NAME} signature, with context.
|
||||
/// - Parameters:
|
||||
/// - data: The data to sign.
|
||||
/// - context: Context for the signature.
|
||||
/// - Returns: The ${NAME} signature.
|
||||
/// This method throws if CryptoKit encounters an error producing the signature.
|
||||
public func signature<D: DataProtocol, C: DataProtocol>(for data: D, context: C) throws -> Data {
|
||||
return try impl.signature(for: data, context: context)
|
||||
}
|
||||
|
||||
/// The associated public key.
|
||||
public var publicKey: PublicKey {
|
||||
get {
|
||||
PublicKey(impl.publicKey)
|
||||
}
|
||||
}
|
||||
|
||||
/// Initializes a private key from an integrity-checked data representation.
|
||||
///
|
||||
/// - Parameter integrityCheckedRepresentation: The integrity-checked data representation of the private key.
|
||||
/// The parameter needs to be 64 bytes long, and contain the seed and a hash of the public key.
|
||||
public init<D: DataProtocol>(integrityCheckedRepresentation: D) throws {
|
||||
self.impl = try .init(integrityCheckedRepresentation: integrityCheckedRepresentation)
|
||||
let seedSize = MLDSAPrivateKeyImpl<${NAME}>.seedSize
|
||||
guard integrityCheckedRepresentation.count == seedSize + 32 else {
|
||||
throw CryptoKitError.incorrectParameterSize
|
||||
}
|
||||
|
||||
let seed = Data(integrityCheckedRepresentation).subdata(in: 0..<seedSize)
|
||||
let publicKeyHashData = Data(integrityCheckedRepresentation).subdata(in: seedSize..<integrityCheckedRepresentation.count)
|
||||
let publicKeyHash = SHA3_256Digest(bytes: [UInt8](publicKeyHashData))
|
||||
|
||||
self.impl = try MLDSAPrivateKeyImpl<${NAME}>(seedRepresentation: seed, publicKeyHash: publicKeyHash)
|
||||
}
|
||||
|
||||
/// The integrity-checked data representation of the private key.
|
||||
|
||||
@@ -22,13 +22,13 @@
|
||||
#endif
|
||||
|
||||
extension XWingMLKEM768X25519.PrivateKey {
|
||||
static func generateWithRng(ccrngState: SequenceDrbg) throws -> Self {
|
||||
static func generateWithRng(rngState: SequenceDrbg) throws -> Self {
|
||||
// We're going to generate a "random" seed.
|
||||
var seed: [UInt8] = []
|
||||
seed.reserveCapacity(32)
|
||||
|
||||
for i in 0..<32 {
|
||||
seed.append(ccrngState.state[i % ccrngState.state.count])
|
||||
seed.append(rngState.state[i % rngState.state.count])
|
||||
}
|
||||
|
||||
return try Self(seedRepresentation: seed, publicKey: nil)
|
||||
@@ -36,13 +36,13 @@ extension XWingMLKEM768X25519.PrivateKey {
|
||||
}
|
||||
|
||||
extension XWingMLKEM768X25519.PublicKey {
|
||||
func encapsulateWithRng(ccrngState: SequenceDrbg) throws -> KEM.EncapsulationResult {
|
||||
func encapsulateWithRng(rngState: SequenceDrbg) throws -> KEM.EncapsulationResult {
|
||||
// We're going to generate "random" entropy
|
||||
var seed: [UInt8] = []
|
||||
seed.reserveCapacity(64)
|
||||
|
||||
for i in 0..<64 {
|
||||
seed.append(ccrngState.state[i % ccrngState.state.count])
|
||||
seed.append(rngState.state[i % rngState.state.count])
|
||||
}
|
||||
|
||||
return try self.impl.encapsulateWithOptionalEntropy(entropy: seed)
|
||||
|
||||
@@ -12,7 +12,6 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
import XCTest
|
||||
|
||||
#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
|
||||
// Skip tests that require @testable imports of CryptoKit.
|
||||
#else
|
||||
@@ -40,28 +39,20 @@ final class MLKEMKeyGenTests: XCTestCase {
|
||||
let katTests = try processKeyGenKATFile(filename:"MLKEM768_BSSLKAT")
|
||||
for katTest in katTests {
|
||||
let publicKey = try MLKEM768.PublicKey(rawRepresentation: katTest.pk)
|
||||
#if false
|
||||
let privateKey = try MLKEM768.PrivateKey(seedRepresentation: katTest.dz, publicKey: publicKey)
|
||||
let publicKeyHash = Data(SHA3_256.hash(data: privateKey.publicKey.rawRepresentation))
|
||||
#else
|
||||
let privateKey = try MLKEM768.PrivateKey(seedRepresentation: katTest.dz, publicKey: nil)
|
||||
#endif
|
||||
|
||||
XCTAssert(privateKey.seedRepresentation == katTest.dz)
|
||||
#if false
|
||||
XCTAssert(privateKey.integrityCheckedRepresentation == katTest.dz + publicKeyHash)
|
||||
#endif
|
||||
XCTAssert(privateKey.publicKey.rawRepresentation == katTest.pk)
|
||||
|
||||
// Try initializing keys with incorrect integrity checks
|
||||
#if false
|
||||
let randomPrivateKey = try MLKEM768.PrivateKey.generate()
|
||||
let randomPublicKeyHash = Data(SHA3_256.hash(data: randomPrivateKey.publicKey.rawRepresentation))
|
||||
XCTAssertThrowsError(try MLKEM768.PrivateKey(seedRepresentation: privateKey.seedRepresentation, publicKey: randomPrivateKey.publicKey), error: KEM.Errors.publicKeyMismatchDuringInitialization)
|
||||
XCTAssertThrowsError(try MLKEM768.PrivateKey(seedRepresentation: randomPrivateKey.seedRepresentation, publicKey: publicKey), error: KEM.Errors.publicKeyMismatchDuringInitialization)
|
||||
XCTAssertThrowsError(try MLKEM768.PrivateKey(integrityCheckedRepresentation: privateKey.seedRepresentation + randomPublicKeyHash), error: KEM.Errors.publicKeyMismatchDuringInitialization)
|
||||
XCTAssertThrowsError(try MLKEM768.PrivateKey(integrityCheckedRepresentation: randomPrivateKey.seedRepresentation + publicKeyHash), error: KEM.Errors.publicKeyMismatchDuringInitialization)
|
||||
#endif
|
||||
}
|
||||
|
||||
}
|
||||
@@ -70,28 +61,20 @@ final class MLKEMKeyGenTests: XCTestCase {
|
||||
let katTests = try processKeyGenKATFile(filename:"MLKEM1024_BSSLKAT")
|
||||
for katTest in katTests {
|
||||
let publicKey = try MLKEM1024.PublicKey(rawRepresentation: katTest.pk)
|
||||
#if false
|
||||
let privateKey = try MLKEM1024.PrivateKey(seedRepresentation: katTest.dz, publicKey: publicKey)
|
||||
let publicKeyHash = Data(SHA3_256.hash(data: privateKey.publicKey.rawRepresentation))
|
||||
#else
|
||||
let privateKey = try MLKEM1024.PrivateKey(seedRepresentation: katTest.dz, publicKey: nil)
|
||||
#endif
|
||||
|
||||
XCTAssert(privateKey.seedRepresentation == katTest.dz)
|
||||
#if false
|
||||
XCTAssert(privateKey.integrityCheckedRepresentation == katTest.dz + publicKeyHash)
|
||||
#endif
|
||||
XCTAssert(privateKey.publicKey.rawRepresentation == katTest.pk)
|
||||
|
||||
// Try initializing keys with incorrect integrity checks
|
||||
#if false
|
||||
let randomPrivateKey = try MLKEM1024.PrivateKey.generate()
|
||||
let randomPublicKeyHash = Data(SHA3_256.hash(data: randomPrivateKey.publicKey.rawRepresentation))
|
||||
XCTAssertThrowsError(try MLKEM1024.PrivateKey(seedRepresentation: privateKey.seedRepresentation, publicKey: randomPrivateKey.publicKey), error: KEM.Errors.publicKeyMismatchDuringInitialization)
|
||||
XCTAssertThrowsError(try MLKEM1024.PrivateKey(seedRepresentation: randomPrivateKey.seedRepresentation, publicKey: publicKey), error: KEM.Errors.publicKeyMismatchDuringInitialization)
|
||||
XCTAssertThrowsError(try MLKEM1024.PrivateKey(integrityCheckedRepresentation: privateKey.seedRepresentation + randomPublicKeyHash), error: KEM.Errors.publicKeyMismatchDuringInitialization)
|
||||
XCTAssertThrowsError(try MLKEM1024.PrivateKey(integrityCheckedRepresentation: randomPrivateKey.seedRepresentation + publicKeyHash), error: KEM.Errors.publicKeyMismatchDuringInitialization)
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,6 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
import XCTest
|
||||
|
||||
#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
|
||||
// Skip tests that require @testable imports of CryptoKit.
|
||||
#else
|
||||
@@ -31,12 +30,8 @@ final class MLKEMTests: XCTestCase {
|
||||
try XCTAssert(publicKey.rawRepresentation == MLKEM768.PublicKey(rawRepresentation: publicKey.rawRepresentation).rawRepresentation)
|
||||
|
||||
// Test Private Key serialization
|
||||
#if false
|
||||
try XCTAssert(privateKey.seedRepresentation == MLKEM768.PrivateKey(seedRepresentation: privateKey.seedRepresentation, publicKey: publicKey).seedRepresentation)
|
||||
try XCTAssert(privateKey.integrityCheckedRepresentation == MLKEM768.PrivateKey(integrityCheckedRepresentation: privateKey.integrityCheckedRepresentation).integrityCheckedRepresentation)
|
||||
#else
|
||||
try XCTAssert(privateKey.seedRepresentation == MLKEM768.PrivateKey(seedRepresentation: privateKey.seedRepresentation, publicKey: nil).seedRepresentation)
|
||||
#endif
|
||||
|
||||
let er = try publicKey.encapsulate()
|
||||
let ss = try privateKey.decapsulate(er.encapsulated)
|
||||
@@ -52,12 +47,8 @@ final class MLKEMTests: XCTestCase {
|
||||
try XCTAssert(publicKey.rawRepresentation == MLKEM1024.PublicKey(rawRepresentation: publicKey.rawRepresentation).rawRepresentation)
|
||||
|
||||
// Test Private Key serialization
|
||||
#if false
|
||||
try XCTAssert(privateKey.seedRepresentation == MLKEM1024.PrivateKey(seedRepresentation: privateKey.seedRepresentation, publicKey: publicKey).seedRepresentation)
|
||||
try XCTAssert(privateKey.integrityCheckedRepresentation == MLKEM1024.PrivateKey(integrityCheckedRepresentation: privateKey.integrityCheckedRepresentation).integrityCheckedRepresentation)
|
||||
#else
|
||||
try XCTAssert(privateKey.seedRepresentation == MLKEM1024.PrivateKey(seedRepresentation: privateKey.seedRepresentation, publicKey: nil).seedRepresentation)
|
||||
#endif
|
||||
|
||||
let er = try publicKey.encapsulate()
|
||||
let ss = try privateKey.decapsulate(er.encapsulated)
|
||||
@@ -80,6 +71,7 @@ final class MLKEMTests: XCTestCase {
|
||||
|
||||
func test768KAT() throws {
|
||||
#if CRYPTO_IN_SWIFTPM
|
||||
// No support for encapsulateWithSeed in BoringSSL.
|
||||
throw XCTSkip()
|
||||
#else
|
||||
let katTests = try processKATFile(filename:"MLKEM768KAT")
|
||||
@@ -87,12 +79,12 @@ final class MLKEMTests: XCTestCase {
|
||||
let rndGen = try Drbg(katTest.rngSeed)
|
||||
|
||||
var keyGenSeed = Data(count: 64)
|
||||
keyGenSeed.withUnsafeMutableBytes { buffer in
|
||||
buffer.initializeWithRandomBytes(count: buffer.count, ccrngState: rndGen.detRngPtr)
|
||||
try keyGenSeed.withUnsafeMutableBytes { buffer in
|
||||
try buffer.initializeWithRandomBytes(count: buffer.count, rngState: rndGen)
|
||||
}
|
||||
var encapSeed = Data(count: 32)
|
||||
encapSeed.withUnsafeMutableBytes { buffer in
|
||||
buffer.initializeWithRandomBytes(count: buffer.count, ccrngState: rndGen.detRngPtr)
|
||||
try encapSeed.withUnsafeMutableBytes { buffer in
|
||||
try buffer.initializeWithRandomBytes(count: buffer.count, rngState: rndGen)
|
||||
}
|
||||
|
||||
let privateKey = try MLKEM768.PrivateKey.generateWithSeed(keyGenSeed) // 2 * 32 bytes
|
||||
@@ -110,6 +102,7 @@ final class MLKEMTests: XCTestCase {
|
||||
|
||||
func test1024KAT() throws {
|
||||
#if CRYPTO_IN_SWIFTPM
|
||||
// No support for encapsulateWithSeed in BoringSSL.
|
||||
throw XCTSkip()
|
||||
#else
|
||||
let katTests = try processKATFile(filename:"MLKEM1024KAT")
|
||||
@@ -117,12 +110,12 @@ final class MLKEMTests: XCTestCase {
|
||||
let rndGen = try Drbg(katTest.rngSeed)
|
||||
|
||||
var keyGenSeed = Data(count: 64)
|
||||
keyGenSeed.withUnsafeMutableBytes { buffer in
|
||||
buffer.initializeWithRandomBytes(count: buffer.count, ccrngState: rndGen.detRngPtr)
|
||||
try keyGenSeed.withUnsafeMutableBytes { buffer in
|
||||
try buffer.initializeWithRandomBytes(count: buffer.count, rngState: rndGen)
|
||||
}
|
||||
var encapSeed = Data(count: 32)
|
||||
encapSeed.withUnsafeMutableBytes { buffer in
|
||||
buffer.initializeWithRandomBytes(count: buffer.count, ccrngState: rndGen.detRngPtr)
|
||||
try encapSeed.withUnsafeMutableBytes { buffer in
|
||||
try buffer.initializeWithRandomBytes(count: buffer.count, rngState: rndGen)
|
||||
}
|
||||
|
||||
let privateKey = try MLKEM1024.PrivateKey.generateWithSeed(keyGenSeed)
|
||||
|
||||
@@ -12,7 +12,6 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
import XCTest
|
||||
|
||||
#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
|
||||
// Skip tests that require @testable imports of CryptoKit.
|
||||
#else
|
||||
@@ -28,10 +27,12 @@ final class XWingTests: XCTestCase {
|
||||
|
||||
let publicKey = privateKey.publicKey
|
||||
|
||||
try XCTAssert(publicKey.rawRepresentation == XWingMLKEM768X25519.PublicKey(dataRepresentation: publicKey.rawRepresentation).rawRepresentation)
|
||||
try XCTAssert(privateKey.dataRepresentation == XWingMLKEM768X25519.PrivateKey(bytes: privateKey.dataRepresentation).dataRepresentation)
|
||||
try XCTAssert(publicKey.rawRepresentation == XWingMLKEM768X25519.PublicKey(rawRepresentation: publicKey.rawRepresentation).rawRepresentation)
|
||||
try XCTAssert(privateKey.seedRepresentation == XWingMLKEM768X25519.PrivateKey(seedRepresentation: privateKey.seedRepresentation, publicKey: nil).seedRepresentation)
|
||||
try XCTAssert(privateKey.seedRepresentation == XWingMLKEM768X25519.PrivateKey(seedRepresentation: privateKey.seedRepresentation, publicKey: publicKey).seedRepresentation)
|
||||
try XCTAssert(privateKey.integrityCheckedRepresentation == XWingMLKEM768X25519.PrivateKey(integrityCheckedRepresentation: privateKey.integrityCheckedRepresentation).integrityCheckedRepresentation)
|
||||
|
||||
let er = try publicKey.encapsulate()
|
||||
let er = try privateKey.publicKey.encapsulate()
|
||||
let ss = try privateKey.decapsulate(er.encapsulated)
|
||||
|
||||
XCTAssert(er.sharedSecret == ss)
|
||||
@@ -54,12 +55,11 @@ final class XWingTests: XCTestCase {
|
||||
let katTests = try processKATFile(filename:"test-vectors")
|
||||
for katTest in katTests {
|
||||
let privateKeyDrbg = try SequenceDrbg(katTest.seed)
|
||||
let privateKey = try XWingMLKEM768X25519.PrivateKey.generateWithRng(ccrngState: privateKeyDrbg.detRngPtr)
|
||||
XCTAssertEqual(privateKey.dataRepresentation, katTest.sk + katTest.pk)
|
||||
let privateKey = try XWingMLKEM768X25519.PrivateKey.generateWithRng(rngState: privateKeyDrbg)
|
||||
XCTAssertEqual(privateKey.publicKey.rawRepresentation, katTest.pk)
|
||||
|
||||
let encapDrbg = try SequenceDrbg(katTest.eseed)
|
||||
let encapsulatedKey = try privateKey.publicKey.encapsulateWithRng(ccrngState: encapDrbg.detRngPtr)
|
||||
let encapsulatedKey = try privateKey.publicKey.encapsulateWithRng(rngState: encapDrbg)
|
||||
XCTAssertEqual(encapsulatedKey.encapsulated, katTest.ct)
|
||||
XCTAssertEqual(encapsulatedKey.sharedSecret.dataRepresentation, katTest.ss)
|
||||
let retrievedSharedSecret = try privateKey.decapsulate(encapsulatedKey.encapsulated)
|
||||
@@ -70,25 +70,19 @@ final class XWingTests: XCTestCase {
|
||||
func testPrivateKeyRepresentations() throws {
|
||||
let privateKey = try XWingMLKEM768X25519.PrivateKey.generate()
|
||||
XCTAssertEqual(privateKey.seedRepresentation.count, 32)
|
||||
#if false
|
||||
XCTAssertEqual(privateKey.integrityCheckedRepresentation.count, 64)
|
||||
XCTAssertEqual(privateKey.integrityCheckedRepresentation.dropLast(32), privateKey.seedRepresentation)
|
||||
#endif
|
||||
|
||||
let recoveredPrivateKey = try XWingMLKEM768X25519.PrivateKey(seedRepresentation: privateKey.seedRepresentation, publicKeyHash: nil)
|
||||
XCTAssertNotNil(recoveredPrivateKey)
|
||||
#if false
|
||||
XCTAssertEqual(recoveredPrivateKey.integrityCheckedRepresentation, privateKey.integrityCheckedRepresentation)
|
||||
#endif
|
||||
|
||||
#if false
|
||||
let otherKey = try XWingMLKEM768X25519.PrivateKey.generate()
|
||||
XCTAssertThrowsError(try XWingMLKEM768X25519.PrivateKey(seedRepresentation: privateKey.seedRepresentation, publicKeyHash: SHA3_256.hash(data: otherKey.publicKey.rawRepresentation)), error: KEM.Errors.publicKeyMismatchDuringInitialization)
|
||||
|
||||
let exportedFormat = privateKey.integrityCheckedRepresentation
|
||||
let importedKey = try XWingMLKEM768X25519.PrivateKey.init(integrityCheckedRepresentation: exportedFormat)
|
||||
XCTAssertEqual(importedKey.seedRepresentation, privateKey.seedRepresentation)
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -12,7 +12,6 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
import XCTest
|
||||
|
||||
#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
|
||||
// Skip tests that require @testable imports of CryptoKit.
|
||||
#else
|
||||
@@ -40,28 +39,20 @@ final class MLDSAKeyGenTests: XCTestCase {
|
||||
let katTests = try processKeyGenKATFile(filename: "MLDSA65_KeyGen_KAT")
|
||||
for katTest in katTests {
|
||||
let publicKey = try MLDSA65.PublicKey(rawRepresentation: katTest.pk)
|
||||
#if false
|
||||
let privateKey = try MLDSA65.PrivateKey(seedRepresentation: katTest.seed, publicKey: publicKey)
|
||||
let publicKeyHash = Data(SHA3_256.hash(data: privateKey.publicKey.rawRepresentation))
|
||||
#else
|
||||
let privateKey = try MLDSA65.PrivateKey(seedRepresentation: katTest.seed, publicKey: nil)
|
||||
#endif
|
||||
|
||||
XCTAssert(privateKey.seedRepresentation == katTest.seed)
|
||||
#if false
|
||||
XCTAssert(privateKey.integrityCheckedRepresentation == katTest.seed + publicKeyHash)
|
||||
#endif
|
||||
XCTAssert(privateKey.publicKey.rawRepresentation == katTest.pk)
|
||||
|
||||
// Try initializing keys with incorrect integrity checks
|
||||
let randomPrivateKey = try MLDSA65.PrivateKey()
|
||||
#if false
|
||||
let randomPublicKeyHash = Data(SHA3_256.hash(data: randomPrivateKey.publicKey.rawRepresentation))
|
||||
XCTAssertThrowsError(try MLDSA65.PrivateKey(seedRepresentation: privateKey.seedRepresentation, publicKey: randomPrivateKey.publicKey), error: CryptoKitError.unwrapFailure)
|
||||
XCTAssertThrowsError(try MLDSA65.PrivateKey(seedRepresentation: randomPrivateKey.seedRepresentation, publicKey: publicKey), error: CryptoKitError.unwrapFailure)
|
||||
XCTAssertThrowsError(try MLDSA65.PrivateKey(integrityCheckedRepresentation: privateKey.seedRepresentation + randomPublicKeyHash), error: CryptoKitError.unwrapFailure)
|
||||
XCTAssertThrowsError(try MLDSA65.PrivateKey(integrityCheckedRepresentation: randomPrivateKey.seedRepresentation + publicKeyHash), error: CryptoKitError.unwrapFailure)
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,28 +60,20 @@ final class MLDSAKeyGenTests: XCTestCase {
|
||||
let katTests = try processKeyGenKATFile(filename: "MLDSA87_KeyGen_KAT")
|
||||
for katTest in katTests {
|
||||
let publicKey = try MLDSA87.PublicKey(rawRepresentation: katTest.pk)
|
||||
#if false
|
||||
let privateKey = try MLDSA87.PrivateKey(seedRepresentation: katTest.seed, publicKey: publicKey)
|
||||
let publicKeyHash = Data(SHA3_256.hash(data: privateKey.publicKey.rawRepresentation))
|
||||
#else
|
||||
let privateKey = try MLDSA87.PrivateKey(seedRepresentation: katTest.seed, publicKey: nil)
|
||||
#endif
|
||||
|
||||
XCTAssert(privateKey.seedRepresentation == katTest.seed)
|
||||
#if false
|
||||
XCTAssert(privateKey.integrityCheckedRepresentation == katTest.seed + publicKeyHash)
|
||||
#endif
|
||||
XCTAssert(privateKey.publicKey.rawRepresentation == katTest.pk)
|
||||
|
||||
// Try initializing keys with incorrect integrity checks
|
||||
let randomPrivateKey = try MLDSA87.PrivateKey()
|
||||
#if false
|
||||
let randomPublicKeyHash = Data(SHA3_256.hash(data: randomPrivateKey.publicKey.rawRepresentation))
|
||||
XCTAssertThrowsError(try MLDSA87.PrivateKey(seedRepresentation: privateKey.seedRepresentation, publicKey: randomPrivateKey.publicKey), error: CryptoKitError.unwrapFailure)
|
||||
XCTAssertThrowsError(try MLDSA87.PrivateKey(seedRepresentation: randomPrivateKey.seedRepresentation, publicKey: publicKey), error: CryptoKitError.unwrapFailure)
|
||||
XCTAssertThrowsError(try MLDSA87.PrivateKey(integrityCheckedRepresentation: privateKey.seedRepresentation + randomPublicKeyHash), error: CryptoKitError.unwrapFailure)
|
||||
XCTAssertThrowsError(try MLDSA87.PrivateKey(integrityCheckedRepresentation: randomPrivateKey.seedRepresentation + publicKeyHash), error: CryptoKitError.unwrapFailure)
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,6 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
import XCTest
|
||||
|
||||
#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
|
||||
// Skip tests that require @testable imports of CryptoKit.
|
||||
#else
|
||||
@@ -31,27 +30,25 @@ final class MLDSATests: XCTestCase {
|
||||
try XCTAssert(publicKey.rawRepresentation == MLDSA65.PublicKey(rawRepresentation: publicKey.rawRepresentation).rawRepresentation)
|
||||
|
||||
// Test Private Key serialization
|
||||
#if false
|
||||
try XCTAssert(privateKey.seedRepresentation == MLDSA65.PrivateKey(seedRepresentation: privateKey.seedRepresentation, publicKey: publicKey).seedRepresentation)
|
||||
try XCTAssert(privateKey.integrityCheckedRepresentation == MLDSA65.PrivateKey(integrityCheckedRepresentation: privateKey.integrityCheckedRepresentation).integrityCheckedRepresentation)
|
||||
#endif
|
||||
|
||||
// Test signing without a context
|
||||
let message = Data("ML-DSA test message".utf8)
|
||||
let signature = try privateKey.signature(for: message)
|
||||
XCTAssertNotNil(signature)
|
||||
let isValid = publicKey.isValidSignature(signature: signature, for: message)
|
||||
let isValid = publicKey.isValidSignature(signature, for: message)
|
||||
XCTAssertTrue(isValid)
|
||||
|
||||
// Test signing with a context
|
||||
let context = Data("ML-DSA test context".utf8)
|
||||
let signatureWithContext = try privateKey.signature(for: message, context: context)
|
||||
let isValidWithContext = publicKey.isValidSignature(signature: signatureWithContext, for: message, context: context)
|
||||
let isValidWithContext = publicKey.isValidSignature(signatureWithContext, for: message, context: context)
|
||||
XCTAssertTrue(isValidWithContext)
|
||||
|
||||
// Check that invalid signatures (mismatching contexts) fail
|
||||
XCTAssertFalse(publicKey.isValidSignature(signature: signature, for: message, context: context))
|
||||
XCTAssertFalse(publicKey.isValidSignature(signature: signatureWithContext, for: message))
|
||||
XCTAssertFalse(publicKey.isValidSignature(signature, for: message, context: context))
|
||||
XCTAssertFalse(publicKey.isValidSignature(signatureWithContext, for: message))
|
||||
}
|
||||
|
||||
func testMLDSA87() throws {
|
||||
@@ -62,27 +59,25 @@ final class MLDSATests: XCTestCase {
|
||||
try XCTAssert(publicKey.rawRepresentation == MLDSA87.PublicKey(rawRepresentation: publicKey.rawRepresentation).rawRepresentation)
|
||||
|
||||
// Test Private Key serialization
|
||||
#if false
|
||||
try XCTAssert(privateKey.seedRepresentation == MLDSA87.PrivateKey(seedRepresentation: privateKey.seedRepresentation, publicKey: publicKey).seedRepresentation)
|
||||
try XCTAssert(privateKey.integrityCheckedRepresentation == MLDSA87.PrivateKey(integrityCheckedRepresentation: privateKey.integrityCheckedRepresentation).integrityCheckedRepresentation)
|
||||
#endif
|
||||
|
||||
// Test signing without a context
|
||||
let message = Data("ML-DSA test message".utf8)
|
||||
let signature = try privateKey.signature(for: message)
|
||||
XCTAssertNotNil(signature)
|
||||
let isValid = publicKey.isValidSignature(signature: signature, for: message)
|
||||
let isValid = publicKey.isValidSignature(signature, for: message)
|
||||
XCTAssertTrue(isValid)
|
||||
|
||||
// Test signing with a context
|
||||
let context = Data("ML-DSA test context".utf8)
|
||||
let signatureWithContext = try privateKey.signature(for: message, context: context)
|
||||
let isValidWithContext = publicKey.isValidSignature(signature: signatureWithContext, for: message, context: context)
|
||||
let isValidWithContext = publicKey.isValidSignature(signatureWithContext, for: message, context: context)
|
||||
XCTAssertTrue(isValidWithContext)
|
||||
|
||||
// Check that invalid signatures (mismatching contexts) fail
|
||||
XCTAssertFalse(publicKey.isValidSignature(signature: signature, for: message, context: context))
|
||||
XCTAssertFalse(publicKey.isValidSignature(signature: signatureWithContext, for: message))
|
||||
XCTAssertFalse(publicKey.isValidSignature(signature, for: message, context: context))
|
||||
XCTAssertFalse(publicKey.isValidSignature(signatureWithContext, for: message))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -33,7 +33,7 @@ final class MLDSATests: XCTestCase {
|
||||
let test = "Hello, world!".data(using: .utf8)!
|
||||
try XCTAssertTrue(
|
||||
key.publicKey.isValidSignature(
|
||||
signature: key.signature(for: test),
|
||||
key.signature(for: test),
|
||||
for: test
|
||||
)
|
||||
)
|
||||
@@ -41,7 +41,7 @@ final class MLDSATests: XCTestCase {
|
||||
let context = "ctx".data(using: .utf8)!
|
||||
try XCTAssertTrue(
|
||||
key.publicKey.isValidSignature(
|
||||
signature: key.signature(for: test, context: context),
|
||||
key.signature(for: test, context: context),
|
||||
for: test,
|
||||
context: context
|
||||
)
|
||||
@@ -62,7 +62,7 @@ final class MLDSATests: XCTestCase {
|
||||
let test = "Hello, world!".data(using: .utf8)!
|
||||
try XCTAssertTrue(
|
||||
key.publicKey.isValidSignature(
|
||||
signature: key.signature(for: test),
|
||||
key.signature(for: test),
|
||||
for: test
|
||||
)
|
||||
)
|
||||
@@ -70,7 +70,7 @@ final class MLDSATests: XCTestCase {
|
||||
let context = "ctx".data(using: .utf8)!
|
||||
try XCTAssertTrue(
|
||||
key.publicKey.isValidSignature(
|
||||
signature: key.signature(for: test, context: context),
|
||||
key.signature(for: test, context: context),
|
||||
for: test,
|
||||
context: context
|
||||
)
|
||||
@@ -115,8 +115,8 @@ final class MLDSATests: XCTestCase {
|
||||
XCTAssertNotEqual(signature1, signature2)
|
||||
|
||||
// Even though the signatures are different, they both verify.
|
||||
XCTAssertTrue(publicKey.isValidSignature(signature: signature1, for: message))
|
||||
XCTAssertTrue(publicKey.isValidSignature(signature: signature2, for: message))
|
||||
XCTAssertTrue(publicKey.isValidSignature(signature1, for: message))
|
||||
XCTAssertTrue(publicKey.isValidSignature(signature2, for: message))
|
||||
}
|
||||
|
||||
func testMLDSA87SignatureIsRandomized() throws {
|
||||
@@ -135,8 +135,8 @@ final class MLDSATests: XCTestCase {
|
||||
XCTAssertNotEqual(signature1, signature2)
|
||||
|
||||
// Even though the signatures are different, they both verify.
|
||||
XCTAssertTrue(publicKey.isValidSignature(signature: signature1, for: message))
|
||||
XCTAssertTrue(publicKey.isValidSignature(signature: signature2, for: message))
|
||||
XCTAssertTrue(publicKey.isValidSignature(signature1, for: message))
|
||||
XCTAssertTrue(publicKey.isValidSignature(signature2, for: message))
|
||||
}
|
||||
|
||||
func testInvalidMLDSA65PublicKeyEncodingLength() throws {
|
||||
@@ -264,15 +264,15 @@ final class MLDSATests: XCTestCase {
|
||||
switch test.result {
|
||||
case .valid:
|
||||
if let context {
|
||||
XCTAssertTrue(publicKey.isValidSignature(signature: signature, for: message, context: context))
|
||||
XCTAssertTrue(publicKey.isValidSignature(signature, for: message, context: context))
|
||||
} else {
|
||||
XCTAssertTrue(publicKey.isValidSignature(signature: signature, for: message))
|
||||
XCTAssertTrue(publicKey.isValidSignature(signature, for: message))
|
||||
}
|
||||
case .invalid:
|
||||
if let context {
|
||||
XCTAssertFalse(publicKey.isValidSignature(signature: signature, for: message, context: context))
|
||||
XCTAssertFalse(publicKey.isValidSignature(signature, for: message, context: context))
|
||||
} else {
|
||||
XCTAssertFalse(publicKey.isValidSignature(signature: signature, for: message))
|
||||
XCTAssertFalse(publicKey.isValidSignature(signature, for: message))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -299,15 +299,15 @@ final class MLDSATests: XCTestCase {
|
||||
switch test.result {
|
||||
case .valid:
|
||||
if let context {
|
||||
XCTAssertTrue(publicKey.isValidSignature(signature: signature, for: message, context: context))
|
||||
XCTAssertTrue(publicKey.isValidSignature(signature, for: message, context: context))
|
||||
} else {
|
||||
XCTAssertTrue(publicKey.isValidSignature(signature: signature, for: message))
|
||||
XCTAssertTrue(publicKey.isValidSignature(signature, for: message))
|
||||
}
|
||||
case .invalid:
|
||||
if let context {
|
||||
XCTAssertFalse(publicKey.isValidSignature(signature: signature, for: message, context: context))
|
||||
XCTAssertFalse(publicKey.isValidSignature(signature, for: message, context: context))
|
||||
} else {
|
||||
XCTAssertFalse(publicKey.isValidSignature(signature: signature, for: message))
|
||||
XCTAssertFalse(publicKey.isValidSignature(signature, for: message))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user