Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add API providing basic RSA pubkey encrypt and privkey decrypt #125

Merged
merged 14 commits into from
Jul 17, 2023
Merged

Add API providing basic RSA pubkey encrypt and privkey decrypt #125

merged 14 commits into from
Jul 17, 2023

Conversation

gwynne
Copy link
Contributor

@gwynne gwynne commented Jul 21, 2022

Add API to _CryptoExtras to provide basic RSA pubkey encrypt and privkey decrypt operations.

Closes #124.

Checklist

  • I've run tests to see all new and existing tests pass
  • I've followed the code style of the rest of the project
  • I've read the Contribution Guidelines
  • I've updated the documentation if necessary

Motivation:

See #124 for details.

Modifications:

Added two new APIs to the RSA support under a new RSA.Encryption namespace, with associated data types necessary for representing the appropriate inputs and outputs. Added new test vectors and associated test cases exercising the new API surface.

Note: The extent of the exposed API has been deliberately limited to the bare minimum, as it is not desirable to expose more of RSA's core primitives than absolutely necessary.

@swift-server-bot
Copy link

Can one of the admins verify this patch?

6 similar comments
@swift-server-bot
Copy link

Can one of the admins verify this patch?

@swift-server-bot
Copy link

Can one of the admins verify this patch?

@swift-server-bot
Copy link

Can one of the admins verify this patch?

@swift-server-bot
Copy link

Can one of the admins verify this patch?

@swift-server-bot
Copy link

Can one of the admins verify this patch?

@swift-server-bot
Copy link

Can one of the admins verify this patch?

@Lukasa
Copy link
Collaborator

Lukasa commented Jul 22, 2022

@swift-server-bot add to allowlist

@Lukasa
Copy link
Collaborator

Lukasa commented Jul 22, 2022

Thanks for this PR @gwynne. For now I'm going to circulate this with some colleagues and get back to you.

@Higher08
Copy link

Higher08 commented Sep 28, 2022

There is no option to set the algorithm (only SHA1 is available). It would be nice to add this parameter.
Both security and BoringSSL support that.
For setting alg in BoringSSL we can use

EVP_PKEY_CTX_set_rsa_oaep_md
EVP_PKEY_CTX_set_rsa_mgf1_md

@t089
Copy link

t089 commented Oct 5, 2022

Is this something that could be merged any time soon? I've just stumbled upon this while trying to sign a JWT for a GitHub app that I could use RSA support from Crypto. ... Ah but this PR does not include "signing" correct?

@Lukasa
Copy link
Collaborator

Lukasa commented Oct 6, 2022

RSA signing is already merged.

@t089
Copy link

t089 commented Oct 6, 2022

RSA signing is already merged.

🙈 Thanks for the pointer! Looked for it in the wrong place.

@0xTim
Copy link
Contributor

0xTim commented Oct 6, 2022

Is there any update on this getting merged?

@Lukasa
Copy link
Collaborator

Lukasa commented Oct 6, 2022

Currently, no. It remains a source of real tension including a PKCS1v1.5 decrypt operation, and certainly as implemented here we wouldn't want to merge it, because it's not possible to use it safely (no constant-time padding check and no appropriate mitigation path). Marking the PKCS1v1.5 padding insecure is helping, but less than ideal, especially as the API does not provide a mechanism by which users could hold it safely. If we can find a way to not needing it at all, that would be supremely helpful to getting this merged.

@bjhomer
Copy link
Contributor

bjhomer commented Jun 30, 2023

@Lukasa If the PKCS1v1.5 padding were removed and we were left with only PKCS1_OAEP padding, would that open a path forward for merging this PR? Or are there safety concerns when using OAEP as well?

@Lukasa
Copy link
Collaborator

Lukasa commented Jun 30, 2023

OAEP does not have a safety concern to the same degree. However, I'm open to renegotiating the question of marking the PKCS 1v1.5 padding insecure in this context if @gwynne is willing to update the PR.

@gwynne
Copy link
Contributor Author

gwynne commented Jun 30, 2023

OAEP does not have a safety concern to the same degree. However, I'm open to renegotiating the question of marking the PKCS 1v1.5 padding insecure in this context if @gwynne is willing to update the PR.

I'm willing to just remove support for the PKCS1 v1.5 padding altogether. It's not needed for my purposes in any event. I'll rebase the PR against the current tip of trunk and take the insecure padding out.

@gwynne
Copy link
Contributor Author

gwynne commented Jun 30, 2023

