mirror of
https://github.com/apple/swift-crypto
synced 2026-11-22 04:30:39 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
deb49b961e | ||
|
|
0f6f68df04 |
No files matched your search
@@ -834,21 +834,37 @@ extension _RSA {
|
||||
d: ArbitraryPrecisionInteger
|
||||
) throws -> (p: ArbitraryPrecisionInteger, q: ArbitraryPrecisionInteger) {
|
||||
// This is based on the proof of fact 1 in https://www.ams.org/notices/199902/boneh.pdf
|
||||
let k = (d * e) - 1
|
||||
let t = k.trailingZeroBitCount
|
||||
let r = k >> t
|
||||
|
||||
guard k.isEven else {
|
||||
// Valid keys have e < n, and BoringSSL rejects d >= n and moduli over its maximum. Larger
|
||||
// values let the caller pick the cost of the exponentiations and squaring loop below.
|
||||
guard n.byteCount <= _RSA.maximumModulusBitCount / 8, e < n, d < n else {
|
||||
throw CryptoKitError.incorrectParameterSize
|
||||
}
|
||||
|
||||
let k = (d * e) - 1
|
||||
|
||||
// k == 0 (that is, e * d == 1) gives r == 0 below, for which every base is trivial.
|
||||
guard k > 0, k.isEven else {
|
||||
throw CryptoKitError.incorrectParameterSize
|
||||
}
|
||||
|
||||
let t = k.trailingZeroBitCount
|
||||
let r = k >> t
|
||||
|
||||
var y: ArbitraryPrecisionInteger = 0
|
||||
var i = 1
|
||||
|
||||
let context = try FiniteFieldArithmeticContext(fieldSize: n)
|
||||
|
||||
while i <= 100 {
|
||||
// Every attempt must count towards the limit, including those where g^r is trivially
|
||||
// 1 or n - 1. Inputs for which that holds for every base would otherwise never return.
|
||||
for _ in 0..<100 {
|
||||
let g = try ArbitraryPrecisionInteger.random(inclusiveMin: 2, exclusiveMax: n)
|
||||
|
||||
// The check at the end of this attempt only holds for bases coprime to n.
|
||||
guard try ArbitraryPrecisionInteger.gcd(g, n) == 1 else {
|
||||
continue
|
||||
}
|
||||
|
||||
y = try context.pow(g, r)
|
||||
|
||||
guard y != 1, y != n - 1 else {
|
||||
@@ -877,7 +893,11 @@ extension _RSA {
|
||||
return (p, q)
|
||||
}
|
||||
|
||||
i &+= 1
|
||||
// Here x is g^k unless the loop stopped at n - 1. For a valid key g^k == 1 for every
|
||||
// base coprime to n, so any other value means the numbers do not form a key.
|
||||
guard x == n - 1 else {
|
||||
throw CryptoKitError.incorrectParameterSize
|
||||
}
|
||||
}
|
||||
|
||||
throw CryptoKitError.incorrectParameterSize
|
||||
|
||||
@@ -1131,3 +1131,8 @@ enum BlindSigningHelpers {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension _RSA {
|
||||
static let maximumModulusBitCount = Int(OPENSSL_RSA_MAX_MODULUS_BITS)
|
||||
}
|
||||
@@ -1062,6 +1062,70 @@ final class TestRSASigning: XCTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
func testConstructKeyFromRSANumbersWithDegenerateExponentsThrows() throws {
|
||||
// e * d == 1 makes g^(e * d - 1) == 1 for every base, so prime recovery can never
|
||||
// make progress. This must throw rather than retry forever.
|
||||
var n = [UInt8](repeating: 0xA7, count: 256)
|
||||
n[255] = 0x01
|
||||
|
||||
XCTAssertThrowsError(
|
||||
try _RSA.Signing.PrivateKey._createFromNumbers(n: n, e: [1] as [UInt8], d: [1] as [UInt8])
|
||||
)
|
||||
XCTAssertThrowsError(
|
||||
try _RSA.Encryption.PrivateKey._createFromNumbers(n: n, e: [1] as [UInt8], d: [1] as [UInt8])
|
||||
)
|
||||
XCTAssertThrowsError(
|
||||
try _RSA.BlindSigning.PrivateKey<SHA384>._createFromNumbers(
|
||||
n: n, e: [1] as [UInt8], d: [1] as [UInt8], parameters: .RSABSSA_SHA384_PSS_Randomized
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
func testExtractPrimeFactorsGivesUpWhenEveryBaseIsTrivial() throws {
|
||||
// n = 7 is prime and e * d - 1 == 24 == 2^3 * 3, so g^3 is 1 or n - 1 for every base g
|
||||
// (the only cubic residues mod 7). The attempt limit must still apply.
|
||||
XCTAssertThrowsError(try _RSA.extractPrimeFactors(n: 7, e: 5, d: 5))
|
||||
|
||||
// e * d == 1 gives an exponent of 0, so g^0 == 1 for every base.
|
||||
XCTAssertThrowsError(try _RSA.extractPrimeFactors(n: 3233, e: 1, d: 1))
|
||||
}
|
||||
|
||||
func testExtractPrimeFactorsRejectsOversizedNumbers() throws {
|
||||
// d == 2753 is valid for n == 3233, e == 17. Adding lambda(n) == 780 keeps the numbers
|
||||
// consistent, so only the range check rejects d >= n.
|
||||
XCTAssertNoThrow(try _RSA.extractPrimeFactors(n: 3233, e: 17, d: 2753))
|
||||
XCTAssertThrowsError(try _RSA.extractPrimeFactors(n: 3233, e: 17, d: 2753 + 780))
|
||||
XCTAssertThrowsError(try _RSA.extractPrimeFactors(n: 3233, e: 17 + 780 * 5, d: 2753))
|
||||
|
||||
// e * d - 1 == 2^9992, so without the range check the squaring loop runs 9991 times
|
||||
// per attempt.
|
||||
var n = [UInt8](repeating: 0xA7, count: 256)
|
||||
n[255] = 0x01
|
||||
var d = [UInt8](repeating: 0, count: 1250)
|
||||
d[0] = 0x01
|
||||
d[1249] = 0x01
|
||||
XCTAssertThrowsError(try _RSA.Signing.PrivateKey._createFromNumbers(n: n, e: [1] as [UInt8], d: d))
|
||||
|
||||
// A modulus over 8192 bits with d < n would otherwise make each attempt arbitrarily slow.
|
||||
var bigN = [UInt8](repeating: 0xA7, count: 1025)
|
||||
bigN[1024] = 0x01
|
||||
var bigD = [UInt8](repeating: 0, count: 1025)
|
||||
bigD[1] = 0x01
|
||||
bigD[1024] = 0x01
|
||||
XCTAssertThrowsError(try _RSA.Signing.PrivateKey._createFromNumbers(n: bigN, e: [1] as [UInt8], d: bigD))
|
||||
}
|
||||
|
||||
func testExtractPrimeFactorsRejectsInconsistentNumbers() throws {
|
||||
for testVector in RFC9474TestVector.allValues {
|
||||
let n = try ArbitraryPrecisionInteger(bytes: Data(hexString: testVector.n))
|
||||
let e = try ArbitraryPrecisionInteger(bytes: Data(hexString: testVector.e))
|
||||
let d = try ArbitraryPrecisionInteger(bytes: Data(hexString: testVector.d))
|
||||
|
||||
// Using d = d + 2 means d is still even but no longer a multiple of lambda(n).
|
||||
XCTAssertThrowsError(try _RSA.extractPrimeFactors(n: n, e: e, d: d + 2))
|
||||
}
|
||||
}
|
||||
|
||||
func testGetKeyPrimitives() throws {
|
||||
for testVector in RFC9474TestVector.allValues {
|
||||
let n = try Data(hexString: testVector.n)
|
||||
|
||||
Reference in new issue
Block a user