Title: OpenPGP: decryption of session key with RSA fails with custom cipher provider if the first byte is 0 · Issue #1003 · bcgit/bc-java · GitHub
Open Graph Title: OpenPGP: decryption of session key with RSA fails with custom cipher provider if the first byte is 0 · Issue #1003 · bcgit/bc-java
X Title: OpenPGP: decryption of session key with RSA fails with custom cipher provider if the first byte is 0 · Issue #1003 · bcgit/bc-java
Description: Hello, we are using bcpg-jdk15on v1.69 to read OpenPGP messages encrypted using RSA keys. The private RSA key is stored in a Hardware Security Module, thus when we are performing the decryption of the session key we are not using org.bou...
Open Graph Description: Hello, we are using bcpg-jdk15on v1.69 to read OpenPGP messages encrypted using RSA keys. The private RSA key is stored in a Hardware Security Module, thus when we are performing the decryption of ...
X Description: Hello, we are using bcpg-jdk15on v1.69 to read OpenPGP messages encrypted using RSA keys. The private RSA key is stored in a Hardware Security Module, thus when we are performing the decryption of ...
Opengraph URL: https://github.com/bcgit/bc-java/issues/1003
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"OpenPGP: decryption of session key with RSA fails with custom cipher provider if the first byte is 0","articleBody":"Hello, we are using **bcpg-jdk15on v1.69** to read OpenPGP messages encrypted using RSA keys.\r\n\r\nThe private RSA key is stored in a Hardware Security Module, thus when we are performing the decryption of the session key we are not using **org.bouncycastle.jcajce.provider.asymmetric.rsa.CipherSpi**, but a class from a jar provided by our HSM provider.\r\n\r\nWe sometimes have an IllegalBlockSizeException in the Cipher engine when the library tries to decrypt the session key of the message from the Multiprecision Integer contained in Public-Key Encrypted Session Key Packet.\r\n\r\nWe found that this exception occurred everytime that when writing the PGP message, the ciphered session key had its first byte being a 0.\r\n\r\nAfter digging in the library, we found that in the class JcePublicKeyDataDecryptorFactoryBuilder, it takes whatever the multiprecision integer from the public key packet is, and passes the bytes from it directly to the Cipher engine (after removing the first 2 bytes corresponding to the MPI length). See: https://github.com/bcgit/bc-java/blob/master/pg/src/main/java/org/bouncycastle/openpgp/operator/jcajce/JcePublicKeyDataDecryptorFactoryBuilder.java#L262\r\n\r\nHowever, in a PGP message the multiprecision integer does not always have a size corresponding to the modulus of the RSA key (n), as all leading 0 bits are lost when converting the ciphertext to a multiprecision integer.\r\n\r\nThus when the first 8 bits of the ciphertext have a value of zero, the length of the MPI is no more (n + 2) bytes, but only (n - 1 + 2) bytes, meaning that when we send the MPI bytes without the first two bytes to the engine at decryption time, there are only (n - 1) bytes sent.\r\n\r\nIt means that we do not send the correct number of bytes to the Cipher engine to decrypt the key. The RSABlindedEngine that is used by Bouncy Castle works fine with it, but the RSA engine from our HSM provider throws an IllegalBlockSizeException.\r\n\r\nAccording to the RFC 8017 which I think is the correct one (PKCS #1: RSA Cryptography Specifications Version 2.2), the way it is handled by the library from our HSM provider seems to be expected. In section 7.2.2 about RSAES-PKCS1-v1_5 decryption that given the input:\r\n\r\n```\r\nInput:\r\n\r\n K recipient's RSA private key\r\n C ciphertext to be decrypted, an octet string of length k,\r\n where k is the length in octets of the RSA modulus n\r\n```\r\n\r\nThen the first step of the decryption is: \r\n```\r\n 1. Length checking: If the length of the ciphertext C is not k\r\n octets (or if k \u003c 11), output \"decryption error\" and stop.\r\n```\r\n\r\nWhich seems to explain why the library from our HSM provider throws an IllegalBlockSizeException when we only send a ciphertext of (n - 1) bytes to it.\r\n\r\nWe fixed the issue by manually adding the \"missing\" 0 bytes in the Cipher engine in JcePublicKeyDataDecryptorFactoryBuilder#decryptSessionData when we detect them, before adding the bytes from the MPI (there can be more than one leading 0 bytes in ciphertext after encrypting the session key with RSA, that will absent in the MPI).\r\n\r\nIn conclusion, I think that the Bouncy Castle library should be doing this padding with 0 bytes, as the size of the MPI is not guaranteed to be equal to the size of the RSA key, and trying to decrypt a ciphertext of a size not equal to the modulus of the key should not be allowed. What do you think about it ?","author":{"url":"https://github.com/lclement57","@type":"Person","name":"lclement57"},"datePublished":"2021-08-02T08:42:24Z","url":"https://github.com/bcgit/bc-java/issues/1003"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:f789a465-f7ee-4ef0-e997-9b8856c124ce |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 94D2:76282:D46A83:1258F14:6AFBED11 |
| html-safe-nonce | ff527b5c822246c31777aa519261a4621896584ea75882d9834b89d18639fd3a |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5NEQyOjc2MjgyOkQ0NkE4MzoxMjU4RjE0OjZBRkJFRDExIiwidmlzaXRvcl9pZCI6IjgzODUxMDU5OTcxMzA4ODY0MTciLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 284a14a73a8775ee8a3333edcafe07f11e14e7a751f742546281194b1ae5a97e |
| hovercard-subject-tag | issue:957908274 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/bcgit/bc-java/1003/issue_layout |
| twitter:image | https://opengraph.githubassets.com/0be53a3988e27c677ace5871f3e8e48feeee53b3d174396217ec19567fafa635/bcgit/bc-java/issues/1003 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/0be53a3988e27c677ace5871f3e8e48feeee53b3d174396217ec19567fafa635/bcgit/bc-java/issues/1003 |
| og:image:alt | Hello, we are using bcpg-jdk15on v1.69 to read OpenPGP messages encrypted using RSA keys. The private RSA key is stored in a Hardware Security Module, thus when we are performing the decryption of ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | lclement57 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 744d4381b1c4dbd6d8736c7d7e5bbe7dc11217584e457a3555020c631764e8ec |
| turbo-cache-control | no-preview |
| go-import | github.com/bcgit/bc-java git https://github.com/bcgit/bc-java.git |
| octolytics-dimension-user_id | 4566970 |
| octolytics-dimension-user_login | bcgit |
| octolytics-dimension-repository_id | 10416648 |
| octolytics-dimension-repository_nwo | bcgit/bc-java |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 10416648 |
| octolytics-dimension-repository_network_root_nwo | bcgit/bc-java |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | df859c48a4c8b4c0009c19bb4e82101aacb4b998 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width