Skip to content

Commit

Permalink
chore: Minor performance improvements
Browse files Browse the repository at this point in the history
fix: Swift 6.0 build issue
  • Loading branch information
amosavian committed Aug 2, 2024
1 parent a6b72b2 commit 3ab31a3
Show file tree
Hide file tree
Showing 26 changed files with 177 additions and 100 deletions.
1 change: 1 addition & 0 deletions Sources/JWSETKit/Base/EncryptedData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Foundation
import Crypto

/// A container for AES ciphers, e.g. AES-GCM, AES-CBC-HMAC, etc.
@frozen
public struct SealedData: DataProtocol, BidirectionalCollection, Hashable, Sendable {
public typealias Nonce = Data

Expand Down
2 changes: 2 additions & 0 deletions Sources/JWSETKit/Base/ProtectedContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ extension TypedProtectedWebContainer {
}
}

@frozen
public struct ProtectedDataWebContainer: ProtectedWebContainer, Codable {
public var encoded: Data

Expand All @@ -119,6 +120,7 @@ public struct ProtectedDataWebContainer: ProtectedWebContainer, Codable {
///
/// This cotainer preserves original data to keep consistancy of signature as re-encoding payload
/// may change sorting.
@frozen
public struct ProtectedJSONWebContainer<Container: JSONWebContainer>: TypedProtectedWebContainer, Codable {
private var _protected: Data
private var _value: Container
Expand Down
6 changes: 3 additions & 3 deletions Sources/JWSETKit/Cryptography/Algorithms/Signature.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,14 @@ extension JSONWebSignatureAlgorithm {
/// - curve: Curve if key is elliptic curve.
/// - publicKeyClass: Public key class.
/// - privateKeyClass: Private key class. In case the key is symmetric, it equals to `publicKeyClass`.
public static func register<Public, Private>(
public static func register<Public, Private, Hash>(
_ algorithm: Self,
type: JSONWebKeyType,
curve: JSONWebKeyCurve? = nil,
hashFunction: any HashFunction.Type,
hashFunction: Hash.Type,
publicKeyClass: Public.Type,
privateKeyClass: Private.Type
) where Public: JSONWebValidatingKey, Private: JSONWebSigningKey {
) where Public: JSONWebValidatingKey, Private: JSONWebSigningKey, Hash: HashFunction {
keyRegistryClasses[algorithm] = (publicKeyClass, privateKeyClass)
keyTypes[algorithm] = type
curves[algorithm] = curve
Expand Down
18 changes: 11 additions & 7 deletions Sources/JWSETKit/Cryptography/Certificate/SecCertificate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import CommonCrypto
import CryptoKit
import X509

extension Security.SecCertificate: JSONWebValidatingKey {
extension Security.SecCertificate: Swift.Codable {}

extension SecCertificate: JSONWebValidatingKey {
public var storage: JSONWebValueStorage {
var key = AnyJSONWebKey(storage: (try? publicKey.storage) ?? .init())
if let certificate = try? Certificate(self) {
Expand Down Expand Up @@ -46,13 +48,15 @@ extension Security.SecCertificate: JSONWebValidatingKey {
}
}

extension Security.SecCertificate: Expirable {
extension SecCertificate: Expirable {
public func verifyDate(_ currentDate: Date) throws {
try Certificate(self).verifyDate(currentDate)
}
}

extension Security.SecTrust: JSONWebValidatingKey {
extension Security.SecTrust: Swift.Codable {}

extension SecTrust: JSONWebValidatingKey {
public var storage: JSONWebValueStorage {
var key = AnyJSONWebKey(storage: (try? certificateChain.first?.publicKey.storage) ?? .init())
key.certificateChain = (try? certificateChain.compactMap(Certificate.init)) ?? []
Expand Down Expand Up @@ -97,13 +101,13 @@ extension Security.SecTrust: JSONWebValidatingKey {
}
}

extension Security.SecTrust: Expirable {
extension SecTrust: Expirable {
public func verifyDate(_ currentDate: Date) throws {
try certificateChain.forEach { try $0.verifyDate(currentDate) }
}
}

extension X509.Certificate {
extension Certificate {
/// Casts `X509.Certificate` into `SecCertificate`.
///
/// - Returns: A new `SecCertificate` instance.
Expand All @@ -120,11 +124,11 @@ extension X509.Certificate {
}
}

public func == (lhs: X509.Certificate, rhs: Security.SecCertificate) -> Bool {
public func == (lhs: Certificate, rhs: SecCertificate) -> Bool {
lhs == (try? Certificate(rhs))
}

public func == (lhs: Security.SecCertificate, rhs: X509.Certificate) -> Bool {
public func == (lhs: SecCertificate, rhs: Certificate) -> Bool {
(try? Certificate(lhs)) == rhs
}
#endif
14 changes: 9 additions & 5 deletions Sources/JWSETKit/Cryptography/Certificate/X509Certificate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ import X509
import _CryptoExtras
#endif

extension X509.Certificate.PublicKey: JSONWebValidatingKey {
extension X509.Certificate.PublicKey: Swift.Codable {}

extension Certificate.PublicKey: JSONWebValidatingKey {
public var storage: JSONWebValueStorage {
(try? jsonWebKey().storage) ?? .init()
}

public static func create(storage: JSONWebValueStorage) throws -> X509.Certificate.PublicKey {
public static func create(storage: JSONWebValueStorage) throws -> Certificate.PublicKey {
let key = AnyJSONWebKey(storage: storage)

switch (key.keyType, key.curve) {
Expand Down Expand Up @@ -81,14 +83,16 @@ extension DERImplicitlyTaggable {
}
}

extension X509.Certificate: JSONWebValidatingKey {
extension X509.Certificate: Swift.Codable {}

extension Certificate: JSONWebValidatingKey {
public var storage: JSONWebValueStorage {
var key = AnyJSONWebKey(storage: publicKey.storage)
key.certificateChain = [self]
return key.storage
}

public static func create(storage: JSONWebValueStorage) throws -> X509.Certificate {
public static func create(storage: JSONWebValueStorage) throws -> Certificate {
let key = AnyJSONWebKey(storage: storage)
guard let certificate = key.certificateChain.first else {
throw JSONWebKeyError.keyNotFound
Expand All @@ -101,7 +105,7 @@ extension X509.Certificate: JSONWebValidatingKey {
}
}

extension X509.Certificate: Expirable {
extension Certificate: Expirable {
public func verifyDate(_ currentDate: Date) throws {
if currentDate > notValidAfter {
throw JSONWebValidationError.tokenExpired(expiry: notValidAfter)
Expand Down
30 changes: 19 additions & 11 deletions Sources/JWSETKit/Cryptography/EC/Ed25519.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import Foundation
import Crypto

extension Crypto.Curve25519.Signing.PublicKey: CryptoECPublicKey {
extension Crypto.Curve25519.Signing.PublicKey: @retroactive Hashable, Swift.Codable {}

extension Curve25519.Signing.PublicKey: CryptoECPublicKey {
static var curve: JSONWebKeyCurve { .ed25519 }

public var storage: JSONWebValueStorage {
Expand All @@ -28,7 +30,9 @@ extension Crypto.Curve25519.Signing.PublicKey: CryptoECPublicKey {
}
}

extension Crypto.Curve25519.KeyAgreement.PublicKey: CryptoECPublicKey {
extension Crypto.Curve25519.KeyAgreement.PublicKey: @retroactive Hashable, Swift.Codable {}

extension Curve25519.KeyAgreement.PublicKey: CryptoECPublicKey {
static var curve: JSONWebKeyCurve { .x25519 }

public var storage: JSONWebValueStorage {
Expand All @@ -48,20 +52,22 @@ extension Crypto.Curve25519.KeyAgreement.PublicKey: CryptoECPublicKey {
}
}

extension Crypto.Curve25519.Signing.PublicKey: JSONWebValidatingKey {
extension Curve25519.Signing.PublicKey: JSONWebValidatingKey {
public func verifySignature<S, D>(_ signature: S, for data: D, using _: JSONWebSignatureAlgorithm) throws where S: DataProtocol, D: DataProtocol {
if !isValidSignature(signature, for: data) {
throw CryptoKitError.authenticationFailure
}
}
}

extension Crypto.Curve25519.Signing.PublicKey: CryptoEdKeyPortable {}
extension Curve25519.Signing.PublicKey: CryptoEdKeyPortable {}

extension Crypto.Curve25519.KeyAgreement.PublicKey: CryptoEdKeyPortable {}
extension Curve25519.KeyAgreement.PublicKey: CryptoEdKeyPortable {}

extension Crypto.Curve25519.Signing.PrivateKey: JSONWebSigningKey, CryptoECPrivateKey {
public init(algorithm _: any JSONWebAlgorithm) throws {
extension Crypto.Curve25519.Signing.PrivateKey: @retroactive Hashable, Swift.Codable {}

extension Curve25519.Signing.PrivateKey: JSONWebSigningKey, CryptoECPrivateKey {
public init(algorithm _: some JSONWebAlgorithm) throws {
self.init()
}

Expand All @@ -70,15 +76,17 @@ extension Crypto.Curve25519.Signing.PrivateKey: JSONWebSigningKey, CryptoECPriva
}
}

extension Crypto.Curve25519.KeyAgreement.PrivateKey: CryptoECPrivateKey {
public init(algorithm _: any JSONWebAlgorithm) throws {
extension Crypto.Curve25519.KeyAgreement.PrivateKey: @retroactive Hashable, Swift.Codable {}

extension Curve25519.KeyAgreement.PrivateKey: CryptoECPrivateKey {
public init(algorithm _: some JSONWebAlgorithm) throws {
self.init()
}
}

extension Crypto.Curve25519.Signing.PrivateKey: CryptoEdKeyPortable {}
extension Curve25519.Signing.PrivateKey: CryptoEdKeyPortable {}

extension Crypto.Curve25519.KeyAgreement.PrivateKey: CryptoEdKeyPortable {}
extension Curve25519.KeyAgreement.PrivateKey: CryptoEdKeyPortable {}

protocol CryptoEdKeyPortable: JSONWebKeyImportable, JSONWebKeyExportable {
var rawRepresentation: Data { get }
Expand Down
2 changes: 1 addition & 1 deletion Sources/JWSETKit/Cryptography/EC/JWK-EC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public struct JSONWebECPrivateKey: MutableJSONWebKey, JSONWebSigningKey, Sendabl
self.storage = storage
}

public init(algorithm: any JSONWebAlgorithm) throws {
public init(algorithm: some JSONWebAlgorithm) throws {
try self.init(curve: algorithm.curve ?? .empty)
}

Expand Down
48 changes: 35 additions & 13 deletions Sources/JWSETKit/Cryptography/EC/P256.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,19 @@
import Foundation
import Crypto

extension Crypto.P256.Signing.PublicKey: CryptoECPublicKey {
extension Crypto.P256.Signing.PublicKey: Swift.Hashable, Swift.Codable {}

extension P256.Signing.PublicKey: CryptoECPublicKey {
static var curve: JSONWebKeyCurve { .p256 }
}

extension Crypto.P256.KeyAgreement.PublicKey: CryptoECPublicKey {
extension Crypto.P256.KeyAgreement.PublicKey: Swift.Hashable, Swift.Codable {}

extension P256.KeyAgreement.PublicKey: CryptoECPublicKey {
static var curve: JSONWebKeyCurve { .p256 }
}

extension Crypto.P256.Signing.PublicKey: JSONWebValidatingKey {
extension P256.Signing.PublicKey: JSONWebValidatingKey {
public func verifySignature<S, D>(_ signature: S, for data: D, using _: JSONWebSignatureAlgorithm) throws where S: DataProtocol, D: DataProtocol {
let ecdsaSignature: P256.Signing.ECDSASignature
// swiftformat:disable:next redundantSelf
Expand All @@ -31,12 +35,14 @@ extension Crypto.P256.Signing.PublicKey: JSONWebValidatingKey {
}
}

extension Crypto.P256.Signing.PublicKey: CryptoECKeyPortableCompactRepresentable {}
extension P256.Signing.PublicKey: CryptoECKeyPortableCompactRepresentable {}

extension Crypto.P256.KeyAgreement.PublicKey: CryptoECKeyPortableCompactRepresentable {}
extension P256.KeyAgreement.PublicKey: CryptoECKeyPortableCompactRepresentable {}

extension Crypto.P256.Signing.PrivateKey: JSONWebSigningKey, CryptoECPrivateKey {
public init(algorithm _: any JSONWebAlgorithm) throws {
extension Crypto.P256.Signing.PrivateKey: Swift.Hashable, Swift.Codable {}

extension P256.Signing.PrivateKey: JSONWebSigningKey, CryptoECPrivateKey {
public init(algorithm _: some JSONWebAlgorithm) throws {
self.init(compactRepresentable: false)
}

Expand All @@ -45,18 +51,22 @@ extension Crypto.P256.Signing.PrivateKey: JSONWebSigningKey, CryptoECPrivateKey
}
}

extension Crypto.P256.KeyAgreement.PrivateKey: CryptoECPrivateKey {
public init(algorithm _: any JSONWebAlgorithm) throws {
extension Crypto.P256.KeyAgreement.PrivateKey: Swift.Hashable, Swift.Codable {}

extension P256.KeyAgreement.PrivateKey: CryptoECPrivateKey {
public init(algorithm _: some JSONWebAlgorithm) throws {
self.init(compactRepresentable: false)
}
}

extension Crypto.P256.Signing.PrivateKey: CryptoECKeyPortable {}
extension P256.Signing.PrivateKey: CryptoECKeyPortable {}

extension Crypto.P256.KeyAgreement.PrivateKey: CryptoECKeyPortable {}
extension P256.KeyAgreement.PrivateKey: CryptoECKeyPortable {}

#if canImport(Darwin)
extension Crypto.SecureEnclave.P256.Signing.PrivateKey: CryptoECPrivateKey {
extension Crypto.SecureEnclave.P256.Signing.PrivateKey: Swift.Hashable, Swift.Codable {}

extension SecureEnclave.P256.Signing.PrivateKey: CryptoECPrivateKey {
public var storage: JSONWebValueStorage {
// Keys stored in SecureEnclave are not exportable.
//
Expand All @@ -69,7 +79,7 @@ extension Crypto.SecureEnclave.P256.Signing.PrivateKey: CryptoECPrivateKey {
fatalError("Private Keys in Secure Enclave are not encodable.")
}

public init(algorithm _: any JSONWebAlgorithm) throws {
public init(algorithm _: some JSONWebAlgorithm) throws {
try self.init(compactRepresentable: true)
}

Expand All @@ -81,4 +91,16 @@ extension Crypto.SecureEnclave.P256.Signing.PrivateKey: CryptoECPrivateKey {
try signature(for: SHA256.hash(data: data)).rawRepresentation
}
}

extension Crypto.SecureEnclave.P256.KeyAgreement.PrivateKey: Swift.Hashable, Swift.Codable {}

extension SecureEnclave.P256.KeyAgreement.PrivateKey: CryptoECPrivateKey {
var rawRepresentation: Data {
fatalError("Private Keys in Secure Enclave are not encodable.")
}

init(rawRepresentation: Data) throws {
throw JSONWebKeyError.operationNotAllowed
}
}
#endif
30 changes: 19 additions & 11 deletions Sources/JWSETKit/Cryptography/EC/P384.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,19 @@
import Foundation
import Crypto

extension Crypto.P384.Signing.PublicKey: CryptoECPublicKey {
extension Crypto.P384.Signing.PublicKey: Swift.Hashable, Swift.Codable {}

extension P384.Signing.PublicKey: CryptoECPublicKey {
static var curve: JSONWebKeyCurve { .p384 }
}

extension Crypto.P384.KeyAgreement.PublicKey: CryptoECPublicKey {
extension Crypto.P384.KeyAgreement.PublicKey: Swift.Hashable, Swift.Codable {}

extension P384.KeyAgreement.PublicKey: CryptoECPublicKey {
static var curve: JSONWebKeyCurve { .p384 }
}

extension Crypto.P384.Signing.PublicKey: JSONWebValidatingKey {
extension P384.Signing.PublicKey: JSONWebValidatingKey {
public func verifySignature<S, D>(_ signature: S, for data: D, using _: JSONWebSignatureAlgorithm) throws where S: DataProtocol, D: DataProtocol {
let ecdsaSignature: P384.Signing.ECDSASignature
// swiftformat:disable:next redundantSelf
Expand All @@ -31,12 +35,14 @@ extension Crypto.P384.Signing.PublicKey: JSONWebValidatingKey {
}
}

extension Crypto.P384.Signing.PublicKey: CryptoECKeyPortableCompactRepresentable {}
extension P384.Signing.PublicKey: CryptoECKeyPortableCompactRepresentable {}

extension Crypto.P384.KeyAgreement.PublicKey: CryptoECKeyPortableCompactRepresentable {}
extension P384.KeyAgreement.PublicKey: CryptoECKeyPortableCompactRepresentable {}

extension Crypto.P384.Signing.PrivateKey: JSONWebSigningKey, CryptoECPrivateKey {
public init(algorithm _: any JSONWebAlgorithm) throws {
extension Crypto.P384.Signing.PrivateKey: Swift.Hashable, Swift.Codable {}

extension P384.Signing.PrivateKey: JSONWebSigningKey, CryptoECPrivateKey {
public init(algorithm _: some JSONWebAlgorithm) throws {
self.init(compactRepresentable: false)
}

Expand All @@ -45,12 +51,14 @@ extension Crypto.P384.Signing.PrivateKey: JSONWebSigningKey, CryptoECPrivateKey
}
}

extension Crypto.P384.KeyAgreement.PrivateKey: CryptoECPrivateKey {
public init(algorithm _: any JSONWebAlgorithm) throws {
extension Crypto.P384.KeyAgreement.PrivateKey: Swift.Hashable, Swift.Codable {}

extension P384.KeyAgreement.PrivateKey: CryptoECPrivateKey {
public init(algorithm _: some JSONWebAlgorithm) throws {
self.init(compactRepresentable: false)
}
}

extension Crypto.P384.Signing.PrivateKey: CryptoECKeyPortable {}
extension P384.Signing.PrivateKey: CryptoECKeyPortable {}

extension Crypto.P384.KeyAgreement.PrivateKey: CryptoECKeyPortable {}
extension P384.KeyAgreement.PrivateKey: CryptoECKeyPortable {}
Loading

0 comments on commit 3ab31a3

Please sign in to comment.