{"action":"created","comment":{"url":"https://api.github.com/repos/sfackler/rust-openssl/pulls/comments/82104784","pull_request_review_id":3035065,"id":82104784,"diff_hunk":"@@ -162,6 +180,125 @@ impl RSA {\n         }\n     }\n \n+    pub fn max_data(&self) -> Option<u32> {\n+        // 41 comes from RSA_public_encrypt(3) for OAEP\n+        self.size().map(|len| len - 41)\n+    }\n+\n+    pub fn private_decrypt_with_padding(&self, from: &[u8], padding: EncryptionPadding) -> Result<Vec<u8>, ErrorStack> {\n+        assert!(self.d().is_some(), \"private components missing\");\n+        let k_len = self.size().expect(\"RSA missing an n\");\n+        let mut to: Vec<u8> = vec![0; k_len as usize];\n+\n+        unsafe {\n+            let enc_len = try_ssl_returns_size!(ffi::RSA_private_decrypt(from.len() as i32,\n+                                   from.as_ptr(),\n+                                   to.as_mut_ptr(),\n+                                   self.0,\n+                                   padding.openssl_padding_code()));\n+           to.truncate(enc_len as usize);\n+           Ok(to)\n+        }\n+    }\n+\n+    pub fn private_encrypt_with_padding(&self, from: &[u8], padding: EncryptionPadding) -> Result<Vec<u8>, ErrorStack> {\n+        assert!(self.d().is_some(), \"private components missing\");\n+        let k_len = self.size().expect(\"RSA missing an n\");\n+        let mut to:Vec<u8> = vec![0; k_len as usize];\n+\n+        assert!(from.len() < self.max_data().unwrap_or_else(|| 0) as usize);\n+\n+        unsafe {\n+            let enc_len = try_ssl_returns_size!(ffi::RSA_private_encrypt(from.len() as c_int,\n+                                   from.as_ptr(),\n+                                   to.as_mut_ptr(),\n+                                   self.0,\n+                                   padding.openssl_padding_code()));\n+           assert!(enc_len as u32 == k_len);\n+\n+           Ok(to)\n+        }\n+    }\n+\n+    pub fn public_decrypt_with_padding(&self, from: &[u8], padding: EncryptionPadding) -> Result<Vec<u8>, ErrorStack> {\n+        let k_len = self.size().expect(\"RSA missing an n\");\n+        let mut to: Vec<u8> = vec![0; k_len as usize];\n+\n+        unsafe {\n+            let enc_len = try_ssl_returns_size!(ffi::RSA_public_decrypt(from.len() as i32,\n+                                   from.as_ptr(),\n+                                   to.as_mut_ptr(),\n+                                   self.0,\n+                                   padding.openssl_padding_code()));\n+           to.truncate(enc_len as usize);\n+           Ok(to)\n+        }\n+    }\n+\n+    pub fn public_encrypt_with_padding(&self, from: &[u8], padding: EncryptionPadding) -> Result<Vec<u8>, ErrorStack> {\n+        let k_len = self.size().expect(\"RSA missing an n\");\n+        let mut to:Vec<u8> = vec![0; k_len as usize];\n+\n+        assert!(from.len() < self.max_data().unwrap_or_else(|| 0) as usize);\n+\n+        unsafe {\n+            let enc_len = try_ssl_returns_size!(ffi::RSA_public_encrypt(from.len() as c_int,\n+                                   from.as_ptr(),\n+                                   to.as_mut_ptr(),\n+                                   self.0,\n+                                   padding.openssl_padding_code()));\n+           assert!(enc_len as u32 == k_len);\n+\n+           Ok(to)\n+        }\n+    }\n+\n+    /**\n+      * Encrypts data with the public key, using OAEP padding, returning the encrypted data. The\n+      * supplied data must not be larger than max_data().\n+      */\n+     pub fn encrypt(&self, s: &[u8]) -> Result<Vec<u8>, ErrorStack> { self.public_encrypt_with_padding(s, EncryptionPadding::OAEP) }","path":"openssl/src/crypto/rsa.rs","position":106,"original_position":106,"commit_id":"f16cd5586f554530aa1511a7586ed7d3b4642235","original_commit_id":"f16cd5586f554530aa1511a7586ed7d3b4642235","user":{"login":"sfackler","id":1455697,"avatar_url":"https://avatars.githubusercontent.com/u/1455697?v=3","gravatar_id":"","url":"https://api.github.com/users/sfackler","html_url":"https://github.com/sfackler","followers_url":"https://api.github.com/users/sfackler/followers","following_url":"https://api.github.com/users/sfackler/following{/other_user}","gists_url":"https://api.github.com/users/sfackler/gists{/gist_id}","starred_url":"https://api.github.com/users/sfackler/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sfackler/subscriptions","organizations_url":"https://api.github.com/users/sfackler/orgs","repos_url":"https://api.github.com/users/sfackler/repos","events_url":"https://api.github.com/users/sfackler/events{/privacy}","received_events_url":"https://api.github.com/users/sfackler/received_events","type":"User","site_admin":false},"body":"I would just not have this and force people to specify their padding personally.","created_at":"2016-10-06T01:43:55Z","updated_at":"2016-10-06T01:44:23Z","html_url":"https://github.com/sfackler/rust-openssl/pull/461#discussion_r82104784","pull_request_url":"https://api.github.com/repos/sfackler/rust-openssl/pulls/461","_links":{"self":{"href":"https://api.github.com/repos/sfackler/rust-openssl/pulls/comments/82104784"},"html":{"href":"https://github.com/sfackler/rust-openssl/pull/461#discussion_r82104784"},"pull_request":{"href":"https://api.github.com/repos/sfackler/rust-openssl/pulls/461"}}},"pull_request":{"url":"https://api.github.com/repos/sfackler/rust-openssl/pulls/461","id":88061922,"html_url":"https://github.com/sfackler/rust-openssl/pull/461","diff_url":"https://github.com/sfackler/rust-openssl/pull/461.diff","patch_url":"https://github.com/sfackler/rust-openssl/pull/461.patch","issue_url":"https://api.github.com/repos/sfackler/rust-openssl/issues/461","number":461,"state":"open","locked":false,"title":"Added public/private encrypt/decrypt functions to RSA","user":{"login":"aoprisan","id":99908,"avatar_url":"https://avatars.githubusercontent.com/u/99908?v=3","gravatar_id":"","url":"https://api.github.com/users/aoprisan","html_url":"https://github.com/aoprisan","followers_url":"https://api.github.com/users/aoprisan/followers","following_url":"https://api.github.com/users/aoprisan/following{/other_user}","gists_url":"https://api.github.com/users/aoprisan/gists{/gist_id}","starred_url":"https://api.github.com/users/aoprisan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aoprisan/subscriptions","organizations_url":"https://api.github.com/users/aoprisan/orgs","repos_url":"https://api.github.com/users/aoprisan/repos","events_url":"https://api.github.com/users/aoprisan/events{/privacy}","received_events_url":"https://api.github.com/users/aoprisan/received_events","type":"User","site_admin":false},"body":"This is about issue #460 . I have ported partially the code in the commit to PKey, including tests. I added the following functions https://github.com/thommay/rust-openssl/blob/11e3b1b56317ca1e24cfc1c0a3805123fa73bfb8/openssl/src/crypto/pkey.rs#L393, though I am not sure all are needed. \r\nI also added two macros to handle OpenSSL methods which return size values (-1 is the error code).","created_at":"2016-10-05T11:42:53Z","updated_at":"2016-10-06T01:44:23Z","closed_at":null,"merged_at":null,"merge_commit_sha":"bdff3fb9bdc96184dced200550d04d84e4a12d38","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/sfackler/rust-openssl/pulls/461/commits","review_comments_url":"https://api.github.com/repos/sfackler/rust-openssl/pulls/461/comments","review_comment_url":"https://api.github.com/repos/sfackler/rust-openssl/pulls/comments{/number}","comments_url":"https://api.github.com/repos/sfackler/rust-openssl/issues/461/comments","statuses_url":"https://api.github.com/repos/sfackler/rust-openssl/statuses/f16cd5586f554530aa1511a7586ed7d3b4642235","head":{"label":"aoprisan:RSAEncDec","ref":"RSAEncDec","sha":"f16cd5586f554530aa1511a7586ed7d3b4642235","user":{"login":"aoprisan","id":99908,"avatar_url":"https://avatars.githubusercontent.com/u/99908?v=3","gravatar_id":"","url":"https://api.github.com/users/aoprisan","html_url":"https://github.com/aoprisan","followers_url":"https://api.github.com/users/aoprisan/followers","following_url":"https://api.github.com/users/aoprisan/following{/other_user}","gists_url":"https://api.github.com/users/aoprisan/gists{/gist_id}","starred_url":"https://api.github.com/users/aoprisan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aoprisan/subscriptions","organizations_url":"https://api.github.com/users/aoprisan/orgs","repos_url":"https://api.github.com/users/aoprisan/repos","events_url":"https://api.github.com/users/aoprisan/events{/privacy}","received_events_url":"https://api.github.com/users/aoprisan/received_events","type":"User","site_admin":false},"repo":{"id":69948904,"name":"rust-openssl","full_name":"aoprisan/rust-openssl","owner":{"login":"aoprisan","id":99908,"avatar_url":"https://avatars.githubusercontent.com/u/99908?v=3","gravatar_id":"","url":"https://api.github.com/users/aoprisan","html_url":"https://github.com/aoprisan","followers_url":"https://api.github.com/users/aoprisan/followers","following_url":"https://api.github.com/users/aoprisan/following{/other_user}","gists_url":"https://api.github.com/users/aoprisan/gists{/gist_id}","starred_url":"https://api.github.com/users/aoprisan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aoprisan/subscriptions","organizations_url":"https://api.github.com/users/aoprisan/orgs","repos_url":"https://api.github.com/users/aoprisan/repos","events_url":"https://api.github.com/users/aoprisan/events{/privacy}","received_events_url":"https://api.github.com/users/aoprisan/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/aoprisan/rust-openssl","description":"OpenSSL bindings for Rust","fork":true,"url":"https://api.github.com/repos/aoprisan/rust-openssl","forks_url":"https://api.github.com/repos/aoprisan/rust-openssl/forks","keys_url":"https://api.github.com/repos/aoprisan/rust-openssl/keys{/key_id}","collaborators_url":"https://api.github.com/repos/aoprisan/rust-openssl/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/aoprisan/rust-openssl/teams","hooks_url":"https://api.github.com/repos/aoprisan/rust-openssl/hooks","issue_events_url":"https://api.github.com/repos/aoprisan/rust-openssl/issues/events{/number}","events_url":"https://api.github.com/repos/aoprisan/rust-openssl/events","assignees_url":"https://api.github.com/repos/aoprisan/rust-openssl/assignees{/user}","branches_url":"https://api.github.com/repos/aoprisan/rust-openssl/branches{/branch}","tags_url":"https://api.github.com/repos/aoprisan/rust-openssl/tags","blobs_url":"https://api.github.com/repos/aoprisan/rust-openssl/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/aoprisan/rust-openssl/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/aoprisan/rust-openssl/git/refs{/sha}","trees_url":"https://api.github.com/repos/aoprisan/rust-openssl/git/trees{/sha}","statuses_url":"https://api.github.com/repos/aoprisan/rust-openssl/statuses/{sha}","languages_url":"https://api.github.com/repos/aoprisan/rust-openssl/languages","stargazers_url":"https://api.github.com/repos/aoprisan/rust-openssl/stargazers","contributors_url":"https://api.github.com/repos/aoprisan/rust-openssl/contributors","subscribers_url":"https://api.github.com/repos/aoprisan/rust-openssl/subscribers","subscription_url":"https://api.github.com/repos/aoprisan/rust-openssl/subscription","commits_url":"https://api.github.com/repos/aoprisan/rust-openssl/commits{/sha}","git_commits_url":"https://api.github.com/repos/aoprisan/rust-openssl/git/commits{/sha}","comments_url":"https://api.github.com/repos/aoprisan/rust-openssl/comments{/number}","issue_comment_url":"https://api.github.com/repos/aoprisan/rust-openssl/issues/comments{/number}","contents_url":"https://api.github.com/repos/aoprisan/rust-openssl/contents/{+path}","compare_url":"https://api.github.com/repos/aoprisan/rust-openssl/compare/{base}...{head}","merges_url":"https://api.github.com/repos/aoprisan/rust-openssl/merges","archive_url":"https://api.github.com/repos/aoprisan/rust-openssl/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/aoprisan/rust-openssl/downloads","issues_url":"https://api.github.com/repos/aoprisan/rust-openssl/issues{/number}","pulls_url":"https://api.github.com/repos/aoprisan/rust-openssl/pulls{/number}","milestones_url":"https://api.github.com/repos/aoprisan/rust-openssl/milestones{/number}","notifications_url":"https://api.github.com/repos/aoprisan/rust-openssl/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/aoprisan/rust-openssl/labels{/name}","releases_url":"https://api.github.com/repos/aoprisan/rust-openssl/releases{/id}","deployments_url":"https://api.github.com/repos/aoprisan/rust-openssl/deployments","created_at":"2016-10-04T08:59:10Z","updated_at":"2016-10-04T08:59:11Z","pushed_at":"2016-10-05T12:20:36Z","git_url":"git://github.com/aoprisan/rust-openssl.git","ssh_url":"git@github.com:aoprisan/rust-openssl.git","clone_url":"https://github.com/aoprisan/rust-openssl.git","svn_url":"https://github.com/aoprisan/rust-openssl","homepage":"","size":11930,"stargazers_count":0,"watchers_count":0,"language":"Rust","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"sfackler:master","ref":"master","sha":"623a34aa0245e7103ffe3adb98507d114ca27239","user":{"login":"sfackler","id":1455697,"avatar_url":"https://avatars.githubusercontent.com/u/1455697?v=3","gravatar_id":"","url":"https://api.github.com/users/sfackler","html_url":"https://github.com/sfackler","followers_url":"https://api.github.com/users/sfackler/followers","following_url":"https://api.github.com/users/sfackler/following{/other_user}","gists_url":"https://api.github.com/users/sfackler/gists{/gist_id}","starred_url":"https://api.github.com/users/sfackler/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sfackler/subscriptions","organizations_url":"https://api.github.com/users/sfackler/orgs","repos_url":"https://api.github.com/users/sfackler/repos","events_url":"https://api.github.com/users/sfackler/events{/privacy}","received_events_url":"https://api.github.com/users/sfackler/received_events","type":"User","site_admin":false},"repo":{"id":15500883,"name":"rust-openssl","full_name":"sfackler/rust-openssl","owner":{"login":"sfackler","id":1455697,"avatar_url":"https://avatars.githubusercontent.com/u/1455697?v=3","gravatar_id":"","url":"https://api.github.com/users/sfackler","html_url":"https://github.com/sfackler","followers_url":"https://api.github.com/users/sfackler/followers","following_url":"https://api.github.com/users/sfackler/following{/other_user}","gists_url":"https://api.github.com/users/sfackler/gists{/gist_id}","starred_url":"https://api.github.com/users/sfackler/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sfackler/subscriptions","organizations_url":"https://api.github.com/users/sfackler/orgs","repos_url":"https://api.github.com/users/sfackler/repos","events_url":"https://api.github.com/users/sfackler/events{/privacy}","received_events_url":"https://api.github.com/users/sfackler/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/sfackler/rust-openssl","description":"OpenSSL bindings for Rust","fork":false,"url":"https://api.github.com/repos/sfackler/rust-openssl","forks_url":"https://api.github.com/repos/sfackler/rust-openssl/forks","keys_url":"https://api.github.com/repos/sfackler/rust-openssl/keys{/key_id}","collaborators_url":"https://api.github.com/repos/sfackler/rust-openssl/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/sfackler/rust-openssl/teams","hooks_url":"https://api.github.com/repos/sfackler/rust-openssl/hooks","issue_events_url":"https://api.github.com/repos/sfackler/rust-openssl/issues/events{/number}","events_url":"https://api.github.com/repos/sfackler/rust-openssl/events","assignees_url":"https://api.github.com/repos/sfackler/rust-openssl/assignees{/user}","branches_url":"https://api.github.com/repos/sfackler/rust-openssl/branches{/branch}","tags_url":"https://api.github.com/repos/sfackler/rust-openssl/tags","blobs_url":"https://api.github.com/repos/sfackler/rust-openssl/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/sfackler/rust-openssl/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/sfackler/rust-openssl/git/refs{/sha}","trees_url":"https://api.github.com/repos/sfackler/rust-openssl/git/trees{/sha}","statuses_url":"https://api.github.com/repos/sfackler/rust-openssl/statuses/{sha}","languages_url":"https://api.github.com/repos/sfackler/rust-openssl/languages","stargazers_url":"https://api.github.com/repos/sfackler/rust-openssl/stargazers","contributors_url":"https://api.github.com/repos/sfackler/rust-openssl/contributors","subscribers_url":"https://api.github.com/repos/sfackler/rust-openssl/subscribers","subscription_url":"https://api.github.com/repos/sfackler/rust-openssl/subscription","commits_url":"https://api.github.com/repos/sfackler/rust-openssl/commits{/sha}","git_commits_url":"https://api.github.com/repos/sfackler/rust-openssl/git/commits{/sha}","comments_url":"https://api.github.com/repos/sfackler/rust-openssl/comments{/number}","issue_comment_url":"https://api.github.com/repos/sfackler/rust-openssl/issues/comments{/number}","contents_url":"https://api.github.com/repos/sfackler/rust-openssl/contents/{+path}","compare_url":"https://api.github.com/repos/sfackler/rust-openssl/compare/{base}...{head}","merges_url":"https://api.github.com/repos/sfackler/rust-openssl/merges","archive_url":"https://api.github.com/repos/sfackler/rust-openssl/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/sfackler/rust-openssl/downloads","issues_url":"https://api.github.com/repos/sfackler/rust-openssl/issues{/number}","pulls_url":"https://api.github.com/repos/sfackler/rust-openssl/pulls{/number}","milestones_url":"https://api.github.com/repos/sfackler/rust-openssl/milestones{/number}","notifications_url":"https://api.github.com/repos/sfackler/rust-openssl/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/sfackler/rust-openssl/labels{/name}","releases_url":"https://api.github.com/repos/sfackler/rust-openssl/releases{/id}","deployments_url":"https://api.github.com/repos/sfackler/rust-openssl/deployments","created_at":"2013-12-28T23:56:50Z","updated_at":"2016-09-29T13:22:46Z","pushed_at":"2016-10-05T11:42:54Z","git_url":"git://github.com/sfackler/rust-openssl.git","ssh_url":"git@github.com:sfackler/rust-openssl.git","clone_url":"https://github.com/sfackler/rust-openssl.git","svn_url":"https://github.com/sfackler/rust-openssl","homepage":"","size":12574,"stargazers_count":214,"watchers_count":214,"language":"Rust","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":158,"mirror_url":null,"open_issues_count":43,"forks":158,"open_issues":43,"watchers":214,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/sfackler/rust-openssl/pulls/461"},"html":{"href":"https://github.com/sfackler/rust-openssl/pull/461"},"issue":{"href":"https://api.github.com/repos/sfackler/rust-openssl/issues/461"},"comments":{"href":"https://api.github.com/repos/sfackler/rust-openssl/issues/461/comments"},"review_comments":{"href":"https://api.github.com/repos/sfackler/rust-openssl/pulls/461/comments"},"review_comment":{"href":"https://api.github.com/repos/sfackler/rust-openssl/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/sfackler/rust-openssl/pulls/461/commits"},"statuses":{"href":"https://api.github.com/repos/sfackler/rust-openssl/statuses/f16cd5586f554530aa1511a7586ed7d3b4642235"}}}}
{"actor":{"display_login":"sfackler"}}