mirror of
https://github.com/apple/swift-crypto
synced 2026-11-24 04:37:48 +00:00
### Motivation: The BoringSSL RSA backing computes `keySizeInBits` using `RSA_size(key) * 8`, which is incorrect. `RSA_size` returns the number of bytes required to represent the modulus, so is rounding up to the nearest byte. Because of this, the minimum key strength checks are slightly off. For example, 1017-bit keys are accepted by initializers that document a 1024 minimum, and similar for the initializers guarding for a 2048-bit minimum. ### Modifications: - Compute `keySizeInBits` using `RSA_bits` instead of `RSA_size`, which returns the modulus bit length. - Add a `modulusByteCount` property, which does still use `RSA_size`, and use it for the block-size checks. - Document the byte-alignment requirement of `_RSA.Encryption.init(pemRepresentation:)` and friends. - Update and add more tests. ### Result: - RSA `keySizeInBits` correctly reports the modulus bit length. - The documented minimum key length requirements are now correctly enforced, closing a 7-bit gap.