From 953b34a60168bb44ffc5e0c3fc345189cdbca128 Mon Sep 17 00:00:00 2001 From: David Nadoba Date: Thu, 17 Aug 2023 14:53:44 +0100 Subject: [PATCH] Use `SecKeyGetBlockSize` instead of `kSecAttrKeySizeInBits` (#198) --- Sources/_CryptoExtras/RSA/RSA.swift | 8 ++++---- Sources/_CryptoExtras/RSA/RSA_security.swift | 6 ++---- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/Sources/_CryptoExtras/RSA/RSA.swift b/Sources/_CryptoExtras/RSA/RSA.swift index 073be204..cb244574 100644 --- a/Sources/_CryptoExtras/RSA/RSA.swift +++ b/Sources/_CryptoExtras/RSA/RSA.swift @@ -54,7 +54,7 @@ extension _RSA.Signing { public init(pemRepresentation: String) throws { self.backing = try BackingPublicKey(pemRepresentation: pemRepresentation) - if self.keySizeInBits < 1024 || self.keySizeInBits % 8 != 0 { + guard self.keySizeInBits >= 1024 else { throw CryptoKitError.incorrectParameterSize } } @@ -66,7 +66,7 @@ extension _RSA.Signing { public init(derRepresentation: Bytes) throws { self.backing = try BackingPublicKey(derRepresentation: derRepresentation) - if self.keySizeInBits < 1024 || self.keySizeInBits % 8 != 0 { + guard self.keySizeInBits >= 1024 else { throw CryptoKitError.incorrectParameterSize } } @@ -108,7 +108,7 @@ extension _RSA.Signing { public init(pemRepresentation: String) throws { self.backing = try BackingPrivateKey(pemRepresentation: pemRepresentation) - if self.keySizeInBits < 1024 || self.keySizeInBits % 8 != 0 { + guard self.keySizeInBits >= 1024 else { throw CryptoKitError.incorrectParameterSize } } @@ -120,7 +120,7 @@ extension _RSA.Signing { public init(derRepresentation: Bytes) throws { self.backing = try BackingPrivateKey(derRepresentation: derRepresentation) - if self.keySizeInBits < 1024 || self.keySizeInBits % 8 != 0 { + guard self.keySizeInBits >= 1024 else { throw CryptoKitError.incorrectParameterSize } } diff --git a/Sources/_CryptoExtras/RSA/RSA_security.swift b/Sources/_CryptoExtras/RSA/RSA_security.swift index 30924ebe..42b729b1 100644 --- a/Sources/_CryptoExtras/RSA/RSA_security.swift +++ b/Sources/_CryptoExtras/RSA/RSA_security.swift @@ -61,8 +61,7 @@ internal struct SecurityRSAPublicKey { } var keySizeInBits: Int { - let attributes = SecKeyCopyAttributes(self.backing)! as NSDictionary - return (attributes[kSecAttrKeySizeInBits]! as! NSNumber).intValue + SecKeyGetBlockSize(self.backing) * 8 } fileprivate init(_ backing: SecKey) { @@ -146,8 +145,7 @@ internal struct SecurityRSAPrivateKey { } var keySizeInBits: Int { - let attributes = SecKeyCopyAttributes(self.backing)! as NSDictionary - return (attributes[kSecAttrKeySizeInBits]! as! NSNumber).intValue + SecKeyGetBlockSize(self.backing) * 8 } var publicKey: SecurityRSAPublicKey {