summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEdoardo Morandi <morandidodo@gmail.com>2020-12-05 18:20:28 +0100
committerEdoardo Morandi <morandidodo@gmail.com>2020-12-05 18:20:28 +0100
commitd0676bf38d773989d60b342a9efc960c30a4b1fd (patch)
treec98740033dd8baa1a0623826aa8600c174efc5ad
parentb2410923fe57b41dd4e1cdfb7894aaf326d5f936 (diff)
downloadrust-openssl-d0676bf38d773989d60b342a9efc960c30a4b1fd.zip
Add encrypt_len and decrypt_len
-rw-r--r--openssl/src/encrypt.rs44
1 files changed, 44 insertions, 0 deletions
diff --git a/openssl/src/encrypt.rs b/openssl/src/encrypt.rs
index 37fc690c..24d35a85 100644
--- a/openssl/src/encrypt.rs
+++ b/openssl/src/encrypt.rs
@@ -117,6 +117,8 @@ impl<'a> Encrypter<'a> {
/// Performs public key encryption.
///
+ /// In order to know the size needed for the output buffer, use [`encrypt_len`](Encrypter::encrypt_len).
+ ///
/// This corresponds to [`EVP_PKEY_encrypt`].
///
/// [`EVP_PKEY_encrypt`]: https://www.openssl.org/docs/manmaster/man3/EVP_PKEY_encrypt.html
@@ -134,6 +136,26 @@ impl<'a> Encrypter<'a> {
Ok(written)
}
+
+ /// Gets the size of the buffer needed to encrypt the input data.
+ ///
+ /// This corresponds to [`EVP_PKEY_encrypt`] called with a null pointer as output argument.
+ ///
+ /// [`EVP_PKEY_encrypt`]: https://www.openssl.org/docs/manmaster/man3/EVP_PKEY_encrypt.html
+ pub fn encrypt_len(&self, from: &[u8]) -> Result<usize, ErrorStack> {
+ let mut written = 0;
+ unsafe {
+ cvt(ffi::EVP_PKEY_encrypt(
+ self.pctx,
+ ptr::null_mut(),
+ &mut written,
+ from.as_ptr(),
+ from.len(),
+ ))?;
+ }
+
+ Ok(written)
+ }
}
pub struct Decrypter<'a> {
pctx: *mut ffi::EVP_PKEY_CTX,
@@ -245,6 +267,8 @@ impl<'a> Decrypter<'a> {
/// Performs public key decryption.
///
+ /// In order to know the size needed for the output buffer, use [`decrypt_len`](Decrypter::decrypt_len).
+ ///
/// This corresponds to [`EVP_PKEY_decrypt`].
///
/// [`EVP_PKEY_decrypt`]: https://www.openssl.org/docs/manmaster/man3/EVP_PKEY_decrypt.html
@@ -262,6 +286,26 @@ impl<'a> Decrypter<'a> {
Ok(written)
}
+
+ /// Gets the size of the buffer needed to decrypt the input data.
+ ///
+ /// This corresponds to [`EVP_PKEY_decrypt`] called with a null pointer as output argument.
+ ///
+ /// [`EVP_PKEY_decrypt`]: https://www.openssl.org/docs/manmaster/man3/EVP_PKEY_decrypt.html
+ pub fn decrypt_len(&self, from: &[u8]) -> Result<usize, ErrorStack> {
+ let mut written = 0;
+ unsafe {
+ cvt(ffi::EVP_PKEY_decrypt(
+ self.pctx,
+ ptr::null_mut(),
+ &mut written,
+ from.as_ptr(),
+ from.len(),
+ ))?;
+ }
+
+ Ok(written)
+ }
}
#[cfg(test)]