diff --git a/Sources/CryptoExtras/RSA/RSA+BlindSigning.swift b/Sources/CryptoExtras/RSA/RSA+BlindSigning.swift index 3f14da32..04401805 100644 --- a/Sources/CryptoExtras/RSA/RSA+BlindSigning.swift +++ b/Sources/CryptoExtras/RSA/RSA+BlindSigning.swift @@ -105,9 +105,15 @@ extension _RSA.BlindSigning { } /// Construct a RSA public key with the specified parameters. + /// + /// This constructor supports key sizes of 2048 bits or more. Users should validate that key sizes are appropriate + /// for their use-case. public init(n: some ContiguousBytes, e: some ContiguousBytes, parameters: Parameters) throws { self.backing = try BackingPublicKey(n: n, e: e) self.parameters = parameters + guard self.keySizeInBits >= 2048 else { + throw CryptoKitError.incorrectParameterSize + } } public var pkcs1DERRepresentation: Data { @@ -206,9 +212,15 @@ extension _RSA.BlindSigning { } /// Construct an RSA private key with the specified parameters. + /// + /// This constructor supports key sizes of 2048 bits or more. Users should validate that key sizes are appropriate + /// for their use-case. public init(n: some ContiguousBytes, e: some ContiguousBytes, d: some ContiguousBytes, p: some ContiguousBytes, q: some ContiguousBytes, parameters: Parameters) throws { self.backing = try BackingPrivateKey(n: n, e: e, d: d, p: p, q: q) self.parameters = parameters + guard self.keySizeInBits >= 2048 else { + throw CryptoKitError.incorrectParameterSize + } } /// Randomly generate a new RSA private key of a given size. diff --git a/Sources/CryptoExtras/RSA/RSA.swift b/Sources/CryptoExtras/RSA/RSA.swift index dc58bb41..16ca21d2 100644 --- a/Sources/CryptoExtras/RSA/RSA.swift +++ b/Sources/CryptoExtras/RSA/RSA.swift @@ -117,8 +117,14 @@ extension _RSA.Signing { } /// Construct an RSA public key with the specified parameters. + /// + /// This constructor supports key sizes of 2048 bits or more. Users should validate that key sizes are appropriate + /// for their use-case. public init(n: some ContiguousBytes, e: some ContiguousBytes) throws { self.backing = try BackingPublicKey(n: n, e: e) + guard self.keySizeInBits >= 2048 else { + throw CryptoKitError.incorrectParameterSize + } } public var pkcs1DERRepresentation: Data { @@ -248,8 +254,14 @@ extension _RSA.Signing { } /// Construct an RSA private key with the specified parameters. + /// + /// This constructor supports key sizes of 2048 bits or more. Users should validate that key sizes are appropriate + /// for their use-case. public init(n: some ContiguousBytes, e: some ContiguousBytes, d: some ContiguousBytes, p: some ContiguousBytes, q: some ContiguousBytes) throws { self.backing = try BackingPrivateKey(n: n, e: e, d: d, p: p, q: q) + guard self.keySizeInBits >= 2048 else { + throw CryptoKitError.incorrectParameterSize + } } /// Randomly generate a new RSA private key of a given size. @@ -574,8 +586,14 @@ extension _RSA.Encryption { } /// Construct an RSA public key with the specified parameters. + /// + /// This constructor supports key sizes of 2048 bits or more. Users should validate that key sizes are appropriate + /// for their use-case. public init(n: some ContiguousBytes, e: some ContiguousBytes) throws { self.backing = try BackingPublicKey(n: n, e: e) + guard self.keySizeInBits >= 2048 else { + throw CryptoKitError.incorrectParameterSize + } } public var pkcs1DERRepresentation: Data { self.backing.pkcs1DERRepresentation } @@ -641,8 +659,14 @@ extension _RSA.Encryption { /// Construct an RSA private key with the specified parameters. + /// + /// This constructor supports key sizes of 2048 bits or more. Users should validate that key sizes are appropriate + /// for their use-case. public init(n: some ContiguousBytes, e: some ContiguousBytes, d: some ContiguousBytes, p: some ContiguousBytes, q: some ContiguousBytes) throws { self.backing = try BackingPrivateKey(n: n, e: e, d: d, p: p, q: q) + guard self.keySizeInBits >= 2048 else { + throw CryptoKitError.incorrectParameterSize + } } /// Randomly generate a new RSA private key of a given size. diff --git a/Tests/CryptoExtrasTests/TestRSABlindSigning.swift b/Tests/CryptoExtrasTests/TestRSABlindSigning.swift index ceaca2d7..9b50dd1d 100644 --- a/Tests/CryptoExtrasTests/TestRSABlindSigning.swift +++ b/Tests/CryptoExtrasTests/TestRSABlindSigning.swift @@ -201,6 +201,17 @@ final class TestRSABlindSigning: XCTestCase { e: bytesValues.randomElement()!, parameters: .RSABSSA_SHA384_PSS_Randomized ) + // Modulus of a 512-bit key: `openssl rsa -in key.pem -noout -modulus`. + let _512BitModulus = + "e24c4c2c70e673315c2420b0e211542bbccf5b79f2ce6bdaa4aac29650bd064b" + + "955ae1613a449c7143e906d7f251e49356771d9d6f8f15bcf4044f87de1c1bed" + XCTAssertThrowsError( + try _RSA.BlindSigning.PublicKey( + n: Data(hexString: _512BitModulus), + e: Data(hexString: "010001"), + parameters: .RSABSSA_SHA384_PSS_Randomized + ) + ) } func testConstructAndUseKeyFromRSANumbersWhileRecoveringPrimes() throws { diff --git a/Tests/CryptoExtrasTests/TestRSAEncryption.swift b/Tests/CryptoExtrasTests/TestRSAEncryption.swift index 3f76f755..4e3c7dac 100644 --- a/Tests/CryptoExtrasTests/TestRSAEncryption.swift +++ b/Tests/CryptoExtrasTests/TestRSAEncryption.swift @@ -111,6 +111,13 @@ final class TestRSAEncryption: XCTestCase { n: bytesValues.randomElement()!, e: bytesValues.randomElement()! ) + // Modulus of a 512-bit key: `openssl rsa -in key.pem -noout -modulus`. + let _512BitModulus = + "e24c4c2c70e673315c2420b0e211542bbccf5b79f2ce6bdaa4aac29650bd064b" + + "955ae1613a449c7143e906d7f251e49356771d9d6f8f15bcf4044f87de1c1bed" + XCTAssertThrowsError( + try _RSA.Encryption.PublicKey(n: Data(hexString: _512BitModulus), e: Data(hexString: "010001")) + ) } func testConstructAndUseKeyFromRSANumbersWhileRecoveringPrimes() throws { diff --git a/Tests/CryptoExtrasTests/TestRSASigning.swift b/Tests/CryptoExtrasTests/TestRSASigning.swift index 66c27407..c2d35247 100644 --- a/Tests/CryptoExtrasTests/TestRSASigning.swift +++ b/Tests/CryptoExtrasTests/TestRSASigning.swift @@ -1036,6 +1036,13 @@ final class TestRSASigning: XCTestCase { n: bytesValues.randomElement()!, e: bytesValues.randomElement()! ) + // Modulus of the 512-bit key above: `openssl rsa -in key.pem -noout -modulus`. + let _512BitModulus = + "e24c4c2c70e673315c2420b0e211542bbccf5b79f2ce6bdaa4aac29650bd064b" + + "955ae1613a449c7143e906d7f251e49356771d9d6f8f15bcf4044f87de1c1bed" + XCTAssertThrowsError( + try _RSA.Signing.PublicKey(n: Data(hexString: _512BitModulus), e: Data(hexString: "010001")) + ) } func testConstructAndUseKeyFromRSANumbersWhileRecoveringPrimes() throws {