@Lukasa Fully rebased, PKCS1 v1.5 padding support removed. Also fixed two deprecation warnings in as minimal a fashion as I could manage. All tests passing on Linux in my local Docker environment. (There's 6 failing tests related to unsupported non-power-of-2 key sizes on macOS, but it seems unrelated to my changes.)

@Lukasa Lukasa added the 🔼 needs-minor-version-bump For PRs that when merged cause a bump of the minor version, ie. 1.x.0 -> 1.(x+1).0 label Jul 4, 2023
Sources/_CryptoExtras/RSA/RSA.swift Outdated Show resolved Hide resolved
Sources/_CryptoExtras/RSA/RSA.swift Outdated Show resolved Hide resolved
Sources/_CryptoExtras/RSA/RSA_boring.swift Outdated Show resolved Hide resolved
Sources/_CryptoExtras/RSA/RSA.swift Outdated Show resolved Hide resolved
Sources/_CryptoExtras/RSA/RSA_boring.swift Outdated Show resolved Hide resolved
@gwynne gwynne requested a review from Lukasa July 4, 2023 17:50
@gwynne
Copy link
Contributor Author

gwynne commented Jul 4, 2023

@Lukasa I think I addressed everything you mentioned. I did have to add a @_documentation(visibility: public) attribute to the _RSA type to make the docs visible.

Copy link
Collaborator

@Lukasa Lukasa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, this is very close to good.

Sources/_CryptoExtras/RSA/RSA.swift Outdated Show resolved Hide resolved
Copy link
Collaborator

@Lukasa Lukasa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, LGTM.

@Lukasa Lukasa merged commit 5ac5632 into apple:main Jul 17, 2023
8 checks passed
@gwynne gwynne deleted the gwynne/rsa-encrypt-decrypt branch July 17, 2023 17:16
Lukasa pushed a commit to Lukasa/swift-crypto that referenced this pull request Aug 3, 2023
…#125)

* Add basic RSA encrypt/decrypt API.

* Add test vectors and tests for RSA encryption API

* Fix build failures

* Fix soundness issue

* Remove support for RSA encryption using the unsafe PKCS1 v1.5 padding

* Fix deprecation warning about the "pointer" version of Data.withUnsafeMutableBytes() by nudging the compiler to infer the "buffer" version.

* Fix deprecation warning about String.init(bytesNoCopy:length:freeWhenDone:) by using String.init(unsafeInitializedCapacity:initializingWith:) instead.

* Make _RSA.Encryption.[Public|Private]Key their own types

* Remove _RSA.Encryption.RSA[Encrypted|Decrypted]Data and just use Data directly. Don't use intermediate Array in BoringSSL implementations.

* Document the message size limits on RSA encrypt/decrypt operations

* Mark `_RSA` visible to documentation so the new docs can actually be seen (underscored name is assumed private by default)

* Fix generic type parameter shadowing warning (new warning in Swift 5.9)

* Fix pre-5.8 build

* Un-correct switch case indentation to make it wrong

(cherry picked from commit 5ac5632)
Lukasa added a commit that referenced this pull request Aug 3, 2023
…#191)

* Add basic RSA encrypt/decrypt API.

* Add test vectors and tests for RSA encryption API

* Fix build failures

* Fix soundness issue

* Remove support for RSA encryption using the unsafe PKCS1 v1.5 padding

* Fix deprecation warning about the "pointer" version of Data.withUnsafeMutableBytes() by nudging the compiler to infer the "buffer" version.

* Fix deprecation warning about String.init(bytesNoCopy:length:freeWhenDone:) by using String.init(unsafeInitializedCapacity:initializingWith:) instead.

* Make _RSA.Encryption.[Public|Private]Key their own types

* Remove _RSA.Encryption.RSA[Encrypted|Decrypted]Data and just use Data directly. Don't use intermediate Array in BoringSSL implementations.

* Document the message size limits on RSA encrypt/decrypt operations

* Mark `_RSA` visible to documentation so the new docs can actually be seen (underscored name is assumed private by default)

* Fix generic type parameter shadowing warning (new warning in Swift 5.9)

* Fix pre-5.8 build

* Un-correct switch case indentation to make it wrong

(cherry picked from commit 5ac5632)

Co-authored-by: Gwynne Raskind <gwynne@darkrainfall.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🔼 needs-minor-version-bump For PRs that when merged cause a bump of the minor version, ie. 1.x.0 -> 1.(x+1).0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support for basic RSA encrypt/decrypt operation
7 participants